$ cat writeup.md…
$ cat writeup.md…
fortid2026
Task: networked PyCryptodome vault encrypts secrets with AES-CTR but derives the CTR nonce from the plaintext's first 8 bytes under a fixed per-session key. Solution: exploit keystream reuse (two-time pad) by submitting chosen plaintexts that share the flag's 8-byte prefix, brute-force the unknown 8th byte via ciphertext-prefix collision, then XOR-recover every flag byte against a long chosen plaintext.
Safely Store a Secret Value. A networked "secret vault" lets you store secrets and export the stored ciphertexts. The flag is pre-loaded as the first vault entry.
English summary: A TLS-wrapped ncat service (port 1337) runs a Python/PyCryptodome vault. Options: (1) store a secret, (2) export all ciphertexts as hex JSON, (3) exit. The flag is auto-inserted as Secret #0 at startup. A 15-second SIGALRM limits each session. Goal: recover the flag (FortID{...}).
The cipher class is the whole vulnerability:
class Cipher: def __init__(self): self.key = get_random_bytes(16) # fixed for the whole session def encrypt(self, plaintext): cipher = AES.new(self.key, AES.MODE_CTR, nonce=plaintext[:8]) return cipher.encrypt(plaintext)
Key observations:
insert.plaintext[:8] — attacker-controlled and, crucially, identical whenever two plaintexts share their first 8 bytes.ct[i] = pt[i] XOR ks[i], where the keystream ks depends only on (key, nonce).Therefore the keystream depends only on (key, first 8 bytes of plaintext). Any two plaintexts beginning with the same 8 bytes get the same keystream — a classic two-time pad / keystream-reuse situation.
The flag's prefix is partially known: FLAG.startswith("FortID{"), so bytes 0–6 are "FortID{". Only the 8th byte (index 7) is unknown, and it is drawn from a small printable charset. The export option leaks every ciphertext including Secret #0 (the flag), so we can compare ciphertexts directly.
Recover the unknown 8th byte via a ciphertext-prefix collision. For each candidate character c, submit a chosen secret "FortID{" + c + ("A"*300). When c equals the flag's real 8th byte, the chosen plaintext shares the flag's full 8-byte nonce, so both share an identical keystream. Because the first 8 bytes of both plaintexts are then equal and are XORed with the same keystream, their ciphertext prefixes collide: chosen_ct[:8] == flag_ct[:8]. Detecting that collision reveals the 8th byte (here it was 'S', prefix "FortID{S").
Recover the full flag by XOR against the chosen plaintext. With identical keystreams, for every index j:
ks[j] = chosen_ct[j] XOR chosen_pt[j]
flag[j] = flag_ct[j] XOR ks[j]
= flag_ct[j] XOR chosen_ct[j] XOR chosen_pt[j]
The 300-byte filler guarantees the chosen plaintext (and thus the keystream) is long enough to cover the whole flag (~70 bytes).
Pipeline everything into one write. The 15-second alarm forbids a round-trip per candidate, so send all 1\n<secret>\n store commands plus 2\n3\n (export + exit) in a single sendall, then read the full response.
Extract the leaked JSON. The export is one long line buried inside banner/menu output; pull it out with a regex \{"Secret #0".*?\} rather than line-splitting.
#!/usr/bin/env python3 import socket, ssl, json, string, time, re HOST = "<challenge-host>" PORT = 1337 ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE raw = socket.create_connection((HOST, PORT), timeout=10) s = ctx.wrap_socket(raw, server_hostname=HOST) s.settimeout(10) charset = string.ascii_letters + string.digits + "_!@#$%^&*()-+=:;,.?/<>[]|~" prefix7 = "FortID{" FILLER = "A" * 300 # Build all store commands + export + exit in one payload (15s alarm). candidates = [] payload = b"" for c in charset: pt = prefix7 + c + FILLER # shares the flag's nonce iff c is correct candidates.append((c, pt)) payload += b"1\n" + pt.encode() + b"\n" payload += b"2\n3\n" s.sendall(payload) # Drain the response. data = b"" start = time.time() while time.time() - start < 12: try: chunk = s.recv(65536) except Exception: break if not chunk: break data += chunk s.close() # Pull the exported ciphertext JSON out of the banner/menu noise. m = re.search(rb'\{"Secret #0".*?\}', data, re.S) entries = json.loads(m.group(0).decode()) flag_ct = bytes.fromhex(entries["Secret #0"]) # Find the candidate whose ciphertext prefix collides with the flag's. for i, (c, pt) in enumerate(candidates): ct = bytes.fromhex(entries.get(f"Secret #{i+1}", "")) if ct[:8] == flag_ct[:8]: pt_bytes = pt.encode() n = min(len(flag_ct), len(ct), len(pt_bytes)) # flag[j] = flag_ct[j] XOR chosen_ct[j] XOR chosen_pt[j] flag = bytes(flag_ct[j] ^ ct[j] ^ pt_bytes[j] for j in range(n)) print("8th byte:", repr(c)) print("FLAG:", flag.rstrip(b"\x00").split(b"}")[0].decode() + "}") # FortID{REDACTED} break
Use this technique when:
nonce=plaintext[:8]) or otherwise lets the attacker control or predict the nonce.FortID{).target[j] = target_ct[j] XOR chosen_ct[j] XOR chosen_pt[j].chosen_ct[:8] == target_ct[:8]) is a reliable detector that two plaintexts share the same (key, nonce) keystream.$ cat /etc/motd
Liked this one?
Pro unlocks every complete writeup and expanded API access. $9/mo.
$ cat pricing.md$ grep --similar