AFTERIMAGE

The archives have sealed the final record of a repeatedly altered memory. Each playback leaves behind different traces, and the annotations can no longer be trusted. You possess the verification engine and the original archive. Find the ticket that validates that memory.

Recon

The handout unzips into two files. afterimage is a stripped x86-64 PIE ELF (a position-independent executable with no symbols) of 14,392 bytes, and archive.rpl is 6,684 bytes of raw data whose first bytes spell AIMG90. The engine replays the archive against a ticket typed on stdin and answers with one of two verdicts, and the ticket that earns Replay authenticated. is the flag. The binary’s own strings admit the intended workflow (Usage: %s archive.rpl < ticket.txt), so that is how we used it.

Running the engine before reading it

First contact, with no input at all:

$ ./afterimage archive.rpl </dev/null
AFTERIMAGE / replay authority 90
One ticket, one line. End input to replay.
Replay rejected.
EXIT=1

Before opening a disassembler we typed the alphabet at it (each run also prints the banner, omitted here):

$ printf 'ABCDEFGHIJKLMNOP\n' | ./afterimage archive.rpl
Replay rejected.

Empty input and a plain alphabet line earned the same banner and the same Replay rejected. with exit code 1. The engine never explains why a ticket fails and never prints anything per round, so the program is a single accept-or-reject oracle. With no feedback to iterate on, guessing was out and the check itself would have to be reconstructed and inverted.

strings showed how small the surface is:

$ strings -n 6 afterimage
Usage: %s archive.rpl < ticket.txt
AFTERIMAGE / replay authority 90
One ticket, one line. End input to replay.
Archive unavailable.
Archive seal broken.
Replay rejected.
Replay authenticated.

(The full output also lists loader and library names; these are the program’s own strings.)

The imports are only fread, getc, fopen, puts, fwrite and friends. No crypto library is linked, so every hash and cipher in this program is hand-rolled inside the binary and readable in the disassembly.

What the archive holds

