Temba Bavuma, who resumed on 15, fought a lone battle to remain unbeaten on 44 off 138 balls before Hasan wrapped up the innings by claiming the last two wickets of successive deliveries.
The diminutive Bavuma successfully overturned a leg before decision against him in the fourth over of the day, but could not get enough support from the lower order.
Bavuma and Wiaan Mulder (33) dug in for 90 minutes and raised a half century stand before Pakistan struck with two quick wickets.
Mulder was shaping up well against the spinners as he lofted Yasir Shah for a six over long off and also swept well against left-arm spinner Nauman Ali before he was run-out while going for a needless second run.
George Linde scored a run-a-ball 21 with three fours and a six before he was deceived by Hasan with a slower delivery and was clean bowled.
Resuming on 106-4, captain Quinton de Kocks poor run of form in the series continued when he could add only five to his overnight score of 24 and was clean bowled by Shaheen Afridi (1-37) in the third over of the day.
De Kock, who hit five boundaries late on the first day, attempted an over-ambitious drive against Afridi too soon and dragged the ball back onto his stumps.
Pakistan should have got Maharaj without scoring before lunch but Yasir Shah dropped him in the second slip before Nauman missed a sitter shortly after the break. Umpire Ahsan Raza erred for the second time in the day when Maharaj successfully went for lbw television review after the on-field umpire had ruled him out off Hasan as South Africa moved to 188-7 at lunch.
Hasan returned after lunch and cleaned bowled both Maharaj and No. 11 Andrich Nortje and Rabada was run out.
Pakistans problems up front continued in the series after it lost openers Abid Ali for 13 and Imran Butt for zero before going to tea at 42-2 to lead by 113 runs.
Kagiso Rabada trapped Butt lbw before Pakistan had scored in the second innings. The right-handed opener, who made his test debut at Karachi where Pakistan won by seven wickets, could score only 36 runs in four innings after Shan Masood was dropped for the home series.
Abid also had below-par scores of 4, 10 and 6 in the previous three test innings before he was caught behind off left-arm spinner Keshav Maharaj for his top score in the series.
Babar Azam (13) fell leg before wicket for the third time in the series against Maharaj as the Pakistan skipper failed to read the arm ball of the left-arm spinner which didnt turn.
Linde then reduced Pakistan to 76-5 when he had Azhar Ali lbw and Fawad Alam offered a regulation bat and pad catch.
Pakistan could have been 76-7, but first Ashraf was dropped by Elgar off Linde in the slip and then Rassie van der Dussen couldnt hold onto a sharp chance off Rizwans bat at silly point.
Both batsmen counterattacked thereafter with Ashraf smashing Maharaj for three boundaries in an over before the left-hander threw away his wicket.
More AP sports: https://apnews.com/apf-sports and https://twitter.com/AP_Sports