Lab 01 — Keystore Inventory and Profiling¶
Difficulty: Beginner
Time: 60–90 minutes
Prerequisites: Chapter 49 §3.1–3.8, a rooted device or
emulator with frida-server running, adb. See 51 — Dynamic Analysis
for the environment build and 53 — Frida for Frida itself.
Objective¶
Enumerate every AndroidKeyStore alias an application owns, read back each key's actual
properties from KeyInfo, and produce the inventory table that belongs in every report where a client
claims to use the Keystore. Then do the part no tool does for you: work out what each key protects.
Why this matters¶
"We store it in the hardware Keystore" is the most common sentence in a mobile security questionnaire, and on its own it means almost nothing. It does not say which keys exist, what algorithm and mode they use, whether they are authentication-bound, whether they are actually in hardware on this device, or whether the value they protect also exists in plaintext three directories away.
This lab turns that sentence into a table of facts.
Mental Model
KeyGenParameterSpecis what the app asked for.KeyInfois what the device gave it. They differ on real hardware more often than anyone expects — a StrongBox request silently downgraded, or no hardware request at all on a device that then produced a software key. You cannot audit key handling by reading source code; you have to read the key back on the target's own hardware.Definition — alias The string handle under which a key is stored. It is the app's only reference to the key: there is no API that returns key material, and
getEncoded()on anAndroidKeyStorekey returnsnull.
Setup¶
# host
adb devices
adb shell getprop ro.build.version.release
adb shell getprop ro.build.version.security_patch
adb shell getprop ro.build.type
Record all four. Keystore behaviour is version-dependent and hardware-dependent, and a finding without the device it came from is not reproducible.
Check what the device can do at all — this needs no root and takes five seconds:
# host
adb shell pm list features | grep -Ei 'hardware_keystore|strongbox|keystore'
adb shell getprop | grep -Ei 'keystore|keymaster|keymint|strongbox'
adb shell 'lshal 2>/dev/null | grep -i keymint'
adb shell 'service list | grep -i keystore'
Start frida-server and pick a target:
# adb shell as root
/data/local/tmp/frida-server &
# host
frida-ps -Ua | head -30
Use a real app you are authorised to test, or build the correct implementation from
../examples/05-secure-token-store.kt so you know the ground
truth while learning the tooling.
Steps¶
Step 1 — Establish what the device is capable of¶
# host
adb shell pm list features | grep -i hardware_keystore
# android.hardware.hardware_keystore=100
adb shell pm list features | grep -i strongbox
# android.hardware.strongbox_keystore=100 (absent on most devices)
Question 1. pm list features reports a feature version number. What does that number tell you,
and why is confirming this before you start testing worth the five seconds? What would you conclude if
an app claims StrongBox protection and this command shows no strongbox_keystore feature?
Step 2 — Enumerate the aliases¶
# host
frida -U -f com.target.app -l ../scripts/enumerate-keystore.js
The script runs ks.list() automatically at startup. If it prints nothing, that is a result, not a
failure — see Question 2.
ks.list() # re-run after exercising the app
ks.table() # markdown table for the report
Question 2. The inventory is empty. There are exactly two explanations and they lead to completely different findings. Name both, and say which single command distinguishes them.
Step 3 — Make the app create its keys¶
Keys are frequently created lazily — on first login, on enabling biometrics, on opening a protected screen. A hook installed at spawn and left running for a full session catches things a single screen never will.
# host — in a second terminal, catch creation as it happens
frida -U -n com.target.app -l ../scripts/hook-keygenparameterspec.js
Now exercise the app fully: first launch, register or log in, enable every security feature it offers,
open every screen that handles data. Then re-run ks.list().
Question 3. The builder hook reports options that were never called, listing their defaults. Why is that more useful than reporting only the options that were called? Give a concrete example of a finding you can only detect this way.
Step 4 — Profile every key¶
For each alias:
ks.one("db_key_v2")
Fill in a row per alias:
| Alias | Alg | Size | Purposes | Block/pad/digest | Auth req | Timeout | Auth type | Invalidated by enrolment | Sec level | Origin |
|---|---|---|---|---|---|---|---|---|---|---|
Question 4. One key reports securityLevel = 0 (SECURITY_LEVEL_SOFTWARE). The app's marketing
and its privacy policy both say "hardware-backed encryption". Is that a finding? What must you check
before writing it up, and how would you phrase it so a developer cannot dismiss it?
Question 5. Another key reports origin = IMPORTED. What does that tell you, and what is your very
next investigative step?
Step 5 — Distinguish per-use binding from a time window¶
This is the subtlest and most consequential distinction in the chapter, and it is a single number.
ks.one("bio_key")
Look at authValidityDurationSeconds.
| Value | Meaning |
|---|---|
-1 or 0 |
Per-use. The authorisation is tied to one operation handle via a CryptoObject |
any positive N |
An N-second window after any authentication, including an unrelated device unlock |
Question 6. A key reports isUserAuthenticationRequired = true and
authValidityDurationSeconds = 30. Write the one-sentence description of that key's real security
property, as you would put it in a report. Then say what an attacker with code execution in the process
has to do to use it — and note how little that is.
Step 6 — Prove non-extractability, once, so you never doubt it again¶
ks.proveHandle("db_key_v2")
Question 7. getEncoded() returned null. State precisely what that proves and — more
importantly — what it does not prove. Why is this the fastest single diagnostic in the chapter?
Step 7 — Catch the keys that never reach the Keystore¶
The inventory only shows keys that are in the Keystore. The most common real finding is a key that never went near it.
# host
frida -U -f com.target.app -l ../scripts/hook-secretkeyspec.js
Exercise the app again, fully.
sks.summary()
sks.heap()
Question 8. The hook fires and prints 16 bytes with an entropy of 3.1 bits/byte. What does the low entropy suggest? What are the three follow-up steps, in order, before this becomes a finding?
Step 8 — Map each key to what it protects¶
This is the column ks.table() cannot fill in, and it is the column that turns an inventory into an
assessment.
ks.watch()
Then exercise the app and watch which aliases are touched, and when. Cross-reference with the filesystem:
# adb shell as root
ls -laR /data/data/com.target.app/
cat /data/data/com.target.app/shared_prefs/*.xml
# host — and where the plaintext usually turns up
adb logcat -d | grep -Ei 'token|bearer|secret|password|key='
Complete the table:
| Alias | Protects | Where the ciphertext lives | Does the plaintext exist anywhere else? |
|---|---|---|---|
Question 9. One alias is touched by nothing during a full session, and you cannot find any ciphertext it corresponds to. What have you found, and why is it worth reporting?
Question 10. For one alias, the same value appears both as a ciphertext blob in secure.xml and
in plaintext in session.xml. Which finding number is that in §5.2, what is its severity, and why does
the sophistication of the key configuration become irrelevant?
Step 9 — Check the namespace boundary¶
# adb shell
dumpsys package com.target.app | grep -iE 'userId|sharedUser'
pm list packages -U | grep ':10412$' # substitute the target's UID
Question 11. The target shares a UID with com.vendor.updater. What does that mean for the
Keystore specifically, and what is the finding shape? Does exploiting it require root?
Step 10 — Without Frida¶
Not every engagement permits instrumentation. Establish what is still available.
# adb shell as root
ls -la /data/misc/keystore/
sqlite3 /data/misc/keystore/persistent.sqlite ".tables"
sqlite3 /data/misc/keystore/persistent.sqlite "select id, domain, namespace, alias from keyentry;"
Question 12. You can read alias names and namespaces out of that database, but not key material. Why not — and what exactly would you have if you copied the whole file to another device?
Hints¶
Hint 1 — the script reports "Could not load AndroidKeyStore"
The Keystore is loaded per-process, and if you spawned with `-f` the app may not have finished starting. Wait for the UI, then call `ks.list()` from the REPL by hand. If it persists, confirm you are actually in the app's process:Java.perform(function () {
console.log(Java.use('android.app.ActivityThread').currentProcessName());
});
Hint 2 — getSecurityLevel() throws
It is API 31+. On older devices the script falls back to `isInsideSecureHardware()`, which is a
boolean floor rather than a grade: `true` means "not pure software" and tells you nothing about whether
the TEE is strong or minimal.
If you need a finer answer on an older device, generate an attested key and read
`keymasterSecurityLevel` out of the attestation instead — see
[lab 03](lab-03-attestation-analysis.md).
Hint 3 — the app crashes when you attach
Some apps detect Frida. That is [37 — Root Detection](../../37-RootDetection/README.md) and [67 — Bypassing Protections](../../67-BypassingProtections/README.md), not this chapter. For now, get what you can without instrumentation (Step 10), and note in the methodology that anti-instrumentation was present — that is itself a control worth recording.Hint 4 — you cannot find where the ciphertext is stored
Widen the search. Apps put it in `shared_prefs`, in a Room/SQLite database, in a file under `files/`, in the account manager, or in a WebView's storage. Watch the filesystem while you drive the app:# adb shell as root
inotifyd - /data/data/com.target.app/shared_prefs:n
Hint 5 — the inventory looks fine and you have nothing to report
Good. Write that up. A report that says "the application generates a 256-bit AES-GCM key in the trusted execution environment, binds the refresh-token key to a per-use strong biometric authentication, and stores no plaintext copy of either credential" is worth a great deal to a client, and it makes your other findings far more credible. Testers who only ever report breakage give clients no way to distinguish a control that worked from one nobody tested.Solution¶
Answers
**1.** The number is the Keymaster/KeyMint feature version the device declares — it tells you which generation of the HAL is present, and therefore which capabilities exist at all. Checking it first is worth the time because it bounds everything else: if the device declares no `hardware_keystore` feature, no key on it can be hardware-backed regardless of what the app requests. If an app claims StrongBox and the device has no `strongbox_keystore` feature, the app is either falling back silently (Finding 8) or the claim is false. Confirm with `KeyInfo` before writing it up — and confirm that the *device* you were given may not be the device the claim was made about, which is a question for the client rather than a finding. **2.** Either (a) the keys are created lazily and have not been created yet, or (b) the app does not actually use the Keystore. The command that distinguishes them is the builder hook:frida -U -f com.target.app -l ../scripts/hook-keygenparameterspec.js
Success criteria¶
- You recorded the device's release, patch level, build type and Keystore feature version
- You enumerated every alias the app owns, after exercising the app fully
- You produced the inventory table with algorithm, size, purposes, auth binding, timeout, security level and origin for every key
- You can explain why
KeyGenParameterSpecandKeyInfoare different facts - You distinguished per-use binding from a time window, and can state the security consequence of a positive timeout in one sentence
- You demonstrated
getEncoded() == nulland can say precisely what it does and does not prove - You ran the
SecretKeySpechook and can say whether any key bypasses the Keystore entirely - You mapped at least one alias to the specific data it protects
- You searched for a plaintext copy of every protected value
- You checked
sharedUserIdand can explain its Keystore consequence - You obtained the alias list without Frida, from
persistent.sqlite - Your notes include at least one control that worked, phrased as a positive finding