LAST RECEIPT

The last kiosk in the store is still waiting to close out its books. Inside the drawer lies a shredded roll of receipts, old tickets, and a few suspicious confirmations. Piece together the story on paper and find the ticket that makes the machine accept the final transaction.

Recon

Running the kiosk first

$ unzip -o last_receipt-player.zip
Archive:  last_receipt-player.zip
  inflating: last_receipt

$ file last_receipt
last_receipt: 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

strings shows the kiosk’s personality right away, including three flag-shaped strings:

$ strings last_receipt | grep -i "LAST RECEIPT\|kiosk\|ticket\|settle\|NVR"
LAST RECEIPT / kiosk 05
Ticket rejected.
Receipt settled.
NVR{strings_are_not_receipts}
NVR{receipt_chain_valid}
NVR{refund_approved}
One ticket, one line. End input to settle.
NVRSP01

The first run is the simplest one, no input at all, and the three NVR{...} strings from strings are the only ticket candidates we hold, so we typed each of them in directly, one per run. Every run printed the same three lines and exited with status 1:

$ ./last_receipt < /dev/null; echo EXIT=$?
LAST RECEIPT / kiosk 05
One ticket, one line. End input to settle.
Ticket rejected.
EXIT=1

The binary’s own flag-shaped strings are rejected exactly like the empty input, so the strings are decoys and the answer is not in them. Nothing we can observe from outside says what an accepted line looks like, so the input rules have to come out of the code.

What the reader accepts

main sits at 0x10a0. It prints the banner and the prompt, then loads three pointers from .data (0x4010, 0x4018, 0x4020) and only checks each for being non-NULL. A dump of .data shows those pointers aim at the three decoy strings at 0x203e, 0x205c and 0x2075, and nothing in the binary ever compares input against them. After the pointer checks, main calls the reader at 0x1380 and then the validator at 0x1250:

10fb: call 0x1380          ; read one line into the stack buffer
1100: test eax, eax
1102: je   0x1134          ; failure -> "Ticket rejected."
1107: call 0x1250          ; validate the line
110c: test eax, eax
110e: je   0x1134          ; failure -> "Ticket rejected."
1110: lea  rdi, [rip+0xf16] ; "Receipt settled."

The reader is a getc loop into a 256-byte stack buffer that stops at \n or EOF and trims one trailing \r. It accepts the line only if it is exactly 24 characters (cmp rbp, 0x18 at 0x13f2), and it checks each byte with a range idiom:

1412: lea  ecx, [rdx - 0x21] ; c = byte - 0x21
1415: cmp  cl, 0x5d          ; unsigned compare against 0x5d
1418: jbe  0x1400            ; accept and copy the byte

Subtracting 0x21 and comparing the low byte unsigned against 0x5d accepts exactly the bytes 0x21 through 0x7e, every printable character except space, and the reader fails on the first byte outside that range.

Now the rules hand us exact numbers to aim at, so we went back to the kiosk and confirmed them from outside. Three probes, 23 As, 25 As, and 24 As with one byte replaced by a space, each printed the identical banner and Ticket rejected. with exit 1, which is the length rule and the range rule from the disassembly rejecting each probe. The informative probe is the one that violates no input rule at all:

$ printf "%s\n" "AAAAAAAAAAAAAAAAAAAAAAAA" | ./last_receipt; echo EXIT=$?
LAST RECEIPT / kiosk 05
One ticket, one line. End input to settle.
Ticket rejected.
EXIT=1

Twenty-four printable As survive the reader yet still get rejected, so the content check lives in the validator, past the input rules. The decoy NVR{receipt_chain_valid} is exactly 24 printable characters, so it survived the reader too and died in the validator; all three strings are decoys.

Any inversion step that produces a byte outside 0x21..0x7e is provably wrong, because the result could never be typed into the kiosk. That is a cheap correctness check on any inversion of the validator.

