Slumped in a Nairobi courtroom, suit coats rumpled and reading glasses dangling from librarian chains, the defendants made a poor showing for the notorious Quds Force of the elite Iranian Revolutionary Guard Corps. Ahmad Abolafathi Mohammed and Sayed Mansour Mousa had been caught red-handed and middle-aged. And if the latter did them a certain credit — blandly forgettable always having been a good look for a secret agent — the prisoners still had to explain why they had hidden 15 kg of the military explosive RDX under bushes on a Mombasa golf course.

Created to advance Iran’s interests clandestinely overseas, the Quds Force has lately provided mostly embarrassment, stumbling in Azerbaijan, Georgia, India, Kenya and most spectacularly in Thailand, where before accidentally blowing up their Bangkok safe house, Iran’s secret agents were photographed in the sex-tourism mecca of Pattaya, one arm around a hookah, the other around a hooker. In its ongoing shadow war with Israel, the Iranian side’s lone “success” was the July 18 bombing of a Bulgarian bus carrying Israeli tourists — though European investigators last week officially attributed that attack to Iran’s Lebanese proxy, Hizballah. That leaves the Islamic Republic itself with a failure rate hovering near 100% abroad and an operational tempo — nine overseas plots uncovered in nine months — that carries a whiff of desperation. A Tehran government long branded by U.S. officials as the globe’s leading exporter of terrorism may be cornering the market on haplessness.

Within Iran’s own borders, however, the story is different. Twice in the past two years Iranian intelligence has cracked espionage rings working with Israel’s Mossad, Western intelligence officials tell TIME. In both cases, the arrests were the furthest thing from secret: announced at a news conference, each was later followed up by televised confessions broadcast on Iranian state television in prime time. Given Iran’s history of trumped-up confessions, skepticism is more than justified. But the arrests appear to be solid. One intelligence official said the captured Iranians provided “support and logistics” to the Mossad operatives who carried out the assassinations of Iranian nuclear scientists.

At least four scientists were killed on Tehran’s streets from 2010 to 2012, when, as TIME has reported, Israel ratcheted back on covert operations inside Iran. Officially, Israel has remained silent on the killings, though government officials will coyly say they welcome the deaths. The Jewish state maintains the same ambiguous posture on other “setbacks” to Iran’s nuclear program widely — and correctly, Western intelligence officials say — attributed to Mossad, from the Stuxnet computer virus, to mysterious explosions like the massive blast at a missile base, which destroyed ballistic missiles that could reach Israel.

The covert onslaught dovetails with Israel’s history of reaching “over the horizon” to disarm perceived threats at a distance. To keep advanced arms from reaching Hamas and Hizballah, Israel in the past year sent warplanes to bomb convoys and arms depots in Sudan and Syria, respectively, without apparent retribution. In the case of Iran, however, experts say the audacity of Israel’s covert campaign stirred Tehran to revive an espionage effort that lay largely fallow since 9/11. The Spy vs. Spy contest that ensued would prove woefully one-sided, even in the third-world countries where Iran chose to strike, hoping to avoid heightened security awareness in the developed world. In the end, its only success came inside Iran, where the secret police operate without inhibition.

The shadow war may have started on Jan. 15, 2007, the day Ardeshir Hosseinpour passed away. Hosseinpour was a specialist in electromagnetics at the Nuclear Technology Center in the city of Isfahan, Iran, but his death might have escaped notice had Iran’s government not kept it under wraps for almost a week, finally attributing it to fumes from a faulty heater. An online report by the American private intelligence firm Stratfor suggested another cause — radioactive poisoning — and hinted that Mossad’s Caesarea section was back in business. Caesarea, named for an Israeli beach town that dates back to Roman times, is the operations branch of Israel’s secret service, most notoriously responsible for the assassinations of some two dozen Palestinians (and an innocent waiter) after the 1972 Munich Olympics. Assassinations are carried out by a very small unit dubbed Kidon, the Hebrew word for “tip of the spear.” Kidon operates at a remove from the legions of Mossad employees working in less lethal fields.

It would have been a unit called Hatzomet, or “The Junction,” that recruited Majid Fashi, a handsome young Iranian who dropped out of high school to pursue a career in kickboxing. By the account he gave on Iranian state television early in 2011, Fashi presented himself at the Israeli consulate in Istanbul in 2007 and was vetted for a solid year before being shown any trust. Two years later, on Jan. 12, 2010, he would place a bomb on a motorbike parked on the sidewalk outside the Tehran home of Masoud Alimohammadi; the nuclear physicist was killed when it was detonated by remote control.

