Paceman Anrich Nortje picked up 5-56 as South Africa bowled out Pakistan for 272 in the second Test in Rawalpindi on Friday.
Faheem Ashraf anchored the hosts' lower-order resistance with a top score of 78 not out, including 12 fours, before Nortje wrapped up the innings late in the second session on day 2.
Fast bowler Hasan Ali struck off successive deliveries to leave the Proteas in early trouble at 2-26 at tea.
Hasan, returning to Test cricket in this series after a two-year absence, had Dean Elgar (15) caught behind in the last over before the break and then followed it up by uprooting Rassie van der Dussen's off stump with his next delivery.
Earlier, Nortje bowled with menacing pace - on a wicket which has eased considerably for batting - to bag his third five-wicket haul in his 10th Test match.
He claimed two wickets in the first session, including Babar Azam's off the second ball of the day after Pakistan resumed on 3-145. Azam played a loose cut shot and Faf du Plessis held onto a head-high catch at second slip.
Babar couldn't add to his overnight 77. He had helped to revive Pakistan innings from 3-22 with Fawad Alam on the first day before rain and a wet outfield wiped out the last session.
Alam, who scored 45, fell to Temba Bavuma's splendid direct throw at the non-striker's end from short mid-wicket as Alam went for a needless run.
Ashraf then provided the bulk of the scoring and shared a 41-run stand with Mohammad Rizwan (18) before Nortje struck immediately with the second new ball.
Nortje tested Rizwan with a short-pitched delivery and the batter's top-edged pull went straight to Kagiso Rabada at fine leg before Ashraf raised his fourth half-century (off 97 balls) in his eighth Test match.
Ashraf survived an early lbw TV referral against left-arm spinner Keshav Maharaj (3-90), and he also needed brief treatment after Rabada's short-pitched delivery with the second new ball struck the left-hander's right arm.
Ashraf showed lot of patience in his more than three-and-a-half-hour knock. He prolonged Pakistan's resistance after lunch, adding 30 runs with Yasir Shah (8) and 21 with Nauman Ali (8) before Nortje claimed the last two wickets in three balls.
Pakistan leads the two-match series 1-0 after winning the first Test by seven wickets at Karachi.