The receipt chain in .rodata

The validator at 0x1250 anchors itself on the string NVRSP01 at 0x20bb (lea rbp, [rip+0xe31]) and sets up its state before the loop: K = 0xa7 (mov r10d, 0xffffffa7), h = 0x6d (mov r14d, 0x6d), a chain cursor of 14 (mov eax, 0xe) and a visited-mask with bit 14 already set (mov edx, 0x4000). Its data is a table of 24 four-byte nodes at 0x20cb, which is exactly rbp + 0x10:

$ xxd -s 0x20bb -l 0x80 last_receipt
000020bb: 4e56 5253 5030 3100 180e 6d05 0300 0000  NVRSP01...m.....
000020cb: 04eb 4ffa ffaa 646b 01e5 64da 0d92 5491  ..O...dk..d...T.
000020db: 09af 9965 11e4 b21d 037f ba9c 0077 d9f4  ...e...........w..
000020eb: 0aab 9e65 1252 8e94 07f5 b61e 15ed 4ae8  ...e.R...J..
000020fb: 16d4 1189 109f 1cc9 08e2 56e6 0ccf 1b82  ..........V.....
0000210b: 02f2 b41e 06f4 56fe 13e3 41eb 0fdc 78f1  ......V...A...x.
0000211b: 17f5 3880 0519 6325 14a3 a944 0bf8 b71e  ..8...c%...D.....
0000212b: 0001 1b03 3b40 0000 0007 0000 00f4 eeff  ....;@..........

Treat each node as (c0, c1, c2, c3). The loop head shows how the nodes link into a chain:

1298: cmp    eax, 0x17             ; the incoming link c0 must be <= 23
12a7: add    edi, 0x5              ; A += 5 (ticket position cursor)
12aa: add    r10d, 0xd             ; K += 0x0d
12ae: shl    edx, cl               ; bit = 1 << c0
12b0: test   edx, r9d              ; each node may be visited once only
12b9: lea    eax, [4*rax + 0x10]   ; node address = rbp + 0x10 + 4*c0
12c6: movzx  r8d, byte ptr [rax]        ; c0
12ca: movzx  edx, byte ptr [rax + 0x2]  ; c2
12ce: movzx  ecx, byte ptr [rax + 0x1]  ; c1
12d2: mov    r15d, r8d             ; r15d = c0
12d5: xor    r15d, edx             ; ^ c2
12d8: xor    r15d, ecx             ; ^ c1
12db: xor    r15d, 0x5a            ; checksum constant baked into the code
12df: cmp    r15b, byte ptr [rax + 0x3] ; node checksum c3 must match

So c0 of each node is the index of the next node, c3 is a checksum of the other three bytes, the visited-bitmask enforces a single visit, and at loop exit cmp eax, 0xff at 0x1363 demands that the last visited node carries the terminator 0xff instead of another index. Following c0 from node 14 therefore has to walk all 24 nodes and stop on the 0xff one. The pre-set bit 14 covers the start node, and indeed 14 never appears again as a link value.

The round body ties one ticket byte into this (the compiler computes A % 24 and i % 7 through the usual multiply-shift divisions, with the constants 0xaaaaaaab and 0x24924925):

12ff: movzx  eax, byte ptr [rbx + r15] ; t = ticket[A % 24]
1304: xor    eax, r14d                 ; t ^ h
130c: add    r15d, ecx                 ; (t ^ h) + c1
1332: lea    ecx, [rax + 0x1]          ; rotation amount = (i % 7) + 1
1335: mov    eax, edx                  ; eax = c2
1337: xor    edx, r14d                 ; edx = c2 ^ h
133a: rol    r15b, cl                  ; x = rol8(((t^h)+c1) & 0xff, (i%7)+1)
133d: xor    eax, r10d                 ; y = (c2 ^ K) & 0xff
1340: rol    dl                        ; rotated c2^h, consumed two lines below
1342: xor    r15d, eax                 ; this round's mismatch x ^ y
1345: lea    r14d, [rdx + rsi + 0x3d]  ; h' = rol8((c2^h)&0xff, 1) + i + 0x3d
1351: or     r11d, r15d                ; accumulate mismatches over 24 rounds

