Encryption, from the ground up · Puzzle 4 · 5 of 9

The man in the middle

Puzzle 3 ended with a gap: a key exchange gives you a secret shared with whoever answered. Here's the attack that fills that gap, step by step, and the one thing that would stop it.

The problem

Puzzle 3's key exchange gives your app and "the server" a secret nobody else can compute from the public values. But who sent the public value your app received? The phone can't see a server; it only sees bytes arriving. What can someone in the middle do with that?

See it

The app sends its public value, the attacker keeps it and sends its own to the server; the server's public value is swapped the same way; the app shares secret one with the attacker and the server shares secret two with the attacker; the attacker decrypts, reads and re-encrypts every message app attacker server app'sattacker's server'sattacker's secret 1secret 2 decrypt, read, re-encrypt
  1. Your app sends its public value. The attacker keeps it and sends the server its own, pretending to be you.

    Step 1 of 4
  2. The server's public value is swapped the same way, on the way back.

    Step 2 of 4
  3. Your app shares one secret with the attacker; the server shares another with the attacker. Predict: what can it do now?

    Step 3 of 4
  4. Decrypt, read or change, re-encrypt, pass on: a man-in-the-middle attack. Only proof of who sent each public value stops it.

    Step 4 of 4

The building block

The listener from puzzle 0 now acts. It sits between your app and the server and intercepts the key exchange:

  1. Your app sends its public value. The attacker keeps it, and sends the server its own public value instead, pretending to be your app.
  2. The server answers with its public value. The attacker keeps it, and sends your app its own, pretending to be the server.
  3. Your app computes a shared secret with the attacker. The server computes a different shared secret with the attacker. Neither knows.

This is a man-in-the-middle attack. From now on the attacker decrypts each message with one secret, reads it or changes it, encrypts it again with the other secret, and passes it on. Both sides see working encryption, and every byte goes through the attacker.

Encryption didn't fail: it worked perfectly, with the wrong party. What's missing is proof that the public value really came from your server. That proof needs something only the real server can produce, which the attacker can't fake: puzzle 7 builds it, and HTTPS uses it on the exchange itself, so a swapped public value is caught.

The analogy

Two people agree to post locked boxes to each other, but a postman intercepts both. He gives each of them his own box to lock their message into, opens each one with his key, reads it, re-locks it in the other person's box, and passes it on. Both believe the box was the other person's.

Check yourself

In a man-in-the-middle attack, who does your app share its secret with?

Not quite. The exchange only guarantees a secret with whoever answered.

Right. With whoever answered.

Not quite. The exchange succeeds; it just succeeds with the attacker.

Why doesn't encryption stop a man in the middle?

Not quite. Length doesn't matter if you share the secret with the wrong party.

Not quite. It doesn't need to break anything; it has the keys.

Right. Encrypted, but to the wrong party.

What's the difference between a listener and a man in the middle?

Not quite. That's the two swapped. Re-read that part ↑

Right. Watching vs standing in between.

Sources

  • RFC 4949, Internet Security Glossary ("man-in-the-middle attack: A form of active wiretapping attack in which the attacker intercepts and selectively modifies communicated data to masquerade as one or more of the entities"; with an unauthenticated Diffie-Hellman exchange, the attacker could "establish separate session keys with each of them, and … function as a clandestine proxy server between them")

What you now know

A man in the middle intercepts the key exchange and runs two: one with your app, pretending to be the server, and one with the server, pretending to be your app. Each side ends with a shared secret, but shares it with the attacker, who decrypts, reads or changes everything, and re-encrypts it. Encryption doesn't help. What stops it is proof of who sent the public values: a signature, puzzle 7.