If there was one telling moment in Pakistan in the 10 days since Osama bin Laden's death, when a Hollywood-style American assault on a suburban house left the country reeling, torn between anger, shame and denial, it occurred late one evening on a prime-time television show hosted by Kamran Khan.

Chatshow hosts are the secular mullahs of modern Pakistan: fist-banging populists who preach to the nation over supper, often through a rightwing lens. Khan, a tubby 50-year-old journalist with neat glasses and a small chin, is the biggest of them. Every night on Geo, the largest channel, he rails against "corrupt" civilian politicians and America, and lionises the armed forces; some colleagues nickname him "the brigadier". But as the country seethed over Bin Laden last week, Khan tore off his metaphorical stripes and stamped them into the ground.

The army had failed its people, he railed. To Pakistan's shame US soldiers had invaded the country; their finding Bin Laden in Abbottabad, two hours north of Islamabad, was a disgrace. The country's "two-faced" approach to extremism had disastrously backfired, he said, reeling off a list of atrocities – New York, Bali, London, Madrid – linked to Pakistan. "We have become the world's biggest haven of terrorism," he declared. "We need to change." Viewers watched in astonishment. The unprecedented attack targeted not only the army chief, General Ashfaq Kayani, but also the most sensitive policies of the military's premier spy agency, the Inter-Services Intelligence Directorate (ISI). Feared, reviled and admired in equal measure, the ISI is considered the embodiment of army power in Pakistan, the object of hushed deference. But now, as one US official told me, "the world has changed". And the ISI finds itself in the line of fire.

The Bin Laden debacle has triggered a blizzard of uncomfortable questions, the sharpest come from Washington. How, President Barack Obama wondered aloud last Sunday, could Bin Laden shelter for years in a garrison town that is home to three regimental headquarters, the local version of Sandhurst, and thousands of soldiers? One retired US officer who has served in the region told me he had been mulling the same question. "All those times we drove up to Abbottabad, and we could have taken out our pistols and done the job ourselves," he said. The CIA chief Leon Panetta, meanwhile, says he didn't warn the ISI about the special forces raid because he feared word might leak to the al-Qaida leader. Behind the pointed statements lies an urgent question: was the ISI hiding Bin Laden?

The answer may lie inside the ISI's headquarters in Abpara, on the edge of Islamabad. The entrance, beside a private hospital, is suitably discreet: no sign, just a plainclothes officer packing a pistol who direct visitors through a chicane of barriers, soldiers and sniffer dogs. But inside, past the smooth electric gates, lies a neatly tended cluster of adobe buildings separated by smooth lawns and tinkling fountains that resembles a well-funded private university. Cars purr up to the entrance of the central building, a modern structure with a round, echoing lobby. On the top floor sits the chief spy: the director general Ahmed Shuja Pasha, a grey-haired 59-year-old three-star general. One American counterpart describes him as "brilliant and extremely intelligent . . . Thoughtful, pensive and extremely well read; if he was in the US military he would be a very successful officer."

Pasha and the ISI are the heart of Pakistan's "establishment" – a nebulous web of generals, bureaucrats and hand-picked politicians (not always elected ones) who form the DNA of Pakistan's defence and security policies. It has at least 10,000 employees (some say twice as many), mixing serving army officers, many on three-year rotations from other services, with thousands of civilian employees, from suited analysts to beefy street spies. In theory they answer to the prime minister; in reality they are a tool of the army chief, Kayani. To supporters, the ISI safeguards national security – monitoring phones, guarding the country's nuclear weapons. But to its many critics, the ISI is the army's dirty tricks department, accused of abduction and assassination, vote-rigging and torture, and running Islamist terrorist outfits. "The ISI," said Minoo Bhandara, an outspoken Parsi businessman who ran a brewery across the road from army headquarters before he died in 2008, "is an institution full of intelligence but devoid of wisdom."

Oddly, it was founded by an Australian. As Pakistan recovered from its disastrous first war with India in 1948, Major General R Cawthorne, on secondment from the British army, decided the fledgling military needed a proper intelligence outfit. The first decades were inauspicious. The ISI mishandled the 1965 war with India and failed to predict the East Pakistan conflict in 1971, which sundered Pakistan in two and created Bangladesh. All changed, however, eight years later when Soviet tanks rolled into Afghanistan on Christmas Day 1979. The decade-long war of resistance – bankrolled by the United States, fought by Afghans and Arabs, but largely run by the ISI from Pakistan's tribal areas – revolutionised the agency's fortunes. It ran a network of secret training camps along the Afghan border that trained more than 80,000 fighters. It controlled a weapons pipeline, funded by the CIA and Saudi intelligence, that smuggled Kalashnikovs and Stinger anti-aircraft missiles from Karachi to the Khyber Pass. And it grew powerful and rich.

