Writeup · 31 Nights of Root

Quickline

cryptounknownsolvedFlag: r00t{REDACTED}

Overview

A logistics company’s “lightweight encrypted memo” tool. Four intercepted memos sit waiting for review. Files provided: memos.json containing four hex-encoded ciphertexts:

Ref Length
SHIP-2291 299 bytes
INV-0447 231 bytes
SCHED-118 199 bytes
ACCESS-09 165 bytes

Prerequisites / Setup

  • Python 3
  • memos.json in the working directory

Recon

Step 1 — Identify the cipher

Goal: Decode the hex ciphertexts and confirm they are raw bytes sharing a keystream.

The four memos are stored as hex in memos.json under the key ciphertext_hex. Decoding them and inspecting their lengths confirmed they are just raw bytes.

XORing the ciphertexts pairwise immediately revealed strong structure:

SHIP-2291 ⊕ INV-0447
0701020c0d1155520a481b150e0a0017493f212824445d5c19574f1645...

If the ciphertexts were independent random streams, their pairwise XORs would look like uniform random bytes. Instead, the XORs contain runs of low-value bytes and readable-looking structure — the signature of a reused keystream:

Cᵢ = Pᵢ ⊕ K

where the same keystream K is XORed with every plaintext. Then:

Cᵢ ⊕ Cⱼ = Pᵢ ⊕ Pⱼ

and the keystream cancels. The challenge reduces to the classic two-time pad / many-time pad attack.

Vulnerability Analysis

The flaw class is Keystream Reuse / Many-Time Pad, equivalent to reusing a one-time pad or reusing a nonce in a stream cipher.

Exploitation

Step 2 — Recover the first 28 keystream bytes

Goal: Guess an English plaintext prefix and derive the keystream, then validate against all four memos.

The first bytes of the memos are ordinary logistics text. By guessing the plaintext prefix of one memo and XORing with its ciphertext, we get a candidate keystream. If the same keystream applied to the other memos produces sensible ASCII, the guess is correct.

The first 28 bytes were recovered this way:

SHIP-2291: Notice: shipment SHIP-2291 d
INV-0447:  Inventory reconciliation for
SCHED-118: Driver schedule change for n
ACCESS-09: Reminder: the warehouse over

XORing each of these plaintext prefixes against its ciphertext yields the same 28-byte keystream:

K[0:28] = 961bd3a731c57628a2e73e69d357efb8bcd26c7a27e5b4cb5a290b41

This is the anchor. Any subsequent keystream byte we recover is only accepted if it makes all four memos simultaneously readable.

Step 3 — Extend the keystream by cross-constrained hypothesis testing

Goal: Recover K[0:106] one byte at a time.

Beyond byte 28, automatic English scorers drifted because they selected keystream bytes independently at each position without cross-memo agreement. The reliable method is cross-constrained hypothesis testing, one position at a time.

The rule: at each new position i, hypothesise what each active memo should say next, derive the keystream byte from that hypothesis, and require at least two memos to produce the same key byte, with the other memos producing coherent ASCII.

Example at position 27: all four memos agree that the keystream byte 0x41 continues SHIP-2291 as delayed while simultaneously making INV-0447 read ...for, SCHED-118 read ...for n, and ACCESS-09 read ...over.

Working this way one position at a time, we recovered K[0:106].

Step 4 — Decrypt ACCESS-09

Goal: Apply the recovered keystream to the fourth memo and read the flag.

The confirmed keystream, as bytes 0–105:

961bd3a731c57628a2e73e69d357efb8bcd26c7a27e5b4cb5a290b41
1a873fd2774a37e1340bee97a690b5f3efa2a1d68ce3fbec01be
ce5c44133f09a25e9480d2f203cd9ce9f2e3e0dfef8bfedffc0f3e
92e0232d5b0bc95eba0e24eab9eb99526d796d95aaa52da6ad8c7e
a6fc160245299c070cf522040c40b998de33ffde42a764e07c90e5
3890f390176bc9c66caca99ee28fb231bc41293a710c2ce2dc2c3
42789b44e48116e2b78441a0c82b414b3a513307263e44280078
9ace847845814068e2025e82c4da9c1f8762935f4e8b825899811
440f8c1b293cdf973721acc04405ac35bc79a93d58080035e08c
e191707cf13011b15c9c688930940981406ca472000425ea0808d8
899450040189f84ce4b5b1a4ec59a0840c21a1a4395435e02d75f
4b

Decrypting the four ciphertexts with K[0:106] gives:

SHIP-2291: Notice: shipment SHIP-2291 delayed at the Mombasa port due to
           customs paperwork. Revised ETA is now Thursday
