Lab 04 — Sandbox Escape by the Front Door: Exported Components¶
Difficulty: Intermediate Time: 2 hours Prerequisites: Lab 03, Chapter 21 §3.14.3 and §5.7, a working probe APK.
Objective¶
Take a target application whose sandbox is intact, find an exported ContentProvider with an
openFile() implementation, exploit a path traversal through it to read a file the provider never
intended to share, then remediate with FileProvider and verify the fix from the same probe APK.
The point is not the traversal technique. The point is that the sandbox held throughout, and the data left anyway, because the developer opened a door in it.
Why this matters¶
Mental Model The sandbox is a wall. Exported components are doors the developer cut into it deliberately, because an app that cannot talk to anything is not useful. The wall does not have an opinion about the doors. Every question you can ask about a door — who may open it, what is behind it, does it check who is knocking — is an application question, not a platform question.
This lab exists because the highest-yield findings on an ordinary Android engagement are here, and because the mechanism behind the most serious one — file-descriptor passing — is genuinely counter-intuitive the first time you meet it. An attacker who cannot open a file gets its contents, because a more privileged process opened it on their behalf and handed over a descriptor that the kernel will never recheck.
Definition — exported component An
<activity>,<service>,<receiver>or<provider>that other applications may invoke. It is exported ifandroid:exported="true", or — for apps targeting API < 31 — implicitly if it declares an<intent-filter>and does not say otherwise.<provider>defaulted toexported="true"for apps targeting below API 17, which is why very old apps are so consistently leaky.
Scope and cross-references¶
This lab covers exported components only as a boundary question: what the sandbox does and does not do about them. It deliberately does not re-teach IPC mechanics or manifest analysis. Those live in their own chapters and are better there:
| For | Read |
|---|---|
How Binder actually carries a call, getCallingUid, transaction codes |
25-Binder |
| The full IPC attack surface and the probe-app methodology | 24-IPC, and its attacker-app |
Intent redirection, PendingIntent mutability, deep links |
26-Intent |
UriMatcher, URI permission grants, provider SQL injection |
27-ContentProviders |
| Every manifest attribute that affects exposure, and how to read a binary manifest | 32-Manifest |
What is here and nowhere else: the argument that none of this is a sandbox failure, and the
descriptor-passing mechanism that makes openFile the highest-severity case.
Setup¶
Use the victim from Lab 03 — it already has the vulnerable provider — or any app you own with an exported provider. Install both apps.
# host
adb install -r victim/build/outputs/apk/debug/victim-debug.apk
adb install -r attacker/build/outputs/apk/debug/attacker-debug.apk
adb shell am start -n com.example.victim/.MainActivity # let it write its prefs
Confirm the target's identity and that it is intact:
# adb shell
pm list packages -U | grep victim
ls -ld /data/data/com.example.victim 2>&1 # Permission denied — the wall is standing
Steps¶
Step 1 — Enumerate the doors, statically¶
# host
apktool d -f victim.apk -o work/ >/dev/null
grep -nE '<(activity|activity-alias|service|receiver|provider)' work/AndroidManifest.xml
Build the inventory. For every component record four things, because the fourth is the one people forget:
| Component | Type | exported |
Permission attribute | targetSdk of the app |
|---|---|---|---|---|
# host
grep -oE 'android:targetSdkVersion="[0-9]+"' work/apktool.yml work/AndroidManifest.xml
grep -oE 'android:(permission|readPermission|writePermission|protectionLevel)="[^"]*"' work/AndroidManifest.xml
grep -oE 'android:authorities="[^"]*"' work/AndroidManifest.xml
grep -oE 'android:grantUriPermissions="[^"]*"' work/AndroidManifest.xml
Question 1. One component in your inventory has no android:exported attribute at all but does have
an <intent-filter>. Is it exported? What do you need to know before you can answer, and where do you
find it?
Question 2. Another has android:permission="com.example.victim.permission.INTERNAL". Before you
record it as protected, what must you check about that permission, and which file tells you?
Step 2 — Confirm the platform's own view¶
Static analysis of a manifest is a claim. dumpsys is the platform's own record, and it accounts for
manifest merging from libraries and SDKs, which the source manifest does not.
# adb shell
dumpsys package com.example.victim | sed -n '/Activity Resolver Table/,/Permissions:/p'
dumpsys package com.example.victim | grep -A3 -i 'ContentProvider Authorities'
dumpsys package com.example.victim | grep -iE 'permission|protectionLevel'
Question 3. Is any component in the platform's list absent from the source manifest you decoded, or outside the app's own package prefix? What does that mean, and why is it a common blind spot?
Step 3 — Probe from the shell (and understand why that is not enough)¶
# adb shell
content query --uri content://com.example.victim.provider
content read --uri content://com.example.victim.provider/input.txt
Record the results — and then immediately record that they prove nothing about apps. Your shell is UID
2000 in the shell domain and holds privileges no application holds.
Question 4. Name a concrete case in which content query from the shell would succeed and the same
query from an installed app would fail. Name one where the reverse could happen.
Step 4 — Probe from the app, which is the real experiment¶
Run the probe APK from Lab 03 and read section 3.
# host
adb logcat -c
adb shell am start -n com.attacker.sandboxprobe/.ProbeActivity
adb logcat -s SANDBOXPROBE:W
You should see REACHABLE on the plain query(). Fill in both columns:
| Operation | From adb shell |
From the probe APK |
|---|---|---|
query() on the authority root |
||
openFile() on a legitimate name |
||
insert() |
||
call() |
Question 5. query() returned the session token to an app holding no permissions at all. Walk the
access decision: which check did the platform perform, what did it consult, and what did it conclude?
Was any part of the sandbox bypassed?
Step 5 — The traversal¶
The provider's openFile() concatenates a caller-supplied path segment onto filesDir and calls
ParcelFileDescriptor.open(). Attack it.
// in the probe
listOf(
"input.txt", // control: should work
"../shared_prefs/secure.xml", // one level up
"../databases/app.db",
"../../../../data/data/com.example.victim/shared_prefs/secure.xml",
"..%2Fshared_prefs%2Fsecure.xml", // encoded
"../../../../system/etc/hosts" // outside the app entirely
).forEach { seg -> /* openInputStream(Uri.withAppendedPath(base, seg)) */ }
Warning
Uri.withAppendedPathandUri.parsenormalise some inputs. If a payload silently does nothing, loguri.lastPathSegmentanduri.pathinside the victim to see exactly what arrived. Half the time a "failed" traversal never left the attacker.
Record which payloads work.
Question 6. The attacker's own File("/data/data/com.example.victim/shared_prefs/secure.xml").canRead()
returned false in section 1 of the probe. The traversal returned the file's contents. Both are true
simultaneously. Explain the mechanism precisely — which process called open(), under which identity,
and at what moment did the kernel perform its permission check?
Question 7. ../../../../system/etc/hosts probably succeeded and another app's private data
certainly did not. State the exact bound on this vulnerability, and write the one-sentence severity
statement you would put in a report — including the prerequisite an attacker needs.
Step 6 — Establish reach-through¶
A door is only as serious as the room behind it. Do the part no tool does for you.
For each REACHABLE result, answer: what can you make this component do that you could not do
directly?
| Reached | Contents | Reach-through |
|---|---|---|
shared_prefs/secure.xml |
session token | Authenticate to the backend as the user? Test it. |
databases/app.db |
||
files/ contents |
Question 8. You have the session token. Before you write "account takeover", what must you actually verify, and how? What would make the same token a Low finding rather than a High one?
Step 7 — Remediate, two ways¶
Preferred: delete the provider and use FileProvider.
<!-- victim: AndroidManifest.xml -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.victim.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<!-- victim: res/xml/provider_paths.xml — an allowlist, not a filter -->
<paths>
<files-path name="exports" path="exports/" />
</paths>
// Sharing is now a per-URI, per-recipient, revocable grant — not a standing door.
val file = File(File(filesDir, "exports").apply { mkdirs() }, "report.pdf")
val uri = FileProvider.getUriForFile(this, "com.example.victim.fileprovider", file)
startActivity(Intent(Intent.ACTION_SEND).apply {
type = "application/pdf"
putExtra(Intent.EXTRA_STREAM, uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
})
If a custom provider genuinely must remain, canonicalise and verify containment:
override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor {
if (mode != "r") throw SecurityException("read-only")
val root = File(context!!.filesDir, "exports").canonicalFile
val name = uri.lastPathSegment ?: throw FileNotFoundException("no segment")
val target = File(root, name).canonicalFile
// canonicalFile resolves ".." and symlinks. Compare AFTER resolution, and
// include the separator so /exports-evil does not match /exports.
if (!target.path.startsWith(root.path + File.separator))
throw SecurityException("path escapes the export root")
return ParcelFileDescriptor.open(target, ParcelFileDescriptor.MODE_READ_ONLY)
}
Pentester's Note When you review a fix, check the comparison happens after canonicalisation and that the separator is included.
startsWith(root.path)without the trailing separator is a real bug:/data/.../exportsis a prefix of/data/.../exports-backup. And a check performed on the raw string before resolving..is no check at all. These two mistakes account for most "fixed" traversals that are not fixed.
Now re-run the probe.
Question 9. Which rows changed, and which did not? If query() still returns the token, what did
you forget?
Step 8 — The honest write-up¶
Question 10. Write the finding. It must contain, in this order: the vulnerable code, why a developer writes it, how you detect it statically and dynamically, the exploit, the fix as code, and a severity with its prerequisite stated. Then add one sentence stating which sandbox mechanism failed.
Hints¶
Hint 1 — deciding whether a component is exported without an explicit attribute
The rule depends on the app's `targetSdkVersion`: - Any component with an `Hint 2 — a permission attribute that protects nothing
A custom permission is only as strong as its `protectionLevel`. Check the `# host
grep -A3 '<permission ' work/AndroidManifest.xml
Hint 3 — content read exists and you did not know
# adb shell
content query --uri content://authority/path
content read --uri content://authority/path
content insert --uri content://authority/path --bind key:s:value
content call --uri content://authority --method someMethod
Hint 4 — the traversal payload disappears
`Uri` normalisation. Try, in order: building the URI from a full string rather than `withAppendedPath`; URL-encoding the separators (`..%2F`); using multiple path segments so `lastPathSegment` returns only the final one (which tells you the provider's parsing is different from what you assumed); and logging what actually arrives on the victim's side. If the provider uses `uri.path` rather than `uri.lastPathSegment`, your payload shape changes completely. Read the decompiled `openFile` before guessing.Hint 5 — you cannot tell whether the fix works
Test three inputs against the fixed provider, not one: a legitimate name (must succeed), a traversal (must be refused), and a *symlink* you plant inside the export directory pointing outside it (must be refused, because `canonicalFile` resolves symlinks too). The third is the one that distinguishes a real fix from a string check.Solution¶
Answers
**1.** You cannot answer without the app's `targetSdkVersion`. Below API 31, a component with an `Success criteria¶
- You produced a component inventory including
exported, permission, protection level, and the app'stargetSdkVersion - You cross-checked the source manifest against
dumpsys packageand looked for SDK-contributed components - You can state the export-default rule for each relevant API level without looking it up
- You checked the
protectionLevelof every custom permission before treating it as a control - You ran every probe from both
adb shelland the probe APK and recorded both columns - You extracted a file the provider never intended to share
- You can explain the file-descriptor mechanism in terms of which process opened the file and when the kernel checked
- You established the exact bound of the traversal by testing what it cannot reach
- You walked the reach-through of at least one reached file rather than asserting impact
- You applied the
FileProviderfix and verified it against a legitimate name, a traversal, and a symlink - You noticed that fixing
openFilealone leavesquery()open - Your write-up states that the sandbox did not fail