Man-in-the-middle attack
In cryptography and computer security, a man-in-the-middle, monster-in-the-middle, machine-in-the-middle, monkey-in-the-middle, meddler-in-the-middle (MITM) or person-in-the-middle (PITM) attack is a cyberattack where the attacker secretly relays and possibly alters the communications between two parties who believe that they are directly communicating with each other, as the attacker has inserted themselves between the two parties. One example of a MITM attack is active eavesdropping, in which the attacker makes independent connections with the victims and relays messages between them to make them believe they are talking directly to each other over a private connection, when in fact the entire conversation is controlled by the attacker. The attacker must be able to intercept all relevant messages passing between the two victims and inject new ones. This is straightforward in many circumstances; for example, an attacker within the reception range of an unencrypted Wi-Fi access point could insert themselves as a man-in-the-middle. As it aims to circumvent mutual authentication, a MITM attack can succeed only when the attacker impersonates each endpoint sufficiently well to satisfy their expectations. Most cryptographic protocols include some form of endpoint authentication specifically to prevent MITM attacks. For example, TLS can authenticate one or both parties using a mutually trusted certificate authority.

Example 

Suppose Alice wishes to communicate with Bob. Meanwhile, Mallory wishes to intercept the conversation to eavesdrop and optionally to deliver a false message to Bob.

First, Alice asks Bob for his public key. If Bob sends his public key to Alice, but Mallory is able to intercept it, an MITM attack can begin. Mallory sends Alice a forged message that appears to originate from Bob, but instead includes Mallory's public key.

