Intercept a Diffie-Hellman Exchange
medium · 100 XP · machine with a lab · free · solves: 0
Alice and Bob agree on a key with Diffie-Hellman, but nothing authenticates their public values. Every message between them passes through you. Alice then sends Bob one message with AES-256-GCM. Read it, and pass it on so that Bob accepts it.
Start the lab. The service runs on host oracle, TCP port 9000.
Setup. p is a 512-bit prime. g = 2 generates the full multiplicative group, of order p - 1. Alice and Bob pick new private exponents on each connection, so do the whole exchange on one connection.
Key derivation. Each side encodes its shared value as an unsigned big-endian integer of exactly ceil(p.bit_length() / 8) = 64 bytes, with leading zero bytes when needed. The AES key is SHA-256 of those 64 bytes. Messages use AES-256-GCM with a 12-byte nonce, a 16-byte tag and empty associated data.
Protocol. The service sends five banner lines when you connect: a help line, p = ..., g = ..., Alice sends A = ... and Bob sends B = .... Send one command per line. Numbers are decimal. Bytes are hex.
to_alice <value>: deliver a public value to Alice as if it came from Bob. Alice derives her key and repliesalice: <nonce> <ciphertext> <tag>, her encrypted message to Bob.to_bob <value>: deliver a public value to Bob as if it came from Alice. Bob derives his key and repliesbob: key ready.to_bob_message <nonce> <ciphertext> <tag>: deliver a message to Bob. Bob repliesbob: acceptedorbob: rejected, <reason>. Bob rejects a reused nonce, a tag that does not verify, and any message that is not the one from Alice.- Public values must be from 2 to p - 2. You can send
to_aliceandto_bobonce each, andto_bob_messagefive times, on one connection. - A bad command gets one reply line that starts with
error:. A line longer than 1024 characters getserror: line too longand closes the connection.
client.py holds the connection for your scripts, or lets you type commands. aesgcm.py encrypts and decrypts AES-256-GCM in plain Python.
Your task.
- Choose private exponents u and v. Send g^u mod p to Bob and g^v mod p to Alice.
- Compute the shared value with Bob, B^u mod p, and with Alice, A^v mod p. Derive both keys.
- Verify and decrypt Alice's message with the key you share with Alice.
- Encrypt the same message with the key you share with Bob and a fresh random 12-byte nonce. Send it to Bob until he replies
bob: accepted. - Run
python3 verify.py <message>to check the message.
The flag is CSEMA{<message>}.
Tags: learn
Try next
- Read the Vault's Timing (medium)
- Recover the Hidden Suffix (medium)
- Recover Plaintext with a Padding Oracle (medium)