Full name: Man-in-the-middle attack.
Definition
A MITM attack, or man-in-the-middle attack, is an active attack on the communication between two parties. The attacker sits on the path between them and relays their messages, and can read, change, drop or insert messages, while each party believes it is talking directly to the other. Against a key agreement, the attacker runs one key agreement with each party, so each party shares a key with the attacker instead of with its intended partner. Encryption does not prevent the attack; authenticating the key agreement to the intended partner does.
The idea
A phone A wants to set a thermostat B. The two devices agree a key by Diffie–Hellman key agreement §19.1.4, in which each party sends a public value derived from a private number and combines its own private number with the other’s public value; both reach the same shared secret, which a listener cannot compute. An attacker M within radio range intercepts A’s public value and sends M’s own to B, and does the same in the other direction. A now shares one key with M, and B shares another key with M. M decrypts each message from A, reads or changes it, encrypts it again under B’s key and sends it on. No cipher is broken. The mistake is about who holds the other end of each key.
Encryption answers whether anyone other than the key holders can read a message. The MITM attack exploits a different question: who the key holders are. A key agreement defeats a passive attacker, who only listens, but on its own it cannot tell a partner from an impostor, because any message it receives may come from the attacker. Picture sealed envelopes passed through a messenger who opens each one, reads it and reseals it in a new envelope: the seals are perfect, but the messenger is inside them. Unlike a resealed envelope, the attack leaves no trace: the two parties see only well-formed encrypted traffic. The attack is also called machine-in-the-middle or adversary-in-the-middle, with the same abbreviation.
How it works
The attack runs in three steps.
Getting into the path. The attacker makes each party’s traffic reach the attacker, for example by advertising under the name of the real device, so that the phone connects to the attacker while the attacker connects to the real device.
Substituting keys. A sends its public value , and M passes M’s own public value to B instead. B sends , and M passes to A. A computes key from its private number and ; B computes key from its private number and ; M computes both.
Relaying. M decrypts each message with one key, reads it, changes it if M wants to, and encrypts it with the other key.
The defense is to authenticate the key agreement: to tie the public values that a party used to the identity of the partner it intended, with something the attacker cannot produce. Every working defense has the same shape: a check computed over the public values as this party saw them, which only the intended partner can pass.
A long-term key §7.28 known in advance. The partner signs the exchanged values with a private key whose public key the party already holds, or proves knowledge of a secret key that the two already share.
A certificate. A trusted authority has signed a statement that binds the partner’s public key to the partner’s name, and the partner signs the exchange with that key. Secure web connections work this way.
A check by a person. A person compares or carries a short value tied to the exchange, over a path the attacker cannot reach: the screens and keyboards of the two devices, or a second channel. BLE §7.1 (Bluetooth Low Energy) pairing §7.17 works this way.
The Authenticated key exchange and transcript binding §19.1.6 entry treats the first two defenses in general.
In BLE pairing, MITM protection means an authenticated key, and the association model supplies the check:
numeric comparison §7.24: both devices display a number computed from both public keys and both devices’ random values, and the user confirms that the numbers are equal;
passkey entry §7.23: each device proves, one bit at a time, that it knows a six-digit passkey which the user carried between the devices, in commitments that include both public keys;
out-of-band association §7.25: a commitment to a device’s public key arrives over another channel, such as NFC (Near Field Communication).
In each case the public keys enter the check, so a substituted key changes the checked value. Just Works §7.22 has no such check and gives no MITM protection.
Worked example
This example uses numbers far too small to be secure, so that every step can be checked by hand. Here Diffie–Hellman key agreement works modulo the prime with base : a party with private number sends the public value , the remainder of after division by . Write when and leave the same remainder after division by 23. A’s private number is 6, B’s is 15 and M’s is 13.
, so .
, and . Then , so .
, so .
Without M, A computes and B computes , and both get the key 2. With M substituting in both directions, two facts shorten the work: , and , so .
A computes , so . M computes , the same key.
B computes , so . M computes , the same key.
A believes it shares the key 18 with B; B believes it shares the key 7 with A; M holds both. A encrypts “set 20 degrees” under 18. M decrypts it, encrypts “set 30 degrees” under 7, and B accepts a correctly encrypted command.
Now let each device display the two public values it used, the initiating device’s value first; A started the exchange. A shows (8, 21) and B shows (21, 19). The pairs differ, so a user who compares them sees the attack. Without M, both devices would show (8, 19). Numeric comparison in BLE does the same with 256-bit public keys, mixing them with random values into a six-digit number.
Stated precisely
Let A and B be the intended parties and M the attacker, who can read, change, drop and insert every message between them. Let be the key that A computes in a session in which A believes its partner is B. The Handbook of Applied Cryptography by Menezes, van Oorschot and Vanstone [518] names two properties:
implicit key authentication §1.6 to A: no party other than A and B can compute ;
key confirmation §5.42 to A: A has evidence that its partner has actually computed .
A MITM attack on the key agreement succeeds against A exactly when A accepts as shared with B and M can compute .
Unauthenticated Diffie–Hellman key agreement keeps secret from a passive attacker only. It gains implicit key authentication when A accepts only after a check that (1) is computed over the public values as A received them and (2) needs something M cannot obtain: B’s private key, a secret shared only by A and B, or a value carried over a path that M cannot alter. The guarantee rests on three assumptions: M cannot obtain that credential; the credential belongs to the intended B and not to another party; and M cannot compute the shared secret from the two public values, which is the Diffie–Hellman problem §19.1.3.
Limits and common mistakes
An encrypted connection can end at the attacker. Encryption, even with change detection, protects a message only between the holders of the key.
Authentication proves who is at the other end, not where. In a relay attack, an attacker forwards unchanged messages between two genuine parties that are far apart, for example between a car and its owner’s key fob in another room, and the car opens because it believes the fob is near. Each message is authentic, so authenticating the endpoints does not stop the relay. Distance bounding, which measures the round-trip time of a challenge and response, does.
The intended identity must be stated. A BLE pairing binds the key to the physical device the user handled; a certificate binds it to a name chosen by an authority. Neither says that the partner is honest or authorized to do what it asks.
When data passes through a middle device on purpose, such as a phone that forwards a server’s data to a sensor, each link can be authenticated and the middle device can still change the data, because it legitimately holds the keys of both links. Protection that must survive the middle device needs a signature or a message authentication code §5.7 over the data itself, checked by the final receiver; the Channel confidentiality versus object authenticity §7.34 entry explains this.
An attacker in the path can also alter the negotiation that comes before the key agreement, for example to make both parties choose a weaker method. This downgrade attack §1.27 is a MITM attack on the negotiation. The defense is that each party refuses any outcome below its own minimum, or that the final check covers the negotiation messages.
After an authenticated key agreement, an attacker in the path can still block traffic, which denies service, but cannot read or change it undetected without the key.
Questions answered
What binds the peer’s key exchange to the intended device identity?
A check that is computed over the exchanged public keys, and that only the intended device can pass, binds the key exchange to that device; encryption alone binds nothing. In a key exchange each side sends a public key and both compute a shared key. An attacker in the middle can replace the public keys and end up sharing one key with each side, which is a man-in-the-middle (MITM) attack. The binding check makes the replacement visible, because a replaced key changes the checked value, and the attacker cannot produce a value that passes.
The check needs something the attacker cannot obtain. In general protocols, this is a signature by the device’s long-term private key, whose public key is known in advance or certified by a trusted authority, or a secret key that the two sides already share. In BLE (Bluetooth Low Energy) pairing, it is the association model: in numeric comparison, the user confirms that both devices show the same six-digit number computed from both public keys and random values; in passkey entry, each device proves bit by bit, in commitments that include both public keys, that it knows a passkey the user carried between the devices; in out-of-band association, a commitment to the device’s public key arrives over another channel, such as NFC (Near Field Communication). The identity bound this way is the physical device the user handled, not its advertised name; Just Works binds nothing.
Related entries: LE Secure Connections (§7.21), Numeric comparison (§7.24), Out-of-band (OOB) association (§7.25), Authentication (§1.6), Channel confidentiality versus object authenticity (§7.34), Diffie–Hellman key agreement (§19.1.4), Authenticated key exchange and transcript binding (§19.1.6), Downgrade attack (§1.27). Further reading: [18, 518].