South Africa trails Pakistan by 166 runs in a match it must win to draw the series.
Pakistan fast bowler Hasan Ali (2-29) grabbed his wickets off successive deliveries and Ashraf had Faf du Plessis (17) caught behind to leave South Africa struggling in its first innings on day 2.
Left-arm spinner Nauman Ali (1-19), who bagged seven wickets in his debut test at Karachi last week at the age of 34, got the key wicket of Aiden Markram (32) with South Africa troubled by both pace and spin.
Temba Bavuma survived a dropped catch and remained unbeaten on 15. Captain Quinton de Kock struck three successive boundaries off Yasir Shah for his unbeaten 24 off 11 balls.
Earlier, pacer Anrich Nortje picked up 5-56 to bowl out Pakistan half an hour before tea.
Ashraf anchored Pakistan's lower-order resistance with a top score of 78 not out, including 12 fours, before Nortje wrapped up the innings by claiming the last two wickets in three balls.
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.
"I think with the hard ball there is something for the fast bowlers," Nortje said. He called his performance "definitely special."
Nortje claimed two wickets in the first session, including Babar's off the second ball of the day after Pakistan resumed on 145-3. Azam played a loose cut shot and 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 22-3 with Fawad Alam on the first day before rain and a wet outfield wiped out the last session on Thursday.
Alam, who scored 45, fell to Bavuma's splendid direct throw at the non-striker's end from short mid-wicket as left-handed batsman 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 top-edged pull went straight to Kagiso Rabada's hands at fine leg before Ashraf raised his fourth 50 in his eighth test match.
Ashraf completed his half century off 97 balls with a straight-driven boundary off Rabada just before lunch and two balls later pulled the fast bowler to midwicket for his ninth boundary.
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 3-1/2 hour knock. He prolonged Pakistan's resistance after lunch, adding 30 runs with Yasir (8) and 21 with Nauman Ali (8) before Nortje claimed the last two wickets in three balls.
"I still believe I am a bowling all-rounder and batting is my plus point," Ashraf said.
Hasan, returning to test cricket in this series after a two-year absence, had Dean Elgar (15) caught behind in the last over before tea and then followed it up by uprooting Rassie van der Dussen's off stump in the next delivery.
Du Plessis denied Hasan a hat trick with an edged boundary through slips but his poor run continued when Ashraf got a thin edge in the last session dominated by Pakistan.
Pakistan leads the two-match series 1-0 after beating the Proteas in the first test by seven wickets at Karachi.