Lab 04 — CTF: The gate that was not there¶
Difficulty: CTF Time: 4–6 hours Prerequisites: Lab 01, Lab 02, Lab 03, 18-ARM, 34-Cryptography, 68-ReverseEngineering
The challenge¶
You are given a single AArch64 executable, ctfvault. Run it with a licence key and it says:
vault sealed
It says that for every key you try. It says it on a clean device and on a rooted one. There is no error message, no log output, and no network traffic — the program never touches the network.
Open the vault and recover the flag.
The flag has the form R2CTF{...}.
Rules¶
- Recovering the flag is the objective. Making the program print "vault open" is not, on its own, a solve. Read that sentence twice; it is the whole challenge.
- You may use radare2, rizin, Cutter, Ghidra, IDA, a debugger, Frida, or a pencil.
- You may not read
../examples/03-ctf-vault.c. It is the source of the binary you are attacking and it contains the answer. - Target only this binary, which you compiled yourself.
Setup¶
# host -- builds out of tree; nothing is written into the repository
cd 59-radare2/examples
./build.sh
ctfvault appears in $HOME/ch59-build. Run it:
# host, or on a device
adb push ~/ch59-build/ctfvault /data/local/tmp/ch59_ctfvault
adb shell chmod 755 /data/local/tmp/ch59_ctfvault
adb shell /data/local/tmp/ch59_ctfvault
adb shell /data/local/tmp/ch59_ctfvault somekey
Measured — built with
aarch64-linux-android21-clang(NDK 25.2.9519653) at-O2 -Wall -Wextra -pie -Wl,-z,max-page-size=16384, zero warnings, and executed on the AVDsdk_gphone16k_x86_64(Android 15 / API 35, x86_64, 16 KiB pages) throughlibndk_translation.so. With no arguments it prints a usage line and a hint; with any argument tried so far it printsvault sealed.On the state of this chapter radare2 was not installed on the machine where this chapter was written, so every
r2command below is Unverified. Every stated fact about the binary and its behaviour on a device is Measured and says so. If a command behaves differently, trust the tool.
Objective¶
Recover the flag, and be able to explain:
- Why the program refuses every key, including the correct one.
- Why patching the function with the obvious name does not solve the challenge.
- Why a complete bypass still does not produce the flag.
- What the difference is between defeating a check and recovering a secret.
Scoring¶
| Level | Achievement |
|---|---|
| Bronze | Make the program print vault open at all |
| Silver | Explain precisely why the correct key alone is rejected |
| Gold | Recover the real flag |
| Platinum | Recover it without patching anything — read the key out of the binary and supply it |
| Diamond | Write a script that recovers the flag from the binary alone, with no device |
Bronze is easy and is a trap. Read the rules again.
Hints¶
Open these only when stuck. Each costs you nothing but the satisfaction.
Hint 1 — where to start
Start with no analysis at all. What does the binary tell you about itself for free?# Unverified
rabin2 -I ~/ch59-build/ctfvault
rabin2 -qz ~/ch59-build/ctfvault # strings
rabin2 -qi ~/ch59-build/ctfvault # imports
Hint 2 — why every key fails
You found four functions. One of them sounds like the security check. It is not the one that decides. Disassemble the function that *calls* the others and read the order of operations:# Unverified
r2 ~/ch59-build/ctfvault
[0x0]> aa
[0x0]> pdf @ sym.vault_open
Hint 3 — the environment probe
The probe checks whether a file exists. The path is XOR-encrypted in the source — and at `-O2` the compiler folded the decode away, so `strings` shows you the plaintext anyway. This is the measured finding from [lab 02](lab-02-triage-a-real-library.md) Part 4, reused here deliberately.# host
strings -n 4 ~/ch59-build/ctfvault | grep -i xbin
Hint 4 — Bronze, and why it is a trap
Patch the probe to return 0 and the gate opens for the correct key. Patch *both* the probe and the verifier and it opens for **any** key. Do the second one and look carefully at what it prints. > **Measured** — with both `vault_probe_environment` and `vault_verify_token` patched to return > constants, running with the key `wrongkey` prints `vault open:` followed by **42 bytes of > unprintable binary noise** — high-byte and control characters, not text. It is not the flag and it > is not close to the flag. (The literal bytes are deliberately not reproduced in this document; > pasting raw control characters into a Markdown file corrupts it.) Why would a full bypass produce garbage? What is the flag being decrypted *with*?Hint 5 — the reveal function
# Unverified
[0x0]> pdf @ sym.vault_reveal
Hint 6 — identifying the algorithms
Two published algorithms are in this binary and both are identifiable by their constants alone. In `vault_verify_token`, look for a `mov`/`movk` pair building each 32-bit constant: - `0x811C9DC5` — a very well-known initial value - `0x01000193` — its companion multiplier In `vault_reveal`, look for three shift amounts applied in sequence to one register: **13, 17, 5**. Search for these:# Unverified -- endianness matters; /v4 handles it for you
[0x0]> /v4 0x01000193
[0x0]> /v4 0x811c9dc5
Hint 7 — recovering the key
FNV-1a is a hash. It is not invertible. You cannot compute the key from the target value. But you were told the search space. Look again at the string the program prints when run with no arguments: > **Measured** — `rabin2 -qz` / `strings` reports the string > `licence keys are five lowercase letters`. Five lowercase letters is 265 = **11,881,376** candidates. That is seconds in C and well under a minute in Python. Find the target value in the disassembly of `vault_verify_token` — it is the constant the hash is compared against — and brute force it.Hint 8 — the last step
You have the key. Do not patch anything. Run the program with the correct key on a device where the probe does **not** fire — or patch only the probe, which does not affect the decryption at all, since `vault_reveal` never consults it.Walkthrough¶
Do not read this until you have solved it or genuinely given up.
Full solution
### Step 1 — orientation, no analysis# Unverified
$ rabin2 -I ~/ch59-build/ctfvault
$ rabin2 -qz ~/ch59-build/ctfvault
$ rabin2 -qi ~/ch59-build/ctfvault
0000000000004914 T vault_open
0000000000004804 T vault_probe_environment
0000000000004888 T vault_reveal
000000000000483c T vault_verify_token
# Unverified
[0x0]> aa
[0x0]> pdf @ sym.vault_open
# Unverified
$ cp ~/ch59-build/ctfvault ~/ch59-build/cv.both
$ r2 -w ~/ch59-build/cv.both
[0x0]> e io.cache=1
[0x0]> aa
[0x0]> wa mov w0, 0; ret @ sym.vault_probe_environment
[0x0]> wa mov w0, 1; ret @ sym.vault_verify_token
[0x0]> wci
# Unverified
[0x0]> pdf @ sym.vault_reveal
x ^= x << 13; x ^= x >> 17; x ^= x << 5;
#!/usr/bin/env python3
# host -- recover the licence key by exhaustive search over the stated space
import itertools, string
TARGET = 0x6DC666D5 # read this from YOUR binary's disassembly
def fnv1a(s):
h = 0x811c9dc5
for c in s.encode():
h = ((h ^ c) * 0x01000193) & 0xFFFFFFFF
return h
for t in itertools.product(string.ascii_lowercase, repeat=5):
cand = "".join(t)
if fnv1a(cand) == TARGET:
print("key =", cand)
break
# Unverified
[0x0]> wa mov w0, 0; ret @ sym.vault_probe_environment
vault open: R2CTF{gr4mm4r_n0t_l1st_p4tch_th3_d3c1s10n}
#!/usr/bin/env python3
# host -- recover the flag from the binary alone, no device, no execution
import itertools, string, sys
TARGET = 0x6DC666D5
def fnv1a(s):
h = 0x811c9dc5
for c in s.encode():
h = ((h ^ c) * 0x01000193) & 0xFFFFFFFF
return h
def xorshift32(x):
x ^= (x << 13) & 0xFFFFFFFF
x ^= (x >> 17)
x ^= (x << 5) & 0xFFFFFFFF
return x & 0xFFFFFFFF
# 1. recover the key
key = None
for t in itertools.product(string.ascii_lowercase, repeat=5):
c = "".join(t)
if fnv1a(c) == TARGET:
key = c
break
if key is None:
sys.exit("key not found -- check TARGET against your binary")
print("key =", key)
# 2. pull the 42-byte ciphertext out of .rodata.
# Find its address with: rabin2 -S ctfvault (or llvm-readelf -S)
# then read it with: px 42 @ <addr> (or slice the file below)
CIPHERTEXT_FILE_OFFSET = 0x0 # <-- set this from your own binary
LENGTH = 42
data = open(sys.argv[1], "rb").read()
ct = data[CIPHERTEXT_FILE_OFFSET:CIPHERTEXT_FILE_OFFSET + LENGTH]
# 3. regenerate the keystream and decrypt
state = fnv1a(key)
out = []
for b in ct:
state = xorshift32(state)
out.append(b ^ (state & 0xFF))
print("flag =", bytes(out).decode("ascii", "replace"))
# Unverified
[0x0]> iS~rodata
[0x0]> px 128 @ <rodata address>
[0x0]> axt @@ str.* # what references what
What this challenge teaches¶
| Lesson | Where it bit you |
|---|---|
| The function with the security-sounding name is rarely the boundary | vault_verify_token patched, nothing changed |
| Read the caller before patching the callee | vault_open's short-circuit explains everything |
| A bypass defeats a check; it cannot produce a secret | Full patch, garbage flag |
| Published algorithms are identifiable from their constants alone | FNV-1a and xorshift32, neither named in the binary |
| Compile-time obfuscation is not obfuscation | The XOR-ed su path visible in strings at -O2 |
| Static analysis is enough more often than people expect | Diamond needs no device at all |
| Your test environment is part of the measurement | A stock AVD ships /system/xbin/su and fires the probe |
That last row is worth dwelling on. If you had tested on a device without su, the unpatched binary
would have accepted the correct key immediately and the entire short-circuit lesson would have been
invisible. The environment you measure in changes what you conclude, which is the discipline
68-ReverseEngineering formalises and the reason this
repository records which device produced every number.
Cleanup¶
# host
adb shell 'rm -f /data/local/tmp/ch59_*'
adb shell 'rm -f /data/local/tmp/ch59c_*'
adb shell 'ls /data/local/tmp/' # confirm nothing of ours remains
Also remove your local build directory if you want a clean slate:
rm -rf ~/ch59-build
Extending the challenge¶
If you want more, modify ../examples/03-ctf-vault.c and rebuild:
- Strip it.
-sat link time. Now there are novault_*names and you work from strings, imports and.init_arrayalone. This is what a real target looks like. - Move the probe into
.init_arrayso it runs beforemain, and set a global flag. Now the short-circuit is not even in the same function. - Split the key check across three functions so no single patch suffices.
- Make the probe's result perturb the keystream seed rather than gate the branch — the decoupled-consequence pattern from 37-RootDetection lab 04. This is the nastiest variant and the most realistic.
- Lengthen the key to seven characters and see where Python stops being adequate.
Variant 4 is the one worth building. It produces a binary where patching anything yields a plausible-looking but wrong flag, with no error, which is exactly how real anti-tamper is designed to waste your afternoon.