INV-0447:  Inventory reconciliation for bay 4 completed. Discrepancy of 3
           pallets resolved -- found misfiled under bay 6 during
SCHED-118: Driver schedule change for next week: Amara covers the northern
           route Monday through Wednesday, Otieno takes over Thursday
ACCESS-09: Reminder: the warehouse override code for the night shift
           this cycle is r00t{REDACTED}

The flag sits at positions 72–105 of ACCESS-09, embedded directly in the plaintext.

Step 5 — Verify the flag bytes

Goal: Confirm each flag byte by dumping ciphertext byte, keystream byte, and plaintext byte.

For ACCESS-09 at positions 72–105:

pos 72:  C=0x0f  K=0x7d  P=0x72  'r'
pos 73:  C=0xc2  K=0xf2  P=0x30  '0'
pos 74:  C=0xbc  K=0x8c  P=0x30  '0'
pos 75:  C=0xf5  K=0x81  P=0x74  't'
pos 76:  C=0x25  K=0x5e  P=0x7b  '{'
pos 77:  C=0x5d  K=0x3e  P=0x63  'c'
pos 78:  C=0x8a  K=0xfe  P=0x74  't'
pos 79:  C=0xf9  K=0x8b  P=0x72  'r'
pos 80:  C=0xa1  K=0xfe  P=0x5f  '_'
pos 81:  C=0xb2  K=0xdf  P=0x6d  'm'
pos 82:  C=0xcc  K=0xfc  P=0x30  '0'
pos 83:  C=0x6b  K=0x0f  P=0x64  'd'
pos 84:  C=0x0d  K=0x3e  P=0x33  '3'
pos 85:  C=0xcd  K=0x92  P=0x5f  '_'
pos 86:  C=0x8e  K=0xe0  P=0x6e  'n'
pos 87:  C=0x10  K=0x23  P=0x33  '3'
pos 88:  C=0x1e  K=0x2d  P=0x33  '3'
pos 89:  C=0x3f  K=0x5b  P=0x64  'd'
pos 90:  C=0x78  K=0x0b  P=0x73  's'
pos 91:  C=0x96  K=0xc9  P=0x5f  '_'
pos 92:  C=0x6a  K=0x5e  P=0x34  '4'
pos 93:  C=0xe5  K=0xba  P=0x5f  '_'
pos 94:  C=0x68  K=0x0e  P=0x66  'f'
pos 95:  C=0x56  K=0x24  P=0x72  'r'
pos 96:  C=0xd9  K=0xea  P=0x33  '3'
pos 97:  C=0xca  K=0xb9  P=0x73  's'
pos 98:  C=0x83  K=0xeb  P=0x68  'h'
pos 99:  C=0xc6  K=0x99  P=0x5f  '_'
pos 100: C=0x3c  K=0x52  P=0x6e  'n'
pos 101: C=0x5d  K=0x6d  P=0x30  '0'
pos 102: C=0x17  K=0x79  P=0x6e  'n'
pos 103: C=0xed  K=0x8e  P=0x63  'c'
pos 104: C=0xea  K=0xd9  P=0x33  '3'
pos 105: C=0x27  K=0x5a  P=0x7d  '}'

Which spells exactly:

r00t{REDACTED}

Each of these keystream bytes is also constrained by the other three memos at the same positions, so the plaintext is not just printable ASCII — it is the only keystream that keeps SHIP-2291, INV-0447, and SCHED-118 readable at the same time.

Flag

r00t{REDACTED}

Embedded at positions 72–105 of ACCESS-09, decrypted with the recovered reused keystream.

Lessons / Takeaways

  • A stream cipher is only as strong as the uniqueness of its keystream. Reusing a keystream across messages is equivalent to reusing a one-time pad, and the security collapses immediately.
  • In a many-time pad, XOR-of-ciphertexts is the primary tool: it cancels the key and exposes the XOR of plaintexts, which is enough to recover English text with cribbing.
  • Automatic scoring heuristics can drift when the constraint weakens. Manual cross-memo hypothesis testing is slower but does not drift, because it requires every accepted keystream byte to make all active memos readable.
  • The flag itself names the fix: use a fresh nonce per message, so the keystream never repeats. The flag body is leet for ctr_mode_needs_a_fresh_nonce.

Mitigations

  • Use a fresh nonce / IV per message so the keystream never repeats.
  • Prefer authenticated encryption (e.g. AES-GCM, ChaCha20-Poly1305) over unauthenticated stream ciphers.
  • Never reuse a one-time pad key.

Techniques

References