West Midlands Police said an unmarked car was targeted in Birmingham just after midnight in the early hours of Tuesday.
The force said shots were discharged at the vehicle in the Ladywood area of the city, near St Mark’s Crescent and Crosby Close.
None of the officers were hit.
Five people were arrested on suspicion of attempted murder and remain in police custody.
A car thought to have been used in the shooting was recovered in West Bromwich.
Assistant chief constable Sarah Boycott, of West Midlands Police, said: “Officers were working on a proactive operation to tackle gun crime and keep people safe and it is unusual that they should come under attack in this way in the line of that duty.
“There is a minority within our society who don’t give a second thought to illegally carrying and using guns; and although these are significant arrests we will continue to take action against those who think it’s ok to carry guns on our streets.”
She added: “We appreciate there will be concerns within our communities but we have increased our patrols in neighbourhoods to offer reassurance.
“This includes firearms officers being on mobile patrol and standby 24/7.
“The role of the public will play an important role in helping us to overcome this difficult period.
“They can be our eyes and ears in helping to get guns in the wrong hands off our streets.”