A stunning spell of fast bowling from Duanne Olivier turned the first Test at Centurion in South Africa's favour after Pakistan collapsed in their second innings from 100-1 at tea to 190 all out at stumps.
Olivier took five for 59 in the second innings.
South Africa need 149 runs to win. Pakistan, however, believe they have enough runs on the board to take the series opener.
"Have we got a chance? Without a shadow of doubt we can defend 149," said Shan Masood, who top-scored for Pakistan with 65 and found himself fighting virtually a lone battle.
"We had them 43 for four in the first innings," said Masood. "Ideally we would have liked to set them 200 but if we can get early wickets and contain the runs we can put pressure on them. We are going to give it our all."
Temba Bavuma, whose 53 was a major contributor to South Africa taking a 42-run lead in the first innings, said there was no sense of complacency in his dressing room.
"The first 15 or 20 overs will be quite key. We would like to be as positive as we can," he said.
"Pakistan have a very good bowling attack so it won't be easy. If we can dominate those first 15 or 20 overs it will make things a lot easier."
Bavuma said batting conditions were tricky.
"I never felt in. You've got to exercise a lot of patience but when a bad ball presents itself you want to be in a position to put it away."
Masood and Imam-ul-Haq, who made 57, took Pakistan to 100 for one at tea, at which stage the tourists were well-placed to set a challenging target.
Olivier took two wickets in four balls immediately after the interval and went on to complete match figures of 11 for 96 following his six for 37 in the first innings.
Imam was bowled off an inside edge in the first over after tea and Masood quickly found himself running out of partners before he was ninth out after a fighting 120-ball innings which included seven fours.
"There was a bit of frustration because you hope one of your partners will bat well but the ball is deviating and the bounce is not consistent," said Masood.
"The positive is that this game has ebbed and flowed both ways. If we can win the crucial moments we have a very good chance of winning the series."
Kagiso Rabada and Dale Steyn supported Olivier with three and two wickets respectively.
It was the second successive day on which 15 wickets fell.
Bavuma (53) and Quinton de Kock (45) were mainly responsible for South Africa squeezing out a 42-run first innings lead despite good bowling from Mohammad Amir (four for 62) and teenager Shaheen Shah Afridi, who took four for 64.