Dozens were wounded when a car bomb exploded overnight near a security building and a courthouse in a Cairo suburb. (Reuters)

The Islamic State claimed responsibility for a massive car bombing on Thursday that targeted security forces here in the Egyptian capital in what the militant group deemed as retaliation for a deadly government operation against its fighters.

The Interior Ministry said six police officers were injured in the predawn attack on a branch of the National Security Agency, Egypt’s domestic spy service, in a Cairo suburb. The Health Ministry said 29 people were injured.

The blast, which could be heard for miles, has raised fears of stepped-up insurgent attacks in the capital. Islamist militants have waged an increasingly deadly campaign against security forces since a military coup ousted Islamist President Mohamed Morsi in 2013.

Among the extremist factions is a group based in the restive Sinai Peninsula that has pledged loyalty to the Islamic State. The organization has carried out several car bombings, including one last year targeting the security directorate in Cairo.

Map: What a year of Islamic State terror looks like

Also Thursday, the Palestinian militant group Hamas said gunmen abducted four Palestinians after they crossed into Sinai from the Hamas-controlled Gaza Strip late Wednesday. Hamas has clashed with Sinai-based extremists in recent months.

In a message posted online describing the Cairo blast, the Islamic State said that “soldiers of the caliphate were able to strike the state security building in the area of Shubra al-Kheima with a car full of explosives.”

The bomb-rigged vehicle was parked outside the security facility, which faces a busy street about six miles from the iconic Tahrir Square. The driver fled before the explosion, the Interior Ministry said. Residents said the driver jumped out of the car while stopped at a traffic light and hopped onto a waiting motorcycle.

“We woke up to rumbling in our house, the windows breaking and things collapsing,” said Sawsan Ahmed, 58, who lives next-door to the headquarters. “We thought it was an earthquake and ran out of the house. It’s the most terrifying situation I’ve ever been in.”

The Islamic State said Thursday’s attack was in retaliation for “Arab Sharkas,” a reference to the site of a deadly shootout between militants and security forces in a village close to Shubra al-Kheima last year.

The hours-long gun battle was touched off by a raid on a timber workshop thought to be linked to militants who later forged ties with the Islamic State. Militants and senior army officers were among those killed.

In May, Egypt executed six men who it said belonged to the militant cell in the Arab Sharkas incident. Human Rights Watch has said that three of those men were already in custody when the gun battle occurred.

Egyptian police and security forces patrol where a bomb detonated in a Cairo neighborhood Thursday. (Oliver Weiken/European Photopress Agency)

On Thursday, a handful of police officers guarded the site of the explosion, which tore through concrete blast walls and left an enormous crater. The security building’s facade was blown off in some places and the interior mangled.

Nearby, cars were totaled, shop fronts had collapsed and livelihoods were destroyed.

“This government, from the youngest police officer to the ministers, is a bunch of incompetents,” said 60-year-old Mohsen Mohamed, whose parked taxi was torn apart by the blast. The bomb also damaged his apartment nearby.

Police “block off the streets around the building here all the time, and yet they are unprepared for something like this,” he said.

As he spoke, two construction workers attempted to rewire the traffic light damaged by the blast.

“We just installed this!” one of the workers said of the traffic lights, which are rare on Egypt’s run-down and chaotic roads.

In Sinai, more than 200 miles from Cairo, gunmen seized four men from a bus filled with Palestinian travelers on Wednesday night, Hamas said. In a statement Thursday, the Islamist militant movement called the abductions a serious “security breach.”

The incident underscored the lawlessness in Sinai, a stronghold of Islamic State loyalists. Hamas, an offshoot of Egypt’s banned Muslim Brotherhood, has also clashed with Islamic State supporters in Gaza.

“It’s possible a Wilayat Sinai faction is trying to pressure Hamas” with the kidnappings, said Zack Gold, a visiting fellow and Sinai expert at the Institute for National Security Studies in Tel Aviv. Wilayat Sinai is the name of the Islamic State affiliate in the peninsula.

Still, the abductions are likely to complicate Egypt’s bid to contain a growing insurgency.

The government has focused almost solely on defeating the extremists militarily, Gold said.

But “over the past year, the terror threat has spread in both geographical and ideological dimensions,” he said, “with attacks throughout the country.”

Read more:

Syria says Islamic State militants beheaded a noted archaeologist

Map: The world according to the Islamic State

This town has resisted the Islamic State for 18 months. But food is running low.

Today's coverage from Post correspondents around the world