Ben Guerdane is a dusty town in Tunisia's south, just 20 miles from the border with Libya, a roiling nation of militias and guns galore. It's a smuggling town, and it depends on the nearly 300-mile border with Libya to survive.

In more normal times, it's everyday products that get smuggled, but these days something more nefarious is coming across that border — weapons and militants.

The spillover from the conflict in Libya is setting off alarm bells in Tunisia, threatening a fragile democracy in the one place that emerged from the 2011 Arab revolts as a bright spot.

Meanwhile, Libya is a nation in chaos with three battling governments, a plethora of militias and a security vacuum being filled by a growing presence of the Islamic State extremist group on Tunisia's border. And even more ominously, many of those Islamic State fighters now in Libya are actually from Tunisia.

On March 7, Islamic State fighters tried to seize Ben Guerdane and claim it for ISIS. If the extremists had succeeded, it would have given the group free movement between Tunisia and Libya. But the Tunisian security forces prevailed in bloody fighting that left more than 50 dead.

On Ben Guerdane's main street, café owner Omar Rabshi recounts how it began with gunfire at 5 a.m.

"I came here to my café and I found lots of armed people here," he says. "About five who were checking people and making people show ID, one had the RPG and the others carried Kalashnikovs."

The gunmen were in there 20s, were unmasked and spoke with Tunisian accents. They patted people down. If they were civilians, the gunmen let them go. If they were from the Tunisian security forces, the gunmen killed them. Then they set up checkpoints at each end of the road and made a declaration.

"We are the Islamic State and tomorrow we will govern you," Rabshi recalls them saying.

All along the street are the signs of the recent battle. Bullets scarred the building next to Rabshi's cafe, where ISIS shot a customs agent. Up the road, the police station is pocked with evidence of the shootout between officers and ISIS fighters. And in the main square, a banner hangs, emblazoned with pictures of the town's dead.

Police say it was a multi-pronged attack by some 70 ISIS men. And the attackers were mostly Tunisians who'd trained with ISIS across the border in Libya, many from this very town. The fighters came with a truck full of weapons to hand out to sympathetic townspeople.

Some swarmed through farms to hit the army barracks on the west side of town, others hit the police station in the town center and another group went to the home of the head of the anti-terrorism brigade, Abdul Atti al-Kabir, to assassinate him.

His cousin, Jilan Abdul Kabir, shows me how it happened, walking me through the low cinder block walls erected on the farm and up the unpaved driveway.

"We heard gunfire," he says, "We saw a truck with masked gunmen drive by and then return. "

So Abdul Kabir and his cousin ran. The attackers trapped al-Kabir against a wall in the yard. All he had was a pistol filled with eight bullets.

Abdul Kabir jumped over a wall to escape.

He heard his cousin say, "I lived as a man and I will die as a man." Then came the gunfire.

"I heard them yelling, 'God is great,'" Abdul Kabir says. That's when he knew his cousin was gone.

He later found his cousin's body on a mound of dirt against the wall where he'd been cornered.

Inside al-Kabir's home, his brother Omar blames the Tunisian justice system.

"My brother arrested so many of these terrorists," he says. "But the courts let them go just because the police use some violence to get the truth from these criminals. The court says, 'Well you beat them, so I will set them free.' It's incredible."

That kind of sentiment is growing in Tunisia, where more people are willing to throw out civil liberties, a key promise of the new democracy, in favor of tough security measure that may bring stability. After three major attacks last year, the country is under a state of emergency.

But in Ben Guerdane, ISIS made a fatal miscalculation. The extremists thought it was ripe for rebellion. Instead, in a matter of hours the militants had lost.

Back at the café, Rabshi explains why ISIS thought it could take Ben Guerdane.

"They thought because Ben Guerdane is a city on the borders, because there is no development, there are no factories, it's been oppressed for 50 years. It's living under marginalization. No one takes care of it," he said.

But people in Ben Guerdane, he says, don't take kindly to any outside occupiers. And ISIS was turned back _ at least this time.

More attacks are expected. Hundreds of Tunisians are training with ISIS in Libya. And that's in part because there is dissatisfaction across Tunisia.

Many feel the promises of social justice and economic prosperity have not materialized after Tunisia's dictatorship was ousted in 2011. ISIS promises power, money and paradise in the afterlife.

Meanwhile, Tunisia is building a wall that's designed to stem the flow of fighters and weapons. It's a sand berm that stretches for more than 100 miles and is reinforced with water trenches and electronic surveillance. The U.S. and Germany are helping Tunisia.

At the central police station in Ben Guerdane, police Lt. Salah Mansouri acknowledges the growing danger.

Copyright 2016 NPR. To see more, visit