Javid Ahmad is a nonresident fellow at the Atlantic Council and at West Point’s Modern War Institute. The views are his own. On Twitter: @ahmadjavid.

The brazen Taliban attack on an Afghan military base in northern Afghanistan on Friday that killed more than 130 Afghan soldiers represented a major intelligence failure. A photograph by the Taliban shows 10 assailants dressed in Afghan army uniforms who breached multiple security checkpoints at the base in military vehicles before going on a killing spree. In a Twitter message, a Taliban spokesperson claimed that four Afghan soldiers were Taliban infiltrators who helped the attackers in entering the base.

Such incidents, also known as insider attacks, typically involve a disgruntled Afghan soldier who facilitates the Taliban in infiltrating Afghan forces and staging attacks. Strangely, however, there has been little study of the causes or consequences of such attacks, which threaten to undo the fitful military progress made after nearly 16 years of Afghan war. In a new study, I find that insider attacks have increasingly become the preferred war-fighting tactic of the Taliban, a group that well understands how to apply limited resources for maximum effect.

In fact, despite a reputation for cultural myopia, the Taliban’s use of such attacks reveals that it understands Afghan military and intelligence gaps far better than its opponents do. Using Afghan soldiers to plot large-scale attacks on Afghan or U.S. troops is in effect a “cultural weapon” that targets a key weakness in Afghan and the U.S. civil-military apparatus: a deep aversion to casualties. Since 2007, such insider attacks have killed more than 157 NATO personnel, mostly U.S. troops, and more than 550 Afghan soldiers. At times, the attacks have also served as an effective tool for the Taliban to drive a wedge between Afghan and U.S. forces. In 2012 and 2013, NATO forces even discontinued joint operations below the battalion level with Afghan troops and temporarily stopped training Afghan forces to avoid the risk of being killed by disgruntled Afghan soldiers.

The Taliban employs at least four methods to stage such attacks. First, the group infiltrates Afghan forces as a way not only to target Afghan and NATO personnel, but also to collect tactical intelligence and undermine cooperation. Most Taliban infiltration is in the ranks of the poorly vetted Afghan local police, a network of U.S.-funded village-defense units that receive basic training from U.S. forces to protect their communities. Second, the Taliban recruits rogue and aggrieved Afghan soldiers to either facilitate insider attacks or personally conduct attacks on behalf of the insurgency. Taliban members often coerce Afghan soldiers through financial incentives, ideological pressure and intimidation — including the kidnapping of family members — as well as influences through tribal networks. Third, the Taliban has become adept at impersonation, with fighters posing as Afghan police or army personnel to evade security inspection and barriers while infiltrating checkpoints. The group frequently uses counterfeit Afghan military uniforms and identity cards to penetrate Afghan security ranks. At times, these tactics have involved some level of facilitation or complicity by rogue Afghan soldiers, especially in providing identification cards and access to bases. And, fourth, the Taliban exploits those disgruntled Afghan soldiers who are personally motivated to stage or facilitate an attack for reasons ranging from grievances rooted in personal clashes and insults to cultural misunderstandings, disrespect for religious beliefs or local norms, civilian casualties and miscommunication. At times, combat stress is a factor. But the Taliban often exploits these weaknesses to convince resentful soldiers to either join forces with insurgents or react with their guns.

Nonetheless, as counterinsurgency is an intelligence-driven effort, the Afghan intelligence services bear the responsibility for such Taliban infiltration. Unfortunately, Afghanistan’s National Directorate of Security (NDS), the country’s main intelligence agency, faces dangerous gaps in intelligence collection and analysis, as well as effective leadership to confront the Taliban’s new tactics. The NDS’s human intelligence presence across Afghanistan’s 34 provinces is negligible; its surveillance and reconnaissance capabilities are deficient; and its sabotage activities are minuscule. Meanwhile, Afghan intelligence and security operations are often not in sync, and tactical operations typically lack intelligence-gathering components that produce information for future operations. Despite infighting and divisions in the insurgency, the NDS has not done a good job in infiltrating the Taliban ranks.

The NDS places little emphasis on its analytical functions to efficiently process and analyze intelligence collection. At times, the NDS’s ownership of information has been questionable. It is also highly politicized, factional, corrupt and a place where personal agendas and biases warp the use of intelligence. Interference by political strongmen and former intelligence chiefs in security affairs is a regular occurrence, which has led to intelligence leakages and has compromised operational security.

In the end, Afghan forces are operators and not intelligence collectors, and they rely heavily on the NDS for intelligence. But because of critical shortfalls in Afghan intelligence, it put the onus on the NDS for failing to prevent Friday’s deadly attack. These operational gaps resulted in a staggering 15,000 casualties among Afghan forces in the first eight months of 2016 alone, in addition to a 2.4 percent attrition rate each month, which weakens the military’s operational posture.

Serious measures should include a modest surge in the NDS’s presence in the field and improvements in the agency’s collection and analytical capabilities. Ultimately, if the machinery of Afghan intelligence were effective, the Taliban’s infiltration would be categorically minimized and so would the odds of resentful Afghan soldiers becoming willing insiders for insurgent missions to kill Afghan forces.