LONDON — The Islamic State's so-called caliphate ends 2017 in tatters.

But after a year in which U.S.-backed forces drove ISIS militants from strongholds in Iraq and Syria, the group is far from defeated.

Analysts warn that ISIS is retreating into what some call a “virtual caliphate" from where it will attempt to inspire more lone wolf attacks in the West in an effort to remain relevant.

“It’s like a cornered cat that will lash out indiscriminately and viciously to save itself,” said Peter Vincent, a counterterrorism expert and former Department of Homeland Security official. “The war has yet to be won, and if it’s ever going to be won it’s going to take many more years, and many more civilians will lose their lives."

Iraqi forces hold the ISIS flag outside the destroyed Al-Nuri Mosque in Mosul after the area was retaken on June 30, 2017. Fadel Senna / AFP - Getty Images file

During 2017, military campaigns retook Mosul in Iraq as well as the Syrian city of Raqqa — the de facto capital of the self-proclaimed caliphate.

By early December, the Pentagon said 97 percent of ISIS-held territory in Iraq and Syria had been liberated. On Dec. 9, Iraq declared its war against the militant group over.

But three days after declaring victory, Iraqi Prime Minister Haider al-Abadi warned that the extremists might "erupt again somewhere else" without international cooperation in combating the militants. "ISIS has this unfortunate ability to recruit young people very quickly," he added.

Richard Barrett, a former director of global counterterrorism at Britain's MI6 foreign intelligence service, said the fall of the caliphate would damage the group's appeal to potential recruits because they can no longer sell the idea of a "perfect Islamic state."

Related: ISIS weapons arsenal included some purchased by U.S. government

"Many people went to join something that existed, not something that is hypothetical," he said.

As ISIS leaders spend time "scurrying around" trying to survive, they have less time to plan and coordinate attacks, he said.

New York City police stand guard after an attack at the New York Port Authority Bus Terminal on Dec. 11. Drew Angerer / Getty Images

But Shiraz Maher, deputy director of the International Centre for the Study of Radicalization and Political Violence at King's College London, believes the military campaign only addressed the territorial aspect of ISIS and warned that the terror group was "reverting to type."

“This is a group that emerged from being an insurgency into a proto-state, and it’s now simply pulling back to what it knows best,” Maher told a counterterrorism conference in London at the Royal United Services Institute (RUSI), a think tank on international defense and security.

The ability of ISIS to shift forms demonstrates its “inherent resilience," he said.

“This notion that we’re defeating Daesh, that we can begin to think about a post-Daesh reality is not to my mind accurate,” he said, using the pejorative Arabic term for ISIS. “This is an ongoing threat, it’s a live event.”

Related: Jihadi who 'likes the U.S.' says he knows how to stop terrorism

With the caliphate all but gone, the question of where displaced fighters might go and what they will do when they get there is what keeps intelligence officers up at night, according to Karin von Hippel, RUSI's director.

It's an acute fear in Europe, which suffered a string of deadly terror attacks this year, including car-ramming attacks in Barcelona, Spain, Stockholm and London, and the bombing at an Ariana Grande concert in Manchester, England.

Of the 5,000 to 6,000 European nationals who went to fight with ISIS in Syria and Iraq, some 25 to 30 percent have returned home, according to Rob Wainwright, director of the European Union's police agency, Europol. A similar number are believed to have died in battle, he said, leaving roughly 2,500 to 3,000 alive.

“There is a danger, yes, that some of them will go underground, through other countries, in a way in which we have lost sight of them,” he told NBC News recently in an interview, although he said the actual number of foreign fighters returning to the West in the past 18 months had been surprisingly low.

“The intelligence community doesn’t fear a massive flow of returnees but more a trickle,” Gilles de Kerchove, counterterrorism coordinator for the European Union, told the RUSI conference.

He warned that even a small number of returning foreign fighters have the potential to cause mass casualties.

De Kerchove said that of the more than 40 attacks carried out in the E.U. since 2014, three by returning ISIS jihadis accounted for more than two-thirds of the total deaths and injuries.

Related: Children were taught terrorism in math classes under ISIS

Vincent, the Homeland Security official, said ISIS' focus was shifting to places such as Yemen, Chad, Mali and the southern Philippines, from where fighters can plan attacks and find “operational breathing room."

Such outposts have made the loss of territory in Iraq and Syria less acute, according to RUSI's von Hippel.

“Wherever they shift their headquarters, they will still be able to direct and plan attacks. Even if Baghdadi is killed, other opportunistic leaders will be able to take up the brand,” she said, referring to ISIS leader Abu Bakr al-Baghdadi.

Von Hippel said having operatives spread across the world was part of the extremists' “long-term strategy,” which the U.S.-led coalition against ISIS had not done enough to tackle.

ISIS could also try to rebuild its caliphate in Iraq and Syria.

People leave a cordoned off area after a van ploughed into the crowd, killing one person and injuring several others on the Rambla in Barcelona on Aug. 17, 2017. Pau Barrena / AFP - Getty Images file

“The international community better be very careful about dancing on top of the tombstones of the Islamic State’s caliphate because, like nature, ISIS abhors a vacuum," said Vincent, who added that there were already examples of ISIS fighters returning to liberated areas.

Barrett, the former MI6 officer, said he believes the caliphate is still a reality in the minds of ISIS fighters.

"For them the caliphate exists even if it's only one room in one house, and they will always try to expand it back to real, significant territorial control," he said.