rol8 is an 8-bit rotate left. A round passes when rol8(((t ^ h) + c1) & 0xff, (i % 7) + 1) equals (c2 ^ K) & 0xff, and the validator accepts only if the low byte of the accumulated mismatch is zero after all 24 rounds.

Exploitation

Why the ticket can be replayed without knowing it

The ticket enters the loop at exactly one instruction, the t load. Everything else a round consumes comes from the node table and the round counter: the chain index from the previous node’s c0, K from a fixed +0x0d per round, h from a fixed recurrence, A from a fixed +5 per round. The whole state sequence can therefore be replayed with no ticket at all, and each replayed round reveals the constants its comparison uses. We checked the replay against an instruction-level Unicorn emulation of the validator, hooking the end of each round at 0x1351 with a dummy ticket A through X:

$ python emu.py
ret = 0
round  0: A=  3 K=0xa7 h=0xb3 r15b=0xe7 eax(c0?)=0x08
round  1: A=  8 K=0xb4 h=0x98 r15b=0xbc eax(c0?)=0x0a
round  2: A= 13 K=0xc1 h=0x9b r15b=0x29 eax(c0?)=0x07
...
round 23: A=118 K=0xd2 h=0x80 r15b=0xc8 eax(c0?)=0xff

The cursor sequence starts 3, 8, 13, exactly A = 3 + 5*i, K climbs by 0x0d each round, the last column walks the chain link by link, and round 23 reports the 0xff terminator. Round 0 enters the loop directly at the node fetch (the jmp at 0x1293 skips the head), so the head’s +5 and +0xd run between rounds rather than before the first. The r15b column holds each round’s mismatch for the dummy ticket, all nonzero, so the emulation rejects it as expected.

Since 5 and 24 share no factor, the positions (3 + 5*i) mod 24 cover all 24 positions exactly once. Each round pins exactly one ticket byte, and 24 rounds pin all of them, so the accepted ticket is unique and there is nothing to search. Solving one round for t is one line of algebra, t = ((ror8((c2 ^ K) & 0xff, (i % 7) + 1) - c1) & 0xff) ^ h, stored at position (3 + 5*i) mod 24.

Tracing the rotation

One instruction in the round body decides the inversion: the rol dl at 0x1340 works on an edx-derived value, so its result could feed the comparison or only the hash update. Round 0 is small enough to trace instruction by instruction, so we emulated just that round with a Unicorn hook before every instruction of the body (emu_fine.py). Round 0 visits node 14, whose c2 is 0x56, with K = 0xa7, so a raw c2 ^ K is 0xf1 while a rotated one would be 0xe3. Each trace line shows the registers just before the instruction named in its label runs (three of the nine lines shown):

$ python emu_fine.py
after xor eax,r10d (eax=c2^K)          eax=0x00000056 ecx=0x00000001 edx=0x0000003b r15=0x00000116 r8=0x00000008
after rol dl,1                         eax=0xfffffff1 ecx=0x00000001 edx=0x0000003b r15=0x00000116 r8=0x00000008
after xor r15d,eax (r15b=x^y)          eax=0xfffffff1 ecx=0x00000001 edx=0x00000076 r15=0x00000116 r8=0x00000008

By the time rol dl,1 is about to run, eax already holds 0xfffffff1, the raw c2 ^ K with only the low byte meaningful. After it runs, only edx moved, 0x3b to 0x76, and 0x76 is exactly what the hash update lea r14d, [rdx + rsi + 0x3d] consumes, since 0x76 + 0 + 0x3d = 0xb3 matches the h the per-round trace reported after round 0. The rotation feeds the hash update only, never the comparison value.

