DEAD LETTER

A letter is stuck at the relay station. The logs are filled with confirmations, retries, and cancellations, yet the recipient has never seen it appear. Find the ticket that makes the system truly complete the final delivery.

Recon

First contact with the relay

The handout zip holds a single file, dead_letter, so before opening a disassembler we ran it to see what it does.

$ file dead_letter
dead_letter: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, stripped

It is a Linux x86-64 executable. With no input at all the relay introduces itself:

$ printf '' | ./dead_letter
DEAD LETTER / relay 17
One 32-byte ticket on stdin; finish input with EOF.
Delivery rejected.
exit=1

The banner states the input contract up front, one 32-byte ticket terminated by EOF, and a rejected delivery exits 1. A strings pass then shows three complete flag-shaped strings sitting next to the two verdicts:

$ strings dead_letter | grep -E 'NVR|Delivery'
Delivery rejected.
Delivery committed.
NVR{retry_queue_drained}
NVR{draft_accepted_by_relay}
NVR{mailbox_delivery_confirmed}

Embedded flags cost nothing to try, so we fed all three decoys to the binary, and every one was rejected. One example:

$ printf 'NVR{mailbox_delivery_confirmed}\n' | ./dead_letter
DEAD LETTER / relay 17
One 32-byte ticket on stdin; finish input with EOF.
Delivery rejected.
exit=1

Then we poked the input to map out the acceptance rules. Every probe below printed the same banner and Delivery rejected. with exit 1:

  • hello (5 bytes) and 33 As were both rejected, so the length is enforced exactly, not as a minimum.
  • 32 As was rejected, so a correct length alone is not enough and real content checks exist.
  • 32 bytes with one A replaced by a space (0x20) were rejected, so the accepted range starts above 0x20.

The gate is exactly 32 bytes on one EOF-terminated line, with each byte in a narrower range than printable ASCII, and the actual decision happens behind that gate. That is when we opened the disassembler.

How main reads the ticket

The import table holds only getc, puts, and stdin, so every check is hand-rolled rather than delegated to a libc helper. main at 0x10a0 prints the banner and prompt, then runs three pointer sanity checks at 0x4010, 0x4018, and 0x4020. Those pointers aim at the decoy strings and only need to be non-NULL, which is why the decoys exist in strings yet never appear in any output. main then reads stdin byte by byte with getc into a stack buffer, strips a trailing \r, requires EOF right after a \n, demands exactly 32 bytes (cmp rbx, 0x20 at 0x1143) each within 0x21..0x7e (lea ecx, [rdx-0x21] followed by cmp cl, 0x5d at 0x119c), and passes the buffer to a validator at 0x12f0. A nonzero return prints Delivery committed., anything else Delivery rejected. The disassembled range 0x21..0x7e matches what the space probe showed at runtime.

The validator is a tiny bytecode VM

The validator turned out to be an interpreter for a 40-instruction program stored at .rodata 0x2038, 16 bytes per instruction, dispatched through a jump table at 0x2004. Each instruction has this layout:

Offset Field Role
+0 op opcode, must be ≤ 6
+1 B byte mixed into the index derivation
+2 shift rotate amount, must be 1..31 (checked at 0x13e4)
+3 magic must equal 0x5d on every instruction (cmp byte [rsi+3], 0x5d at 0x134c)
+4 K 32-bit key
+8 E 32-bit addend
+12 F 32-bit expected CRC state

The VM keeps a running key in edi (init 0xb3197c65), a running CRC-32 state in ebx (init 0x142fd603), and a counter in r10d (init 0x17), plus a seen-index mask, a mismatch accumulator, and an armed flag. Dispatch goes through the jump table at 0x2004; the handler blocks at 0x148d and 0x14b4 are the opcode 1 and 2 entries of that table.

The reconstructed opcodes, with ROL/ROR meaning 32-bit rotate left/right:

op Name Semantics
1 MIX key = ROL(key ^ K, (ctr % 31) + 1) + 0x6d2b79f5, crc ^= K, ctr++
2 ARM reject if already armed; checkpoint {key, crc, ctr}; clear masks; set armed
3 STEP read and check one ticket dword (details below)
4 COMMIT fold the group’s seen mask and mismatches into the global accumulators; disarm
5 ROLLBACK restore {key, crc, ctr} from the checkpoint; discard everything since the ARM
6 FINAL accept only at pc == 0x27, with no group armed, seen mask 0xff, and no committed mismatch

