Rabat, Morocco — At the center of dismay over the deaths of four Americans in an attack on the US consulate in Benghazi yesterday is a basic question: How could this happen?

Whether the incident that killed Ambassador Chris Stevens stemmed from protests over a film denigrating the prophet Muhammad turned into rocket-fire or was a pre-planned attack disguised by the protests remains unclear. But the attack points to underlying problems: While post-Qaddafi Libya works to build a new state, authorities are struggling to manage militias, weapons, and a surge in violent Islamism.

Tripoli’s weakness was laid bare last month when interior minister Fawzi Abdel-Aal said the government would not risk a fight with ultraconservative Salafi Muslims who methodically bulldozed a Tripoli mosque whose graves and Sufi shrines they called blasphemous.

That incident, and others like it, show a growing pattern of armed groups flouting the government, says John Hamilton, a director at Cross-Border Information, a British risk assessment firm. “This pattern needs to be addressed instantly.

Yesterday’s attack raises the stakes for the fledgling post-Qaddafi state. A new government to be named in the coming weeks will be hard-pressed to get a grip on security to reassure both Libyans and foreigners.

“The pressure will be immense,” says Mr. Hamilton. “Allowing a US ambassador to be killed is about as bad an event as you could possibly imagine.”

Containing the threat, not resolving it

The roots of Libya’s security woes go back at least to February 2011, when revolt erupted in Benghazi against Muammar Qaddafi. The war that brought down Mr. Qaddafi’s dictatorship also delivered much of his arsenal into the hands of local militias.

Many are still unwilling to give up their weapons. A drive through Libya remains a tour of checkpoints, with armed young men stationed at the outskirts of many cities and towns.

Interim authorities outgunned by militias must instead work with them to keep the peace. Those in larger cities are often cooperative, but smaller groups can prove difficult, says Henry Smith, a Middle East and North Africa analyst at Control Risks, another British risk assessment firm.

Some are organized on religious lines, some around small towns, and some are just criminal gangs,” he says. “The fear is that as some Islamist brigades have moved slowly into the formal security institutions, unclear political agendas may have moved in with them.”

While authorities have managed to contain most violence so far – often inter-militia or tribal squabbles – religiously-driven attacks present a more serious problem, Hamilton says.

Last month’s mosque attack was among several recent assaults in Libya on sites, including shrines and graves, associated with mystical Sufi practices that most Salafis consider un-Islamic.

Salafi militants have also been blamed for a bomb attack against the US consulate in Benghazi in June, and for a string of bomb and rocket attacks over the summer against Libya offices of the International Committee of the Red Cross.

Those incidents mirror a spike in Salafi violence across North Africa in places where war or revolution have weakened security services. Tunisian Salafis have rioted over art they call blasphemous, while Salafi militias in Mali have destroyed Sufi shrines.

'There will be bad days'

Most North Africans spurn such violence and the ideology that fuels it. In Libya, first place in July elections went to a coalition of parties that rejected Islamism of any sort, while also avoiding a secularist label.

Meanwhile, the motives of Libya’s self-styled Salafis may be mixed, says Smith. Some may also feel anger over unemployment and a sense that revolution has failed to improve lives.

“I’m skeptical of saying the incident yesterday points to a heightened terrorist threat in Libya,” he says.

However, yesterday’s attack may delay the forming of a new government as politicians review their horse-trading in light of the new urgency surrounding security questions, he says.

Such attacks may at least “affect where the political process goes: how inclusive it will be, whether the demands of fringe groups will be met,” says Henry Smith, a Middle East and North Africa analyst at Control Risks, another British risk assessment firm.

For Sami Zaptia, a Libyan business consultant, that process goes hand in hand with establishing order and growing a vibrant economy. He wants continued US engagement in Libya as state institutions take shape.

“There will be bad days, and this was the worst so far,” says Mr. Zaptia, who knew Ambassador Stevens and helped guide a US trade delegation to Libya last April. “What we need now is for our friends to stand with us.”

The US has vowed to help Libyan authorities track down those responsible for yesterday’s attack. According to US officials cited by CNN, drone flights are planned over northeastern Libya to look for encampments of violent Islamists suspected by US authorities.

Isobel Coleman, a senior fellow at the Council on Foreign Relations in the US, said in a phone briefing with reporters today that it’s unlikely the drone flights are a precursor to a US strike. Instead, the intelligence gathered by the drones is likely to be shared with Libyan authorities to aid their own search.

As for the efforts of Libyan authorities, “it is time for the gloves to come off,” Zaptia says. “Without law and order we can do nothing.”