#!/usr/bin/env python3 """ modern-crypto-101 (NHNC 2026) — CTR keystream reuse The organiser encrypts every ticket with AES-CTR under the same 16-byte KEY and the same 8-byte NONCE = b"ticket42", starting the counter at zero each time. Same key + same nonce + same starting counter = identical keystream on every call, so: C_i = P_i XOR K (single, reused K) If any P_i is fully known, K = C_i XOR P_i and every other ciphertext under that same nonce decrypts to P_j = C_j XOR K. We reconstruct guest 2's plaintext byte-for-byte (the JSON template is fixed in chall.py, and the (name, seat) values come from public.txt), recover the keystream, and use it to decrypt admin_cipher — flag included. """ from __future__ import annotations import json import re from pathlib import Path HERE = Path(__file__).resolve().parent ARTIFACTS = HERE / "artifacts" # ---------------------------------------------------------------------- # Rebuild guest plaintexts using the same code path as chall.py. # ---------------------------------------------------------------------- def encode_ticket(ticket: dict) -> bytes: # separators=(",", ":") matches chall.py exactly — no spaces. return json.dumps(ticket, separators=(",", ":")).encode() def make_guest(name: str, seat: str) -> bytes: return encode_ticket({ "event": "modern-crypto-101", "role": "guest", "name": name, "seat": seat, "note": "enjoy the workshop", }) # ---------------------------------------------------------------------- # Load the handout files. # ---------------------------------------------------------------------- def read_attendees() -> list[tuple[str, str]]: rows = ARTIFACTS.joinpath("public.txt").read_text().strip().splitlines() assert rows[0] == "name,seat", rows[0] # NOTE: public.txt is CSV with no quoting. The names don't contain commas, # so a bare split() suffices; if they ever did, use the csv module. return [tuple(line.split(",", 1)) for line in rows[1:]] def read_ciphertexts() -> dict[str, bytes]: ct = {} for line in ARTIFACTS.joinpath("output.txt").read_text().splitlines(): m = re.match(r"(\w+)\s*=\s*([0-9a-fA-F]+)$", line) if m: ct[m.group(1)] = bytes.fromhex(m.group(2)) return ct # ---------------------------------------------------------------------- # XOR helper. # ---------------------------------------------------------------------- def xor(a: bytes, b: bytes) -> bytes: return bytes(x ^ y for x, y in zip(a, b)) # ---------------------------------------------------------------------- # Solve. # ---------------------------------------------------------------------- def main() -> None: attendees = read_attendees() ct = read_ciphertexts() # Pick the guest with the longest plaintext (index 2 — the wall-of-read # attendee) so the recovered keystream is long enough to cover the whole # admin_cipher. lengths = [len(make_guest(n, s)) for n, s in attendees] victim = lengths.index(max(lengths)) name, seat = attendees[victim] pt_victim = make_guest(name, seat) ct_victim = ct[f"guest_cipher_{victim}"] assert len(pt_victim) == len(ct_victim), ( f"length mismatch on guest {victim}: pt={len(pt_victim)} ct={len(ct_victim)}" ) keystream = xor(pt_victim, ct_victim) print(f"[*] recovered {len(keystream)}-byte keystream via guest {victim}") # Sanity check: decrypting every other guest with the same keystream # should reproduce its known plaintext exactly. for i, (n, s) in enumerate(attendees): if i == victim: continue expected = make_guest(n, s) recovered = xor(ct[f"guest_cipher_{i}"], keystream[: len(expected)]) if recovered != expected: raise SystemExit(f"[-] guest {i} mismatch — keystream is wrong") print(f"[+] guest {i} plaintext matches ({n!r} @ {s!r})") # Decrypt admin_cipher. admin_ct = ct["admin_cipher"] assert len(admin_ct) <= len(keystream), ( f"admin ciphertext ({len(admin_ct)}B) is longer than keystream " f"({len(keystream)}B); pick a longer guest." ) admin_pt = xor(admin_ct, keystream[: len(admin_ct)]) admin = json.loads(admin_pt) print() print("== admin ticket ==") print(json.dumps(admin, indent=2)) print() print(f"[+] FLAG: {admin['flag']}") if __name__ == "__main__": main()