Inverting all 24 rounds

The solver reads the node table straight out of the binary at offset 0x20cb (in this ELF the .rodata file offset equals the virtual address the validator computes), checks every node checksum, walks the chain from index 14, and undoes each round. Diagnostics go to stderr so that stdout is only the ticket:

#!/usr/bin/env python3
"""Invert the 24-round receipt chain of last_receipt (validator at 0x1250)."""
import sys

NODES = 0x20cb

def log(msg):
    print(msg, file=sys.stderr)

def rol8(v, n):
    n &= 7
    return ((v << n) | (v >> (8 - n))) & 0xff if n else v & 0xff

def ror8(v, n):
    return rol8(v, (8 - n) & 7)

code = open("last_receipt", "rb").read()
T = [tuple(code[NODES + 4 * k + j] for j in range(4)) for k in range(24)]

for c0, c1, c2, c3 in T:
    assert (c0 ^ c1 ^ c2 ^ 0x5a) & 0xff == c3, "node integrity failed"
log("[+] all 24 node integrity checks pass (c0^c1^c2^0x5a == c3)")

chain, idx = [], 14           # mov eax, 0xe: the chain starts at node index 14
for i in range(24):
    chain.append(idx)
    if i == 23:
        assert T[idx][0] == 0xff, "chain terminator missing"
        break
    idx = T[idx][0]           # the current node's c0 is the next node's index
log("[+] chain (24 nodes): %s" % chain)
log("[+] terminal node T[%d] c0 == 0xff OK" % chain[-1])

ticket = [None] * 24
A, K, h = 3, 0xA7, 0x6D
for i, idx in enumerate(chain):
    c0, c1, c2, c3 = T[idx]
    y = (c2 ^ K) & 0xff                      # comparison value, no rotation
    pos = A % 24
    assert ticket[pos] is None               # every round pins a fresh position
    ticket[pos] = ((ror8(y, (i % 7) + 1) - c1) & 0xff) ^ (h & 0xff)
    K = (K + 0x0D) & 0xFFFFFFFF
    h = (rol8((c2 ^ h) & 0xFF, 1) + i + 0x3D) & 0xFF
    A += 5

print(bytes(ticket).decode())
$ python solve.py 2>&1 >/dev/null
[+] all 24 node integrity checks pass (c0^c1^c2^0x5a == c3)
[+] chain (24 nodes): [14, 8, 10, 7, 0, 4, 9, 18, 19, 15, 12, 22, 20, 23, 11, 21, 5, 17, 6, 3, 13, 16, 2, 1]
[+] terminal node T[1] c0 == 0xff OK

The 24 positions fill with no conflicts, the chain visits all 24 nodes exactly once and ends on the terminator, and every derived byte lands inside the reader’s printable range. The stdout result is 24 readable characters wrapped in the NVR{...} format; we keep the value for the end. Feeding those bytes to the Unicorn emulation of the validator’s own code returned 1, the accept value, and they satisfy the reader’s length and range rules.

A negative control on the real binary, with the ticket itself kept out of the transcript:

$ T="$(python solve.py 2>/dev/null)"; printf '%s\n' "${T%?}~" | ./last_receipt; echo "EXIT=$?"
LAST RECEIPT / kiosk 05
One ticket, one line. End input to settle.
Ticket rejected.
EXIT=1

${T%?}~ swaps the final character of the ticket for a tilde, keeping the line at 24 printable characters. One changed character flips the verdict, so the validator really checks the content round by round and the ticket is not some fuzzy near-match.

Flag capture

$ python solve.py 2>/dev/null
NVR{Nay_la_flag_that_^^}

$ printf '%s\n' "$(python solve.py 2>/dev/null)" | ./last_receipt
LAST RECEIPT / kiosk 05
One ticket, one line. End input to settle.
Receipt settled.