emulaSON JPEG-appended ELF runs a bytecode VM

rev
flag L3m0n{0d2bbd654d34219219e49382f2171cea}

One handout, yolo.png. It opens fine as a JPEG - and then keeps going past the end-of-image marker into a full x86-64 PIE ELF binary. file on the carved tail confirms it; the ELF header sits at the first \x7fELF right after \xff\xd9.

The handout yolo.png, a JPEG portrait with an x86-64 ELF appended after the end-of-image marker

Hint

JPEG viewers stop reading at EOI. Most other tools, including the ones you'd use to inspect a file, don't.

The binary is stripped and takes the flag on argv[1] or stdin: checks the L3m0n{ prefix, checks strlen == 0x27 (so the inner part is 32 bytes), checks the trailing }, then runs a 496-tick bytecode VM over those 32 bytes, feeds the VM's 32 output bytes into a 4-round Feistel network, and compares the result against 32 bytes baked into the binary.

Decoding the VM's instruction stream

Each instruction is 4 bytes, and the opcode isn't stored directly - it's derived at runtime:

output
op = (insn ^ tick ^ state[insn & 7]) & 0x1f

where state is an 8-byte array that starts as SON\x11\x22\x33\x44\x55 and mutates every tick.

Pitfall

I spent a while convinced state[0] changes from 'S' to 'R' partway through, because of a mov al, dl; add al, 0x52 sequence where dl comes from a setne. If the flag matched and strncmp returned 0, setne would give 0 and al would become 0x52, flipping the state to read RON. That reasoning is wrong: the setne right before it doesn't actually look at the strncmp result at all - it looks at the flags left by an unrelated add r12, 0x1 (r12 is a ptrace return value, always 0), which leaves ZF clear, so setne gives 1 and al becomes 0x53. The state stays SON the whole time. Decoding with the wrong RON reading produces a stream full of invalid high-numbered opcodes and only 16 loads where there should be 32 - a strong tell that something upstream was misread, well before the Feistel stage even comes into play.

Also: an absolute breakpoint on the state-write address never hits, because this is a PIE binary and the disassembly offsets are file offsets, not runtime addresses. Stepping from starti and adding the real load base fixes it.

With the correct SON state, the stream decodes cleanly into 32 LOAD...STORE blocks plus one halt at tick 496: each block is one byte in (LOAD copies input[i] into register 0), a short sequence of XOR/ROL/ADD/ROR/XOR operations across eight carried registers, and one byte out (STORE writes register 0 to the output buffer).

Inverting the Feistel

The 32 VM outputs feed a 4-round Feistel keyed by themselves:

python
1def feistel_round(k, r):
2    return bytes(rol8(k[i], ((r + i) % 7) + 1) ^ b"SON"[(r + i) % 3]
3                 for i in range(16))

Insight

My first reading treated the XOR partner at each round as a fixed constant - the first 16 outputs. Wrong: the recurrence is CBC-like, each round's key feeding forward into the next (K1 = F(K0,0) ^ P, K2 = F(K1,1) ^ K0, K3 = F(K2,2) ^ K1, K4 = F(K3,3) ^ K2). Once that clicked, the whole chain runs backwards cleanly: the binary's comparison target is K3 || K4, both known, so K2 = F(K3,3) ^ K4, K1 = F(K2,2) ^ K3, K0 = F(K1,1) ^ K2, and P = F(K0,0) ^ K1 recover all 32 VM outputs with zero guessing. A forward round-trip check (feistel_round applied forward from P reproduces K3 || K4 exactly) confirms it before trusting the result.

Recovering the input byte-by-byte

With all 32 VM outputs now known, each of the 32 blocks has exactly one unknown left: its single input byte. Walking the blocks in order, brute-forcing all 256 candidates per block, and keeping the register state carried forward from the previous block (so each block is cheap) recovers the full 32-byte inner flag with no ambiguity:

