FARAH, Afghanistan — A police officer guarding the outskirts of this city remembers the call from his commander, warning that hundreds of Talibanfighters were headed his way.
"Within half an hour, they attacked," recalled Officer Najibullah Amiri, 35. The Taliban swarmed the farmlands surrounding his post and seized the western riverbank here in Farah, the capital of the province by the same name.
It was the start of a three-week siege in October, and only after American air support was called in to end it and the smoke cleared did Afghan security officials realize who was behind the lightning strike: Iran.
Four senior Iranian commandos were among the scores of dead, Afghan intelligence officials said, noting their funerals in Iran. Many of the Taliban dead and wounded were also taken back across the nearby border with Iran, where the insurgents had been recruited and trained, village elders told Afghan provincial officials.
The assault, coordinated with attacks on several other cities, was part of the Taliban's most ambitious attempt since 2001 to retake power. But it was also a piece of an accelerating Iranian campaign to step into a vacuum left by departing American forces — Iran's biggest push into Afghanistan in decades.
President Trump recently lamented that the United States was losing its 16-year war in Afghanistan, and threatened to fire the American generals in charge.
There is no doubt that as the United States winds down the Afghan war — the longest in American history, and one that has cost half a trillion dollars and more than 150,000 lives on all sides — regional adversaries are muscling in.
Saudi Arabia and Pakistan remain the dominant players. But Iran is also making a bold gambit to shape Afghanistan in its favor.
Over the past decade and a half, the United States has taken out Iran's chief enemies on two of its borders, the Taliban government in Afghanistan and Saddam Hussein in Iraq. Iran has used that to its advantage, working quietly and relentlessly to spread its influence.
In Iraq, it has exploited a chaotic civil war and the American withdrawal to create a virtual satellite state. In Afghanistan, Iran aims to make sure that foreign forces leave eventually, and that any government that prevails will at least not threaten its interests, and at best be friendly or aligned with them.
One way to do that, Afghans said, is for Iran to aid its onetime enemies, the Taliban, to ensure a loyal proxy and also to keep the country destabilized, without tipping it over. That is especially true along their shared border of more than 500 miles.
But fielding an insurgent force to seize control of a province shows a significant — and risky — escalation in Iran's effort.
"Iran does not want stability here," Naser Herati, one of the police officers guarding the post outside Farah, said angrily. "People here hate the Iranian flag. They would burn it."
Iran has conducted an intensifying covert intervention, much of which is only now coming to light. It is providing local Taliban insurgents with weapons, money and training. It has offered Taliban commanders sanctuary and fuel for their trucks. It has padded Taliban ranks by recruiting among Afghan Sunni refugees in Iran, according to Afghan and Western officials.
"The regional politics have changed," said Mohammed Arif Shah Jehan, a senior intelligence official who recently took over as the governor of Farah Province. "The strongest Taliban here are Iranian Taliban."
Iran and the Taliban — longtime rivals, one Shiite and the other Sunni — would seem to be unlikely bedfellows.
Iran nearly went to war with the Taliban when their militias notoriously killed 11 Iranian diplomats and an Iranian government journalist in fighting in 1998.
After that, Iran supported the anti-Taliban opposition — and it initially cooperated with the American intervention in Afghanistan that drove the Taliban from power.
But as the NATO mission in Afghanistan expanded, the Iranians quietly began supporting the Taliban to bleed the Americans and their allies by raising the cost of the intervention so that they would leave.
Iran has come to see the Taliban not only as the lesser of its enemies but also as a useful proxy force. The more recent introduction of the Islamic State, which carried out a terrorist attack on Iran's parliament this year, into Afghanistan has only added to the Taliban's appeal.
In the empty marble halls of the Iranian Embassy in Kabul, Mohammad Reza Bahrami, the ambassador, denied that Iran was supporting the Taliban, and emphasized the more than $400 million Iran has invested to help Afghanistan access ports on the Persian Gulf.
"We are responsible," he said in an interview last year. "A strong accountable government in Afghanistan has more advantages for strengthening our relations than anything."
But Iran's Foreign Ministry and its Islamic Revolutionary Guards Corps act as complementary arms of policy — the first openly sowing economic and cultural influence, and the second aggressively exerting subversive force behind the scenes.
Iran has sent squads of assassins, secretly nurtured spies and infiltrated police ranks and government departments, especially in western provinces, Afghan officials say.
Even NATO's top commander in Afghanistan at the time, Gen. Sir David Richards of Britain, discovered that Iran had recruited his interpreter, Cpl. Daniel James, a British-Iranian citizen. Corporal James was sentenced to 10 years in prison for sending coded messages to the Iranian military attaché in Kabul during a tour of duty in 2006.
More recently, Iran has moved so aggressively in bulking up the Taliban insurgency that American forces rushed to Farah Province a second time in January to stave off a Taliban attack.
"The Iranian game is very complicated," said Javed Kohistani, a military analyst based in Kabul.
Having American forces fight long and costly wars that unseated Iran's primary enemies has served Tehran's interests just fine. But by now, the Americans and their allies have outlasted their usefulness, and Iran is pursuing a strategy of death by a thousand cuts "to drain them and cost them a lot."
An Ambitious Expansion
The depth of Iran's ties to the Taliban burst unexpectedly into view last year. An American drone struck a taxi on a desert road in southwestern Pakistan, killing the driver and his single customer.
The passenger was none other than the leader of the Taliban, Mullah Akhtar Muhammad Mansour. A wanted terrorist with an American bounty on his head who had been on the United Nations sanctions list since before 2001, Mullah Mansour was traveling without guards or weapons, confident and quite at home in Pakistan.
The strike exposed for the second time since the discovery of Osama bin Laden in the Pakistani hill town of Abbottabad the level of Pakistan's complicity with wanted terrorists. It was the first time the United States had conducted a drone attack in Pakistan's Baluchistan Province, a longtime sanctuary for the Taliban but until then off limits for American drones because of Pakistani protests.
Yet even more momentous was that Mullah Mansour was returning from a trip to Iran, where he had been meeting Iranian security officials and, through Iran, with Russian officials.
Afghan officials, Western diplomats and security analysts, and a former Taliban commander familiar with Mullah Mansour's inner circle confirmed details of the meetings.
Both Russia and Iran have acknowledged that they have held meetings with the Taliban but maintain that they are only for information purposes.
That the Taliban leader was personally developing ties with both Iran and Russia signaled a stunning shift in alliance for the fundamentalist Taliban movement, which had always been supported by the Sunni powers among the Arab gulf states and Pakistan.
But times were changing with the American drawdown in Afghanistan, and Mullah Mansour had been seeking to diversify his sources of money and weapons since taking over the Taliban leadership in 2013. He had made 13 trips to Dubai, United Arab Emirates, and one to Bahrain, his passport showed, but also at least two visits to Iran.
Set on expanding the Taliban's sway in Afghanistan, he was also preparing to negotiate an end to the war, playing all sides on his terms, according to both Afghan officials with close knowledge of the Taliban and the former Taliban commander close to Mullah Mansour's inner circle.
It was that ambitious expansionism that probably got him killed, they said.
"Mansour was a shrewd politician and businessman and had a broader ambition to widen his appeal to other countries," said Timor Sharan, a former senior analyst of the International Crisis Group in Afghanistan who has since joined the Afghan government.
Mullah Mansour had been tight with the Iranians since his time in the Taliban government in the 1990s, according to Mr. Kohistani, the military analyst. Their interests, he and other analysts and Afghan officials say, overlapped in opium. Afghanistan is the world's largest source of the drug, and Iran the main conduit to get it out.
Iran's border guards have long fought drug traffickers crossing from Afghanistan, but Iran's Revolutionary Guards and the Taliban have both benefited from the illicit trade, exacting dues from traffickers.
The main purpose of Mullah Mansour's trips to Iran was tactical coordination, according to Bruce Riedel, a former C.I.A. analyst and fellow at the Brookings Institution in Washington. At the time, in 2016, the Taliban were gearing up for offensives across eight Afghan provinces. Farah was seen as particularly ripe fruit.
Iran facilitated a meeting between Mullah Mansour and Russian officials, Afghan officials said, securing funds and weapons from Moscow for the insurgents.
Mullah Mansour's cultivation of Iran for weapons was done with the full knowledge of Pakistan, said the former Taliban commander, who did not want to be identified since he had recently defected from the Taliban.
"He convinced the Pakistanis that he wanted to go there and get weapons, but he convinced the Pakistanis that he would not come under their influence and accept their orders," he said.
Pakistan had also been eager to spread the political and financial burden of supporting the Taliban and had encouraged the Taliban's ties with Iran, said Haji Agha Lalai, a presidential adviser and the deputy governor of Kandahar Province.
On his last visit, Mullah Mansour traveled to the Iranian capital, Tehran, to meet someone very important — possibly Iran's supreme leader, Ayatollah Ali Khamenei, said the former Taliban commander, who said he had gleaned the information from members of Mullah Mansour's inner circle.
Mullah Mansour stayed for a week, also meeting with a senior Russian official in the town of Zahedan, said Mr. Lalai, who spoke with relatives of the Taliban leader.
He was almost certainly negotiating an escalation in Iranian and Russian assistance before his death, Mr. Lalai and other Afghan officials said, pointing to the increase in Iranian support for the Taliban during his leadership and since.
But the meeting with the Russians was apparently a step too far, Afghan officials say. His relations with Iran and Russia had expanded to the point that they threatened Pakistan's control over the insurgency.
The United States had been aware of Mullah Mansour's movements, including his ventures into Iran, for some time before the strike and had been sharing information with Pakistan, said Seth G. Jones, associate director at the RAND Corporation. Pakistan had also provided helpful information, he added. "They were partly supportive of targeting Mansour."
Gen. John Nicholson, the United States commander of coalition forces in Afghanistan, said President Barack Obama had approved the strike after Mullah Mansour failed to join peace talks being organized in Pakistan.
Col. Ahmad Muslem Hayat, a former Afghan military attaché in London, said he believed that the American military had been making a point by striking Mullah Mansour on his return from Iran.
"When they target people like this, they follow them for months," he said. "It was smart to do it to cast suspicions on Iran. They were trying to create a gap between Iran and the Taliban."
But if that was the intention, Mr. Lalai said, it has not succeeded, judging by the way the new Taliban leader, Mawlawi Haibatullah Akhundzada, has picked up his predecessor's work.
"I don't think the contact is broken," he said. "Haibatullah is still reaching out to Iran. They are desperately looking for more money if they want to extend the fight."
Intrigue in 'Little Iran'
There is no place in Afghanistan where Iran's influence is more deeply felt than the western city of Herat, nearly in sight of the Iranian border.
Two million Afghans took refuge in Iran during the Soviet invasion in the 1980s. Three million live and work in Iran today. Herat, sometimes called "Little Iran," is their main gateway between the countries.
People in Herat speak with Iranian accents. Iranian schools, colleges and bookshops line the streets. Women wear the head-to-foot black chador favored in Iran. Shops are full of Iranian sweets and produce.
But even as the city is one of Afghanistan's most decorous and peaceful, an air of intrigue infuses Herat.
The city is filled with Iranian spies, secret agents and hit squads, local officials say, and it has been plagued by multiple assassinations and kidnappings in recent years. The police say Iran is funding militant groups and criminal gangs. A former mayor says it is sponsoring terrorism.
Iran is constantly working in the shadows. The goal, Afghan officials say, is to stoke and tip local power struggles in its favor, whether through bribery, infiltration or violence.
One day in January, Herat's counterterrorism police deployed undercover officers to stake out the house of one of their own men. Two strangers on a motorbike seemed to be spying on the house, so secret agents were sent out to spy on the spies.
Within hours, the police had detained the men and blown their cover: They were Iranian assassins, according to the Afghans. The passenger was armed with two pistols.
Forensics tests later found that one of the guns had been used in the murder of an Iranian citizen in Herat 10 months earlier, police officials said.
The two Iranians are still in Afghan custody and have yet to be charged. They have become a source of contention between Iran and Afghanistan.
Iran disowned them, pointing to their Afghan identity cards, but Afghan officials paraded them on television, saying they were carrying false papers and had admitted to being sent by Iran as a hit squad.
The Afghan police say they have arrested 2,000 people in counterterrorism operations in Herat over the last three years. Many of them, they say, are armed insurgents and criminals who reside with their families in Iran and enter Afghanistan to conduct dozens of attacks on police or government officials.
Iran is set on undermining the Afghan government and its security forces, and the entire United States mission, and maintaining leverage over Afghanistan by making it weak and dependent, Afghan officials say.
"We caught a terrorist who killed five people with an I.E.D.," a senior police officer said, referring to a roadside bomb. "We released a boy who was kidnapped. We defused an I.E.D. in the city."
Flicking through photographs on his phone, he pointed to one of a man in a mauve shirt. "He was convicted of kidnapping five people." Much of the kidnapping is criminal, for ransom, but at least some of it is politically motivated, he added.
The 33-year-old, English-speaking Farhad Niayesh, a former mayor of Herat, is even more blunt, and exasperated. He says the Iranians use their consulate in the city as a base for propaganda and "devising terrorist activities."
"Iran has an important role in terrorist attacks in Herat," Mr. Niayesh said. "Three or four Iranians were captured. They had a plan against government officials who were not working in their interest."
Members of Parliament and security officials say Iran bribes local and central government officials to work for it, offering them 10 to 15 Iranian visas per week to give to friends and associates. Afghans visit to conduct business, receive medical care and see family.
The Afghan police have uncovered cases of even deeper infiltration, too. A female member of the Afghan police service was sentenced to death, accused of being a secret Iranian agent, after fatally shooting an American trainer in the Kabul Police Headquarters in 2012.
"Our western neighbor is working very seriously," said the senior Afghan police official in Herat who requested anonymity because of the nature of his work. " We have even found heavy artillery to be used against the city."
Iran is supporting multiple anti-government militant groups in half a dozen western provinces, he said. The Afghan police, despite a lack of resources, are working to dismantle them.
"The same sort of people are still in the city," he added. "They are doing their work, and we are doing our work."
Double-Edged Soft Power
Afghans dream of restoring their landlocked, war-torn country to the rich trading center it was in days of old, when caravans carried goods along the Silk Road from China to Europe, and people and ideas traveled along the same route.
If Tehran has its way, the modern Silk Road will once again run across Afghanistan's western border, and proceed through Iran. At least that is the ambition.
On one side of the Afghan border, India has been building a road through southwestern Afghanistan to allow traders to bypass Pakistan, which has long restricted the transit of Afghan goods.
Tehran's goal is to join that route on the Iranian side of the border with road and rail links ending at the port of Chabahar on the Persian Gulf.
"We said that Afghanistan would not be landlocked anymore and we would be at Afghanistan's disposal," said Mr. Bahrami, the Iranian ambassador in Kabul, stressing that Iran's contribution to the Afghan road was not stalled even by its economic difficulties under sanctions.
But Iran's economic leverage comes at a price.
Afghan officials say Iran's support of the Taliban is aimed in part at disrupting development projects that might threaten its dominance. The Iranian goal, they contend, is to keep Afghanistan supplicant.
The biggest competition is for water, and Afghans have every suspicion that Iran is working to subvert plans in Afghanistan for upstream dams that could threaten its water supply.
Iran has raised the issue of the dams in bilateral meetings, and President Hassan Rouhani recently criticized the projects as damaging to the environment.
With the upheaval of 40 years of coups and wars in Afghanistan, large-scale development plans, like hydroelectric projects, have largely been stalled since the 1970s. Even after international assistance poured into Afghanistan after 2001, internal and external politics often got in the way.
But President Ashraf Ghani, determined to generate economic growth, made a priority of completing the Salma dam in Herat Province, and has ordered work on another dam at Bakhshabad, to irrigate the vast western province of Farah.
In Farah, despite the two calamitous Taliban offensives on the provincial capital in October and January, the Bakhshabad dam is the first thing everyone mentions.
"We don't want help from nongovernmental organizations or from the government," said Mohammed Amin, who owns a flourishing vegetable farm, growing cucumbers and tomatoes under rows of plastic greenhouses. "We in Farah don't want anything. Just Bakhshabad."
Afghanistan's lack of irrigation makes it impossible to compete with Iranian produce prices, something Bakhshabad could solve, he said.
The project is still only in the planning stage. But the dam, with its promise of irrigation and hydroelectricity for a population lacking both, is a powerful dream — if Iran does not thwart it.
"The most important issue is water," Mr. Lalai, the presidential adviser, said of relations with Iran. "Most of our water goes to our neighbors. If we are prosperous, we might give them less."
Peace or Proxy War?
The death of Mullah Mansour removed Iran's crucial link to the Taliban. But it has also fractured the Taliban, spurring a number of high-level defections and opening opportunities for others, including Iran, to meddle.
An overwhelming majority of Taliban blame Pakistan for Mullah Mansour's death. The strike deepened disillusionment with their longtime Pakistani sponsors.
About two dozen Taliban commanders, among them senior leaders who had been close to Mullah Mansour, have since left their former bases in Pakistan.
They have moved quietly into southern Afghanistan, settling back in their home villages, under protection of local Afghan security officials who hope to encourage a larger shift by insurgents to reconcile with the government.
Those with family still in Pakistan live under close surveillance and control by Pakistani intelligence, said the former Taliban commander, who recently abandoned the fight and moved his family into Afghanistan to escape reprisals.
He said he had become increasingly disaffected by Pakistan's highhanded direction of the war. "We all know this is Pakistan's war, not Afghanistan's war," he said. "Pakistan never wanted Afghanistan to be at peace."
The question now: Does Iran?
Citing the threat from the Islamic State as an excuse, Iran may choose, with Russian help, to deepen a proxy war in Afghanistan that could undermine an already struggling unity government.
Or it could encourage peace, as it did in the first years after 2001, for the sake of stability on at least one of its borders, prospering with Afghanistan.
For now, Iran and Russia have found common cause similar to their partnership in Syria, senior Afghan officials and others warn.
Emboldened by their experience in Syria, they seem to be building on their partnership to hurt America in Afghanistan, cautioned the political analyst Mr. Sharan.
As American forces draw down in Afghanistan, jockeying for influence over the Taliban is only intensifying.
"Pakistan is helping the Taliban straightforwardly," said Mr. Jehan, the former Afghan intelligence official who is now governor of Farah. "Russia and Iran are indirectly helping the Taliban. We might come to the point that they interfere overtly.
"I think we should not give them this chance," he added. "Otherwise, Afghanistan will be given up to the open rivalry of these countries."
The former Afghan foreign minister, Rangin Dadfar Spanta, warned that the country risked being pulled into the larger struggle between Sunni powers from the Persian Gulf and Shiite Iran.
"Afghanistan should keep out the rivalry of the regional powers," he said. "We are vulnerable."
Some officials are optimistic that Iran is not an enemy of Afghanistan, but the outlook is mixed.
"There is a good level of understanding," Abdullah Abdullah, the Afghan government's chief executive, said of relations with Iran.
"What we hear is that contacts with the Taliban are to encourage them to pursue peace rather than military activities," he said.
Mohammad Asif Rahimi, the governor of Herat, warned that if Farah had fallen to the Taliban, the entire western region would have been laid open for the insurgents.
Iran's meddling has now grown to the extent that it puts the whole country at risk of a Taliban takeover, not just his province, he said.
But it could have been prevented, in the view of Mr. Sharan.
"The fact is that America created this void," he said. "This vacuum encouraged countries to get involved. The Syria issue gave confidence to Iran and Russia, and now that confidence is playing out in Afghanistan."