A legendary figure from that period was a man named Colonel Imam, whom I first met five years ago. He was tall and burly, with a thick beard and a crooked smile that suggested several missing teeth. He wore a white turban and an olive-green, British issue second world war-issue paratroop jacket, which he told me he had been wearing since he joined the army in 1971. During the 80s, Imam ran many of the ISI training camps, becoming popular among ethnic Pashtun fighters for his love of Islam and his fondness for killing Soviets. "Those were wonderful times," he told me. Although his real name was Sultan Amir, to the Afghans he became "Colonel Imam". "I loved the fight. And the mujahideen were very fond of me," he said with a smile.

The US liked him too. On the wall of his Rawalpindi home hung war trophies from the 80s – daggers, faded photos, a Russian general's gun – but on the table sat a chunk of the Berlin wall, cased in glass. "To one who helped deliver the first blow," it read. "The Americans gave me that," he said.

With the Russian withdrawal from Afghanistan in 1989, the CIA largely abandoned Pakistan. But the spirit of "jihad" – fighters imbued with Islamist vim – lived on in the ISI. Pakistani officers, having imbibed too much of their own ideology, transformed the spy agency. It started to support Islamist groups across Asia – Bangladesh, Uzbekistan, Burma, India – and the US placed Pakistan on a terrorist watchlist. In 1993, Javed Ashraf Qazi, a secular-minded general officer, was sent in to clean up the mess. "I was shocked at what I found," he tells me. Senior ISI officers had jettisoned their uniforms for shalwar kameez; their subordinates would disappear off to the mosque for hours on end. The ISI had bought a hotel in Bangkok, probably to facilitate gun-running. The outgoing spy chief, Javed Nasir, was a playboy turned zealot who had grown a scraggly beard and refused to shake women's hands. On his first day in the office Qazi found him running out of the door to a Muslim missionary conference. "When people say the ISI is a rogue agency, it was true in those days," he says.

Qazi fired the ideologues, sold the hotel and ordered his subordinates to wear their uniforms (some struggled to fit in them). "We cleaned it up," says Qazi, who later became a minister under Pervez Musharraf.

But the ISI was not done with jihad; it had merely narrowed its focus. The proof is on the wall of Qazi's home. I notice an unusual rifle hanging on the wall. It is an Indian service rifle, Qazi admits half bashfully – a present from one of the "mujahideen" fighters the ISI started to send into Indian-occupied Kashmir from the mid 90s, when he was in charge. "We turned a blind eye to some groups," he says. They included Lashkar-e-Toiba, he admits – the terrorist outfit that in 2008 would attack hotels and train stations in the Indian city of Mumbai, killing 170 people.

In the early 90s, the ISI also started to support an obscure Islamist movement in Afghanistan called the Taliban. Colonel Imam was sent back into Afghanistan to advise the one-eyed Taliban leader Mullah Omar. They had history: Imam, it turned out, had trained Omar back in the mujahideen camps in the 80s. With ISI backing, the Taliban swept to power in Kabul; at the UN in New York, a beleaguered Afghan official complained that Imam was the "de facto governor" of the newly conquered territories. "Ah, they are naughty people," Imam told me of the Taliban with his shy smile. "Rough people, good fighters, but respected. And they were all my friends."

Over the past decade, however, the ISI has professed to have abandoned jihad. As American troops swarmed across Afghanistan, in search of Bin Laden in late 2001, President General Pervez Musharraf disavowed the Taliban, sacked his most Islamist generals (including the then ISI director, Mahmud Ahmed) and brought Colonel Imam home. The following January he made a signature speech banning a slew of jihadi groups. "We need to rid society of extremism," he declared.

On the ground, though, things have looked different. US diplomatic cables released through WikiLeaks last year claimed the ISI was still covertly supporting the Afghan Taliban, Lashkar-e-Taiba, and the Haqqani network, as part of its decades-old grudge match with India. And despite billions of dollars in American assistance, wrote ambassador Anne Patterson, "no amount of money" was likely to make the army – or the ISI – change direction.

Simultaneously, though, the ISI has become a victim of jihadi violence. The Pakistani Taliban – related to the Afghan movement, but separate, and heavily influenced by al-Qaida – is seeking to oust the Pakistani state. The ISI, deemed to have betrayed them, has become the enemy. Hundreds of ISI officials have died in recent years, killed in bombings of buses and offices, and ISI spies have been beheaded in the tribal belt. In the latest atrocity on 8 March a massive car bomb outside an ISI office in Faisalabad destroyed an airline office and killed 32 people.