python
1for i, blk in enumerate(blocks):
2    for b in range(256):
3        if run_block(blk, regs, b)[0] == outputs[i]:
4            inner.append(b)
5            regs = run_block(blk, regs, b)
6            break
output
L3m0n{0d2bbd654d34219219e49382f2171cea}
Show the full solve script
python
1#!/usr/bin/env python3
2"""emulaSON - self-contained solve.
3
4yolo.png is a JPEG with an x86-64 ELF appended. The ELF runs a 496-tick
5bytecode VM over the 32 bytes inside L3m0n{...}, then a 4-round Feistel
6over the VM's 32 outputs, and compares against a blob in .rodata.
7"""
8import sys
9SON = b"SON"
10
11def rol8(b, n):
12    n &= 7
13    return ((b << n) | (b >> (8 - n))) & 0xFF if n else b & 0xFF
14
15def ror8(b, n):
16    n &= 7
17    return ((b >> n) | (b << (8 - n))) & 0xFF if n else b & 0xFF
18
19def carve_elf(png_path):
20    data = open(png_path, "rb").read()
21    eoi = data.rfind(b"\xff\xd9")
22    off = data.find(b"\x7fELF", eoi)
23    return data[off:]
24
25def decode_stream(elf):
26    program = elf[0x20C0:]
27    state = bytearray(b"SON\x11\x22\x33\x44\x55")
28    stream = []
29    tick = 0
30    while tick < 0x1F0:
31        insn = program[4 * tick]
32        r13 = program[4 * tick + 1]
33        r9 = program[4 * tick + 2]
34        cl = program[4 * tick + 3]
35        op = (insn ^ tick ^ state[insn & 7]) & 0x1F
36        stream.append((op, r13, r9, cl))
37        if op == 15:
38            break
39        a = insn & 7
40        state[(a + 1) & 7] ^= (r13 + op) & 0xFF
41        state[(a + 3) & 7] = (state[(a + 3) & 7] + r9) & 0xFF
42        tick += 1
43    return stream
44
45def split_blocks(stream):
46    blocks, cur = [], []
47    for ev in stream:
48        cur.append(ev)
49        if ev[0] == 1:
50            blocks.append(cur)
51            cur = []
52    return blocks
53
54def run_block(block, regs, byte):
55    regs = list(regs)
56    for op, r13, r9, cl in block:
57        d, s = r13 & 7, r9 & 7
58        if op == 0: regs[d] = byte
59        elif op == 2: regs[d] ^= regs[s]
60        elif op == 3: regs[d] = (regs[d] + regs[s]) & 0xFF
61        elif op == 4: regs[d] = (regs[d] - regs[s]) & 0xFF
62        elif op == 5: regs[d] = rol8(regs[d], cl)
63        elif op == 6: regs[d] = ror8(regs[d], cl)
64        elif op == 7: regs[d], regs[s] = regs[s], regs[d]
65        elif op == 8: regs[d] ^= cl
66        elif op == 9: regs[d] = (regs[d] + cl) & 0xFF
67        elif op == 10: regs[d] = regs[s]
68        elif op == 11: regs[d] = 1 if regs[s] == regs[d] else 0
69        elif op == 12: regs[d] = (regs[d] + 1) & 0xFF
70        elif op == 13: regs[d] = (regs[d] - 1) & 0xFF
71        elif op == 14: regs[d] &= cl
72    return regs
73
74def feistel_round(k, r):
75    return bytes(rol8(k[i], ((r + i) % 7) + 1) ^ SON[(r + i) % 3] for i in range(16))
76
77path = sys.argv[1] if len(sys.argv) > 1 else "yolo.png"
78elf = carve_elf(path)
79blocks = split_blocks(decode_stream(elf))
80expected = elf[0x20A0:0x20C0]
81k3, k4 = expected[:16], expected[16:32]
82
83k2 = bytes(a ^ b for a, b in zip(feistel_round(k3, 3), k4))
84k1 = bytes(a ^ b for a, b in zip(feistel_round(k2, 2), k3))
85k0 = bytes(a ^ b for a, b in zip(feistel_round(k1, 1), k2))
86prev = bytes(a ^ b for a, b in zip(feistel_round(k0, 0), k1))
87outputs = prev + k0
88
89regs = [0] * 8
90inner = bytearray()
91for i, blk in enumerate(blocks):
92    for b in range(256):
93        if run_block(blk, regs, b)[0] == outputs[i]:
94            inner.append(b)
95            regs = run_block(blk, regs, b)
96            break
97
98print("L3m0n{" + inner.decode() + "}")