Alice, believing this public key to be Bob's, encrypts her message with Mallory's key and sends the enciphered message back to Bob. Mallory again intercepts, deciphers the message using her private key, possibly alters it if she wants, and re-enciphers it using the public key she intercepted from Bob when he originally tried to send it to Alice. When Bob receives the newly enciphered message, he believes it came from Alice.

 Alice sends a message to Bob, which is intercepted by Mallory:
 Alice "Hi Bob, it's Alice. Give me your key." →     Mallory     Bob
 Mallory relays this message to Bob; Bob cannot tell it is not really from Alice:
 Alice     Mallory "Hi Bob, it's Alice. Give me your key." →     Bob
 Bob responds with his encryption key:
 Alice     Mallory     ← [Bob's key] Bob
 Mallory replaces Bob's key with her own, and relays this to Alice, claiming that it is Bob's key:
 Alice     ← [Mallory's key] Mallory     Bob
 Alice encrypts a message with what she believes to be Bob's key, thinking that only Bob can read it:
 Alice "Meet me at the bus stop!" [encrypted with Mallory's key] →     Mallory     Bob
 However, because it was actually encrypted with Mallory's key, Mallory can decrypt it, read it, modify it (if desired), re-encrypt with Bob's key, and forward it to Bob:
 Alice     Mallory "Meet me at the van down by the river!" [encrypted with Bob's key] →     Bob
 Bob thinks that this message is a secure communication from Alice.
This example shows the need for Alice and Bob to have some way to ensure that they are truly each using each other's public keys, rather than the public key of an attacker. Otherwise, such attacks are generally possible, in principle, against any message sent using public-key technology. A variety of techniques can help defend against MITM attacks.

Defense and detection 
MITM attacks can be prevented or detected by two means: authentication and tamper detection. Authentication provides some degree of certainty that a given message has come from a legitimate source. Tamper detection merely shows evidence that a message may have been altered.

Authentication 
All cryptographic systems that are secure against MITM attacks provide some method of authentication for messages. Most require an exchange of information (such as public keys) in addition to the message over a secure channel. Such protocols, often using key-agreement protocols, have been developed with different security requirements for the secure channel, though some have attempted to remove the requirement for any secure channel at all.

A public key infrastructure, such as Transport Layer Security, may harden Transmission Control Protocol against MITM attacks. In such structures, clients and servers exchange certificates which are issued and verified by a trusted third party called a certificate authority (CA). If the original key to authenticate this CA has not been itself the subject of a MITM attack, then the certificates issued by the CA may be used to authenticate the messages sent by the owner of that certificate. Use of mutual authentication, in which both the server and the client validate the other's communication, covers both ends of a MITM attack. If the server or client's identity is not verified or deemed as invalid, the session will end. However, the default behavior of most connections is to only authenticate the server, which means mutual authentication is not always employed and MITM attacks can still occur.

Attestments, such as verbal communications of a shared value (as in ZRTP), or recorded attestments such as audio/visual recordings of a public key hash are used to ward off MITM attacks, as visual media is much more difficult and time-consuming to imitate than simple data packet communication. However, these methods require a human in the loop in order to successfully initiate the transaction.

In a corporate environment, successful authentication (as indicated by the browser's green padlock) does not always imply secure connection with the remote server. Corporate security policies might contemplate the addition of custom certificates in workstations' web browsers in order to be able to inspect encrypted traffic. As a consequence, a green padlock does not indicate that the client has successfully authenticated with the remote server but just with the corporate server/proxy used for SSL/TLS inspection.

HTTP Public Key Pinning (HPKP), sometimes called "certificate pinning," helps prevent a MITM attack in which the certificate authority itself is compromised, by having the server provide a list of "pinned" public key hashes during the first transaction. Subsequent transactions then require one or more of the keys in the list must be used by the server in order to authenticate that transaction.

DNSSEC extends the DNS protocol to use signatures to authenticate DNS records, preventing simple MITM attacks from directing a client to a malicious IP address.

Tamper detection 
Latency examination can potentially detect the attack in certain situations, such as with long calculations that lead into tens of seconds like hash functions. To detect potential attacks, parties check for discrepancies in response times. For example: Say that two parties normally take a certain amount of time to perform a particular transaction. If one transaction, however, were to take an abnormal length of time to reach the other party, this could be indicative of a third party's interference inserting additional latency in the transaction.

Quantum cryptography, in theory, provides tamper-evidence for transactions through the no-cloning theorem. Protocols based on quantum cryptography typically authenticate part or all of their classical communication with an unconditionally secure authentication scheme. As an example Wegman-Carter authentication.

Forensic analysis 
Captured network traffic from what is suspected to be an attack can be analyzed in order to determine whether there was an attack and, if so, determine the source of the attack. Important evidence to analyze when performing network forensics on a suspected attack includes:
 IP address of the server
 DNS name of the server
 X.509 certificate of the server
 Whether the certificate has been self signed
 Whether the  certificate has been signed by a trusted certificate authority
 Whether the certificate has been revoked
 Whether the certificate has been changed recently
 Whether other clients, elsewhere on the Internet, received the same certificate

Notable instances 
A notable non-cryptographic MITM attack was perpetrated by a Belkin wireless network router in 2003. Periodically, it would take over an HTTP connection being routed through it: this would fail to pass the traffic on to its destination, but instead itself responded as the intended server. The reply it sent, in place of the web page the user had requested, was an advertisement for another Belkin product. After an outcry from technically literate users, this feature was removed from later versions of the router's firmware.

In 2011, a security breach of the Dutch certificate authority DigiNotar resulted in the fraudulent issuing of certificates. Subsequently, the fraudulent certificates were used to perform MITM attacks.

In 2013, Nokia's Xpress Browser was revealed to be decrypting HTTPS traffic on Nokia's proxy servers, giving the company clear text access to its customers' encrypted browser traffic. Nokia responded by saying that the content was not stored permanently, and that the company had organizational and technical measures to prevent access to private information.

In 2017, Equifax withdrew its mobile phone apps following concern about MITM vulnerabilities.

Other notable real-life implementations include the following:
 DSniff the first public implementation of MITM attacks against SSL and SSHv1
 Fiddler2 HTTP(S) diagnostic tool
 NSA impersonation of Google
 Qaznet Trust Certificate
 Superfish malware
 Forcepoint Content Gateway used to perform inspection of SSL traffic at the proxy
 Comcast uses MITM attacks to inject JavaScript code to 3rd party web pages, showing their own ads and messages on top of the pages

See also 

ARP spoofing – a technique by which an attacker sends Address Resolution Protocol messages onto a local area network
 Aspidistra transmitter a British radio transmitter used for World War II "intrusion" operations, an early MITM attack.
 Babington Plot the plot against Elizabeth I of England, where Francis Walsingham intercepted the correspondence.
 Computer security the design of secure computer systems.
 Cryptanalysis the art of deciphering encrypted messages with incomplete knowledge of how they were encrypted.
 Digital signature a cryptographic guarantee of the authenticity of a text, usually the result of a calculation only the author is expected to be able to perform.
 Evil maid attack attack used against full disk encryption systems
 Interlock protocol a specific protocol to circumvent an MITM attack when the keys may have been compromised.
 Key management how to manage cryptographic keys, including generation, exchange and storage.
 Key-agreement protocol a cryptographic protocol for establishing a key in which both parties can have confidence.
 Man-in-the-browser a type of web browser MITM
 Man-on-the-side attack a similar attack, giving only regular access to a communication channel.
 Mutual authentication how communicating parties establish confidence in one another's identities.
 Password-authenticated key agreement a protocol for establishing a key using a password.
 Quantum cryptography the use of quantum mechanics to provide security in cryptography.
 Secure channel a way of communicating resistant to interception and tampering.

References

External links
 Finding Hidden Threats by Decrypting SSL (PDF). SANS Institute.

Cryptographic attacks
Computer network security
Transport Layer Security