South Africa was penalised five runs for ball tampering as Pakistan fought grimly to avoid a heavy loss to the Proteas on an eventful third day of the second cricket test on Friday.

Pakistan was reeling at 132-4 in its second innings at stumps with captain Misbah-ul-Haq unbeaten on 42 and Asad Shafiq not out on 28. Pakistan still needs 286 runs to avoid an innings defeat, at least.

“We’re definitely in a strong position to win this test,” South Africa batsman AB de Villiers said.

“We just need to be patient tomorrow, build the blocks again and work really hard to get that wicket No. 5 and ... then we can feel we can run through them.”

Earlier, South Africa was bowled out for 517 for a monumental lead of 418 runs after Pakistan was bowled out for only 99. The top-ranked Proteas, humbled inside four days in the first test, look set to avenge that and force a drawn series.

But Faf du Plessis stained the dominant South Africans when he was caught on television rubbing the ball over a zip on his thigh. The umpires called over captain Graeme Smith, changed the ball, and the rare five-run penalty was given to Pakistan as extras.

Pakistan began its second bat calamitously, as fast bowlers Dale Steyn and Vernon Philander had Pakistan at 2-2 in two pacey overs before lunch, removing Shan Masood and Khurram Manzoor for ducks.

It was Manzoor’s first pair in a test. The opener has struggled since scoring 142 in Pakistan’s seven-wicket win in the first test at Abu Dhabi last week.

But Pakistan hung in for the rest of the day, losing just two more wickets in the next two sessions. Azhar Ali was trapped leg before wicket for 19 by JP Duminy in his first over before tea.

Duminy could have dismissed Younis Khan in the last over before tea, but Jacques Kallis dropped a low catch in the slips.

But Khan (36) couldn’t hold his patience as he needlessly danced down the wicket, missed the line of legspinner Imran Tahir’s delivery and was clean bowled at 70-4.

Misbah and Shafiq then endured with their gritty unbeaten 56-run stand. The captain survived a television referral late in the afternoon off Tahir.

“Our batting is letting us down, the pitch is not as bad as it’s been projected, it’s still good for batting,” offspinner Saeed Ajmal said.

“The first task (tomorrow) will be to avoid an innings defeat.”

Resuming at the overnight 460-4, South Africa added 57 more before it was bowled out for 517. Ajmal (6-151) claimed four wickets on Friday.

South Africa lost overnight big scorers Graeme Smith (234) and AB de Villiers (164) inside the first half an hour and Ajmal mopped up the tail quickly.

Pacer Mohammad Irfan (3-102) had de Villiers caught behind to end a marathon fifth-wicket stand of 338 runs, and Smith was caught by Khan in the lone slip off Ajmal after spending 10 1/2 hours at the crease to take the game away from Pakistan. Smith’s marathon knock came off 388 balls and he hit 16 fours.

De Villiers frustrated Pakistan for over six hours and made them pay heavily after wicketkeeper Adnan Akmal dropped him off the first ball he faced off Irfan on Wednesday before falling to the same combination. De Villiers faced 274 balls, hit 17 fours and a six in sharing South Africa’s all-time best fifth-wicket stand with Smith.

Adnan again frustrated Irfan when he dropped a regulation catch off Smith, but the captain departed two balls later as Khan became the top fielder for Pakistan in test cricket with 94 catches.

Pakistan also suffered a blow when it had to take Irfan out of the attack soon after he clipped the bails of Duminy (7). Since umpire Rod Tucker warned the fast bowler for extending his follow through too far for the second time, Irfan couldn’t bowl for the remainder of the innings.

Ajmal then mopped the tail by taking the last three wickets in the space of 12 runs.

More In: Cricket | Sport