After losing Mosul and vast territories in Iraq and Syria, the Islamic State of Iraq and the Levant (ISIL) is probably hoping to move to Afghanistan.

It has substantially increased its attacks in the past two years and recruited hundreds of additional supporters. It is targeting mainly the Shias and the Hazara minority and in parts joining forces with the Taliban thereby changing the dynamics of the war in Afghanistan. By doing so, it is provoking Iran and possibly Russia to get involved. The Persian-speaking Shia Hazara, estimated to make up about nine percent of Afghanistan's population, have close ties to Iran.

ISIL (also known as ISIS) could take advantage of another "lost" American war and another failing state, as it did in Iraq. Afghanistan's complex set of security and political problems are providing the armed group the chaos conditions that it needs to prosper.

In its latest attack on a village in the northern province of Sar-e Pul, described as a war crime by the Afghan president, Ashraf Ghani, government officials said ISIL joined forces with the Taliban in the brutal killing of more than 50 civilians, mainly Shia Hazaras.

Only one week earlier the twin attacks claimed by ISIL on the Iraqi embassy in Kabul and the Shia mosque in the western city of Herat, with over 120 casualties, appeared carefully chosen to take revenge against both Iraqi and Iranian forces for the loss of its stronghold, Mosul.

These were the tail end of six attacks this year targeting Shia mosques. Four of the attacks occurred in Herat and ISIL claimed responsibility for two. In 2016, there were four separate attacks against Shia mosques and ISIL claimed responsibility for two. In July last year, ISIL's twin explosions tore through a demonstration by the Shia Hazara minority in Kabul killing at least 80 people and wounding more than 230.

READ MORE: Who are the Hazaras?

ISIL seems intentionally to target Iran's interests in Afghanistan: Shia mosques, the Hazara minority, and the city of Herat -with a large population of Tajiks- have all received the bulk of Iran's financial and political support. Iran has spent millions of dollars in aid and reconstruction projects building a 400km highway and a major railway linking Herat to Iran's Khorasan province. Most of the work has been carried out by Khatam ul Anbya Construction which is the economic arm of Iran's Revolutionary Guards Corp (IRGC). These transport links have greatly enhanced trade, especially for Iran.

Given his strong partnership with the US, President Ghani would never willingly invite Russia or Iran for military cooperation as, for example, Iraq and Syria did.

Herat is located at the heart of the 1,000km border between the two countries, which share a rich historical and literary heritage. Iran values this heritage beyond its push to influence politics in Afghanistan. Moreover, Iran regards itself as the custodian of Shia rights around the world, and would not take ISIL attacks lightly especially since they follow the twin attacks in Iran two months ago after which Iran arrested several suspected ISIL operatives. Last week, Iran announced that it arrested further 27 suspected ISIL members.

Iran's strong condemnation of ISIL attacks in Afghanistan came with an offer of "collective security guarantee". The national security chief, Ali Shamkhani, said Tehran would "expand regional cooperation especially with the Afghan government to jointly confront this dangerous threat".

In April, when Shamkhani met the Afghan national security adviser, Hanif Atmar, he condemned "the attempts by certain regional states" to upset security in Afghanistan "as part of a broader scheme to dispatch the defeated terrorists from Iraq and Syria to Afghanistan". His reference can only be to Saudi Arabia, which as a staunch ally of Pakistan, has reportedly been funding Taliban through "private or covert channels".

So, Iran regards these advances in the context of the Iran-Saudi regional rivalries and Sunni advances against Shias, while rejecting reports that it is funding the Taliban.

Equally concerned is Russia about the 2,000km border Central Asian republics have with Afghanistan. Russia is aware that since its military operations in Syria, thousands of ISIL fighters are regrouping in Afghanistan to take revenge. According to intelligence by the Collective Security Treaty Organization (CSTO), ISIL's activity in Afghanistan has grown by one-third this year compared with 2016, with around 1,000 Central Asian operatives working along the border areas.

OPINION: ISIL won't get very far in Afghanistan - for now

In February, the Russian Foreign Ministry expressed the necessity of strengthening "military-technical cooperation with Kabul". Zamir Kabulov, the Russian president's special envoy to Afghanistan warned that if the situation on the border between Tajikistan and Afghanistan deteriorates "capabilities of the CSTO may be used under a due appeal of the Tajik side".

In April, Russia proposed an international conference on Afghanistan inviting all neighbours including Iran, Pakistan and India but US government did not attend citing Russian military assistance to Taliban. Russia rejected the claim.

The meeting in mid-April between the US Secretary of State Rex Tillerson and his Russian counterpart, Sergey Lavrov, for improving mutual military understanding also came to nothing after the US imposed sanctions on Russia.

Given his strong partnership with the US, President Ghani would never willingly invite Russia or Iran for military cooperation as, for example, Iraq and Syria did. Yet, he is aware that the US administration is paralysed by its own internal squabbles over Afghanistan.

Moreover, President Ghani is himself facing arguably the most difficult time of his leadership with internal challenges from three former strongmen demanding security reforms, his own National Unity Government in disarray, and civil society accusing him of inaction. That is to say nothing of the ongoing corruption, unemployment, war fatigue, and a nation traumatised by the highest ever number of civilian casualties.

President Ghani's legitimacy has not as yet been eroded. Nevertheless, the danger signs are there of a failing or fragile state that would provide suitable ground for the regrouping of ISIL and Taliban.

The argument that Taliban would not allow ISIL to gain ground in Afghanistan is increasingly invalidated by facts on the ground. The more likely scenario is that more Taliban commanders would follow the example of Sher Mohammad Ghazanfar in Sar-e Pul, and pledge allegiance to ISIL. "There are no strict ideological distinctions between them so they build bridges when it helps them both," said one Afghan security source who cited three other joint operations.

The US and NATO chief command, General John W Nicholson warned Pentagon in December that political instability in Afghanistan would have two outcomes: the convergence of terrorist groups in Afghanistan, and the malign influence of Pakistan, Iran and Russia.

The first outcome is already unfolding: Taliban controls more than one-third of Afghanistan and is seriously challenging another third. ISIL seems increasingly unstoppable. There are no plausible indications that either the Afghan or the international forces in their present state can stop their convergence in Afghanistan.

That leaves the second Nicholson outcome; an outcome that may complicate matters in Afghanistan to the point of no return.

That is why Afghanistan must choose a third option and that is the option of leaving open the channels of diplomatic and military consultations with Russia and Iran to avoid their retaliatory covert action.

Time is running out.

Massoumeh Torfeh is the former director of strategic communication at the UN Assistance Mission in Afghanistan and is currently a research associate at the London School of Economics and Political Science, specialising in Iran, Afghanistan and Central Asia.

The views expressed in this article are the author's own and do not necessarily reflect Al Jazeera's editorial policy.