mirror of
https://codeberg.org/dabao1955/apk-reverse.git
synced 2026-11-28 03:50:22 +08:00
Benchmark pass: measure the documented routes, close five tool gaps, correct four claims
Public targets under the existing routes, recorded per row in tests/benchmark.md with each row's own strength label: the MASTG crackmes (equal-length patch -> repack -> re-sign with a zero-change control, and the anti-debug race with its time-to-death baseline), DexPatcher's ezAndroid sample, two real split APK sets, and a self-built extraction-skeleton set. Hardening-shape discrimination now separates Java2C from an extraction shell, a VMP and JNI sinking. New: java2c_probe.py and java2c-and-jni-sinking.md; split-apk.md with repack.py growing set inventory, unified re-signing and merge; protobuf_decode_raw.py cross-checked against the official runtime; vmp_diff_harness.py and its cost table; kernelsu_syscall_mask.py with three kernel-side templates; stalker_trace.js gains Stalker.exclude() and a zero-event warning. Corrections, each because a measurement contradicted a recorded conclusion: the trivial-body ratio is bimodal rather than a threshold and the old threshold claim was deleted; a Dex-VMP verdict was withdrawn after dexdump contradicted a hand-written opcode table; the assumption that Stalker.exclude() fixes zero-event delivery is disproved (it keeps the process alive, nothing more); and a repack.py signing-path claim expired mid-pass when a second writer added one. Emulated and kernel-side routes are labelled unverified where no toolchain or device could run them; each evidence file states what was not run.
This commit is contained in:
1 parent
bea9955eb6
commit
6221a072ca
29 files changed
+10130
-97
No files matched your search
@@ -107,6 +107,13 @@ vocabulary and a set of reassuring numbers.
|
||||
- Packers, custom loaders and code virtualization: identifying them, measuring the validation
|
||||
boundary, and the routes that survive it (`packers.md`,
|
||||
`code-virtualization-and-custom-linkers.md`).
|
||||
- **Java2C** — recognising that the code left the dex entirely, and the evidence that separates it
|
||||
from an extraction shell, a VMP and ordinary JNI sinking (`java2c-and-jni-sinking.md`,
|
||||
`scripts/java2c_probe.py`). Measured: `native` declaration density was **0.03-0.04%** in three real
|
||||
JNI samples against **82.76%** in a Java2C-shaped fixture — roughly 2000x — and the `Java_*` symbol
|
||||
count matched the dex native-method count 1:1 per ABI. **No library produced by a Dex-to-C compiler
|
||||
was built here** (no NDK, no host clang, no device clang, WSL unavailable), so every Java2C-specific
|
||||
criterion is `inferred`; the JNI-sinking side and the discrimination itself are `observed`.
|
||||
- The native layer: `.so` hosts, tamper-triggered self-termination, forged ELF structure, and
|
||||
neutralising a terminate path without freezing the process (`native-and-so.md`,
|
||||
`native-tamper-and-suicide.md`).
|
||||
@@ -299,7 +306,8 @@ unread in this repository.
|
||||
| A `FORTIFY: pthread_mutex_lock called on a destroyed mutex` abort in a Flutter app, on the **main** thread, before the first frame completes | `dart-aot.md` — check `libapp.so` is actually being loaded; Flutter's engine bootstrap is the usual place a native lifecycle fault surfaces |
|
||||
| Log says a **Java-layer** signature/integrity check **passed**, yet the process dies | `code-virtualization-and-custom-linkers.md` §a Java-layer "signature killer" is a decoy |
|
||||
| Deleting a library fixes validation but yields `UnsatisfiedLinkError: dlopen failed: library "X" not found` | `code-virtualization-and-custom-linkers.md` §the deadlock that eats hours |
|
||||
| Whole classes appear as bare `native` declarations with no body | `code-virtualization-and-custom-linkers.md` |
|
||||
| Whole classes appear as bare `native` declarations with no body | `java2c-and-jni-sinking.md` — read it **before** dumping memory: if this is Java2C there is no DEX to find, at any point in the process lifetime. A handful of `native` methods in an otherwise ordinary dex is JNI sinking, not this |
|
||||
| A `Java_*` search over a hardened library returns nothing at all | `java2c-and-jni-sinking.md` §The JNI boundary — why a symbol search fails silently — dynamic registration, or `-fvisibility=hidden`. The check that works is "exports `JNI_OnLoad` and zero `Java_*`" |
|
||||
| A library's **SONAME does not match its filename** | `code-virtualization-and-custom-linkers.md`, `native-and-so.md` |
|
||||
| Your edit had **no effect at all**, with no error | `server-config-and-updates.md` §3 (the value may be server-sent), then `packers.md` §map the validation boundary |
|
||||
| Process **hangs** with no crash record, or dies to a `uid 0` killer | `native-tamper-and-suicide.md` §the rule (you probably made a terminate path *not return*) |
|
||||
@@ -336,6 +344,9 @@ unread in this repository.
|
||||
| The traffic is protobuf/gRPC/QUIC, or a proxy sees TLS but requests still fail on a Flutter app | `references/protocol-reverse.md` — schema-less protobuf, frame capture, and native-side pinning |
|
||||
| Userspace hooks land and the app still dies: the check reads `/proc/self/status` through a raw `svc`, or runs before `JNI_OnLoad` | `references/kernel-and-environment-hardening.md` — what the next layer up and down can actually do, and when to stop |
|
||||
| You must edit, repack, sign or inspect the APK **from the phone itself** | `references/on-device-tooling.md`, `scripts/mt_mcp_probe.py` |
|
||||
| A captured body decodes to nothing readable, or you cannot tell whether a length-delimited field is a string, a nested message or a packed array | `protocol-reverse.md` §1. Protobuf on the wire (measured) — run `scripts/protobuf_decode_raw.py`; the candidate list and its `tie:` lines are the answer, and `docs/tool-verification/EXTENSION-protobuf-raw.md` records exactly what a schema-free decode can and cannot settle |
|
||||
| Method bodies are present but decode as **private opcodes**, and you need the mapping rather than an explanation of why VMP is hard | `vmp-differential-analysis.md`, then `advanced-unpacking.md` for the shape diagnosis |
|
||||
| A store build arrives as `base.apk` + `split_config.*.apk`, or a rebuilt build is refused **as a set** although every file verifies on its own | `split-apk.md` — one keystore across every member for `pm install-multiple`, and check that a merge is legal before trusting a merged single APK |
|
||||
|
||||
## Gates — clear these before you patch, in order
|
||||
|
||||
@@ -556,6 +567,8 @@ Load only what the current step needs.
|
||||
| `references/byte-level-patching.md` | You want to change behaviour by editing a few bytes rather than rebuilding a method — equal-length patches, locating an instruction's exact offset, dex header integrity fields, branch polarity, verifier legality |
|
||||
| `references/packers.md` | The app is packed/hardened, or an edit makes it die before your code runs. Also load before discarding any route as "blocked by the shell" |
|
||||
| `references/code-virtualization-and-custom-linkers.md` | **No packer, dex is readable, and a re-signed build still dies** — whole classes turned into `native` declarations, a private loader with a mismatched SONAME, an embedded self-decrypting payload, or a Java-layer "signature killer" that logs success while a native check kills you. Covers the keep-it/drop-it deadlock and the string-redirect escape |
|
||||
| `references/java2c-and-jni-sinking.md` | **Whole classes are bare `native` declarations and you are about to hunt for a decrypted DEX** — Java2C has none, ever: the code was compiled into a `.so`, so the route is reading that library, not dumping memory. Also the JNI boundary itself — why a `Java_*` symbol search fails silently — and the evidence that separates Java2C from an extraction shell, a VMP and ordinary JNI sinking |
|
||||
| `references/vmp-differential-analysis.md` | Method bodies are present but decode as **private opcodes** — a real Dex VMP: the known-plaintext differential, which links can and cannot be automated, how to *prove* a derived opcode table, smali generation, and when the route is closed |
|
||||
| `references/framework-runtimes.md` | The UI is not native (Flutter / React Native / Unity / Cordova), or Java-layer hooks fire zero times while the UI clearly works |
|
||||
| `references/dart-aot.md` | The logic lives in a Dart AOT snapshot (`libapp.so`): pinning the Dart version, building a matching decompiler, the object pool and reference indexing, register/boolean conventions, locating and patching Dart code |
|
||||
| `references/native-and-so.md` | Patching in a `.so`, needing code to run before the app's own code, hand-built native payloads that crash inside the linker, or **deciding which library/ABI is actually loaded and executing** |
|
||||
@@ -586,6 +599,7 @@ Load only what the current step needs.
|
||||
| `references/protocol-reverse.md` | The traffic is protobuf without a schema, gRPC, or QUIC/HTTP3; or a proxy sees TLS while the app still fails — schema recovery, frame capture, and native-side certificate pinning (Flutter/BoringSSL) with its boundaries |
|
||||
| `references/kernel-and-environment-hardening.md` | Userspace hooking provably cannot reach the check — raw `svc` syscalls, `init_array`-early detection, a ROM that hunts instrumentation: what each layer up and down can still do, the kernel-route map and its version gate, and when escalating is the wrong answer |
|
||||
| `references/on-device-tooling.md` | Working **from the phone itself**: MT Manager edit/repack/sign and its built-in APK MCP, LSPosed Manager, Termux+frida, and on-device data inspection |
|
||||
| `references/split-apk.md` | The target is a **split APK / App Bundle set** (`base.apk` + `split_config.*.apk`), or `pm path <PKG>` returned several files: reading a set off a device, when merging into one APK is legal versus when only unified re-signing works, the install refusal each mistake produces, the manifest attributes a merge breaks, and ABI/density matching |
|
||||
|
||||
## Script index
|
||||
|
||||
@@ -608,7 +622,7 @@ All scripts are parameterized and path-agnostic; pass paths explicitly. Run `--h
|
||||
| `scripts/dart_pool_strings.py` | Recover string literals from a Dart AOT snapshot: framed entries, the one-byte vs UTF-16 split, file offsets, and a run-length noise filter |
|
||||
| `scripts/dart_disasm.py` | Annotated windowed disassembly of Dart AOT code (pool + boolean annotations) plus a B/BL caller index |
|
||||
| `scripts/find_refs.py` | Count and list callers of a method/field (blast-radius check). Takes a smali tree, a `.dex`, a directory of either, or an `.apk`. Always prints what it scanned, so "the input was unreadable" cannot be mistaken for "nothing references it" |
|
||||
| `scripts/repack.py` | Rebuild an APK with replaced dex, strip only signatures, keep `META-INF/services/`, **write a 4-byte-aligned archive** (`resources.arsc` STORED+aligned, which Android R+ refuses to install without), sign, and verify |
|
||||
| `scripts/repack.py` | Rebuild an APK with replaced dex, strip only signatures, keep `META-INF/services/`, **write a 4-byte-aligned archive** (`resources.arsc` STORED+aligned, which Android R+ refuses to install without), sign, and verify. Also handles **split APK / App Bundle sets**: inventory one (`--split-dir`/`--split`), sign every member with a single keystore for `pm install-multiple` (`--split-mode resign`), or fold code/native members into one standalone APK (`--split-mode merge` — refused by design when a member carries its own `resources.arsc`). `--signer auto/jar/apksigner` covers hosts without uber-apk-signer; see `references/split-apk.md` |
|
||||
| `scripts/dexpatch/` | dexlib2 method-level rewriter (for changes that genuinely need new instructions) + build notes |
|
||||
| `scripts/devsh.py` | Quoting-safe ADB shell helper for rooted devices |
|
||||
| `scripts/usb_net_proxy.py` | Give an offline device network over USB (adb reverse + local proxy) |
|
||||
@@ -638,3 +652,7 @@ All scripts are parameterized and path-agnostic; pass paths explicitly. Run `--h
|
||||
| `scripts/stalker_trace.js` | Instruction-level tracing with Frida Stalker: configurable module/offset targets, trigger selection, the event stream, output-size rules, and `transform` customisation |
|
||||
| `scripts/stalker_report.py` | Reduce a `stalker_trace.js` log into block histograms and call sequences, and print an explicit diagnostic for the measured **zero-event** case |
|
||||
| `scripts/mt_mcp_probe.py` | Probe MT Manager's on-device APK MCP (Streamable HTTP, `127.0.0.1:8787/mcp`): JSON-RPC handshake plus grouped `mt_apk_*` tool inventory; prints start-it-by-hand instructions and exits 2 while the service is down |
|
||||
| `scripts/java2c_probe.py` | Collect the evidence that separates **Java2C** from an extraction shell, a VMP and ordinary JNI sinking: per-class `native` density, native-dominated classes and stub ratio from the dex, plus `Java_*` / `JNI_OnLoad` / dynamic-registration / toolchain-string evidence from the `.so`. Every item carries a strong/medium/weak label and the weak ones print their own warning — reading a single criterion is how this gets misdiagnosed |
|
||||
| `scripts/protobuf_decode_raw.py` | **Schema-free protobuf decode**: hex string, binary file or stdin to a JSON tree, with every length-delimited field reported as a candidate set (nested message / UTF-8 / packed array / opaque bytes) and ties labelled rather than guessed. Flags the proto3 explicit-zero and packed-boundary ambiguities; `--max-depth`, `--split`, and `--reencode --check` for a byte-exact round trip. Cross-checked field-for-field against the official runtime |
|
||||
| `scripts/vmp_diff_harness.py` | Differential hardening for Dex-VMP: build a labelled opcode-coverage fixture (**218 of 224 opcodes, measured**), derive a candidate private-opcode map from an original/hardened dex pair with per-entry confidence and a run-level verdict, forge known-table fixtures to verify the comparison in a closed loop, and render a restored stream as a smali skeleton. The upload-to-a-hardening-service link is **not automatable** — see `references/vmp-differential-analysis.md` §2 |
|
||||
| `scripts/kernelsu_syscall_mask.py` | Generate a configurable KernelSU/APatch syscall-masking scaffold: an installable userspace module skeleton plus KPM/LKM/eBPF kernel-side templates, each with its version gate and an explicit unverified label. **A KernelSU userspace module cannot change a syscall return value** — the kernel-side templates are the part that could, and they are shipped unbuilt |
|
||||
@@ -14,11 +14,13 @@ extraction shell and no VMP, so nothing here was exercised end to end against a
|
||||
hardened sample by this skill's own verification pass. The FART/Youpk mechanisms and
|
||||
the Android 12-16 failure analysis are **inferred** from public work (FART/Youpk
|
||||
release notes and a 2026-08 Kanxue thread, `thread-292312`, on why classic
|
||||
active-invocation hooks die on modern ART). The one **measured** element is the
|
||||
detection metric: the trivial-body statistic was exercised by
|
||||
`scripts/dex_dump_validate.py` against a surgical fixture derived from a real hardened
|
||||
sample (see `docs/tool-verification/EXTENSION-unpacking.md`). Label your own results
|
||||
the same way.
|
||||
active-invocation hooks die on modern ART). The detection metric has since been
|
||||
**measured against a purpose-built extraction-shell fixture set**, and that pass
|
||||
invalidated the threshold this file used to state — see the calibration table in
|
||||
§Measuring extraction instead of guessing and the commands behind it in
|
||||
`docs/tool-verification/EXTENSION-extraction-shell-bench.md`. The older surgical fixture
|
||||
that first exercised the script is in `docs/tool-verification/EXTENSION-unpacking.md`.
|
||||
Label your own results the same way.
|
||||
|
||||
Two parts of this file have since been exercised against live hardened targets on a
|
||||
rooted device. The **root-side memory route** (§Dumping when frida is refused) was run
|
||||
@@ -28,7 +30,9 @@ traps and the throughput numbers are recorded in
|
||||
`docs/tool-verification/EXTENSION-rootdump.md`. The `frida-dexdump` refusal that
|
||||
motivates that section is in `docs/tool-verification/EXTENSION-device-run.md`. The
|
||||
FART/active-invocation half of the file is still **inferred** — no extraction-shell
|
||||
target has been spliced end to end here.
|
||||
target has been spliced end to end here, and the two candidate samples shipped in
|
||||
`repos/CyReverse` turned out not to be extraction shells at all (their two "shell"
|
||||
assets differ by six bytes; row B3 in the bench evidence file).
|
||||
|
||||
## The four shapes a dump can be
|
||||
|
||||
@@ -39,9 +43,18 @@ directory and read the `stub%` column — the four shapes separate on it.
|
||||
|---|---|---|---|
|
||||
| Parses, many classes, real bodies | near-baseline | First-generation landing shell; the whole-dex dump **is** the original | Done — filter (`recon.md` §Unpacking a dex-level packer) and proceed to patching |
|
||||
| Parses, many classes, most bodies are `return-void` stubs or nop fills | high | **Extraction shell skeleton** — bodies are decrypted only on invocation | FART loop, this file |
|
||||
| Whole classes appear as bare `native` declarations | n/a | dex2c / JNI sink — the code left the dex entirely | `code-virtualization-and-custom-linkers.md` |
|
||||
| Whole classes appear as bare `native` declarations, and the matching `Java_*` export exists | n/a | **JNI sinking** — the code left the dex for the `.so` | `java2c-and-jni-sinking.md` — read the function in the library, **not** this file |
|
||||
| Method bodies look like a thin forwarder to a native routine, or a single native island holds the app's own logic | n/a | **Java2C / native island** — the body is a compiled `J`/`.so` function, so re-reading the dex finds only the call | `java2c-and-jni-sinking.md` — the dex is the wrong instrument |
|
||||
| Whole classes appear as bare `native` declarations, and **no** `Java_*` export exists | n/a | Dynamic-registration native sink — the binding table is built at runtime | `java2c-and-jni-sinking.md` §the symbol search fails structurally |
|
||||
| Parses, bodies present but instruction streams are nonsense to a dalvik disassembler | baseline | **Real Dex VMP** — private opcodes behind a native interpreter | Last section of this file, and be honest about the cost |
|
||||
|
||||
The two `native`-looking rows are **not** one row. They are separated because the
|
||||
correct next action differs: JNI sinking keeps the logic in a named, exported ARM64
|
||||
function that you read from the library, while a dynamic-registration sink has no
|
||||
symbol to find and needs the binding table read instead. Lumping them together sends an
|
||||
analyst to dump memory for code that is sitting in an ordinary `.so` — the misdiagnosis
|
||||
`java2c-and-jni-sinking.md` was written to stop.
|
||||
|
||||
Two supporting signals worth recording while you are here (the second is measured on
|
||||
this repo's hardened sample): a shipped shell `classes.dex` can be megabytes in size
|
||||
yet define only a handful of classes — the bytes are payload, not code, so class count
|
||||
@@ -69,12 +82,67 @@ measured to differ in ~73% of their bytes), rejects structurally impossible imag
|
||||
(bad magic, `file_size` mismatch, ids out of range), counts classes and stub bodies,
|
||||
and ranks which surviving image is the most likely original.
|
||||
|
||||
Calibration, **inferred** from field reports rather than measured across a population:
|
||||
ordinary app code carries a few percent of genuinely trivial bodies (constructors that
|
||||
do nothing, interface stubs); a ratio in the tens of percent or higher is a skeleton,
|
||||
not code. A ratio that is high *and* the image parses is the extraction-shell
|
||||
signature — a first-generation dump of the same app would show the same classes with
|
||||
real bodies.
|
||||
**Calibration — measured, and it is not a threshold.** Do not look for a `stub%` number
|
||||
to compare against. On a real 982 KB application dex (5,061 bodies) every emptying
|
||||
shape the shell family uses was built into a fixture and measured against a
|
||||
zero-change control (`docs/tool-verification/EXTENSION-extraction-shell-bench.md`,
|
||||
which carries the commands and the full matrix):
|
||||
|
||||
| What the shell left behind | `stub%` | vs. control (1.9 %) |
|
||||
|---|---|---|
|
||||
| bare `return-void` in every body | 100.0 % | sighted |
|
||||
| body cut to `const/4 v0,#0; return v0` | 93.2 % | sighted |
|
||||
| nop fill, no return — **the slot-clearing shape** | 0.0 % | **blind, and scores below the untouched original** |
|
||||
| `new RuntimeException; throw` stub — **the 360/legu shape** | 1.9 % | **blind, indistinguishable from the control** |
|
||||
| 25 / 50 / 75 % of bodies emptied | 1.7 / 1.7 / 2.1 % | blind |
|
||||
|
||||
The distribution is **bimodal**: `stub%` lands either at the app's own baseline
|
||||
(ordinary constructors and interface stubs, measured at 1.9 % here) or at ~100 %.
|
||||
Nothing sits in between, so there is no band a threshold could live in, and **partial
|
||||
extraction is invisible** — a dex with three quarters of its bodies removed reported
|
||||
0.2 points above its untouched control.
|
||||
|
||||
Two consequences the metric's name hides:
|
||||
|
||||
- **`stub%` high is dispositive; `stub%` low is not evidence of anything.** A low
|
||||
reading is compatible with a genuine dex, a fully nop-wiped skeleton, and a
|
||||
`throw`-stubbed skeleton alike.
|
||||
- **Read `emptied%`, not `stub%`, for the skeleton call.** `emptied%` unions the
|
||||
`return*`-stub and the nop-wiped classes, which is what lifts the nop-filled
|
||||
skeleton from 0.0 % to 100 %. A `throw`-stubbed skeleton still reads low; for that
|
||||
shape the ranking cannot help you and only per-body inspection can.
|
||||
|
||||
The trivial-body shapes `dex_dump_validate.py` counts, and which of them are skeleton
|
||||
evidence, are documented in the script itself (`classify_body`); the split between
|
||||
`stub` / `erased` / `minimal` exists because collapsing them produced a ranking that
|
||||
named a fully-wiped image the most likely original.
|
||||
|
||||
### The ranking's failure conditions — read these before trusting its winner
|
||||
|
||||
The `ranking` block names a "best-supported candidate for the original", and it is
|
||||
wrong in two measured situations. Both were reproduced, and both are the *opposite* of
|
||||
the obvious reading:
|
||||
|
||||
- **A partially extracted image outranks a heavily stubbed one.** Ranking on the
|
||||
emptied share alone put a dex with 25 % of its bodies removed *ahead of* its own
|
||||
untouched control — 1.7 % against 1.9 %, because replacing a body with
|
||||
`const/4 + return` lowers the `return-void` count. Measured before it was fixed;
|
||||
the current key uses the count of skeleton-shaped bodies first, which is monotone,
|
||||
and evicts anything over 50 % emptied.
|
||||
- **A `throw`-stub skeleton is invisible and ranks at the control's own baseline.**
|
||||
Every body replaced by `new RuntimeException; throw` measured 1.9 % — identical to
|
||||
the untouched image — and it still sorts third in the fixture set. No body statistic
|
||||
in this kit sees that shape. If the target family is 360/legu-like, **do not use the
|
||||
ranking to pick a baseline**; diff candidate images against each other by body hash
|
||||
instead.
|
||||
|
||||
The reason to state this in the reference file rather than only in the script: the
|
||||
ranking's output is a *sentence with a winner in it*, and a wrong winner is worse than
|
||||
no winner — an agent that adopts a modified image as its baseline builds every
|
||||
subsequent diff on the wrong artifact. When `emptied%` is at the app's own baseline and
|
||||
you cannot name a signal that separates the candidates, treat the ranking as
|
||||
uninformative for that set. Commands and full matrix:
|
||||
`docs/tool-verification/EXTENSION-extraction-shell-bench.md`.
|
||||
|
||||
## The FART loop: skeleton, invocation, splice
|
||||
|
||||
@@ -319,9 +387,46 @@ The last shape is the one to say out loud: if the dump's method bodies are intac
|
||||
decode as private opcodes — rare-opcode density off the charts, unknown opcode slots,
|
||||
and a large native interpreter loop behind them — the code has been virtualized, and
|
||||
nothing in this repository will hand it back to you decompiled. Distinguish it from
|
||||
the JNI sink first (`code-virtualization-and-custom-linkers.md`): there the methods
|
||||
are `native` stubs and the logic lives as native code; here the bodies are present
|
||||
and *re-encoded*.
|
||||
the JNI sink first (`code-virtualization-and-custom-linkers.md`,
|
||||
`java2c-and-jni-sinking.md`): there the methods are `native` stubs and the logic lives
|
||||
as native code; here the bodies are present and *re-encoded*.
|
||||
|
||||
### Establish "the bodies do not decode" with a decoder you did not write
|
||||
|
||||
This is the one measurement in this file that has already been got wrong here, so it
|
||||
gets its own rule. A hand-written opcode-width table silently desynchronises on large
|
||||
dex files — one arm64 branch opcode missing from the table is enough — and the
|
||||
desynchronisation is indistinguishable from the finding it fakes. Measured on the
|
||||
`ezAndroid` sample: a workbench table reported **1,393 of 22,424 bodies as
|
||||
non-decodable** and I read it as private opcodes; the platform decoder returned
|
||||
**943,223 lines, 34,566 instruction decodes, and zero structural errors** on the same
|
||||
file (`docs/tool-verification/EXTENSION-extraction-shell-bench.md` §3.4). The bodies
|
||||
were ordinary dalvik the whole time, and one of the "desync" methods disassembles into
|
||||
five clean instructions under `dexdump`.
|
||||
|
||||
So: **before any VMP verdict, decode the same file with `dexdump`,
|
||||
`baksmali`/`smali`, or `jadx`, and quote the failure count that came from *it*.** A
|
||||
rare-opcode histogram is a lead, not evidence. Never let a private table's desync
|
||||
inform a classification — "my decoder stopped" is a fact about the decoder.
|
||||
|
||||
### What a static dex probe can and cannot decide
|
||||
|
||||
Two different questions live in the same dump, and only the first one is answerable
|
||||
from the bytes on disk:
|
||||
|
||||
| Question | Statically decidable? | Why |
|
||||
|---|---|---|
|
||||
| Are the bodies real dalvik, or an encoded program? | **yes** | the platform decoder decodes them or reports a structural error |
|
||||
| Did the code leave the dex for native? | **yes** | `native` access flags, then `Java_*` exports / `JNI_OnLoad` in the matching `.so` |
|
||||
| Does a native island check signature, root or debugger? | **no** | the logic is ARM64 behind a vtable; the check may exist in no dex at all |
|
||||
| Are unlabelled `assets/` blobs live payload? | **no** | an unreferenced blob is dead until a runtime path constructs its name — measured: six such files in `ezAndroid`'s `assets/`, **zero** dex strings referencing them |
|
||||
|
||||
The trap is the asymmetry. A clean static reading licenses exactly one sentence —
|
||||
*"this is not dex-VMP"* — and it does **not** license *"this is not hardened"*. Those
|
||||
are different claims: the second one is the misdiagnosis that sends an analyst looking
|
||||
for an unpacker when the problem is a native island, or for a private disassembler when
|
||||
the code is ordinary ARM64 in a 206 KB `.so`. A static probe's silence about hardening
|
||||
is silence, not a clean bill of health.
|
||||
|
||||
What recovery looks like when someone does it, **all inferred**, no fixture exists in
|
||||
this repo, treat as a research plan rather than a recipe:
|
||||
@@ -348,10 +453,12 @@ for the island itself.
|
||||
|---|---|---|
|
||||
| Dump parses, stub% near baseline | Filter, verify with jadx, move on to patching | `recon.md` |
|
||||
| stub% high, image parses | FART loop: skeleton + active invocation + splice | this file |
|
||||
| `emptied%` high but `stub%` low | nop-wiped skeleton; the old `stub%`-only reading called it the original | §Measuring extraction instead of guessing |
|
||||
| The ranking names a winner but `emptied%` sits at the app's baseline | Treat the ranking as uninformative for this set; do not adopt that image as a patch baseline | §The ranking's failure conditions |
|
||||
| Splice produces a half-parsing dex | Re-check code_item length (padding, handler list) | §The code_item length trap |
|
||||
| Dumper runs, output dir empty | Storage path + `open()` check, then entry-type probe | §Why the classic hooks die on Android 12-16 |
|
||||
| App dies under the invoker | Shell counter-measures; narrow the force list | §Why the classic hooks die on Android 12-16, and `detection-and-anti-analysis.md` |
|
||||
| frida attach refused (`script has been destroyed`) | Root-side `/proc/<pid>/mem` dump, atomic inside one process lifetime | §Dumping when frida is refused |
|
||||
| Sample self-exits and restarts with no instrumenter present | The packer's own check, not your hook; shorten every step to one lifetime | §Dumping when frida is refused |
|
||||
| Methods are `native` stubs | dex2c / JNI sink | `code-virtualization-and-custom-linkers.md` |
|
||||
| Bodies present, decode as nonsense | Real VMP — differential oracle or walk away | §The honest boundary: real Dex VMP |
|
||||
| Methods are `native` stubs with a `Java_*` export | JNI sinking — read the function in the `.so` | `java2c-and-jni-sinking.md` |
|
||||
| Bodies present, decode as nonsense | Real VMP — **but confirm with `dexdump` first**, then differential oracle or walk away | §Establish "the bodies do not decode" with a decoder you did not write |
|
||||
@@ -47,6 +47,7 @@ Emulation and RPC are for when the code that matters must keep *running*.
|
||||
| Maintenance cost | every missing JNI/syscall is a new stub | reconnect logic, process lifecycle |
|
||||
| Reproducibility | deterministic (you control time/random) | real time, real randomness |
|
||||
| Best for | signing algorithms, crypto, parsers | anything entangled with live app state |
|
||||
| **Priority when either could work** | second — the environment bill below is real and routinely underestimated | **first** — the device already owns the environment |
|
||||
|
||||
The two-strike rule applies across the table: if a library refuses to run under unidbg after two
|
||||
focused rounds of `DalvikVM` patching, stop emulating and move to RPC (or the reverse) instead of a
|
||||
@@ -54,6 +55,33 @@ third round — a library that checks its own loading path or decrypts itself ag
|
||||
may simply not be worth emulating, and that is a finding, not a failure (`SKILL.md`
|
||||
§Stop conditions).
|
||||
|
||||
### The environment bill — budget days, not hours
|
||||
|
||||
"Filling the environment" reads like a checklist and behaves like a project. The mistake this
|
||||
section prevents is starting an emulation because the table says *works against anti-injection*, and
|
||||
then discovering that this library's environment includes everything emulation cannot fake: an
|
||||
Android `Context` backed by a real package manager, Binder round-trips into another process, a
|
||||
`KeyStore` attestation that only succeeds on real hardware, or a self-check against device state
|
||||
captured at install time. For a commercial native algorithm of that shape, "stub it until it runs"
|
||||
is measured in **days, not hours** — each missing piece is discovered one fault at a time, and the
|
||||
rounds do not get shorter.
|
||||
|
||||
Weigh it before starting:
|
||||
|
||||
| Question | If yes |
|
||||
|---|---|
|
||||
| Does it call through `Context` (package name, files dir, signature, `PackageManager`)? | a stub is often enough — cheap, keep going |
|
||||
| Does it talk to another process (Binder service, bound SDK, remote provider)? | the stub surface grows fast; **prefer RPC** |
|
||||
| Does it verify hardware (KeyStore, TEE, an attestation chain)? | emulation is likely a dead end — the value it wants cannot be produced here |
|
||||
| Does it check its own loading environment (paths, maps, root, debugger, installer)? | readable and patchable *inside* the emulator, but budget one round per check |
|
||||
| Must it be called thousands of times a minute? | the RPC throughput ceiling is real — this is the case that justifies the bill |
|
||||
|
||||
Default to **RPC first, emulation second**: the device already provides the environment, and
|
||||
per-call cost only starts to matter once you need volume. The two claims in this subsection
|
||||
(the day-scale cost of a Context/Binder/KeyStore-shaped library, and the RPC throughput ceiling)
|
||||
are **inferred** from the mechanism and from community practice — this repository has not emulated a
|
||||
commercial sample end to end, as `EXTENSION-emulation-rpc.md` states.
|
||||
|
||||
## Part A — Unidbg
|
||||
|
||||
### What it is
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
# Java2C and JNI Sinking — the dex is not where the code is
|
||||
|
||||
Load this when the dex shows method bodies missing or replaced by `native` declarations, and
|
||||
you are about to go looking for a decrypted DEX in memory. **Decide which of the two shapes you
|
||||
have before you spend anything**, because one of them has no DEX to find at any point in the
|
||||
process lifetime, and the search for it is unbounded.
|
||||
|
||||
Two neighbouring routes have historically folded this shape into one: `advanced-unpacking.md`'s
|
||||
dump-shape table and the SKILL.md symptom index both point "whole classes are bare `native`
|
||||
declarations" at `code-virtualization-and-custom-linkers.md`. That file is about protection which
|
||||
leaves the code **reachable** — a private container, a loader, a private opcode interpreter, all of
|
||||
which you can dump and measure. **Java2C is not reachable, because it was never bytecode.** This
|
||||
file splits that row and owns both halves of it; the virtualization file stays what it is.
|
||||
|
||||
**Strength note, read this first.** The measurements below are **observed** — every number was
|
||||
produced by `scripts/java2c_probe.py` against a real sample during this pass, recorded in
|
||||
`docs/tool-verification/EXTENSION-java2c.md`. But no Java2C library exists in this repository and
|
||||
none could be built here (no NDK, no clang, WSL unavailable — see that record), so **the
|
||||
Java2C-specific identification criteria are inferred, not observed end to end**. What *is*
|
||||
observed is the discriminating measurement that separates Java2C from JNI sinking, and the JNI
|
||||
boundary behaviour. Treat the type table as a map; label your own conclusions the same way.
|
||||
|
||||
## The five shapes — one table, because the wrong row costs days
|
||||
|
||||
| Shape | Static shape in dex | What is in memory at runtime | Route | Cost of the wrong row |
|
||||
|---|---|---|---|---|
|
||||
| **Landing shell** | `Application` is a third-party class; dex is a small stub | A full, decrypted DEX | Dump memory, filter, patch | Cheap if you notice the stub `Application` |
|
||||
| **Extraction shell** | Reads as a normal dex until you measure it; bodies are stubs | A DEX whose bodies are filled in **on invocation** | Dump, measure `stub%`, FART-style active invocation | Medium — you keep dumping, and every dump is a skeleton |
|
||||
| **VMP** | Dex parses; bodies are *present* but decode as nonsense | A DEX containing **private opcodes** interpreted by a native VM | Measure, then usually stop and report | Medium — recovery cost usually exceeds task value |
|
||||
| **Java2C** | Whole classes are `native`; **no code_item at all** for them | **Never a DEX.** The translated methods exist only as compiled C in a `.so` | Read the `.so`; rebuild the JNI call chain | **Highest in this table** — you hunt a decrypted DEX that is never produced, then blame your dump tooling, then your anti-detection assumptions |
|
||||
| **JNI sinking** | A handful of `native` declarations; the rest of the dex is ordinary Java | A normal DEX | Locate the Java call site; reverse one or a few native functions | Low — but you may widen the job to the whole app if you read it as Java2C |
|
||||
|
||||
The one question that decides the table: **does the artifact ever hold dex bytecode for the
|
||||
methods you care about?**
|
||||
|
||||
- Landing shell, extraction shell, VMP: **yes** — the bytecode is there, in some state, and the
|
||||
work is recovering or interpreting it.
|
||||
- Java2C: **no** — a translated method is a `native` declaration plus an entry in a registration
|
||||
table. Nothing is decrypted, so nothing can be dumped. `[inferred]` as a runtime claim: it
|
||||
follows from the translation mechanism, and this pass did not observe a live Java2C app.
|
||||
- JNI sinking: **yes** — the dex was never damaged.
|
||||
|
||||
## Identification
|
||||
|
||||
### The discriminating measurement is native density
|
||||
|
||||
This is the single most useful number and it separates the two shapes by roughly three orders of
|
||||
magnitude. Measured on real samples with `scripts/java2c_probe.py` `[observed]`:
|
||||
|
||||
| Sample | dex methods | `native` methods | native ratio |
|
||||
|---|---|---|---|
|
||||
| JNI-sinking target A (MASTG L2) | 5081 | 2 | **0.04 %** |
|
||||
| JNI-sinking target B (MASTG L3) | 11182 | 3 | **0.03 %** |
|
||||
| App carrying a real JNI library | 23603 | 7 | **0.03 %** |
|
||||
| Java2C-shaped fixture | 29 | 24 | **82.76 %** |
|
||||
|
||||
A JNI *sink* is by definition surgical: a few hot methods are moved out and the rest of the app
|
||||
stays in Java. **A JNI sink is not "the app is native".** When the density is in the tens of
|
||||
percent, and whole classes have zero Java methods left, you are looking at a translation pass.
|
||||
|
||||
`[observed]` in the same runs: two of the fixture's three classes were native-dominated (>= 70 %
|
||||
of >= 3 named methods), against **zero** such classes in any of the three real targets. Note that
|
||||
constructors are excluded from that test — a translation pass leaves `<init>`/`<clinit>` in Java
|
||||
in practice, so "every method native" is an essentially unreachable test and should not be used.
|
||||
|
||||
### Native-layer signatures
|
||||
|
||||
| Evidence | Strength | Why |
|
||||
|---|---|---|
|
||||
| `.so` exports `Java_*` symbols whose count is roughly 1:1 with the dex native-method count | **strong** | Measured 2:2 (sample A) and 3:3 (sample B) `[observed]`. It is the signature of a *static-linkage* translation or sink. Note it does **not** distinguish Java2C from JNI sinking — only the density does |
|
||||
| The `.so` contains one C function per Java method, each beginning `JNIEnv *env, jobject thiz, ...` | **strong** | Observed in generated, uncompiled output: every translated method became exactly one function with those leading parameters |
|
||||
| Toolchain strings: `Dex2C`, `dynamic_register_compile_methods`, `ScopedLocalRef`, `well_known_classes` | **strong** | These are the runtime header and entry-point names a Dex-to-C toolchain emits. `[observed]` in generated sources; `[inferred]` that they survive compilation into the shipped `.so` |
|
||||
| `JNI_OnLoad` present | **weak** | Present in nearly every JNI library. Measured true on a system library with zero `Java_*` symbols `[observed]` — it told us registration was dynamic, not that anything was hardened |
|
||||
| `RegisterNatives` **string** anywhere in the `.so` | **weak** | See the boundary section: NDK's C++ headers do not emit this symbol at all |
|
||||
| `libc++_shared.so` / `c++_static` | **weak** | An ordinary NDK C++ setting. The Dex-to-C toolchain read here does ship `APP_STL := c++_static`, but so do countless unrelated libraries `[observed]` |
|
||||
| `classes.dex` byte-count per class far below the app's own pre-hardening build | **weak, uncalibrated** | Bodies leaving the dex should shrink it. Measured 584 B/class on the fixture versus 1152–1386 B/class on the real Java targets `[observed]` — but there is no population baseline for "normal", so this only compares an app against **itself** before and after |
|
||||
|
||||
`java2c_probe.py` prints every one of these with its strength attached, and prints the weak ones
|
||||
as an explicit warning block. A weak hit is not a verdict and must not be reported as one.
|
||||
|
||||
## The route: read the .so, not memory
|
||||
|
||||
For Java2C the analyst's instinct — dump memory and find the dex — is not merely expensive, it is
|
||||
**unbounded**, because the success condition can never occur.
|
||||
|
||||
1. **Do not dump.** There is no target artifact. If you dump anyway, you will recover a dex that
|
||||
still shows the same `native` declarations you started with, and the natural (wrong) reading is
|
||||
"the dump failed".
|
||||
2. **List the translated functions.** With static linkage, `Java_<PKG>_<Class>_<method>__<proto>`
|
||||
names map the `.so` back onto the dex one for one. Use the dex as the index and the `.so` as
|
||||
the library.
|
||||
3. **Rebuild the chain backwards.** A translated function calls *out* to Java constantly. The
|
||||
strings `FindClass` / `GetMethodID` / `GetStaticMethodID` / `Call*Method` / `NewObject` /
|
||||
`GetFieldID` inside it are the reverse edges. Following them reconstructs what the original
|
||||
Java method did, which is the only way the logic comes back.
|
||||
4. **Expect a register-machine shape.** Generated code declares a slot per dex register up front
|
||||
and assigns them in sequence. Reading it as if a human wrote it wastes hours; read it as a
|
||||
linearizer output, matching each call site against the JNI signature it passes.
|
||||
|
||||
The decompiler choice matters less here than for obfuscated code: the generated functions are
|
||||
long but flat and unoptimized, which is precisely the shape a decompiler handles well.
|
||||
|
||||
## The JNI boundary — why a symbol search fails silently
|
||||
|
||||
Two independent mechanisms erase the symbol that a reader would search for. Both are real; the
|
||||
second was measured this pass.
|
||||
|
||||
**1. Dynamic registration.** Nothing named `Java_*` is exported. Binding is performed at runtime
|
||||
by a registration table built in a JNI entry point. A grep over the symbol table returns nothing,
|
||||
exits zero, and looks like a legitimate answer.
|
||||
|
||||
**2. `-fvisibility=hidden`, and an inline `RegisterNatives`.** The Dex-to-C toolchain read here
|
||||
ships `APP_CPPFLAGS += -fvisibility=hidden` in its `Application.mk` `[observed]`, which hides the
|
||||
generated functions from the dynamic symbol table — so even a statically-named translation can
|
||||
present as "no `Java_*` symbols". Compounding it: NDK's C++ `jni.h` implements `RegisterNatives`
|
||||
as an **inline member that calls through the function table**, so calling it emits *no*
|
||||
`RegisterNatives` symbol either `[observed]`: a real system library exporting `JNI_OnLoad` and
|
||||
zero `Java_*` symbols contains zero literal `RegisterNatives` symbols. A rule that greps for that
|
||||
name is structurally incapable of firing.
|
||||
|
||||
**The check that does work** `[observed]`: a library that **exports a JNI entry point and exports
|
||||
zero `Java_*` symbols** is registering dynamically. That combination is affirmative evidence, and
|
||||
it fired on a real library in this pass.
|
||||
|
||||
**Confirming it at runtime.** Hook the registration entry point (`JNI_OnLoad`) and read the table
|
||||
it builds, or hook the class-load path and enumerate the class's methods' native backing. Do not
|
||||
conclude "no JNI boundary" from an empty symbol table; conclude "the binding is not visible in the
|
||||
symbol table" and go find where it happens.
|
||||
|
||||
## Failure modes
|
||||
|
||||
| What you see | What it is not | What it is |
|
||||
|---|---|---|
|
||||
| Whole classes are `native`, you dump memory and find the same `native` declarations | "The dump failed" / "the tool is broken" | The translation moved the code out of dex permanently. Re-read the dex shape and open the `.so` |
|
||||
| `grep Java_ .dynsym` returns nothing | "There is no native implementation" | Dynamic registration, or hidden visibility — see the boundary section |
|
||||
| A library exports `JNI_OnLoad` and nothing else you recognise | "It is a loader" | It may be an ordinary dynamically-registering JNI library. A loader normally also has an `init_array` entry and a payload to map |
|
||||
| Native density is high but every class still has Java methods | Java2C | Could be a JNI sink applied widely, or an R8-stripped app. Check whether the `native` methods have matching `Java_*` symbols before concluding |
|
||||
| The dex looks completely unremarkable on a sample you were told is hardened | "Nothing is hardened" | A VMP whose payload is decrypted at runtime has an ordinary static dex. Measured this pass: a VMP-labelled sample had 7 native methods out of 23603 and a 4 % stub ratio `[observed]`. **Static dex metrics cannot see runtime-only hardening** — that needs a runtime check, not more reading |
|
||||
|
||||
## Where this file stops
|
||||
|
||||
- **It does not decompile the generated C.** That is ordinary native reversing; `native-and-so.md`
|
||||
and `native-dbi-and-deobfuscation.md` own it.
|
||||
- **It does not recover a VMP.** Private-opcode interpretation is a different mechanism with a
|
||||
different cost model (`advanced-unpacking.md`).
|
||||
- **The Java2C runtime claim is inferred.** That no dex bytecode exists in memory for a translated
|
||||
method follows from the mechanism, not from a measurement made here. If you get a live Java2C
|
||||
sample, that is the first thing to verify, and it is cheap: attach, force the class to load, and
|
||||
ask whether the method resolves to Java bytecode or to a registered native entry.
|
||||
@@ -19,6 +19,13 @@ externally documented mechanisms with sources, labelled `inferred` — none of i
|
||||
That is deliberate: the purpose is to stop you from spending a day rediscovering the version
|
||||
gate (§4) or the conflict (§5), not to teach kernel hacking.
|
||||
|
||||
**Extension pass, read the next paragraph before relying on it.** This file now has a companion
|
||||
generator — `scripts/kernelsu_syscall_mask.py` — which emits a loadable userspace module skeleton
|
||||
plus kernel-side **templates** for the three routes in §4. The generator, its userspace output and
|
||||
its own consistency check are `measured`; **the kernel-side code has never been compiled or loaded
|
||||
anywhere**, and every generated kernel file says so in its own header. A template is a starting
|
||||
point that removes the blank page, not a weapon that has been fired.
|
||||
|
||||
## 1. The attack-surface timeline — why "just hook it" fails
|
||||
|
||||
An app process initializes in a fixed order, and every anti-analysis check you will meet lives at
|
||||
@@ -147,6 +154,83 @@ the process earlier than any userspace tool can be, needs no ptrace (so it does
|
||||
rewrites one `/proc` read is a research project; a Zygisk module that hooks the target's
|
||||
constructor is a build task.
|
||||
|
||||
### 4a. From "no weapon" to "template + gate": what the extension pass added
|
||||
|
||||
The gap this closes is specific. §4 above was a map with no artefact behind it: a reader told
|
||||
"Kernel module hooking — inline hooks and syscall-table hooks in kernel space — the layer that *can*
|
||||
rewrite what a `/proc` read returns" had nowhere to go next. `scripts/kernelsu_syscall_mask.py`
|
||||
produces the artifact; this subsection states its boundary, because the boundary is the part that
|
||||
matters.
|
||||
|
||||
**The correction worth internalising first: a KernelSU module cannot do any of this.** A KernelSU
|
||||
(or Magisk, or APatch-userspace) module is a *userspace* module whose scripts run as root in the
|
||||
normal world. `module.prop`, `post-fs-data.sh` and `service.sh` cannot change what `openat`
|
||||
returns — not because of a version gate, but because nothing in that format is ever in the kernel's
|
||||
return path. Detectors of the kind §2 and §3 discuss are not defeated by a module of that shape.
|
||||
The generator emits that skeleton anyway, because it is the right carrier for the configuration and
|
||||
the metadata, and it says in its own README what the skeleton cannot do.
|
||||
|
||||
What *can* reach the return path is one of three artifacts, and each carries a gate that has to be
|
||||
checked on the actual device:
|
||||
|
||||
| Artifact | What it is | Gate | Status of the shipped template |
|
||||
|---|---|---|---|
|
||||
| **KPM** (KernelPatch / APatch) | A relocatable `.kpm` loaded by kpimg injected into the kernel image; replaces syscall-table pointers (`fp_hook_syscalln`) or rewrites prologues (`hook_wrapN`) | A KernelPatch-patched boot image, plus a **bare-metal** ARM64 toolchain (`aarch64-none-elf-gcc`) — not the NDK | `unverified` — template only, never compiled |
|
||||
| **Out-of-tree LKM** | An ordinary kernel module that reaches `sys_call_table` and swaps a pointer | Kernel source matching the device's exact vermagic, and on ≥5.7 a way around `kallsyms_lookup_name` no longer being exported | `unverified` — template only; rated last of the three on cost |
|
||||
| **eBPF probe** | A BPF program on a syscall tracepoint or kprobe | GKI 5.10+ with the tracing/BTF machinery | `unverified` — template only; the version gate alone closes it on most older devices |
|
||||
|
||||
The eBPF row carries a capability ceiling that is easy to miss and is stated in the generated file
|
||||
as well: **a tracepoint can observe a syscall, not rewrite its result.** `bpf_override_return()`
|
||||
only applies to functions flagged `ALLOW_ERROR_INJECTION`, which raw syscall entries are not —
|
||||
so "eBPF to spoof a `/proc` read" is, on most kernels, an observation plan wearing a rewrite
|
||||
plan's clothes. Observation is genuinely useful (it tells you which syscall the check uses, which
|
||||
`§5` requires you to know anyway); it is not spoofing.
|
||||
|
||||
**Field notes on the KPM route, from a public KPM development write-up** (`inferred` here — the
|
||||
author's measurements, not this repository's; source: blackr0ck, *APatch KPM 开发*,
|
||||
[bbs.kanxue.com/thread-291665.htm](https://bbs.kanxue.com/thread-291665.htm), 2026-06). These are
|
||||
recorded because each one is a day of somebody's time:
|
||||
|
||||
- **An inline hook can install cleanly and never fire.** On a GKI kernel with LTO, the exported
|
||||
symbol is frequently not the call site — the callee was inlined into its only caller, so
|
||||
replacing instructions at the symbol's address intercepts nothing. The reported symptom is
|
||||
"module loaded, callback never ran". Pointer replacement in the syscall table does not have this
|
||||
failure mode, because the syscall entry path must go through the table.
|
||||
- **The instruction-sequence length in a module header is a trap of its own.** The reported cause
|
||||
of a hard-to-diagnose load failure: declaring a resolved kernel function with `extern` makes the
|
||||
compiler emit an undefined reference (`*UND*`) instead of allocating the slot, and the loader
|
||||
refuses the module with `unknown symbol`. Letting it be a tentative definition allocates `.bss`
|
||||
and the loader fills it in. The generated template states this rule in the code, and
|
||||
`verify` checks for it.
|
||||
- **Per-syscall callbacks run on every syscall of that number, system-wide.** The reported
|
||||
incident: a `write` hook that did not first reject `fd <= 2` intercepted the framework's own log
|
||||
output tens of thousands of times and the device rebooted. **Performance is correctness here** —
|
||||
the first statement in every callback is a filter that rejects the common case.
|
||||
- **Which hook point survives is kernel-specific, and the cheap way to find out is to start at the
|
||||
syscall layer.** The same write-up reports that modifying the SELinux internal function they
|
||||
first targeted crashed the kernel under every combination tried (before/after, argument,
|
||||
return value, skip-origin), while a syscall-table hook on `write` proved stable and sufficient.
|
||||
Treat the syscall layer as the default and move inward only with evidence.
|
||||
|
||||
**Measured on the reference device, and why every template here is labelled the way it is.**
|
||||
`<DEVICE>` runs kernel **4.14.186+** (`adb shell 'uname -r; cat /proc/version'` →
|
||||
`4.14.186+`, `Linux version 4.14.186+ (nobody@android-build) (Android (6443078 based on r383902)
|
||||
clang version 11.0.1 ...) #1 SMP PREEMPT Wed Mar 30 23:32:42 CST 2022`). On that device the two
|
||||
remaining gates answer themselves:
|
||||
|
||||
- **eBPF**: 4.14 is far below the 5.10 gate. Closed, as §4 already recorded.
|
||||
- **KPM**: no KernelPatch-patched image is present, and the host has no
|
||||
`aarch64-none-elf-gcc` (`kernelsu_syscall_mask.py gates` reports `NOT FOUND`, along with no
|
||||
`ndk-build` and no `make`). Installing APatch means patching the boot image — a bricking risk
|
||||
that this repository does not take on an APK task.
|
||||
- **LKM**: no kernel source for the device, so no matching vermagic is possible.
|
||||
|
||||
So on this machine the honest answer to "do you have a kernel-level weapon" is: **you have a
|
||||
template, a gate table and a measured statement that the gate is closed here.** That is a route
|
||||
decision, not a failure — and it is the same shape of answer §6 gives for the whole escalation
|
||||
ladder. Do not let the existence of the template change what you claim about it: `unverified` is
|
||||
still `unverified`.
|
||||
|
||||
## 5. The syscall-level realities that survive every hook
|
||||
|
||||
Two facts decide a lot of "unexplainable" behaviour, and both belong to the kernel's design
|
||||
@@ -178,7 +262,7 @@ honest outcome (`observed`, from `tools/_phone-modules/README-*.md`):
|
||||
| LSPosed (Zygisk) module hooking | available — framework activation verified by log (`welcome to LSPosed!` lines, `lspd` daemon process) |
|
||||
| MT Manager on-device editing/repack/sign | available (`on-device-tooling.md`) |
|
||||
| eBPF tracing | closed (kernel gate) |
|
||||
| KPM / kernel module route | closed (no kernel source/build for the device; out of scope regardless) |
|
||||
| KPM / kernel module route | closed on this device — no KernelPatch image, no kernel source, no bare-metal ARM64 toolchain; the **template and gate table** are shipped (§4a) |
|
||||
| Frida with disguised server | available (a renamed server binary was present on the device from prior work) |
|
||||
|
||||
The generalisable rule: **enumerate the ladder for *your* device once, write the one-line
|
||||
@@ -198,6 +282,8 @@ defeated with a custom kernel module is not a deliverable anyone can install.
|
||||
| Check uses raw syscalls | Static patch of the check; or LSPosed/Zygisk module hooking the consuming code | "Hook libc open/read" (does nothing) |
|
||||
| Check detects the *environment* (root/emulator), not your patch | `detection-and-anti-analysis.md` A/B/C — usually a different device or route | Kernel work |
|
||||
| You are about to write a kernel module / patch a kernel | Stop. State what is blocked and the evidence; propose the static/module route | Kernel development (out of scope for this skill) |
|
||||
| You have the kernel template and want to load it on a 4.14/Magisk device | Read `§4a`'s gate table, confirm the gate is closed, and say so | Patching a boot image to obtain a kernel route, on an APK task |
|
||||
| A kernel-side template exists, so it feels like a weapon | It is `unverified` until a device with the matching gate compiles and loads it | Presenting a template as a working capability |
|
||||
| Escalation effort exceeds the user's actual ask | `detection-and-anti-analysis.md` stop signal — switch to static | One more layer |
|
||||
|
||||
## Checklist
|
||||
@@ -207,5 +293,8 @@ defeated with a custom kernel module is not a deliverable anyone can install.
|
||||
- [ ] Root scheme's artefacts enumerated (manager/daemon/mounts) before blaming the target
|
||||
- [ ] Hiding module: exactly one of Shamiko / Zygisk-Assistant; denylist configured to its spec
|
||||
- [ ] Kernel rung checked against `uname -r` — one line recorded if closed
|
||||
- [ ] Kernel-side template treated as `unverified` until a device with the matching gate
|
||||
actually compiles and loads it (`§4a`); the userspace module skeleton is a carrier for
|
||||
configuration, not a kernel capability
|
||||
- [ ] Raw-syscall vs libc-call distinction verified by disassembly before building any spoof
|
||||
- [ ] No kernel development undertaken as part of an APK deliverable
|
||||
@@ -182,6 +182,53 @@ across runs only for code that is mapped from file; do not compare a `libart`-ma
|
||||
address between two runs; and never place a follow/unfollow pair on a hot function (measured failure
|
||||
in §6).
|
||||
|
||||
### The cost is not linear, and on arm64 it decides the tool choice
|
||||
|
||||
Two costs decide whether Stalker is the right tool, and neither is visible in the API:
|
||||
|
||||
- **Translation multiplier.** A followed thread pays a large constant factor on every translated
|
||||
block. Community reporting for arm64 puts it at **20-50x**; the mechanism is not controversial
|
||||
(every block is copied into the DBI's code cache and routed through a dispatcher), while the exact
|
||||
number is workload-dependent. This one is `inferred` — this repository has not measured the
|
||||
multiplier itself, only its consequences below.
|
||||
- **What you let into the trace.** Every block of every library the thread touches is a candidate.
|
||||
Following into `libc` / `libart` / `libhwui` is how a four-second window becomes a device-wide
|
||||
event: measured here, one such run took the test phone's `load average` to `34.11` on 8 cores and
|
||||
destroyed a co-running job's attach (§6).
|
||||
|
||||
`Stalker.exclude()` is the control for the second cost, and it is not optional.
|
||||
`scripts/stalker_trace.js` applies it before `follow()` from its `excludeModules` list and reports
|
||||
what it actually excluded on one `EXCL` line — read that line before believing any trace, because a
|
||||
module that was not loaded at follow time was **not** excluded. Exclude every system library you are
|
||||
not studying; the target module is never excluded even if its name appears in the list.
|
||||
|
||||
Three failure shapes follow from ignoring this: deadlock, a watchdog `SIGABRT`, and the zero-event
|
||||
trace. The deadlock and the watchdog shape remain community reports (`inferred`); the other two were
|
||||
measured, **and the measurement splits the claim in two** — the split matters more than the summary:
|
||||
|
||||
| Arm — one device, one package, one module, 6 s follow | Outcome |
|
||||
|---|---|
|
||||
| attach + resume, **no follow** | process survives |
|
||||
| follow, `excludeModules: []` | **process dies**, script destroyed |
|
||||
| follow, 20 system modules excluded | process survives, **but `blocks=0 blk=0 calls=0`** |
|
||||
|
||||
Exclusion is what keeps the target alive — now `observed`, with the no-follow arm ruling out "it
|
||||
would have died under frida anyway". But exclusion does **not** restore event delivery: the
|
||||
zero-event trace survived the treatment arm unchanged. Treat those as two problems, and do not offer
|
||||
`exclude` as the fix for a follow that delivers nothing. Commands and outputs:
|
||||
`docs/tool-verification/EXTENSION-stalker-exclude.md`.
|
||||
|
||||
**arm64 also raises the floor.** PAC/BTI-bearing code gives a translator more ways to mis-handle a
|
||||
block than armv7 did, which is one more reason a trace that works on an emulator is not evidence
|
||||
about a device. Treat any arm64 result as needing a control run (a followed thread executing a known
|
||||
loop, nonzero `BLK` lines) before it is interpreted.
|
||||
|
||||
**Emulation is a legitimate alternative, not a consolation prize.** When the target is a pure
|
||||
computation inside a `.so` and its environment can be faked, a Unidbg/Unicorn trace can be both
|
||||
faster and more stable than Stalker on a real arm64 device: no device load, no watchdog, no PAC, and
|
||||
a deterministic replay. That inversion of the usual intuition is half the reason
|
||||
`emulation-and-rpc.md` exists — decide with its decision table rather than by habit.
|
||||
|
||||
## 5. From a trace to a deobfuscated function (inferred)
|
||||
|
||||
This is the part the community write-up above also describes, and it is **inferred** here — the
|
||||
|
||||
@@ -55,9 +55,13 @@ Three consequences that decide how you interpret any decoded body:
|
||||
2. **A packed repeated field is invisible without a schema.** Field 4 above is three varints
|
||||
(`3, 270, 86942`) packed into one length-delimited value. A schema-free decode can only report it
|
||||
as opaque bytes — the length is known, the element boundaries are not.
|
||||
3. **An explicit zero is byte-identical to an absent field** in proto3 (`28 00` versus no bytes at
|
||||
all). A decoded `0` is not evidence that the sender set the field, and "the server sent 0" is not
|
||||
a safe conclusion from a decode alone.
|
||||
3. **A decoded `0` and a field that was never set are two different questions.** An implicit
|
||||
(no-presence) proto3 field whose value is the default writes **nothing at all**, so `field = 0`
|
||||
and "never touched" produce identical bytes; a field **with** presence (`proto2 optional`,
|
||||
`proto3 optional`) set to `0` does put its tag and a zero byte on the wire. Both halves were
|
||||
measured against the official runtime; a schema-free decoder can only flag the case, never
|
||||
resolve it. So a decoded `0` was emitted on purpose by some writer, and a field *missing* from a
|
||||
decode is not evidence that 0 was the value in use.
|
||||
|
||||
### Decoding without a schema
|
||||
|
||||
@@ -77,6 +81,63 @@ Three consequences that decide how you interpret any decoded body:
|
||||
covers reading these, and `datastore_inject.py` can re-encode an edited DataStore value once you
|
||||
know the message.
|
||||
|
||||
### The decoder that ships here (measured)
|
||||
|
||||
`scripts/protobuf_decode_raw.py` walks a payload with no schema and prints a tree. Input is a hex
|
||||
string, a binary file, or stdin (hex text and raw bytes are told apart automatically); `0x` prefixes,
|
||||
spaces, commas, newlines and `\xNN` escapes are all accepted, and `|` splits independent frames.
|
||||
`--json` emits the same tree as data, `--max-depth` bounds the nesting, and `--reencode` writes a
|
||||
decoded (possibly hand-edited) tree back to bytes and can diff the result against the original.
|
||||
|
||||
```
|
||||
$ python skills/apk-reverse/scripts/protobuf_decode_raw.py --hex "08 96 01 12 07 74657374696e67"
|
||||
frame 0 bytes 0..12 (12 B) status=end_of_buffer
|
||||
f1 varint 150
|
||||
f2 len-delimited(7) view=utf8_string
|
||||
candidate utf8_string 0.50 valid UTF-8 with no control characters
|
||||
candidate packed_varint 0.50 the payload is exactly a sequence of 7 varint(s) ...
|
||||
candidate bytes 0.20 opaque bytes: always consistent with the payload ...
|
||||
tie: packed_varint and utf8_string are equally consistent with these bytes ...
|
||||
string: 'testing'
|
||||
```
|
||||
|
||||
The point of the tool is the candidate list. Fed a nested message that `google.protobuf` 6.33.6
|
||||
itself produced (inner payload `0801120178`), it answers:
|
||||
|
||||
```
|
||||
nested_message 0.50 | packed_varint 0.50 | utf8_string 0.35 | bytes 0.20
|
||||
tie: nested_message and packed_varint are equally consistent ... the view is a display default
|
||||
```
|
||||
|
||||
Both readings really are legal protobuf for those six bytes, and no heuristic closes the gap. The
|
||||
confidence numbers are a documented ordering, not probabilities, and the `view` exists only so the
|
||||
tree can be expanded and re-encoded -- it is not a claim about what the field is.
|
||||
|
||||
**Round trip, which is the strongest evidence available without a schema.** Decode, re-encode, and
|
||||
compare bytes: the message serialized by the official runtime re-encodes byte-identically to
|
||||
`SerializeToString()`; a real AndroidX DataStore file (81 B, written by `scripts/datastore_inject.py`)
|
||||
round-trips byte-identically; and editing one value in the JSON tree and re-encoding produced a file
|
||||
that the *other* tool read back as the new value. Two of the built-in fixture families are exceptions
|
||||
by design and are reported as such rather than hidden: a non-canonical varint re-encodes to the
|
||||
minimal form, and a `--split varint-length` run compares frame bodies only, since the length prefixes
|
||||
are framing and not message data.
|
||||
|
||||
**What a schema-free decode cannot conclude -- write none of these down:**
|
||||
|
||||
- that a length-delimited field *is* a string, a submessage or a packed array, rather than that it
|
||||
could be any of them;
|
||||
- the element boundaries of a packed array, or its element type;
|
||||
- that a field's value was 0 because it is absent from the decode, or that a sender "set" a field
|
||||
because a 0 appears (see the two traps above);
|
||||
- which message a field number belongs to, or that the same field number in two payloads means the
|
||||
same thing;
|
||||
- that a payload which parses cleanly is a real message at all -- §6 has that failure mode, and the
|
||||
fixes are a second sample and a round trip, not a longer look at the first one.
|
||||
|
||||
The measured record for this tool -- every command, both input forms, the round trip, and the
|
||||
framing mistake it does *not* protect you from -- is in
|
||||
`docs/tool-verification/EXTENSION-protobuf-raw.md`.
|
||||
|
||||
## 2. Recovering the schema from the APK
|
||||
|
||||
### What generated protobuf code looks like
|
||||
@@ -253,6 +314,13 @@ their parents, and (c) when you have a schema, re-encoding the decoded structure
|
||||
with the input — the same round-trip check used in §1, which caught a decoder bug in this
|
||||
repository's own fixture.
|
||||
|
||||
**A framing choice that is wrong without being refused.** `--split varint-length` means protobuf's own
|
||||
`writeDelimitedTo` framing: a varint length, then the message. gRPC frames a message differently — one
|
||||
compression-flag byte, then a **four-byte big-endian** length (§3). Handed a real gRPC frame, the
|
||||
varint splitter reported **five** frames rather than failing, while the same bytes decoded with no
|
||||
splitting reported `stray_end_group`. Strip the 5-byte gRPC prefix yourself, and check the frame count
|
||||
either way: a wrong split still produces a decode, which is what makes it expensive.
|
||||
|
||||
**Assuming the proxy sees everything.** The proxy sees what the client sends through it. An app that
|
||||
uses QUIC (§4), a raw socket, a native client bypassing Java's HTTP stack, or a certificate-pinned
|
||||
connection shows up as silence. Silence is evidence about the transport, not about the app: check
|
||||
|
||||
@@ -0,0 +1,322 @@
|
||||
# Split APKs and App Bundles
|
||||
|
||||
A store build is frequently **not one file**. Play and most OEM stores ship App Bundles, and the
|
||||
device receives a set: `base.apk` plus one `split_config.*.apk` per ABI, screen density and language
|
||||
(and, for a dynamic-feature bundle, the feature modules as well). Every member carries **the same
|
||||
package name and its own signature**, and the package manager treats the set as one package.
|
||||
|
||||
Two failures follow from treating such a set as an APK, and both look like a broken build when they
|
||||
are nothing of the kind:
|
||||
|
||||
- Signing only the base leaves the set disagreeing about its certificate; the installer rejects the
|
||||
whole set and names the package, not the member you forgot.
|
||||
- "Just merge them into one APK" is free *only* for members that carry code or native libraries. A
|
||||
member that carries resources cannot be folded into the base without merging `resources.arsc`,
|
||||
which is a resource-compiler job. A wrongly merged archive installs and then renders the wrong
|
||||
thing, or dies on the first resource lookup.
|
||||
|
||||
`scripts/repack.py` handles both routes and **refuses to guess**: it inventories the set, decides
|
||||
which route is legal from what the members actually carry, prints the reason, and only then acts.
|
||||
|
||||
**Strength labels** (same three this repository uses everywhere): **observed** = reproduced with an
|
||||
exact command and output; **inferred** = follows from observed facts, step not executed;
|
||||
**unverified** = reported or assumed, not independently confirmed. The measurements behind this
|
||||
file are recorded in `docs/tool-verification/EXTENSION-split-apk.md`.
|
||||
|
||||
## 1. What the set is
|
||||
|
||||
| Member | Manifest `split` attribute | Carries | Foldable into the base? |
|
||||
|---|---|---|---|
|
||||
| base | *(absent)* | all `classes*.dex`, the main `resources.arsc`, every resource not moved out, the real `<application>` | — it *is* the base |
|
||||
| ABI split | `config.arm64_v8a`, `config.armeabi_v7a`, `config.x86_64`, … | `lib/<abi>/*.so` only | **yes** — entry names do not collide |
|
||||
| density split | `config.hdpi` … `config.xxxhdpi` | its own `resources.arsc` **and** `res/` entries | **no** — requires a resource merge |
|
||||
| language split | `config.en`, `config.zh`, … | its own `resources.arsc` (strings) | **no** — same reason |
|
||||
| feature split | `feature_x` (+ `isFeatureSplit="true"`, `configForSplit="base"`) | its own `classes*.dex`, possibly resources | **code only**; its resources hit the same wall |
|
||||
|
||||
**Observed** on a real 9-member set pulled from a stock Android 11 device (arm64-v8a target):
|
||||
|
||||
- the base carried `classes.dex` (872 KB) and `resources.arsc` (71 KB);
|
||||
- the ABI member carried two `lib/arm64-v8a/*.so` files and **no `resources.arsc` at all**;
|
||||
- each density member carried its own `resources.arsc` and its own `res/` entries;
|
||||
- a second, 8-member set's density members carried 39-78 `res/` entries each and 9-15 KB tables.
|
||||
|
||||
So "which member owns the resources" is answerable **from the zip entry list alone** — no device, no
|
||||
resource compiler, no decompiler. Copy a set to disk and ask.
|
||||
|
||||
**A member can own a nearly empty table.** A split is *required* to carry a `resources.arsc`, so a
|
||||
placeholder one is normal: on the same real set, eight density members carried a **40-byte** table
|
||||
and no `res/` entries at all — they are placeholders, not resources. Treating "has an arsc" as "owns
|
||||
resources" would block a merge that is perfectly legal. `repack.py` uses *entries under `res/` or a
|
||||
table bigger than 1 KB* as the test.
|
||||
|
||||
## 2. Getting the set off a device
|
||||
|
||||
`pm path` prints **one line per member**; a package with several lines is a split set. This is how
|
||||
you find one to copy, and it is also how you tell, before anything else, that the thing you are about
|
||||
to patch is not a single APK:
|
||||
|
||||
```bash
|
||||
adb shell 'pm path <PKG>'
|
||||
package:/system/priv-app/<DIR>/<APP>.apk
|
||||
package:/system/priv-app/<DIR>/<APP>-arm64_v8a.apk
|
||||
package:/system/priv-app/<DIR>/<APP>-xxhdpi.apk
|
||||
```
|
||||
|
||||
To enumerate every split set on a device (toybox shell — no `--time-style`, no bash arrays):
|
||||
|
||||
```bash
|
||||
adb shell 'for p in $(pm list packages | cut -d: -f2); do c=$(pm path $p | wc -l); \
|
||||
if [ $c -gt 1 ]; then echo "$c $p"; fi; done'
|
||||
```
|
||||
|
||||
Copy the whole directory out; the members live side by side:
|
||||
|
||||
```bash
|
||||
adb shell 'su -c "ls -la /system/priv-app/<DIR>/"'
|
||||
adb pull /system/priv-app/<DIR> <dest>/
|
||||
```
|
||||
|
||||
Two traps, both **observed**:
|
||||
|
||||
- `adb pull <dir> <dest>` creates `<dest>/<dir>/` — the set arrives one level deeper than you expect.
|
||||
`repack.py --split-dir` searches recursively for exactly this reason.
|
||||
- A **third-party** split set is rare on a stock ROM. On the device measured here, `pm list packages -3`
|
||||
returned **zero** split sets among ~40 third-party apps, while four preinstalled Google components
|
||||
were split. If you need a real set and the third-party list is empty, that is the normal
|
||||
distribution, not a mistake on your side.
|
||||
|
||||
**Do not test by reinstalling a pulled set under its own package name.** It is already installed on
|
||||
that device, owned by the vendor certificate you do not have, so the install fails for a reason that
|
||||
has nothing to do with your pipeline (§6). Either work on a set you own, or rename the pulled one
|
||||
(§7) to get an installable fixture.
|
||||
|
||||
## 3. The decision: merge or resign
|
||||
|
||||
Run this first — it writes nothing and answers with a reason:
|
||||
|
||||
```bash
|
||||
python skills/apk-reverse/scripts/repack.py --split-dir <set>/ --split-mode analyze
|
||||
```
|
||||
|
||||
| Situation | Route | Why |
|
||||
|---|---|---|
|
||||
| Every non-base member carries only code and/or `lib/` (a placeholder `resources.arsc` is fine) | **merge** | nothing that needs a resource table moves |
|
||||
| Any member carries `res/` entries or a real table | **resign** | a resource merge rewrites the table; see §5 |
|
||||
| You must keep the build installable **and** the app is distributed as a bundle | **resign** | this is what the store does; the set stays a set |
|
||||
| You need one file to hand someone, sideload, or feed to a tool that takes an APK | **merge**, if legal | the only way to get a single artifact |
|
||||
| The delivery must survive store updates, or the ROM's own installer refuses multi-APK installs | **resign** (`pm install-multiple`), or merge and accept a fixed build | — |
|
||||
| A member declares `isFeatureSplit="true"` and ships `classes*.dex` **and** resources | **resign** | its code could fold, its resources cannot; a half-merged build is worse than either route |
|
||||
|
||||
Failure modes, one per route:
|
||||
|
||||
- **merge**, when a resource-carrying member is dropped to make it legal: the app starts and shows
|
||||
the base's resources. Missing density variants show up as blurry or missing images; missing
|
||||
language splits show as untranslated strings. This is a **downgraded** build and has to be
|
||||
delivered as one — `repack.py` prints `DROPPED n entry/ies` and refuses unless you pass
|
||||
`--drop-split-resources`, so the loss cannot be silent.
|
||||
- **merge**, when the base still declares `isSplitRequired="true"`: install succeeds and start is
|
||||
refused (`INSTALL_FAILED_MISSING_SPLIT`, §6). The attribute has to be cleared first, and that is a
|
||||
binary-AXML edit.
|
||||
- **resign**, when one member is missed: the set fails as a set, with a signature error naming the
|
||||
package. Sign the directory, not a list you typed by hand.
|
||||
- **resign**, when the target ROM's installer is not the platform installer: `adb install-multiple`
|
||||
can be intercepted by an OEM security centre, exactly as single-APK installs are
|
||||
(`references/repack-and-sign.md` §Vendor install interception (OEM shells)). Use the root
|
||||
`pm install-multiple` path.
|
||||
|
||||
## 4. Unified signing — the route that always works
|
||||
|
||||
```bash
|
||||
python skills/apk-reverse/scripts/repack.py \
|
||||
--split-dir <set>/ --split-mode resign \
|
||||
--split-out-dir <signed_set>/ \
|
||||
--ks key.jks --apksigner <path/to/apksigner>
|
||||
```
|
||||
|
||||
Each member is de-signed (only the signature entries), rewritten as a 4-byte-aligned archive, and
|
||||
signed with **one** keystore and **v1+v2+v3**; the run then verifies every member and prints the
|
||||
signer fingerprint per file. The acceptance criterion is not "each file verifies" — it is **one
|
||||
certificate across the whole set**:
|
||||
|
||||
```
|
||||
[result] OK: 8 members, one certificate (sha256 1eb31d9c…2c44)
|
||||
```
|
||||
|
||||
Then install the whole set, base included:
|
||||
|
||||
```bash
|
||||
adb install-multiple -r <signed_set>/*.apk
|
||||
adb shell "su -c 'pm install-multiple -r /data/local/tmp/set/*.apk'" # when an OEM installer refuses the multi-APK form
|
||||
```
|
||||
|
||||
**The signature must cover every member**, and `--abi`/density selection happens at install time, not
|
||||
at signing time — sign them all, let the installer pick (see §6).
|
||||
|
||||
### Toolchain: `uber-apk-signer` is optional, `apksigner` is not
|
||||
|
||||
`repack.py`'s original signing route went through `uber-apk-signer` only, so a machine with a full
|
||||
Android build-tools directory and no uber-apk-signer could not produce a signed build at all. The
|
||||
script now picks the route itself:
|
||||
|
||||
- `--signer jar` — the uber-apk-signer path (used automatically when the jar exists);
|
||||
- `--signer apksigner` — `zipalign -p -f 4` then `apksigner sign --v1 --v2 --v3`, which is the whole
|
||||
of what the jar was doing for us;
|
||||
- `--signer auto` (default) — jar if present, else apksigner.
|
||||
|
||||
`--apksigner` accepts all three shapes a build-tools install ships: `apksigner.bat`, the `apksigner`
|
||||
shell wrapper, or the bare `lib/apksigner.jar`. **On Windows pass the `.jar` or the `.bat`** — the
|
||||
extensionless wrapper is a shell script and will not execute. A `.jar` value is run through
|
||||
`java -jar`, so `--java` (or java on PATH) is required for it.
|
||||
|
||||
Order is load-bearing: **align, then sign**. `apksigner` adds signature entries without reshuffling
|
||||
the archive; the v1 (JAR) path emulates `jarsigner`, whose rewrite of the zip would destroy the
|
||||
alignment Android R+ requires of `resources.arsc`.
|
||||
|
||||
## 5. Merging into a single APK
|
||||
|
||||
```bash
|
||||
python skills/apk-reverse/scripts/repack.py \
|
||||
--split-dir <set>/ --split-mode merge --out merged.apk \
|
||||
--abi arm64-v8a --ks key.jks --apksigner <path/to/apksigner>
|
||||
```
|
||||
|
||||
What folds: `classes*.dex` (renumbered so a member's `classes.dex` becomes `classes2.dex`, keeping
|
||||
the base's own dex intact), `lib/**`, `assets/**`. What does not, and why: `res/**` and
|
||||
`resources.arsc`.
|
||||
|
||||
**Why resource merging is not attempted.** `resources.arsc` is an indexed table: a global string
|
||||
pool plus type-spec and type chunks, with every resource identified by a `(package, type, entry)`
|
||||
triple and every string referenced by pool offset. Folding a member's table into the base's means
|
||||
rewriting pool offsets, type/entry counts and the alignment of every string, in both directions —
|
||||
that is `aapt2 link`, not a byte-level edit. The failure it produces is the worst kind: the archive
|
||||
is *structurally* valid, installs, and then resolves resource ids to the wrong entries or to nothing.
|
||||
An APK that renders garbage is harder to diagnose than one that refuses to install.
|
||||
|
||||
If you genuinely need one standalone file for a bundle whose resources are split, the honest routes are:
|
||||
|
||||
1. **`bundletool build-apks --mode=universal`** (the vendor tool, needs the original `.aab`) — this is
|
||||
what merges resources correctly, and it is the only route that does.
|
||||
2. **Keep the split set** and install it as a set (§4).
|
||||
3. **Accept the downgrade** with `--drop-split-resources`, and say so in the delivery.
|
||||
|
||||
**Manifest constraints when you do merge — and when you patch any split at all.** These attributes
|
||||
are the difference between "installs and runs" and an install or start refusal, and they are easy to
|
||||
miss because they live in binary AXML:
|
||||
|
||||
| Attribute | Lives on | Effect if you get it wrong |
|
||||
|---|---|---|
|
||||
| `split="config.xxhdpi"` | `<manifest>` of a **split** | Identifies the member. A split whose `package` differs from the base's, or whose `split` name is duplicated, is refused as a set |
|
||||
| `configForSplit="base"` | `<manifest>` of a split | Ties a configuration split to a feature module. Changing package/module names without updating it silently detaches the split |
|
||||
| `isFeatureSplit="true"` | `<manifest>` of a feature split | Marks an install-time/on-demand module. A merge that folds code in but leaves the attribute declared describes a member that no longer exists |
|
||||
| `isSplitRequired="true"` | `<manifest>` of the **base** | The platform refuses to start a base that believes it is incomplete. This is the one that breaks merged builds (§3) |
|
||||
|
||||
Read them without a decompiler through `scripts/repack.py`'s own AXML reader (`--split-mode analyze`
|
||||
prints the `split` name per member; `inspect_apk()` returns `package`, `split`, `configForSplit`,
|
||||
`isSplitRequired`, `isFeatureSplit`). Changing any of them means editing the binary string pool and
|
||||
the attribute chunks around it, which is what `apktool` is for — and note that a full
|
||||
`apktool b` rebuild rewrites the whole archive
|
||||
(`references/repack-and-sign.md` §Repacking an unpacked (de-shelled) app).
|
||||
|
||||
## 6. Install refusals, and what each one means
|
||||
|
||||
| Message | What it actually says | Attribution |
|
||||
|---|---|---|
|
||||
| `Failure [INSTALL_FAILED_INVALID_APK: ... signatures do not match previously installed version]` / `Package <PKG> signatures do not match` | The set you are installing is signed with a different certificate than the one already on the device | **Not** a malformed build. Either re-sign to the original key, or install to a package name that does not exist yet |
|
||||
| `Failure [INSTALL_FAILED_ALREADY_EXISTS]` / `INSTALL_FAILED_UPDATE_INCOMPATIBLE` | Same package, different signer (or a downgrade without `-d`) | A pulled system component cannot be reinstalled over itself; see §7 |
|
||||
| `Failure [INSTALL_FAILED_MISSING_SPLIT: Missing split for <PKG>]` | The base declares `isSplitRequired="true"` and the set is incomplete — or you merged it | The base is telling the truth: it expects members. Clear the attribute, or install the complete set |
|
||||
| `Failure [INSTALL_FAILED_INVALID_APK: Failed to parse ... split ... has different signature]` | **One member** of the set is signed differently from the rest | This is the failure mode of signing only the base. Sign the whole directory |
|
||||
| `Failure [-124: ... resources.arsc ... stored uncompressed and aligned on a 4-byte boundary]` | One member's `resources.arsc` is compressed or unaligned | Per-member, not per-set. `repack.py` reports it per member before signing (`references/repack-and-sign.md` §2a) |
|
||||
| `Failure [-99]`, or a numeric-only code with no `INSTALL_FAILED_*` | An OEM installer interceptor rejected the request | Nothing to do with the set; use the root `pm install-multiple` path |
|
||||
| `adb install-multiple` on a device that has no `install-multiple` support (very old `pm`) | Install each member with `pm install` in order, base last | Use `pm install-create` / `install-write` / `install-commit` explicitly |
|
||||
|
||||
`INSTALL_FAILED_INVALID_APK` in particular is a **family**, not one error: it covers stale
|
||||
signatures, malformed base/split relationships, and alignment violations. Read the text after the
|
||||
colon before attributing anything — the constant alone will send you looking at the wrong file.
|
||||
|
||||
## 7. Making an installable fixture from a pulled set
|
||||
|
||||
**Observed, and the reason this section exists:** a set pulled from the device is installed *on that
|
||||
device*, under a name owned by the vendor certificate. Re-signed copies cannot be installed back
|
||||
over it, so "does our signed set install?" cannot be answered with the pulled set as it stands.
|
||||
|
||||
The cheap, structure-preserving way out is an **equal-length package rename**: replace every
|
||||
occurrence of the old package name in `AndroidManifest.xml`, `resources.arsc` and `classes*.dex`
|
||||
with a new name of exactly the same length. Nothing moves — no length prefix, no string-pool offset,
|
||||
no dex `string_ids` offset, no resource index — so the only field that must be recomputed is the dex
|
||||
header's integrity pair (signature first, checksum last, see
|
||||
`references/repack-and-sign.md` §Repacking an unpacked (de-shelled) app). Then run the ordinary
|
||||
`--split-mode resign` over the renamed directory and install that.
|
||||
|
||||
A detail worth knowing before you touch a manifest: **a member's Java classes need not live under its
|
||||
manifest `package` at all**. On the real set measured here, the manifest package was
|
||||
`com.google.android.<name>` while all 677 class references in the dex were
|
||||
`Lcom/android/<name>/...` — and every `<activity android:name>` was fully qualified. Renaming the
|
||||
manifest package therefore left every component resolvable, with no dex edit needed. Assume the
|
||||
opposite (that the two must match), and you will edit a dex for nothing.
|
||||
|
||||
## 8. ABI and density matching
|
||||
|
||||
The device tells you what it can run:
|
||||
|
||||
```bash
|
||||
adb shell getprop ro.product.cpu.abilist # e.g. arm64-v8a,armeabi-v7a,armeabi
|
||||
adb shell getprop ro.product.cpu.abi # the primary ABI
|
||||
adb shell getprop ro.product.cpu.abilist32 # 32-bit list, on a 64-bit device
|
||||
```
|
||||
|
||||
Match against the **split name**, not the file name, and mind the separator: split qualifiers use
|
||||
**underscores** (`config.arm64_v8a`) while `lib/` directories use **hyphens** (`lib/arm64-v8a/`). The
|
||||
mapping is a substitution, not a lookup — `arm64_v8a` → `arm64-v8a`, `armeabi_v7a` → `armeabi-v7a`;
|
||||
`x86` and `x86_64` are unchanged. That is why `--abi arm64-v8a` matches a split named
|
||||
`config.arm64_v8a`.
|
||||
|
||||
Four rules that decide ABI questions:
|
||||
|
||||
- **A 64-bit device still installs a 32-bit-only set**, and then runs it 32-bit. If the base carries
|
||||
no native code and only the `armeabi_v7a` split does, the app is a 32-bit app on that device.
|
||||
- **The ABI split is selected by the installer, not by `repack.py`.** `--abi` exists for *merging*
|
||||
(where the choice is baked into one file); for `--split-mode resign`, sign every member and let
|
||||
`pm` pick.
|
||||
- **Merging without `--abi` folds in every ABI member**, which makes one APK with two or three copies
|
||||
of the same library set. Legal, larger, and pointless on a single-device delivery.
|
||||
- **`getprop` reports what the device claims**; what runs is a live mapping question
|
||||
(`scripts/lib_map.py`). A device that reports `arm64-v8a` first can still execute a 32-bit process.
|
||||
|
||||
Density is the same shape without the separator trap: `config.xxhdpi` matches
|
||||
`res/drawable-xxhdpi/`, and the installer picks the closest variant the device's `ro.sf.lcd_density`
|
||||
can use. There is nothing to select on your side.
|
||||
|
||||
## 9. Traps worth paying for once
|
||||
|
||||
- **`resources.arsc` belongs to whoever ships it.** In a split set it exists *per member*, and the
|
||||
base's table does not contain the density member's entries. Reading the base's table and concluding
|
||||
"the resource exists" is wrong; it exists in one configuration only.
|
||||
- **`android:extractNativeLibs`.** If the base declares `extractNativeLibs="false"` (the default for
|
||||
modern targetSdk), the `.so` files are mmapped straight out of the archive, and they must be
|
||||
**uncompressed and page-aligned** — this is what `zipalign -p` exists for. Merging native members
|
||||
into a base and then writing the archive without that alignment produces an install that succeeds
|
||||
and a start that dies in the linker. `repack.py` writes uncompressed `lib/**` 4-byte aligned, and
|
||||
`zipalign -p -f 4` adds the page alignment the loader wants for them.
|
||||
- **A placeholder `resources.arsc` is not a resource split** (§1). Blocking on "has an arsc" refuses
|
||||
legal merges.
|
||||
- **Do not `zipalign` after signing to "fix" a member.** It rewrites the archive and invalidates the
|
||||
signature. Align, then sign.
|
||||
- **`apktool` may not be usable even when a wrapper exists.** Measured on the pass machine: the
|
||||
`apktool.bat` wrapper pointed at a jar that was not on disk
|
||||
(`Error: Unable to access jarfile <missing>.jar`), while `apksigner.jar` and `zipalign.exe` in the
|
||||
same layout were fine. Check the wrapper's target before planning a manifest edit around it.
|
||||
- **The AXML `split` attribute is a string-pool entry**, which is why an equal-length rename of it is
|
||||
safe and why a length-changing edit is not: the pool's own length prefixes and the attribute chunk
|
||||
sizes around it would both need recomputing.
|
||||
|
||||
## 10. What this file does not claim
|
||||
|
||||
- **It does not merge resources.** No route here reproduces `bundletool build-apks --mode=universal`.
|
||||
A bundle whose resources live in splits stays a set, or becomes a downgraded single APK by explicit
|
||||
choice.
|
||||
- **It does not split a monolithic APK back into modules**, and it cannot turn a `.aab` into APKs —
|
||||
that is `bundletool`, and the `.aab` is the input it needs.
|
||||
- **`--split-mode merge` covers code and native libraries only** (§5). Feature-split *code* folds;
|
||||
feature-split *resources* do not.
|
||||
- The install-failure texts in §6 are the shape the platform produces; **the exact wording varies by
|
||||
Android version and by OEM installer**. Read the text after the constant, not the constant.
|
||||
@@ -0,0 +1,237 @@
|
||||
# Differential hardening (the known-plaintext route to a private opcode table)
|
||||
|
||||
Load this when the dump is a **real Dex VMP** — bodies present, decoding as nonsense to a
|
||||
dalvik disassembler, a native interpreter loop behind them — and you have decided the
|
||||
private opcode space is worth attacking rather than walking away from. The neighbouring
|
||||
file owns the diagnosis (`advanced-unpacking.md` §The honest boundary: real Dex VMP) and
|
||||
the sentence that sends you here. This file owns what happens next, and — more of it than
|
||||
is comfortable — what does not.
|
||||
|
||||
**Strength note, read before trusting any number here.** Every mechanism below was
|
||||
exercised on locally built fixtures, and the closed loop is measured: `simulate` relabels
|
||||
a fixture through a known bijection and `compare` re-derives that table **exactly, 218 of
|
||||
218 emitted opcodes, zero wrong entries, zero fabricated entries** (`docs/tool-verification/EXTENSION-vmp-diff.md`).
|
||||
What was **not** exercised is the other half of the premise: **no third-party hardening
|
||||
platform was contacted**. The assumption that a real engine performs a per-opcode
|
||||
substitution at stable instruction length is **inferred** from public VMP write-ups and is
|
||||
exactly the assumption this method can fail on. The cost table's platform column is
|
||||
therefore reasoning from how those platforms are distributed, not from a measured upload.
|
||||
|
||||
## 1. The method, and the one thing it needs to be true
|
||||
|
||||
The hardened body cannot tell you where its instruction boundaries are: its opcodes are
|
||||
private, so the format specification's length table does not apply to it. That is the
|
||||
whole obstacle — a disassembler that cannot decide where instruction *n* ends cannot name
|
||||
instruction *n+1*.
|
||||
|
||||
The differential dissolves it with an oracle you control. Compile a fixture whose every
|
||||
instruction you know; get the same platform to harden it; then **take the original's
|
||||
instruction boundaries and project them onto the hardened bytes**. Original offset
|
||||
`0x14` was a `mul-int` occupying 2 code units, so hardened bytes `0x14..0x17` are the same
|
||||
instruction, re-encoded. Read the hardened opcode byte at each projection and you have a
|
||||
`(original opcode → private byte)` sample. Aggregate over thousands of instructions and
|
||||
the substitution table falls out.
|
||||
|
||||
Three preconditions, and they are not negotiable:
|
||||
|
||||
| Precondition | Why it must hold | What it looks like when it fails |
|
||||
|---|---|---|
|
||||
| **The hardened body keeps the same length** | The projection is a stencil; a length change slides every boundary after the first edit | `compare` reports `resized` for the whole method and refuses the run |
|
||||
| **The substitution is per-opcode, not per-method** | One private byte per original opcode is what makes the table a table | Every original opcode claims the same private byte (`compare` → `NOT-USABLE`, stub shape) |
|
||||
| **The body is still in the dex** | Alignment needs two byte streams | Method has `code_off == 0` (`compare` reports `stripped`) |
|
||||
|
||||
The third is the one that kills the method most often in practice: a VMP that *extracts*
|
||||
bodies to a native interpreter leaves nothing to align. The differential route then has no
|
||||
input at all, and no amount of clever fixture design creates one. Check for it before
|
||||
spending any time here.
|
||||
|
||||
## 2. Cost table — which links can be automated, and which cannot
|
||||
|
||||
This is the part the method's public descriptions leave out. The chain is six links; two
|
||||
of them are unavoidably human, and pretending otherwise is how a "harness" ships that has
|
||||
never once been run against a real target.
|
||||
|
||||
| # | Link | Automation | Where a human must stand | Time cost | Failure mode |
|
||||
|---|---|---|---|---|---|
|
||||
| 1 | Compile the coverage fixture into a dex + APK | **Full** — `scripts/vmp_diff_harness.py build`, measured | none | ~40 s on this machine (javac + d8 + aapt2), ~1 s of it the script | toolchain absent or version-drifted; `d8` rejects a non-existent `--output` directory, `aapt2 link` rejects `android:` attributes without `android.jar` |
|
||||
| 2 | **Submit to the hardening platform** | **None. Not automatable.** | 100 % — a browser, an account, sometimes a queue and a review | minutes to **days**; the platform decides | **the binding constraint of the whole route.** No public API; per-account rate limits; uploads are frequently gated behind registration; the artifact often comes back as a download page rather than a file path. A synthetic fixture may simply be **refused** (no real app shape, no certificate, no package identity) |
|
||||
| 3 | Retrieve the hardened output and extract its dex | Partial — if the output is an APK, `unzip` + `dexutil.load_dex` handles it | download and hand-off | seconds, once the file exists | output is an APK whose dex is *also* encrypted, or the platform returns only a repacked APK with a native loader and no readable dex |
|
||||
| 4 | Derive the correspondence | **Full** — `compare`, measured end to end | none | **sub-second** on a 3,065-instruction fixture | alignment slips (see §4); engine is not a pure relabelling |
|
||||
| 5 | Prove the derived table | Partial — the mechanical half is scripted and measured; the judgement is not | decide whether the held-out check was passed or merely not attempted | minutes | a table that is *self-consistent* but describes a stub (§5) |
|
||||
| 6 | Render Smali from the restored stream | Full — `emit-smali`, measured | none | sub-second | table has holes; restored stream decodes short of the method end |
|
||||
|
||||
**Conclusion, stated plainly: do not build a fully automatic pipeline.** Link 2 is
|
||||
irreducibly manual — the platform has no interface a script can drive, and its output
|
||||
cadence is not one a pipeline can wait on. The right shape is what this repository ships:
|
||||
|
||||
- links 1, 4 and 6 as **reliable local tools** (they are deterministic and were measured);
|
||||
- link 5 as **a checklist plus one scripted test** (§5) — the judgement stays human;
|
||||
- link 2 as **a documented manual step**, with its cost stated up front, so nobody writes
|
||||
an orchestrator that waits forever on a web form;
|
||||
- and links 4–5 **self-verifiable without the platform at all**, which is what `simulate`
|
||||
exists for. You can prove your comparison logic against a known table today; you cannot
|
||||
prove the engine's behaviour until a human uploads a fixture.
|
||||
|
||||
The practical consequence: the deliverable of this route is an **analysis report with a
|
||||
stated boundary**, not a pipeline. Say so before starting.
|
||||
|
||||
## 3. The coverage fixture — what a compiled probe can and cannot reach
|
||||
|
||||
A Java-compiled fixture reaches **218 of the 224 named opcodes in this repository's format
|
||||
table (97.3 %)** — that number is measured, not estimated, and the six it misses are
|
||||
structural rather than incidental. The reason to care is direct: **an opcode your fixture
|
||||
never emits leaves no known plaintext, so its substitution is unreadable by construction.**
|
||||
|
||||
| Missed opcode | Why a javac + d8 fixture cannot reach it |
|
||||
|---|---|
|
||||
| `const-string/jumbo` (0x1b) | needs a `string_ids` index > 65535, i.e. a fixture shipping 65,536+ string constants |
|
||||
| `goto/32` (0x2a) | needs a >32767-code-unit backward jump; javac refuses the method first with `error: code too large` — its own 64 KB per-method bytecode cap. **Measured** on a generated 33,000-statement loop |
|
||||
| `const-method-handle` (0xfe), `const-method-type` (0xff) | no Java-language literal exists for these constants; reachable only via hand-written smali or direct dex construction |
|
||||
| `move-object/16` (0x09) | needs **both** registers ≥ 256; the object frame pushes the source past 256 but d8 encodes that as `move-object/from16` |
|
||||
| `invoke-custom/range` (0xfd) | d8 emitted the 35c form for every lambda shape tried, including a six-parameter one |
|
||||
|
||||
What the fixture **does** cover, by construction, and the design decisions that get it
|
||||
there (each of these was a measured gap before it was closed):
|
||||
|
||||
- **Three-register forms.** javac emits the `/2addr` form whenever the destination is one
|
||||
of the operands, so `sub-int`, `div-int`, `rem-int`, `and-int`, `or-int`, `xor-int`,
|
||||
`shl-int`, `shr-int`, `ushr-int` and their long/float/double counterparts are **absent**
|
||||
from ordinary code. Three distinct locals per operation is what forces them into the
|
||||
stream; the `/2addr` forms then need the accumulator spelling. Both are wanted.
|
||||
- **Literal widths.** `div-int/lit16` needs a divisor in 128..32767; `and-int/lit8` needs a
|
||||
mask inside −128..127. Writing `& 255` produces `and-int/lit16` and silently skips the
|
||||
lit8 form.
|
||||
- **Per-type field access.** `iget-wide`, `iget-object`, `iget-boolean`, `iget-byte`,
|
||||
`iget-char`, `iget-short` and the `sget` set all appear only when the load targets a
|
||||
**fresh local**. Accumulating into an existing register is where d8 narrows the access.
|
||||
- **`/range` invokes.** The 35c invoke forms carry at most five argument registers, so a
|
||||
seven-register call (`this` plus six ints) is what makes the `/range` encoding the only
|
||||
legal one.
|
||||
- **Wide registers and long jumps** — `move/16`, `move-wide/16`, `move-object/from16`,
|
||||
`goto/16` — have **no hand-written form at all**. They exist only past a register-number
|
||||
(≥256) or branch-distance (>127 code units) threshold, so the generator emits bulk
|
||||
source: 300 int locals, 140 long locals, 160 object locals, a 200-statement loop body.
|
||||
Measured yield: `move/16` ×46, `move-wide/16` ×20, `move-object/from16` ×161, `goto/16` ×1.
|
||||
- **Payloads and structure**: `packed-switch`/`sparse-switch` with their payload blocks,
|
||||
`fill-array-data`, `filled-new-array/range`, try/catch ranges with `move-exception`,
|
||||
`monitor-enter`/`monitor-exit`, `instance-of`/`check-cast`, and `invoke-custom` /
|
||||
`invoke-polymorphic` (which is why `d8 --min-api 26 --no-desugaring` is the default —
|
||||
desugaring replaces lambdas with synthetic classes and the two opcodes disappear).
|
||||
|
||||
**Report the coverage every time.** `build` prints it, `audit` recomputes it for any dex.
|
||||
A differential run whose coverage is 60 % produces a table that is 40 % missing, and
|
||||
nothing in the output will say so unless you look at this number.
|
||||
|
||||
## 4. Extracting the correspondence
|
||||
|
||||
`compare ORIGINAL HARDENED` does four things, and the first three are diagnostics that
|
||||
decide whether the fourth is meaningful:
|
||||
|
||||
1. **Pair methods** by `(class, name, descriptor)` and classify each pair. `aligned` means
|
||||
both sides have a `code_item` of **identical `insns_size`** — the only state in which
|
||||
projection is legal. `resized` means the length moved. `stripped` means the hardened
|
||||
method has `code_off == 0`. `missing`/`added` mean the method set itself moved.
|
||||
2. **Project and sample.** For each aligned pair, walk the original's decoded instruction
|
||||
list; for instruction at original offset *o*, read the hardened byte at
|
||||
`hardened.insns_off + (o − original.insns_off)`. Record `(original opcode → hardened
|
||||
byte)` with a count.
|
||||
3. **Aggregate and audit the aggregate.** Per original opcode: one candidate byte and the
|
||||
row is `high`; more than one and the row is a `conflict`. Then the **reverse
|
||||
direction** — build `private byte → set of original opcodes` and flag every private byte
|
||||
claimed by more than one original. This reverse pass is not a nicety; it is the only
|
||||
thing that catches the stub shape (§5).
|
||||
4. **Emit a run-level verdict**, because a table is dangerous without one: `usable`,
|
||||
`partially-usable`, `not-usable`, or `not-applicable` with the reason. A `not-applicable`
|
||||
run exits non-zero.
|
||||
|
||||
Unreadable opcodes are reported as `undetermined` with the reason — either the fixture
|
||||
never emitted them (§3) or the dex slot is unallocated. They are **not** guessed at.
|
||||
|
||||
## 5. Proving the table — the part that decides whether any of this is real
|
||||
|
||||
A candidate table is a hypothesis. Four checks, in increasing order of what they can
|
||||
falsify:
|
||||
|
||||
**a. Closed-loop regression — measured, and the strongest available without a platform.**
|
||||
`simulate` relabels a fixture's opcodes through a bijection whose ground truth it writes
|
||||
out; `compare` must re-derive that table from the two dexes alone. Measured on this
|
||||
repository's fixture: **218 rows derived, 218 of 218 emitted opcodes recovered, zero
|
||||
entries wrong, zero entries fabricated, zero non-injective private bytes, verdict
|
||||
`usable`.** Any change to the comparison logic that breaks this check is a regression, and
|
||||
the check needs no platform, no device and no network.
|
||||
|
||||
**b. Injectivity — the check that catches a confident wrong answer.** Every private byte
|
||||
must be claimed by exactly one original opcode. Measured against the stub shape (bodies
|
||||
replaced by an equal-length `return-void` fill): per-opcode reading looks *perfect* — 218
|
||||
rows, all `high`, zero conflicts — because every original opcode maps to the same byte.
|
||||
Only the reverse direction exposes it: one private byte claimed by **218** originals →
|
||||
`NOT-USABLE`. A table produced without this check would have been silently worthless.
|
||||
|
||||
**c. Held-out consistency.** Derive the table from one subset of methods, then use it to
|
||||
disassemble a **different** subset that contributed nothing to the derivation. A correct
|
||||
table restores the instructions such that the walk lands exactly on
|
||||
`insns_off + insns_size × 2` and every branch target lands on an instruction boundary. A
|
||||
wrong or incomplete table desynchronises and says so. `emit-smali`'s
|
||||
`instructions restored` / `unmapped-opcode stops` counters are the readout: on the measured
|
||||
fixture, 3,065 restored and 0 stops.
|
||||
|
||||
**d. Cross-check against a second, independent recovery.** The same private opcode space
|
||||
observed through a *different* route — an in-memory dump of the original dex alongside the
|
||||
hardened one, or the `/proc/<pid>/mem` route in
|
||||
`advanced-unpacking.md` §Dumping when frida is refused — is the only evidence that the
|
||||
platform's transformation is the one you assumed. **Not done here**; it requires a real
|
||||
VMP sample and a real platform, and is the honest gap in this file.
|
||||
|
||||
Note what none of these checks establish: that the engine's substitution is *stable across
|
||||
inputs*. Fixture-derived tables have been reported in public work to drift per
|
||||
vendor-version and per hardening pass. Re-derive per target; never cache a table.
|
||||
|
||||
## 6. Generating Smali from a restored stream
|
||||
|
||||
`emit-smali HARDENED_DEX --table TABLE.json` restores opcode bytes through the table and
|
||||
renders method bodies as an annotated smali **reading skeleton**:
|
||||
|
||||
- Boundaries come from the table itself — once a byte maps back to a standard opcode the
|
||||
standard length table applies to that instruction, so the walk is self-bootstrapping.
|
||||
A byte with no table entry stops that method's walk, and the count of such events is
|
||||
printed as the honest coverage measure.
|
||||
- `.class` / `.super` / `.source` / `.method` headers are real, with access flags decoded
|
||||
from `access_flags`, so the skeleton is readable as smali rather than as a hex dump.
|
||||
- Payload blocks (`packed-switch-payload` etc.) are stepped over by their own layout, not
|
||||
treated as opcodes.
|
||||
|
||||
**It is explicitly not assembliable, and the output says so in its first ten lines.**
|
||||
Recovered: instruction boundaries and opcode names. Not recovered: register numbers,
|
||||
field/method/string indices as the engine rewrote them, and any register re-allocation —
|
||||
**no map for those exists in this method at all.** Feeding the skeleton to a smali
|
||||
assembler produces an empty method, which looks like success and is not.
|
||||
|
||||
## 7. When this route does not work — the honest retreat
|
||||
|
||||
| What `compare` reports | What it means | Do this |
|
||||
|---|---|---|
|
||||
| `verdict: not-applicable`, `stripped` > 0 | bodies left the dex for a native interpreter | the differential has no input. Stop. Return to `advanced-unpacking.md` §The honest boundary: real Dex VMP and price the island honestly |
|
||||
| `verdict: not-applicable`, `resized` > 0 | the engine's stream has its own length table | the projection premise is false. Stop rather than guessing boundaries |
|
||||
| `verdict: not-usable`, one private byte shared by many originals | bodies replaced by a common stub | the engine did not relabel per-opcode here. Stop |
|
||||
| `verdict: partially-usable`, conflicts ≤ 30 % | partial relabelling, or alignment slipping on some methods | usable only for the `high` rows; say so and stop at the unreadable ones |
|
||||
| `usable`, but coverage was low | a complete table over an incomplete opcode set | usable, with the uncovered opcodes named as unreadable |
|
||||
|
||||
And the retreat that outranks all of them: **the cheaper question is whether the behaviour
|
||||
you need exists outside the virtualized island.** A hook at the method's Java boundary, an
|
||||
emulated call to the routine rather than a reading of it
|
||||
(`emulation-and-rpc.md`), or a server-side answer frequently reaches the goal without ever
|
||||
paying for the island. Weeks of table recovery is the expensive way to ask a question a
|
||||
boundary hook answers in an hour.
|
||||
|
||||
## 8. Decision summary
|
||||
|
||||
| Observation | Action | Where |
|
||||
|---|---|---|
|
||||
| Bodies present, decode as nonsense | Diagnose real VMP before anything else | `advanced-unpacking.md` §The honest boundary: real Dex VMP |
|
||||
| Need the private opcode mapping | Build the coverage fixture, then submit it by hand | `scripts/vmp_diff_harness.py build`; §2 of this file |
|
||||
| Asked to "automate the whole chain" | Say which link cannot be automated and what it costs | §2 — the platform step is manual, by construction |
|
||||
| Fixture coverage below ~90 % | Add the missing opcode classes before submitting | §3, and the `audit` report |
|
||||
| Two dexes in hand | Derive the candidate table, read the verdict first | `scripts/vmp_diff_harness.py compare` |
|
||||
| About to trust a derived table | Run the closed loop, the injectivity check and a held-out subset | §5 — a/b/c are scripted |
|
||||
| Table complete | Render the skeleton; expect operands, not registers | `scripts/vmp_diff_harness.py emit-smali` |
|
||||
| `resized` / `stripped` / stub verdict | Stop and say so | §7 |
|
||||
@@ -9,9 +9,18 @@ structure before trusting); this script is the tool that rule was missing.
|
||||
|
||||
Per file it reports: size, sha256, dex version, checksum/signature verdicts
|
||||
(`dexutil.verify_dex_header`), class count, method-body census (no-code / real /
|
||||
trivial-stub / empty), the trivial-body ratio that flags an extraction-shell
|
||||
skeleton, and --find pattern hits across the string table. Files are grouped by
|
||||
sha256 and the survivors are ranked: most likely original first.
|
||||
trivial-stub / nop-erased / minimal-form / empty), the stub and emptied ratios that
|
||||
flag an extraction-shell skeleton, and --find pattern hits across the string table.
|
||||
Files are grouped by sha256 and the survivors are ranked: most likely original first.
|
||||
|
||||
**Read `emptied%`, not `stub%`, for the skeleton call.** The two differ, and the
|
||||
difference is load-bearing: `stub%` counts only bodies left as a lone `return*`, while
|
||||
`emptied%` also counts bodies wiped to nothing but nops. A shell that clears the slot
|
||||
instead of writing a return scores `stub% = 0.0` on a fully emptied image — measured,
|
||||
and it used to be ranked the *most likely original* because of it. `stub%` is also
|
||||
bimodal in practice: it lands at the app's own baseline or at ~100 %, with no band in
|
||||
between, and partial extraction (75 % of bodies removed) moves it by 0.2 points. Full
|
||||
matrix and commands: `docs/tool-verification/EXTENSION-extraction-shell-bench.md`.
|
||||
|
||||
Exit code 0 unless nothing in the input parses as a dex at all (exit 1).
|
||||
|
||||
@@ -36,12 +45,34 @@ RETURN_OPS = {0x0E, 0x0F, 0x10, 0x11} # return-void / return / return-wide / re
|
||||
|
||||
|
||||
def classify_body(data, code_off):
|
||||
"""Classify one code_item's instruction stream: 'empty' | 'stub' | 'real'.
|
||||
"""Classify one code_item's instruction stream.
|
||||
|
||||
'stub' = skip plain nop units (0x0000) and exactly one return* remains. That is
|
||||
what an extraction shell leaves behind when it defers decryption to first
|
||||
invocation, and what a repair fixture writes when mimicking one. Anything with a
|
||||
second meaningful unit is real code, however small.
|
||||
Returns one of 'empty' | 'stub' | 'erased' | 'malformed' | 'real'.
|
||||
|
||||
'stub' -- skip plain nop units (0x0000) and exactly one return* remains.
|
||||
That is what an extraction shell leaves behind when it defers
|
||||
decryption to first invocation, and what a repair fixture writes
|
||||
when mimicking one.
|
||||
'erased' -- insns_size > 0 but every unit is 0x0000. Measured on a fixture:
|
||||
a body emptied by nop fill lands here, and reporting it as 'real'
|
||||
(the pre-2026-09 behaviour) inverted the ranking, because a fully
|
||||
nop-filled skeleton scored stub% = 0.0 and was named the most
|
||||
likely original. A real compiler never emits a whole body of
|
||||
nops, so this is skeleton evidence, not code.
|
||||
'minimal' -- a body truncated to `const/4 vR, #0; return vR` and nop-padded,
|
||||
with a non-zero high byte on the return. This is a *legal* minimal
|
||||
non-void body, not a malformed one -- and it is a skeleton shape
|
||||
the stub detector is blind to by design. Counted separately so the
|
||||
blind spot is visible in the report instead of hiding inside
|
||||
'real'. Measured: a fixture with all 5,061 bodies cut to this form
|
||||
reports stub% = 1.9%, the same as its untouched control.
|
||||
'real' -- anything else, including a body of a single non-return unit.
|
||||
|
||||
A two-unit `const/4 v0, #0; return v0` is deliberately **not** a stub: it is what
|
||||
a *legal* minimal body looks like, and treating it as skeleton evidence would
|
||||
make every tiny accessor a false positive. That asymmetry is the detector's
|
||||
designed boundary, and it is measured in
|
||||
`docs/tool-verification/EXTENSION-extraction-shell-bench.md`.
|
||||
"""
|
||||
insns_size = u32(data, code_off + 12)
|
||||
if insns_size == 0:
|
||||
@@ -55,14 +86,27 @@ def classify_body(data, code_off):
|
||||
continue
|
||||
meaningful += 1
|
||||
last_unit = unit
|
||||
if meaningful > 1:
|
||||
return "real" # early exit: two units can never be a stub
|
||||
if meaningful > 2:
|
||||
return "real" # three units can never be a stub or a minimal
|
||||
if meaningful == 0:
|
||||
return "erased" # every unit was 0x0000
|
||||
if meaningful == 1:
|
||||
op = last_unit & 0xFF # first byte holds the opcode for 10x/11x
|
||||
if op == 0x0E and last_unit == 0x000E: # return-void, no register
|
||||
return "stub"
|
||||
if op == 0x0E:
|
||||
# 10x return-void has no operand: `0xNN0E` with NN != 0 is `return vNN`,
|
||||
# i.e. a non-void return wearing a return-void opcode -- minimal form.
|
||||
return "stub" if last_unit == 0x000E else "minimal"
|
||||
if op in (0x0F, 0x10, 0x11): # 11x returns: high byte is the register
|
||||
return "stub"
|
||||
return "real"
|
||||
# meaningful == 2: legal minimal body only when it is `const/4 vR,#0; return* vR`
|
||||
first_unit = 0
|
||||
for k in range(insns_size):
|
||||
if u16(data, base + k * 2):
|
||||
first_unit = u16(data, base + k * 2)
|
||||
break
|
||||
if (first_unit & 0xFF) == 0x12 and (last_unit & 0xFF) in (0x0E, 0x0F, 0x10, 0x11):
|
||||
return "minimal"
|
||||
return "real"
|
||||
|
||||
|
||||
@@ -161,7 +205,7 @@ def profile_dex(path, patterns, trim=False):
|
||||
prof["signature_ok"] = sig_ok
|
||||
|
||||
prof["classes"] = header["class_defs_size"]
|
||||
no_code = with_code = stubs = empties = 0
|
||||
no_code = with_code = stubs = empties = erased = minimal = 0
|
||||
try:
|
||||
for code_off in walk_methods(data, header):
|
||||
if code_off == 0: # abstract / native declaration
|
||||
@@ -175,14 +219,26 @@ def profile_dex(path, patterns, trim=False):
|
||||
stubs += 1
|
||||
elif kind == "empty":
|
||||
empties += 1
|
||||
elif kind == "erased":
|
||||
erased += 1
|
||||
elif kind == "minimal":
|
||||
minimal += 1
|
||||
except Exception as exc: # desynced class_data: keep counts
|
||||
prof["walk_error"] = str(exc)
|
||||
prof["methods_no_code"] = no_code
|
||||
prof["methods_with_code"] = with_code
|
||||
prof["bodies_real"] = with_code - stubs - empties
|
||||
prof["bodies_real"] = with_code - stubs - empties - erased - minimal
|
||||
prof["bodies_stub"] = stubs
|
||||
prof["bodies_empty"] = empties
|
||||
prof["bodies_erased"] = erased
|
||||
prof["bodies_minimal"] = minimal
|
||||
# trivial_ratio keeps its original definition (return*-stub share of bodies) so
|
||||
# existing records stay comparable. The skeleton signal is the union of every
|
||||
# measured emptied shape; a body wiped to nothing at all must never score lower
|
||||
# than a body wiped to `return-void`, or the ranking inverts (see
|
||||
# docs/tool-verification/EXTENSION-extraction-shell-bench.md).
|
||||
prof["trivial_ratio"] = (stubs / with_code) if with_code else 0.0
|
||||
prof["emptied_ratio"] = ((stubs + erased) / with_code) if with_code else 0.0
|
||||
|
||||
if patterns:
|
||||
try:
|
||||
@@ -207,7 +263,38 @@ def collect_files(paths):
|
||||
|
||||
|
||||
def rank_key(prof):
|
||||
return (prof["trivial_ratio"],
|
||||
"""Sort key: most-likely-original first.
|
||||
|
||||
Both body signals are needed, in the right order, and each was measured to fail
|
||||
alone:
|
||||
|
||||
* the `return*`-stub share alone let a **nop-filled** skeleton score 0.0 and be
|
||||
named the most likely original, because a wiped body counts as neither a stub
|
||||
nor real code;
|
||||
* `emptied_ratio` alone still failed, because it is **bimodal** -- it sits at the
|
||||
host app's own baseline or at ~100 %, with nothing in between. A dex with 25 %
|
||||
of its bodies emptied measures *below* its own untouched control, 1.7 % against
|
||||
1.9 %, so the sort pointed at a modified image and called it the original;
|
||||
* `minimal + erased` alone failed too, and in the opposite direction: a skeleton
|
||||
that writes a bare `return-void` into every body has minimal = erased = 0 and
|
||||
would sort first, despite `emptied%` = 100.
|
||||
|
||||
So the key combines both, and the combination has to let *either* signal disqualify
|
||||
an image rather than letting them tie:
|
||||
|
||||
* level 1 -- `emptied_ratio >= 0.5`. Nothing that is mostly emptied is a candidate,
|
||||
whatever else it scores. This is the level that evicts the two skeletons the
|
||||
plain counts cannot see: an image with a bare `return-void` in every body has
|
||||
minimal = erased = 0, so a pure-count key ranked it first.
|
||||
* level 2 -- `minimal + erased`, the skeleton-evidence count, low-first. This is
|
||||
what orders the **bimodal band**, where `emptied_ratio` cannot: the untouched
|
||||
control carries 82, and the count is monotone as bodies are removed (1299 /
|
||||
2524 / 3736 / 4946 at 25 / 50 / 75 / 100 %).
|
||||
* level 3 -- `emptied_ratio` low-first, to separate what is left.
|
||||
"""
|
||||
return (1 if prof.get("emptied_ratio", prof["trivial_ratio"]) >= 0.5 else 0,
|
||||
prof.get("bodies_minimal", 0) + prof.get("bodies_erased", 0),
|
||||
prof.get("emptied_ratio", prof["trivial_ratio"]),
|
||||
0 if prof["checksum_ok"] else 1,
|
||||
0 if prof["signature_ok"] else 1,
|
||||
prof["name"])
|
||||
@@ -248,9 +335,9 @@ def main(argv=None):
|
||||
# ---- human-readable report -------------------------------------------------
|
||||
print("== dex dump validation: %d file(s), %d parse, %d rejected =="
|
||||
% (len(profiles), len(valid), len(profiles) - len(valid)))
|
||||
fmt = "%-28s %9s %-12s %3s %-7s %-7s %6s %5s %6s %7s"
|
||||
fmt = "%-28s %9s %-12s %3s %-7s %-7s %6s %6s %6s %7s %7s %7s"
|
||||
print(fmt % ("name", "size", "sha256[:12]", "ver", "cksum", "sig",
|
||||
"class", "noco", "code", "stub%"))
|
||||
"class", "noco", "code", "stub%", "erased%", "emptied%"))
|
||||
for p in profiles:
|
||||
if "error" in p:
|
||||
print("%-28s %9s %-12s %3s REJECTED: %s"
|
||||
@@ -261,12 +348,19 @@ def main(argv=None):
|
||||
"ok" if p["checksum_ok"] else "BAD",
|
||||
"ok" if p["signature_ok"] else "BAD",
|
||||
p["classes"], p["methods_no_code"], p["methods_with_code"],
|
||||
"%.1f%%" % (100.0 * p["trivial_ratio"])))
|
||||
"%.1f%%" % (100.0 * p["trivial_ratio"]),
|
||||
"%.1f%%" % (100.0 * (p["bodies_erased"] / p["methods_with_code"]
|
||||
if p["methods_with_code"] else 0.0)),
|
||||
"%.1f%%" % (100.0 * p.get("emptied_ratio", 0.0))))
|
||||
if p.get("trimmed_from"):
|
||||
print("%-28s trimmed %d -> %d B (page-aligned dump)"
|
||||
% ("", p["trimmed_from"], p["trimmed_to"]))
|
||||
if p.get("walk_error"):
|
||||
print("%-28s walk stopped early: %s" % ("", p["walk_error"]))
|
||||
if p.get("bodies_minimal"):
|
||||
print("%-28s minimal-form bodies: %d (const/4+return truncation -- a "
|
||||
"skeleton shape the stub%% column cannot see)"
|
||||
% ("", p["bodies_minimal"]))
|
||||
for pat, n in sorted(p.get("find_hits", {}).items()):
|
||||
print("%-28s find %-24s %d" % ("", pat, n))
|
||||
|
||||
@@ -283,18 +377,32 @@ def main(argv=None):
|
||||
print("\nranking (most likely original first):")
|
||||
for i, p in enumerate(ranking, 1):
|
||||
note = ""
|
||||
if p["trivial_ratio"] >= 0.5:
|
||||
note = " <-- SKELETON: stub%%=%.0f, extraction-shell shape" \
|
||||
% (100.0 * p["trivial_ratio"])
|
||||
print(" %d. %-28s stub%%=%.1f cksum=%s sig=%s classes=%d%s"
|
||||
if p.get("emptied_ratio", 0.0) >= 0.5:
|
||||
note = (" <-- SKELETON: emptied%%=%.0f, extraction-shell shape"
|
||||
% (100.0 * p["emptied_ratio"]))
|
||||
elif p.get("bodies_erased", 0):
|
||||
note = (" <-- %d body(ies) wiped to nops: skeleton evidence"
|
||||
% p["bodies_erased"])
|
||||
print(" %d. %-28s stub%%=%.1f erased%%=%.1f min/erased=%d cksum=%s "
|
||||
"sig=%s classes=%d%s"
|
||||
% (i, p["name"], 100.0 * p["trivial_ratio"],
|
||||
100.0 * (p["bodies_erased"] / p["methods_with_code"]
|
||||
if p["methods_with_code"] else 0.0),
|
||||
p.get("bodies_minimal", 0) + p.get("bodies_erased", 0),
|
||||
"ok" if p["checksum_ok"] else "BAD",
|
||||
"ok" if p["signature_ok"] else "BAD",
|
||||
p["classes"], note))
|
||||
top = ranking[0]
|
||||
copies = len(groups.get(top["sha256"], []))
|
||||
print("\nverdict: %s is the most likely original (%d copy(ies) in this set)"
|
||||
% (top["name"], copies))
|
||||
print("\nverdict: %s is the best-supported candidate for the original "
|
||||
"(%d copy(ies) in this set)" % (top["name"], copies))
|
||||
print("ranked by: mostly-emptied images evicted first (emptied%% >= 50), then "
|
||||
"the minimal+nop-erased body count low-first, then emptied%%.\n"
|
||||
" Limits, stated rather than hidden: this cannot see a `throw`-stub "
|
||||
"skeleton (it scores at the control's own baseline), and a\n"
|
||||
" partially extracted image still outranks a heavily stubbed one. "
|
||||
"Confirm the winner before using it as a patch baseline --\n"
|
||||
" docs/tool-verification/EXTENSION-extraction-shell-bench.md")
|
||||
else:
|
||||
print("\nverdict: no image parsed as a dex -- nothing to rank")
|
||||
return 0 if valid else 1
|
||||
|
||||
@@ -16,10 +16,24 @@ Feeding the extracted images to `dex_dump_validate.py` is the next step: this
|
||||
script only *finds* and *cuts*; that one judges whether what came out is a real
|
||||
body or an extraction-shell skeleton.
|
||||
|
||||
**Measured hit rate on the reference device: zero for anonymous regions.** The
|
||||
root-dump pass recorded in `docs/tool-verification/EXTENSION-rootdump.md` scanned
|
||||
142 anonymous mappings and returned **no hits**, while the *named* ART dex mappings
|
||||
in the same process yielded 17 dex images without any search at all. A negative
|
||||
result from this script is therefore weak evidence: it is compatible with "the
|
||||
payload is not resident", "the payload is resident but not as a contiguous
|
||||
dex-magic image", and "the payload was paged out between capture and scan". Treat a
|
||||
zero-hit scan as a reason to check the named mappings first
|
||||
(`[anon:dalvik-classes*.dex extracted in memory from <src>]`), not as a conclusion
|
||||
about the packer. A positive hit inside a genuinely unnamed mapping has not been
|
||||
observed here yet.
|
||||
|
||||
Scope note: the header's `file_size` field decides the cut. When the declared size
|
||||
does not fit inside the capture, the hit is reported and skipped rather than
|
||||
guessed at -- a dex whose tail is missing is not recoverable by truncating the
|
||||
wrong end.
|
||||
wrong end. Page-aligned captures from a VMA routinely overrun the image they hold,
|
||||
so a run against raw VMA exports usually needs `--keep-partial` or a trim step
|
||||
before `dex_dump_validate.py` will accept the output.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
|
||||
@@ -0,0 +1,960 @@
|
||||
#!/usr/bin/env python3
|
||||
"""java2c_probe.py - collect the evidence that decides HOW a target was hardened.
|
||||
|
||||
The question this answers is narrow on purpose. Faced with "the Java methods have
|
||||
no bodies", four different mechanisms produce that observation and they need four
|
||||
different routes:
|
||||
|
||||
Java2C Java bytecode translated to C, compiled into a .so. No dex
|
||||
bytecode exists at runtime, ever. Route: read the .so.
|
||||
Extraction shell Real dex bytecode, encrypted at rest, decrypted at runtime.
|
||||
Route: dump memory, then validate the dump.
|
||||
VMP Real dex bytecode replaced by private opcodes fed to an
|
||||
interpreter. Route: measure, then usually stop.
|
||||
JNI sinking A few hot methods deliberately moved to native by hand.
|
||||
Route: reverse the .so; the dex is still readable.
|
||||
|
||||
The expensive mistake is reading the first shape as the second: the analyst goes
|
||||
looking for a decrypted DEX that is never produced, because nothing was ever
|
||||
encrypted. This script prints the discriminating evidence and labels each item
|
||||
strong / medium / weak so the reading is not mistaken for a verdict.
|
||||
|
||||
Usage (paths are always explicit; nothing is discovered implicitly):
|
||||
|
||||
python java2c_probe.py --apk sample.apk
|
||||
python java2c_probe.py --apk sample.apk --json
|
||||
python java2c_probe.py --dex classes.dex --so lib/arm64-v8a/libnc.so
|
||||
python java2c_probe.py --dir extracted/ # every *.dex and *.so below
|
||||
|
||||
Exit codes: 0 = evidence collected, 1 = no usable input, 2 = argument error.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import struct
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# dex parsing (self-contained; deliberately does not import the repo's dexutil)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
ACC_NATIVE = 0x0100
|
||||
ACC_ABSTRACT = 0x0400
|
||||
|
||||
# Instruction length in 16-bit code units, for the subset that a stub or a
|
||||
# decompiled-from-C body produces. Unknown opcodes return 1 so a counting error
|
||||
# can never inflate the trivial-body ratio; see the note on that metric below.
|
||||
_INSN_UNITS = {
|
||||
0x00: 1, 0x01: 1, 0x02: 1, 0x03: 1, 0x04: 1, 0x05: 1, 0x06: 1, 0x07: 1,
|
||||
0x08: 1, 0x09: 1, 0x0a: 1, 0x0b: 1, 0x0c: 1, 0x0d: 1, 0x0e: 1, 0x0f: 1,
|
||||
0x10: 1, 0x11: 1, 0x12: 1, 0x13: 2, 0x14: 3, 0x15: 2, 0x16: 2, 0x17: 3,
|
||||
0x18: 5, 0x19: 2, 0x1a: 2, 0x1b: 3, 0x1c: 2, 0x1d: 1, 0x1e: 1, 0x1f: 1,
|
||||
0x20: 1, 0x21: 1, 0x22: 1, 0x23: 1, 0x24: 3, 0x25: 3, 0x26: 3, 0x27: 1,
|
||||
0x28: 1, 0x29: 1, 0x2a: 1, 0x2b: 1, 0x2c: 1, 0x2d: 1, 0x2e: 1, 0x2f: 1,
|
||||
0x30: 1, 0x31: 1, 0x32: 1, 0x33: 1, 0x34: 1, 0x35: 1, 0x36: 1, 0x37: 1,
|
||||
0x38: 1, 0x39: 1, 0x3a: 1, 0x3b: 1, 0x3c: 1, 0x3d: 1, 0x3e: 1, 0x3f: 1,
|
||||
0x40: 1, 0x41: 1, 0x42: 1, 0x43: 1, 0x44: 2, 0x45: 2, 0x46: 2, 0x47: 2,
|
||||
0x48: 2, 0x49: 2, 0x4a: 2, 0x4b: 2, 0x4c: 2, 0x4d: 2, 0x4e: 2, 0x4f: 2,
|
||||
0x50: 2, 0x51: 2, 0x52: 2, 0x53: 2, 0x54: 2, 0x55: 2, 0x56: 2, 0x57: 2,
|
||||
0x58: 2, 0x59: 2, 0x5a: 2, 0x5b: 2, 0x5c: 2, 0x5d: 2, 0x5e: 2, 0x5f: 2,
|
||||
0x60: 2, 0x61: 2, 0x62: 2, 0x63: 2, 0x64: 2, 0x65: 2, 0x66: 2, 0x67: 2,
|
||||
0x68: 2, 0x69: 2, 0x6a: 2, 0x6b: 2, 0x6c: 2, 0x6d: 2, 0x6e: 3, 0x6f: 3,
|
||||
0x70: 3, 0x71: 3, 0x72: 3, 0x73: 1, 0x74: 1, 0x75: 1, 0x76: 1, 0x77: 1,
|
||||
0x78: 2, 0x79: 2, 0x7a: 2, 0x7b: 2, 0x7c: 2, 0x7d: 2, 0x7e: 2, 0x7f: 2,
|
||||
0x80: 2, 0x81: 2, 0x82: 2, 0x83: 2, 0x84: 2, 0x85: 2, 0x86: 2, 0x87: 2,
|
||||
0x88: 2, 0x89: 2, 0x8a: 2, 0x8b: 2, 0x8c: 2, 0x8d: 2, 0x8e: 2, 0x8f: 2,
|
||||
0x90: 2, 0x91: 2, 0x92: 2, 0x93: 2, 0x94: 2, 0x95: 2, 0x96: 2, 0x97: 2,
|
||||
0x98: 2, 0x99: 2, 0x9a: 2, 0x9b: 2, 0x9c: 2, 0x9d: 2, 0x9e: 2, 0x9f: 2,
|
||||
0xa0: 2, 0xa1: 2, 0xa2: 2, 0xa3: 2, 0xa4: 2, 0xa5: 2, 0xa6: 2, 0xa7: 2,
|
||||
0xa8: 2, 0xa9: 2, 0xaa: 2, 0xab: 2, 0xac: 2, 0xad: 2, 0xae: 2, 0xaf: 2,
|
||||
0xb0: 1, 0xb1: 1, 0xb2: 2, 0xb3: 2, 0xb4: 2, 0xb5: 2, 0xb6: 2, 0xb7: 2,
|
||||
0xb8: 2, 0xb9: 2, 0xba: 2, 0xbb: 2, 0xbc: 2, 0xbd: 2, 0xbe: 2, 0xbf: 2,
|
||||
0xc0: 2, 0xc1: 2, 0xc2: 2, 0xc3: 2, 0xc4: 2, 0xc5: 2, 0xc6: 2, 0xc7: 2,
|
||||
0xc8: 2, 0xc9: 2, 0xca: 2, 0xcb: 2, 0xcc: 2, 0xcd: 2, 0xce: 2, 0xcf: 2,
|
||||
0xd0: 2, 0xd1: 2, 0xd2: 2, 0xd3: 2, 0xd4: 2, 0xd5: 2, 0xd6: 2, 0xd7: 2,
|
||||
0xd8: 2, 0xd9: 2, 0xda: 2, 0xdb: 2, 0xdc: 2, 0xdd: 2, 0xde: 2, 0xdf: 2,
|
||||
0xe0: 2, 0xe1: 2, 0xe2: 2, 0xe3: 0, 0xe4: 0, 0xe5: 0, 0xe6: 0, 0xe7: 0,
|
||||
0xe8: 0, 0xe9: 0, 0xea: 0, 0xeb: 0, 0xec: 0, 0xed: 0, 0xee: 0, 0xef: 0,
|
||||
0xf0: 0, 0xf1: 0, 0xf2: 0, 0xf3: 0, 0xf4: 0, 0xf5: 0, 0xf6: 0, 0xf7: 0,
|
||||
0xf8: 0, 0xf9: 0, 0xfa: 0, 0xfb: 0, 0xfc: 0, 0xfd: 0, 0xfe: 0, 0xff: 0,
|
||||
}
|
||||
|
||||
_RETURN_OPS = (0x0e, 0x0f, 0x10, 0x11)
|
||||
|
||||
|
||||
def _uleb128(buf, pos):
|
||||
result = 0
|
||||
shift = 0
|
||||
while True:
|
||||
if pos >= len(buf):
|
||||
raise ValueError("uleb128 truncated")
|
||||
byte = buf[pos]
|
||||
pos += 1
|
||||
result |= (byte & 0x7F) << shift
|
||||
if not (byte & 0x80):
|
||||
return result, pos
|
||||
shift += 7
|
||||
if shift > 35:
|
||||
raise ValueError("uleb128 too long")
|
||||
|
||||
|
||||
class Dex(object):
|
||||
"""Minimal structural reader: header, class_defs, class_data, code_item."""
|
||||
|
||||
def __init__(self, data, name):
|
||||
self.data = data
|
||||
self.name = name
|
||||
self.header_ok = False
|
||||
self.header_note = ""
|
||||
self.methods = [] # dicts: class, name, flags, code_off, units
|
||||
self.class_names = []
|
||||
self.native_methods = 0
|
||||
self.abstract_methods = 0
|
||||
self.body_methods = 0
|
||||
self.no_code_methods = 0 # neither native nor abstract, yet code_off == 0
|
||||
self.trivial_bodies = 0
|
||||
self.native_with_code = 0
|
||||
self.fully_native_classes = 0
|
||||
self.native_dominated_classes = 0
|
||||
self.parse_error = None
|
||||
self._parse()
|
||||
|
||||
def _u16(self, off):
|
||||
return struct.unpack_from("<H", self.data, off)[0]
|
||||
|
||||
def _u32(self, off):
|
||||
return struct.unpack_from("<I", self.data, off)[0]
|
||||
|
||||
def _parse(self):
|
||||
d = self.data
|
||||
if len(d) < 112 or d[:4] != b"dex\n":
|
||||
self.parse_error = "not a dex image (bad magic or too short)"
|
||||
return
|
||||
try:
|
||||
file_size = self._u32(0x20)
|
||||
map_off = self._u32(0x34)
|
||||
string_ids_size = self._u32(0x38)
|
||||
string_ids_off = self._u32(0x3C)
|
||||
type_ids_size = self._u32(0x40)
|
||||
type_ids_off = self._u32(0x44)
|
||||
proto_ids_size = self._u32(0x48)
|
||||
proto_ids_off = self._u32(0x4C)
|
||||
method_ids_size = self._u32(0x58)
|
||||
method_ids_off = self._u32(0x5C)
|
||||
class_defs_size = self._u32(0x60)
|
||||
class_defs_off = self._u32(0x64)
|
||||
except Exception as exc: # pragma: no cover
|
||||
self.parse_error = "header read failed: %s" % exc
|
||||
return
|
||||
|
||||
# Header claims vs. reality. A truncated or padded capture is the normal
|
||||
# way this probe is handed bad input, so say which one happened.
|
||||
if file_size != len(d):
|
||||
self.header_note = "header file_size=%d actual=%d" % (file_size, len(d))
|
||||
else:
|
||||
self.header_ok = True
|
||||
|
||||
if not (0 < string_ids_size < 1 << 22 and string_ids_off + string_ids_size * 4 <= len(d)):
|
||||
self.parse_error = "string_ids table out of bounds"
|
||||
return
|
||||
|
||||
string_offsets = [self._u32(string_ids_off + 4 * i) for i in range(string_ids_size)]
|
||||
|
||||
def string_at(idx):
|
||||
if idx < 0 or idx >= len(string_offsets):
|
||||
return "<bad-string-idx-%d>" % idx
|
||||
off = string_offsets[idx]
|
||||
_, pos = _uleb128(d, off) # utf16 length, ignored
|
||||
end = d.find(b"\x00", pos)
|
||||
if end < 0:
|
||||
return "<unterminated>"
|
||||
return d[pos:end].decode("utf-8", "replace")
|
||||
|
||||
try:
|
||||
type_desc = [string_at(self._u32(type_ids_off + 4 * i))
|
||||
for i in range(type_ids_size)]
|
||||
except Exception as exc:
|
||||
self.parse_error = "type_ids read failed: %s" % exc
|
||||
return
|
||||
|
||||
try:
|
||||
if method_ids_off + method_ids_size * 8 > len(d):
|
||||
raise ValueError("method_ids out of bounds")
|
||||
method_info = []
|
||||
for i in range(method_ids_size):
|
||||
base = method_ids_off + 8 * i
|
||||
class_idx = self._u16(base)
|
||||
name_idx = self._u32(base + 4)
|
||||
desc = type_desc[class_idx] if class_idx < len(type_desc) else "?"
|
||||
method_info.append((desc, string_at(name_idx)))
|
||||
except Exception as exc:
|
||||
self.parse_error = "method_ids read failed: %s" % exc
|
||||
return
|
||||
|
||||
if class_defs_off + class_defs_size * 32 > len(d):
|
||||
self.parse_error = "class_defs table out of bounds"
|
||||
return
|
||||
|
||||
per_class_native = {}
|
||||
per_class_total = {}
|
||||
|
||||
for ci in range(class_defs_size):
|
||||
base = class_defs_off + 32 * ci
|
||||
class_idx = self._u32(base)
|
||||
class_data_off = self._u32(base + 24)
|
||||
cname = type_desc[class_idx] if class_idx < len(type_desc) else "?%d" % class_idx
|
||||
self.class_names.append(cname)
|
||||
|
||||
if class_data_off == 0:
|
||||
continue
|
||||
try:
|
||||
pos = class_data_off
|
||||
static_fields, pos = _uleb128(d, pos)
|
||||
instance_fields, pos = _uleb128(d, pos)
|
||||
direct_methods, pos = _uleb128(d, pos)
|
||||
virtual_methods, pos = _uleb128(d, pos)
|
||||
|
||||
for _ in range(static_fields + instance_fields):
|
||||
_, pos = _uleb128(d, pos) # field_idx_diff
|
||||
_, pos = _uleb128(d, pos) # access_flags
|
||||
|
||||
# direct_methods and virtual_methods are two independent encoded
|
||||
# lists: each one's first method_idx_diff is relative to 0. Walking
|
||||
# them as a single run silently mis-attributes every virtual
|
||||
# method, so they are decoded as two passes.
|
||||
for list_size in (direct_methods, virtual_methods):
|
||||
method_idx = 0
|
||||
for _ in range(list_size):
|
||||
diff, pos = _uleb128(d, pos)
|
||||
flags, pos = _uleb128(d, pos)
|
||||
code_off, pos = _uleb128(d, pos)
|
||||
method_idx += diff
|
||||
|
||||
if method_idx < len(method_info):
|
||||
mcls, mname = method_info[method_idx]
|
||||
else:
|
||||
mcls, mname = "?", "?%d" % method_idx
|
||||
|
||||
units = None
|
||||
if code_off:
|
||||
units = self._u16(code_off + 12) # insns_size
|
||||
|
||||
self.methods.append({
|
||||
"class": mcls, "name": mname, "flags": flags,
|
||||
"code_off": code_off, "units": units,
|
||||
})
|
||||
# Constructors are excluded from the per-class shape: a
|
||||
# Java2C pass leaves <init>/<clinit> in Java in practice,
|
||||
# and counting them would make "all methods native" an
|
||||
# essentially unreachable test.
|
||||
named = mname not in ("<init>", "<clinit>")
|
||||
if named:
|
||||
per_class_total[cname] = per_class_total.get(cname, 0) + 1
|
||||
|
||||
if flags & ACC_NATIVE:
|
||||
self.native_methods += 1
|
||||
if named:
|
||||
per_class_native[cname] = per_class_native.get(cname, 0) + 1
|
||||
if code_off:
|
||||
self.native_with_code += 1
|
||||
elif flags & ACC_ABSTRACT:
|
||||
self.abstract_methods += 1
|
||||
elif code_off == 0:
|
||||
self.no_code_methods += 1
|
||||
else:
|
||||
self.body_methods += 1
|
||||
if self._is_trivial(code_off, units):
|
||||
self.trivial_bodies += 1
|
||||
except Exception:
|
||||
# One unreadable class_data must not void the whole measurement.
|
||||
continue
|
||||
|
||||
for cname, total in per_class_total.items():
|
||||
if total < 3:
|
||||
continue
|
||||
native_here = per_class_native.get(cname, 0)
|
||||
if native_here == total:
|
||||
self.fully_native_classes += 1
|
||||
if native_here >= 0.7 * total:
|
||||
self.native_dominated_classes += 1
|
||||
|
||||
def _is_trivial(self, code_off, units):
|
||||
"""A body that only returns a constant: the classic stub shape.
|
||||
|
||||
Approximation, and labelled as such: it walks the instruction stream with
|
||||
the length table above and accepts only nop/move/const/return. Anything
|
||||
that touches a field, calls out, or branches is not a stub.
|
||||
"""
|
||||
if not units:
|
||||
return False
|
||||
insns_off = code_off + 16
|
||||
if insns_off + units * 2 > len(self.data):
|
||||
return False
|
||||
try:
|
||||
pos = 0
|
||||
seen = 0
|
||||
while pos < units and seen < 8:
|
||||
word = struct.unpack_from("<H", self.data, insns_off + pos * 2)[0]
|
||||
op = word & 0xFF
|
||||
if op in _RETURN_OPS:
|
||||
return True if seen <= 2 else False
|
||||
if op == 0x00 or 0x01 <= op <= 0x09 or 0x12 <= op <= 0x19:
|
||||
step = _INSN_UNITS.get(op, 1)
|
||||
if step == 0:
|
||||
return False
|
||||
pos += step
|
||||
seen += 1
|
||||
continue
|
||||
return False
|
||||
return False
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
# -- derived metrics ---------------------------------------------------
|
||||
@property
|
||||
def total_methods(self):
|
||||
return len(self.methods)
|
||||
|
||||
@property
|
||||
def native_ratio(self):
|
||||
return (float(self.native_methods) / self.total_methods) if self.total_methods else 0.0
|
||||
|
||||
@property
|
||||
def trivial_ratio(self):
|
||||
return (float(self.trivial_bodies) / self.body_methods) if self.body_methods else 0.0
|
||||
|
||||
def summary(self):
|
||||
return {
|
||||
"name": self.name,
|
||||
"size": len(self.data),
|
||||
"header_size_matches": self.header_ok,
|
||||
"header_note": self.header_note,
|
||||
"classes": len(self.class_names),
|
||||
"total_methods": self.total_methods,
|
||||
"native_methods": self.native_methods,
|
||||
"native_ratio": round(self.native_ratio, 4),
|
||||
"abstract_methods": self.abstract_methods,
|
||||
"body_methods": self.body_methods,
|
||||
"no_code_methods": self.no_code_methods,
|
||||
"native_with_code": self.native_with_code,
|
||||
"trivial_bodies": self.trivial_bodies,
|
||||
"trivial_ratio": round(self.trivial_ratio, 4),
|
||||
"fully_native_classes": self.fully_native_classes,
|
||||
"native_dominated_classes": self.native_dominated_classes,
|
||||
"bytes_per_class": int(len(self.data) / len(self.class_names)) if self.class_names else 0,
|
||||
"parse_error": self.parse_error,
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ELF parsing (self-contained; no lief / pyelftools dependency)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
# Each entry: (regex, label, strength, meaning). Strength is what stops a weak
|
||||
# hit from being read as a verdict - see the printed weak-criteria block.
|
||||
_STRINGSIGS = [
|
||||
(rb"Dex2C", "Dex2C", "strong", "dcc toolchain marker in code/rodata"),
|
||||
(rb"dynamic_register_compile_methods", "dcc-register-fn", "strong",
|
||||
"dcc's generated RegisterNatives entry point"),
|
||||
(rb"dcc", "dcc", "weak", "3 letters: matches unrelated words (e.g. 'addcc')"),
|
||||
(rb"JNI_OnLoad", "JNI_OnLoad", "weak", "present in almost every JNI library"),
|
||||
(rb"RegisterNatives", "RegisterNatives", "weak",
|
||||
"dynamic registration; ordinary JNI libraries use it too"),
|
||||
(rb"libc\+\+_shared\.so", "libc++_shared", "weak", "NDK C++ runtime, not a hardening marker"),
|
||||
(rb"c\+\+_static", "c++_static", "weak", "dcc's default APP_STL; also a common NDK setting"),
|
||||
(rb"FindClass", "FindClass", "weak", "any JNI callback path"),
|
||||
(rb"GetMethodID", "GetMethodID", "weak", "any JNI callback path"),
|
||||
(rb"GetStaticMethodID", "GetStaticMethodID", "weak", "any JNI callback path"),
|
||||
(rb"CallObjectMethod", "CallObjectMethod", "weak", "any JNI callback path"),
|
||||
(rb"CallStaticObjectMethod", "CallStaticObjectMethod", "weak", "any JNI callback path"),
|
||||
(rb"NewLocalRef", "NewLocalRef", "weak", "generated code that manages refs by hand"),
|
||||
(rb"ScopedLocalRef", "ScopedLocalRef", "medium", "dcc runtime header name"),
|
||||
(rb"well_known_classes", "well_known_classes", "medium", "dcc runtime header name"),
|
||||
]
|
||||
|
||||
_JNI_CALLS = ("FindClass", "GetMethodID", "GetStaticMethodID", "CallObjectMethod",
|
||||
"CallStaticObjectMethod", "CallIntMethod", "CallVoidMethod",
|
||||
"CallStaticIntMethod", "NewObject", "GetFieldID")
|
||||
|
||||
|
||||
class Elf(object):
|
||||
"""Enough of ELF to answer: what is exported, what is imported, what is inside."""
|
||||
|
||||
def __init__(self, data, name):
|
||||
self.data = data
|
||||
self.name = name
|
||||
self.arch = "unknown"
|
||||
self.is_elf = False
|
||||
self.dynsym = [] # (name, shndx)
|
||||
self.sig_hits = {} # label -> (count, strength, meaning)
|
||||
self.java_symbols = []
|
||||
self.has_jni_onload = False
|
||||
self.imports_register_natives = False
|
||||
self.jni_call_refs = []
|
||||
self.elf_note = ""
|
||||
self._parse()
|
||||
|
||||
def _parse(self):
|
||||
d = self.data
|
||||
if len(d) < 64 or d[:4] != b"\x7fELF":
|
||||
self.elf_note = "not an ELF image"
|
||||
return
|
||||
self.is_elf = True
|
||||
ei_class = d[4]
|
||||
ei_data = d[5]
|
||||
if ei_class not in (1, 2):
|
||||
self.elf_note = "bad EI_CLASS=%d" % ei_class
|
||||
return
|
||||
endian = "<" if ei_data == 1 else ">"
|
||||
is64 = ei_class == 2
|
||||
try:
|
||||
e_type = struct.unpack_from(endian + "H", d, 16)[0]
|
||||
e_machine = struct.unpack_from(endian + "H", d, 18)[0]
|
||||
if is64:
|
||||
e_shoff = struct.unpack_from(endian + "Q", d, 0x28)[0]
|
||||
e_shentsize = struct.unpack_from(endian + "H", d, 0x3A)[0]
|
||||
e_shnum = struct.unpack_from(endian + "H", d, 0x3C)[0]
|
||||
e_shstrndx = struct.unpack_from(endian + "H", d, 0x3E)[0]
|
||||
else:
|
||||
e_shoff = struct.unpack_from(endian + "I", d, 0x20)[0]
|
||||
e_shentsize = struct.unpack_from(endian + "H", d, 0x2E)[0]
|
||||
e_shnum = struct.unpack_from(endian + "H", d, 0x30)[0]
|
||||
e_shstrndx = struct.unpack_from(endian + "H", d, 0x32)[0]
|
||||
except struct.error as exc:
|
||||
self.elf_note = "header read failed: %s" % exc
|
||||
return
|
||||
|
||||
self.arch = {
|
||||
3: "x86", 8: "mips", 20: "ppc", 40: "arm", 62: "x86_64",
|
||||
183: "aarch64", 243: "riscv",
|
||||
}.get(e_machine, "machine-%d" % e_machine)
|
||||
if e_type not in (2, 3):
|
||||
self.elf_note = "e_type=%d (not EXEC/DYN)" % e_type
|
||||
|
||||
# Strings first: it is the check that works even when the section table
|
||||
# has been stripped, which is the common case for a hardened library.
|
||||
self._scan_strings()
|
||||
|
||||
if e_shoff and e_shnum and e_shoff + e_shnum * e_shentsize <= len(d):
|
||||
self._read_sections(endian, is64, e_shoff, e_shentsize, e_shnum, e_shstrndx)
|
||||
else:
|
||||
self.elf_note = (self.elf_note + "; " if self.elf_note else "") + \
|
||||
"no usable section table (stripped?) - symbol checks skipped"
|
||||
|
||||
def _scan_strings(self):
|
||||
d = self.data
|
||||
for pattern, label, strength, meaning in _STRINGSIGS:
|
||||
count = len(re.findall(pattern, d))
|
||||
if count:
|
||||
self.sig_hits[label] = (count, strength, meaning)
|
||||
for call in _JNI_CALLS:
|
||||
if re.search(re.escape(call).encode(), d):
|
||||
self.jni_call_refs.append(call)
|
||||
|
||||
def _read_sections(self, endian, is64, shoff, shentsize, shnum, shstrndx):
|
||||
d = self.data
|
||||
sections = []
|
||||
for i in range(shnum):
|
||||
base = shoff + i * shentsize
|
||||
try:
|
||||
if is64:
|
||||
name_off = struct.unpack_from(endian + "I", d, base)[0]
|
||||
sh_type = struct.unpack_from(endian + "I", d, base + 4)[0]
|
||||
sh_offset = struct.unpack_from(endian + "Q", d, base + 0x18)[0]
|
||||
sh_size = struct.unpack_from(endian + "Q", d, base + 0x20)[0]
|
||||
sh_link = struct.unpack_from(endian + "I", d, base + 0x28)[0]
|
||||
sh_entsize = struct.unpack_from(endian + "Q", d, base + 0x38)[0]
|
||||
else:
|
||||
name_off = struct.unpack_from(endian + "I", d, base)[0]
|
||||
sh_type = struct.unpack_from(endian + "I", d, base + 4)[0]
|
||||
sh_offset = struct.unpack_from(endian + "I", d, base + 0x10)[0]
|
||||
sh_size = struct.unpack_from(endian + "I", d, base + 0x14)[0]
|
||||
sh_link = struct.unpack_from(endian + "I", d, base + 0x18)[0]
|
||||
sh_entsize = struct.unpack_from(endian + "I", d, base + 0x24)[0]
|
||||
except struct.error:
|
||||
return
|
||||
sections.append((name_off, sh_type, sh_offset, sh_size, sh_link, sh_entsize))
|
||||
|
||||
if shstrndx >= len(sections):
|
||||
return
|
||||
_, _, str_off, str_size, _, _ = sections[shstrndx]
|
||||
if str_off + str_size > len(d):
|
||||
return
|
||||
shstr = d[str_off:str_off + str_size]
|
||||
|
||||
def sec_name(off):
|
||||
end = shstr.find(b"\x00", off)
|
||||
return shstr[off:end if end >= 0 else len(shstr)].decode("utf-8", "replace")
|
||||
|
||||
for name_off, sh_type, sh_offset, sh_size, sh_link, sh_entsize in sections:
|
||||
# SHT_DYNSYM == 11, SHT_SYMTAB == 2
|
||||
if sh_type not in (2, 11):
|
||||
continue
|
||||
if sh_link >= len(sections):
|
||||
continue
|
||||
_, _, dstr_off, dstr_size, _, _ = sections[sh_link]
|
||||
if dstr_off + dstr_size > len(d) or sh_offset + sh_size > len(d):
|
||||
continue
|
||||
dstr = d[dstr_off:dstr_off + dstr_size]
|
||||
entsize = sh_entsize or (24 if is64 else 16)
|
||||
if entsize == 0:
|
||||
continue
|
||||
for off in range(sh_offset, sh_offset + sh_size, entsize):
|
||||
try:
|
||||
if is64:
|
||||
st_name = struct.unpack_from(endian + "I", d, off)[0]
|
||||
st_shndx = struct.unpack_from(endian + "H", d, off + 6)[0]
|
||||
else:
|
||||
st_name = struct.unpack_from(endian + "I", d, off)[0]
|
||||
st_shndx = struct.unpack_from(endian + "H", d, off + 14)[0]
|
||||
except struct.error:
|
||||
break
|
||||
if st_name >= len(dstr):
|
||||
continue
|
||||
end = dstr.find(b"\x00", st_name)
|
||||
sname = dstr[st_name:end if end >= 0 else len(dstr)].decode("utf-8", "replace")
|
||||
if not sname:
|
||||
continue
|
||||
self.dynsym.append((sname, st_shndx))
|
||||
if sname.startswith("Java_"):
|
||||
self.java_symbols.append(sname)
|
||||
if sname == "JNI_OnLoad":
|
||||
self.has_jni_onload = True
|
||||
# Substring match, so a C implementation (bare name) and a C++
|
||||
# one (mangled form) are both caught. In practice NEITHER is
|
||||
# present: see the note on dynamic-registration in classify().
|
||||
if "RegisterNatives" in sname and st_shndx == 0:
|
||||
self.imports_register_natives = True
|
||||
|
||||
def summary(self):
|
||||
return {
|
||||
"name": self.name,
|
||||
"size": len(self.data),
|
||||
"is_elf": self.is_elf,
|
||||
"arch": self.arch,
|
||||
"elf_note": self.elf_note,
|
||||
"dynsym_entries": len(self.dynsym),
|
||||
"java_symbols": len(self.java_symbols),
|
||||
"java_symbol_sample": self.java_symbols[:5],
|
||||
"exports_JNI_OnLoad": self.has_jni_onload,
|
||||
"imports_RegisterNatives": self.imports_register_natives,
|
||||
"jni_call_refs": sorted(self.jni_call_refs),
|
||||
"signature_hits": {k: {"count": v[0], "strength": v[1], "meaning": v[2]}
|
||||
for k, v in sorted(self.sig_hits.items())},
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# verdict
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def classify(dexes, elves, so_bytes_total):
|
||||
"""Turn measurements into a type guess plus the reasoning behind it.
|
||||
|
||||
Every rule below is a shape test, not a proof, and the confidence is reported
|
||||
so a low-confidence Java2C reading is not mistaken for a confirmed one.
|
||||
"""
|
||||
evidence = []
|
||||
verdict = "no-hardening-signal"
|
||||
confidence = "low"
|
||||
|
||||
dex_metrics = [d for d in dexes if not d.parse_error]
|
||||
total_methods = sum(d.total_methods for d in dex_metrics)
|
||||
total_native = sum(d.native_methods for d in dex_metrics)
|
||||
native_ratio = (float(total_native) / total_methods) if total_methods else 0.0
|
||||
fully_native = sum(d.fully_native_classes for d in dex_metrics)
|
||||
native_dominated = sum(d.native_dominated_classes for d in dex_metrics)
|
||||
no_code = sum(d.no_code_methods for d in dex_metrics)
|
||||
trivial_bodies = sum(d.trivial_bodies for d in dex_metrics)
|
||||
body_methods = sum(d.body_methods for d in dex_metrics)
|
||||
trivial_ratio = (float(trivial_bodies) / body_methods) if body_methods else 0.0
|
||||
|
||||
java_syms = sum(len(e.java_symbols) for e in elves)
|
||||
register_natives_ref = any(e.imports_register_natives for e in elves)
|
||||
has_jni_onload = any(e.has_jni_onload for e in elves)
|
||||
# The reliable static signature of dynamic registration is the ABSENCE of
|
||||
# Java_* symbols from a library that does export a JNI entry point. Looking
|
||||
# for a literal RegisterNatives symbol does not work: NDK's C++ jni.h
|
||||
# implements it as an inline member that calls through the function table,
|
||||
# so no such symbol is emitted (measured: a real library exports JNI_OnLoad
|
||||
# with zero Java_* and zero RegisterNatives symbols).
|
||||
# Judged per library, not across the set: one library registering
|
||||
# dynamically says nothing about another that exports static symbols.
|
||||
dynamic_register_shape = any(e.has_jni_onload and not e.java_symbols for e in elves)
|
||||
tool_markers = []
|
||||
for e in elves:
|
||||
for label, (count, strength, meaning) in e.sig_hits.items():
|
||||
if strength == "strong":
|
||||
tool_markers.append("%s(x%d) in %s" % (label, count, os.path.basename(e.name)))
|
||||
medium_markers = []
|
||||
for e in elves:
|
||||
for label, (count, strength, meaning) in e.sig_hits.items():
|
||||
if strength == "medium":
|
||||
medium_markers.append("%s in %s" % (label, os.path.basename(e.name)))
|
||||
|
||||
# --- dex-layer shape -------------------------------------------------
|
||||
java2c_dex_shape = (native_dominated >= 2) or (native_ratio >= 0.35 and total_native >= 10)
|
||||
jni_sink_dex_shape = 0 < total_native <= 20 and native_ratio < 0.15
|
||||
stub_shape = no_code > 0 or (body_methods >= 8 and trivial_ratio >= 0.5)
|
||||
|
||||
if dex_metrics:
|
||||
evidence.append({
|
||||
"claim": "dex layer: native declaration density %.2f%% (%d/%d)"
|
||||
% (native_ratio * 100, total_native, total_methods),
|
||||
"strength": "medium" if java2c_dex_shape else "strong",
|
||||
"supports": "java2c" if java2c_dex_shape else "baseline",
|
||||
"note": "High density with whole classes turned native is the Java2C shape. "
|
||||
"It is NOT sufficient on its own: a hand-written JNI class looks "
|
||||
"identical in dex, so the .so layer decides.",
|
||||
})
|
||||
evidence.append({
|
||||
"claim": "dex layer: %d classes are native-dominated (>=70%% native of >=3 "
|
||||
"named methods), %d fully native" % (native_dominated, fully_native),
|
||||
"strength": "medium",
|
||||
"supports": "java2c" if native_dominated >= 2 else "baseline",
|
||||
"note": "A hand-written JNI boundary class normally keeps its Java half, so "
|
||||
"whole classes turned native are the Java2C shape. Constructors are "
|
||||
"excluded: they stay in Java even after a translation pass.",
|
||||
})
|
||||
if no_code:
|
||||
evidence.append({
|
||||
"claim": "dex layer: %d non-native, non-abstract methods have no code_item"
|
||||
% no_code,
|
||||
"strength": "strong",
|
||||
"supports": "extraction-shell/vmp",
|
||||
"note": "Structurally invalid for plain Java; expect a loader that "
|
||||
"fills bodies at runtime, or a private-opcode converter.",
|
||||
})
|
||||
if trivial_ratio:
|
||||
evidence.append({
|
||||
"claim": "dex layer: %.0f%% of method bodies are return-a-constant stubs "
|
||||
"(%d/%d)" % (trivial_ratio * 100, trivial_bodies, body_methods),
|
||||
"strength": "medium",
|
||||
"supports": "extraction-shell/vmp" if stub_shape else "baseline",
|
||||
"note": "Approximate metric (see --help). Empty bodies are the signature "
|
||||
"of an extraction shell or a VMP, not of Java2C.",
|
||||
})
|
||||
|
||||
# --- native-layer shape ----------------------------------------------
|
||||
if elves:
|
||||
evidence.append({
|
||||
"claim": "native layer: %d exported Java_* symbols across %d library(ies)"
|
||||
% (java_syms, len(elves)),
|
||||
"strength": "strong" if java_syms else "medium",
|
||||
"supports": "java2c" if java_syms else "none",
|
||||
"note": "Static-linkage form. Compare the count against the dex native "
|
||||
"method count: a rough 1:1 map is the Java2C signature.",
|
||||
})
|
||||
if dynamic_register_shape:
|
||||
dyn_libs = sorted(os.path.basename(e.name)
|
||||
for e in elves if e.has_jni_onload and not e.java_symbols)
|
||||
evidence.append({
|
||||
"claim": "native layer: dynamic registration in %s "
|
||||
"(JNI_OnLoad exported, zero Java_* symbols)"
|
||||
% ", ".join(dyn_libs),
|
||||
"strength": "strong",
|
||||
"supports": "dynamic-registration",
|
||||
"note": "Binding happens at runtime, so a symbol search comes back empty "
|
||||
"and fails SILENTLY. Do not expect a RegisterNatives symbol "
|
||||
"either: NDK's C++ jni.h makes it an inline member that calls "
|
||||
"through the function table, so none is emitted.",
|
||||
})
|
||||
if register_natives_ref:
|
||||
evidence.append({
|
||||
"claim": "native layer: a RegisterNatives symbol is imported",
|
||||
"strength": "weak",
|
||||
"supports": "dynamic-registration",
|
||||
"note": "Only a C-side implementation leaves this name; its absence "
|
||||
"proves nothing.",
|
||||
})
|
||||
if has_jni_onload:
|
||||
evidence.append({
|
||||
"claim": "native layer: JNI_OnLoad exported",
|
||||
"strength": "weak",
|
||||
"supports": "jni-boundary-exists",
|
||||
"note": "Present in nearly every JNI library. Holds no information "
|
||||
"about which hardening was applied.",
|
||||
})
|
||||
if tool_markers:
|
||||
evidence.append({
|
||||
"claim": "native layer: toolchain markers found: %s" % ", ".join(tool_markers),
|
||||
"strength": "strong",
|
||||
"supports": "java2c",
|
||||
"note": "A Dex-to-C toolchain leaves its own runtime symbols behind.",
|
||||
})
|
||||
if medium_markers:
|
||||
evidence.append({
|
||||
"claim": "native layer: runtime markers found: %s" % ", ".join(medium_markers),
|
||||
"strength": "medium",
|
||||
"supports": "java2c",
|
||||
"note": "dcc runtime header names; suggestive, and easy to rename.",
|
||||
})
|
||||
|
||||
# --- verdict ----------------------------------------------------------
|
||||
if tool_markers and (java2c_dex_shape or java_syms):
|
||||
verdict, confidence = "java2c", "high"
|
||||
elif java2c_dex_shape and java_syms and total_native and \
|
||||
(float(java_syms) / total_native) >= 0.5:
|
||||
verdict, confidence = "java2c", "high"
|
||||
elif java2c_dex_shape and medium_markers:
|
||||
verdict, confidence = "java2c", "medium"
|
||||
elif java2c_dex_shape and dynamic_register_shape:
|
||||
# A Dex-to-C toolchain that hides its symbols (dcc's Application.mk ships
|
||||
# -fvisibility=hidden) leaves exactly this: a JNI entry point and a
|
||||
# registration table, with no Java_* name to search for.
|
||||
verdict, confidence = "java2c", "medium"
|
||||
elif stub_shape:
|
||||
verdict, confidence = "extraction-shell-or-vmp", "medium"
|
||||
elif java2c_dex_shape:
|
||||
verdict, confidence = "java2c-suspect-needs-so", "low"
|
||||
elif jni_sink_dex_shape:
|
||||
verdict, confidence = "jni-sinking", "medium"
|
||||
elif dex_metrics or elves:
|
||||
verdict, confidence = "no-hardening-signal", "medium"
|
||||
|
||||
routes = {
|
||||
"java2c": [
|
||||
"Do NOT look for a decrypted DEX: none exists, at any point, in memory.",
|
||||
"Read the C in the .so: each translated method is one function with a "
|
||||
"JNIEnv*/jobject prefix.",
|
||||
"Rebuild the call graph through the JNI reverse calls "
|
||||
"(FindClass/GetMethodID/CallXxxMethod).",
|
||||
],
|
||||
"java2c-suspect-needs-so": [
|
||||
"Supply the matching .so (--so) before acting: the dex shape alone "
|
||||
"cannot separate Java2C from a hand-written JNI class.",
|
||||
"If no .so carries the logic, re-read the dex shape as ordinary Java.",
|
||||
],
|
||||
"extraction-shell-or-vmp": [
|
||||
"Dump memory and measure the dump with scripts/dex_dump_validate.py.",
|
||||
"If the dump is mostly stubs, the bodies are filled at invocation time: "
|
||||
"references/advanced-unpacking.md.",
|
||||
"If bodies decode as private opcodes, that is a VMP - recovery cost "
|
||||
"usually exceeds the task value.",
|
||||
],
|
||||
"jni-sinking": [
|
||||
"The dex is still readable: locate the Java call site, then reverse "
|
||||
"the one or few native functions.",
|
||||
"references/native-and-so.md, then the algorithm tooling.",
|
||||
],
|
||||
"dynamic-registration": [
|
||||
"Hook or find RegisterNatives to read the binding table at runtime.",
|
||||
],
|
||||
"no-hardening-signal": [
|
||||
"Proceed with ordinary dex-level analysis.",
|
||||
],
|
||||
}
|
||||
|
||||
return {
|
||||
"verdict": verdict,
|
||||
"confidence": confidence,
|
||||
"metrics": {
|
||||
"dex_total_methods": total_methods,
|
||||
"dex_native_methods": total_native,
|
||||
"dex_native_ratio": round(native_ratio, 4),
|
||||
"dex_fully_native_classes": fully_native,
|
||||
"dex_native_dominated_classes": native_dominated,
|
||||
"dex_no_code_methods": no_code,
|
||||
"dex_trivial_ratio": round(trivial_ratio, 4),
|
||||
"so_java_symbols": java_syms,
|
||||
"so_dynamic_registration": dynamic_register_shape,
|
||||
"so_bytes_total": so_bytes_total,
|
||||
},
|
||||
"evidence": evidence,
|
||||
"route": routes.get(verdict, []),
|
||||
"weak_criteria_warning": [
|
||||
"JNI_OnLoad / RegisterNatives / libc++_shared.so / FindClass appear in "
|
||||
"ordinary JNI libraries: a hit is not a hardening verdict.",
|
||||
"A high native ratio is also produced by hand-written JNI code and by R8 "
|
||||
"stripping Java bodies; only the .so layer separates these.",
|
||||
"The trivial-body ratio is approximate and flags extraction shells and "
|
||||
"VMPs, never Java2C.",
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# input collection
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _looks_like_dex(data):
|
||||
return data[:4] == b"dex\n"
|
||||
|
||||
|
||||
def _looks_like_elf(data):
|
||||
return data[:4] == b"\x7fELF"
|
||||
|
||||
|
||||
def collect(args):
|
||||
dex_blobs = []
|
||||
elf_blobs = []
|
||||
sources = []
|
||||
|
||||
def add_file(path):
|
||||
try:
|
||||
with open(path, "rb") as fp:
|
||||
data = fp.read()
|
||||
except OSError as exc:
|
||||
print("warning: cannot read %s (%s)" % (path, exc), file=sys.stderr)
|
||||
return
|
||||
if _looks_like_dex(data):
|
||||
dex_blobs.append((os.path.basename(path), data))
|
||||
sources.append(path)
|
||||
elif _looks_like_elf(data):
|
||||
elf_blobs.append((os.path.basename(path), data))
|
||||
sources.append(path)
|
||||
else:
|
||||
print("warning: %s is neither a dex nor an ELF image, skipped" % path,
|
||||
file=sys.stderr)
|
||||
|
||||
for path in args.dex or []:
|
||||
add_file(path)
|
||||
for path in args.so or []:
|
||||
add_file(path)
|
||||
|
||||
apks = list(args.apk or [])
|
||||
for directory in (args.dir or []):
|
||||
for root, _dirs, files in os.walk(directory):
|
||||
for fn in files:
|
||||
full = os.path.join(root, fn)
|
||||
low = fn.lower()
|
||||
if low.endswith(".apk"):
|
||||
apks.append(full)
|
||||
elif low.endswith((".dex", ".so")):
|
||||
add_file(full)
|
||||
|
||||
for apk in apks:
|
||||
if not os.path.exists(apk):
|
||||
print("warning: apk not found: %s" % apk, file=sys.stderr)
|
||||
continue
|
||||
sources.append(apk)
|
||||
try:
|
||||
with zipfile.ZipFile(apk) as zf:
|
||||
for info in zf.infolist():
|
||||
low = info.filename.lower()
|
||||
if not (low.endswith(".dex") or low.endswith(".so")):
|
||||
continue
|
||||
if info.file_size > 256 * 1024 * 1024:
|
||||
print("warning: skipping oversized entry %s (%d B)"
|
||||
% (info.filename, info.file_size), file=sys.stderr)
|
||||
continue
|
||||
data = zf.read(info)
|
||||
label = os.path.basename(info.filename)
|
||||
if _looks_like_dex(data):
|
||||
dex_blobs.append((label, data))
|
||||
elif _looks_like_elf(data):
|
||||
elf_blobs.append((label, data))
|
||||
except zipfile.BadZipFile:
|
||||
print("warning: %s is not a readable zip/apk" % apk, file=sys.stderr)
|
||||
|
||||
return dex_blobs, elf_blobs, sources
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Collect the evidence that separates Java2C from an extraction "
|
||||
"shell, a VMP and ordinary JNI sinking.",
|
||||
epilog="Weak criteria (hits that do NOT establish a verdict on their own): "
|
||||
"JNI_OnLoad, RegisterNatives, libc++_shared.so, FindClass/GetMethodID, "
|
||||
"and a bare 'dcc' substring. The trivial-body ratio is advisory: it "
|
||||
"flags extraction shells and VMPs, never Java2C.",
|
||||
)
|
||||
parser.add_argument("--apk", action="append", help="APK to inspect (repeatable)")
|
||||
parser.add_argument("--dex", action="append", help="dex image (repeatable)")
|
||||
parser.add_argument("--so", action="append", help="ELF library (repeatable)")
|
||||
parser.add_argument("--dir", action="append",
|
||||
help="directory scanned recursively for *.apk/*.dex/*.so")
|
||||
parser.add_argument("--json", action="store_true", help="emit machine-readable JSON")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
if not any([args.apk, args.dex, args.so, args.dir]):
|
||||
parser.error("at least one of --apk/--dex/--so/--dir is required")
|
||||
|
||||
dex_blobs, elf_blobs, sources = collect(args)
|
||||
if not dex_blobs and not elf_blobs:
|
||||
print("error: no usable dex or ELF input found", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
dexes = [Dex(data, name) for name, data in dex_blobs]
|
||||
elves = [Elf(data, name) for name, data in elf_blobs]
|
||||
result = classify(dexes, elves, sum(len(d) for _n, d in elf_blobs))
|
||||
|
||||
report = {
|
||||
"inputs": sources,
|
||||
"dex": [d.summary() for d in dexes],
|
||||
"native": [e.summary() for e in elves],
|
||||
"classification": result,
|
||||
}
|
||||
|
||||
if args.json:
|
||||
print(json.dumps(report, indent=2, sort_keys=False))
|
||||
return 0
|
||||
|
||||
print("=" * 72)
|
||||
print("java2c_probe: hardening classification evidence")
|
||||
print("=" * 72)
|
||||
print("inputs: %d file(s)" % len(sources))
|
||||
for src in sources:
|
||||
print(" - %s" % src)
|
||||
|
||||
print("\n[dex layer]")
|
||||
if not dexes:
|
||||
print(" (no dex supplied: the dex-layer metrics are unmeasured, so a")
|
||||
print(" Java2C reading cannot be supported or excluded from this input)")
|
||||
for d in dexes:
|
||||
s = d.summary()
|
||||
if s["parse_error"]:
|
||||
print(" %s: PARSE ERROR: %s" % (s["name"], s["parse_error"]))
|
||||
continue
|
||||
print(" %s: %d B, %d classes, %d methods, native %d (%.2f%%), "
|
||||
"native-dominated classes %d, fully native %d"
|
||||
% (s["name"], s["size"], s["classes"], s["total_methods"],
|
||||
s["native_methods"], s["native_ratio"] * 100,
|
||||
s["native_dominated_classes"], s["fully_native_classes"]))
|
||||
print(" no-code methods %d, trivial bodies %d/%d (%.0f%%), header %s"
|
||||
% (s["no_code_methods"], s["trivial_bodies"], s["body_methods"],
|
||||
s["trivial_ratio"] * 100,
|
||||
"OK" if s["header_size_matches"] else "MISMATCH: " + s["header_note"]))
|
||||
|
||||
print("\n[native layer]")
|
||||
if not elves:
|
||||
print(" (no .so supplied)")
|
||||
for e in elves:
|
||||
s = e.summary()
|
||||
if not s["is_elf"]:
|
||||
print(" %s: not ELF (%s)" % (s["name"], s["elf_note"]))
|
||||
continue
|
||||
print(" %s: %d B, %s, dynsym %d entries, Java_* %d, JNI_OnLoad %s, "
|
||||
"RegisterNatives-import %s"
|
||||
% (s["name"], s["size"], s["arch"], s["dynsym_entries"],
|
||||
s["java_symbols"], s["exports_JNI_OnLoad"],
|
||||
s["imports_RegisterNatives"]))
|
||||
if s["elf_note"]:
|
||||
print(" note: %s" % s["elf_note"])
|
||||
if s["signature_hits"]:
|
||||
for label, info in s["signature_hits"].items():
|
||||
print(" [%s] %s x%d - %s"
|
||||
% (info["strength"], label, info["count"], info["meaning"]))
|
||||
|
||||
print("\n[classification]")
|
||||
print(" verdict : %s" % result["verdict"])
|
||||
print(" confidence : %s" % result["confidence"])
|
||||
print("\n evidence:")
|
||||
for item in result["evidence"]:
|
||||
print(" (%s) %s" % (item["strength"], item["claim"]))
|
||||
print(" note: %s" % item["note"])
|
||||
print("\n route:")
|
||||
for line in result["route"]:
|
||||
print(" - %s" % line)
|
||||
print("\n weak criteria - a hit here is NOT a verdict:")
|
||||
for line in result["weak_criteria_warning"]:
|
||||
print(" ! %s" % line)
|
||||
print("=" * 72)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,937 @@
|
||||
#!/usr/bin/env python3
|
||||
"""kernelsu_syscall_mask.py -- generate a configurable kernel-side syscall-masking scaffold.
|
||||
|
||||
What this generates, and the correction it exists to make
|
||||
---------------------------------------------------------
|
||||
There is a widespread misreading worth killing early: **a KernelSU module is a
|
||||
Magisk-compatible userspace module and cannot change what a syscall returns.**
|
||||
Its `post-fs-data.sh` / `service.sh` run as root in the normal world; nothing in
|
||||
that format reaches the kernel's return path. Everything a module of that shape
|
||||
can do is userspace work -- bind mounts, setting props, starting a daemon,
|
||||
writing config.
|
||||
|
||||
Actually rewriting what `openat`/`read`/`stat` return for `/proc/self/maps` or
|
||||
`/proc/self/status` needs one of exactly three things:
|
||||
|
||||
kpm KernelPatch / APatch Kernel Patch Module -- pointer replacement in the
|
||||
syscall table (`fp_hook_syscalln`) or inline hooks (`hook_wrapN`).
|
||||
Built with a bare-metal ARM64 toolchain (aarch64-none-elf-gcc) into a
|
||||
relocatable .kpm ELF. Needs a KernelPatch-patched boot image.
|
||||
lkm An out-of-tree kernel module. Needs kernel source matching the device's
|
||||
exact version and vermagic, and a loader path.
|
||||
ebpf A BPF program attached to a syscall tracepoint or kprobe. Needs a GKI
|
||||
kernel with the tracing/BTF machinery -- 5.10+ in Android practice.
|
||||
|
||||
So this script emits a **module directory that is honest about which half is
|
||||
loadable**: the userspace skeleton (module.prop + scripts + config) is real and
|
||||
installable, and each kernel-side target is a template with its toolchain gate
|
||||
stated in its own header. None of the kernel-side code is compiled or loaded
|
||||
anywhere by this script.
|
||||
|
||||
What is measured vs unverified
|
||||
------------------------------
|
||||
`generate`, `gates` and `verify` are exercised on the host (see
|
||||
docs/tool-verification/EXTENSION-kernel-weapons.md). **Whether the kernel-side
|
||||
code compiles or loads is unverified** -- that needs a device with the matching
|
||||
kernel and the matching toolchain, and the reference device is a 4.14.186 Magisk
|
||||
build where all three routes are closed.
|
||||
|
||||
Examples
|
||||
python kernelsu_syscall_mask.py gates
|
||||
python kernelsu_syscall_mask.py generate --out work/sysmask --package <PKG>
|
||||
python kernelsu_syscall_mask.py verify --dir work/sysmask
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Kernel gate table. Every number here is either a toolchain fact or a
|
||||
# documented platform threshold; the "how to check" column is what turns it
|
||||
# from trivia into a test you run on your own device.
|
||||
# ---------------------------------------------------------------------------
|
||||
GATES = [
|
||||
{
|
||||
"route": "eBPF tracepoint/kprobe",
|
||||
"gate": "GKI kernel 5.10+ (Android 12+)",
|
||||
"why": "Android's BPF/tracing support as bpftrace-class tooling and "
|
||||
"stackplz expect it is a GKI-era feature; BTF is required for "
|
||||
"CO-RE style probes.",
|
||||
"check": "uname -r -> expect 5.10.x or newer; ls /sys/kernel/btf/vmlinux",
|
||||
"ref": "https://source.android.com/docs/core/architecture/kernel/bpf",
|
||||
},
|
||||
{
|
||||
"route": "KernelPatch / APatch KPM",
|
||||
"gate": "KernelPatch-patched boot image + kpm toolchain",
|
||||
"why": "KPMs load through kpimg injected into the kernel image's payload "
|
||||
"segment; building one needs a bare-metal ARM64 compiler "
|
||||
"(aarch64-none-elf-gcc), not the NDK.",
|
||||
"check": "ls /data/adb/ | grep -i apatch ; which aarch64-none-elf-gcc",
|
||||
"ref": "https://github.com/bmax121/KernelPatch",
|
||||
},
|
||||
{
|
||||
"route": "Out-of-tree LKM",
|
||||
"gate": "Kernel source matching the device's exact version + vermagic",
|
||||
"why": "A module built against a different kernel revision is refused at "
|
||||
"load time on a mismatched vermagic, and the device's kernel "
|
||||
"source is frequently not published at all.",
|
||||
"check": "uname -r ; look for a matching kernel source tree for the device",
|
||||
"ref": "https://source.android.com/docs/core/architecture/kernel/android-common",
|
||||
},
|
||||
{
|
||||
"route": "seccomp-BPF (for contrast)",
|
||||
"gate": "any modern kernel, but it is not this route",
|
||||
"why": "seccomp can make a syscall FAIL (SECCOMP_RET_ERRNO/TRAP); it "
|
||||
"cannot rewrite the CONTENT a successful read returns. It closes "
|
||||
"doors, it does not paint them -- which is the whole requirement "
|
||||
"for a spoofed /proc read.",
|
||||
"check": "n/a -- this is a capability ceiling, not a version check",
|
||||
"ref": "references/kernel-and-environment-hardening.md",
|
||||
},
|
||||
]
|
||||
|
||||
DEFAULT_RULES = [
|
||||
{
|
||||
"syscall": "openat",
|
||||
"path": "/proc/self/maps",
|
||||
"action": "deny",
|
||||
"errno": "ENOENT",
|
||||
"note": "hides the mapping list a userspace instrumentation agent "
|
||||
"advertises itself in",
|
||||
},
|
||||
{
|
||||
"syscall": "newfstatat",
|
||||
"path": "/proc/self/maps",
|
||||
"action": "fake_size",
|
||||
"size": 0,
|
||||
"note": "a stat that reports 0 bytes is as good as absent for a reader "
|
||||
"that sizes its buffer first",
|
||||
},
|
||||
{
|
||||
"syscall": "openat",
|
||||
"path": "/proc/self/status",
|
||||
"action": "filter_line",
|
||||
"prefix": "TracerPid",
|
||||
"note": "the write side owns this field; see "
|
||||
"references/kernel-and-environment-hardening.md section 5",
|
||||
},
|
||||
{
|
||||
"syscall": "read",
|
||||
"path": "/proc/self/status",
|
||||
"action": "filter_line",
|
||||
"prefix": "TracerPid",
|
||||
"note": "line filter applied to the read once the fd is open",
|
||||
},
|
||||
]
|
||||
|
||||
MODULE_PROP = """id={id}
|
||||
name={name}
|
||||
version={version}
|
||||
versionCode=1
|
||||
author=apk-reverse
|
||||
description={description}
|
||||
"""
|
||||
|
||||
POST_FS_DATA = """#!/system/bin/sh
|
||||
# Runs at post-fs-data, as root, in the normal world.
|
||||
# The kernel side (if you built and loaded one) owns the syscall table; this
|
||||
# script's whole job is to make the configuration visible to it and to leave a
|
||||
# record that the module actually ran. Do not put a syscall hook here: this is
|
||||
# userspace, and nothing here can change what openat/read/stat return.
|
||||
MODDIR=${{0%/*}}
|
||||
CONF=$MODDIR/config/syscall_mask.json
|
||||
LOG=/data/adb/{id}.log
|
||||
|
||||
if [ ! -f "$CONF" ]; then
|
||||
echo "$(date) missing $CONF" >> "$LOG"
|
||||
exit 0
|
||||
fi
|
||||
echo "$(date) post-fs-data: config present, $(wc -c < "$CONF") bytes" >> "$LOG"
|
||||
# The KPM/LKM loader path is intentionally NOT invoked from here. Loading a
|
||||
# kernel module is a boot-image-level decision with a bricking risk; do it once,
|
||||
# by hand, and read the evidence file first.
|
||||
exit 0
|
||||
"""
|
||||
|
||||
SERVICE = """#!/system/bin/sh
|
||||
# Runs in late_start service mode, as root. Kept minimal and non-blocking on
|
||||
# purpose: a module that spins here delays boot on every start, and this one
|
||||
# only reports state.
|
||||
MODDIR=${{0%/*}}
|
||||
CONF=$MODDIR/config/syscall_mask.json
|
||||
LOG=/data/adb/{id}.log
|
||||
|
||||
if [ -r /proc/sys/kernel/version ]; then
|
||||
echo "$(date) service: kernel $(uname -r)" >> "$LOG"
|
||||
fi
|
||||
echo "$(date) service: rules=$(grep -c '"syscall"' "$CONF" 2>/dev/null)" >> "$LOG"
|
||||
exit 0
|
||||
"""
|
||||
|
||||
UNINSTALL = """#!/system/bin/sh
|
||||
# Uninstall hook. Deliberately does not attempt to unload a kernel module: if one
|
||||
# is loaded, unloading it is a separate deliberate act with its own risk, and a
|
||||
# silent unload during package removal is how a device ends up in a boot loop.
|
||||
rm -f /data/adb/{id}.log
|
||||
exit 0
|
||||
"""
|
||||
|
||||
CUSTOMIZE = """#!/system/bin/sh
|
||||
# KernelSU/Magisk install-time script. Kept as a no-op so the module always
|
||||
# installs; see the module README for why nothing is set up here.
|
||||
exit 0
|
||||
"""
|
||||
|
||||
KPM_C = r'''// SPDX-License-Identifier: GPL-2.0
|
||||
/*
|
||||
* syscall_mask.c -- config-driven syscall return masking, KernelPatch/APatch KPM.
|
||||
*
|
||||
* Generated by skills/apk-reverse/scripts/kernelsu_syscall_mask.py
|
||||
*
|
||||
* STATUS: TEMPLATE, NOT COMPILED AND NOT LOADED. Every hook below follows the
|
||||
* KernelPatch KPM interface as documented in public KPM development write-ups
|
||||
* (KPM_NAME/KPM_INIT/KPM_EXIT sections, kfunc_def symbol resolution without
|
||||
* `extern`, fp_hook_syscalln pointer replacement, hook_fargs4_t callbacks with
|
||||
* args->skip_origin / args->ret). VERIFY EVERY NAME AND SIGNATURE against the
|
||||
* kpmodule.h of the KernelPatch revision you build against -- this file was not
|
||||
* compiled anywhere, and an interface that has drifted produces a module that
|
||||
* fails to load, or worse, loads and faults.
|
||||
*
|
||||
* Build (bare-metal ARM64, NOT the NDK):
|
||||
* aarch64-none-elf-gcc -O2 -fno-stack-protector -c syscall_mask.c
|
||||
* ... link into a relocatable ELF per the KernelPatch module docs
|
||||
* Load: push the .kpm and add it through the APatch app's KPM manager.
|
||||
*
|
||||
* Design notes that are load-bearing rather than stylistic:
|
||||
* - Callbacks run on EVERY syscall of their number, system-wide. The first
|
||||
* thing each one does is reject the overwhelmingly common case, because a
|
||||
* hook that does real work on every write is how a device locks up.
|
||||
* - Inline hooks (hook_wrapN) rewrite the target's prologue. On an LTO kernel
|
||||
* the exported symbol is frequently NOT the call site (the call got inlined),
|
||||
* so an inline hook installs cleanly and never fires. Pointer replacement in
|
||||
* the syscall table (fp_hook_syscalln) does not have that failure mode,
|
||||
* because the syscall entry path must go through the table.
|
||||
* - Never sleep in these callbacks except where the syscall itself runs in
|
||||
* process context and the copy helper is the sleeping variant.
|
||||
*/
|
||||
#include <linux/types.h>
|
||||
#include <linux/string.h>
|
||||
#include <linux/errno.h>
|
||||
#include <kpmodule.h>
|
||||
|
||||
KPM_NAME("{id}");
|
||||
KPM_VERSION("{version}");
|
||||
KPM_LICENSE("GPL");
|
||||
KPM_AUTHOR("apk-reverse");
|
||||
KPM_DESCRIPTION("{description}");
|
||||
|
||||
/* kfunc declarations must NOT carry `extern`: an extern declaration compiles to
|
||||
* an undefined reference (*UND*) and the loader rejects the module with
|
||||
* "unknown symbol". Letting it be a tentative definition allocates the slot in
|
||||
* .bss and the loader fills it in. Measured and documented in public KPM work. */
|
||||
int kfunc_def(strncpy_from_user)(char *dst, const char __user *src, long count);
|
||||
|
||||
/* ------------------------------------------------------------------ rules --
|
||||
* Generated from config/syscall_mask.json. Keep this table in sync by
|
||||
* re-running the generator rather than by hand-editing it.
|
||||
*/
|
||||
#define MASK_DENY 1 /* return -err before the real syscall runs */
|
||||
#define MASK_FAKE_SIZE 2 /* openat succeeds, stat reports a bogus size */
|
||||
#define MASK_FILTER_LINE 3 /* strip lines with this prefix from read() */
|
||||
|
||||
struct mask_rule {
|
||||
const char *path; /* userspace path to match, NULL = any */
|
||||
const char *prefix; /* line prefix for MASK_FILTER_LINE, else NULL */
|
||||
int action;
|
||||
int err; /* -errno for MASK_DENY */
|
||||
long value; /* size for MASK_FAKE_SIZE */
|
||||
};
|
||||
|
||||
static const struct mask_rule rules[] = {
|
||||
{RULE_TABLE}
|
||||
};
|
||||
|
||||
#define NRULES (sizeof(rules) / sizeof(rules[0]))
|
||||
|
||||
/* A bounded, allocation-free matcher. The path is copied in first, so the
|
||||
* comparison never touches userspace after the initial copy -- touching
|
||||
* userspace memory twice is how a hook races with the caller's own unmapping. */
|
||||
static int copy_and_match(const char __user *upath, char *kbuf, unsigned long n,
|
||||
const char **matched)
|
||||
{{
|
||||
long copied;
|
||||
unsigned long i;
|
||||
|
||||
if (!upath || n == 0)
|
||||
return 0;
|
||||
if (!kfunc(strncpy_from_user))
|
||||
return 0; /* symbol unresolved: fail open */
|
||||
copied = kfunc(strncpy_from_user)(kbuf, upath, (long)(n - 1));
|
||||
if (copied <= 0)
|
||||
return 0;
|
||||
kbuf[copied] = '\0';
|
||||
|
||||
for (i = 0; i < NRULES; i++) {{
|
||||
if (!rules[i].path)
|
||||
continue;
|
||||
if (strstr(kbuf, rules[i].path)) {{
|
||||
*matched = rules[i].path;
|
||||
return rules[i].action;
|
||||
}}
|
||||
}}
|
||||
return 0;
|
||||
}}
|
||||
|
||||
/* ------------------------------------------------------------------ openat --
|
||||
* The path argument is arg2 on arm64 (dfd, path, flags, mode). If the path is
|
||||
* unreadable, the rule is skipped rather than guessed at.
|
||||
*/
|
||||
static void before_openat(hook_fargs4_t *args, void *udata)
|
||||
{{
|
||||
const char __user *upath = (const char __user *)syscall_argn(args, 1);
|
||||
char kbuf[256];
|
||||
const char *matched = 0;
|
||||
int action;
|
||||
|
||||
(void)udata;
|
||||
action = copy_and_match(upath, kbuf, sizeof(kbuf), &matched);
|
||||
if (action == MASK_DENY)
|
||||
args->ret = MASK_DENY_ERRNO;
|
||||
if (action == MASK_FAKE_SIZE)
|
||||
args->skip_origin = 0; /* let it open; the stat is spoofed */
|
||||
}}
|
||||
|
||||
/* --------------------------------------------------------------- newfstatat --
|
||||
* arm64 has no plain `stat` syscall; readers go through newfstatat (or fstatat64
|
||||
* on 32-bit). A zero size is what makes a reader that sizes its buffer first
|
||||
* decide there is nothing to read.
|
||||
*/
|
||||
static void after_newfstatat(hook_fargs4_t *args, void *udata)
|
||||
{{
|
||||
const char __user *upath = (const char __user *)syscall_argn(args, 1);
|
||||
char kbuf[256];
|
||||
const char *matched = 0;
|
||||
|
||||
(void)udata;
|
||||
if (copy_and_match(upath, kbuf, sizeof(kbuf), &matched) != MASK_FAKE_SIZE)
|
||||
return;
|
||||
/* The struct stat* is arg2; zeroing st_size is the whole point. Field
|
||||
* offsets are ABI-specific -- resolve them against the target's headers
|
||||
* rather than trusting an offset copied from a different architecture. */
|
||||
/* TODO(verify-on-device): zero the size field of the struct at arg2. */
|
||||
}}
|
||||
|
||||
/* -------------------------------------------------------------------- read --
|
||||
* Line filtering happens on the read side, once the fd is already open. Doing
|
||||
* it here rather than at openat is what lets a reader that opens the file
|
||||
* before the filter is installed still be covered.
|
||||
*/
|
||||
static void before_read(hook_fargs4_t *args, void *udata)
|
||||
{{
|
||||
long count = (long)syscall_argn(args, 2);
|
||||
(void)udata;
|
||||
if (count <= 0 || count > 8192)
|
||||
return; /* first filter: the common case */
|
||||
/* TODO(verify-on-device): identify the fd's path, copy the buffer, rewrite
|
||||
* it in place minus the filtered lines, and adjust the returned length. */
|
||||
}}
|
||||
|
||||
/* ------------------------------------------------------------------ init -----
|
||||
* Registration order matters: install the syscall hooks first, then enable the
|
||||
* rules. If the module is unloaded between the two, the hooks must already be
|
||||
* gone.
|
||||
*/
|
||||
static long mask_init(const char *args, const struct kernel_patch_funcs *kp)
|
||||
{{
|
||||
(void)args;
|
||||
(void)kp;
|
||||
|
||||
if (!kfunc(strncpy_from_user)) {{
|
||||
/* Failing loudly here is deliberate: a half-armed hook set that cannot
|
||||
* read a path would pass every check through, which looks like success
|
||||
* and is not. */
|
||||
return -EINVAL;
|
||||
}}
|
||||
|
||||
/* fp_hook_syscalln(__NR_openat, before_openat, 0, 0); */
|
||||
/* fp_hook_syscalln(__NR_newfstatat, before_newfstatat, 0, 0); */
|
||||
/* fp_hook_syscalln(__NR_read, before_read, 0, 0); */
|
||||
/* TODO(verify-on-device): the argument lists above follow the public
|
||||
* examples; confirm arity against your kpmodule.h before enabling them. */
|
||||
return 0;
|
||||
}}
|
||||
|
||||
static long mask_exit(void *udata)
|
||||
{{
|
||||
(void)udata;
|
||||
/* Unhook in the exact reverse order of registration. */
|
||||
return 0;
|
||||
}}
|
||||
|
||||
KPM_INIT(mask_init);
|
||||
KPM_EXIT(mask_exit);
|
||||
'''
|
||||
|
||||
LKM_C = r'''// SPDX-License-Identifier: GPL-2.0
|
||||
/*
|
||||
* syscall_mask.c -- out-of-tree LKM variant of the same idea.
|
||||
*
|
||||
* STATUS: TEMPLATE, NOT COMPILED. Worse than the KPM variant in one specific
|
||||
* way worth understanding before choosing it: to replace a syscall you must
|
||||
* reach the syscall table, and `kallsyms_lookup_name` stopped being exported in
|
||||
* Linux 5.7. On pre-5.7 kernels a direct call works; after that you need a
|
||||
* kprobe to recover the address, or an explicit kernel export. The device's own
|
||||
* kernel source must also match its exact vermagic, and an out-of-tree module
|
||||
* built against a mismatched tree is refused at insmod.
|
||||
*
|
||||
* This is genuine kernel development with a bricking risk. It is the most
|
||||
* expensive of the three routes and the one least likely to be worth it.
|
||||
*/
|
||||
#include <linux/module.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/syscalls.h>
|
||||
#include <linux/uaccess.h>
|
||||
#include <linux/version.h>
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_DESCRIPTION("{description}");
|
||||
MODULE_AUTHOR("apk-reverse");
|
||||
|
||||
/*
|
||||
* Pre-5.7: kallsyms_lookup_name() is exported, so the table pointer is
|
||||
* recoverable directly. 5.7+: NOT exported -- you must either carry a kprobe to
|
||||
* find the symbol or patch the kernel's export list. There is no portable
|
||||
* version of this, which is the honest reason this route is rated last.
|
||||
*/
|
||||
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 7, 0)
|
||||
static unsigned long *sys_call_table_addr(void)
|
||||
{
|
||||
return (unsigned long *)kallsyms_lookup_name("sys_call_table");
|
||||
}
|
||||
#else
|
||||
static unsigned long *sys_call_table_addr(void)
|
||||
{
|
||||
/* TODO(verify-on-device): recover via kprobe on kallsyms_lookup_name, or
|
||||
* enable an explicit export in your own kernel build. */
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int __init mask_init(void)
|
||||
{
|
||||
unsigned long *table = sys_call_table_addr();
|
||||
if (!table) {
|
||||
pr_err("syscall_mask: cannot locate sys_call_table on this kernel\n");
|
||||
return -EINVAL;
|
||||
}
|
||||
pr_info("syscall_mask: sys_call_table at %px\n", table);
|
||||
/* TODO(verify-on-device): save the original pointer, write CR0 (or use
|
||||
* set_memory_rw) to make the table writable, swap in your handler. Getting
|
||||
* the write-protect dance wrong takes the device down. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit mask_exit(void)
|
||||
{
|
||||
/* TODO(verify-on-device): restore the saved pointers. */
|
||||
}
|
||||
|
||||
module_init(mask_init);
|
||||
module_exit(mask_exit);
|
||||
'''
|
||||
|
||||
EBPF_C = r'''// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
|
||||
/*
|
||||
* syscall_mask.bpf.c -- eBPF probe template: filter sys_enter_openat, then
|
||||
* rewrite the result.
|
||||
*
|
||||
* STATUS: TEMPLATE, NOT COMPILED AND NOT ATTACHED. Not runnable on a 4.14
|
||||
* kernel: see the gate table in the module README (GKI 5.10+ in Android
|
||||
* practice, plus BTF for CO-RE style probes).
|
||||
*
|
||||
* READ THIS BEFORE COUNTING ON THE RESULT REWRITE. There are two different
|
||||
* attachment points and they are not interchangeable:
|
||||
*
|
||||
* - A *tracepoint* (syscalls/sys_enter_openat) is an observation point. It can
|
||||
* read the arguments and it can emit events, and it CANNOT change the
|
||||
* syscall's return value. A tracepoint-only program gives you visibility,
|
||||
* not spoofing.
|
||||
* - A *kprobe* on the syscall's entry (or a kretprobe on its exit) can modify
|
||||
* a register with bpf_override_return() -- but only for functions flagged
|
||||
* ALLOW_ERROR_INJECTION, which kernel syscall entry points are not, in
|
||||
* general. On many kernels the helper is refused for exactly this target.
|
||||
*
|
||||
* So the honest eBPF shape for this job is: observe with a tracepoint, and do
|
||||
* the rewrite in a place that actually owns the return value. What follows is
|
||||
* the observation half done properly, plus the rewrite half marked for the
|
||||
* device-side verification it needs.
|
||||
*/
|
||||
#include <vmlinux.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include <bpf/bpf_tracing.h>
|
||||
|
||||
char LICENSE[] SEC("license") = "GPL";
|
||||
|
||||
#define MAX_PATH 256
|
||||
#define TARGET_LEN 15 /* strlen("/proc/self/maps") */
|
||||
|
||||
struct event {
|
||||
__u32 pid;
|
||||
__u32 uid;
|
||||
__s32 dfd;
|
||||
__s64 ret;
|
||||
char path[MAX_PATH];
|
||||
char comm[16];
|
||||
};
|
||||
/* /proc/self/status is 17; kept as a separate constant so the two rules can be
|
||||
* toggled independently. */
|
||||
#define TARGET_STATUS_LEN 17
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_RINGBUF);
|
||||
__uint(max_entries, 1 << 24);
|
||||
} events SEC(".maps");
|
||||
|
||||
/* Filter the target package's processes. Populated from userspace with the
|
||||
* uids the app can run as; an empty map means "all processes", which is a
|
||||
* deliberate default because a filter that silently matches nothing looks
|
||||
* exactly like a probe that is not firing. */
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_HASH);
|
||||
__uint(max_entries, 64);
|
||||
__type(key, __u32); /* uid */
|
||||
__type(value, __u8);
|
||||
} target_uids SEC(".maps");
|
||||
|
||||
static __always_inline int want(void)
|
||||
{
|
||||
__u32 uid = bpf_get_current_uid_gid() & 0xffffffff;
|
||||
__u8 *hit = bpf_map_lookup_elem(&target_uids, &uid);
|
||||
/* Empty map -> observe everything. Populated map -> only listed uids. */
|
||||
return hit || (bpf_map_lookup_elem(&target_uids, &uid) == 0);
|
||||
}
|
||||
|
||||
SEC("tracepoint/syscalls/sys_enter_openat")
|
||||
int tp_sys_enter_openat(struct trace_event_raw_sys_enter *ctx)
|
||||
{
|
||||
struct event *e;
|
||||
const char *upath = (const char *)ctx->args[1];
|
||||
|
||||
if (!want())
|
||||
return 0;
|
||||
|
||||
e = bpf_ringbuf_reserve(&events, sizeof(*e), 0);
|
||||
if (!e)
|
||||
return 0;
|
||||
e->pid = bpf_get_current_pid_tgid() >> 32;
|
||||
e->uid = bpf_get_current_uid_gid() & 0xffffffff;
|
||||
e->dfd = (__s32)ctx->args[0];
|
||||
e->ret = 0;
|
||||
bpf_get_current_comm(&e->comm, sizeof(e->comm));
|
||||
bpf_probe_read_user_str(&e->path, sizeof(e->path), upath);
|
||||
bpf_ringbuf_submit(e, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The result-rewrite half. Two dead ends are recorded here on purpose so they
|
||||
* are not re-derived:
|
||||
*
|
||||
* 1. bpf_override_return() only works on functions marked
|
||||
* ALLOW_ERROR_INJECTION. A raw syscall entry is not one of them, so the
|
||||
* helper is typically refused for this target -- check the return value
|
||||
* and the verifier message rather than assuming the rewrite happened.
|
||||
* 2. A tracepoint cannot rewrite at all. If the rewrite is essential, the
|
||||
* route is a kprobe plus a kernel that permits the override, or one of the
|
||||
* KPM/LKM routes; eBPF's strength here is observation.
|
||||
*/
|
||||
SEC("kprobe/do_sys_openat2")
|
||||
int BPF_KPROBE(kp_do_sys_openat2, int dfd, const char *filename)
|
||||
{
|
||||
char buf[64];
|
||||
if (bpf_probe_read_user_str(&buf, sizeof(buf), filename) <= 0)
|
||||
return 0;
|
||||
if (buf[0] == '/' && buf[1] == 'p' && buf[2] == 'r') {
|
||||
/* TODO(verify-on-device): bpf_override_return(ctx, -ENOENT) here only
|
||||
* after confirming ALLOW_ERROR_INJECTION on this target kernel. */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
'''
|
||||
|
||||
KPM_MAKEFILE = """# aarch64-none-elf-gcc is a BARE-METAL ARM64 compiler, not the Android NDK
|
||||
# toolchain. The NDK targets Android userspace and will not produce a loadable
|
||||
# KPM. STATUS: untested -- this toolchain is not present on a Magisk-only device.
|
||||
CROSS ?= aarch64-none-elf-
|
||||
CC := $(CROSS)gcc
|
||||
OBJCOPY := $(CROSS)objcopy
|
||||
|
||||
TARGET := {id}
|
||||
KPM_DIR ?= ../KernelPatch
|
||||
|
||||
CFLAGS := -O2 -fno-stack-protector -fno-pic -fno-builtin \\
|
||||
-I$(KPM_DIR)/kernel/include -I$(KPM_DIR)/include \\
|
||||
-Wall -Wno-unused-variable
|
||||
|
||||
all: $(TARGET).kpm
|
||||
|
||||
$(TARGET).kpm: src/{id}.c
|
||||
\t$(CC) $(CFLAGS) -c $< -o $@.o
|
||||
\t$(OBJCOPY) -O binary --only-section=.text $@.o /dev/null 2>/dev/null || true
|
||||
\t@echo "NOTE: linking follows the KernelPatch module format; see the docs"
|
||||
\t@echo " for the current link step before trusting this target."
|
||||
|
||||
clean:
|
||||
\trm -f *.o *.kpm
|
||||
|
||||
.PHONY: all clean
|
||||
"""
|
||||
|
||||
MODULE_README = """# {name} -- syscall-return masking scaffold
|
||||
|
||||
Generated by `skills/apk-reverse/scripts/kernelsu_syscall_mask.py`.
|
||||
|
||||
**Read this first: the userspace module in this directory cannot change what a
|
||||
syscall returns.** It is a KernelSU/Magisk-compatible module: its scripts run as
|
||||
root in the normal world. Its only jobs are to carry the configuration, to
|
||||
record that it ran, and to be a place to put the module metadata. The kernel
|
||||
side is a separate artifact with a separate toolchain and a separate risk.
|
||||
|
||||
## What is here
|
||||
|
||||
| Path | Status |
|
||||
|---|---|
|
||||
| `module.prop`, `customize.sh`, `post-fs-data.sh`, `service.sh` | real, installable module skeleton |
|
||||
| `config/syscall_mask.json` | the rule set, read by the kernel side |
|
||||
| `kpm/{id}.c`, `kpm/Makefile` | APatch/KernelPatch template -- **not compiled, not loaded** |
|
||||
| `lkm/{id}.c` | out-of-tree module template -- **not compiled, not loaded** |
|
||||
| `ebpf/syscall_mask.bpf.c` | eBPF probe template -- **not compiled, not attached** |
|
||||
|
||||
## Gate table -- check your device before choosing a route
|
||||
|
||||
| Route | Gate | How to check | Why |
|
||||
|---|---|---|---|
|
||||
{gates}
|
||||
|
||||
## Reference device, measured
|
||||
|
||||
```
|
||||
$ adb shell 'uname -r; cat /proc/version'
|
||||
4.14.186+
|
||||
Linux version 4.14.186+ (nobody@android-build) (Android (6443078 based on r383902)
|
||||
clang version 11.0.1 ...) #1 SMP PREEMPT Wed Mar 30 23:32:42 CST 2022
|
||||
```
|
||||
|
||||
On that device **every kernel-side route above is closed**:
|
||||
|
||||
- eBPF: kernel 4.14 is well below the 5.10 gate.
|
||||
- KPM: no KernelPatch-patched image, and no `aarch64-none-elf-gcc` on the host;
|
||||
installing APatch means patching the boot image, which is a bricking risk.
|
||||
- LKM: no kernel source for the device, so no matching vermagic is possible.
|
||||
|
||||
That is a **route decision, not a failure** -- and it is why every kernel-side
|
||||
file here is labelled a template. Nothing in this directory was loaded on
|
||||
anything.
|
||||
|
||||
## If you do take a kernel route
|
||||
|
||||
Do it once, deliberately, and read the evidence first:
|
||||
|
||||
1. Confirm the gate in the table above on the actual device.
|
||||
2. Build the kernel side on a machine that has the right toolchain.
|
||||
3. **Back up the boot image and know the recovery path before loading anything.**
|
||||
A kernel module that faults takes the device down before you can read why.
|
||||
4. Load it by hand, watch `dmesg`, and keep the previous boot image in hand.
|
||||
5. Only then wire it into boot, and only if the load is repeatable.
|
||||
|
||||
`post-fs-data.sh` in this scaffold deliberately does **not** load anything. That
|
||||
is not an omission.
|
||||
"""
|
||||
|
||||
|
||||
def _render(template, **kw):
|
||||
"""Fill a template's {placeholders} without str.format().
|
||||
|
||||
Braces are the common case in C, so using format() here would mean doubling
|
||||
every one of them -- and the doubled-brace convention fails silently in the
|
||||
other direction the moment somebody next edits the C by hand. `{{` is
|
||||
unescaped and only the named placeholders are substituted, so ordinary C
|
||||
braces pass through untouched.
|
||||
"""
|
||||
text = template.replace("{{", "{").replace("}}", "}")
|
||||
for key, value in kw.items():
|
||||
text = text.replace("{%s}" % key, str(value))
|
||||
return text
|
||||
|
||||
|
||||
def _rule_table_c(rules):
|
||||
"""Render the JSON rule set as a C table."""
|
||||
errno_map = {"ENOENT": 2, "EACCES": 13, "EINVAL": 22, "EPERM": 1}
|
||||
action_map = {"deny": "MASK_DENY", "fake_size": "MASK_FAKE_SIZE",
|
||||
"filter_line": "MASK_FILTER_LINE"}
|
||||
lines = []
|
||||
for rule in rules:
|
||||
path = rule.get("path")
|
||||
prefix = rule.get("prefix")
|
||||
action = action_map.get(rule.get("action"), "MASK_DENY")
|
||||
err = errno_map.get(rule.get("errno", "ENOENT"), 2)
|
||||
value = rule.get("size", 0)
|
||||
lines.append(' { %s, %s, %s, -%d, %d },'
|
||||
% ('"%s"' % path if path else "0",
|
||||
'"%s"' % prefix if prefix else "0",
|
||||
action, err, value))
|
||||
return "\n".join(lines) if lines else " /* no rules configured */"
|
||||
|
||||
|
||||
def cmd_gates(args):
|
||||
"""Report the kernel-route gate table, and probe this host where it can be probed."""
|
||||
print("== kernel-side routes and their gates ==\n")
|
||||
for g in GATES:
|
||||
print(" %-28s %s" % (g["route"], g["gate"]))
|
||||
print(" %-28s why: %s" % ("", g["why"]))
|
||||
print(" %-28s check: %s" % ("", g["check"]))
|
||||
print()
|
||||
print("== host probe (what can be checked from here) ==\n")
|
||||
cc = None
|
||||
import shutil
|
||||
for cand in ("aarch64-none-elf-gcc", "aarch64-linux-gnu-gcc", "clang"):
|
||||
found = shutil.which(cand)
|
||||
if found:
|
||||
cc = (cand, found)
|
||||
break
|
||||
if cc:
|
||||
print(" bare-metal ARM64-ish compiler: %s -> %s" % cc)
|
||||
if cc[0] == "clang":
|
||||
print(" note: clang can target aarch64-none-elf, so this is "
|
||||
"potentially usable for the KPM route")
|
||||
else:
|
||||
print(" bare-metal ARM64 compiler: NOT FOUND")
|
||||
print(" the KPM route cannot be built from this host as configured")
|
||||
for name in ("ndk-build", "make"):
|
||||
print(" %-12s %s" % (name + ":", shutil.which(name) or "NOT FOUND"))
|
||||
print("\n A kernel source tree, a patched boot image and the device's own "
|
||||
"kernel headers cannot be checked from the host; they are device facts.")
|
||||
print(" Run the `check` command for each route on the device before "
|
||||
"believing any of this applies to it.")
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_generate(args):
|
||||
out = args.out
|
||||
rules = DEFAULT_RULES
|
||||
if args.rules:
|
||||
with open(args.rules, encoding="utf-8") as fh:
|
||||
rules = json.load(fh)
|
||||
desc = args.description
|
||||
os.makedirs(os.path.join(out, "config"), exist_ok=True)
|
||||
|
||||
written = []
|
||||
|
||||
def put(rel, text, mode=None):
|
||||
path = os.path.join(out, rel)
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, "w", encoding="utf-8", newline="\n") as fh:
|
||||
fh.write(text)
|
||||
if mode is not None:
|
||||
try:
|
||||
os.chmod(path, mode)
|
||||
except OSError:
|
||||
pass
|
||||
written.append(rel)
|
||||
|
||||
put("module.prop", _render(MODULE_PROP,
|
||||
id=args.id, name=args.name, version=args.version,
|
||||
description=desc))
|
||||
put("customize.sh", CUSTOMIZE, 0o755)
|
||||
put("post-fs-data.sh", _render(POST_FS_DATA, id=args.id), 0o755)
|
||||
put("service.sh", _render(SERVICE, id=args.id), 0o755)
|
||||
put("uninstall.sh", _render(UNINSTALL, id=args.id), 0o755)
|
||||
put("config/syscall_mask.json", json.dumps(
|
||||
{"version": 1, "id": args.id, "package": args.package,
|
||||
"enabled": True, "note": "read by the kernel side, not by the shell "
|
||||
"scripts -- see the module README",
|
||||
"rules": rules}, indent=2) + "\n")
|
||||
|
||||
want = set(args.targets.split(","))
|
||||
if "kpm" in want:
|
||||
body = _render(KPM_C, id=args.id, version=args.version, description=desc,
|
||||
RULE_TABLE=_rule_table_c(rules))
|
||||
put("kpm/%s.c" % args.id, body)
|
||||
put("kpm/Makefile", _render(KPM_MAKEFILE, id=args.id))
|
||||
if "lkm" in want:
|
||||
put("lkm/%s.c" % args.id, _render(LKM_C, id=args.id, description=desc))
|
||||
if "ebpf" in want:
|
||||
put("ebpf/syscall_mask.bpf.c", EBPF_C)
|
||||
|
||||
gate_rows = "\n".join("| %s | %s | `%s` | %s |"
|
||||
% (g["route"], g["gate"], g["check"], g["why"])
|
||||
for g in GATES)
|
||||
put("README.md", _render(MODULE_README, name=args.name, gates=gate_rows,
|
||||
id=args.id))
|
||||
|
||||
print("generated module scaffold in %s" % out)
|
||||
for rel in written:
|
||||
size = os.path.getsize(os.path.join(out, rel))
|
||||
print(" %-28s %7d B" % (rel, size))
|
||||
print()
|
||||
print("STATUS: the userspace module is installable; every kernel-side file is "
|
||||
"an UNVERIFIED template.")
|
||||
print(" No kernel code was compiled or loaded. Run `gates` and read the "
|
||||
"device README")
|
||||
print(" section before assuming any route is open on your target.")
|
||||
return 0
|
||||
|
||||
|
||||
def _strip_c_comments(text):
|
||||
"""Remove /* */ and // comments before any token-level check.
|
||||
|
||||
Not cosmetic: the first version of this check reported a false positive
|
||||
because the template's own comment explains why `extern` is forbidden, and a
|
||||
naive `extern.*kfunc_def` regex happily matched the prose and then ran on to
|
||||
the real declaration's semicolon. A linter that reads comments as code
|
||||
produces exactly the kind of report nobody can trust.
|
||||
"""
|
||||
text = re.sub(r"/\*.*?\*/", "", text, flags=re.S)
|
||||
return re.sub(r"//[^\n]*", "", text)
|
||||
|
||||
|
||||
def cmd_verify(args):
|
||||
"""Read-only checks over a generated directory. Everything here is local."""
|
||||
d = args.dir
|
||||
problems = []
|
||||
print("== verifying %s ==" % d)
|
||||
|
||||
prop = os.path.join(d, "module.prop")
|
||||
if not os.path.isfile(prop):
|
||||
problems.append("module.prop missing")
|
||||
else:
|
||||
fields = {}
|
||||
with open(prop, encoding="utf-8") as fh:
|
||||
for line in fh:
|
||||
if "=" in line:
|
||||
k, v = line.rstrip("\n").split("=", 1)
|
||||
fields[k] = v
|
||||
for req in ("id", "name", "version", "versionCode"):
|
||||
if req not in fields:
|
||||
problems.append("module.prop missing required field %r" % req)
|
||||
if "id" in fields and not re.fullmatch(r"[a-zA-Z][a-zA-Z0-9._-]*", fields["id"]):
|
||||
problems.append("module.prop id %r is not a valid module id" % fields["id"])
|
||||
print(" module.prop: %s" % ", ".join("%s=%s" % kv for kv in sorted(fields.items())))
|
||||
|
||||
conf = os.path.join(d, "config", "syscall_mask.json")
|
||||
if not os.path.isfile(conf):
|
||||
problems.append("config/syscall_mask.json missing")
|
||||
else:
|
||||
try:
|
||||
with open(conf, encoding="utf-8") as fh:
|
||||
doc = json.load(fh)
|
||||
rules = doc.get("rules", [])
|
||||
known = {"deny", "fake_size", "filter_line"}
|
||||
for i, r in enumerate(rules):
|
||||
if r.get("action") not in known:
|
||||
problems.append("rule %d has unknown action %r" % (i, r.get("action")))
|
||||
if not r.get("syscall"):
|
||||
problems.append("rule %d names no syscall" % i)
|
||||
print(" config: %d rule(s), actions=%s"
|
||||
% (len(rules), sorted({r.get("action") for r in rules})))
|
||||
except (ValueError, OSError) as exc:
|
||||
problems.append("config/syscall_mask.json unreadable: %s" % exc)
|
||||
|
||||
for rel, want_exec in (("post-fs-data.sh", True), ("service.sh", True),
|
||||
("customize.sh", True), ("uninstall.sh", True)):
|
||||
p = os.path.join(d, rel)
|
||||
if not os.path.isfile(p):
|
||||
problems.append("%s missing" % rel)
|
||||
continue
|
||||
if want_exec and not os.access(p, os.X_OK):
|
||||
# Not fatal on a filesystem without an exec bit; the installer sets it.
|
||||
print(" note: %s is not executable here (the installer chmods it)" % rel)
|
||||
|
||||
for rel in ("kpm", "lkm", "ebpf"):
|
||||
p = os.path.join(d, rel)
|
||||
if os.path.isdir(p):
|
||||
print(" %-4s target present: %s"
|
||||
% (rel, ", ".join(sorted(os.listdir(p)))))
|
||||
print(" status: UNVERIFIED TEMPLATE -- not compiled, not loaded")
|
||||
|
||||
# The one static check worth doing on the C: the KPM interface has a
|
||||
# documented trap (an extern kfunc declaration compiles to *UND* and the
|
||||
# loader refuses the module), so catch it here rather than on a device.
|
||||
kpm_dir = os.path.join(d, "kpm")
|
||||
if os.path.isdir(kpm_dir):
|
||||
for name in sorted(os.listdir(kpm_dir)):
|
||||
if not name.endswith(".c"):
|
||||
continue
|
||||
with open(os.path.join(kpm_dir, name), encoding="utf-8") as fh:
|
||||
text = fh.read()
|
||||
code = _strip_c_comments(text)
|
||||
if re.search(r"\bextern\b[^;]*\bkfunc_def\b", code):
|
||||
problems.append("%s declares kfunc_def with `extern`, which the "
|
||||
"loader rejects as an unknown symbol" % name)
|
||||
for macro in ("KPM_NAME", "KPM_INIT", "KPM_EXIT"):
|
||||
if macro not in code:
|
||||
problems.append("%s is missing %s" % (name, macro))
|
||||
print(" kpm/%s: KPM_* lifecycle macros present, kfunc_def not extern"
|
||||
% name)
|
||||
|
||||
print()
|
||||
if problems:
|
||||
print("== %d problem(s) ==" % len(problems))
|
||||
for p in problems:
|
||||
print(" - %s" % p)
|
||||
return 1
|
||||
print("== 0 problem(s): structure is consistent ==")
|
||||
print(" (this says nothing about whether the kernel side compiles or loads)")
|
||||
return 0
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="kernelsu_syscall_mask.py",
|
||||
description=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
sub = parser.add_subparsers(dest="cmd")
|
||||
|
||||
p = sub.add_parser("gates", help="print the kernel-route gate table and probe this host",
|
||||
description="The gate table, plus whatever part of it can be "
|
||||
"checked from this machine. Device facts still have "
|
||||
"to be checked on the device.")
|
||||
p.set_defaults(func=cmd_gates)
|
||||
|
||||
p = sub.add_parser("generate", help="write the module scaffold",
|
||||
description="Generate a KernelSU/APatch-compatible module "
|
||||
"directory. The userspace half is installable; the "
|
||||
"kernel half is a template.")
|
||||
p.add_argument("--out", required=True, help="directory to write into")
|
||||
p.add_argument("--id", default="syscall-mask", help="module id / artifact name")
|
||||
p.add_argument("--name", default="Syscall Mask", help="human-readable module name")
|
||||
p.add_argument("--version", default="0.1.0", help="module version string")
|
||||
p.add_argument("--description", default="config-driven syscall return masking "
|
||||
"(openat/read/stat on /proc/self/*)",
|
||||
help="module description")
|
||||
p.add_argument("--package", default="<PKG>", help="target package placeholder")
|
||||
p.add_argument("--rules", default=None,
|
||||
help="JSON file with a {rules:[...]} rule set (default: built-in)")
|
||||
p.add_argument("--targets", default="kpm,lkm,ebpf",
|
||||
help="comma-separated kernel targets to emit (kpm,lkm,ebpf)")
|
||||
p.set_defaults(func=cmd_generate)
|
||||
|
||||
p = sub.add_parser("verify", help="read-only consistency checks over a generated dir",
|
||||
description="Check module.prop fields, the rule JSON, and the "
|
||||
"static traps in the kernel template. Says nothing "
|
||||
"about compilation or loading.")
|
||||
p.add_argument("--dir", required=True, help="a directory from `generate`")
|
||||
p.set_defaults(func=cmd_verify)
|
||||
|
||||
args = parser.parse_args(argv)
|
||||
if not getattr(args, "cmd", None):
|
||||
parser.print_help()
|
||||
return 2
|
||||
return args.func(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
File diff suppressed because it is too large.
Load diff
@@ -159,6 +159,474 @@ def ensure_keystore(ks_path, alias, password, keytool):
|
||||
return pwd
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Split APK / App Bundle sets
|
||||
#
|
||||
# A store build is often NOT one file. An App Bundle turns into
|
||||
# base.apk + split_config.arm64_v8a.apk + split_config.xxhdpi.apk + ...
|
||||
# and every member carries the same package name and its own signature. Two
|
||||
# consequences, and they are the reason this section exists:
|
||||
#
|
||||
# * Signing only the base leaves the set disagreeing about its certificate.
|
||||
# `pm install-multiple` then refuses the whole set, with a signature error
|
||||
# that points at the base rather than at the member you did not sign.
|
||||
# * "Just merge them into one APK" is free only for splits that carry CODE or
|
||||
# NATIVE LIBRARIES. A split that carries RESOURCES cannot be folded in
|
||||
# without merging `resources.arsc` -- rewriting the table, its global string
|
||||
# pool and its type/entry offsets -- which is a resource-compiler job, not a
|
||||
# byte-level one. A wrongly merged arsc produces an APK that installs and
|
||||
# then renders the wrong thing, or fails on the first resource lookup.
|
||||
#
|
||||
# So the job here is to say which of the two routes is legal for a given set and
|
||||
# then do that one, instead of always emitting the same artifact.
|
||||
# ---------------------------------------------------------------------------
|
||||
ABI_SPLIT_NAMES = ('armeabi', 'armeabi_v7a', 'arm64_v8a', 'x86', 'x86_64',
|
||||
'riscv64', 'mips', 'mips64')
|
||||
DENSITY_SPLIT_NAMES = ('ldpi', 'mdpi', 'tvdpi', 'hdpi', 'xhdpi', 'xxhdpi',
|
||||
'xxxhdpi', 'anydpi', 'nodpi')
|
||||
# A split is required to carry an `resources.arsc`, so an almost-empty table is
|
||||
# normal and must not be mistaken for real resources. Measured on a real set:
|
||||
# the density splits that hold nothing but a placeholder table are 40 bytes,
|
||||
# while the ones that actually own drawables are 9-15 KB.
|
||||
EMPTY_ARSC_BYTES = 1024
|
||||
|
||||
|
||||
def find_apks(root):
|
||||
"""Every .apk at or under `root`.
|
||||
|
||||
Recursive on purpose: `adb pull <dir> <dest>` creates `<dest>/<dir>/`, so a
|
||||
pulled set arrives one level deeper than the caller expects (measured).
|
||||
"""
|
||||
if os.path.isfile(root):
|
||||
return [os.path.abspath(root)]
|
||||
found = []
|
||||
for dirpath, dirnames, filenames in os.walk(root):
|
||||
dirnames[:] = [d for d in dirnames if not d.startswith('.')]
|
||||
for name in sorted(filenames):
|
||||
if name.lower().endswith('.apk'):
|
||||
found.append(os.path.abspath(os.path.join(dirpath, name)))
|
||||
return sorted(found)
|
||||
|
||||
|
||||
def axml_string_pool(blob):
|
||||
"""Decode the string pool of a binary AndroidManifest.xml. No dependencies.
|
||||
|
||||
Only the pool is decoded, not the element tree: every name this script needs
|
||||
(`package`, `split`, `configForSplit`, `isSplitRequired`) is a pool entry, and
|
||||
a hand-rolled element walk is where such parsers usually go wrong. The pool
|
||||
is also what makes equal-length hex patching of a manifest possible later.
|
||||
"""
|
||||
import struct
|
||||
if len(blob) < 36 or struct.unpack_from('<H', blob, 0)[0] != 0x0003:
|
||||
return None # not binary XML
|
||||
if struct.unpack_from('<H', blob, 8)[0] != 0x0001:
|
||||
return None # no string pool where one must be
|
||||
hdr_size = struct.unpack_from('<H', blob, 10)[0]
|
||||
count, _styles, flags, strings_start, _styles_start = struct.unpack_from(
|
||||
'<IIIII', blob, 16)
|
||||
if not 0 < count <= 1000000:
|
||||
return None
|
||||
offsets = struct.unpack_from('<%dI' % count, blob, 8 + hdr_size)
|
||||
utf8 = bool(flags & (1 << 8))
|
||||
base = 8 + strings_start
|
||||
out = []
|
||||
for off in offsets:
|
||||
p = base + off
|
||||
if p >= len(blob):
|
||||
return None
|
||||
try:
|
||||
if utf8:
|
||||
n = blob[p]
|
||||
p += 1
|
||||
if n & 0x80:
|
||||
n = ((n & 0x7F) << 8) | blob[p]
|
||||
p += 1
|
||||
m = blob[p]
|
||||
p += 1
|
||||
if m & 0x80:
|
||||
m = ((m & 0x7F) << 8) | blob[p]
|
||||
p += 1
|
||||
out.append(blob[p:p + m].decode('utf-8', 'replace'))
|
||||
else:
|
||||
n = struct.unpack_from('<H', blob, p)[0]
|
||||
p += 2
|
||||
if n & 0x8000:
|
||||
n = ((n & 0x7FFF) << 16) | struct.unpack_from('<H', blob, p)[0]
|
||||
p += 2
|
||||
out.append(blob[p:p + n * 2].decode('utf-16-le', 'replace'))
|
||||
except (IndexError, struct.error):
|
||||
return None
|
||||
return out
|
||||
|
||||
|
||||
def axml_root_attrs(blob):
|
||||
"""Attributes of the root <manifest> element, as {name: value}.
|
||||
|
||||
Starts at the first start-element chunk, which is the root element in every
|
||||
manifest Android produces. Values come back as strings where the pool has
|
||||
them and as formatted scalars otherwise, so a caller can print them without
|
||||
caring which encoding the compiler chose (a boolean is `0xffffffff`, not the
|
||||
word "true", in the binary form).
|
||||
"""
|
||||
import struct
|
||||
strings = axml_string_pool(blob)
|
||||
if strings is None:
|
||||
return None
|
||||
total = struct.unpack_from('<I', blob, 4)[0] or len(blob)
|
||||
end = min(len(blob), total)
|
||||
off = 8
|
||||
while off + 8 <= end:
|
||||
ctype, _hsize, csize = struct.unpack_from('<HHI', blob, off)
|
||||
if csize < 8 or off + csize > end:
|
||||
break
|
||||
if ctype == 0x0102: # RES_XML_START_ELEMENT
|
||||
attr_start, attr_size, attr_count = struct.unpack_from(
|
||||
'<HHH', blob, off + 24)
|
||||
attrs = {}
|
||||
first = off + 16 + attr_start
|
||||
for i in range(attr_count):
|
||||
p = first + i * attr_size
|
||||
if p + 20 > off + csize:
|
||||
break
|
||||
name_i, raw_i = struct.unpack_from('<II', blob, p + 4)
|
||||
_size, _res0, dtype = struct.unpack_from('<HBB', blob, p + 12)
|
||||
data = struct.unpack_from('<I', blob, p + 16)[0]
|
||||
name = strings[name_i] if name_i < len(strings) else '?'
|
||||
if dtype == 0x03:
|
||||
val = strings[data] if data < len(strings) else ''
|
||||
elif raw_i != 0xFFFFFFFF and raw_i < len(strings):
|
||||
val = strings[raw_i]
|
||||
elif dtype == 0x10:
|
||||
val = str(data - (1 << 32) if data >= (1 << 31) else data)
|
||||
elif dtype == 0x11:
|
||||
val = '0x%x' % data
|
||||
elif dtype == 0x12:
|
||||
val = 'true' if data else 'false'
|
||||
elif dtype == 0x01:
|
||||
val = '@ref/0x%08x' % data
|
||||
else:
|
||||
val = 'type=0x%02x/0x%x' % (dtype, data)
|
||||
attrs[name] = val
|
||||
return attrs
|
||||
off += csize
|
||||
return None
|
||||
|
||||
|
||||
def inspect_apk(path):
|
||||
"""Structural facts about one member of a split set, from the zip alone.
|
||||
|
||||
Nothing here needs a device or a resource compiler: which split owns dex,
|
||||
which owns native libraries and which owns resources is visible in the entry
|
||||
list, and the split's own name (and therefore its role) is a manifest
|
||||
attribute. Reading it from the file is what keeps this usable on a set whose
|
||||
package name is irrelevant to the caller.
|
||||
"""
|
||||
info = {'path': os.path.abspath(path), 'name': os.path.basename(path),
|
||||
'size': os.path.getsize(path), 'dex': [], 'abis': [], 'res': 0,
|
||||
'assets': 0, 'libs': [], 'arsc': None, 'arsc_stored': None,
|
||||
'package': None, 'split': None, 'config_for_split': None,
|
||||
'is_split_required': None, 'is_feature': None, 'readable': True}
|
||||
try:
|
||||
with zipfile.ZipFile(path, 'r') as z:
|
||||
names = []
|
||||
for item in z.infolist():
|
||||
name = item.filename
|
||||
names.append(name)
|
||||
if item.is_dir():
|
||||
continue
|
||||
if re.match(r'^classes\d*\.dex$', name):
|
||||
info['dex'].append(name)
|
||||
elif name == 'resources.arsc':
|
||||
info['arsc'] = item.file_size
|
||||
info['arsc_stored'] = (item.compress_type == 0)
|
||||
elif name.startswith('lib/') and name.count('/') >= 2:
|
||||
abi = name.split('/')[1]
|
||||
if abi not in info['abis']:
|
||||
info['abis'].append(abi)
|
||||
info['libs'].append(name)
|
||||
elif name.startswith('res/'):
|
||||
info['res'] += 1
|
||||
elif name.startswith('assets/'):
|
||||
info['assets'] += 1
|
||||
if 'AndroidManifest.xml' in names:
|
||||
attrs = axml_root_attrs(z.read('AndroidManifest.xml')) or {}
|
||||
info['package'] = attrs.get('package')
|
||||
info['split'] = attrs.get('split')
|
||||
info['config_for_split'] = attrs.get('configForSplit')
|
||||
info['is_split_required'] = attrs.get('isSplitRequired')
|
||||
info['is_feature'] = attrs.get('isFeatureSplit')
|
||||
except (zipfile.BadZipFile, OSError) as exc:
|
||||
info['readable'] = False
|
||||
info['error'] = str(exc)
|
||||
return info
|
||||
|
||||
|
||||
def split_role(info):
|
||||
"""What a member of a set contributes: base / abi / code / resources.
|
||||
|
||||
The declared split name is consulted first (it is the authoritative label),
|
||||
then the actual contents, because a set pulled off a device is often named
|
||||
`Foo-arm64_v8a.apk` while its manifest still says `config.arm64_v8a`.
|
||||
"""
|
||||
if not info.get('readable'):
|
||||
return 'unreadable'
|
||||
if not info.get('split'):
|
||||
return 'base'
|
||||
name = info['split']
|
||||
tail = name.split('.', 1)[1] if '.' in name else name
|
||||
if info['abis'] or tail in ABI_SPLIT_NAMES:
|
||||
return 'abi'
|
||||
if name.startswith('config.'):
|
||||
if tail in DENSITY_SPLIT_NAMES:
|
||||
return 'density'
|
||||
return 'config'
|
||||
if info['dex']:
|
||||
return 'code'
|
||||
return 'split'
|
||||
|
||||
|
||||
def split_has_real_resources(info):
|
||||
"""True when folding this split into the base would require merging arsc."""
|
||||
if info.get('res'):
|
||||
return True
|
||||
return (info.get('arsc') or 0) > EMPTY_ARSC_BYTES
|
||||
|
||||
|
||||
def pick_base(items):
|
||||
"""The base is the member with no `split` attribute; ties break toward dex."""
|
||||
cands = [i for i in items if not i.get('split')]
|
||||
if not cands:
|
||||
return None, []
|
||||
cands.sort(key=lambda i: (not i['dex'], -i['size']))
|
||||
return cands[0], cands[1:]
|
||||
|
||||
|
||||
def split_report(items):
|
||||
"""Human-readable inventory of a set: the input to the route decision."""
|
||||
rows = []
|
||||
for i in items:
|
||||
role = split_role(i)
|
||||
extra = []
|
||||
if i['dex']:
|
||||
extra.append('dex=%s' % ','.join(i['dex']))
|
||||
if i['abis']:
|
||||
extra.append('libs=%s' % ','.join(i['abis']))
|
||||
if i['res']:
|
||||
extra.append('res=%d entries' % i['res'])
|
||||
if i['arsc'] is not None:
|
||||
extra.append('arsc=%d%s' % (i['arsc'], '' if i['arsc_stored'] else ' COMPRESSED'))
|
||||
if i['assets']:
|
||||
extra.append('assets=%d' % i['assets'])
|
||||
rows.append('%-10s %-40s %10d B split=%-18s %s'
|
||||
% (role, i['name'], i['size'], i.get('split') or '-',
|
||||
' '.join(extra)))
|
||||
return rows
|
||||
|
||||
|
||||
def plan_split_set(items):
|
||||
"""Decide merge vs resign for this set, and say why in one sentence each.
|
||||
|
||||
Returns (base, others, blockers, notes). `blockers` non-empty means merging
|
||||
is not legal without explicitly accepting a loss, and the message names the
|
||||
splits responsible.
|
||||
"""
|
||||
notes, blockers = [], []
|
||||
base, extras = pick_base(items)
|
||||
if base is None:
|
||||
return None, [], ['no base APK in the set (no member lacks a `split` '
|
||||
'attribute) -- this does not look like a split set'], notes
|
||||
others = [i for i in items if i is not base]
|
||||
for i in extras:
|
||||
notes.append('extra candidate without a `split` attribute: %s' % i['name'])
|
||||
pkgs = sorted({i['package'] for i in items if i.get('package')})
|
||||
if len(pkgs) > 1:
|
||||
notes.append('members disagree about the package name: %s -- a set must '
|
||||
'share one, so these files probably do not belong together'
|
||||
% ', '.join(pkgs))
|
||||
if base.get('is_split_required') == 'true':
|
||||
notes.append('base sets isSplitRequired=true: after a merge the platform '
|
||||
'will refuse to start it unless the installer still knows the '
|
||||
'set, so a merged build is only safe once that attribute is '
|
||||
'cleared')
|
||||
if not base['dex']:
|
||||
notes.append('base carries no classes*.dex (unusual: %s)'
|
||||
% (', '.join(base['dex']) or 'none'))
|
||||
carrying = [i for i in others if split_has_real_resources(i)]
|
||||
if carrying:
|
||||
blockers.append('merging is not legal for %d of %d splits: %s carry their '
|
||||
'own resources, and folding those in means merging '
|
||||
'resources.arsc (a resource-compiler job). Use '
|
||||
'--split-mode resign, or pass --drop-split-resources to '
|
||||
'accept losing exactly those resources'
|
||||
% (len(carrying), len(others),
|
||||
', '.join(i.get('split') or i['name'] for i in carrying)))
|
||||
elif others:
|
||||
notes.append('every non-base split carries only code/native libraries, so '
|
||||
'a merged single APK is legal')
|
||||
fabis = [i for i in others if i['abis']]
|
||||
for i in fabis:
|
||||
notes.append('%s provides libs for %s -- keep only the ABIs the target '
|
||||
'device runs, selected with --abi'
|
||||
% (i.get('split') or i['name'], ','.join(i['abis'])))
|
||||
return base, others, blockers, notes
|
||||
|
||||
|
||||
def collect_split_inputs(args):
|
||||
"""Every APK the caller pointed at as part of one set (empty = not a set)."""
|
||||
if not (args.split_dir or args.split):
|
||||
return []
|
||||
paths = []
|
||||
if args.split_dir:
|
||||
paths += find_apks(os.path.abspath(args.split_dir))
|
||||
for p in (args.split or []):
|
||||
ap = os.path.abspath(p)
|
||||
paths += find_apks(ap) if os.path.isdir(ap) else [ap]
|
||||
if args.apk:
|
||||
ap = os.path.abspath(args.apk)
|
||||
if ap not in paths:
|
||||
paths.append(ap)
|
||||
# A set is a set: dedupe by real path so `--split-dir X --split X/base.apk`
|
||||
# does not try to merge an APK into itself.
|
||||
seen, out = set(), []
|
||||
for p in paths:
|
||||
key = os.path.normcase(p)
|
||||
if key not in seen:
|
||||
seen.add(key)
|
||||
out.append(p)
|
||||
return out
|
||||
|
||||
|
||||
def abi_of_split(info):
|
||||
"""The ABI a split provides, from its lib directories or its split name."""
|
||||
if info['abis']:
|
||||
return info['abis'][0]
|
||||
name = (info.get('split') or '').split('.', 1)[-1]
|
||||
return name.replace('_', '-') if name in ABI_SPLIT_NAMES else None
|
||||
|
||||
|
||||
def merge_split_set(base, others, out_apk, allow_drop_resources=False, keep_abi=None):
|
||||
"""Fold a set into one standalone APK: base plus every foldable member.
|
||||
|
||||
Folded: `classes*.dex` (renumbered so the second dex becomes classes2.dex),
|
||||
`lib/**` and `assets/**`. Not folded: `res/**` and `resources.arsc`, because
|
||||
a correct resource merge rewrites the table, and a wrong one is invisible
|
||||
until the app renders.
|
||||
|
||||
Returns (stats, dropped) so the caller can report the cost out loud instead
|
||||
of shipping a silently downgraded build.
|
||||
"""
|
||||
plan = []
|
||||
taken = set()
|
||||
stats = {'dex': 0, 'libs': 0, 'assets': 0, 'conflicts': 0}
|
||||
dropped = []
|
||||
|
||||
with zipfile.ZipFile(base['path'], 'r') as z:
|
||||
for item in z.infolist():
|
||||
name = item.filename
|
||||
if item.is_dir() or is_signature_entry(name):
|
||||
continue
|
||||
taken.add(name)
|
||||
plan.append((name, item, z.read(name),
|
||||
name in STORE_ONLY or os.path.basename(name) in STORE_ONLY))
|
||||
base_dex = len([n for n in taken if re.match(r'^classes\d*\.dex$', n)])
|
||||
next_dex = base_dex + 1 # a base with no dex still wants classes.dex first
|
||||
|
||||
for info in others:
|
||||
if info['abis'] and keep_abi and keep_abi not in abi_of_split(info):
|
||||
dropped.append('%s (ABI %s, keeping %s)' % (info['name'], abi_of_split(info), keep_abi))
|
||||
continue
|
||||
if split_has_real_resources(info) and not allow_drop_resources:
|
||||
raise SystemExit(
|
||||
'error: refusing to merge %s -- it carries its own resources '
|
||||
'(%d res entries, arsc=%s bytes).\n'
|
||||
' Folding it in would require merging resources.arsc, which this '
|
||||
'script does not do.\n'
|
||||
' Either use --split-mode resign (recommended for a resource '
|
||||
'split), or pass --drop-split-resources to accept that the '
|
||||
'resources it owns are lost.'
|
||||
% (info['name'], info['res'], info['arsc']))
|
||||
with zipfile.ZipFile(info['path'], 'r') as z:
|
||||
for item in z.infolist():
|
||||
name = item.filename
|
||||
if item.is_dir() or is_signature_entry(name):
|
||||
continue
|
||||
if name == 'AndroidManifest.xml':
|
||||
continue # the base manifest already exists
|
||||
if name == 'resources.arsc' or name.startswith('res/'):
|
||||
dropped.append('%s: %s' % (info['name'], name))
|
||||
continue
|
||||
if re.match(r'^classes\d*\.dex$', name):
|
||||
new = 'classes%d.dex' % next_dex if next_dex > 1 else 'classes.dex'
|
||||
while new in taken:
|
||||
next_dex += 1
|
||||
new = 'classes%d.dex' % next_dex
|
||||
data = z.read(name)
|
||||
zi = zipfile.ZipInfo(new, date_time=(2024, 1, 1, 0, 0, 0))
|
||||
plan.append((new, zi, data, False))
|
||||
taken.add(new)
|
||||
stats['dex'] += 1
|
||||
log('[merge] + %s (%d bytes <- %s:%s)' % (new, len(data), info['name'], name))
|
||||
next_dex += 1
|
||||
continue
|
||||
data = z.read(name)
|
||||
if name in taken:
|
||||
existing = next((d for n, _i, d, _k in plan if n == name), None)
|
||||
if existing == data:
|
||||
continue
|
||||
stats['conflicts'] += 1
|
||||
log('[merge] CONFLICT %s: %s and the base both provide it with '
|
||||
'different content; keeping the base copy' % (name, info['name']))
|
||||
continue
|
||||
plan.append((name, item, data, name in STORE_ONLY))
|
||||
taken.add(name)
|
||||
if name.startswith('lib/'):
|
||||
stats['libs'] += 1
|
||||
elif name.startswith('assets/'):
|
||||
stats['assets'] += 1
|
||||
log('[merge] + %s (%d bytes <- %s)' % (name, len(data), info['name']))
|
||||
|
||||
_write_aligned_zip(out_apk, plan)
|
||||
return stats, dropped
|
||||
|
||||
|
||||
def resign_split_set(items, outdir, sign_one):
|
||||
"""Sign every member of a set with the same keystore, keeping the structure.
|
||||
|
||||
Each member is de-signed and rewritten as an aligned archive first, exactly
|
||||
like a single-APK repack -- a member whose `resources.arsc` is compressed or
|
||||
unaligned is refused by the installer on its own, and the error names the
|
||||
member, not the set.
|
||||
"""
|
||||
os.makedirs(outdir, exist_ok=True)
|
||||
scratch = os.path.join(outdir, '.unsigned')
|
||||
if os.path.isdir(scratch):
|
||||
shutil.rmtree(scratch, ignore_errors=True)
|
||||
os.makedirs(scratch, exist_ok=True)
|
||||
outs = []
|
||||
for info in items:
|
||||
unsigned = os.path.join(scratch, info['name'])
|
||||
build_unsigned(info['path'], {}, unsigned)
|
||||
problems = check_alignment(unsigned)
|
||||
for p in problems:
|
||||
log('[resign] alignment problem in %s: %s' % (info['name'], p))
|
||||
signed = sign_one(unsigned, os.path.join(outdir, info['name']))
|
||||
outs.append((info, signed))
|
||||
log('[resign] %s -> %s (%d bytes)' % (info['name'], signed, os.path.getsize(signed)))
|
||||
shutil.rmtree(scratch, ignore_errors=True)
|
||||
return outs
|
||||
|
||||
|
||||
def cert_fingerprints(apk, tools):
|
||||
"""SHA-256 of each signer certificate, as reported by apksigner."""
|
||||
apksigner = tools.get('apksigner')
|
||||
if not apksigner:
|
||||
return []
|
||||
cmd = signer_invocation(apksigner, tools) + ['verify', '--print-certs', apk]
|
||||
r = run(cmd)
|
||||
text = (r.stdout or '') + (r.stderr or '')
|
||||
return sorted({m.lower() for m in re.findall(r'SHA-256 digest:\s*([0-9a-fA-F:]+)', text)})
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Packing
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -422,20 +890,26 @@ def check_alignment(apk):
|
||||
|
||||
|
||||
def zip_report(apk):
|
||||
"""Entry summary, including the storage/alignment facts Android gates on."""
|
||||
"""Entry summary, including the storage/alignment facts Android gates on.
|
||||
|
||||
Only entries that MUST be aligned are judged on alignment. `AndroidManifest.xml`
|
||||
must be STORED but not aligned, and printing "NOT ALIGNED" next to it read as a
|
||||
failing build when it is the normal, correct layout.
|
||||
"""
|
||||
import struct
|
||||
rows = []
|
||||
with zipfile.ZipFile(apk, 'r') as z, open(apk, 'rb') as fh:
|
||||
for i in z.infolist():
|
||||
base = os.path.basename(i.filename)
|
||||
needs = (i.filename in STORE_ONLY or base in STORE_ONLY
|
||||
or _needs_alignment(i.filename))
|
||||
must_align = _needs_alignment(i.filename)
|
||||
needs = (i.filename in STORE_ONLY or base in STORE_ONLY or must_align)
|
||||
if needs:
|
||||
fh.seek(i.header_offset)
|
||||
lh = fh.read(30)
|
||||
nlen, elen = struct.unpack('<HH', lh[26:30])
|
||||
data_off = i.header_offset + 30 + nlen + elen
|
||||
verdict = 'aligned' if data_off % ALIGN == 0 else 'NOT ALIGNED'
|
||||
verdict = ('aligned' if data_off % ALIGN == 0
|
||||
else 'NOT ALIGNED') if must_align else '-'
|
||||
stored = 'STORED' if i.compress_type == 0 else \
|
||||
'COMPRESSED(method=%d)' % i.compress_type
|
||||
rows.append('%-40s %-22s offset=0x%-8x %s'
|
||||
@@ -449,6 +923,88 @@ def zip_report(apk):
|
||||
# ---------------------------------------------------------------------------
|
||||
# Sign / verify
|
||||
# ---------------------------------------------------------------------------
|
||||
def signer_invocation(path, tools):
|
||||
"""Command prefix that runs apksigner, whatever shape it was shipped as.
|
||||
|
||||
A build-tools directory offers `apksigner.bat` (Windows), `apksigner` (a
|
||||
shell wrapper) or the bare `lib/apksigner.jar`. All three are legitimate
|
||||
values for --apksigner, and only the jar needs java in front of it. A bare
|
||||
shell wrapper does not run on Windows, which is why the jar is the value to
|
||||
pass there.
|
||||
"""
|
||||
if path.lower().endswith('.jar'):
|
||||
java = require(tools, 'java', 'run apksigner.jar')
|
||||
return [java, '-jar', path]
|
||||
return [path]
|
||||
|
||||
|
||||
def sign_with_apksigner(unsigned_apk, out_apk, ks, alias, password, tools):
|
||||
"""zipalign + apksigner (v1+v2+v3) -- the route that needs no uber-apk-signer.
|
||||
|
||||
Order is load-bearing: align first, sign second. apksigner adds signature
|
||||
entries without reshuffling the archive; the v1 (JAR) path emulates
|
||||
jarsigner, whose rewrite of the zip would destroy the alignment that Android
|
||||
R+ requires of `resources.arsc`.
|
||||
"""
|
||||
apksigner = require(tools, 'apksigner', 'sign the apk')
|
||||
src = unsigned_apk
|
||||
aligned = None
|
||||
if tools.get('zipalign'):
|
||||
aligned = unsigned_apk + '.aligned'
|
||||
r = run([tools['zipalign'], '-p', '-f', '4', unsigned_apk, aligned])
|
||||
log('[sign:zipalign] rc=%d' % r.returncode)
|
||||
if r.returncode != 0 or not os.path.exists(aligned):
|
||||
log(((r.stdout or '') + (r.stderr or '')).strip())
|
||||
raise SystemExit('error: zipalign failed; refusing to sign an '
|
||||
'unaligned build')
|
||||
src = aligned
|
||||
else:
|
||||
log('[sign:zipalign] skipped: zipalign not available; the archive written '
|
||||
'by this script is already 4-byte aligned (see the alignment gate)')
|
||||
try:
|
||||
cmd = signer_invocation(apksigner, tools) + [
|
||||
'sign', '--ks', ks, '--ks-key-alias', alias,
|
||||
'--ks-pass', 'pass:' + password, '--key-pass', 'pass:' + password,
|
||||
'--v1-signing-enabled', 'true', '--v2-signing-enabled', 'true',
|
||||
'--v3-signing-enabled', 'true', '--out', out_apk, src]
|
||||
r = run(cmd)
|
||||
log('[sign:apksigner] rc=%d' % r.returncode)
|
||||
log(((r.stdout or '') + (r.stderr or '')).strip())
|
||||
if r.returncode != 0 or not os.path.exists(out_apk):
|
||||
raise SystemExit('error: apksigner failed (output above)')
|
||||
finally:
|
||||
if aligned and os.path.exists(aligned):
|
||||
os.remove(aligned)
|
||||
return out_apk
|
||||
|
||||
|
||||
def choose_signer(args, tools):
|
||||
"""Pick the signing route: the jar when it exists, else apksigner directly.
|
||||
|
||||
The jar stays first so an existing workflow does not change behaviour. The
|
||||
apksigner branch exists because a machine can have a complete build-tools
|
||||
directory and no uber-apk-signer at all -- the previous single-route design
|
||||
made every signed build impossible there, with an error that named a jar the
|
||||
user never had.
|
||||
"""
|
||||
jar = args.signer_jar or os.environ.get('APK_SIGNER_JAR', 'uber-apk-signer.jar')
|
||||
kind = getattr(args, 'signer', 'auto')
|
||||
if kind in ('auto', 'jar') and os.path.exists(jar):
|
||||
return {'kind': 'jar', 'jar': jar}
|
||||
if kind == 'jar':
|
||||
raise SystemExit('error: --signer jar requested but %s does not exist'
|
||||
% jar)
|
||||
if tools.get('apksigner'):
|
||||
return {'kind': 'apksigner', 'path': tools['apksigner']}
|
||||
raise SystemExit(
|
||||
'error: no signing route available.\n'
|
||||
' * uber-apk-signer.jar not found at %s\n'
|
||||
' * apksigner not found on PATH either (pass --apksigner <path>; it may\n'
|
||||
' be apksigner.bat, an apksigner wrapper, or lib/apksigner.jar)\n'
|
||||
' A machine with Android build-tools and no uber-apk-signer is supported:\n'
|
||||
' give it --apksigner and this script aligns and signs on its own.' % jar)
|
||||
|
||||
|
||||
def sign_apk(unsigned_apk, workdir, ks, alias, password, tools, signer_jar):
|
||||
java = require(tools, 'java', 'run the signer jar')
|
||||
if not os.path.exists(signer_jar):
|
||||
@@ -498,8 +1054,9 @@ def verify_apk(apk, tools, signer_jar=None, expect_signed=True):
|
||||
# are meaningful.
|
||||
if tools.get('apksigner'):
|
||||
sig_checks += 1
|
||||
r = run([tools['apksigner'], 'verify', '--print-certs', '--verbose',
|
||||
'--min-sdk-version', '21', '--max-sdk-version', '34', apk])
|
||||
r = run(signer_invocation(tools['apksigner'], tools) + [
|
||||
'verify', '--print-certs', '--verbose',
|
||||
'--min-sdk-version', '21', '--max-sdk-version', '34', apk])
|
||||
log('[verify:apksigner] rc=%d\n%s' % (r.returncode, ((r.stdout or '') +
|
||||
(r.stderr or '')).strip()[:3000]))
|
||||
if r.returncode != 0:
|
||||
@@ -559,27 +1116,295 @@ def verify_apk(apk, tools, signer_jar=None, expect_signed=True):
|
||||
|
||||
|
||||
def fingerprint(apk, tools):
|
||||
if not tools.get('keytool'):
|
||||
return []
|
||||
r = run([tools['keytool'], '-printcert', '-jarfile', apk])
|
||||
return re.findall(r'(SHA1|SHA256):\s*([0-9A-F:]+)', r.stdout or '')
|
||||
"""Signer fingerprints. keytool when there is one, apksigner otherwise.
|
||||
|
||||
keytool is frequently absent (it lives in a JDK bin directory that is not on
|
||||
PATH), and reporting no fingerprint at all reads as "unsigned". apksigner is
|
||||
the tool that actually signed the file, so its answer is the authoritative
|
||||
one anyway.
|
||||
"""
|
||||
if tools.get('keytool'):
|
||||
r = run([tools['keytool'], '-printcert', '-jarfile', apk])
|
||||
found = re.findall(r'(SHA1|SHA256):\s*([0-9A-F:]+)', r.stdout or '')
|
||||
if found:
|
||||
return found
|
||||
return [('SHA-256', f) for f in cert_fingerprints(apk, tools)]
|
||||
|
||||
|
||||
def resolve_keystore(args, tools):
|
||||
"""Return (keystore path, password), creating the keystore on first use."""
|
||||
if not args.ks:
|
||||
raise SystemExit(
|
||||
"error: --ks is required when signing (there is no default keystore).\n"
|
||||
" e.g. --ks work/out/release.keystore")
|
||||
keytool = require(tools, 'keytool', 'create the keystore on first use')
|
||||
ks = os.path.abspath(args.ks)
|
||||
return ks, ensure_keystore(ks, args.ks_alias, args.ks_pass, keytool)
|
||||
|
||||
|
||||
def sign_one_artifact(unsigned_apk, out_path, args, tools, signer, ks, password, workdir):
|
||||
"""Sign one unsigned APK through whichever route choose_signer() picked."""
|
||||
if signer['kind'] == 'jar':
|
||||
signed = sign_apk(unsigned_apk, workdir, ks, args.ks_alias, password,
|
||||
tools, signer['jar'])
|
||||
if os.path.abspath(signed) != os.path.abspath(out_path):
|
||||
shutil.copy2(signed, out_path)
|
||||
else:
|
||||
sign_with_apksigner(unsigned_apk, out_path, ks, args.ks_alias, password, tools)
|
||||
return out_path
|
||||
|
||||
|
||||
def scratch_dir(args, fallback):
|
||||
workdir = os.path.abspath(args.workdir) if args.workdir else fallback
|
||||
if not workdir:
|
||||
workdir = os.getcwd()
|
||||
os.makedirs(workdir, exist_ok=True)
|
||||
return workdir
|
||||
|
||||
|
||||
def run_split_mode(args, tools):
|
||||
"""Handle --split-dir / --split: inventory the set, pick a legal route, act.
|
||||
|
||||
Nothing here guesses which route "should" work: the set is inspected, merge
|
||||
legality is decided from what the splits actually carry, and the reason is
|
||||
printed before anything is written.
|
||||
"""
|
||||
items = [inspect_apk(p) for p in collect_split_inputs(args)]
|
||||
broken = [i['name'] for i in items if not i['readable']]
|
||||
if broken:
|
||||
raise SystemExit('error: not readable as an APK: %s' % ', '.join(broken))
|
||||
|
||||
log('== split set: %d apk(s), %d bytes total'
|
||||
% (len(items), sum(i['size'] for i in items)))
|
||||
for row in split_report(items):
|
||||
log(' ' + row)
|
||||
|
||||
base, others, blockers, notes = plan_split_set(items)
|
||||
if base is None or not others:
|
||||
for b in blockers:
|
||||
log('[plan] BLOCKED: %s' % b)
|
||||
if base is not None:
|
||||
log('[plan] %s is the only member -- a single APK: run without '
|
||||
'--split/--split-dir' % base['name'])
|
||||
return 2
|
||||
log('[plan] base = %s (package=%s)' % (base['name'], base.get('package')))
|
||||
for n in notes:
|
||||
log('[plan] note: %s' % n)
|
||||
for b in blockers:
|
||||
log('[plan] MERGE BLOCKED: %s' % b)
|
||||
if not blockers:
|
||||
log('[plan] merge = legal: %d member(s) fold into the base' % len(others))
|
||||
log('[plan] resign = legal: one keystore for all %d members, install the set with'
|
||||
% len(items))
|
||||
log('[plan] adb install-multiple -r %s'
|
||||
% ' '.join(i['name'] for i in items))
|
||||
|
||||
mode = args.split_mode
|
||||
if mode == 'auto':
|
||||
if blockers:
|
||||
mode = 'resign'
|
||||
elif (args.out or '').lower().endswith('.apk'):
|
||||
mode = 'merge'
|
||||
else:
|
||||
mode = 'resign'
|
||||
log('[plan] --split-mode auto resolved to: %s' % mode)
|
||||
|
||||
if mode == 'analyze':
|
||||
log('[result] analyze only: nothing was written. Pass --split-mode '
|
||||
'merge|resign|auto to act on this set.')
|
||||
return 0
|
||||
|
||||
if mode == 'merge':
|
||||
return _split_merge(args, tools, base, others)
|
||||
|
||||
outdir = args.split_out_dir or (os.path.abspath(args.out) if args.out else None)
|
||||
if not outdir:
|
||||
raise SystemExit('error: --split-mode resign needs --split-out-dir <dir> '
|
||||
'(or --out, which is then treated as a directory).')
|
||||
outdir = os.path.abspath(outdir)
|
||||
log('[resign] output directory: %s' % outdir)
|
||||
workdir = scratch_dir(args, os.path.join(outdir, '.work'))
|
||||
signer = None
|
||||
ks = password = None
|
||||
if not args.no_sign:
|
||||
signer = choose_signer(args, tools)
|
||||
ks, password = resolve_keystore(args, tools)
|
||||
log('[resign] signer route: %s' % signer['kind'])
|
||||
|
||||
if args.no_sign:
|
||||
def sign_member(unsigned, out_path):
|
||||
shutil.copy2(unsigned, out_path)
|
||||
return out_path
|
||||
else:
|
||||
def sign_member(unsigned, out_path):
|
||||
return sign_one_artifact(unsigned, out_path, args, tools, signer,
|
||||
ks, password, workdir)
|
||||
|
||||
outs = resign_split_set(items, outdir, sign_member)
|
||||
|
||||
fps = set()
|
||||
ok_all = True
|
||||
signer_jar = signer['jar'] if signer and signer['kind'] == 'jar' else None
|
||||
for info, path in outs:
|
||||
if not verify_apk(path, tools, signer_jar, expect_signed=not args.no_sign):
|
||||
ok_all = False
|
||||
for f in cert_fingerprints(path, tools):
|
||||
fps.add(f)
|
||||
log('[cert] %s %s' % (info['name'], f))
|
||||
|
||||
if not args.no_sign:
|
||||
if len(fps) == 1:
|
||||
log('[result] OK: %d members, one certificate (sha256 %s)'
|
||||
% (len(outs), sorted(fps)[0]))
|
||||
else:
|
||||
log('[result] CHECK-FAILED: %d distinct certificates across the set '
|
||||
'(a mismatch is exactly what an installer rejects): %s'
|
||||
% (len(fps), ', '.join(sorted(fps))))
|
||||
ok_all = False
|
||||
else:
|
||||
log('[result] UNSIGNED (--no-sign): de-signed and aligned set, not installable '
|
||||
'as-is')
|
||||
log('[next] adb install-multiple -r %s'
|
||||
% ' '.join(os.path.join(outdir, i['name']) for i in items))
|
||||
return 0 if ok_all else 2
|
||||
|
||||
|
||||
def _split_merge(args, tools, base, others):
|
||||
"""The merge half of run_split_mode (kept separate to stay readable)."""
|
||||
if not args.out:
|
||||
raise SystemExit('error: --split-mode merge needs --out <merged.apk>')
|
||||
out = os.path.abspath(args.out)
|
||||
folder = os.path.dirname(out)
|
||||
if folder:
|
||||
os.makedirs(folder, exist_ok=True)
|
||||
unsigned = out + '.unsigned'
|
||||
if os.path.exists(unsigned):
|
||||
os.remove(unsigned)
|
||||
|
||||
stats, dropped = merge_split_set(base, others, unsigned,
|
||||
allow_drop_resources=args.drop_split_resources,
|
||||
keep_abi=args.abi)
|
||||
log('[merge] folded: dex=%d libs=%d assets=%d conflicts=%d'
|
||||
% (stats['dex'], stats['libs'], stats['assets'], stats['conflicts']))
|
||||
if dropped:
|
||||
log('[merge] DROPPED %d entry/ies. This is a DOWNGRADED build -- say so when '
|
||||
'you deliver it:' % len(dropped))
|
||||
for d in dropped[:40]:
|
||||
log(' - %s' % d)
|
||||
if len(dropped) > 40:
|
||||
log(' ... and %d more' % (len(dropped) - 40))
|
||||
|
||||
for row in zip_report(unsigned):
|
||||
log('[zip] ' + row)
|
||||
problems = check_alignment(unsigned)
|
||||
if problems:
|
||||
log('== ALIGNMENT/STORAGE GATE FAILED -- do not ship this build:')
|
||||
for p in problems:
|
||||
log(' - %s' % p)
|
||||
else:
|
||||
log('== alignment gate: resources.arsc STORED and 4-byte aligned (OK)')
|
||||
|
||||
if args.no_sign:
|
||||
shutil.move(unsigned, out)
|
||||
log('[out] %s (%d bytes)' % (out, os.path.getsize(out)))
|
||||
log('[result] UNSIGNED (--no-sign): inspection only, not installable as-is')
|
||||
return 0
|
||||
|
||||
workdir = scratch_dir(args, folder or os.getcwd())
|
||||
signer = choose_signer(args, tools)
|
||||
ks, password = resolve_keystore(args, tools)
|
||||
log('[merge] signer route: %s' % signer['kind'])
|
||||
sign_one_artifact(unsigned, out, args, tools, signer, ks, password, workdir)
|
||||
os.remove(unsigned)
|
||||
log('[out] %s (%d bytes)' % (out, os.path.getsize(out)))
|
||||
|
||||
ok = verify_apk(out, tools,
|
||||
signer['jar'] if signer['kind'] == 'jar' else None)
|
||||
for algo, val in fingerprint(out, tools):
|
||||
log('[cert] %s %s' % (algo, val))
|
||||
log('[result] %s' % ('OK' if ok else 'CHECK-FAILED'))
|
||||
log('[next] adb install -r %s (one file, no set to keep together)' % out)
|
||||
return 0 if ok else 2
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
description='Repack an APK with replaced dex files, drop only the signature '
|
||||
'entries, re-sign and verify.')
|
||||
ap.add_argument('--apk', required=True, help='source apk to use as the template')
|
||||
ap.add_argument('--out', required=True, help='output apk path')
|
||||
'entries, re-sign and verify.\n'
|
||||
'Also handles SPLIT APK / App Bundle sets (base.apk plus\n'
|
||||
'split_config.*.apk): inventory them, then either sign the whole\n'
|
||||
'set with one keystore (installable with `pm install-multiple`)\n'
|
||||
'or fold the code/native members into one standalone APK.',
|
||||
epilog='usage examples:\n'
|
||||
' # single APK (unchanged behaviour)\n'
|
||||
' python repack.py --apk original.apk --dexdir dex/ --out out.apk '
|
||||
'--ks ks.jks\n'
|
||||
'\n'
|
||||
' # inspect a split set and get the route decision (writes nothing)\n'
|
||||
' python repack.py --split-dir pulled_set/ --split-mode analyze\n'
|
||||
'\n'
|
||||
' # resign every member with ONE keystore -> pm install-multiple\n'
|
||||
' python repack.py --split-dir pulled_set/ --split-mode resign \\\n'
|
||||
' --split-out-dir signed_set/ --ks ks.jks \\\n'
|
||||
' --apksigner /path/to/lib/apksigner.jar\n'
|
||||
'\n'
|
||||
' # fold code/native splits into one standalone APK\n'
|
||||
' python repack.py --split-dir pulled_set/ --split-mode merge \\\n'
|
||||
' --out merged.apk --ks ks.jks\n'
|
||||
'\n'
|
||||
' # no uber-apk-signer on this machine: --apksigner replaces it\n'
|
||||
' python repack.py --apk app.apk --out out.apk --ks ks.jks \\\n'
|
||||
' --apksigner /path/to/lib/apksigner.jar --zipalign '
|
||||
'/path/to/zipalign\n'
|
||||
'\n'
|
||||
'split route decision (enforced, not guessed):\n'
|
||||
' merge is legal only when every non-base split carries code or\n'
|
||||
' native libraries. A split that carries its own resources\n'
|
||||
' cannot be folded in without merging resources.arsc, which\n'
|
||||
' needs a resource compiler; pass --drop-split-resources to\n'
|
||||
' accept losing exactly those resources.\n'
|
||||
' resign always works: one keystore, v1+v2+v3 on every member.\n')
|
||||
ap.add_argument('--apk', help='source apk to use as the template (in split mode: '
|
||||
'an explicit base; optional)')
|
||||
ap.add_argument('--out', help='output apk path (in --split-mode resign: the '
|
||||
'output directory, or use --split-out-dir)')
|
||||
ap.add_argument('--dexdir', help='directory of classes*.dex to swap in')
|
||||
ap.add_argument('--dex', action='append', default=[],
|
||||
help='name=path, repeatable, e.g. classes8.dex=work/patch/classes8.dex')
|
||||
ap.add_argument('--split-dir', default=None,
|
||||
help='directory holding a split set (searched recursively for '
|
||||
'*.apk); every member must share one package name')
|
||||
ap.add_argument('--split', action='append', default=[],
|
||||
help='one split member, repeatable; a directory is searched '
|
||||
'recursively. Supplying any --split/--split-dir switches '
|
||||
'this script into split-set mode')
|
||||
ap.add_argument('--split-mode', choices=('analyze', 'auto', 'merge', 'resign'),
|
||||
default='analyze',
|
||||
help='analyze (default): print the inventory and the route '
|
||||
'decision, write nothing. auto: merge when that is legal '
|
||||
'and --out ends in .apk, else resign. merge: fold into one '
|
||||
'standalone APK (--out). resign: sign every member with one '
|
||||
'keystore (--split-out-dir)')
|
||||
ap.add_argument('--split-out-dir', default=None,
|
||||
help='output directory for --split-mode resign')
|
||||
ap.add_argument('--drop-split-resources', action='store_true',
|
||||
help='merge mode: explicitly allow dropping splits whose '
|
||||
'resources cannot be merged (produces a downgraded build)')
|
||||
ap.add_argument('--abi', default=None,
|
||||
help='merge mode: keep only the ABI split matching this ABI '
|
||||
'(e.g. arm64-v8a). Compare with the device via '
|
||||
'`getprop ro.product.cpu.abilist`; split names use '
|
||||
'underscores (arm64_v8a), lib/ directories use hyphens')
|
||||
ap.add_argument('--workdir', default=None,
|
||||
help='scratch directory (default: next to --out)')
|
||||
ap.add_argument('--ks', default=None, help='keystore path (created on first use)')
|
||||
ap.add_argument('--ks-alias', default='apkreverse', help='keystore alias')
|
||||
ap.add_argument('--ks-pass', default=None,
|
||||
help='keystore password; default: <ks>.pass.txt, else generated')
|
||||
ap.add_argument('--signer', choices=('auto', 'jar', 'apksigner'), default='auto',
|
||||
help='signing route. auto (default): uber-apk-signer jar when it '
|
||||
'exists, otherwise zipalign+apksigner directly')
|
||||
ap.add_argument('--signer-jar', default=None,
|
||||
help='jar used for zipalign+sign (default: $APK_SIGNER_JAR or '
|
||||
'uber-apk-signer.jar)')
|
||||
@@ -588,25 +1413,32 @@ def main():
|
||||
ap.add_argument('--keytool', default=None, help='path to keytool (default: from PATH)')
|
||||
ap.add_argument('--jarsigner', default=None, help='path to jarsigner (default: from PATH)')
|
||||
ap.add_argument('--zipalign', default=None, help='path to zipalign (default: from PATH)')
|
||||
ap.add_argument('--apksigner', default=None, help='path to apksigner (default: from PATH)')
|
||||
ap.add_argument('--apksigner', default=None,
|
||||
help='path to apksigner (default: from PATH). Accepts '
|
||||
'apksigner.bat, an apksigner wrapper, or lib/apksigner.jar')
|
||||
args = ap.parse_args()
|
||||
|
||||
tools = resolve_tools(args)
|
||||
signer_jar = args.signer_jar or os.environ.get('APK_SIGNER_JAR', 'uber-apk-signer.jar')
|
||||
|
||||
split_mode = bool(args.split_dir or args.split)
|
||||
if not split_mode:
|
||||
if not args.apk:
|
||||
raise SystemExit('error: --apk is required (or supply --split/--split-dir '
|
||||
'for a split set).')
|
||||
if not args.out:
|
||||
raise SystemExit('error: --out is required.')
|
||||
|
||||
if split_mode:
|
||||
return run_split_mode(args, tools)
|
||||
|
||||
apk = os.path.abspath(args.apk)
|
||||
out = os.path.abspath(args.out)
|
||||
if not os.path.exists(apk):
|
||||
raise SystemExit('missing apk: %s' % apk)
|
||||
|
||||
# Default the scratch dir next to --out; never to a machine-specific path.
|
||||
workdir = os.path.abspath(args.workdir) if args.workdir else os.path.dirname(out)
|
||||
if not workdir:
|
||||
workdir = os.getcwd()
|
||||
os.makedirs(workdir, exist_ok=True)
|
||||
out_folder = os.path.dirname(out)
|
||||
if out_folder:
|
||||
os.makedirs(out_folder, exist_ok=True)
|
||||
workdir = scratch_dir(args, os.path.dirname(out))
|
||||
|
||||
repl = collect_replacement_dex(args.dexdir, args.dex)
|
||||
log('[in ] %s (%d bytes)' % (apk, os.path.getsize(apk)))
|
||||
@@ -622,19 +1454,14 @@ def main():
|
||||
for row in zip_report(unsigned):
|
||||
log('[zip] ' + row)
|
||||
|
||||
signer = None
|
||||
if args.no_sign:
|
||||
shutil.copy2(unsigned, out)
|
||||
else:
|
||||
if not args.ks:
|
||||
raise SystemExit(
|
||||
"error: --ks is required when signing (there is no default keystore).\n"
|
||||
" e.g. --ks work/out/release.keystore")
|
||||
keytool = require(tools, 'keytool', 'create the keystore on first use')
|
||||
password = ensure_keystore(os.path.abspath(args.ks), args.ks_alias,
|
||||
args.ks_pass, keytool)
|
||||
signed = sign_apk(unsigned, workdir, os.path.abspath(args.ks), args.ks_alias,
|
||||
password, tools, signer_jar)
|
||||
shutil.copy2(signed, out)
|
||||
signer = choose_signer(args, tools)
|
||||
log('[sign] route: %s' % signer['kind'])
|
||||
ks, password = resolve_keystore(args, tools)
|
||||
sign_one_artifact(unsigned, out, args, tools, signer, ks, password, workdir)
|
||||
|
||||
log('[out] %s (%d bytes)' % (out, os.path.getsize(out)))
|
||||
log('== zip layout of final apk')
|
||||
@@ -650,7 +1477,9 @@ def main():
|
||||
else:
|
||||
log('== alignment gate: resources.arsc STORED and 4-byte aligned (OK)')
|
||||
|
||||
ok = verify_apk(out, tools, signer_jar, expect_signed=not args.no_sign)
|
||||
ok = verify_apk(out, tools,
|
||||
signer['jar'] if signer and signer['kind'] == 'jar' else None,
|
||||
expect_signed=not args.no_sign)
|
||||
if not args.no_sign:
|
||||
for algo, val in fingerprint(out, tools):
|
||||
log('[cert] %s %s' % (algo, val))
|
||||
|
||||
@@ -11,13 +11,29 @@ The same app launched normally, after Frida has left, renders correctly.
|
||||
|
||||
The fix is an ordering rather than a different hook:
|
||||
|
||||
1. spawn and attach,
|
||||
2. let the probe write its patch into memory (``Memory.patchCode``),
|
||||
3. **detach** -- the write is a plain memory write and survives; ``Interceptor``
|
||||
hooks go away with the session, which is usually what you want for an
|
||||
observation run because it removes the instrumentation from the picture,
|
||||
1. spawn (`device.spawn()` leaves the target **paused**),
|
||||
2. attach and load the probe,
|
||||
3. resume, then **wait for the probe to report `PATCHED`** and only detach after
|
||||
that -- the write is a plain memory write and survives; ``Interceptor`` hooks go
|
||||
away with the session, which is usually what you want for an observation run
|
||||
because it removes the instrumentation from the picture,
|
||||
4. start the Activity normally and capture.
|
||||
|
||||
**The two orderings that do not work, both measured on MASTG UnCrackable-Level3
|
||||
(``libfoo.so`` self-destruct, ``goodbyev`` at ``base+0x3080``):**
|
||||
|
||||
- *Detach immediately after ``script.load()``.* The probe's ``Process.findModuleByName``
|
||||
poll has not seen the library yet, so the write never happens and the target kills
|
||||
itself on its own schedule. The measured run reported ``patched=False`` and the
|
||||
target was gone by the first capture.
|
||||
- *Hold the target paused until ``PATCHED``.* While the process is frozen its
|
||||
libraries are not mapped, so the poll can never succeed: 15 s paused produced no
|
||||
module, and ``libfoo.so base=0x764c0aa000`` appeared 0.11 s after resume.
|
||||
|
||||
So the target must be running for the patch to be possible at all, and the time
|
||||
between resume and the write is race time, not slack. Report ``patched=False`` loudly
|
||||
rather than presenting an unpatched run as a result.
|
||||
|
||||
What the probe may contain
|
||||
--------------------------
|
||||
Anything self-contained. A minimal probe that only neutralises a death site and
|
||||
@@ -63,7 +79,16 @@ def build_parser():
|
||||
p.add_argument("--serial", default=None, help="adb device serial (needed when several are online)")
|
||||
p.add_argument("--adb", default=DEFAULT_ADB, help="path to adb (default: from PATH)")
|
||||
p.add_argument("--wait-patched", type=float, default=15.0,
|
||||
help="seconds to wait for the probe to report PATCHED (default %(default)s)")
|
||||
help="seconds to wait for the probe to report PATCHED after resume; "
|
||||
"the detach happens only when it arrives (default %(default)s)")
|
||||
p.add_argument("--rpc-timeout", type=float, default=20.0,
|
||||
help="seconds to bound each frida RPC (attach / load), so a dead "
|
||||
"device server fails instead of hanging (default %(default)s)")
|
||||
p.add_argument("--playground", default=None,
|
||||
help="package to ask for the launcher activity when --activity is "
|
||||
"omitted (default: --package)")
|
||||
p.add_argument("--launch-timeout", type=float, default=30.0,
|
||||
help="seconds to bound the post-detach `am start` (default %(default)s)")
|
||||
p.add_argument("--detach-settle", type=float, default=3.0,
|
||||
help="pause after detach, before launching (default %(default)s)")
|
||||
p.add_argument("--captures", type=int, default=3, help="number of screenshots (default %(default)s)")
|
||||
@@ -86,6 +111,25 @@ def root_shell(cmd, adb, serial, timeout=200):
|
||||
return adb_run(["shell", "su -c '%s'" % cmd], adb, serial, timeout)
|
||||
|
||||
|
||||
def resolve_launcher(adb, serial, arg_activity, package, playground=None):
|
||||
"""Return the component to start, asking the device when none was given.
|
||||
|
||||
`am start <package>` alone is not accepted on every ROM, and starting nothing
|
||||
leaves the spawned process dead with the screen showing whatever was there
|
||||
before -- which reads exactly like "the app died". Ask the package manager.
|
||||
"""
|
||||
if arg_activity:
|
||||
return arg_activity
|
||||
pkg = playground or package
|
||||
r = adb_run(["shell", "cmd", "package", "resolve-activity", "--brief", pkg], adb, serial)
|
||||
for line in (r.stdout or "").splitlines():
|
||||
line = line.strip()
|
||||
if "/" in line and not line.startswith("priority") and "No activity" not in line:
|
||||
if line.count("/") == 1 and not line.startswith("Starting"):
|
||||
return line
|
||||
return None
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
args = build_parser().parse_args(argv)
|
||||
|
||||
@@ -95,6 +139,7 @@ def main(argv=None):
|
||||
return 2
|
||||
|
||||
patched = {"done": False}
|
||||
failed = {"done": False}
|
||||
|
||||
def on_message(msg, data):
|
||||
if msg.get("type") == "send":
|
||||
@@ -102,6 +147,8 @@ def main(argv=None):
|
||||
print("[MSG] %s" % payload, flush=True)
|
||||
if payload == "PATCHED":
|
||||
patched["done"] = True
|
||||
elif payload == "PATCHFAIL":
|
||||
failed["done"] = True
|
||||
elif msg.get("type") == "error":
|
||||
print("[ERR] %s" % (msg.get("stack") or msg.get("description")), flush=True)
|
||||
else:
|
||||
@@ -112,27 +159,47 @@ def main(argv=None):
|
||||
|
||||
dev = frida.get_device_manager().add_remote_device(args.frida_host)
|
||||
pid = dev.spawn([args.package])
|
||||
print("[i] spawned pid=%d" % pid, flush=True)
|
||||
print("[i] spawned pid=%d (paused)" % pid, flush=True)
|
||||
|
||||
session = dev.attach(pid)
|
||||
script = session.create_script(open(args.js, encoding="utf-8").read())
|
||||
script.on("message", on_message)
|
||||
script.load()
|
||||
|
||||
# Resume, then wait for the probe to report. Holding the target paused until
|
||||
# PATCHED does NOT work for a probe that waits on a module: while the process is
|
||||
# frozen its libraries are not mapped, so Process.findModuleByName() never
|
||||
# returns. Measured on MASTG UnCrackable-Level3: 15 s paused -> no module, then
|
||||
# "libfoo.so base=0x764c0aa000" 0.11 s after resume. What matters is that we do
|
||||
# NOT detach before the write has landed, and that we report patched=False rather
|
||||
# than pretending the target was patched.
|
||||
dev.resume(pid)
|
||||
print("[i] resumed pid=%d" % pid, flush=True)
|
||||
|
||||
deadline = time.time() + args.wait_patched
|
||||
while time.time() < deadline and not patched["done"]:
|
||||
while time.time() < deadline and not patched["done"] and not failed["done"]:
|
||||
time.sleep(0.2)
|
||||
if not patched["done"]:
|
||||
print("[!] the probe did not report PATCHED in %.1fs. Detaching anyway -- the "
|
||||
"target runs UNPATCHED, so nothing about this run tests the probe."
|
||||
% args.wait_patched, flush=True)
|
||||
print("[i] patched=%s" % patched["done"], flush=True)
|
||||
|
||||
time.sleep(1.0)
|
||||
time.sleep(0.5)
|
||||
session.detach()
|
||||
print("[i] detached (memory writes persist, hooks are gone)", flush=True)
|
||||
time.sleep(args.detach_settle)
|
||||
|
||||
if args.activity:
|
||||
out = adb_run(["shell", "am start -n %s" % args.activity], args.adb, args.serial)
|
||||
print("[i] am start: %s" % ((out.stdout or out.stderr or "").strip()), flush=True)
|
||||
component = resolve_launcher(args.adb, args.serial, args.activity, args.package,
|
||||
args.playground)
|
||||
if component:
|
||||
out = adb_run(["shell", "am start -n %s" % component], args.adb, args.serial,
|
||||
timeout=args.launch_timeout)
|
||||
print("[i] am start %s: %s" % (component,
|
||||
(out.stdout or out.stderr or "").strip()), flush=True)
|
||||
else:
|
||||
print("[!] no launcher component resolved; the spawned process may be dead. "
|
||||
"Pass --activity pkg/.Activity.", flush=True)
|
||||
|
||||
for i in range(max(1, args.captures)):
|
||||
time.sleep(args.interval)
|
||||
|
||||
@@ -43,6 +43,16 @@
|
||||
* exec = per-instruction (ENORMOUS; only for a few hundred
|
||||
* instructions around a known point; off by default);
|
||||
* ret = return edges (off by default).
|
||||
* excludeModules: modules Stalker must not translate (see the CONFIG comment).
|
||||
* Followed through libc/libart, an arm64 device pays a large
|
||||
* multiplier and the trace becomes a trace of the OS. Emitted
|
||||
* as one `EXCL excluded=N/M [names]` line before the follow --
|
||||
* read it, because it states what the trace was protected by.
|
||||
* A module not yet loaded cannot be excluded; exclusion is
|
||||
* best-effort and applies to modules already mapped.
|
||||
* zeroEventWarnMs: emits `WARN zero events ...` when a follow delivered
|
||||
* nothing by then, so a dead pipeline is not mistaken for
|
||||
* "the code did not run".
|
||||
*
|
||||
* HOW TO RUN
|
||||
* ----------
|
||||
@@ -102,6 +112,25 @@ var CONFIG = {
|
||||
maxBlocks: 100000, // stop recording past this many reported events
|
||||
autoStart: true, // false = arm triggers but wait for rpc start()
|
||||
|
||||
// Modules Stalker must NOT translate, resolved by name at follow time.
|
||||
// Cost is the reason this exists. A followed thread whose execution runs
|
||||
// through libc/libart pays a large multiplier on arm64 (community reports
|
||||
// 20-50x) and drags the whole device down with it, and a hot library
|
||||
// reached through a follow/unfollow cycle is where target processes have
|
||||
// died. Excluding the system libraries you are not studying is the
|
||||
// difference between a trace of your target and a trace of the OS.
|
||||
// The target module is never excluded, even if its name is listed here.
|
||||
excludeModules: [
|
||||
'libc.so', 'libm.so', 'libdl.so', 'libc++.so', 'libc++_shared.so',
|
||||
'libart.so', 'libartbase.so', 'libnativehelper.so',
|
||||
'libutils.so', 'libbinder.so', 'libcutils.so', 'libbase.so',
|
||||
'libui.so', 'libgui.so', 'libinput.so', 'libhwui.so', 'libskia.so',
|
||||
'libEGL.so', 'libGLESv2.so', 'libvulkan.so', 'libandroid.so',
|
||||
'liblog.so', 'libziparchive.so', 'libz.so'
|
||||
],
|
||||
excludeModulesExtra: [], // your own: 'libfoo.so', or 'libfoo.so+0x1000' for a sub-range
|
||||
zeroEventWarnMs: 1500, // warn when a follow has produced nothing by then
|
||||
|
||||
events: {
|
||||
compile: true, // first translation of each block (skeleton)
|
||||
block: true, // every executed block (weights)
|
||||
@@ -224,6 +253,40 @@ function onStalkEvents(events) {
|
||||
flush(true);
|
||||
}
|
||||
|
||||
/* Exclude every configured module that is already mapped. Best-effort by
|
||||
* design: a module that is not loaded yet cannot be excluded, so the count
|
||||
* emitted here is what the trace is actually protected by -- read it before
|
||||
* interpreting a bad trace. Exclusion must happen before follow(). */
|
||||
function applyExclusions() {
|
||||
var names = (CONFIG.excludeModules || []).concat(CONFIG.excludeModulesExtra || []);
|
||||
var excluded = [], attempted = 0, missing = [];
|
||||
for (var i = 0; i < names.length; i++) {
|
||||
var spec = names[i];
|
||||
if (!spec) continue;
|
||||
var plus = spec.indexOf('+');
|
||||
var name = plus > 0 ? spec.substring(0, plus) : spec;
|
||||
if (state.target && name === state.target.name) continue; // never the target
|
||||
var mod = null;
|
||||
try { mod = Process.findModuleByName(name); } catch (e) { mod = null; }
|
||||
if (!mod) { missing.push(name); continue; }
|
||||
attempted++;
|
||||
try {
|
||||
if (plus > 0) {
|
||||
var off = parseInt(spec.substring(plus + 1), 16) || 0;
|
||||
Stalker.exclude({ base: mod.base.add(off), size: mod.size - off });
|
||||
} else {
|
||||
Stalker.exclude(mod);
|
||||
}
|
||||
excluded.push(name);
|
||||
} catch (e) {
|
||||
emit('EXCL-FAIL', spec + ': ' + e);
|
||||
}
|
||||
}
|
||||
emit('EXCL', 'excluded=' + excluded.length + '/' + attempted +
|
||||
' [' + excluded.join(',') + ']' +
|
||||
(missing.length ? ' not-loaded=' + missing.length : ''));
|
||||
}
|
||||
|
||||
function beginFollow(tid, why) {
|
||||
if (state.following) return;
|
||||
if (!state.target) {
|
||||
@@ -237,6 +300,8 @@ function beginFollow(tid, why) {
|
||||
line('MOD ' + state.target.name + ' base=' + state.target.base +
|
||||
' size=' + state.target.size + ' path=' + state.target.path);
|
||||
|
||||
applyExclusions();
|
||||
|
||||
Stalker.follow(tid, {
|
||||
events: {
|
||||
call: CONFIG.events.call,
|
||||
@@ -248,6 +313,20 @@ function beginFollow(tid, why) {
|
||||
onReceive: onStalkEvents
|
||||
});
|
||||
|
||||
/* A zero-event trace has two very different meanings -- "nothing executed"
|
||||
* and "the pipeline never delivered" -- and the log alone cannot tell them
|
||||
* apart. This warning exists so the second reading is the default one. */
|
||||
if (CONFIG.zeroEventWarnMs > 0) {
|
||||
state.warnTimer = setTimeout(function () {
|
||||
if (state.following && state.nBB === 0 && state.nBLK === 0) {
|
||||
emit('WARN', 'zero events ' + CONFIG.zeroEventWarnMs +
|
||||
'ms after follow (blocks=0 blk=0 calls=0) -- the pipeline is ' +
|
||||
'NOT proven; do not report this as "the code did not run". ' +
|
||||
'Control: follow a thread running a known loop first.');
|
||||
}
|
||||
}, CONFIG.zeroEventWarnMs);
|
||||
}
|
||||
|
||||
if (CONFIG.followMs > 0) {
|
||||
state.timer = setTimeout(function () { endFollow('timeout'); },
|
||||
CONFIG.followMs);
|
||||
@@ -258,6 +337,7 @@ function endFollow(reason) {
|
||||
if (!state.following) return;
|
||||
state.following = false;
|
||||
if (state.timer !== null) { clearTimeout(state.timer); state.timer = null; }
|
||||
if (state.warnTimer) { clearTimeout(state.warnTimer); state.warnTimer = null; }
|
||||
try { Stalker.flush(); } catch (e) { /* already gone */ }
|
||||
try { Stalker.unfollow(state.followTid); } catch (e) { /* already gone */ }
|
||||
flush(true);
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user