Hasan Ali took 5-54 as Pakistan ended day three of the second Test against South Africa in Rawalpindi in a strong position.
The seamer starred as South Africa were bowled out for 201, having lost their last five wickets for 37 runs.
Pakistan then recovered from 76-5, thanks to a half-century stand between Mohammad Rizwan and Faheem Ashraf.
The hosts finished the day on 129-6 in the two-Test series, a second-innings lead of 200.
Having started the day on 106-4, Quinton de Kock's knock of 29 from 20 balls came to an end after the Proteas captain was bowled by seamer Shaheen Afridi.
A 50-run partnership between Temba Bavuma and Wiaan Mulder offered the tourists some hope before Mulder was run out by Shaheen for 33.
That wicket prompted a South Africa collapse, courtesy of some fine reverse-swing bowling from Hasan, with Bavuma left stranded on 44 not out.
Pakistan's top order struggled in reply as captain Babar Azam was dismissed by spinner Keshav Maharaj for the third time in four innings after he was out lbw for 8.
Rizwan and Faheem steadied the ship for Pakistan before Faheem fell to George Linde - who took 3-12 - for 29 late in the day.
Hasan, yet to score, will rejoin Rizwan, unbeaten on 28, on day four as Pakistan - who won the first Test by seven wickets - look to extend their first-innings lead.