STEP is the only opcode that looks at the ticket. It derives idx = ((key >> 24) ^ K ^ B) & 7, loads w = ticket_dword[idx], and computes v = ROL(w ^ key, shift) + (E ^ ctr). The addend XORs the counter into E at 0x1422 (xor eax, r10d), and missing that one instruction quietly breaks any solver built from the disassembly. The value v then goes through a bit-by-bit reflected CRC-32 (the standard zlib checksum, polynomial 0xedb88320, bytes processed LSB-first) chained onto the running state, and the instruction demands F == ~crc_raw. A mismatch is ORed into the group’s accumulator instead of aborting. After the check the key advances as key = ROL(key - 1 - crc_raw, 7) ^ E, the index is marked seen, and the counter ticks.

Exploitation

Mapping the delivery log

The op byte sits in the clear at offset +0 of each instruction, so the group structure reads out of the program without any ticket knowledge. The 40 instructions form five ARM-delimited groups, with MIX instructions padding between them and a MIX plus FINAL at the end:

Group PCs Opcodes Ends in
1 1-11 ARM, 8 STEP, MIX, ROLLBACK ROLLBACK
2 13-18 ARM, STEP, MIX, 2 STEP, COMMIT COMMIT
3 19-24 ARM, 4 STEP, ROLLBACK ROLLBACK
4 26-33 ARM, 3 STEP, MIX, 2 STEP, COMMIT COMMIT
5 34-37 ARM, 2 STEP, COMMIT COMMIT

FINAL accepts only if the last group committed (armed flag clear), the committed reads cover all 8 dwords (seen mask 0xff), and no committed mismatch exists. Because ROLLBACK restores the checkpointed {key, crc, ctr} and clears the masks, groups 1 and 3 cannot contribute to any of those conditions. The open question was whether ignoring them is actually safe, which we answer with data below.

Replaying the VM state without the ticket

For a committed STEP, the F check must pass on the accepting path: a mismatch inside a committed group flows through COMMIT into the global fail accumulator, and FINAL rejects on any committed mismatch. So on the path we want, every committed check holds exactly. The check is F == ~crc_raw(v, crc), which pins crc_raw = ~F, and two consequences follow that no longer involve the ticket byte w:

  • the next CRC state is ~crc_raw = F, exactly the instruction’s expected field
  • the key update ROL(key - 1 - crc_raw, 7) ^ E becomes ROL(key - 1 - ~F, 7) ^ E, computable from the instruction alone

MIX, ARM, COMMIT, and ROLLBACK also move state using only instruction constants, and ROLLBACK restores exact saved values. Starting from the fixed initial state (key = 0xb3197c65, crc = 0x142fd603, ctr = 0x17), the whole {key, crc, ctr} trajectory through a committed group can be replayed without knowing a single ticket byte. The ticket only enters a STEP through w, and the checks pin everything else. Windows of discarded groups get undone by their ROLLBACK anyway, so nothing outside a committed group perturbs the state entering one.

Inverting each committed step

At each committed STEP we now know key, crc, ctr, and the instruction fields, so recovering w takes two inversions.

First, find v with crc_raw(v, crc) = ~F. CRC-32 over a fixed 4-byte message is affine over GF(2), which is a way of saying the map equals one constant XOR a linear function of the input bits. The preimage (the input producing a given output) is therefore a 32x32 linear system with a unique solution. We build the linear part by probing the map with each single-bit input, then solve by Gaussian elimination:

# solve.py, raw CRC-32 over the 4 bytes of v, LSB first
def crc_raw(v, state):
    crc = state ^ 0xffffffff
    for i in range(4):
        b = (v >> (8 * i)) & 0xff
        crc = (crc >> 8) ^ TAB[(crc ^ b) & 0xff]
    return crc