$ xxd -l 32 archive.rpl
00000000: 4149 4d47 3930 0d0a 0100 0000 3000 0000  AIMG90......0...
00000010: a9b7 318c 8800 0000 e85b d84f f062 31af  ..1......[.O.b1.

Those bytes parse as an 8-byte magic AIMG90\r\n, a version dword of 1, a round count of 48 (0x30), a constant 0x8c31b7a9, a record size of 0x88 (136), and then 16 bytes e85b d84f f062 31af ad09 1433 efef ac67 at offset 0x18. After a 0x28-byte header and 48 records of 136 bytes (0x28 + 48*0x88 = 0x19a8), 116 bytes of the 6684-byte file remain, and they are text:

$ tail -c 116 archive.rpl | strings -n 4
ARCHIVE / discarded captions
NVR{the_replay_is_not_the_verdict}
NVR{frame_90_is_a_decoy}
NVR{trust_the_recordings}

Three flag-shaped strings filed under “discarded captions”. They are bait, matching the description’s warning that the annotations cannot be trusted, and as shown next they cannot even be submitted, because each is longer than the 16 bytes the ticket reader accepts.

The round loop in the disassembly

radare2 puts all logic in main (0x1160 to 0x1760) plus one helper at 0x1850. Before any round runs, main validates the archive. The file must be exactly 6684 bytes (cmp r13, 0x1a1c), the magic, version 1, 48 rounds and record size 0x88 must match, and an FNV-1a-64 hash (a simple multiply-and-xor hash) over the entire file must equal 0xfef40165c96d8f4a. That last check is the “seal” from the strings output. It means we cannot patch the archive or a round record to manufacture an easier target, since any edit breaks the seal.

The ticket reader then accepts one line of exactly 16 bytes (cmp rbp, 0x10), each byte in the printable range 0x21 to 0x7e (lea ecx,[rdx-0x21]; cmp cl,0x5d; jbe ok). At 0x13bb the 16 ticket bytes are loaded with movdqa and land in the state slot at rsp+0x50, so the ticket is the initial state of a small VM, read as four little-endian 32-bit words.

Each of the 48 rounds is driven by one 136-byte record, an 8-byte instruction header (selector bytes b0, b1, b2, b3 plus a 32-bit immediate) followed by two 64-byte encrypted microprograms. One round runs the following sequence.

  1. Both index arrays are reshuffled. idx4 starts at [0,1,2,3] and idx8 at [0..7], and the helper at 0x1850 applies a Fisher-Yates shuffle (the classic swap-from-the-tail permutation) driven by a xorshift32 generator (x ^= x<<13; x ^= x>>17; x ^= x<<5) seeded with round * 0x9e3779b9 ^ 0x8c31b7a9. The constant is the one stored at archive offset 0x10. idx4 remaps register numbers and idx8 remaps micro-opcodes, so the same opcode byte means a different operation in every round.
  2. The outer instruction (0x144f) picks a = idx4[b0] and b = idx4[b3 & 3], computes sum = state[a] + rotl(state[b], b2) and stores it into state[a].
  3. The branch bit (0x14bf) is ((imm ^ sum ^ rotl(sum, b2)) >> b1) & 1. It depends on the ticket through sum, and it selects which of the record’s two 64-byte microprograms gets played back. This is the “each playback leaves different traces” design, since two different tickets can execute different programs inside the same round.
  4. The chosen 64 bytes are decrypted (0x14ca) by XOR with a xorshift32 byte stream seeded round * 0xd1b54a35 ^ 0x8c31b7a9 ^ K(bit). The sbb ecx,ecx / and / sub sequence in the disassembly yields K(0) = 0xa1cda054 - 0x6b2fb645 and K(1) = -0x6b2fb645.
  5. The micro-VM (0x1554) reads the 64 decrypted bytes as eight 8-byte micro-instructions [b0, b1, b2, b3, imm2]. The executed opcode is idx8[b0], the registers are ra = idx4[b1] and rb = idx4[b2], and the 8-case switch at 0x20dc decodes to:
op code at semantics
0 0x16e3 state[ra] += imm2 ^ state[rb]
1 0x16d2 state[ra] ^= rotl(imm2 + state[rb], b3)
2 0x16c9 state[ra] = rotl(state[ra], b3)
3 0x16b5 state[ra] *= imm2 | 1
4 0x16a0 swap state[ra] and state[rb]
5 0x1676 state[ra] += rotl(state[rb], b3) ^ imm2, then state[rb] ^= rotl(state[ra], (b3 % 31) + 1)
6 0x15dd state[ra] = bswap(state[ra]) ^ imm2
7 0x16f1 state[ra] ^= imm2

All arithmetic wraps at 32 bits. The validator rejects any micro-instruction with out-of-range fields (b0 > 7, b1 > 3, b2 > 3, b3 outside 1..0x1f), with ra == rb, or with either register equal to the outer a. That last rule is what makes the whole challenge tractable, as explained below.

  1. The round closes by subtracting the outer add back out (0x160e, using the post-microprogram state[b]) and permuting the state lanes with pshufd xmm0, [rsp+0x50], 0x72 at 0x162a, an SSE instruction that shuffles the four 32-bit lanes and turns [s0, s1, s2, s3] into [s2, s0, s3, s1]. The permute sits before the round-count check at 0x1636, so it also runs after round 48.

After the last round the state at rsp+0x50 is compared (0x163c) against the 16 bytes at archive offset 0x18, the e85b d84f ... from the hexdump. Any difference prints Replay rejected.

Exploitation

Why the pipeline can be peeled backwards

Everything except the initial state is fixed by the archive. The shuffles depend only on the round number and the constant seed, and the records are data, so the 48 rounds form one fixed function of the ticket. We transcribed that function into a Python model, solve.py.

The function is invertible round by round. The microprogram can never touch state[a], because the validator rejects any micro-instruction referencing a, so the sum stored into state[a] survives the micro-VM unchanged. Every micro-op is also a bijection. Adds and xors undo directly, op 4 is its own inverse, op 2 is an inverse rotate, op 6 undoes as bswap(x ^ imm2), and op 3 multiplies by imm2 | 1, which is always odd, so it inverts by multiplying with the modular inverse mod 2^32, found with a few Newton iterations:

def inv_mul_const(c):
    x = c
    for _ in range(6):
        x = (x * (2 - c * x)) & 0xFFFFFFFF
    return x

The only unknown when running a round backwards is the branch bit, and it is a single bit. So we peel the pipeline from the target state at archive+0x18 back to the ticket. For each round from 48 down to 1, undo the pshufd lane permutation, undo the outer subtract, then for each candidate bit decrypt the matching microprogram, skip candidates whose micro-instructions would fail the validator (the binary would reject them), undo the eight micro-ops in reverse, undo the outer add, and recompute the branch bit from the resulting pre-round state. If the recomputed bit disagrees with the assumed bit, the candidate is discarded. The keystream decryption and the per-round core of the backward search, from solve.py:

def decode_micro(bit, rnd, base):
    """Decode the 8 micro-instructions for the given selected branch bit."""
    ks_seed = ((rnd * 0xD1B54A35) & M32) ^ INIT_SEED ^ ks_const(bit)
    ct = ARCH[base + 8 + bit * 64 : base + 8 + bit * 64 + 64]
    st = ks_seed
    ks = bytearray()
    for c in ct:
        st = xorshift(st)
        ks.append(c ^ (st & 0xFF))
    prog = []
    for k in range(8):
        b0, b1, b2, b3 = ks[8*k], ks[8*k+1], ks[8*k+2], ks[8*k+3]
        imm2 = struct.unpack_from("<I", ks, 8*k + 4)[0]
        prog.append((b0, b1, b2, b3, imm2))
    return prog
for bit in (0, 1):
    prog = decode_micro(bit, rnd, base)
    ok = True
    for (bb0, bb1, bb2, bb3, i2) in prog:
        if bb0 > 7 or bb1 > 3 or bb2 > 3 or not (1 <= bb3 <= 0x1F):
            ok = False; break
        ra, rb = idx4[bb1], idx4[bb2]
        if ra == rb or ra == a or rb == a:
            ok = False; break
    if not ok:
        continue
    mid = undo_micro(m2, prog, idx4, idx8, a)
    ssum = mid[a]
    v = (imm ^ ssum ^ rotl(ssum, r)) & M32
    if ((v >> s) & 1) != bit:
        continue
    # undo the outer ADD as well: pre[a] = sum - rotl(pre[b], r)
    pre = list(mid)
    pre[a] = (mid[a] - rotl(mid[b], r)) & M32
    nxt.append((pre, f"r{rnd}:bit={bit} " + path))

The bit consistency requirement acts as a one-bit checksum over 32 bits of state, so a wrong branch rarely survives a round. In the actual run the candidate count stayed at one per round the whole way down, never branching.

Validating the model against the real code

Before trusting the backward search we checked the Python model against the binary’s own code with the Unicorn emulation framework. uc_round.py loads the ELF into Unicorn, sets up the registers and stack slots the loop expects (archive buffer at rsp+0x180, state at rsp+0x50, record pointer in r14), executes the real round-loop code from 0x13fe to 0x1636 for a single round, and diffs the four result words against the Python model:

$ python3 uc_round.py
round 1: bit=0 a=2 b=3 real=['0xd8630fe3', '0xd288bb20', '0x3889b4c1', '0x166acaad']
         model=['0xd8630fe3', '0xd288bb20', '0x3889b4c1', '0x166acaad'] MATCH=True
round 2: bit=1 a=0 b=1 real=['0x713f9751', '0xbe132440', '0x83f7de26', '0xc85a4848']
         model=['0x713f9751', '0xbe132440', '0x83f7de26', '0xc85a4848'] MATCH=True
round 3: bit=1 a=2 b=0 real=['0xfb87d964', '0xb4bfbd65', '0x3a1e6440', '0x3b40ef1b']
         model=['0xfb87d964', '0xb4bfbd65', '0x3a1e6440', '0x3b40ef1b'] MATCH=True
round 4: bit=1 a=0 b=3 real=['0x5994eef9', '0x74ecf5ed', '0x22ffc51', '0x9a001c9d']
         model=['0x5994eef9', '0x74ecf5ed', '0x22ffc51', '0x9a001c9d'] MATCH=True

The model matched the real code word for word on every round, and the backward search converged. Feeding the recovered ticket plus a single flipped byte to the real engine showed the expected split, authenticated versus rejected.

Flag capture

$ python3 solve.py archive.rpl
[backward] round 48: 1 candidates
[backward] round 40: 1 candidates
[backward] round 32: 1 candidates
[backward] round 24: 1 candidates
[backward] round 16: 1 candidates
[backward] round 8: 1 candidates

1 solution candidate(s)
  ticket=b'NVR{CR7_la_GOAT}' printable=True path=r1:bit=0 r2:bit=0 r3:bit=0 r4:bit=0 r5:bit=0 r6:bit=1 r7:bit=1 r8:bit=0 r9:bit=1
  forward check -> [1339579368, 2939249392, 856951213, 1739386863] target=[1339579368, 2939249392, 856951213, 1739386863] match=True

solve.py also prints the ticket once more as its >>> FLAG candidate line, trimmed above so the flag appears exactly once, and writes the ticket to ticket.txt, so the engine itself gets the final word:

$ ./afterimage archive.rpl < ticket.txt
AFTERIMAGE / replay authority 90
One ticket, one line. End input to replay.
Replay authenticated.
EXIT=0