In the broadcast, Fashi accurately described the Mossad campus north of Tel Aviv. He said he had been given a laptop equipped with a second operating system and used it to communicate through online drop boxes. He was impressed by his handlers’ thoroughness. At one point Fashi described studying a scale model of Alimohammadi’s street. “It was an exact copy of the real one,” Fashi said. “The tree next it, the street curb, the bridge.” In a later broadcast, he was seated across from Alimohammadi’s widow, who glared at him as he bowed his head and wept. Mossad officials were “pissed off and shocked” seeing their agent on television, the intelligence official said.

Fashi was executed in May 2012. About the same time, Iran’s intelligence minister announced the arrest of 14 more Iranians, eight men and six women dubbed members of the “Terror Club” in the subsequent prime-time broadcast of that name. Filmed in shadow, and rich in atmospherics, the Aug. 5 program recreated Alimohammadi’s death and four subsequent attacks: they started with the Nov. 29, 2010 nearly simultaneous attempts on Majid Shariari and Fereydoun Abbasi, nuclear scientists driving to work when magnetic “sticky bombs” were attached to the side of their cars from passing motorcycles. Abbasi managed to escape before it detonated, saving his wife as well. Shariari was killed — a significant setback for the Iranian nuclear program where he was the top scientist, according to a Western intelligence official.

The confessed agents offered absorbing detail — they were aboard a Bajaj Pulsar, wearing helmets, when the magnet bomb stuck on the right front panel of Shariari’s car exploded. The riders scrambled into the “trail car” assigned to follow the target and disappeared into the traffic of the Imam Ali Autobahn. Already gone was the car assigned to cut off and slow the car carrying the scientist. They claimed to have rehearsed on a practice track inside Israel. None of the details could be confirmed, but an intelligence official acknowledged: “Another network was taken.”

The third scientist, Dariush Rezaeinejad was shot on July 23, 2011 after picking up his child at a day care; his wife described hearing shots whiz by as she chased the assailants. The most recent assassination was the Jan. 11, 2012 death of Mustafa Ahmadi-Roshan, an expert on uranium enrichment, also by a magnet bomb slapped on his car during his morning commute.

By then, Iran was trying to strike back. The task of avenging the scientists fell to the sprawling Quds Force’s own covert-operations division, known as Unit 400. It took a shotgun approach, targeting Israeli diplomatic missions in a variety of countries, mostly in the developing world where the global antiterrorism mesh is not so fine. Exposed in Baku, Tbilisi, Johannesburg, Mombasa and Bangkok, the failures mounted at a pace that was itself one of the problems. In the world of espionage, a quality covert operation can take years to pull together. Yet in the 15 months from May 2011 to July 2012, the Quds Force and Hizballah attempted 20 attacks, by the count of Matthew Levitt, a former State Department counterterrorism official. “Hizballah and the Quds Force traded speed for tradecraft and reaped what they sowed,” Levitt writes in a January report for the Washington Institute for Near East Policy. “Quds Force planners were stretched thin by the rapid tempo of their new attack plan, and were forced to throw together random teams of operatives who had not trained together.”

The decline in quality was so striking it initially inspired disbelief. Recall the preposterous-sounding plot weaving together a former used-car salesman, Mexico’s Zetas drug gang and a bank transfer from a Revolutionary Guard account to assassinate Saudi Arabia’s ambassador — by bombing a Washington restaurant? A year on it looks like the new normal. In Bangkok last month, an Iranian agent entered a courtroom in a wheelchair, having accidentally blown his legs off while fleeing police. A January alert issued by Turkish intelligence was light on specifics but quite certain the Quds operatives would be staying in five-star hotels.

“There’s a number of reasons that Iranian intelligence has suffered,” says Meir Javedanfar, an Iranian-born analyst who lectures at the Interdisciplinary Center in Herzliya, Israel. “No. 1,” he says, “is the 2009 uprisings in Iran.” The street protests over a fraudulent election undermined the perceived legitimacy of the state among people who once would work for it, including in its secret services. “People less and less see it as a nationalist endeavor and more as a Khamenei-related project to strengthen himself,” Javedanfar says, referring to Iran’s Supreme Leader Ayatullah Ali Khamenei, who by some published accounts personally authorizes all overseas attacks.

Hard-liners further aggravated the situation by purging competent reformists from both the secret services and from Iran’s embassies — crucial to a force expected to work undetected abroad. “Basically the Quds Force doesn’t cooperate with the Foreign Ministry, and the Foreign Ministry isn’t what it used to be either,” says Javedanfar. Under President Mahmoud Ahmadinejad, 42% of ministry employees have only high school degrees. “The regime is a bigger threat to itself than Israel,” he says.

Karl Vick has been TIME's Jerusalem bureau chief since 2010, covering Israel,the Palestine territories and nearby sovereignties. He worked 16 years at the Washington Post in Nairobi, Istanbul, Baghdad, Los Angeles and Rockville, MD.