def crc_preimage(state, target_raw):
    """Find unique v (32-bit) with crc_raw(v, state) == target_raw via GF(2) solve."""
    base = crc_raw(0, state)
    cols = [crc_raw(1 << j, state) ^ base for j in range(32)]  # linear part
    target = target_raw ^ base
    rows = []
    for o in range(32):          # one equation per output bit
        mask = 0
        for j in range(32):
            if (cols[j] >> o) & 1:
                mask |= 1 << j
        rows.append((mask, (target >> o) & 1))
    where = {}
    r = 0
    for c in range(32):          # Gaussian elimination
        piv = None
        for i in range(r, 32):
            if (rows[i][0] >> c) & 1:
                piv = i
                break
        if piv is None:
            continue
        rows[r], rows[piv] = rows[piv], rows[r]
        for i in range(32):
            if i != r and ((rows[i][0] >> c) & 1):
                rows[i] = (rows[i][0] ^ rows[r][0], rows[i][1] ^ rows[r][1])
        where[c] = r
        r += 1
    for i in range(r, 32):
        if rows[i][0] == 0 and rows[i][1]:
            return None          # inconsistent system
    v = 0
    for c, ri in where.items():
        if rows[ri][1]:
            v |= 1 << c
    assert crc_raw(v, state) == target_raw, "preimage failed"
    return v

Second, undo the mixing arithmetic. From v = ROL(w ^ key, shift) + (E ^ ctr), subtract the addend, rotate right by shift, then XOR the key:

# solve.py, undo one STEP given the replayed state (M32 = 0xffffffff)
v = crc_preimage(ebx, ins["F"] ^ M32)       # need crc_raw(v, ebx) == ~F
addend = ins["E"] ^ (r10 & M32)             # the 0x1422 xor eax, r10d detail
w = ror32((v - addend) & M32, shift) ^ edi  # the dword this step must read

Each committed STEP pins one dword, and placing them by idx assembles the ticket. The solver decodes the program, replays the state, derives every step, and reports the group structure and coverage:

$ python solve.py
acc = 0xff  r15 = 0
group pc1..pc11 committed=False idxs=[6, 0, 3, 7, 1, 5, 2, 4]
group pc13..pc18 committed=True idxs=[5, 1, 7]
group pc19..pc24 committed=False idxs=[3, 0, 6, 2]
group pc26..pc33 committed=True idxs=[0, 4, 2, 6, 3]
group pc34..pc37 committed=True idxs=[1, 4]
committed idx coverage: [0, 1, 2, 3, 4, 5, 6, 7]

The three committed groups read indices {5, 1, 7}, {0, 4, 2, 6, 3}, and {1, 4}, overlapping on 1 and 4, and the derived dwords agree at the overlaps (the script asserts equality). Coverage is all eight indices, so every dword of the ticket is pinned. The script then assembles the dwords, verifies all 32 bytes are printable, and re-simulates the entire VM with the recovered ticket, ending at printable: True and SIMULATION: ACCEPT.

The rolled-back groups are not binding

Going back to the open question, we derived the dword each discarded STEP would require, under the same replay, and compared it against the committed dword at the same index. Ten of the twelve discarded reads demand values that conflict with the committed solution. The only two that agree both read index 0, which is the literal NVR{ prefix that any candidate shares. So no ticket can satisfy the discarded groups together with the committed ones, and a solver that treats all five groups as binding hits contradictions. The ROLLBACK instructions are what resolve this: the canceled deliveries in the log never counted, and only the three committed groups describe the real ticket.

Checking the emulator against the real binary

An emulator agreeing with itself does not validate our reading of the assembly, so we tested the recovered ticket against the real binary, shown in the capture below. We also ran two adversarial probes against it, both answering Delivery rejected. with exit 1: the recovered ticket with its last byte changed by one, and the recovered ticket with the first four bytes replaced. Every dword feeds the CRC chain, so any mutation breaks some committed check, and both probes failing confirms the checks bind the whole ticket.

Flag capture

The relay never prints a flag; the ticket is the answer, and the real binary accepts it:

$ printf 'NVR{m41l_4ck3d_21edea1f5ec8caf1}\n' | ./dead_letter
DEAD LETTER / relay 17
One 32-byte ticket on stdin; finish input with EOF.
Delivery committed.
exit=0