Iranian intelligence agencies failed miserably not just in this war but for many years before that. Mossad was literally running a huge network inside the country and launching attacks on bases using smuggled equipment.
Part of the problem is the battle between the ongoing "reformers" and the current establishment where individuals who view themselves aligned to the "reformers" are happy to align with foreign forces to work against the state to achieve what they consider to be their political goals, at the expense of the state itself and the interests of the state.
We have also all seen how some overseas Iranians are cheering on the war. Truly shocking, and I cannot consider any scenario ever under which Pakistani origin individuals would cheer on a foreign attack on Pakistan because of political considerations. We have a scenario with regards to the current Field Marshal, but during the last operation, everyone was aligned with the country regardless... This situation with regards to Iran is truly shocking to me, and an eye opener..
Imagine what is going in within Iran in this power struggle ?
In most countries, they would call this treason, and here in the UK, people who do that, used to be
hung, drawn, quartered and their remains scattered across the country. In Iran, it seems to me as a outsider, to be considered to be part of the normal political discourse.
There are other reasons why counter intelligence has been so ineffective, including economic desperation that is forcing people as well, though the amount of that effect differs given that the levers of power are not affected by this issues.
You cannot get the level of infiltration and effect that Mossad has achieved without active and deliberate involvement of senior people within the Iranian state officials and political leaders and senior people who access to power both within Iran, and overseas etc ..
Also, there is professional incompetence and having the head of the institution a religious leader makes no sense at all, but then the current government has paid for that decision with some of their most senior leaders it seems.