The CIA found itself in some rough waters in the Middle East last week. On Thursday, an influential member of Iran's parliament announced that the Islamic republic had arrested 12 "CIA agents" who had allegedly been targeting Iran's military and its nuclear program. The lawmaker didn't give the nationality of the agents, but the presumption is that they were Iranians recruited to spy for the CIA. The agency hasn't yet commented, but from what I've heard it was a serious compromise, one which the CIA is still trying to get to the bottom of.
Even more curious was the flap in Lebanon. In June, Hizballah's secretary-general Hassan Nasrallah announced that the movement had arrested two of its own members as CIA spies. But it wasn't until last week that the story got traction in Washington. The CIA confirmed that operations in Beirut had been compromised but declined to offer details. As in the case of the alleged Iranian debacle, it's no doubt still doing a "damage assessment" a process that can take years. Even then, it will be difficult to determine exactly what happened.
From what I've been able to piece together, Hizballah aggressively went after the CIA in Lebanon using telephone "link analysis." That's a form of electronic intelligence gathering that uses software capable of combing through trillions of gigabytes of phone-call data in search of anomalies prepaid cell phones calling each other, series of brief calls, analysis of a cell-phone company's GPS tracking. Geeks who do this for a living understand how it works, and I'll take their word for it.
But it's not the technology that's remarkable, as much as the idea that it's being employed by Hizballah, a militant Islamic organization better known for acts of terror than for electronic counterespionage. That's another reminder that Hizballah has effectively supplanted the Lebanese state, taking over police and security functions that in other countries are the exclusively the domain of sovereign authority. Indeed, since Nasrallah's announcement of catching the CIA agents, no Lebanese authority has questioned why Hizballah, rather than Lebanese intelligence, would be responsible for catching alleged spies for foreign powers in Lebanon. Nobody bothers to ask what would be a pointless question; everyone knows that when it comes to military and security functions, Hizballah might as well be the state.
Since I served in Beirut during the '80s, I've been struck by the slow but inexorable shift of sovereign power to Hizballah. Not only does the movement have the largest military, with nearly 50,000 rockets pointed at Israel; it has de facto control over Lebanon's spies, both military and civilian. It green-lights senior appointments. Hizballah also is wired into all the databases, keeping track of who enters the country, who leaves, where they stay, whom they see and call. It's capable of monitoring every server in the country. It can even tap into broadband communications like Skype. And, of course, it doesn't bother with such legal niceties as warrants. If foreigners are going to be caught spy in Lebanon, it will be Hizballah that catches them.
I have a feeling last week's events bodes ill for U.S. intelligence because it suggests that anyone capable from organized crime to terrorist groups can greatly enhance their counterintelligence capability by simply buying off-the-shelf equipment and the know-how to use it. Like a lot of people, I thought it would be easy coasting at the end of the Cold War after the KGB was defanged. Instead, globalization and the rapid spread of sophisticated technologies have opened an espionage Pandora's box.
Baer, a former Middle East CIA field officer, is TIME.com's intelligence columnist and the author of See No Evil and The Devil We Know: Dealing with the New Iranian Superpower.