Challenges › Cryptography

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.

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.

  1. Choose private exponents u and v. Send g^u mod p to Bob and g^v mod p to Alice.
  2. Compute the shared value with Bob, B^u mod p, and with Alice, A^v mod p. Derive both keys.
  3. Verify and decrypt Alice's message with the key you share with Alice.
  4. 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.
  5. Run python3 verify.py <message> to check the message.

The flag is CSEMA{<message>}.

Tags: learn

Try next