Counterintelligence comprises the efforts made, typically by states, to defend against the activities of foreign intelligence services on the military, domestic and diplomatic fronts. Operations could be conducted in secret and be restricted to the intelligence or security agencies themselves, or they could be part of larger operations in the context of war. At a diplomatic level, embassies and consulates are typical hosts for intelligence agents under diplomatic guise, who may be declared personae non gratae in the event their cover is blown. Maintaining constant reworking, checking and finetuning of government agencies or their private partners is considered an ongoing duty of counterintelligence services and officers.
Counterintelligence is considered a cat-and-mouse game by most scholars of the field, where defensive and offensive strategies are employed to protect home institutions and operations from foreign or even domestic subversive elements. The most important element of counterintelligence according to manuals and experts is keeping in check sources of human intelligence; i.e., agents. Doctrine would have counterintelligence officers and organizations keeping tabs on foreign or enemy espionage agencies by noting their infrastructure, hierarchy and capabilities. Additionally, risk assessment would consider what the goals of enemy agencies might be.
The onset of the War on Terrorism has tended to treat terrorist organizations as whole intelligence organizations, necessitating the detailed command structure of the organizations. This approach invites some criticism, as agencies might have prejudiced assessments because of political bias, thus assuming terrorist organizations to be wholly organized by military rather than civilian and military divisions. The militant Islamic organizations Hamas and Hezbollah have complexes where there are social services affiliates as well as military command structures, resembling states in and of themselves. This has become more inmportant as both organizations have come to fill the power vacuum left by weak central governments in Gaza and southern Lebanon.
Counterintelligence operations have been used in wartime to neutralize master spies and deceive enemy strategists. The most prominent of such operations in recent Western history was codenamed Operation Bodyguard, where the Allied forces led by the United States and United Kingdom made it appear that Allied preparations for an invasion of Europe would land major forces at the French port of Calais, when in fact hundreds of miles to the west forces would land against presumably depleted German defenses at Normandy. The operation employed deceptive coded messages, and dummy military equipment (much of which was nothing more than balloons shaped like tanks and other weapons). The accompanying Operation Fortitude made Germany concentrate efforts on defending Norway's shores, Crete and Romania from Allied invasion.
Detecting human intelligence or spies comprises the major element of counterintelligence, from screening initial recruits to maintaining updated inspection regimes of current staff and workers. This philosophy has been extended to the private sector as well, where trade secrets or products of company R&D are defended from individuals looking to sell such information to competitors. There is an entire consulting industry for counter- or competitive intelligence in the private sector, whose companies are often run by former military intelligence officers. Agents may be detected by solicitations of money or ideology, but could also find those feeling wronged by the home country in some way, its institutions or the workplace.
Detecting foreign or terrorist infiltrators has also involved domestic crime investigators and their organizations. In the United States, this involves the FBI; in the United Kingdom, MI5. The FBI has been the traditional home of counterespionage, going as far back as the early 1940s. The FBI has been an innovative user of wiretaps and other technologies to keep tabs on suspects. Additionally, criminal investigations have developed in tandem with counterespionage work, including financial forensics to trace payments from foreign intelligence services to suspect agents.
Related to the usage of technology by the Federal Bureau of Investigation, a number of technological innovations have been made for use in counterintelligence. For example, in Israel the firewall was invented to block the penetration of information systems and computers by the enemy, touching off a new era in tech development for use in war and by intelligence services. This has also spurred private sector development.