Lab 04 — CTF: The Honest Trampoline¶
Objective. Open a vault that defends itself three ways: its PIN is not in the binary, its check function has been inlined out of existence, and it checksums its own prologue on every call. Two of the four hook types you know will fail. One walks straight past all three defences — and it does not modify a single instruction.
Difficulty: CTF — the chapter's capstone
Time: 2–4 hours
Prerequisites: Labs 01, 02
and 03
Device used to verify this challenge: i9000S, Android 13 / API 33, unrooted, no
frida-server. The challenge is solvable end to end as the unprivileged shell user. It was solved
that way while writing this lab, and the measured output is in §8.
1. The challenge¶
# host
cd 65-Hooking/labs
export ANDROID_NDK=$HOME/Android/Sdk/ndk/25.2.9519653
./build-ctf.sh --run
Measured baseline:
=== the pin is not in the file (this SHOULD find nothing useful) ===
(nothing)
=== baseline: a wrong pin ===
vault_open @ 0x769ab73768
vault_open("0000") = -1 (wrong pin)
You are given:
| File | What it is |
|---|---|
libvault.so |
The target. Exports vault_open and vault_pin_ok |
vault-runner |
A harness that calls vault_open(pin, out, sizeof out) and prints the result. No hooks, no hints |
Your job: make vault_open return 0 and print the flag, which has the form CH65{...}.
1.1 The return codes — you will meet all of them¶
| Code | Meaning |
|---|---|
0 |
Success, and out holds the flag |
-1 |
Wrong PIN |
-2 |
TAMPERED. vault_open noticed that its own first eight instruction words no longer sum to what they summed to at load time |
-3 |
Output buffer too small (the runner sizes it correctly; you will not see this) |
1.2 Rules¶
- No brute force. The PIN space is far too large and guessing is not the exercise.
- No patching the binary on disk. Everything happens at runtime, in a process you write.
- No root, no
frida-server, no Frida. The whole challenge is solvable asshell. If you have a rooted device, solving it with Frida is a legitimate second lap — but do it after, not instead. - Only this library. Do not point your solver at anything else on the phone.
2. Reconnaissance — do this before writing any code¶
2.1 Is the PIN in the file?¶
# host
strings $OUT/libvault.so | grep -Ei 'pin|flag|CH65|[0-9]{4}'
Measured: nothing useful. Only symbol names (vault_pin_ok, g_pin_key, FLAG_ENC). The strings
are stored encoded and decoded at runtime.
Under the Hood. The first version of this challenge stored the key as
#define PIN_KEY 0x5Aand was trivially broken: clang at-O2saw a constant array XORed with a constant key, folded the whole loop at compile time, and emitted"1379-alp"and"CH65{got_slot_beats_text_checksu"directly into.rodata. The optimiser deobfuscated it. The shipped version stores the keys asvolatile, which forbids the fold. This is why so much real string obfuscation is worthless: if the key is a compile-time constant, your toolchain is a deobfuscator.
2.2 What is the hook surface?¶
# host
cd ../scripts
./hook-surface.sh $OUT/libvault.so
Read the output as a menu of options, not as a list of facts. Pay attention to:
- which symbols are exported (inline-hookable);
- which symbols are imported with a
JUMP_SLOTrelocation (PLT-hookable); - the RELRO line, which tells you whether a GOT write needs an
mprotectfirst.
2.3 What does the prologue look like?¶
# host
$NDK/llvm-objdump -d $OUT/libvault.so | sed -n '/<vault_open>:/,/^$/p'
Two questions to answer from that listing before you continue:
- Does
vault_opencontain abltovault_pin_ok? - What does
vault_opendo before it does anything with the PIN?
2.4 The analyser's verdict¶
# host
python3 -B hookability.py $OUT/libvault.so --symbol vault_pin_ok
python3 -B hookability.py $OUT/libvault.so --symbol vault_open
The callers in file line for vault_pin_ok is worth more than everything else on the screen.
3. Hints, in escalating order¶
Stop reading as soon as you have enough. Each is behind a fold.
Hint 1 — where to start
Do not start by trying to make `vault_open` return 0. Start by finding out what the PIN is. You have a whole chapter of techniques for observing a running program, and the PIN must exist in cleartext *somewhere*, at *some* instant, or the comparison could not happen. Where is it, and what is looking at it when it is there?Hint 2 — the first thing you will try, and why it fails
You will hook `vault_pin_ok` to return 1. It will work — call it directly and it returns 1 — and `vault_open` will still return `-1`. Run the Lab 03 diagnostic:$NDK/llvm-objdump -d $OUT/libvault.so | grep "bl.*<vault_pin_ok>"
Hint 3 — the second thing you will try, and why it fails
So you hook `vault_open` itself, inline, the way Lab 02 taught you. Now it returns `-2`. Look again at what `vault_open` does *first*. It hashes its own first eight instruction words and compares the result to a value captured at load time by a constructor. Your 16-byte patch changed four of those eight words. Measured, patching a single word at `+12` with a `nop`:before patch: vault_open("0000") = -1
after 1-word nop at +12: vault_open("0000") = -2 (-2 == TAMPERED)
restored: vault_open("0000") = -1
Hint 4 — the shape of the answer
Two facts you have already established: 1. The check is inlined **into** `vault_open`, so hooking the standalone copy is useless. 2. `vault_open` verifies its own **code**. Now ask: the inlined copy of the check still has to compare two strings. How does it do that? Look at the `bl` instructions inside `vault_open`. A checksum over instructions cannot see a change that is not in the instructions.Hint 5 — near-complete
`strcmp` is an **import**. The inlined comparison reaches it through this library's PLT, which loads a pointer out of the GOT — a **data** table. Swap that pointer for one of your own and: - no instruction changes anywhere, so the prologue checksum is identical and defence 3 never fires; - the inlining does not matter, because you are not hooking the check, you are hooking what the check calls; - and your replacement receives **both arguments**, the second of which is the decoded PIN. `../examples/plt-got-hook.c` already walks `DT_JMPREL` and swaps a `strcmp` slot. It is 90% of your solver.4. Grading rubric¶
Getting the flag is the entry ticket, not the score. Grade yourself on what your solution proved.
| Tier | What you did |
|---|---|
| Bronze | Got the flag by any means, including patching g_baseline in memory so the checksum passes |
| Silver | Got the flag with a PLT/GOT hook, and can explain why the checksum did not fire |
| Gold | Silver, plus you read the PIN out of the hook, removed the hook entirely, and opened the vault with the correct PIN and no instrumentation at all |
| Platinum | Gold, plus you can state precisely what change to libvault.c would defeat your solution, and what would defeat that |
The Gold tier is the point of the whole chapter. A hook that bypasses a check gives you one result. A hook that tells you what the check wanted gives you the input, and an input is a finding you can put in a report, hand to a developer, and reproduce without a tool.
Mental Model. Hooking is an instrumentation technique. It proves behaviour; it is not itself a finding. "I bypassed the PIN check with a hook" is a statement about your toolkit. "The PIN is
1379-alpha, hard-coded in the library, and here is the byte offset" is a statement about the product.
5. Platinum questions¶
Answer these in writing before you look at §7.
vault_openchecksums eight words of its own code. Where does the baseline live, and what does that imply about the checksum's value as a control?- Name a change to
libvault.cthat would defeat the intended PLT/GOT solution. - Name a hook that would defeat that change.
- The challenge decodes the PIN onto the stack immediately before comparing. If you could not hook anything at all, but could read the process's memory, would that help? What would you have to get right?
- Suppose the developer replaced
strcmpwith a hand-written comparison loop inside the same translation unit. Which of the five hook types survive?
6. Cleanup¶
# host
adb shell rm -f /data/local/tmp/libvault.so /data/local/tmp/vault-runner /data/local/tmp/solver
find 65-Hooking -name '*.so' -o -name '*.o' -o -name '__pycache__'
The find must print nothing.
7. Instructor notes and solution¶
Stop here if you have not solved it.
Full solution and measured output
### 7.1 The three defences and their intended fates | Defence | Intended attacker experience | |---|---| | PIN stored XOR-encoded, decoded on the stack | `strings` finds nothing; forces runtime observation | | `vault_pin_ok` exported **and** inlined at `-O2` | The natural first hook installs perfectly and changes nothing | | `vault_open` checksums its own first 8 words | The natural second hook (inline, on `vault_open`) is caught, returning `-2` | ### 7.2 The solution Walk `libvault.so`'s `DT_JMPREL` relocations, find the `R_AARCH64_JUMP_SLOT` entry for `strcmp`, `mprotect` its GOT page writable, and overwrite the slot with your own function. Your function records `b` — the decoded PIN — and returns 0. The three defences fall in order: - The **inlining** does not matter: you are hooking what the inlined copy calls, not the copy. - The **checksum** does not fire: you changed a pointer in `.got.plt`, not an instruction in `.text`. - The **encoding** does not matter: you are reading the string after it was decoded, at the moment of comparison. ### 7.3 Measured output — solved on the `i9000S`, API 33, unrooted=== decoy 1: hook vault_pin_ok (the inlined check) ===
vault_pin_ok("0000") = 1 (hook works)
vault_open("0000") = -1 (unchanged - it was inlined)
=== intended: PLT/GOT hook on strcmp ===
swapped strcmp slot at 0x73cd055c40
[plt] strcmp("0000", "1379-alpha") <-- second argument is the PIN
vault_open("0000") = 0 -> CH65{got_slot_beats_text_checksum}
captured pin = "1379-alpha"
vault_open @ 0x7002b6a780
vault_open("1379-alpha") = 0 FLAG: CH65{got_slot_beats_text_checksum}
8. Where to go next¶
| If you want to | Go to |
|---|---|
| Understand detection and how it is defeated systematically | 67-BypassingProtections |
| Do all of this with one line of JavaScript instead | 53-Frida |
| Understand the managed layer this challenge deliberately avoided | 13-ART |
| See what an integrity check can and cannot buy | 38-TamperDetection |