George Linde took three wickets and Keshav Maharaj two as South Africa and Pakistan wrestled for the advantage on the third day of their second Test in Rawalpindi on Saturday.
Pakistan closed the day on 129-6 with an overall lead of 200 and four wickets intact on a tacky Rawalpindi stadium pitch which has cracks to help slow bowlers.
Linde (3-12) and Maharaj (2-74) rattled Pakistan's top order before Faheem Ashraf (29) and Mohammad Rizwan (28 not out) saved Pakistan's blushes during their 52-run sixth-wicket stand.
Linde removed Ashraf caught in covers, leaving Hasan Ali not out on nought when bad light ended the game.
The day could have ended in South Africa's domination had Ashraf and Rizwan not been dropped off the two spinners.
Ashraf was yet to get off the mark when Dean Elgar grassed an edge off Linde in slips while Rizwan was four when Rassie van der Dussen let off a sharp chance off Maharaj.
Pakistan would hope another 50 runs on day four could win them the match and the series, having won the first Test in Karachi by seven wickets.
Earlier, Hasan's 5-54 -- his second five-wicket haul in Tests -- restricted South Africa's first innings to 201, giving the home team a lead of 71.
Pakistan had made 272 in their first innings, but South Africa reduced them to 42-2 by tea.
Pakistan lost newcomer Imran Butt -- managing just 36 in four innings of his first series -- in the fifth over, leg-before to pacer Kagiso Rabada for a duck.
Butt's partner, Abid Ali fell caught behind off Maharaj for 13, ending up with 33 in the series to highlight Pakistan's top-order woes.
Azhar, on 11, survived a confident leg-before appeal off Maharaj only to see the referral showed it was the umpire's call.
It was Hasan who wrapped up South African innings after they added 95 runs in the morning.
Temba Bavuma fought a lone battle with 44 not out.
Resuming at 106-4 South Africa had hoped their skipper Quinton de Kock would build on his breezy overnight score of 24, but the left-hander played on to Shaheen in the third over of the day.
De Kock scored 29.
Bavuma and Wiaan Mulder frustrated Pakistan's pace-cum-spin attack during a 50-run sixth-wicket stand before disaster struck.
Mulder nudged a push to deep fine-leg but while returning for a second run failed to beat a Shaheen throw and was run out.
George Linde hit three boundaries and a six in his quickfire 21 but missed the line of a Hasan delivery and was bowled.
Hasan also had Maharaj (one) and Anrich Nortje (nought) to finish the innings.