The entrance is a deep vertical shaft, usually hidden in a house. It drops down a dozen metres or so before reaching a horizontal passage, lined with concrete and electric cables. Most are around a metre wide and perhaps 2.5m high, barely enough to accommodate a man carrying a heavy load of weaponry.

The tunnels descend deeper, reaching up to 30m below the surface. Most are between one and three kilometres long and have many entrances and branches. They interconnect with other passages and with bunkers used as command centres and weapons stores and to keep Hamas's political and military leaders safe from the pounding by Israeli forces above ground.

This is "lower Gaza" and Israel's casus belli: a secret labyrinth of tunnels and bunkers, painstakingly built by Hamas over recent years at enormous cost.

As Israeli forces race to find and destroy as many cross-border tunnels as possible, Hamas and other militant groups are using their underground strategic weapon to launch attacks against troops, both within Gaza and across the border in Israel.

On Friday, militants emerged from a tunnel near Rafah, in the south of Gaza, to kill two soldiers and apparently abduct a third, although Hamas later claimed he had probably died in a subsequent Israeli air strike. On Tuesday, five Israeli soldiers were killed at an Israeli military watchtower by militants who had crossed the border underground and emerged through a hidden tunnel shaft.

The extent and sophistication of the underground network has taken Israel's political and military leadership by surprise. Nevertheless, more than two weeks after the Israel Defence Forces launched a ground offensive in Gaza, it says it is close to achieving its goal. "We are just a few days away from destroying all of the offensive tunnels," the IDF's southern command chief, Major-General Sami Turgeman, said on Thursday.

On the same day Israel's prime minister, Binyamin Netanyahu, had a similar message: "IDF soldiers are completing the neutralisation of the terrorist tunnels. These tunnels would have enabled Hamas to abduct and murder civilians and IDF soldiers via simultaneous attacks from many tunnels that penetrate our territory. We are now dismantling this ability."

Three different kinds of tunnels existed beneath Gaza, said Eado Hecht, an Israeli defence analyst specialising in underground warfare: smuggling tunnels between Gaza and Egypt; defensive tunnels inside Gaza, used for command centres and weapons storage; and –connected to the defensive tunnels –offensive tunnels used for cross-border attacks on Israel. The military says it has located about 32 to 35 offensive tunnels, of which more than half have been destroyed, and it believes that there are around 40 in total.

Tunnel construction began in Gaza more than a decade ago; IDF soldier Gilad Shalit was abducted by militants on the Israeli side of the border and dragged back into the enclave in 2006. But the industry took off after Israel tightened its blockade of Gaza the following year. Hundreds of tunnels for smuggling goods and people were built under the Gaza-Egypt border; Hamas also dug separate tunnels for bringing weapons into Gaza.

In the past few years, Hamas has used the valuable expertise acquired in the construction of the smuggling routes to build a network of defensive and offensive tunnels. The offensive tunnels have been dug by hand, as the use of machinery would risk detection. Military analysts estimate that each tunnel takes two to three years to complete, and costs millions of dollars.

Destroying the tunnels is also a painstaking operation. "This is very dangerous work," said Hecht. "Firstly, locating the tunnel entrances is very difficult; they are needles in a haystack." Remote technology does not yet exist to locate and map tunnels deep underground, he said, hence the need for troops.

"Once you find the entrance, you have to climb inside to know whether it is a defensive or offensive tunnel. Then you have to map the tunnel: where is it going, does it have branches? Then you have to rig almost the entire tunnel with explosives. And all this time the soldiers are at risk of attack, shooting, booby traps. It is a deadly game of hide-and-seek."

In the small agricultural communities dotted along the Israeli side of the border, there is relief that the threat is finally being dealt with. Adele Raemer, who has lived in Kibbutz Nirim – just 2km from Gaza – for 40 years, says local residents have been warning about tunnels for the past two years.

"People in this area have been screaming bloody murder about this, and no one took any notice," she said. "People have said they've heard noises under their floors. Tapping. The army comes with listening devices, and then says it's water pipes."

In recent days Raemer had made contact on Facebook with a young Palestinian woman across the border to offer a message of condolence about the huge civilian death toll, but the tunnel threat had to be eliminated, she said.

"My daughter, who's now 32, has had a fear from when she was a child that a terrorist would come to her window in the night. I always tried to reassure her that she's safe. But this is not an irrational fear any more. I can deal with the rockets, I'm used to running [to a shelter]. But the tunnels? Imagine being in the UK, and someone coming out of a sewerage pipe and spraying people with gunfire."

Former senior military figures acknowledged that Israeli authorities had known about the tunnels for some time. "We need to be honest with ourselves – yes, the intelligence was there, but I would say the strategic awareness of how big the problem is, and how deep it is, I'm not sure that was really widespread in both the political and high military level," said Major-General Israel Ziv, former head of the IDF's Gaza division, now retired.

Brigadier-General Shimon Daniel, head of the IDF's combat engineering corps from 2003 until 2007 and now a reservist, said: "We knew there was a strategic threat, but we couldn't deal with it. We took the risk [that there would be no major penetration of Israel] and we waited. This is a huge problem, it is not an easy thing to deal with."

Asked why the military did not tackle the tunnels threat in November 2012, when Israel launched Operation Pillar of Defence in Gaza, he said: "There are lots of reasons that influence the way we act – political reasons, international reasons. It's very complicated."

Even when Israel declares it has achieved the strategic goal of the current offensive and destroyed Hamas's offensive tunnels, there is no guarantee that every single one will have been found and "neutralised".

Three days into the ground operation, a senior serving IDF source said its goal was to destroy as many tunnels as it could. "It's very hard for me to say all of them, because there's always a chance we don't know about all the tunnels, and what you don't know, you simply don't know."

According to Hecht, "you never know 100% that you've got everything. But the operation will put the tunnellers back several years, and make it harder for them to rebuild."

To that end, Israel has said that the "demilitarisation" of Hamas and other militant groups in Gaza is an essential element of any agreed truce to end the current conflict. It also intends to widen the no-go buffer zone inside Gaza's perimeter to make cross-border tunnel construction and missile launching more difficult.

However, said Daniel, Hamas would not easily be deterred. "Of course Hamas will try to rebuild the tunnels. The moment we go out [of Gaza] they will begin to dig."