AES-256-CTR with a fixed nonce and a fresh cipher object per message. Five log entries come out encrypted, four of them boring "SYSTEM: ..." lines whose plaintext is sitting right there in the source. That's enough to leak the keystream and decrypt the fifth entry - the flag - for free.
Hint
CTR mode turns a block cipher into a stream cipher. Stream ciphers and two-time pads are old enemies.
Reading the script
1nonce = b"\x00" * 8
2cipher = AES.new(key, AES.MODE_CTR, nonce=nonce)
3return cipher.encrypt(plaintext)
Insight
AES.new(key, MODE_CTR, nonce=nonce) defaults initial_value=0. With the
key and nonce both fixed and the counter restarting at zero on every
call, the keystream is a pure function of those three values and is
identical across every message encrypted this way. The five
ciphertexts are all XORed against the exact same keystream - a textbook
two-time pad, just spread across more than two messages. The flavor text
("fixed nonce ensures consistent message ordering in the archive") is the
decoy explanation for a very real bug.
Recovering the keystream
The first four entries are SYSTEM log lines, and their plaintext is
hardcoded in challenge.py:
1known = [
2 b"SYSTEM: Server started at 2024-01-15T08:00:00Z",
3 b"SYSTEM: Authentication successful for user admin",
4 b"SYSTEM: Backup job finished with status SUCCESS",
5 b"SYSTEM: Config update applied to all nodes OK!!",
6]
XOR any one against its ciphertext and the keystream falls out. All four agree on their first 46 bytes - more than enough to cover the 31-byte flag entry:
1import re
2
3txt = open("output.txt").read()
4cts = {int(m.group(1)): bytes.fromhex(m.group(2))
5 for m in re.finditer(r"ct\[(\d+)\]\s*=\s*([0-9a-fA-F]+)", txt)}
6
7streams = [xor(cts[i], known[i]) for i in range(len(known))]
8ks = max(streams, key=len)
9
10flag_ct = cts[max(cts)]
11flag = bytes(c ^ k for c, k in zip(flag_ct, ks))
12print(flag.decode())
Note
Independent confirmation before trusting the decryption: if the nonce
really were unique per message, ct_i ^ ct_j would equal pt_i ^ pt_j
only coincidentally, if at all. Here, every log line overlaps by at least
46 bytes, and ct[i] ^ ct[j] equals known[i] ^ known[j] over that
overlap for every pair (i, j) - the keystream really is shared
across all five messages, not just a lucky two-message collision.
L3m0n{n0nc3_r3us3_1s_d4ng3r0us}
Pitfall
Worth flagging for anyone replaying this: CTR mode's safety depends
entirely on the (key, nonce, counter) triple never repeating. A fresh
AES.new() call per message looks safe at a glance - new cipher object!
- but if the nonce argument is constant and the initial counter always defaults to 0, "fresh object" buys you nothing.