Its much-vaunted caliphate has gone, crushed by the might of Russian, Syrian and US warplanes, Iran-backed militias, Kurdish forces and armies launched by Damascus and Baghdad. But while 2017 might have seen the end of Islamic State’s dream of ruling over its twisted vision of an ideal society, the year ended with an ominous sign that its deadly international campaign against the many people and faiths it sees as spiritual foes has gathered new energy.

Last Thursday, dozens of civilians in Kabul were killed in a suicide attack that targeted a Shia cultural centre in the Afghan capital. The assault was the latest in persistent attacks by an affiliate of Isis, which has proved to be resilient despite a relentless campaign against it in recent months.

According to the Isis-linked Amaq news outlet, three blasts were detonated at the compound, which also houses a news agency. A bomber then blew himself up among crowds in the Tebyan cultural centre. At least 41 people were killed and 90 others were injured.

The attack comes despite an intensifying campaign by the US and Afghanistan to uproot the twin threats emanating from Isis and the Taliban, especially since Donald Trump took office in January. Striking inside the Afghan capital, despite the surge in security and military measures, has also raised fears about the enduring ability of the group as the caliphate it once established in Iraq and Syria has collapsed.

In April, the US even dropped the “mother of all bombs” on an Isis base in Afghanistan, an indication of the ferocious campaign against it. But this relentless campaign failed to root it out. In recent months, experts and officials pointed to a successful effort by the Isis affiliate to build roots for itself inside the capital, recruiting dozens of local members, including children.

In Afghanistan, Isis has done so much with so little. In Libya, for example, the group had hundreds of local battle-hardened fighters with experience stretching back to the early years of the Iraq war and who played a pivotal role in early Isis efforts in Syria in 2014, but its fortunes have dwindled over the past two years.

Its territorial demise might exacerbate other insurgencies, if militants fill up the ranks of affiliates in other countries

The Afghanistan affiliate, in contrast, has competition from resurgent Taliban militants who have deeper links to the country, but it has managed to deepen its presence. Striking inside the capital suggests that the group has successfully evolved from a largely foreign-led organisation to an increasingly localised one.

Aside from its persistence in Afghanistan, the nature of Thursday’s attack is a harbinger of what is to come as Isis loses its caliphate in Iraq and Syria. In its statement about the assault, the Isis media outlet claimed that the cultural centre was bankrolled and sponsored by Iran. “The centre is one of the most notable centres for proselytisation to Shiism in Afghanistan,” the statement added. “Young Afghans would be sent to Iran to receive academic studies at the hands of Iranian clerics.”

Isis has sought to tout itself as the defender of Sunnis across the region and the choice of words in its statement is designed to drive that message. The sectarian theme is likely to be the group’s main focus in the coming years, as it retreats from a caliphate to an insurgency. The sectarian narrative helps the group present a “contiguous ideology” from Afghanistan to Syria, in place of the caliphate it seems to have lost; its message to its followers is that the victims of its attack were potential soldiers in the army that Iran is forming everywhere.

Presenting itself as the last line of defence against Iran will ensure that its localised operations have a general regional theme, even as it has lost the global caliphate. This has been a recurrent theme since its rise in 2014, but the group has increasingly focused on sectarianism, not just against the Shia but also against Christians and other religious minorities.

The group’s attack inside Iran in June was designed to achieve this objective and attacks that it portrays as directed against Iranian interests, such as the one in Kabul, serve a similar purpose. By doing so, it seeks to tap into a market in which even al-Qaida and the Taliban cannot compete with the same vigour, as they tend to focus on a relatively less sectarian struggle in their rhetoric. In October, suicide bombers linked to the group killed at least 57 worshippers in a Shia mosque in Kabul. Isis’s sectarian focus makes its persistence even more troubling for the country and the wider region. A day after the Kabul assault, the group also claimed responsibility for a militant shooting on a church in Cairo, killing about a dozen people, one of several attacks targeting Coptic civilians and churches in the country in recent years.

The lesson from such attacks is that the group can still be deadly regardless of its contraction in Iraq and Syria. Indeed, its territorial demise might even exacerbate insurgencies elsewhere, if militants safely flee the battlefields to fill up the ranks of affiliates in other countries. Reports of militants escaping the collapsing caliphate have emerged recently. Earlier this month, for example, Agence France-Presse reported that French and Algerian fighters travelled to Afghanistan from Syria to join the Isis branch there. Similar trends were reported in Egypt and Libya. An African Union official also warned this month that many of the 6,000 who had travelled to Syria in 2014 may be returning home.

Such fighters could replenish and revitalise insurgencies scattered across the region in a way they could not when the group’s focus was its core in Iraq and Syria. The branches of Isis that sprung up remained limited in size and some weakened as the pool of militants had been small. This could change as former fighters make their way out of Syria and Iraq to countries in the region, where it is easier to link up with existing affiliates than if they travelled to their homelands, such as in Europe and Britain. The group thrives on polarisation and religious minorities present it with soft targets to turn people against each other. These targets also enable it to recast itself in opposition to al-Qaida and other Islamist groups. In addition to political stagnation and persistent conflicts, sectarianism will continue to provide the group with growth opportunities in a region beset with ever deepening divisions and amid the increasing role of Iran in the Middle East.

Egyptians gather at the funeral of the victims of a gun attack at a Coptic church in Cairo. Photograph: Samer Abdallah/AFP/Getty Images

The group hopes the narrative will maintain its appeal among those who see Iran as the usurper of their lands and the domineering sectarian power in the region. The territorial demise of the caliphate might reduce threats against the west, but for the immediate region, where it can move more easily, Isis will continue to exploit social divisions and political stagnation to regroup and entrench itself.

Hassan Hassan is co-author of Isis: Inside the Army of Terror and a resident fellow at the Tahrir Institute for Middle East Policy