challenge description
do something with the bottle of the genie
Handout is a 32 KiB Game Boy ROM seal.gb (title SEAL_NINTH) plus PORT.md. The service prints a 16-bit session seed, takes one JSON movie, replays it on a pristine cart, and returns the flag only if the ROM hits WIN.
writeup
PORT.md is the real attack surface. The movie format is tiny on purpose:
text
joypad[n] = button mask for frame n
codes = up to 12 triples [frame, address, value]
writes = even addrs only, C100-C1FE and C300-C3FE
timing = each write lands before that frame runs
Joypad bits: right=1 left=2 up=4 down=8 A=16 B=32 select=64 start=128.
I opened the ROM in pyboy, dumped WRAM, and stepped buttons to see what flipped. Important fields:
text
C100:C101 gold
C102:C103 gold XOR session key
C104:C105 gold MAC / tag
C200:C201 floor-9 echo state
C202:C203 hash of echo state
C300:C301 echo selector
C406 floor
C40A pending echo flag
C410:C411 tile x,y
Normal floors: chest + monster, walk onto the gate, press start, spend 50 gold. Floor 3 wants 5000. Floor 9 is the vault.
authenticated gold 
A raw poke to gold does nothing useful. Helper at 0x04a5 builds a session key from the seed:
Python
def rol16(x, n):
return ((x << n) | (x >> (16 - n))) & 0xffff
def key_from_seed(seed):
return ((rol16(seed ^ 0xA5C3, 7) + 0x3D29) & 0xffff) ^ 0x6B71
def gold_tag(gold, key):
x = (0x6D2B + rol16(gold ^ key, 3)) & 0xffff
return rol16(x ^ rol16(key, 7), 5)
Whenever gold changes, the ROM writes three words together:
text
C100 = gold
C102 = gold ^ key
C104 = gold_tag(gold, key)
The frame-loop validator checks that triple. If it is broken, backup RAM at C400:C405 is restored. So C100 = 5000 alone silently reverts.
Result: the cheat window already covers C100:C105. Write all three words on one frame, before the CPU runs, and the ROM accepts 5000 gold as internally consistent. That is the bottle.
route
Boot wait 120 null frames. Tile spawn is about (2, 3).
Normal floor path I locked in with pyboy:
text
right 3, down 3 chest
right 8, down 3 monster
right 4, down 6 gate
start
Do that twice → floor 3. After the gold cheat on frame 236:
text
right 15, down 12, start
Straight from spawn to the floor-3 gate, no scavenging. Lands on floor 9.
floor-9 echo
Pressing A arms a pending echo (C40A = 1). Next frame, if C300:C301 is 0, 1, or 2, the ROM runs one of three transforms on C200:C201, then stores a hash at C202:C203. Final readiness compares that hash to 0xB14A in the ROM.
After final-floor setup the echo state resets to 0x1D0F. Three selectors, one target, tiny state space. Offline brute:
text
ops = [2, 0, 2, 1, 2, 2, 0, 2, 1]
Each echo needs two frames: A to arm, then a cheat write into C300 on the next frame. Nine selectors = nine remaining writes. Twelve total = three gold + nine echo. Exact fit.
Local win path from win.py:
Python
for op in [2, 0, 2, 1, 2, 2, 0, 2, 1]:
tick(16) # A
p.memory[0xC300] = op
p.memory[0xC301] = 0
tick(0)
Screen paints:
text
WIN
SUBMIT MOVIE
TO SERVICE
FOR FLAG
remote movie
so i made the script to read seed=, recompute the three gold words
Python
#!/usr/bin/env python3
import json, re, socket, ssl
HOST = "genie-4d24433951d9.chals.z0d1ak.org"
PORT = 1337
def rol16(x, n):
return ((x << n) | (x >> (16 - n))) & 0xffff
def key_from_seed(seed):
return ((rol16(seed ^ 0xA5C3, 7) + 0x3D29) & 0xffff) ^ 0x6B71
def gold_tag(gold, key):
x = (0x6D2B + rol16(gold ^ key, 3)) & 0xffff
return rol16(x ^ rol16(key, 7), 5)
joy = []
def tick(mask=0): joy.append(mask)
def step(mask):
tick(mask)
tick(0)
def go(mask, count):
for _ in range(count):
step(mask)
def normal_floor():
go(1, 3); go(8, 3); go(1, 8); go(8, 3); go(1, 4); go(8, 6)
step(128); tick(0); tick(0)
for _ in range(120):
tick(0)
normal_floor()
normal_floor()
tick(0) # frame 236 gold inject lives here
go(1, 15); go(8, 12); step(128)
for _ in range(10):
tick(0)
ops = [2, 0, 2, 1, 2, 2, 0, 2, 1]
for _ in ops:
tick(16); tick(0)
for _ in range(60):
tick(0)
ctx = ssl.create_default_context()
with socket.create_connection((HOST, PORT), timeout=10) as raw:
with ctx.wrap_socket(raw, server_hostname=HOST) as s:
data = b""
while b"movie-json> " not in data:
data += s.recv(4096)
print(data.decode(), end="")
seed = int(re.search(rb"seed=(d+)", data).group(1))
key = key_from_seed(seed)
gold = 5000
codes = [
[236, 0xC100, gold],
[236, 0xC102, gold ^ key],
[236, 0xC104, gold_tag(gold, key)],
]
for i, op in enumerate(ops):
codes.append([304 + 2 * i, 0xC300, op])
movie = {"version": 1, "seed": seed, "joypad": joy, "codes": codes}
s.sendall((json.dumps(movie, separators=(",", ":")) + "
").encode())
reply = b""
while True:
chunk = s.recv(4096)
if not chunk:
break
reply += chunk
print(reply.decode(), end="")
Seed changes every connect. Key and tag recompute. Addrs and ops stay fixed.
Result:
text
WIN
zdk{7Hree_WOrD5_NiNE_eCHo3S_ON3_oPeN_Seal}