Challenge Description
Category: Reverse Engineering
Difficulty: 🔴 Hard
A decommissioned operator training rig runs its HMI from a cartridge. The title screen admits that an undocumented service mode exists and requires a key sequence, but the referenced manual is gone. Recover the sequence, enter it, and submit the maintenance token.
We get one file, waterworks.gb, and no documentation for the input sequence it expects.
The cartridge is a small Game Boy program dressed up as a Clearwater treatment-plant HMI. The title screen is unusually honest about the backdoor:
OPERATOR MANUAL:
DOC HS-441 SEC 7.3
SERVICE MODE NEEDS
A KEY SEQUENCE
The manual is missing, obviously. The sequence is not stored as text, and neither is the token we eventually need to submit.
So there are really two reversing problems here:
- understand how eight Game Boy inputs are normalized and matched;
- follow the successful state transition far enough to recover the token rendered on screen.
Finding eight suspicious bytes is not enough. I wanted to explain why they are buttons, why they must be separate presses, and what the ROM does after the final one.
TL;DR
waterworks.gbis a 32 KiB, ROM-only Game Boy image titledWATERWORKS- ROM-only means CPU addresses
0x0000–0x7FFFare also file offsets - The joypad routine reads the two active-low halves of
JOYP, combines them, and derives newly pressed edges - The service detector rejects chords with
pressed & (pressed - 1) - It compares eight one-hot masks against a table at
0x07BC - The raw bytes are
08 08 01 10 02 04 20 40, which map to:
Down, Down, Right, A, Left, Up, B, Select
- Eight matches set the application state at
C002to4 - The service renderer decrypts 32 bytes from
0x0A6Ewith an additive rolling XOR key - The final token is:
flag{gb_service_mode_override01}
Initial Triage
I started with the cartridge header rather than loading the ROM into an emulator and guessing buttons.
from pathlib import Path
import hashlib
rom = Path("waterworks.gb").read_bytes()
print(f"size: {len(rom):#x}")
print(f"sha256: {hashlib.sha256(rom).hexdigest()}")
print(f"title: {rom[0x134:0x144].rstrip(b'\x00').decode()}")
print(f"cart: {rom[0x147]:#x}")
print(f"rom: {rom[0x148]:#x}")
print(f"ram: {rom[0x149]:#x}")
size: 0x8000
sha256: 41f2bcb3c80267cc7de348ce06c7dfb4ebfbd375a163d1e70d707e9c59e4bef4
title: WATERWORKS
cart: 0x0
rom: 0x0
ram: 0x0
The three zeroes are useful:
| Header field | Value | Meaning |
|---|---|---|
| Cartridge type | 0x00 |
ROM-only, no memory-bank controller |
| ROM size | 0x00 |
32 KiB |
| RAM size | 0x00 |
No external cartridge RAM |
The stored header checksum is valid (0x9A), and the entry point is the usual small trampoline:
0100 nop
0101 jp 0150
The absence of an MBC is a gift. There is no current-bank value to track and no banked-address ambiguity: CPU address 0x07BC is also byte offset 0x07BC in the file.
A quick strings pass finds the title text, the service warning, and the label MAINT TOKEN:, but no flag and no human-readable key sequence:
strings -a -t x -n 4 waterworks.gb
07d4 CLEARWATER
0829 OPERATOR MANUAL:
083a DOC HS-441 SEC 7.3
084d SERVICE MODE NEEDS
0860 A KEY SEQUENCE
0a30 SERVICE MODE
0a3d OVERRIDE ACCEPTED
0a4f MAINT TOKEN:
That was my first dead end. The UI tells us a sequence exists, but searching for words such as DOWN, LEFT, or SELECT cannot work because the program stores inputs as hardware bitmasks.
The small code base maps cleanly once the main loop is followed from 0x0150:
| Address | Role |
|---|---|
0x0150 |
Boot and hardware initialization |
0x018A |
Main loop |
0x01FB |
Joypad scan and edge detection |
0x022A |
Service-key state machine |
0x0414 |
Screen-render dispatcher |
0x071E |
Service-mode screen |
0x076A |
Service-token decoder |
0x07BC |
Eight-byte service sequence |
0x0A6E |
Encrypted maintenance token |
The call order in the main loop is the important structural clue. Every scan reads the joypad, derives a new-press mask, and calls the service detector before dispatching the normal title/game/completion state. That means the backdoor is globally reachable, not limited to one menu.
Reading an Active-Low, Multiplexed Joypad
The Game Boy joypad register lives at FF00, usually named P1 or JOYP. Eight buttons share four input bits, so software selects one of two groups before reading:
- directions: Right, Left, Up, Down;
- buttons: A, B, Select, Start.
The electrical inputs are active-low. A pressed button reads as zero, which is the opposite of the representation the rest of this program wants.
The scanner at 0x01FB is short enough to annotate instruction by instruction:
01FB ld a,$20 ; select direction-key group
01FD ldh ($00),a ; JOYP = 0x20
01FF ldh a,($00) ; settle/read several times
0201 ldh a,($00)
0203 and $0F
0205 ld b,a ; B = direction nibble
0206 ld a,$10 ; select A/B/Select/Start group
0208 ldh ($00),a
020A ldh a,($00)
020C ldh a,($00)
020E ldh a,($00)
0210 ldh a,($00)
0212 and $0F
0214 swap a ; move button group into high nibble
0216 or b ; combine both groups
0217 cpl ; active-low hardware -> active-high mask
0218 ld b,a ; B = currently held buttons
0219 ld a,($C000) ; previous held mask
021C xor b ; bits that changed
021D and b ; keep only 0 -> 1 transitions
021E ld ($C001),a ; newly pressed edges
0221 ld a,b
0222 ld ($C000),a ; save current held mask
0225 ld a,$30
0227 ldh ($00),a ; deselect both groups
0229 ret
In C-like form, the last four instructions are doing this:
new_press = (previous ^ current) & current;
previous = current;
This is the detail that makes emulator automation fail in a surprisingly quiet way. The state machine consumes edges from C001, not the held mask in C000. Holding Down for ten frames produces one 0x08, not ten of them.
After the complement, the normalized layout is:
| Mask | Input |
|---|---|
0x01 |
Right |
0x02 |
Left |
0x04 |
Up |
0x08 |
Down |
0x10 |
A |
0x20 |
B |
0x40 |
Select |
0x80 |
Start |
Now the ROM has given us an alphabet. The next routine supplies the sentence.
The Chord Check That Gave the Game Away
The service detector begins at 0x022A. Its first check looks odd until viewed as the classic one-hot test:
022A ld a,($C001) ; newly pressed buttons
022D or a
022E ret z ; no new edge
022F ld b,a
0230 dec a ; pressed - 1
0231 and b ; pressed & (pressed - 1)
0232 jr z,$0239 ; zero means exactly one bit was set
0234 xor a
0235 ld ($C003),a ; a chord resets sequence progress
0238 ret
For a power of two, subtracting one clears the only set bit and fills lower positions; ANDing the two values produces zero. If two or more buttons are present, at least one bit survives.
That tells us two things before we have even found the table:
- every sequence element is one button;
- pressing a chord cannot shortcut the matcher.
This was much more useful than staring at the title screen. It told me exactly what kind of data to look for: a short run of powers of two referenced by this routine.
Recovering the Eight-Step State Machine
The continuation at 0x0239 uses C003 as an index and adds it to a hard-coded ROM pointer:
0239 ld a,($C003) ; current progress, 0..7
023C ld e,a
023D ld d,$00
023F ld hl,$07BC ; sequence table
0242 add hl,de
0243 ld a,(hl) ; expected one-hot mask
0244 cp b ; compare with newly pressed mask
0245 jr nz,$0263 ; mismatch path
0247 ld a,($C003)
024A inc a
024B ld ($C003),a
024E cp $08
0250 ret nz ; not finished yet
0251 xor a
0252 ld ($C003),a ; reset matcher
0255 ld a,$04
0257 ld ($C002),a ; application state = SERVICE
025A ld a,$01
025C ld ($C010),a ; redraw requested
025F call $0794 ; acceptance tone
0262 ret
At 0x07BC, the eight bytes are:
08 08 01 10 02 04 20 40
Mapping them through the joypad table gives:
08 Down
08 Down
01 Right
10 A
02 Left
04 Up
20 B
40 Select
So the full service sequence is:
Down → Down → Right → A → Left → Up → B → Select
The mismatch path has one nice implementation detail:
0263 xor a
0264 ld ($C003),a ; progress = 0
0267 ld a,($07BC) ; first expected mask
026A cp b
026B ret nz
026C ld a,$01
026E ld ($C003),a ; wrong key was Down: restart at step 1
0271 ret
A wrong button resets progress, except when that same button is the first element (Down). Then it immediately becomes the beginning of a new attempt. It is a tiny prefix-overlap optimization, the input-sequence equivalent of not throwing away a useful partial string match.
⚠️ The two initial Downs must be two distinct edges. Press Down, let it release, then press Down again. A held Down never advances from step one to step two.
Following Success Instead of Patching It
It would be easy to stop after the table, enter the sequence, and copy the answer from an emulator. I kept going because the challenge deliberately separates the unlock logic from the reward logic.
Eight matches produce three side effects:
C003 = 0 reset sequence progress
C002 = 4 select service-mode state
C010 = 1 request screen redraw
CALL 0x0794 play the acceptance sound
The main renderer at 0x0414 dispatches state 4 to 0x071E. Nothing in the sequence checker contains the token. Forcing C003 to eight or patching the final compare reaches the same screen, but it still does not explain how the screen obtains its text.
The relevant WRAM state is small:
| Address | Purpose |
|---|---|
C000 |
Currently held button mask |
C001 |
Newly pressed edge mask |
C002 |
Screen/state: 0 title, 1 running, 2 complete, 3 trip, 4 service |
C003 |
Service-sequence progress |
C010 |
Redraw request |
That state transition is also an independent verification of our sequence interpretation: after the eighth edge, C002 must become 4 and C003 must return to zero.
The Token Is Tile Data, Not Plaintext
The service renderer prints the fixed strings SERVICE MODE, OVERRIDE ACCEPTED, and MAINT TOKEN:. Then it loads:
HL = 0x0A6E encrypted token bytes
C = 0x3C initial rolling key
DE = 0x9902 first tile-map destination
B = 0x10 16 characters
It calls 0x076A, changes DE to 0x9922, reloads B = 0x10, and calls the same routine again. The 32-character token is rendered as two rows.
The decoder is another compact LR35902 loop:
076A ld a,(hl+) ; ciphertext byte
076B xor c ; recover ASCII
076C sub $20 ; ASCII -> font tile index
076E ld (de),a
076F inc de
0770 ld a,c
0771 add a,$07 ; next rolling key
0773 ld c,a
0774 dec b
0775 jr nz,$076A
0777 ret
My first reading treated SUB 0x20 as another obfuscation step. That produces garbage if it is copied into a static decryptor. The subtraction belongs to the renderer, not the cipher: this font stores ASCII space (0x20) as tile zero, so the code converts printable ASCII into tile indices immediately before writing VRAM.
The real token transform is therefore just:
plaintext[i] = ciphertext[i] XOR key
key = key + 7 mod 256
initial key = 0x3C
The ciphertext occupies 0x0A6E–0x0A8D:
5a 2f 2b 36 23 38 04 32 07 1e f0 ff f9 f4 fb fa
c1 dc de a4 97 a0 a0 b8 96 99 9b 9d 65 37 3f 68
A static extractor is enough:
from pathlib import Path
rom = Path("waterworks.gb").read_bytes()
cipher = rom[0x0A6E:0x0A8E]
key = 0x3C
plain = bytearray()
for byte in cipher:
plain.append(byte ^ key)
key = (key + 7) & 0xFF
print(plain.decode("ascii"))
flag{gb_service_mode_override01}
That gives us the token without running the ROM, but it still leaves one valuable question: does the input sequence really drive the unmodified program to the same bytes?
Reproducing the Whole Path with PyBoy
I used PyBoy 2.8.1 for the second solve path. The 120-frame warm-up is intentional: pressing Start or a sequence key while the cartridge is still booting and performing its first title redraw can be consumed before the main input loop starts.
PyBoy.button() is also easy to misuse. It schedules one pressed frame and an automatic release on the following tick. Calling button_release() immediately after button() collapses both events before this ROM observes a rising edge.
The complete verification script is:
from pyboy import PyBoy
STATE = 0xC002
PROGRESS = 0xC003
gb = PyBoy("waterworks.gb", window="null", sound_emulated=False)
gb.set_emulation_speed(0)
# Finish boot and the initial title-screen render.
gb.tick(120, render=False, sound=False)
assert gb.memory[STATE] == 0
sequence = [
"down", "down", "right", "a",
"left", "up", "b", "select",
]
for button in sequence:
gb.button(button)
gb.tick(2, render=False, sound=False)
assert gb.memory[STATE] == 4
assert gb.memory[PROGRESS] == 0
# Let state 4 render both token rows.
gb.tick(3, render=False, sound=False)
tiles = (
bytes(gb.memory[0x9902:0x9912])
+ bytes(gb.memory[0x9922:0x9932])
)
# The font maps ASCII 0x20 to tile zero.
token = bytes((tile + 0x20) & 0xFF for tile in tiles).decode("ascii")
print(token)
gb.stop(save=False)
During the run, C003 advances exactly as expected:
Down progress=1 state=0
Down progress=2 state=0
Right progress=3 state=0
A progress=4 state=0
Left progress=5 state=0
Up progress=6 state=0
B progress=7 state=0
Select progress=0 state=4
The VRAM rows decode to:
flag{gb_service_
mode_override01}
The newline is a display-layout decision, not part of the token.
PyBoy Input Gotcha, Reproduced
Because the cartridge uses edge detection, the difference between PyBoy’s convenience and persistent-input APIs matters:
# Correct: one pressed tick, then PyBoy releases automatically.
gb.button("start")
gb.tick(2)
# Also correct: manage a persistent press manually.
gb.button_press("start")
gb.tick(1)
gb.button_release("start")
gb.tick(1)
# Wrong for this ROM: both events land before a visible edge.
gb.button("start")
gb.button_release("start")
gb.tick(2)
I tested all three forms against the cartridge. The first two enter the requested state. The last leaves the program on the title screen because C001 never receives the Start edge.
This was the main dynamic-analysis gotcha. If the recovered sequence looks right but the service screen never appears, the problem is probably not the button map. It is usually input lifetime.
Flag
flag{gb_service_mode_override01}
Reflection
This is my favorite of the three cartridge challenges because every layer is native to the platform rather than generic CTF decoration:
JOYPis multiplexed and active-low;- the scanner converts held state into rising edges;
- a one-hot arithmetic trick rejects chords;
- the key sequence is a table-driven finite-state machine;
- mismatch handling preserves a useful one-byte prefix;
- success changes application state instead of jumping directly to a flag routine;
- the final subtraction belongs to tile rendering, not cryptography.
The challenge also supports two clean, independent solutions. Static analysis recovers the table and decrypts the token. Emulation proves the unmodified ROM accepts the mapped buttons, reaches state 4, and renders those same bytes into VRAM.
That agreement is what turns 08 08 01 10 02 04 20 40 from a plausible-looking byte run into a complete reverse-engineering result.