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
Your app sends its public value. The attacker keeps it and sends the server its own, pretending to be you.
Step 1 of 4The server's public value is swapped the same way, on the way back.
Step 2 of 4Your app shares one secret with the attacker; the server shares another with the attacker. Predict: what can it do now?
Step 3 of 4Decrypt, 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:
- Your app sends its public value. The attacker keeps it, and sends the server its own public value instead, pretending to be your app.
- The server answers with its public value. The attacker keeps it, and sends your app its own, pretending to be the server.
- 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
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.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.