I last saw Colonel Imam in January 2010 at his home in Rawalpindi. He joked about media articles describing him as the "father of the Taliban". Weeks later he set off for Waziristan with another former ISI man, Khawaja, and a British journalist, Asad Qureshi, who had been commissioned by Channel 4, to interview the Taliban leader Hakimullah Mehsud. But the Taliban took them hostage. After a few weeks Khawaja was executed, after confessing on video to being a "CIA spy". Qureshi was released in September after his family paid a hefty ransom. Then last January, a video of Imam surfaced showing him kneeling before a group of masked, armed men. Mehsud appeared, and said a few words. Then a Talib opened fire, pumped Imam with bullets.

"When you're Frankenstein, and you create a lot of baby monsters who are running round your ankles looking sort of cute, they eventually grow up to be recalcitrant adults," a US official tells me in Islamabad. "And you hope you can get them back into the fold so they become useful. But the Pakistanis can't control everything they create."

Could the ISI's complex policy towards jihadi militants have caused it to harbour Bin Laden? Its many critics have little doubt, particularly in Afghanistan; last week the former Kabul spy chief Amrullah Saleh said he warned Musharraf about Bin Laden four years ago, only to be rowdily shouted down. Now Musharraf himself admits it's a possibility, albeit one limited to "rogue" officers. Yesterday he told ABC News there was a "possibility" of a "lower-level operative . . . following a policy of his own and violating the policy from above". But could it be done with the knowledge of the top generals? Opinion is split between agnostics and sceptics. "Did Pasha know? It's entirely implausible that he didn't," says a former western military official who has worked in Pakistan. A senior diplomat sees it differently. Perhaps the ISI is neither complicit or incompetent, he says. Maybe they just didn't look. "Looking for Osama may not have been a big priority when not finding him earns you billions of dollars a year, and if you did the Americans would leave the region," he says.

The ISI itself points to its consistent record in fighting al-Qaida. Over the past decade it has rounded up hundreds of Islamist suspects, many dispatched to Guantánamo Bay. They include the most notorious al-Qaida henchmen: Khalid Sheikh Muhammad, architect of the 9/11 attacks, snatched from a Rawalpindi safehouse in 2003; Ramzi bin al-Shibh, captured after a gun battle a year earlier; Abu Faraj al-Libi, then the al-Qaida number three, arrested in Mardan in 2005 by ISI commandos wearing burka disguises. Not finding Bin Laden was "a failure on our side," admits an ISI official. "Unfortunate, but a fact. We are good but we are not God."

Yet the questions remain. How did Bin Laden avoid ISI surveillance in a military area, just a few hundred metres from a major military base, in a zone where military intelligence traditionally keeps a close eye? And what about the army major who recently built his house just behind Osama's? Did he not wonder about his neighbour with the barbed-wire fence and the security cameras perched on the wall? "I find it entirely implausible that the military and intelligence agencies knew nothing," says Dr Farzana Sheikh, author of Making Sense of Pakistan. "There must have been knowledge at the highest levels." But, along with so many other critics, she concedes "there is no proof". In a country where so many pressing mysteries remain unresolved – from the plane crash that killed General Zia ul-Haq in 1988, to the assassination of Benazir Bhutto in 2007 – few are holding their breath.

There could, at least, be accountability, although hopes are fading fast. As television anchors raged and criticism of the army swelled last week, some hoped Pakistan's civilian leadership would seize the moment to claw back part of the power it has ceded over the past 30 years. Yet those hopes were dashed on Monday when prime minister Gilani stood up in parliament for a stout defence of the generals. "The ISI is a national asset," he said. The battle, if it had ever been contemplated, was lost.

In America the scrutiny will not vanish so easily. Angry congressional leaders have called for Pakistan's $3bn annual aid package to be slashed; hostile media coverage portraying the ISI as an enemy unit is growing. Government officials, however, are more circumspect. With Nato's main military supply line running through Pakistan, other al-Qaida figures still at large including Bin Laden's deputy, Ayman al-Zawahiri, and a peace settlement to be negotiated in Afghanistan, many quietly speak of the need to eventually patch up the Pakistan relationship – although few doubt that it has been utterly changed over the past 10 days. "We can't break it, it's too important," says one US official. "We're going to have to sit down across the table and try and tell some truths to each other."

Still, he adds: "There are degrees in truth. We would like to have a degree of the truth."

American popular opinion may be less nuanced. The forthcoming trial of David Headley, an American jihadi accused of helping Lashkar-e-Taiba carry out the Mumbai attacks, is likely to bring fresh accusations of ISI "double-game". And movie culture is likely to have a strong influence. Even before Bin Laden died an action thriller called tentatively "Kill Bin Laden", by Oscar-winning director Kathryn Bigelow, was in the works. Now many more will surely follow. In the coming months, casting directors will start seeking actors to play macho navy Seals, a tense American president and an elusive Saudi fugitive. And, almost certainly, they will be looking for a clutch of double-dealing Pakistani spies. In the ISI, Hollywood may have found a new bad guy.