Afghan National Army soldiers stand guard in Laghman province on August 9, 2012. An Afghan soldier turned his weapon on NATO allies on August 9 as one of the latest incidents in a series of so-called green-on-blue attacks, the US-led International Security Assistance Force said. (WASEEM NAKZAD/AFP/GETTY IMAGES)

The teenage assailant who killed three Marines last week on a U.S. military base in southern Afghanistan had easy access to the weapons arsenal of the Afghan police. He was in near-constant contact with U.S. troops, often when they were without their guns and body armor.

But although Aynoddin, 15, lived among American and Afghan security forces, he was not a soldier or a police officer. He had never been vetted. According to U.S. and Afghan officials, his role on base was hardly formal: He was the unpaid, underage personal assistant of the district police chief.

Officials would later learn that the quiet, willowy boy was also working for the insurgency.

As U.S. troops depart from Afghanistan, American military strategy increasingly hinges on small teams of advisers who live and work with Afghan soldiers and police officers. But those teams — like the one that Aynoddin attacked last week — put themselves at the mercy of often-shoddy Afghan security standards, which permit individuals to live on shared bases without proper scrutiny.

There have been 28 so-called “insider attacks” this year, resulting in the deaths of 39 coalition troops — a full 13 percent of those killed in Afghanistan in 2012. Among the dead are 23 Americans. The attacks continued Friday, when an Afghan Local Police officer shot and killed two U.S. troops during a training exercise in the western province of Farah.

Attacks on NATO troops by Afghan security forces

NATO officials have long claimed that the majority of such attacks are the products of personal disputes. But last week’s shooting was believed to have come from a different, more troubling source: a young Taliban convert who exploited his access to carry out what insurgent leader Mohammad Omar boasted Thursday is a deliberate plan to drive a wedge between foreign and Afghan forces.

Aynoddin should never have been on the base in the first place, because Afghan and U.S. security standards would not have allowed it. But those standards are often violated — especially by the country’s nascent police force.

“We have to have better leadership out of our Afghan leaders. There are some things they need to step up to the plate and do now better than they’ve done,” said Marine Maj. Gen. Charles M. Gurganus, the top U.S. commander in southwestern Afghanistan. “They need to be looking in the eyes of their subordinate commanders and holding them accountable for these people who are in and out of police stations.”

Aynoddin, who, like many Afghans, uses only one name, was in high school when he started work for Garmsir’s police chief, Sarwar Jan, in the southern province of Helmand. He spent his days cooking for Jan and cleaning up after him. Both Afghans and Americans knew him as the boss’s “tea boy.”

At 8:30 p.m. on Aug. 10,, three weeks after he arrived at a joint U.S.-Afghan base called Delhi, the boy stole a Kalashnikov rifle that was lying in an unlocked barracks, according to police officers on the base. He walked to a gym where four unarmed Marines were exercising and held down the trigger until no bullets were left. When he was finished, three Marines were dead and one was badly injured.

Then Aynoddin walked out of the gym, rifle still in hand, and bragged of his accomplishment: “I just did jihad,” he said to nearby police officers, according to several men who were on the scene. “Don’t you want to do jihad, too? If not, I will kill you.” The officers approached him slowly and then tore the gun from his hands.

“The look in his face was angry, like if he had more bullets he would have killed us as well,” said Janan, one of the Garmsir officers.

When U.S. officials discovered that an unvetted 15-year-old had been allowed access to the base — and the weapons strewn around it — they were furious.

“These were jihad-motivated executions,” said a Western official in Afghanistan with knowledge of the incident. To suggest otherwise would be “profoundly distasteful and insulting to the Marines who died.”

Jan, the police chief, said Aynoddin was “given” to him as a personal assistant by a local elder and Afghan Local Police commander. Jan assumed the boy was a police officer, he said, even though he wasn’t wearing a uniform.

Police officers in Garmsir say Aynoddin skulked around the base, keeping to himself. Afghan and NATO officials now speculate that the boy was waiting for the right opportunity to attack foreign troops. He chose a moment when the Marines were unarmed and the Afghan police officers were gathered to break their daily Ramadan fast. A classified investigation into the incident is ongoing, but some officials with knowledge of the attack spoke to The Washington Post on the condition of anonymity.

The Marines who Aynoddin killed were part of a U.S. advisory team attached to the Afghan Uniformed Police, a branch of the national police. In Garmsir, a district once on the front lines of NATO’s surge to vanquish the Taliban from southern Afghanistan, such advisory teams now play a crucial role in the swift U.S. drawdown. The Marines in Garmsir dropped from a battalion to a company this spring, consolidating their footprint from over 60 bases to three.

With far fewer troops, the Marines shifted from a combat role to a mission devoted largely to training their Afghan counterparts — a preview of how U.S. involvement in Afghanistan will evolve over the next two years. In Garmsir, that change in mission meant getting closer to Afghan soldiers and police, trusting that physical proximity would strengthen the relationship rather than damage it.

There were 32 men on the Marines’ police training team in Garmsir. Not only did they work every day at the district police headquarters, they lived there as well, on a part of the Delhi base separated from the U.S. operations center by a small checkpoint. The Marines knew there were risks involved in that living arrangement, but they said it was crucial to building trust.

“The Afghan police and the Marines had a good relationship,” said one Marine on the team, who arrived at the grisly scene shortly after the attack. “A few of the Afghan police even broke into tears afterwards when they realized what had happened.”

Still, there had been rifts. Several Marines said they made it clear to Jan that it was not acceptable to bring underage boys onto the base. But they watched as the police chief disobeyed that rule, they said.

For years, Marines and other U.S. officials said, they had heard local residents complain about Jan’s poorly kept secret — that he invited boys to bases often shared with U.S. troops and engaged in sexual misconduct. Officials say it was the primary reason he was dismissed from a previous posting in Now Zad, another district in Helmand. The officials spoke on the condition of anonymity because publicly accusing the police chief could make it difficult to work in southern Afghanistan.

Jan denies that he has done anything improper. “I’m not fond of young boys,” he said after being released from Afghan custody this week. He was later detained again by Afghan intelligence agents.

In southern Afghanistan, it is not uncommon for men to sexually exploit boys. The practice, called bacha bazi, has been on the rise since the Taliban regime collapsed.

Since the Garmsir incident, top U.S. commanders in Afghanistan and defense officials in Washington have held several meetings to discuss what might be done to prevent insider attacks from occurring, according to senior defense officials. Commanders have agreed to add a counterintelligence specialist at the battalion level to help detect Taliban infiltrators. They are also considering ways to improve the Afghan vetting process.

But in the meantime, the problem continues. In addition to the two killings in western Afghanistan on Friday, an Afghan soldier wounded three NATO troops in a separate incident in southern Afghanistan.

Those attacks came a day after Omar, the Taliban leader, issued a statement lauding insurgents who “have cleverly infiltrated in the ranks of the enemy.” The infiltrators, Omar said, “are able to enter bases, offices and intelligence centers of the enemy. Then, they easily carry out decisive and coordinated attacks.”

Javed Hamdard in Kabul and Greg Jaffe in Washington contributed to this report.