Tunisian gunmen who attacked a beachfront hotel trained with militants in Libya. An ISIS flag is seen on a beach in the Libyan city of Sirte, June 29, 2015. Photo: Media Office Wilayat Barqa

Tunisian militants behind deadly terror attacks in Tunisia in recent months reportedly trained in the mountainous region on the border between their home country and Libya. The attacks point to the border region's long existence as a haven for extremist groups and underscore how the increasingly violent political landscape in Libya is helping the Islamic State group -- also known as ISIS -- expand its reach.

Saif Rezgui opened fire on the beach of the Imperial Marhaba Hotel last Friday and killed 38 people, the majority of whom were tourists. In March, two Tunisians gunned down tourists at Tunisia’s Bardo Museum, killing 21 people. The Islamic State group’s affiliates in the area claimed responsibility for both attacks. Before the attacks, the gunmen traveled to Libya, where they are believed to have trained with the militant group’s Libyan branch.

Militants have set up training camps along the border for years hoping to capitalize on Libya’s massive amount of free-floating weaponry. Tunisians, who make up the largest number of foreign fighters who have travelled to fight with groups in Iraq and Syria, were the most common trainee in Libya’s many camps.

“The camps have historically always been in the Green Mountain region outside of Derna, [Libya]…and in the desert, that’s a huge blind spot,” said Andrew Engel, an Africa analyst at the Washington Institute for Near East Policy and author of "Libya as a Failed State: Causes, Consequences, Options."

Libya has become increasingly unstable since Moammar Gadhafi’s regime collapsed in 2011. Today, Tunisia’s border and most of Libya are home to two main extremist groups: ISIS and Ansar al-Sharia. Some members belonging to the latter group have pledged allegiance to ISIS Caliph Abu Bakr al-Baghdadi, but their leaders have remained quietly aligned to al Qaeda.

The struggle for dominance between al Qaeda affiliates and ISIS’ branch in the country has heated up in recent weeks. Last month, the al Qaeda-linked Abu Salim Martyrs Brigade pushed out ISIS militants from their first Libyan stronghold of Derna, much further east than the Tunisian border.

Last year, al Qaeda militants seized a camp known to militants and local media outlets as “Camp 27” because it is 27 km (16.8 miles) west of Tripoli, Libya. Since then, “that has changed hands several times,” Engel said.

In early January, at least one camp on the Tunisian side of border belonged to ISIS. Tunisian fighters in the camps used Libya as a training layover before a recruiter sent to them to fight in Syria by way of Turkey. In February, the Tunisian government reported that it was able to stop 9,000 would-be fighters en route to the camp, but 540 militants already had completed their training and left the country.

The Tunisian government was aware of the camp but was unable to shut it down until after the attack at the Bardo Museum in March. “The place where ISIS had its base is very dangerous," Wael Ouni, a Tunisian with family ties to the militant group recently told International Business Times. “We lost a lot of Tunisian soldiers who were killed there.”

The recent attacks in Tunisia targeting tourists are part of a campaign known as “lone wolf” attacks. They require some logistical planning, either from an extremist group commander or from the individual, but high-level military skills are not essential.

“For lone wolf attacks, it could have very well been that he stayed in a safe house, then went up to the hills and did target practice,” Engel said, adding that the widely used term may not be the best way to describe the attacker. “Everyone thinks you have people doing jumping jacks, monkey bars and jumping over burning tires. For this kind of operation, that’s not what’s needed.”