Faheem resistance, Hasan strikes put Pakistan on top despite Nortje five-for
Pakistan's bowlers inflicted damage on South Africa's top-order to leave the tourists four down in reply to their first-innings total of 272 on day two of the second Test in Rawalpindi.
Hasan Ali dismissed Dean Elgar and Rassie van der Dussen on successive deliveries before the tea interval, following which Faheem Ashraf and Nauman Ali struck with vital blows to hand Pakistan the advantage.
Earlier, South African paceman Anrich Nortje grabbed his third five wicket haul with 5-56 to restrict Pakistan's total.
The 27-year-old took the prized wicket of Babar Azam off the second ball of the day before wrapping Pakistan's innings half an hour before tea.
Nortje bowled with fire and aggression on a flat, brownish Rawalpindi stadium pitch which is suited more to spin.
Keshav Maharaj finished with 3-90.
Despite South Africa's controlled bowling, the day belonged to Faheem, who conjured a brilliant knock of 78 not out studded with 12 hits to the boundary -- his fourth Test half-century.
Pakistan had looked to Azam and Fawad Alam to build on their partnership of 123 after rain cut short the first day to 58 overs.
Azam slashed at a short delivery from Nortje and was smartly snapped head high by Faf du Plessis in the second slip for his overnight score of 77.
Azam's dismissal was the start of a mini-collapse which saw Pakistan lose two wickets in the space of 34 balls with the addition of just four runs after resuming at 145-3.
The other overnight batsman Alam was run out after he tried to steal a quick single with a push to cover but failed to beat a direct throw from Temba Bavuma at the non-striker's end.
Alam could only add three to his overnight score of 42.
Azam, who was looking set for his sixth Test century, had hit 12 boundaries off 127 balls.
Ashraf then added 41 with Rizwan but once South Africa took the second new ball Nortje struck again, having Rizwan caught off a miscued hook for 18.
When South Africa came out to bat, they deployed an attacking approach which saw them feast on the wayward lines of Shaheen Afridi and Hasan with the new ball.
Soon, Hasan found the breakthrough by coming around the wicket to knock over Elgar and bamboozled van der Dussen with a splendid in-ducker.
Faheem had du Plessis play loose shot off a wide delivery and Nauman's gripping length ball resulted in Markram top-edging a half-hearted attempt at clearing the man positioned at midwicket.
In the dying moments of the day, skipper Quinton de Kock launched a counterattack to give his team a bit of an impetus heading into the third day.