CSCTF 2026 · Misc · 120 pts
golem.
A register machine with ten documented quirks, and a service that checks all of them byte for byte.
| Event | CSCTF 2026, Operation Dawn (online jeopardy) |
|---|---|
| Category | Misc |
| Points | 120 |
| Status | Solved during the event |
| Target | Audit service, 100 rounds (assigned TCP instance) |
TL;DR. The audit service runs 100 random GASM programs and compares the output string exactly. The only way through is an interpreter that implements the specification literally: 16-bit wraparound, div 0 = 65535, mod 0 = dividend, shift counts masked with & 15, in past the tape yielding 65535, rep 0 running zero times, and ifz/ifn testing once at block entry. Validated against 24 examples, 7,000 fuzz programs and 100 mock rounds, then run live.
Challenge
The golem is a small register machine used by Ash Army signals engineers. The handout contains SPEC.md and 24 worked examples. The audit service sends a program plus an input tape per round, and expects the exact output the machine would print. One wrong answer closes the session.
First look
The machine has 16 registers r0..r15, all unsigned 16-bit and initialised to zero. Programs are line-oriented: set, add, sub, mul, div, mod, and, or, xor, not, shl, shr, in, out, rep, ifz, ifn, end. There are no jumps, so every well-formed program terminates.
The protocol is fresh per connection:
=== round k/100 ===
PROGRAM
<lines>
.
INPUT <space-separated bytes>
OUTPUT>
The client answers with one line and reads OK or WRONG. A wrong answer closes the socket; a new connection starts a new set of rounds.
Dead ends
- Guessing "reasonable" behaviour for the edge cases. The spec is explicit where it matters, and the examples pin the rest; an intuitive interpreter gets
mod 0andrep 0wrong. - Treating
ifz/ifnas a loop condition. They test the register once, when the block is entered. Mutating the register inside the body does not re-evaluate the branch. - Sending output without the trailing newline, or sending an extra blank line for a program with no
out. The comparison is exact, including the empty line.
Solution
1. Implement the quirks, not the intent
| Operation | Behaviour |
|---|---|
| Any register write | value & 0xFFFF; immediates are signed and wrapped |
div | unsigned floor division; divisor 0 returns 65535 |
mod | divisor 0 returns the dividend unchanged |
shl, shr | shift count is b & 15; shr is logical |
not | ~v & 0xFFFF |
in | reads the tape left to right; past the end yields 65535 |
rep N | runs the body exactly N times; N=0 runs it zero times |
ifz, ifn | test once at block entry |
no out | prints an empty line |
Two details prevent most bugs. Read both source operands before writing the destination, so add r0 r0 r0 doubles. And parse into a block tree rather than a flat instruction list, because rep/ifz/ifn need matching end tokens and nested bodies.
def write(i, v):
regs[i] = v & 0xFFFF
def div(a, b):
return 0xFFFF if b == 0 else a // b
def mod(a, b):
return a if b == 0 else a % b
def shift(op, a, b):
n = b & 15
return ((a << n) if op == "shl" else (a >> n)) & 0xFFFF
2. Validate before touching the service
python3 golem.py selftest examples.txt
# 24/24 examples reproduced byte-exactly
A second interpreter with a different design (flat bytecode, iterative execution, explicit block frames) was compared on 7,000 randomly generated programs. The generator covered nested blocks, rep 0, negative and overflowing immediates, division by zero, shifts above 15, and short or oversized tapes. Both implementations agreed on every program, and both reproduced all 24 examples.
3. Answer the audit
python3 golem.py <HOST> <PORT>
[*] round 1/100 OK (1 value)
...
[*] round 100/100 OK (3 values)
CSCTF{...}
The client buffers reads, takes the round count from the header, tolerates CRLF and empty tapes, and sends an empty line when the expected output is empty. A wrong answer deliberately exits without printing a flag, so a failed run cannot be mistaken for a solve.
Flag
CSCTF{HKMcdRnEHIUv8LFsCP6s7ZcL}What I took from it
- Spec compliance is a different skill from programming. The work was not writing an interpreter; it was resisting the urge to "fix" the documented edge cases.
- Differential testing finds the gaps that examples miss. Two independent implementations plus a generator covered combinations the 24 examples never touch.
- Exact-output protocols punish whitespace. The empty-output case is a real test, not a formality.