Taliban gunmen and bombers using fake NATO identification attacked an entrance to the Afghan presidential palace in the heart of Kabul on Tuesday, just a week after insurgent leaders opened an office in Qatar for peace talks.
A nearby building known to house a CIA base also came under attack as explosions and gunfire erupted for more than an hour in an area close to heavily secured Western embassies and ministry buildings.
Three Afghan security guards and all five assailants were killed, the interior ministry said.
It was one of the most brazen assaults on the city since President Hamid Karzai narrowly escaped assassination in April 2008 when the Taliban attacked an annual military parade.
Karzai, who lives in the palace, was due to hold a press event in Kabul on Tuesday morning. Officials confirmed that he was in the building at the time of the attack but not in danger.
The strike also came during a visit to Kabul by US envoy James Dobbins after a dispute over the Taliban opening an office in Qatar as a first step towards peace talks ending 12 years of war.
The three guards were killed close to the Ariana hotel building, used as a CIA base since about 2002, but officials said neither the palace nor the CIA property were breached.
Two four-wheel-drive cars using fake badges from NATO's International Security Assistance Force (ISAF) tried to pass through a checkpoint to access the sprawling palace grounds at about 6:30 am (0200 GMT).
"The first vehicle was checked and let in, and as the second car tried to get in the guards became suspicious and tried to prevent it," Mohammad Daud Amin, the Kabul deputy police chief, told AFP.
"The clash started and the cars were detonated. All the attackers were killed."
Police said the cars had been fitted with radio antennae to make them look like ISAF vehicles and that the attackers were also wearing military uniforms.
The car bombs detonated near the CIA base, inside the first of several layers of outer checkpoints for the palace.
The challenge of securing peace in Afghanistan as NATO troops exit next year was underlined when a bomb killed eight women and one child travelling to celebrate a wedding in the southern province of Kandahar.
"A big group of attackers have struck against the CIA office as the main target and also the palace and the defence ministry nearby," Taliban spokesman Zabihullah Mujahid told AFP.
The last major attack in Kabul was on June 11 when the Taliban claimed responsibility for a suicide car bomb outside the Supreme Court that killed at least 15 civilians.
All roads to the presidential palace are permanently closed off, with multiple rings of heavy security around the complex keeping people far away.
Last week the Taliban opened an office in Qatar using the formal name of "Islamic Emirate Of Afghanistan" from the rebels' 1996-2001 government, and flew the white Taliban flag, displaying the trappings of power in a way that infuriated Karzai.
The president broke off Afghan-US talks on an agreement that would allow Washington to maintain soldiers in Afghanistan after 2014.
He has refused to send representatives to Qatar, but pressure is growing for a ceasefire and eventually a peace settlement ahead of the NATO withdrawal and a presidential election due in April.
About 100,000 foreign combat troops, 68,000 of them from the US, are due to exit by the end of 2014, and NATO formally transferred responsibility for nationwide security to Afghan forces a week ago.
When in power, the Taliban imposed a harsh version of Islamic law that banned television, music and cinema, stopped girls from going to school and forced woman to wear the all-covering burqa.
They were ousted in 2001 for sheltering Al-Qaeda in the aftermath of the 9/11 attacks, but launched a resilient and bloody insurgency against US-led NATO troops and the US-backed Afghan government.
US envoy Dobbins on Monday said the United States was "waiting to hear" whether the militants were committed to peace talks after opening the Qatar office.
"It doesn't seem like an entirely spurious effort on their part but whether they are prepared to participate... we just don't know,' he told reporters.
Also on Tuesday, officials in the eastern province of Paktika said four brothers were killed when they were driving home overnight and their car was attacked by Taliban gunmen.