Merge PR #6 "Hardware pass (stage 1)" (dabao1955/feat/ete)

Merges the hardware-layer pass onto the dexsql commit: arm64 hardware
watchpoints, ETE differential tracing, kernel-side event injection, SPE/BRBE
sampling and crash forensics, address symbolization and interpreter-trace
de-slicing. 33 files, 6 new scripts, 6 new test modules, 4 new reference
documents.

The only conflict was in evidence/capability-matrix.json: both sides appended
to the same array tail. Resolved by keeping both -- the PR's seven entries
(44 rows total now) -- and re-validated as JSON before staging.

Verified on the merged tree, not on the PR branch alone:

  * check_repo.py, check_refs.py, check_commands.py, check_routing.py and
    check_budget.py all pass with zero problems;
  * pytest: 766 passed, 172 skipped, 25 xfailed -- the 141 tests this PR adds
    all pass alongside the 625 that were already there;
  * the PR's host-side claims were reproduced by hand, not taken on trust:
    `hw_watch.py plan --addr 0x1004 --len 4 --access w` returns
    `cr: 7701` (WCR 0x1E15) exactly as the record says, both refusals exit 2
    with the documented reason strings, and the kernel scaffold's
    generate/verify round trip returns RESULT=ok.

One documentation defect was found and fixed in the merge: EXTENSION-kernel-instrument.md
recorded `verified 3 kernel-side file(s)` while the same page's own generate
output lists four (kpm/src/kwatch.c, kpm/Makefile, lkm/kwatch.c, lkm/Makefile)
and the tool reports 4. The page now says 4.

Everything the PR ships that needs a device or a kernel toolchain stays
labelled unverified in the files themselves, which is the property that made
it mergeable.
This commit is contained in:
inliver233
2026-10-16 02:05:59 +08:00
33 changed files with 5358 additions and 6 deletions
+6
View File
@@ -208,6 +208,12 @@ unread in this repository.
| You only need to **call** the target's own routine (sign, token, encrypt) rather than change the app | `references/emulation-and-rpc.md` — emulate it, or service-ify the live function over Frida RPC |
| A native function is a many-thousand-line `switch` state machine, or the decompiler's output is meaningless | `references/native-dbi-and-deobfuscation.md` — OLLVM shapes, a Stalker trace, and how far a trace actually gets you |
| `Stalker.follow` installs but no events arrive, or following a hot libc export crashes the process | `references/native-dbi-and-deobfuscation.md` §6 failure modes — this repository measured both |
| You must observe or alter the target with **no code change, no injected thread, and `TracerPid` = 0** — page checksums catch every hook | `hardware-watchpoints.md` — hardware debug slots via ptrace with its honest stealth ledger, then `kernel-event-injection.md` for the kernel-armed route that needs no tracer at all |
| A crash's backtrace cannot be trusted — frames in wrong modules or anonymous regions, flattened/virtualized code, or a smashed stack | `hardware-sampling-and-crash.md` — BRBE branch records / ETE tail as the real control flow, then `scripts/trace_addr2sym.py` to symbolize every PC |
| You hold a list of **runtime addresses** (hits, trace PCs, divergence points) and need symbols / static offsets, not hex | `hardware-sampling-and-crash.md` §3 The crash loop — `scripts/trace_addr2sym.py` with its vaddr/file-offset rules |
| A trace of a virtualized function is just an **interpreter loop repeating** and reads like noise | `scripts/vm_handler_slice.py` — slice at dispatcher entries into handler segments, cluster signatures, recover the guest-opcode trajectory (xvmp-calibrated) |
| You need to know **where two runs of the same operation diverge** (password/license/key), not just what executed | `ete-diff-tracing.md` — ETE hardware-trace gates plus the differential method; `scripts/trace_diff.py` also diffs Stalker logs |
| A generated KPM/LKM template is in front of you, or the kernel route feels like the plan | `kernel-event-injection.md` — the primitive map, the composition recipes, and the gates every kernel-side file must pass before it is a weapon |
| 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` |
+14 -1
View File
@@ -51,6 +51,19 @@
"At least one cross-check with an independent decoder is performed or explicitly flagged as not done."
],
"_targets_reference": "scripts/dex_strings.py, scripts/dexutil.py, scripts/find_refs.py, references/byte-level-patching.md"
},
{
"id": 4,
"prompt": "A hardened .so kills the process whenever I attach frida or set any inline hook; it also checksums its own code pages. I need to know which code writes the plaintext key buffer right before encryption. What route do I use?",
"expected_output": "The answer routes to the hardware layer with its honesty ledger, not to a harder hook. It names the hardware watchpoint as the observation that modifies no code, states plainly that the ptrace delivery leaves TracerPid visible (and that a target reading /proc over raw svc needs the kernel-armed route instead), requires the control run before any hit is trusted, and refuses to present the kernel template as a working weapon.",
"_assertions": [
"Names hardware watchpoints (debug registers) as the route that survives page checksums because no code byte changes.",
"States the TracerPid trade-off of the ptrace route explicitly rather than calling it invisible.",
"Requires a control run (a deliberately touched watched address producing one HIT) before trusting hits.",
"Does not recommend building a kernel module as the first move, and labels kernel-side templates unverified.",
"Routes to references/hardware-watchpoints.md and scripts/hw_watch.py rather than re-deriving the mechanism."
],
"_targets_reference": "references/hardware-watchpoints.md, references/kernel-event-injection.md"
}
]
}
}
@@ -416,6 +416,83 @@
"evidence_not_shipped": ["docs/tool-verification/README.md (repository root, not shipped)"],
"limits": "Determining who owns a gate is covered; breaking an authorization the server performs is not. There is no client-side patch that reaches it, and the honest deliverable is the residual: which authority decides, and why the artifact cannot change it."
},
{
"capability": "hardware-watchpoints-ptrace",
"group": "native-dynamic",
"status": "partial",
"route_measured": false,
"strength": "unverified",
"benchmark_rows": [],
"evidence": ["references/hardware-watchpoints.md", "scripts/hw_watch.py"],
"evidence_not_shipped": ["docs/tool-verification/EXTENSION-hw-watchpoints.md (repository root, not shipped)"],
"limits": "Encoders, refusals and the emitted helper's static aarch64 build are measured on the host; arming a watchpoint on a device, the slot-capacity probe and the control run were never exercised here. The ptrace route leaves TracerPid visible by design -- the ledger in the reference is the contract."
},
{
"capability": "trace-differential-reduction",
"group": "native-dynamic",
"status": "partial",
"route_measured": true,
"strength": "observed",
"benchmark_rows": [],
"evidence": ["references/ete-diff-tracing.md", "scripts/trace_diff.py"],
"evidence_not_shipped": ["docs/tool-verification/EXTENSION-ete-diff.md (repository root, not shipped)"],
"limits": "The reducer/differ is host-tested on synthetic flattened-dispatcher fixtures in both raw and run_probe-wrapped Stalker grammars. No producer (Stalker pair or cs_etm decode) was ever run through it end to end on a real target."
},
{
"capability": "ete-hardware-tracing",
"group": "native-dynamic",
"status": "blocked",
"route_measured": false,
"strength": "unverified",
"benchmark_rows": [],
"evidence": ["references/ete-diff-tracing.md", "scripts/ete_capture.py"],
"evidence_not_shipped": ["docs/tool-verification/EXTENSION-ete-diff.md (repository root, not shipped)"],
"limits": "Gates, recipe and capture wrapper are host-measured to the no-device boundary. No coresight capture or decode ever ran here; the reference device's kernel (4.14.186) closes the gate permanently. The --control record is the one-command check that upgrades or refutes the route on a real device."
},
{
"capability": "kernel-instrument-injection",
"group": "kernel",
"status": "blocked",
"route_measured": false,
"strength": "unverified",
"benchmark_rows": [],
"evidence": ["references/kernel-event-injection.md", "scripts/kernel_instrument_scaffold.py"],
"evidence_not_shipped": ["docs/tool-verification/EXTENSION-kernel-instrument.md (repository root, not shipped)"],
"limits": "Generate/verify and the shared spec-refusal rules are measured on the host. No kernel-side file was ever compiled or loaded; the register-rewrite action is labelled experimental in the template itself. Same gate family as the syscall-mask scaffold: a userspace carrier arms nothing."
},
{
"capability": "spe-brbe-crash-forensics",
"group": "native-dynamic",
"status": "partial",
"route_measured": false,
"strength": "unverified",
"benchmark_rows": [],
"evidence": ["references/hardware-sampling-and-crash.md"],
"evidence_not_shipped": ["docs/tool-verification/EXTENSION-sampling-crash.md (repository root, not shipped)"],
"limits": "SPE data-flow sampling and BRBE branch records are documented-and-gated: no sample or record was ever captured on any device this repository used. The crash loop's ordering (tombstone -> branch records -> symbolize) is the deliverable."
},
{
"capability": "address-symbolization",
"group": "native-static",
"status": "ok",
"route_measured": true,
"strength": "observed",
"benchmark_rows": [],
"evidence": ["references/hardware-sampling-and-crash.md", "scripts/trace_addr2sym.py"],
"evidence_not_shipped": ["docs/tool-verification/EXTENSION-sampling-crash.md (repository root, not shipped)"],
"limits": "The zero-dependency ELF64 walker is host-verified against a real cross-compiled aarch64 .so (symbols cross-checked with readelf) and a hand-built minimal ELF. Runtime-address inputs (hits, traces) were exercised on fixtures only, never on a live device trace."
},
{
"capability": "interpreter-trace-devirtualization",
"group": "native-dynamic",
"status": "partial",
"route_measured": true,
"strength": "observed",
"benchmark_rows": [],
"evidence": ["references/ete-diff-tracing.md", "scripts/vm_handler_slice.py"],
"evidence_not_shipped": ["docs/tool-verification/EXTENSION-sampling-crash.md (repository root, not shipped)"],
"limits": "Dispatcher detection, segment slicing, open-tail handling and the opcode-map trajectory are host-tested on synthetic interpreter traces. Calibration facts (vm_exec shape, opcode table, boxed args, const-materialization dispatch) are read from a public IR virtualizer's source; no hardened binary was ever traced by this repository."
},
{
"capability": "dex-sql-query-layer-dexsql",
"group": "dex",
@@ -36,6 +36,13 @@ mechanism's truth. It means nobody has yet put a command and its output behind i
- **The runtime routes need a real device.** Hooking, dumping, install-then-launch verification and
the module route all require a rooted device; several of them additionally require a framework the
device must have active.
- **The hardware layer is a host-verified toolkit, not a device-verified one.** The watchpoint
encoders and the emitted helper's build (static aarch64, cross gcc) are measured, the differential
reducer is fixture-tested, the ELF symbolizer is verified against a real cross-compiled `.so`, and
the kernel templates generate and verify — but **no watchpoint was armed, no ETE/SPE trace
captured, no BRBE record read, and no kernel file compiled or loaded on any device by this
repository**. Every device-side step carries its control procedure in the reference file, and a
control that has not run means the result is `unverified`, not working-as-intended.
- **The evidence record and the benchmark matrix do not ship.** Their paths appear in older
documents as if they were openable files. They resolve only in the repository they were written in.
@@ -104,6 +104,25 @@ its own strength note at the top.
- **What to do when userspace hooking provably cannot reach the check** — raw `svc` syscalls,
`init_array`-early detection, and the kernel-route map with its version gate
(`kernel-and-environment-hardening.md`).
- **Hardware watchpoints (ptrace route)** — observing who reads/writes one location with zero
code modification, its honest `TracerPid` ledger, and the control run every hit is owed.
Encoders and the on-device helper's build are host-measured; arming on a device is not
(`hardware-watchpoints.md`, `scripts/hw_watch.py`).
- **ETE differential tracing** — the five gates and how each fails silently, per-thread `cs_etm`
capture, and the one-bit-different pair design that turns two traces into the input-dependent
block set. The reducer/differ is host-measured; the producer side needs a coresight device this
repository never had (`ete-diff-tracing.md`, `scripts/ete_capture.py`, `scripts/trace_diff.py`).
- **SPE/BRBE sampling and crash forensics** — data flow without a known address, branch records
when a crash's stack cannot be trusted, and the address→symbol loop. The ELF symbolizer is
host-verified; no SPE sample or BRBE record was ever captured here
(`hardware-sampling-and-crash.md`, `scripts/trace_addr2sym.py`).
- **Interpreter-trace de-virtualization** — dispatcher slicing, handler-signature clustering and
the guest-opcode trajectory, host-tested on synthetic interpreter traces and calibrated
against a public IR virtualizer's source; no hardened binary was ever traced
(`scripts/vm_handler_slice.py`).
- **Kernel-side event injection** — invisible watchpoints and register rewrites from a driver, as
generated templates with their gates; nothing kernel-side was ever compiled or loaded here
(`kernel-event-injection.md`, `scripts/kernel_instrument_scaffold.py`).
- **Working from the phone itself** — MT Manager, its APK MCP surface, LSPosed Manager, on-device
data inspection (`on-device-tooling.md`, `scripts/mt_mcp_probe.py`).
@@ -43,6 +43,11 @@ route was not measured on this repository's evidence**. Treat them as leads with
| Split APK / App Bundle sets | reading a set, merge versus unified re-signing, the install refusal each mistake produces — `split-apk.md` |
| Dex-VMP differential analysis | the known-plaintext route, what it can and cannot automate — `vmp-differential-analysis.md` |
| Kernel-module templates | a scaffold **and** its version gate; a userspace module cannot change a syscall return value — `kernelsu_syscall_mask.py` |
| Hardware watchpoints (ptrace) | who reads/writes one address with zero code change, the encoders host-tested and the helper compiled — never armed on a device here — `hardware-watchpoints.md` |
| ETE differential tracing | two one-bit-different runs into the input-dependent block set; gates probed by script, producer unverified — `ete-diff-tracing.md` |
| SPE/BRBE sampling and crash forensics | data flow without a known address, branch records past an untrustworthy stack, addresses→symbols — `hardware-sampling-and-crash.md` |
| Interpreter-trace de-virtualization | handler slicing + guest-opcode trajectory, calibrated on an open IR-VM's source — `vm_handler_slice.py` |
| Kernel-side event injection | watchpoints and register rewrites from a driver, as gated templates — `kernel-event-injection.md` |
| Working from the phone itself | MT Manager edit/repack/sign and its APK MCP, LSPosed Manager, Termux — `on-device-tooling.md` |
| Publishing without publishing the target | the leak scanner, the do-not-anonymize list, and the graded precedent library — `desensitization-and-leak-scans.md`, `scripts/scan_leaks.py`, `precedents/` |
@@ -58,7 +63,11 @@ dependency yourself.
- **Device work needs `adb` plus a device the app does not object to**; dynamic work additionally needs
a `frida-server` matching the host version.
- **A kernel-side answer needs a module and a kernel that accepts it** — the version gate is in
`kernel-and-environment-hardening.md`.
`kernel-and-environment-hardening.md`; the instrumentation templates add their own
(the kernel's perf breakpoint layer must exist — `kernel-event-injection.md`).
- **The ETE route needs coresight hardware this skill cannot conjure** — an ETE/TRBE-capable core,
a kernel with the drivers, and a perf built against OpenCSD; without them the route is closed,
not expensive (`ete-diff-tracing.md`).
## Not covered — say so rather than improvise
@@ -0,0 +1,199 @@
# ETE differential tracing — hardware execution streams, two runs, one answer
Load this when the question is **where two runs of the same code diverge** — password
accepted versus rejected, license valid versus invalid, key A versus key B — and every
software tracer either modifies the code it traces (Stalker: the DBI cache), crashes under
the load (the measured `load average 34.11` case), or delivers nothing at all (the measured
zero-event follow in `references/native-dbi-and-deobfuscation.md` §6 Failure modes). An
ARMv8.4-era core's **ETE** streams executed-branch records off-core **in hardware**, and
**TRBE** collects them self-hosted; the target's own image, its timing profile and its
`TracerPid` are untouched. This file owns the gates, the capture and the differential
method; `scripts/ete_capture.py` runs the gates and the capture, and `scripts/trace_diff.py`
is the differ — which is producer-agnostic and works on Stalker logs too.
**Claim strength.** The gates, the recipe and the capture wrapper run on the host —
`observed`. The differential reducer is host-tested on synthetic flattened-dispatcher
fixtures — `observed` for the tool, `inferred` for the method. **No ETE trace has been
captured or decoded by this repository**: no reference device here has coresight compiled in
(reference kernel 4.14.186 — the ETE gate is closed there, `observed`). The command shapes
follow the kernel's `tools/perf` CoreSight documentation and the OpenCSD project — treat
them as the documented upstream interface until your own control record decodes.
## 1. The mechanism, in the size that matters
| Piece | What it is | Why you care |
|---|---|---|
| **ETE** (Embedded Trace Extension) | the ARMv8.4 trace unit; emits executed-branch/instruction packets without instrumenting anything | nothing in the process image changes; page checksums stay clean |
| **TRBE** (Trace Buffer Extension) | per-CPU self-hosted trace buffer | per-thread tracing with no external debugger and no shared sink |
| `cs_etm` PMU | the Linux perf interface to ETE/ETM | `perf record -e cs_etm//u --per-thread` is the whole capture |
| OpenCSD | the decode library perf links against | a stock perf **records** an aux buffer it cannot **decode** — the classic silent failure |
| `scripts/trace_diff.py` | reduce + diff two decoded streams | the differ is the reusable half; it also eats Stalker `BLK` logs |
The old `etm`-only world (no TRBE) still traces, but through a **shared** sink
(`tmc_etrN`) that cannot do `--per-thread`, is frequently reserved by the vendor, and
mixes CPUs — the differential method still applies but run design gets harder. Prefer
devices with `trbeN` in `/sys/bus/coresight/devices`.
## 2. The five gates — probe before you design anything
Every closed gate fails **silently** later: an old kernel reports no PMU; a kernel without
TRBE cannot per-thread; a perf without OpenCSD decodes nothing; SELinux can deny
`perf_event_open` even to root. One command each, then believe the table:
```
python skills/apk-reverse/scripts/ete_capture.py gates --device
```
| Gate | Expected | Closed means |
|---|---|---|
| kernel era | ≥ 5.10 | the ETE/TRBE drivers are not in your kernel at all |
| coresight devices | `ete<N>` **and** `trbe<N>` in sysfs | `ete` absent: no trace unit; `trbe` absent: shared sink only |
| `cs_etm` PMU | `/sys/bus/event_source/devices/cs_etm` exists | perf cannot speak to coresight |
| perf with CORESIGHT | `perf list` shows `cs_etm` | you can record but not decode |
| permission | a control record of `true` decodes | the pipe is closed at SELinux/paranoid level |
A closed gate is a **route decision** — record it in one line per
`references/kernel-and-environment-hardening.md` §6 What the reference environment chose
(and why it generalises), and do not re-derive it mid-task. The reference environment's own
line: kernel 4.14.186 → ETE closed, permanently, on that device.
## 3. Capture
`ete_capture.py recipe` prints the full build path for a perf-with-CORESIGHT (on-device
Termux or cross). The capture itself, per-thread on TRBE:
```
python skills/apk-reverse/scripts/ete_capture.py capture --pid <PID> --seconds 10 --out a.data --control
```
`--control` is not decoration: it records a **one-instruction process** (`true`) first and
refuses to continue if that does not decode. A capture of the real target that "looks fine"
is worth nothing until the control has produced packets — this is the same discipline as the
Stalker zero-event lesson, paid for once already in this repository.
Decode to an address stream (device or host, wherever your perf lives):
```
perf script -i a.data > a.log
python skills/apk-reverse/scripts/trace_diff.py reduce --log a.log
```
Volume rule from `native-dbi-and-deobfuscation.md` §3 transfers: one module, one bounded
window. ETE traces the *whole thread*, so capture around one operation (launch to first
frame, one login attempt, one encrypt call), not a session.
## 4. The differential method
**Design the pair before touching the device.** The method answers only the question the
pair was built to ask:
1. **One changed bit of input, everything else identical.** Wrong password *character*,
not wrong password *length* (length changes parsing). Key `0x0000...` vs `0x8000...`.
License whose checksum byte differs. Two runs, same build, same screen state, same
window boundaries.
2. **Reduce both streams to module-relative keys.** Raw pointers move with ASLR;
`liba.so+0x9f6c0` is comparable across runs, `0x7b20f29f6c0` is not.
`trace_diff.py` keeps `mod+0xoff` keys verbatim for exactly this reason.
3. **Diff and read three things** — `trace_diff.py diff --a a.log --b b.log` prints them:
- **identical prefix span** — the shared machinery before your input bit reached
control flow; it names where parsing/handling ends and the decision begins;
- **first divergence** — the branch that first consumed your input difference. In a
flattened dispatcher this is where the state constant differs;
- **blocks unique to each side, ranked** — the input-dependent set. For "valid vs
invalid", the B-unique list *is* what the valid path does that the invalid one
never reaches.
4. **Interpret per obfuscation:**
- **OLLVM flattening**: dispatcher `D` matches on both sides (it is machinery); the
divergent `R<n>` blocks between `D`s are the real blocks the two inputs select
between. Combined with the state-variable watchpoint in
`references/hardware-watchpoints.md` §4, the diff gives you the state constants to
complete the CFG recovery of `native-dbi-and-deobfuscation.md` §5 without a solver.
- **VMP / interpreter loops**: the handler loop matches (it is the interpreter);
unique blocks are guest-opcode handlers. The *sequence* difference between two
inputs is a private-opcode trace — feed it to the differential mapping machinery in
`references/vmp-differential-analysis.md` as ground truth instead of guessing.
- **Whitebox / key-derived logic**: the unique-to-each-side sets across several key
pairs approximate the key-dependent branch set; a branch that never appears in
either side of any pair is not key-dependent — stop reversing it.
5. **Repeat once with the inputs swapped** if the result decides a patch. A divergence that
moves when the inputs move is real; one that moves when it should not is capture noise.
**What the tool does not enforce** (its header says so too): same build, same address space,
same window, one-bit difference. A diff across two builds is noise with a confident
rendering.
## 5. What the obfuscator actually built — read from two open implementations
The differential reads better when you know what the two families actually emit. The facts
below are read from the public AOSP-toolchain forks (dpkg123/ollvm-aosp-toolchain, branches
`ollvm-r614150-clang23-rebase+omp` — the Hikari family — and `ollvm-r614150-clang23+omp` —
the Pluto family; xvmp commits 63fec41f/9b7cf75e; fetched 2026-10): **source text observed,
runtime behaviour inferred from it**.
| | Hikari-family flattening | Pluto-family flattening |
|---|---|---|
| state variable | `switchVar` **alloca in the frame** + a `switchVarAddr` double pointer; every state store goes through an indirect load of that pointer | `switchVar` + `switchXor` **two allocas, rolling XOR**: `enc^=delta; key^=delta; cond=enc^key` on every dispatch |
| case values | `scramble32` permuted (not 0,1,2…) | fully random per build; the true case exists only in the `switchCond` register for one instruction |
| what a memory watchpoint sees | the permuted case value — decodable against the static switch table | **noise**: the stored enc and key never contain the case |
| what a trace sees | identical in both: the block sequence — which is why the trace is the robust instrument |
| skipped functions | EH-bearing (landingpad) | EH, `callbr`, `indirectbr` |
Consequences for this file's method: the **divergent-blocks reading (§4) is family-proof** —
it never decodes the state variable. The **watchpoint-on-state recipe** is family-sensitive:
Hikari-shaped targets can be read at the (frame-resident) slot via the pointer; Pluto-shaped
targets need a breakpoint one instruction after the `switchCond` XOR to read the register
(`references/hardware-watchpoints.md` §4 carries the corrected recipe). And the AntiDebugging
pass of the Hikari family injects **raw `svc` inline asm with randomized register-setup
order and a randomized svc immediate** (source-read): byte signatures cannot survive that
randomization, but ETE's branch stream and `svc_scan.py`'s semantic site-location do not
care — locate the check by what it *is*, never by its bytes.
**xvmp (IR-level virtualizer, same repo)**: each virtualized function becomes a stub calling
`vm_exec(code, size, vm_args, num)` — bytecode bytes plus a boxed-argument array in the
module; 11 one-byte opcodes with inline-const/slot operands and 19 arithmetic sub-ops;
pointer constants materialize through a generated const/call dispatch at first use. For the
tracing routes this means: the interpreter is one dominant loop (the §4 handler reading),
the **guest inputs are observable at the `vm_args` array before interpretation starts** (a
watchpoint there sees arguments and any key material, plaintext, whatever the bytecode
carries), and each constant's bits pass through the materialization dispatch as a returned
i64 — a second, cheap observation point. `scripts/vm_handler_slice.py` consumes the trace
side and documents the handler->opcode map you can build against a *known* table like this
one.
## 6. Failure modes
- **`perf record` succeeds, `perf script` prints nothing address-like** — perf without
OpenCSD decode, or an aux buffer that never wrapped (window too short). The control run
separates the two in one command.
- **`gates --device` shows ete but no trbe** — shared-sink world: drop `--per-thread`,
trace `-C <cpu>` with `cs_etm/@tmc_etr0/u`, and pin the target to that CPU
(`taskset`-style, or accept the mixture). Expect much more post-filtering.
- **Aux overflow / gaps** — decode reports non-contiguous ranges: the window was too hot.
Shrink it, or accept that the diff sees only a sampled path and say so in the conclusion.
- **The two runs are not comparable** — different pids with different module bases and raw
keys: everything diverges from address zero. Re-run through `reduce`, check that the
top-of-histogram block is shared before diffing; if the top blocks differ, the pair is
not a pair.
- **ETE closes on the only device you have** — the honest answer, not a challenge; the
fallback ladder for the *question* is Stalker (measured boundaries apply) or the
watchpoint route (single addresses, no paths), never "trace harder".
## 7. Hand-offs
- Divergence found, now you need the *values* at that site → hardware watchpoint or
breakpoint (`references/hardware-watchpoints.md`), or QBDI per-instruction replay
(`references/native-dbi-and-deobfuscation.md` §4 Stalker, QBDI, or emulation).
- Divergence found, and you must *change* the decision with no code change →
`references/kernel-event-injection.md`.
- The path is known and you only need to call the routine → `references/emulation-and-rpc.md`.
## Checklist
- [ ] `gates --device`: all five closed, or the closed ones recorded as route decisions
- [ ] `--control` record of `true` decodes before any real capture
- [ ] Pair design written down: same build, same window, one changed input bit
- [ ] Both logs reduced; top-of-histogram block shared before diffing
- [ ] Divergences read as: prefix span / first divergence / unique-per-side
- [ ] Input-swap repeat when the diff decides a patch
- [ ] Labels: gates+reducer `observed` (host), ETE capture/decode `unverified` here —
the control run on your device is the only thing that upgrades it
@@ -55,6 +55,12 @@ next** — and points at the one section that says "the full record is elsewhere
| Packer and custom-loader identification | Never exercised end to end; the target that was measured has no packer, so the tooling was recorded as not applicable | partial | unverified | `references/packers.md`, `references/code-virtualization-and-custom-linkers.md` |
| Module delivery instead of a repack | The scaffold builds end to end, but the delivery route has no public-target measurement and needs a device with the framework active | partial | inferred | `references/lsposed-and-modules.md`, `scripts/lsposed_scaffold.py` |
| Kernel-side syscall answer forging | Blocked on the kernel side: the generator is measured, no kernel artefact was compiled or loaded, and a userspace module cannot change a return value | blocked | unverified | `references/kernel-and-environment-hardening.md`, `scripts/kernelsu_syscall_mask.py` |
| Hardware memory watchpoints (ptrace) | The encoders and refusals are host-tested and the emitted helper compiles as a static aarch64 ELF; **no watchpoint was ever armed on a device**, and the route leaves `TracerPid` visible by design | partial | unverified | `references/hardware-watchpoints.md`, `scripts/hw_watch.py` |
| Differential trace reduction | First divergence, divergent regions and per-side unique blocks measured on synthetic flattened-dispatcher fixtures, in both Stalker and wrapped-log grammars; no real-target producer (Stalker pair or cs_etm decode) has fed it yet | partial | observed | `references/ete-diff-tracing.md`, `scripts/trace_diff.py` |
| ETE hardware tracing | Gates and capture wrapper host-measured to the no-device boundary; no coresight capture or decode ever ran here, and `--control` exists to say so on a real device in one command | blocked | unverified | `references/ete-diff-tracing.md`, `scripts/ete_capture.py` |
| SPE/BRBE sampling and crash forensics | The address→symbol loop is host-verified on a real cross-compiled `.so`; SPE/BRBE mechanisms are documented-and-gated, never captured here | partial | unverified | `references/hardware-sampling-and-crash.md`, `scripts/trace_addr2sym.py` |
| Interpreter-trace de-virtualization | Dispatcher slicing, signature clustering and the opcode trajectory measured on synthetic interpreter traces; the calibration facts come from a public IR-VM's source, and no hardened binary was traced | partial | observed | `references/ete-diff-tracing.md` §5, `scripts/vm_handler_slice.py` |
| Kernel-side instrumentation/injection | The scaffold's generate/verify is measured and every kernel file ships unbuilt with its gate; the register-rewrite action is explicitly experimental and never ran anywhere | blocked | unverified | `references/kernel-event-injection.md`, `scripts/kernel_instrument_scaffold.py` |
| `svc` site scanning | Two independent decoders agreed on an identical 214-site set; a byte scan also matches data, so neighbour context decides | ok | observed | `references/kernel-and-environment-hardening.md`, `scripts/svc_scan.py` |
| On-device tooling (MT Manager MCP) | The service-down path is measured; the connected path needs the service started by hand | partial | observed | `references/on-device-tooling.md`, `scripts/mt_mcp_probe.py` |
| Client-side ads and server-issued UI config | Measured on a real mid-size target's ad chain, with a repacked build's on-screen change confirmed | ok | observed | `references/ad-removal.md`, `references/server-config-and-updates.md` |
@@ -0,0 +1,140 @@
# Hardware sampling and crash forensics — SPE data flow, BRBE branch records, and turning addresses into symbols
Load this when the question is **data flow rather than control flow** ("which code touches this
buffer" without knowing the buffer's address yet — that is SPE's job, not a watchpoint's), when
**a crash's stack unwind cannot be trusted** (obfuscated code, frame-pointer omission, stacks
smashed by the fault itself — that is BRBE's job), or when you hold a list of runtime addresses
that must become **symbols and static offsets** before they mean anything (that is
`scripts/trace_addr2sym.py`'s job, and it closes the loop for every other tool here).
**Claim strength.** The ELF walker behind `trace_addr2sym.py` is host-tested against a real
cross-compiled aarch64 shared library and a hand-built minimal ELF — `observed`. SPE/BRBE
mechanisms and their kernel interfaces follow the ARM ARM and the kernel/perf documentation —
`inferred`; **no SPE sample or BRBE record was ever captured on a device by this repository**
(no reference device carries either feature) — the producer side is `unverified` and every
route below starts with its gate probe.
## 1. SPE — the watchpoint that does not need to know the address
The Statistical Profiling Extension (FEAT_SPE, ARMv8.2+, present from Cortex-A75/A55-class
cores onward — far older hardware than ETE) samples instruction execution at a programmable
interval and records, per sample: the **PC**, the **data virtual address** the instruction
touched (for loads/stores), latency, and microarchitectural events (cache/TLB misses, branch
mispredict). Two properties make it a reverse-engineering instrument rather than a profiler:
1. **Data address + PC in one record.** That answers "which code reads/writes key-shaped
buffers" *without a priori address knowledge* — the exact gap a watchpoint has (a
watchpoint is precise and cheap but must be told where to look; SPE is sampled and noisy
but asks nothing in advance). The workflow: capture SPE around the operation, group
samples by data address, find addresses whose accessors are concentrated in the hardened
`.so`, then graduate those addresses to a hardware watchpoint for precision.
2. **It runs whether or not anyone cooperates.** No symbols, no relocations, no hooks; the
aux buffer fills from user space with `perf record -e arm_spe_0// --per-thread`
(`arm_spe<N>` PMU; kernel `CONFIG_ARM_SPE_PMU`; probe
`ls /sys/bus/event_source/devices/`). Decode with the same perf-with-OpenCSD-class build
as ETE (`scripts/ete_capture.py recipe` builds both), or import the capture into
Perfetto, which renders SPE records natively.
Gates and limits: SPE is **sampling** — a buffer touched twice may appear zero times; treat
absence as weak evidence only (the same discipline as trace absence in
`references/native-dbi-and-deobfuscation.md` §7 Reporting what a trace proves). The sampling
interval trades coverage for overhead; and the data-address field is truncated/tagged on some
implementations — verify on one known access first.
## 2. BRBE — the last N branches before the crash
The Branch Record Buffer Extension (FEAT_BRBE, ARMv9.2+, Cortex-X3/A715-class cores and
newer) is hardware that **continuously records the last N taken branches** (from→to pairs,
32–64 records) into a buffer the kernel can read. It is the ARM equivalent of Intel's last
branch record, and it exists precisely for the question a crash asks: *what was the control
flow that led here?* — answered by hardware even when the stack is gone, the frame pointers
were optimized out, or the code was flattened so the unwinder wanders.
Two ways to get at it, both gated:
| Route | What | Gate |
|---|---|---|
| **perf branch stacks** | `perf record -b`/`-j any`; on arm64 kernels with BRBE support (6.1-era merges; `inferred`) the branch-stack samples are hardware-fed by BRBE | kernel with BRBE perf support; probe `perf record -j any -o /dev/null -- true` |
| **crash-time read** | your own tracer (ptrace stop at the fatal signal, or a kernel-side module) reads the BRBE records before the process is reaped | kernel exposure of the records (`unverified` — the registers are EL1-accessed; a KPM-side reader belongs to `references/kernel-event-injection.md`) |
Use it when: the tombstone's backtrace is the thing you do not trust (`scripts/native_crash.py`
prints the stack split by owner — when the frames sit in wrong modules or anonymous regions,
that is the signal); the crash is inside flattened/virtualized code whose stack is decorative;
or the death is a deliberate fault (`references/native-tamper-and-suicide.md`) and the branch
records name the site that jumped to it. On hardware without BRBE, the honest fallback is the
ETE aux buffer: stop the process at the crash (never let it die), and read the tail of the
trace — same answer, more plumbing, gates in `references/ete-diff-tracing.md` §2 The five
gates.
## 3. The crash loop: three sources, one order of asking
1. **Tombstone/logcat first** — `scripts/native_crash.py`: signal, fault address, registers,
frames split app-vs-system, and the arranged-fault flag. Cheap, always available.
2. **If the stack is not trustworthy** — branch records (BRBE if the gate opens; ETE tail
otherwise). This is the *real* control flow, and it survives obfuscation by construction.
3. **Then symbolize everything** — every PC from 1 or 2 through
`scripts/trace_addr2sym.py`, with the maps pulled **in the same process lifetime**:
```
python skills/apk-reverse/scripts/trace_addr2sym.py --hits hwwatch_hits.log --maps maps.txt --elf liba.so
python skills/apk-reverse/scripts/trace_addr2sym.py --elf liba.so --addrs 0x7b20001234c0 --base 0x7b2000000000
python skills/apk-reverse/scripts/trace_addr2sym.py --trace vm_trace.log --mod libvm.so --elf libvm.so
```
The offset-space rules that decide whether the number you paste into IDA is right (vaddr vs
file offset, `p_vaddr != p_offset` payloads) are stated once, in the tool's header — read them
before distrusting a symbol.
## 4. What this adds to the deobfuscation routes
- **OLLVM**: SPE finds the arithmetic that touches key material without a trace
(`references/native-dbi-and-deobfuscation.md` §1's table, data-flow half); BRBE/ETE-tail
recover the real path into a flattened function that crashes — the state machine's taken
states, read from hardware, no unwinder.
- **VMP/xvmp-class**: SPE answers "which handler sequence touches the argument slots" when
the bytecode is opaque (`vm_exec`'s boxed arguments live in a plain array —
`scripts/vm_handler_slice.py`'s header records where); a crash inside the interpreter
keeps its branch records, so the fatal guest opcode is identifiable from the last
handler segment.
- **Anti-debug/anti-hook inserts**: a deliberate `svc`-based check (the Hikari-family
AntiDebugging pass injects inline-assembly syscalls with randomized immediate fields on
Darwin targets, and links precompiled anti-debug IR elsewhere — source-read, `observed`
text / `inferred` behaviour) is invisible to byte-signature scans precisely because the
fields are random. Hardware-level observation does not care: ETE shows the branch into
and back out of the check; a breakpoint at the *semantic* site (the `svc` itself,
located by `scripts/svc_scan.py`) beats a signature by construction.
## 5. Failure modes
- **`arm_spe_0` absent from `/sys/bus/event_source/devices/`** — kernel without
`CONFIG_ARM_SPE_PMU` or a core without FEAT_SPE (pre-ARMv8.2): closed gate, one line,
move on; the watchpoint route still exists if you can name the address.
- **`perf record -j any` on arm64 returns samples with empty branch stacks** — the kernel
took the software path (no BRBE backend); the records are call-chain-ish, not
hardware branch pairs. Check `perf report`'s branch-stack content on a known-hot control
flow before trusting a negative.
- **SPE shows nothing at an address you know is touched** — sampling missed it. Lower the
interval (more samples, more overhead), or graduate to a watchpoint for that one address.
- **Symbolization disagrees with the disassembler** — the offset space was wrong, not the
tool: re-read the header's vaddr/file-offset rules, check `--offs-are-file`, and confirm
the module's load base from the same process lifetime. A runtime-materialized library
(lib_map flags those) has a base that never appears in any APK.
## 6. Hand-offs
- Full-path capture, divergence diffs → `references/ete-diff-tracing.md`.
- Single-address precision, zero code modification → `references/hardware-watchpoints.md`.
- Reading BRBE from a kernel module, crash-time record reads →
`references/kernel-event-injection.md`.
- Triage of the crash itself (signal, fault, arranged-fault) →
`references/native-tamper-and-suicide.md`, `scripts/native_crash.py`.
## Checklist
- [ ] Gate probed before design: `arm_spe_0` present? BRBE branch stacks real or software?
- [ ] SPE: one known access verified visible before absence is interpreted as anything
- [ ] Crash loop in order: tombstone → branch records → symbolize; maps from the same
process lifetime
- [ ] Offsets pasted into a disassembler cross-checked against the tool's offset-space rules
- [ ] Labels: addr2sym host-`observed`; SPE/BRBE producer `unverified` here — your gate
probe is the first real measurement
@@ -0,0 +1,172 @@
# Hardware watchpoints — observing memory without touching the code
Load this when you need to know **who writes or reads a memory location**, and every
in-process tool you have modifies the thing it observes: an inline hook is caught by the
library's own page checksum, a Stalker follow rewrites the code it traces, and the target
dies before your hook exists. An arm64 hardware watchpoint compares every load/store
address **in silicon** and traps on match: no code byte changes, no thread appears, no
mapping is added. This file owns the mechanism, its honest stealth ledger, the traps, and
the procedure; `scripts/hw_watch.py` is the tool; `references/ete-diff-tracing.md` owns
full-path tracing, and `references/kernel-event-injection.md` owns the ptrace-free delivery.
**Claim strength.** The WCR/BCR encoders and the emitted helper are host-tested, and the
helper **compiles clean as a static aarch64 ELF** (cross gcc 16.2, `-static`) — `observed`
on the host. **No watchpoint has been armed on a device by this repository's passes** — the
ptrace dance follows the kernel ABI (arm64 `NT_ARM_HW_WATCH`/`NT_ARM_HW_BREAK` regsets,
`TRAP_HWBKPT` siginfo) and is `unverified` until the control run below passes on your
device. Slots, capacities and per-thread semantics are ARM DDI 0487 / kernel
`arch/arm64/kernel/hw_breakpoint.c` facts — `inferred` from documented interfaces.
## 1. The stealth ledger — read it before choosing this route
| delivery | code modified | foreign thread/mapping | `TracerPid` | survives page checksums | per-hit cost |
|---|---|---|---|---|---|
| inline hook (Frida `Interceptor`) | yes | no | no | **no — caught by construction** | ~none |
| Stalker follow | yes (DBI copy) | yes (agent + code cache) | no | no | large constant |
| **ptrace hardware watchpoint** (this file) | **no** | **no** | **yes — visible** | **yes** | near-zero |
| kernel-armed hardware watchpoint (`kernel-event-injection.md`) | no | no | **no** | yes | near-zero |
Two conclusions follow, and both are instructions: a watchpoint is the answer to
*self-checksumming* code (there is nothing in the image to find), and it is **not** an
answer to a target that reads `TracerPid` over a raw `svc` — for that, the kernel route
exists, with its own gate. Never present the ptrace route as invisible; it is invisible to
the process image, not to `/proc`.
## 2. The mechanism, in the size that matters
Each arm64 core carries a small bank of debug comparators — typically **6 breakpoints
(instruction addresses) and 4 watchpoints (data addresses)**; the kernel reports the true
counts and `scripts/hw_watch.py plan`'s device-side counterpart reads them
(`dbg_info & 0xff` from the `NT_ARM_HW_WATCH` regset). Three hardware rules decide what is
expressible:
1. **A watchpoint covers at most 8 bytes, inside one aligned 8-byte window** (byte-select
field `BAS`). A watch at `0x...4` with length 8 crosses the window and needs two slots.
`hw_watch.py plan` refuses it locally instead of letting the device.
2. **Execution breakpoints need a 4-byte-aligned PC** (A64 instruction width), and matching
uses the whole address.
3. **Slots are per-thread.** A process with N threads needs the watchpoint armed N times,
and a thread created later runs unarmed until your tracer catches its first stop — which
is why the emitted helper seizes with `PTRACE_O_TRACECLONE` and arms every new thread at
birth.
The two canonical encodings (unit-tested; everything else is a variation):
```
watch 8 bytes, load+store, EL0 : WCR = 0x1FFD (BAS=0xff, LSC=both, PAC=EL0, E=1)
break, address match, EL0 : BCR = 0x3F05 (BAS all-ones, PMC=EL0, BT=unlinked, E=1)
```
Delivery is the standard ptrace regset ABI — `PTRACE_SEIZE` + `PTRACE_INTERRUPT` every
thread, `PTRACE_SETREGSET` with `NT_ARM_HW_WATCH`/`NT_ARM_HW_BREAK`, `PTRACE_CONT`, then
`waitpid` for a `SIGTRAP` whose `si_code == TRAP_HWBKPT (4)` and whose `si_addr` is the
data address that matched. On hit you read `NT_PRSTATUS` for the register file. To *continue*
from an execution breakpoint you must disable the slot, `PTRACE_SINGLESTEP`, re-enable —
without that dance the trap re-fires on the same PC forever.
## 3. Procedure
**1. Plan on the host** — validate alignment and see the exact encodings:
```
python skills/apk-reverse/scripts/hw_watch.py plan --addr 0x7b2000123456 --len 8 --access w
python skills/apk-reverse/scripts/hw_watch.py plan --break 0x7b20001234c0
```
**2. Emit the helper** (self-contained C; NDK clang on the host, or Termux clang on the
device — build commands are in the generated README):
```
python skills/apk-reverse/scripts/hw_watch.py emit --out work/hwwatch
```
**3. Gates, then the control run — a watchpoint that has never fired is not armed.** Pick a
global you can touch on demand (a flag the app exposes, or a location you write through
`/proc/<pid>/mem` from the host as root) and confirm one `HIT` arrives with a sensible PC
**before** you spend the window on the real address. The measured Stalker lesson transfers
exactly: zero events from a correctly-installed instrument is a real failure mode
(`references/native-dbi-and-deobfuscation.md` §6 Failure modes), and the only defence is a
producer you have already seen fire.
```
python skills/apk-reverse/scripts/hw_watch.py run --pkg <PKG> --addr 0x... --len 8 --access rw --seconds 20
python skills/apk-reverse/scripts/hw_watch.py decode --hits hwwatch_hits.log --maps maps.txt
```
`decode` turns each hit's PC into `module+offset` given a `/proc/<pid>/maps` file (pull it
in the same process lifetime — bases move with ASLR) or a `lib_map.py` JSON.
**4. Read the hits the way a trace taught you to**: a hit's PC sequence is caller evidence;
run it through `scripts/trace_diff.py reduce` if you collected several, and correlate with
`lib_map.py` for which library actually owns the address.
## 4. Where a watchpoint beats a hook — the recipes
- **The key that never appears in a readable form.** OLLVM-substituted and VM-wrapped crypto
still materialises plaintext in a buffer before use. Watch the buffer you identified
statically (the JNI argument location, the `memcpy` destination) with `--access w`: the
PC that fills it is the decryptor, obfuscation irrelevant — control flow does not matter
to a comparator.
- **The state variable of a flattened dispatcher.** Family-sensitive, and the difference is
real (read from two open implementations — Hikari vs Pluto flattening — in
`references/ete-diff-tracing.md` §5 What the obfuscator actually built — read from two
open implementations): in the Hikari shape the state is a frame-resident slot written
through an indirect pointer — watch the slot and each `HIT` PC is a real block's tail,
with the written (permuted) state value readable afterwards. In the Pluto shape the state
is stored **rolling-XOR encoded**: memory only ever holds noise, so the recipe is a
breakpoint one instruction after the dispatch decode (`switchCond = enc ^ key`) and the
condition register is read at the trap, not from memory. If you cannot tell which family
you have, the block-sequence routes (ETE diff, `scripts/vm_handler_slice.py`) never need
the decoded value at all. That decoding is half of the CFG recovery in
`references/native-dbi-and-deobfuscation.md` §5.
- **The self-modifying / self-decrypting region.** Watch the code page itself with
`--access x`-adjacent breakpoints on entry and `--access w` on the page: the PC that
writes executable memory is the unpacker, whatever the section headers claim.
- **"Does this check even read my patch?"** Watch the patched location: zero reads during a
run that enforces the old behaviour is direct evidence the check reads something else —
cheaper than a full trace and answerable in one window.
A stack slot cannot be watched this way if the frame moves between runs; watch the *caller's*
stable global or the buffer the function receives instead. The 8-byte window limit means
large structs need either the one field that matters or several slots.
## 5. Failure modes and traps
- **`READY ... watch_slots=0`** — the kernel masked hardware debug (some vendor kernels
reserve the slots for their own telemetry, or the SoC reports none). That is a closed
gate; record it and move to `references/kernel-event-injection.md` or the ETE route.
- **Only the arming thread traps.** The watchpoint is per-thread: if you armed one tid and
the work happens on another, you get silence, not an error. The helper arms every thread,
but check the `ARM tid=` lines against `ls /proc/<pid>/task` before trusting a quiet run.
- **A second tracer wins or you break one.** Only one tracer is allowed; if frida is
attached, `PTRACE_SEIZE` fails with `EPERM`, and vice versa. Detach everything else first.
- **Swallowed signals.** A naive loop that resumes with `PTRACE_CONT(tid, 0, 0)` drops the
target's own signals (its crash handlers, its SIGALRMs) and the app misbehaves in ways you
will blame on the target. The helper forwards non-debug `SIGTRAP`s and other signals, and
uses `PTRACE_LISTEN` for group stops; if you write your own loop, this is the part to get
right, not the encodings.
- **Root, and only root.** `ptrace` of an app process from the shell needs `su`; the helper
checks and says so.
- **Detach cleanly or leave the target traced forever**: slots cleared, every tid detached.
The helper does this on every exit path including the caps; a `kill -9` of the helper
leaves `PTRACE_O_EXITKILL` to do the damage — do not do that.
## 6. Hand-offs
- Full execution paths, not single addresses → `references/ete-diff-tracing.md`.
- `TracerPid` must stay 0 → `references/kernel-event-injection.md` (same hardware, armed
from the kernel, no tracer relationship).
- You only need to *call* the routine you located → `references/emulation-and-rpc.md`.
- The watchpoint found the site and you must neutralise a terminate path → the rules in
`references/native-tamper-and-suicide.md` apply unchanged.
## Checklist
- [ ] `plan` accepted the address locally (no window crossing, PC aligned)
- [ ] `emit` + build succeeded; helper runs and prints `READY` with slots > 0
- [ ] **Control run passed**: one deliberate touch produced one `HIT` with a sane PC
- [ ] maps pulled from the same process lifetime; `decode` resolves module+offset
- [ ] Every `ARM tid=` line accounted for against `/proc/<pid>/task`
- [ ] No other tracer (frida, lldb) attached during the window
- [ ] Strength labels on every conclusion: encoders `observed` (host), device behaviour
`unverified` until your control run, kernel-armed route `unverified` until compiled
@@ -17,7 +17,10 @@ Division of labour, so the two files never duplicate:
**This repository does not do kernel development.** Everything in the kernel section is a map of
externally documented mechanisms with sources, labelled `inferred` — none of it was executed here.
That is deliberate: the purpose is to stop you from spending a day rediscovering the version
gate () or the conflict (), not to teach kernel hacking.
gate () or the conflict (), not to teach kernel hacking. When the goal is not *hiding from a
detector* but *instrumenting or altering the target from the kernel* — invisible watchpoints,
register rewrites at a trap — that is a different activity with its own primitive map and recipes:
`references/kernel-event-injection.md`. The two files share the §4a gate table and the stop rules.
**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
@@ -0,0 +1,127 @@
# Kernel-side event injection — the driver as your hands inside the target
Load this when the observation or the change you need is **impossible to deliver from the
target's own process** — `TracerPid` must stay 0, the code must not change by one byte, the
decision must flip at a PC you choose, and it must all happen without any thread or mapping
the target could enumerate. A kernel-side driver can do this because the arm64 debug
registers can be programmed **on the target's behalf** through the kernel's
perf/breakpoint layer, and the overflow callback runs holding the interrupted context's
`pt_regs`. This file owns the primitive map and the composition recipes;
`scripts/kernel_instrument_scaffold.py` generates the templates.
**Division of labour, so nothing gets said twice.** `references/kernel-and-environment-hardening.md`
owns *hiding from detection* — which root scheme, which module, when escalation is wrong;
its §4a gate table applies to this file unchanged. `references/hardware-watchpoints.md`
owns the *ptrace* delivery of the same hardware, including its honest `TracerPid` column.
This file is the third leg: **instrumentation and injection from the kernel**, when the
other two deliveries are unacceptable.
**Claim strength.** `gates`/`generate`/`verify` run on the host — `observed` for the
generator. **No kernel-side file this scaffold emits has been compiled or loaded anywhere,
by anyone in this repository's passes** — every generated file says so in its own header,
and the register-rewrite action is explicitly labelled experimental. The API surface
(`register_user_hw_breakpoint` / `perf_event_create_kernel_counter`,
`PERF_TYPE_BREAKPOINT` with `bp_type`/`bp_addr`/`bp_len`) is the kernel's documented
GPL-exported interface —
`inferred` as a mechanism, `unverified` as a route until a device with the matching gate
compiles and loads it.
## 1. The primitive map — what each route can actually do
| primitive | KPM (KernelPatch) | out-of-tree LKM | eBPF | Zygisk module |
|---|---|---|---|---|
| hardware watchpoint on the target task, hit logged to dmesg | yes (via perf bp layer, symbols resolved at load) | yes (`register_user_hw_breakpoint`, GPL-exported) | **no** — no perf breakpoint creation from BPF | no |
| rewrite a register at the trap (flip a branch decision, no code change) | template action, **experimental** | template action, **experimental** | no | no |
| read/write the target's memory from outside it | yes | yes (`access_process_vm`) | read via helpers, write rarely | in-process only (it *is* the process) |
| force a syscall return value | yes — see `kernelsu_syscall_mask.py` | yes (syscall table / inline hooks) | observe; rewrite only for `ALLOW_ERROR_INJECTION` targets | no (wrong layer) |
| deliver a signal at a chosen moment | yes | yes | yes | in-process |
| visible to the target (`TracerPid`, foreign threads, code deltas) | **none** | **none** | **none** | in-process, pre-init_array |
Read the table as instructions: the **only** route that observes *and* alters with zero
in-process footprint is a kernel module; eBPF is an observer; Zygisk is early *inside* the
process — the right answer for init_array-early checks
(`references/kernel-and-environment-hardening.md` §4 Kernel-level interception — the map,
and the version gate), not for invisible watchpoints.
## 2. The recipes — "wish-mode", assembled from the other two files
These are compositions, not new mechanisms. Each names its deliverable and its fallback,
because a kernel route is never the first thing to try — it is what you escalate to when
the cheaper route's ledger (`references/hardware-watchpoints.md` §1 The stealth ledger)
says the target will see you.
**Recipe 1 — "give me the plaintext key, whatever wraps the cipher."**
Watch the buffer the crypto fills, kernel-armed so the RASP never notices, and read the
buffer at the hit. Steps: locate the buffer statically (`svc_scan.py`, decompiler) →
`kernel_instrument_scaffold.py generate --pid <PID> --watch w:<ADDR>:8:w` → load on a
device with the KPM/LKM gate closed → `dmesg` gives the filling PC → at the next hit read
the buffer (`/proc/<pid>/mem` from root outside the process is enough; no injection needed).
*Fallback:* ptrace watchpoint when `TracerPid` is not read by the target — cheaper, same hit.
**Recipe 2 — "make the check pass without changing one byte."**
The differential (`references/ete-diff-tracing.md` §4) names the branch that first consumes
the valid/invalid difference. Instead of patching that branch (which the page checksum
watches), arm an execution breakpoint at its PC from the kernel and rewrite the condition
register at the trap: the decision flips, the image stays byte-identical.
`generate --break b:<PC> --set-reg x0=0x1`. *This is the experimental action* — the
register rewrite in a perf overflow callback was never run by this repository; validate on
a device you can reboot, and prefer patching the code when the checksum threat model does
not actually include self-verification of that page.
**Recipe 3 — "trace it, but leave TracerPid alone."**
ETE capture needs no ptrace at all (it is perf + aux buffers), and kernel watchpoints need
none either — so the entire observation half of this toolkit composes into a zero-ptrace
session: `ete_capture.py capture` for paths, kernel watchpoints for addresses, dmesg as the
event bus. What remains visible is the perf events themselves (a determined kernel-state
reader can enumerate them) — say so rather than claiming true invisibility.
## 3. The scaffold and its gates
```
python skills/apk-reverse/scripts/kernel_instrument_scaffold.py gates
python skills/apk-reverse/scripts/kernel_instrument_scaffold.py generate \
--out work/kwatch --pid <PID> --watch w:0x7b2000123450:8:w --break b:0x7b20001234c0 --set-reg x0=0x1
python skills/apk-reverse/scripts/kernel_instrument_scaffold.py verify --dir work/kwatch
```
The gates are the §4a table's: **KPM** needs a KernelPatch-patched boot image plus
`aarch64-none-elf-gcc`; **LKM** needs kernel source matching the exact vermagic; the
**userspace module** is an honest carrier that can arm nothing. One gate is new and belongs
to this file: the kernel's **perf breakpoint layer must own the debug registers**
(`CONFIG_HW_PERF_EVENTS`); if it is compiled out, `register_user_hw_breakpoint` does
not exist and both templates close — check
`ls /sys/bus/event_source/devices/breakpoint` on the
device. Design notes carried in the generated files, all load-bearing: the callback runs
on **every hit, system-wide** — the generated template prints **unfiltered** on entry, so
adding a cheap first-statement filter is a required change before you rely on any hit
(the same lesson the syscall-mask template learned publicly); symbols in a KPM are
declared without `extern` or the loader rejects the module; and a slot that fails to arm
is logged, never silently skipped.
## 4. When to stop — the same stop rules, extended one row
Every rule of `references/kernel-and-environment-hardening.md` §7 When to stop escalating —
do not counter-escalate a userspace check with kernel development
applies verbatim, plus this file's own:
| Situation | Do this | Not this |
|---|---|---|
| The check sees `TracerPid` but not perf/kernel state | kernel watchpoint + ETE, zero ptrace | patching the boot image to hide one field |
| The decision site is known and the page is not self-verified | byte patch (`references/native-and-so.md`) | kernel register rewrite |
| You want a kernel route on a 4.14/Magisk device | record the closed gate, use ptrace watchpoint or Zygisk | "just build a quick module" |
| The register rewrite did not take effect | report it — experimental means the first run is a measurement | a second, larger rewrite from the same unverified template |
## Checklist
- [ ] Cheaper route's ledger consulted first (hardware-watchpoints §1); kernel chosen
because the target's detection actually demands it, not because it is stronger
- [ ] `gates` run; KPM/LKM/perf-bp gates answered for **this** device, closed ones recorded
- [ ] Watch specs passed `plan`-style validation at generation (window, alignment)
- [ ] Control: one deliberate touch on a watched global produces one dmesg line before the
real window — an unverified template owes this before any hit is trusted
- [ ] Callback filter first, no sleeping, `printk` only (plus the explicitly-labelled
experimental register action, only on a device you can reboot)
- [ ] Every kernel file still carries its `NOT COMPILED AND NOT LOADED` banner after your
edits (`verify --dir`), or the banner is replaced by your own measurement
- [ ] Deliverable sentence re-read: a kernel-route result is a **rooted-device result**,
labelled a fallback unless the task asked for exactly that
@@ -53,6 +53,7 @@ script header in `scripts/stalker_trace.js`.
| Question | Observation | Tool |
|---|---|---|
| Which blocks executed, and how often? | Block-level trace with execution counts | `scripts/stalker_trace.js` + `scripts/stalker_report.py` |
| Where did two runs of the same operation diverge? | Differential reduction of two block streams — the input-dependent set | `scripts/trace_diff.py` (works on Stalker logs and decoded cs_etm alike; the hardware producer and its gates are `references/ete-diff-tracing.md`) |
| Which functions inside the module call each other? | Call edges from the same trace | same pair (`call` events) |
| Which imported function does this stub reach? | Relocation table, not trace | `scripts/elf_plt.py` |
| Where did the process die, and did it look arranged? | Crash record, signal, fault address, backtrace split by owner | `scripts/native_crash.py` |
@@ -221,7 +222,10 @@ zero-event trace survived the treatment arm unchanged. Treat those as two proble
**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.
loop, nonzero `BLK` lines) before it is interpreted. When the device itself carries ETE/TRBE
(`references/ete-diff-tracing.md` — probe with `scripts/ete_capture.py gates --device`), the
hardware trace replaces Stalker for this question with no code modification and no DBI cache; keep
this file's Stalker route for the devices without it.
**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
+16
View File
@@ -67,6 +67,12 @@ reference file is not named here, or when a script is not named here.
| You only need to **call** the target's own routine (sign, token, encrypt) rather than change the app | `references/emulation-and-rpc.md` — emulate it, or service-ify the live function over Frida RPC |
| A native function is a many-thousand-line `switch` state machine, or the decompiler's output is meaningless | `references/native-dbi-and-deobfuscation.md` — OLLVM shapes, a Stalker trace, and how far a trace actually gets you |
| `Stalker.follow` installs but no events arrive, or following a hot libc export crashes the process | `references/native-dbi-and-deobfuscation.md` §6 failure modes — this repository measured both |
| You must observe or alter the target with **no code change, no injected thread, and `TracerPid` = 0** — page checksums catch every hook | `hardware-watchpoints.md` — hardware debug slots via ptrace with its honest stealth ledger, then `kernel-event-injection.md` for the kernel-armed route that needs no tracer at all |
| A crash's backtrace cannot be trusted — frames in wrong modules or anonymous regions, flattened/virtualized code, or a smashed stack | `hardware-sampling-and-crash.md` — BRBE branch records / ETE tail as the real control flow, then `scripts/trace_addr2sym.py` to symbolize every PC |
| You hold a list of **runtime addresses** (hits, trace PCs, divergence points) and need symbols / static offsets, not hex | `hardware-sampling-and-crash.md` §3 The crash loop — `scripts/trace_addr2sym.py` with its vaddr/file-offset rules |
| A trace of a virtualized function is just an **interpreter loop repeating** and reads like noise | `scripts/vm_handler_slice.py` — slice at dispatcher entries into handler segments, cluster signatures, recover the guest-opcode trajectory (xvmp-calibrated) |
| You need to know **where two runs of the same operation diverge** (password/license/key), not just what executed | `ete-diff-tracing.md` — ETE hardware-trace gates plus the differential method; `scripts/trace_diff.py` also diffs Stalker logs |
| A generated KPM/LKM template is in front of you, or the kernel route feels like the plan | `kernel-event-injection.md` — the primitive map, the composition recipes, and the gates every kernel-side file must pass before it is a weapon |
| 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` |
@@ -124,6 +130,10 @@ _54 row(s) below the header._
| `references/lsposed-and-modules.md` | The client-side logic is reachable but **a rebuilt APK is refused**: delivering a system-level hook module instead, its gradle-free build chain, scope configuration and verification, and the layer a Java module cannot reach |
| `references/emulation-and-rpc.md` | You need to **call** a routine rather than change the app — a signing routine, a token, a cipher: emulated execution (Unidbg/Unicorn) with its environment-filling cost, versus service-ifying the live function over Frida RPC |
| `references/native-dbi-and-deobfuscation.md` | A native function is an OLLVM state machine, or you need instruction-level execution evidence: Stalker traces, the trace-to-CFG route, the Stalker/QBDI/emulation decision, and the **measured** zero-event and crash boundaries |
| `references/hardware-watchpoints.md` | You must observe who reads/writes one memory location with **no code modification** — hardware watchpoints/breakpoints via the arm64 debug registers (ptrace route, its honest `TracerPid` ledger, the alignment rules, the control run you owe any hit) |
| `references/hardware-sampling-and-crash.md` | The question is **data flow without a known address** (SPE: PC + data address per sample), a **crash whose stack is not trustworthy** (BRBE/ETE-tail branch records), or a list of runtime addresses to symbolize (`trace_addr2sym.py`, PIE vaddr/file-offset rules) |
| `references/ete-diff-tracing.md` | You need **where two runs diverge** (password/license/key) with hardware tracing: the ETE/TRBE gates, per-thread `cs_etm` capture, and the differential method that turns two traces into the input-dependent block set |
| `references/kernel-event-injection.md` | The observation or change must come from a **driver** — invisible watchpoints, register rewrites at a trap, cross-memory writes, zero ptrace: the primitive map, the composition recipes, and the gates the scaffold's templates must pass |
| `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: what each layer can still do, the kernel-route map with its version gate, and when escalating is wrong |
| `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 |
@@ -185,6 +195,12 @@ _43 row(s) below the header._
| `scripts/protobuf_decode_raw.py` | **Schema-free protobuf decode**: hex / file / stdin to a JSON tree, every length-delimited field as a candidate set with ties labelled; `--reencode --check` for a byte-exact round trip |
| `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, verify it in a closed loop, render smali |
| `scripts/kernelsu_syscall_mask.py` | Generate a KernelSU/APatch syscall-masking scaffold: an installable userspace module plus KPM/LKM/eBPF kernel-side templates, each with its version gate and an explicit unverified label. **A userspace module cannot change a syscall return value** |
| `scripts/hw_watch.py` | **Invisible memory watchpoints / execution breakpoints** on arm64: plan (WCR/BCR encoders with the 8-byte-window rules, host-tested), emit (self-contained on-device C helper, compiles clean as static aarch64), gates, run (build+push+stream hits), decode (module+offset per hit). ptrace route: `TracerPid` stays visible — the ledger is in the reference |
| `scripts/trace_diff.py` | **Differential trace reduction**: reduce one trace to runs+histogram, or diff two traces into first-divergence / divergent regions / blocks-unique-to-each-side. Producer-agnostic (Stalker `BLK` logs or any address-per-line stream, incl. decoded cs_etm) |
| `scripts/ete_capture.py` | **CoreSight ETE capture with gates first**: the five gates (kernel era, coresight sysfs, cs_etm PMU, perf-with-CORESIGHT, permission), the perf/OpenCSD build recipe, and the per-thread `cs_etm//u` record wrapper with the `--control` validation run |
| `scripts/kernel_instrument_scaffold.py` | Generate the **kernel-side instrumentation scaffold**: KPM/LKM watchpoint+register-rewrite templates plus an honest userspace carrier, each with its gate and a `NOT COMPILED AND NOT LOADED` banner that `verify` enforces |
| `scripts/trace_addr2sym.py` | **Runtime addresses -> symbols + static offsets**, zero-dependency ELF64: PT_LOAD vaddr/file-off conversion, `.dynsym`/`.symtab`, stripped fallback; accepts hw_watch hits, stalker/trace `mod+off` keys, or raw address lists -- host-verified against a real cross-compiled aarch64 `.so` |
| `scripts/vm_handler_slice.py` | **De-virtualize an interpreter trace**: dispatcher detection (top + predecessor diversity), segment slicing at dispatch entries, signature clustering, and the guest-**opcode trajectory** via an optional handler->opcode map -- calibrated against the open xvmp IR virtualizer (`vm_exec`, slot machine, 11 opcodes) |
| `scripts/capabilities.py` | The capability registry `doctor.py` reads: per capability the requires-closure, what is present, what is missing, an install hint and an estimated cost — so "can this machine do X" is computed rather than guessed |
| `scripts/device_shell.py` | Shared device-side command construction: POSIX single-quote escaping for `su -c`, argv arrays rather than string concatenation, and Android identifier validation (`--pkg`, components) so a malformed value is rejected here instead of being parsed by the device shell |
| `scripts/rasc_build.py` | Build and verify **rasc**, the Rust re-implementation of ASC: `--check` what is present, `--build` clone + cargo (no release asset exists), `--verify <apk>` compares its class-definition set against the Python `droidasc` and fails on any difference |
+372
View File
@@ -0,0 +1,372 @@
#!/usr/bin/env python3
"""ete_capture.py -- arm64 hardware execution tracing (CoreSight ETE) capture, with gates first.
What this is
------------
Modern arm64 cores (ARMv8.4+ era: Cortex-A78/X1/A710/X2 and later, all ARMv9 big
cores) carry **ETE**, a trace unit that streams executed-branch/instruction
records off-core in hardware, and **TRBE**, a per-CPU buffer that collects them
*self-hosted* -- no debugger, no JTAG, no code modification, no DBI code cache.
The Linux perf subsystem exposes it as the `cs_etm` PMU: a per-thread trace
recorded with `perf record -e cs_etm//u --per-thread`, decoded on the host or
device by perf built against OpenCSD. `references/ete-diff-tracing.md` owns the
method (and the pairing with `scripts/trace_diff.py`); this script owns the
gates, the recipe and the capture.
Why the gates come first
------------------------
The ETE route is closed on most devices people actually own, and every closed
gate fails *silently* if you skip it: an old kernel reports no cs_etm PMU; a
kernel with coresight but no TRBE cannot do per-thread buffers; a perf without
OpenCSD records an aux area it cannot decode; SELinux can deny perf_event_open
even to root. Probing costs one command per gate. Guessing costs an afternoon.
gate expected check
kernel era >= 5.10 for ETE/TRBE drivers uname -r
coresight devices eteN / trbeN in sysfs ls /sys/bus/coresight/devices
cs_etm PMU dir exists ls /sys/bus/event_source/devices/cs_etm
perf with CORESIGHT perf lists cs_etm perf list | grep cs_etm
permission a control record decodes perf record -e cs_etm//u --per-thread -- true
Measured vs unverified
----------------------
`gates` and `recipe` run on this host (gates probes adb and prints the
device-side plan; `capture` requires a device and exits 3 without one --
contract-tested in tests/cli). **No ETE trace has been captured or decoded by
this repository's passes**: no reference device here has coresight compiled in
(reference kernel 4.14.186). The command shapes follow the kernel's own
tools/perf CoreSight documentation and the OpenCSD project docs -- treat them
as the documented upstream interface, and validate with the control record
before trusting a real capture.
Examples
python ete_capture.py gates
python ete_capture.py gates --device # also runs the device-side probes
python ete_capture.py recipe
python ete_capture.py capture --pid 12345 --seconds 10 --out perf.data
python ete_capture.py capture --pid 12345 --control # validation run first
"""
import argparse
import json
import re
import shlex
import shutil
import subprocess
import sys
GATES = [
{"gate": "kernel era", "expect": ">= 5.10 (ETE/TRBE driver support in-tree)",
"check": "uname -r", "why": "the ETE source and TRBE sink drivers landed with the "
"5.10-era coresight updates; older kernels expose no ETE"},
{"gate": "coresight devices", "expect": "ete<N> and trbe<N> under /sys/bus/coresight/devices",
"check": "ls /sys/bus/coresight/devices", "why": "eteN = the trace unit, trbeN = the "
"self-hosted buffer; etmN/tmc_etrN without "
"trbeN means shared-sink tracing only"},
{"gate": "cs_etm PMU", "expect": "/sys/bus/event_source/devices/cs_etm exists",
"check": "ls /sys/bus/event_source/devices/cs_etm", "why": "perf speaks to coresight "
"through this PMU"},
{"gate": "perf with CORESIGHT", "expect": "perf lists cs_etm, and `perf report -D` decodes "
"packets (OpenCSD linked in)",
"check": "perf list | grep cs_etm", "why": "a stock perf records an aux buffer it cannot "
"decode; decode needs perf built with CORESIGHT=1"},
{"gate": "permission", "expect": "a control record of `true` produces decodable output",
"check": "perf record -e cs_etm//u --per-thread -o ctl.data -- true",
"why": "SELinux can deny perf_event_open even to root; prove the whole pipe on a "
"one-instruction process before the target"},
]
RECIPE = """Building perf with CoreSight decode (pick one):
A. On-device (Termux, no cross toolchain):
pkg install clang make cmake git python
git clone --depth 1 https://github.com/Linaro/OpenCSD.git && cd OpenCSD
cmake -B build -DCMAKE_BUILD_TYPE=Release && cmake --build build -j
# then the kernel's tools/perf against it:
git clone --depth 1 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux/tools/perf && make CORESIGHT=1 \\
EXTLIBS=-l$(pwd)/../../OpenCSD/build/lib/libopencsd \\
O=~/perf-build WERROR=0
~/perf-build/perf record -e cs_etm//u --per-thread -p <PID>
B. Cross (host aarch64 toolchain): same two sources, same CORESIGHT=1, with
CROSS_COMPILE=<prefix> and the OpenCSD built for aarch64 first. Static-link
the result so it drops onto /data/local/tmp as one file.
C. Sanity checks once built (run these in order, read each before continuing):
perf list | grep cs_etm # nothing -> no PMU visibility
perf record -e cs_etm//u --per-thread -o ctl.data -- true
perf report -i ctl.data -D | head # CoreSight packets -> decode works
The control record is not optional: it is the only thing that separates a
closed gate from a capture that looks fine and decodes to nothing.
Capture shape (TRBE per-thread):
perf record -e cs_etm//u --per-thread -p <PID> -o perf.data -- sleep <S>
Shared sink (no TRBE; system-wide-ish, cannot --per-thread):
perf record -e cs_etm/@tmc_etr0/u -C <CPU> -o perf.data -- sleep <S>
Decode to an address stream for scripts/trace_diff.py:
perf script -i perf.data # synthesized instruction/branch events
# then feed the address-bearing lines straight to:
python scripts/trace_diff.py diff --a a.log --b b.log
"""
def adb_base(serial):
"""Locate adb and the serial prefix; (path, prefix) or (None, [])."""
cmd = shutil.which("adb")
if not cmd:
return None, None
return cmd, (["-s", serial] if serial else [])
def adb_shell(adb, prefix, cmd, timeout=15):
"""Run one adb shell command string; return its result."""
return subprocess.run([adb] + prefix + ["shell", cmd], capture_output=True,
text=True, timeout=timeout)
def probe_device(args):
"""Run every device-side gate probe; return (rows, ok_count, total)."""
adb, prefix = adb_base(args.serial)
if not adb:
return None, 0, len(GATES)
rows = []
r = adb_shell(adb, prefix, "uname -r")
kern = (r.stdout or "").strip()
kern_ok = bool(re.match(r"^(\d+)\.(\d+)", kern)) and _kern_at_least(kern, 5, 10)
rows.append({"gate": "kernel era", "probe": "uname -r", "value": kern or "?",
"ok": kern_ok})
r = adb_shell(adb, prefix, "ls /sys/bus/coresight/devices 2>/dev/null")
dev = (r.stdout or "").strip()
has_ete = "ete" in dev
has_trbe = "trbe" in dev
rows.append({"gate": "coresight devices", "probe": "ls /sys/bus/coresight/devices",
"value": (dev or "<none>")[:200], "ok": has_ete and has_trbe,
"note": "ete=%s trbe=%s (trbe absent -> shared-sink only)"
% (has_ete, has_trbe)})
r = adb_shell(adb, prefix, "ls /sys/bus/event_source/devices/cs_etm 2>/dev/null")
rows.append({"gate": "cs_etm PMU", "probe": "ls .../cs_etm",
"value": "present" if (r.stdout or "").strip() else "absent",
"ok": r.returncode == 0 and bool((r.stdout or "").strip())})
r = adb_shell(adb, prefix, "su -c 'perf list' 2>/dev/null | grep -c cs_etm")
n = (r.stdout or "").strip().splitlines()[-1] if (r.stdout or "").strip() else "0"
rows.append({"gate": "perf with CORESIGHT", "probe": "perf list | grep cs_etm",
"value": "%s cs_etm entries" % n, "ok": n not in ("", "0")})
rows.append({"gate": "permission", "probe": "control record (see recipe)",
"value": "not probed by gates; run capture --control",
"ok": False})
ok = sum(1 for row in rows if row["ok"])
return rows, ok, len(GATES)
def _kern_at_least(kern, maj, mnr):
"""True when the kernel version is at least major.minor."""
m = re.match(r"^(\d+)\.(\d+)", kern)
if not m:
return False
a, b = int(m.group(1)), int(m.group(2))
return (a, b) >= (maj, mnr)
def cmd_gates(args):
"""gates subcommand: host probes plus the device-side gates."""
adb = shutil.which("adb")
if args.json:
doc = {"status": "ok", "gates": GATES, "adb": bool(adb)}
if args.device and adb:
rows, ok, total = probe_device(args)
doc["device_rows"] = rows
doc["device_ok"] = ok
doc["device_total"] = total
doc["status"] = "ok" if ok == total else "partial"
print(json.dumps(doc, indent=1))
return 0
print("== ETE capture gates ==")
for g in GATES:
print("\n %s\n expect: %s\n check : %s\n why : %s"
% (g["gate"], g["expect"], g["check"], g["why"]))
print("\nhost: adb %s" % ("present" if adb else "ABSENT -- capture cannot run here"))
device_verdict = "ok"
if args.device and adb:
rows, ok, total = probe_device(args)
print("\ndevice probes (%d/%d closed):" % (ok, total))
for row in rows:
mark = "ok " if row["ok"] else "OPEN"
print(" [%s] %-22s %s" % (mark, row["gate"], row["value"]))
if ok < total:
device_verdict = "partial"
print("\nAn OPEN gate is a route decision, not a failure -- record it per")
print("references/kernel-and-environment-hardening.md and do not retry mid-task.")
elif args.device:
print("\n--device asked for device probes but adb is absent")
print("\nRESULT=%s" % ("ok" if (adb and device_verdict == "ok") else "partial"))
return 0 if adb else 0
def cmd_recipe(args):
"""recipe subcommand: build perf with CoreSight/OpenCSD."""
print(RECIPE)
print("RESULT=ok")
return 0
def control_record_command(out_name):
"""The one-instruction control record: `perf record` of `true` alone.
The whole command travels inside one `su -c` string, so quoting is
two-layered: quote the filename inside the inner command, then quote the
complete inner command for `su -c` (a quote spliced between the literal
single quotes would run in the outer device shell).
"""
inner = ("cd /data/local/tmp && perf record -e cs_etm//u --per-thread "
"-o %s -- true" % shlex.quote(out_name))
return "su -c %s 2>&1" % shlex.quote(inner)
def control_decode_command(out_name):
"""The control's decode gate: `perf script` on the control recording."""
inner = "cd /data/local/tmp && perf script -i %s" % shlex.quote(out_name)
return "su -c %s 2>&1" % shlex.quote(inner)
def perf_record_command(args):
"""The capture command line handed to `adb shell`.
Two rules that are easy to get wrong and load-bearing here: `-o` must
stay BEFORE the `--` workload separator (perf hands everything after
`--` to the target command, so a trailing `-o` never reaches perf), and
every interpolated value is shell-quoted because the whole command
travels inside one `su -c '...'` string -- an unquoted value containing
a quote would run in the outer device shell. `--cmd` is left unquoted
on purpose: the workload argument list is meant to be word-split by the
device shell.
"""
q = shlex.quote
target = "-p %s" % q(str(args.pid)) if args.pid else "-- %s" % args.cmd
remote = "/data/local/tmp/%s" % (args.out.split("/")[-1] or "perf.data")
inner = ("cd /data/local/tmp && timeout %d perf record -e %s --per-thread "
"-o %s %s" % (args.seconds, q(args.event), q(remote), target))
return "su -c %s 2>&1" % q(inner)
def cmd_capture(args):
"""capture subcommand: control record, record, pull."""
if args.seconds < 1:
# `timeout 0` disables the timer (or kills instantly, by flavour):
# either way the --pid route has no capture window and the CLI would
# wait on a record that never ends. Refused before any device check:
# argument validity must not depend on adb being present.
print("RESULT=refused")
print("--seconds must be a positive capture window, got %d" % args.seconds,
file=sys.stderr)
return 2
adb = shutil.which("adb")
if not adb:
print("RESULT=blocked")
print("no adb on PATH: capture needs a rooted device with the ETE gates closed "
"(run `gates --device` first)", file=sys.stderr)
return 3
prefix = ["-s", args.serial] if args.serial else []
if args.control:
out = args.out.replace(".data", "") + ".ctl.data" if args.out.endswith(".data") \
else args.out + ".ctl.data"
ctl = out.split("/")[-1]
r = adb_shell(adb, prefix, control_record_command(ctl), timeout=60)
print(r.stdout or "")
if (r.returncode != 0
or ("cs_etm" in (r.stdout or "") and "failed" in (r.stdout or "").lower())):
print("RESULT=blocked")
print("control record failed: the ETE pipe is not usable on this device as-is")
return 3
# A record exiting 0 only proves perf was not refused -- the documented
# --control guarantee (references/ete-diff-tracing.md: "the control has
# produced packets") is that the record DECODES. Hold the capture to
# the same bar the reference checklist applies to a human.
dec = adb_shell(adb, prefix, control_decode_command(ctl), timeout=60)
print(dec.stdout or "")
dec_lines = [ln for ln in (dec.stdout or "").splitlines()
if ln.strip() and not ln.startswith("#")]
if dec.returncode != 0 or not dec_lines:
print("RESULT=blocked")
print("control record did not decode (perf script exit %d, %d event lines): "
"a capture taken now would be indistinguishable from an empty one, "
"and an empty trace is a capture failure, not a result"
% (dec.returncode, len(dec_lines)), file=sys.stderr)
return 3
print("control record done: %s (decoded %d event lines)" % (out, len(dec_lines)))
if not args.pid and not args.cmd:
print("RESULT=refused")
print("capture needs --pid or --cmd", file=sys.stderr)
return 2
perf_cmd = perf_record_command(args)
remote = "/data/local/tmp/%s" % (args.out.split("/")[-1] or "perf.data")
# a perf.data left by an earlier run would let the pull below succeed on
# stale data; remove it first (best effort -- the exit-status check below
# is what fails the run when the pipe is unusable)
adb_shell(adb, prefix,
"su -c %s 2>&1" % shlex.quote("rm -f %s" % shlex.quote(remote)),
timeout=15)
print("running: %s" % perf_cmd)
proc = subprocess.Popen([adb] + prefix + ["shell", perf_cmd],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
for line in proc.stdout:
print(line.rstrip())
rc = proc.wait()
if rc not in (0, 124): # 124 is `timeout`'s end-of-window kill: the normal end
print("RESULT=blocked")
print("capture did not complete: adb shell exited %d (su denied, perf refused, "
"or the workload never started)" % rc, file=sys.stderr)
return 3
local = args.out
pull = subprocess.run([adb] + prefix + ["pull", remote, local],
capture_output=True, text=True)
if pull.returncode != 0:
print("RESULT=blocked")
print("pull failed: %s" % (pull.stderr or "").strip())
return 3
print("pulled %s" % local)
print("decode next: perf script -i %s (feed the address lines to trace_diff.py)"
% local)
print("RESULT=ok")
return 0
def main():
"""CLI entry: subcommand dispatch."""
ap = argparse.ArgumentParser(
description="CoreSight ETE hardware trace capture on arm64 Android: gates, "
"perf-with-CORESIGHT recipe, per-thread record wrapper.",
epilog="references/ete-diff-tracing.md owns the method and the differential "
"that turns two captures into an answer.")
sub = ap.add_subparsers(dest="cmd", required=True)
p = sub.add_parser("gates", help="the five gates, why each exists, and how to probe it")
p.add_argument("--device", action="store_true",
help="also run the device-side probes over adb (needs a device)")
p.add_argument("--serial", help="adb device serial")
p.add_argument("--json", action="store_true")
p.set_defaults(fn=cmd_gates)
p = sub.add_parser("recipe", help="print how to build perf with CoreSIGHT/OpenCSD decode")
p.set_defaults(fn=cmd_recipe)
p = sub.add_parser("capture", help="record a per-thread cs_etm trace on the device")
p.add_argument("--pid", help="trace this pid (--per-thread, TRBE path)")
p.add_argument("--cmd", help="spawn and trace this command instead of --pid")
p.add_argument("--seconds", type=int, default=10, help="record window (default 10)")
p.add_argument("--event", default="cs_etm//u",
help="perf event spec (default cs_etm//u; shared sink: cs_etm/@tmc_etr0/u)")
p.add_argument("--out", default="perf.data", help="local output file after pull")
p.add_argument("--control", action="store_true",
help="run the one-instruction control record first and refuse on failure")
p.add_argument("--serial", help="adb device serial")
p.set_defaults(fn=cmd_capture)
args = ap.parse_args()
try:
return args.fn(args)
except BrokenPipeError:
return 0
if __name__ == "__main__":
sys.exit(main())
+977
View File
@@ -0,0 +1,977 @@
#!/usr/bin/env python3
"""hw_watch.py -- invisible memory watchpoints and execution breakpoints on arm64 Android.
What this is, and the honest stealth ledger
-------------------------------------------
An arm64 core carries a small number of *hardware* debug slots (typically 6
breakpoints, 4 watchpoints). A watchpoint compares the effective data address of
every load/store the core executes -- in silicon, before the instruction
retires -- and traps to the kernel when it matches. Nothing about the target's
code or memory changes: no inline patch, no trampoline, no injected thread, no
foreign mapping, no DBI code cache. A library that checksums its own pages,
scans for hooks, or counts threads finds nothing, because there is nothing to
find in its process image.
The one thing a ptrace-based watchpoint *does* leave is ``TracerPid`` in
``/proc/<pid>/status``: the tracer relationship is a kernel fact, and this tool
does not hide it. The ledger, in full:
| delivery | code modified | foreign thread/mapping | TracerPid | survives page checksums |
|---------------------|---------------|------------------------|-----------|-------------------------|
| inline hook | yes | no | no | NO -- caught by design |
| Frida Stalker | yes (copy) | yes (agent + cache) | no | no |
| ptrace hw watchpoint| NO | no | YES | yes |
| kernel hw watchpoint| NO | no | no | yes (kernel-event-injection.md) |
Limits that decide whether the tool fits the question: 8-byte maximum per
watchpoint, byte-select inside one aligned 8-byte window only, per-thread slots
(every thread must be armed, new threads arrive unarmed until caught), and the
process must be ptraceable at all (root; nobody else may already be tracing it).
What runs where
---------------
``plan`` pure host: encode DBGWCR/DBGBCR values for an address and check the
alignment rules bite before anything touches a device.
``emit`` pure host: write the self-contained on-device helper (C, one file,
no dependency beyond bionic).
``gates`` host probe: is adb here, is an arm64 compiler here -- plus the exact
one-liners to run on the device to close the device-side gates.
``run`` device: build (NDK clang by default, Termux clang on request), push,
run the helper as root, stream hits to a log.
``decode`` pure host: turn a hit log into module+offset, given a maps file.
Measured vs unverified
----------------------
The encoders and the plan/emit/decode paths are host-tested
(tests/unit/test_hw_watch.py), and the emitted helper **compiles clean as a
static aarch64 ELF** on a cross toolchain (observed: gcc 16.2, `-static`, zero
errors). **No watchpoint has been armed on a device by this repository's
passes** -- the ptrace dance, the slot capacity probe and the clone-catch path
follow the kernel ABI (arch/arm64 ptrace + hw_breakpoint) and must be validated
on your device with the control procedure in references/hardware-watchpoints.md
before any hit is trusted.
Examples
python hw_watch.py plan --addr 0x7b2000123456 --len 16 --access w
python hw_watch.py plan --break 0x7b20001234c0
python hw_watch.py emit --out work/hwwatch
python hw_watch.py gates
python hw_watch.py run --pkg <PKG> --addr 0x7b2000123456 --len 8 --access rw \\
--seconds 20 --out hits.log
python hw_watch.py decode --hits hits.log --maps maps.txt
"""
import argparse
import json
import os
import shutil
import subprocess
import sys
# ---------------------------------------------------------------------------
# Kernel ABI constants (arm64 Linux). These are stable uapi numbers, not
# toolchain facts: include/uapi/linux/ptrace.h, include/uapi/linux/elf.h,
# arch/arm64/include/uapi/asm/ptrace.h.
# ---------------------------------------------------------------------------
PTRACE_CONT = 7
PTRACE_SINGLESTEP = 9
PTRACE_ATTACH = 16
PTRACE_DETACH = 17
PTRACE_GETSIGINFO = 0x4202
PTRACE_SETSIGINFO = 0x4203
PTRACE_GETREGSET = 0x4204
PTRACE_SETREGSET = 0x4205
PTRACE_SEIZE = 0x4206
PTRACE_INTERRUPT = 0x4207
PTRACE_LISTEN = 0x4208
PTRACE_O_EXITKILL = 0x01000000
PTRACE_O_TRACECLONE = 0x00000008
NT_PRSTATUS = 1
NT_ARM_HW_BREAK = 0x402
NT_ARM_HW_WATCH = 0x403
TRAP_HWBKPT = 4 # siginfo si_code for a hardware break/watchpoint hit
USER_PT_REGS_BYTES = 272 # 31 x-regs + sp + pc + pstate, 8 bytes each
MAX_SLOTS = 16 # struct user_hwdebug_state.dbg_regs[] size
# DBGWCR_EL1 field map (ARM DDI 0487): E[0] enable; PAC[2:1] privilege access
# control (0b10 = EL0/unprivileged only -- the value a userspace watchpoint
# wants); LSC[4:3] load/store control (01 load, 10 store, 11 both); BAS[12:5]
# byte-address-select inside the 8-byte window named by DBGWVR.
WCR_E = 1
WCR_PAC_EL0 = 0b10 << 1
WCR_LSC = {"r": 0b01 << 3, "w": 0b10 << 3, "rw": 0b11 << 3}
WCR_BAS_SHIFT = 5
# DBGBCR_EL1 for an unlinked address-matching A64 execution breakpoint:
# BAS[13:8] = 0b111111 (whole-address match), PMC[2:1] = 0b10 (EL0), BT = 0.
BCR_ADDR_MATCH_EL0 = (0x3F << 8) | WCR_PAC_EL0 | WCR_E
# arm64 user_pt_regs offsets (bytes) -- used by the helper and by decode.
REG_OFFSETS = {("x%d" % i): i * 8 for i in range(31)}
REG_OFFSETS.update({"sp": 31 * 8, "pc": 32 * 8, "pstate": 33 * 8})
class PlanError(ValueError):
"""An address/length/access combination the hardware cannot express."""
def encode_watch(addr, length, access):
"""Return (wvr, wcr) for a watchpoint, or raise PlanError with the reason.
The hardware rule that bites: BAS selects bytes inside ONE aligned 8-byte
window. A watch that starts at 0x...4 with length 8 crosses the window and
cannot be expressed in one slot (move the address or use two).
"""
if length not in range(1, 9):
raise PlanError("watch length must be 1..8 bytes, got %d" % length)
if access not in WCR_LSC:
raise PlanError("access must be one of r/w/rw, got %r" % access)
off = addr & 7
if off + length > 8:
raise PlanError(
"watch [0x%x + %d] crosses its aligned 8-byte window "
"(addr&7=%d): shift the address or split across two slots"
% (addr, length, off))
bas = (((1 << length) - 1) << off) << WCR_BAS_SHIFT
wcr = bas | WCR_LSC[access] | WCR_PAC_EL0 | WCR_E
return addr & ~0x7, wcr
def encode_break(addr):
"""Return (bvr, bcr) for an execution breakpoint at a 4-byte-aligned PC."""
if addr & 3:
raise PlanError(
"breakpoint PC 0x%x is not 4-byte aligned -- A64 instructions are"
% addr)
return addr, BCR_ADDR_MATCH_EL0
def canonical_examples():
"""The two encodings everything else is a variation of (unit-tested)."""
return encode_watch(0, 8, "rw")[1], BCR_ADDR_MATCH_EL0
# ---------------------------------------------------------------------------
# The on-device helper. One C file, no dependency beyond bionic; every ptrace
# call goes through syscall() so no header availability can change behaviour.
#
# STATUS: the syntax is host-checked (gcc -fsyntax-only where a compiler
# exists) and the ABI numbers are the kernel's uapi values, but this helper
# has not been run on a device by this repository's passes. Validate with the
# control procedure in references/hardware-watchpoints.md first.
# ---------------------------------------------------------------------------
HWWATCH_C = r'''/* hwwatch_helper.c -- arm64 hardware watchpoint/breakpoint driver.
*
* Generated by skills/apk-reverse/scripts/hw_watch.py -- do not edit by hand.
* TEMPLATE NOT YET DEVICE-TESTED BY THIS REPOSITORY. Build it, run the control
* procedure (references/hardware-watchpoints.md) before trusting a hit, and
* remember: ptrace makes TracerPid non-zero in /proc/<pid>/status.
*
* Usage (as root):
* hwwatch <pid> w:<addr>:<len>:<r|w|rw> [w:... ...] [b:<addr> ...]
* [--max-hits N] [--seconds S]
* Output grammar (one logical line per event):
* READY pid=<pid> watch_slots=<n> break_slots=<n>
* ARM tid=<tid> watch=<k> break=<k>
* HIT tid=<tid> kind=watch|break pc=0x.. addr=0x.. sp=0x.. lr=0x.. x0=0x.. .. x7=0x..
* EXIT tid=<tid> code=<n>
* DONE hits=<n> reason=max-hits|seconds|exit|clean-detach|error
*/
#define _GNU_SOURCE
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <dirent.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <fcntl.h>
/* --- kernel ABI numbers (arm64); kept local so headers cannot disagree --- */
#define PTRACE_CONT 7
#define PTRACE_SINGLESTEP 9
#define PTRACE_DETACH 17
#define PTRACE_GETSIGINFO 0x4202
#define PTRACE_GETREGSET 0x4204
#define PTRACE_SETREGSET 0x4205
#define PTRACE_SEIZE 0x4206
#define PTRACE_INTERRUPT 0x4207
#define PTRACE_LISTEN 0x4208
#define PTRACE_O_EXITKILL 0x01000000
#define PTRACE_O_TRACECLONE 0x00000008
#define NT_PRSTATUS 1
#define NT_ARM_HW_BREAK 0x402
#define NT_ARM_HW_WATCH 0x403
#ifndef TRAP_HWBKPT
#define TRAP_HWBKPT 4
#endif
#define PTRACE_EVENT_CLONE 3
#define PTRACE_EVENT_STOP 128
/* Slot layout is the kernel ABI (arch/arm64/include/uapi/asm/ptrace.h,
* struct user_hwdebug_state): per slot a 64-bit ADDRESS register first, then a
* 32-bit CONTROL register, then 4 bytes of pad -- 16 bytes per slot. Swapping
* the two arms every slot with the control bits as the address. */
struct hwslot { uint64_t wvr; uint32_t wcr; uint32_t pad; };
struct hwstate { uint32_t dbg_info; uint32_t pad; struct hwslot slot[16]; };
struct uregs { uint64_t x[31]; uint64_t sp, pc, pstate; };
static long pt(long req, long pid, long addr, long data) {
return syscall(SYS_ptrace, req, pid, (void *)addr, (void *)data);
}
/* ---- configuration filled from argv ---- */
struct spec { int is_break; uint64_t addr; int len; char access[3]; };
static struct spec specs[16];
static int nspec = 0, max_hits = 0;
static long run_seconds = 0;
#define MAXTIDS 256
static int tids[MAXTIDS]; /* known threads of the target */
static int ntids = 0;
static int target_pid = 0;
static long hits = 0;
static void say(const char *fmt, ...) {
va_list ap; va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
fputc('\n', stdout);
fflush(stdout);
va_end(ap);
}
static int known(int tid) {
for (int i = 0; i < ntids; i++) if (tids[i] == tid) return 1;
return 0;
}
static void remember(int tid) {
if (tid <= 0 || known(tid) || ntids >= MAXTIDS) return;
tids[ntids++] = tid;
}
static int get_regset(int tid, int nt, void *buf, size_t len) {
struct iovec iov = { buf, len };
return (int)pt(PTRACE_GETREGSET, tid, nt, (long)&iov);
}
static int set_regset(int tid, int nt, const void *buf, size_t len) {
struct iovec iov = { (void *)buf, len };
return (int)pt(PTRACE_SETREGSET, tid, nt, (long)&iov);
}
/* Read the slot capacity the kernel reports (dbg_info & 0xff). */
static int slot_capacity(int tid, int nt) {
struct hwstate st;
memset(&st, 0, sizeof st);
if (get_regset(tid, nt, &st, sizeof st) != 0) return -1;
return (int)(st.dbg_info & 0xff);
}
static int wcap = 0, bcap = 0; /* slot capacities, probed once */
/* Arm every watch/break spec on one stopped thread. Watchpoints and
* breakpoints live in separate tables on arm64, so slot indexes of the two
* kinds may overlap. */
static int arm_thread(int tid) {
struct hwstate w, b;
int nw = 0, nb = 0;
memset(&w, 0, sizeof w); memset(&b, 0, sizeof b);
for (int i = 0; i < nspec; i++) {
const struct spec *s = &specs[i];
if (s->is_break) {
if (nb >= bcap) { say("ARMFAIL tid=%d slot=break%d capacity=%d", tid, nb, bcap); return -1; }
b.slot[nb].wcr = (0x3Fu << 8) | (0x2u << 1) | 1u; /* BAS all-ones, PMC=EL0, E */
b.slot[nb].wvr = s->addr;
nb++;
} else {
if (nw >= wcap) { say("ARMFAIL tid=%d slot=watch%d capacity=%d", tid, nw, wcap); return -1; }
unsigned bas = (((1u << s->len) - 1u) << (s->addr & 7)) << 5;
unsigned lsc = s->access[0] == 'r' && s->access[1] == 'w' ? 3u
: (s->access[0] == 'r' ? 1u : 2u);
w.slot[nw].wcr = bas | (lsc << 3) | (0x2u << 1) | 1u;
w.slot[nw].wvr = s->addr & ~0x7ull;
nw++;
}
}
if (nw && set_regset(tid, NT_ARM_HW_WATCH, &w, 8 + 16 * nw) != 0) return -1;
if (nb && set_regset(tid, NT_ARM_HW_BREAK, &b, 8 + 16 * nb) != 0) return -1;
say("ARM tid=%d watch=%d break=%d", tid, nw, nb);
return 0;
}
static int clear_thread(int tid) {
struct hwstate z;
memset(&z, 0, sizeof z);
set_regset(tid, NT_ARM_HW_WATCH, &z, 8 + 16 * 1);
set_regset(tid, NT_ARM_HW_BREAK, &z, 8 + 16 * 1);
return 0;
}
/* Undo everything: clear slots, detach every seized thread. Every error
* path after the first successful PTRACE_SEIZE must go through here:
* PTRACE_O_EXITKILL means an exit without detach SIGKILLs the target --
* an analysis failure must not terminate the app under analysis. */
static void release_all(void) {
for (int i = 0; i < ntids; i++) clear_thread(tids[i]);
for (int i = 0; i < ntids; i++) pt(PTRACE_DETACH, tids[i], 0, 0);
}
/* Enumerate /proc/<pid>/task, seizing and interrupting every thread. */
static int seize_all(void) {
char path[64];
snprintf(path, sizeof path, "/proc/%d/task", target_pid);
DIR *d = opendir(path);
if (!d) { say("DONE hits=%ld reason=error detail=no-such-process", hits); return -1; }
struct dirent *e;
while ((e = readdir(d))) {
if (e->d_name[0] == '.') continue;
int tid = atoi(e->d_name);
if (!tid || known(tid)) continue;
if (pt(PTRACE_SEIZE, tid, 0, PTRACE_O_EXITKILL | PTRACE_O_TRACECLONE) != 0) {
if (errno == EPERM)
say("FATAL seize tid=%d EPERM: not root, or already traced", tid);
else
say("FATAL seize tid=%d errno=%d", tid, errno);
closedir(d);
/* partial seize: threads seized before this one must not be
* EXITKILLed when we exit -- detach them now */
for (int i = 0; i < ntids; i++) pt(PTRACE_DETACH, tids[i], 0, 0);
return -1;
}
remember(tid);
}
closedir(d);
for (int i = 0; i < ntids; i++)
pt(PTRACE_INTERRUPT, tids[i], 0, 0);
/* wait for each to report stopped; other tids' stops stay queued and are
* reaped by the main loop */
for (int i = 0; i < ntids; i++) {
int st;
pid_t r;
do { r = waitpid(tids[i], &st, __WALL); } while (r < 0 && errno == EINTR);
(void)r;
}
return 0;
}
static void print_hit(int tid, int is_break, struct uregs *r, uint64_t waddr) {
printf("HIT tid=%d kind=%s pc=0x%llx addr=0x%llx sp=0x%llx lr=0x%llx "
"x0=0x%llx x1=0x%llx x2=0x%llx x3=0x%llx x4=0x%llx x5=0x%llx "
"x6=0x%llx x7=0x%llx\n",
tid, is_break ? "break" : "watch",
(unsigned long long)r->pc, (unsigned long long)waddr,
(unsigned long long)r->sp, (unsigned long long)r->x[30],
(unsigned long long)r->x[0], (unsigned long long)r->x[1],
(unsigned long long)r->x[2], (unsigned long long)r->x[3],
(unsigned long long)r->x[4], (unsigned long long)r->x[5],
(unsigned long long)r->x[6], (unsigned long long)r->x[7]);
fflush(stdout);
}
/* Step over an execution breakpoint: disable the slot, single-step, re-arm.
* Without this dance the re-enabled trap fires on the same PC forever. */
static int step_over(int tid, uint64_t pc) {
struct hwstate b;
int nb = 0;
memset(&b, 0, sizeof b);
for (int i = 0; i < nspec; i++)
if (specs[i].is_break && specs[i].addr != pc) {
b.slot[nb].wcr = (0x3Fu << 8) | (0x2u << 1) | 1u;
b.slot[nb].wvr = specs[i].addr;
nb++;
}
set_regset(tid, NT_ARM_HW_BREAK, &b, 8 + 16 * 16);
pt(PTRACE_SINGLESTEP, tid, 0, 0);
int st;
waitpid(tid, &st, __WALL);
return arm_thread(tid);
}
int main(int argc, char **argv) {
if (argc < 3) { fprintf(stderr, "usage: hwwatch <pid> w:0xADDR:LEN:rw [b:0xADDR] ... [--max-hits N] [--seconds S]\n"); return 2; }
target_pid = atoi(argv[1]);
if (target_pid <= 0) return 2;
for (int i = 2; i < argc; i++) {
if (!strncmp(argv[i], "--max-hits", 10)) { max_hits = atoi(argv[++i]); continue; }
if (!strncmp(argv[i], "--seconds", 9)) { run_seconds = atol(argv[++i]); continue; }
struct spec s;
memset(&s, 0, sizeof s);
if (argv[i][0] == 'b' && argv[i][1] == ':') {
s.is_break = 1;
s.addr = strtoull(argv[i] + 2, NULL, 0);
if (s.addr & 3) { fprintf(stderr, "break PC must be 4-byte aligned\n"); return 2; }
} else if (argv[i][0] == 'w' && argv[i][1] == ':') {
char *p = argv[i] + 2, *q;
s.addr = strtoull(p, &q, 0);
if (*q != ':') return 2;
s.len = (int)strtol(q + 1, &q, 0);
if (*q != ':') return 2;
snprintf(s.access, sizeof s.access, "%s", q + 1);
unsigned off = s.addr & 7;
if (s.len < 1 || s.len > 8 || off + s.len > 8) {
fprintf(stderr, "watch [0x%llx+%d] crosses its 8-byte window\n",
(unsigned long long)s.addr, s.len);
return 2;
}
} else return 2;
if (nspec < 16) specs[nspec++] = s;
}
if (geteuid() != 0) {
fprintf(stderr, "FATAL not root: ptrace of an app process requires root\n");
return 3;
}
if (seize_all() != 0) return 1; /* seize_all detached its own partials */
wcap = slot_capacity(tids[0], NT_ARM_HW_WATCH);
bcap = slot_capacity(tids[0], NT_ARM_HW_BREAK);
if (wcap < 0 || bcap < 0) {
say("FATAL GETREGSET NT_ARM_HW_* errno=%d -- not arm64, or hw debug masked", errno);
release_all();
return 1;
}
say("READY pid=%d watch_slots=%d break_slots=%d", target_pid, wcap, bcap);
for (int i = 0; i < ntids; i++)
if (arm_thread(tids[i]) != 0) { release_all(); return 1; }
for (int i = 0; i < ntids; i++) pt(PTRACE_CONT, tids[i], 0, 0);
const char *reason = "exit";
long deadline = run_seconds ? time(NULL) + run_seconds : 0;
for (;;) {
if (max_hits && hits >= max_hits) { reason = "max-hits"; break; }
if (deadline && time(NULL) >= deadline) { reason = "seconds"; break; }
int st;
pid_t tid = waitpid(-1, &st, __WALL | WNOHANG);
if (tid == 0) { struct timespec ts = {0, 10 * 1000 * 1000}; nanosleep(&ts, NULL); continue; }
if (tid < 0) {
if (errno == EINTR) continue;
reason = "error"; break;
}
if (WIFEXITED(st) || WIFSIGNALED(st)) {
say("EXIT tid=%d code=%d", tid, WIFEXITED(st) ? WEXITSTATUS(st) : -1);
if (tid == target_pid) { reason = "exit"; break; }
continue;
}
if (!WIFSTOPPED(st)) continue;
int sig = WSTOPSIG(st);
unsigned event = (unsigned)st >> 16;
/* a thread we have never seen: with TRACECLONE this is a new child --
* arm it before it runs a single instruction if we can */
if (!known(tid) && event == PTRACE_EVENT_STOP && sig == SIGSTOP) {
remember(tid);
arm_thread(tid);
pt(PTRACE_CONT, tid, 0, 0);
continue;
}
if (sig == SIGTRAP) {
if (event != 0) {
/* ptrace event stop (clone/exec/...): consumed by us, and it
* is not a signal the target generated -- restart clean */
pt(PTRACE_CONT, tid, 0, 0);
continue;
}
siginfo_t si;
memset(&si, 0, sizeof si);
if (pt(PTRACE_GETSIGINFO, tid, 0, (long)&si) != 0) {
pt(PTRACE_CONT, tid, 0, 0);
continue;
}
if (si.si_code == TRAP_HWBKPT) {
struct uregs r;
memset(&r, 0, sizeof r);
get_regset(tid, NT_PRSTATUS, &r, sizeof r);
int is_break = 0;
for (int i = 0; i < nspec; i++)
if (specs[i].is_break && specs[i].addr == r.pc) is_break = 1;
print_hit(tid, is_break, &r, (uint64_t)(uintptr_t)si.si_addr);
hits++;
if (is_break) { step_over(tid, r.pc); pt(PTRACE_CONT, tid, 0, 0); continue; }
pt(PTRACE_CONT, tid, 0, 0);
continue;
}
/* signal-delivery stop that is not our hardware trap: a genuine
* target SIGTRAP (raise/tgkill/int3). Forward it, or the target's
* handler never runs and the app misbehaves in ways this tool
* would then be blamed for. */
pt(PTRACE_CONT, tid, 0, sig);
continue;
}
if ((sig == SIGSTOP || sig == SIGTSTP || sig == SIGTTIN || sig == SIGTTOU)
&& event == PTRACE_EVENT_STOP) {
pt(PTRACE_LISTEN, tid, 0, 0);
continue;
}
pt(PTRACE_CONT, tid, 0, sig); /* the target's own signal: forward */
}
/* clean detach: stop everyone, clear slots, let go */
for (int i = 0; i < ntids; i++) {
pt(PTRACE_INTERRUPT, tids[i], 0, 0);
int st2;
waitpid(tids[i], &st2, __WALL);
}
release_all();
say("DONE hits=%ld reason=%s", hits, reason);
return 0;
}
'''
HELPER_README = """# hwwatch helper (generated)
Built from `hw_watch.py emit`. Two ways to build it:
NDK (host build, static):
<ndk>/toolchains/llvm/prebuilt/<host>/bin/aarch64-linux-android24-clang \\
-O2 -static -o hwwatch hwwatch_helper.c
Termux (on-device build):
pkg install clang
clang -O2 -static -o hwwatch hwwatch_helper.c
Run as root, one spec per slot:
su -c '/data/local/tmp/hwwatch <pid> w:0x7b2000123456:8:rw b:0x7b20001234c0 --seconds 20'
Checks before trusting any hit (references/hardware-watchpoints.md):
1. READY reports watch_slots/break_slots > 0 (0 means the kernel masked hw debug).
2. Run the control: watch a global you can touch on demand, confirm a HIT arrives.
3. TracerPid will be non-zero while this runs -- that is the ptrace route's
one visible artifact, and the kernel route exists for when that matters.
This helper is a template: its encodings are host-unit-tested, its device
behaviour was not measured by this repository.
"""
# ---------------------------------------------------------------------------
# subcommands
# ---------------------------------------------------------------------------
def parse_int(text):
"""Parse 0x/0o/decimal text the way the CLI accepts addresses."""
return int(text, 0)
def cmd_plan(args):
"""plan subcommand: encode specs locally, refuse impossible ones."""
rows = []
err = None
try:
if args.addr is not None:
wvr, wcr = encode_watch(parse_int(args.addr), args.len, args.access)
rows.append(("watch", parse_int(args.addr), args.len, args.access, wvr, wcr))
if args.break_addr is not None:
bvr, bcr = encode_break(parse_int(args.break_addr))
rows.append(("break", parse_int(args.break_addr), 4, "x", bvr, bcr))
if not rows:
err = "nothing to plan: give --addr or --break"
except PlanError as exc:
err = str(exc)
if err:
if args.json:
print(json.dumps({"status": "refused", "reason": err}))
else:
print("refused: %s" % err)
return 2
if args.json:
print(json.dumps({
"status": "ok",
"specs": [{"kind": k, "addr": a, "len": n, "access": ac,
"vr": v, "cr": c,
"cr_fields": explain_cr(k, c)} for (k, a, n, ac, v, c) in rows],
}))
return 0
specs = []
for (k, a, n, ac, v, c) in rows:
print("%s addr=0x%x len=%s access=%s" % (k, a, n, ac))
print(" %-4s = 0x%x" % ("VR", v))
print(" %-4s = 0x%x %s" % ("CR", c, explain_cr(k, c)))
specs.append("w:0x%x:%d:%s" % (a, n, ac) if k == "watch" else "b:0x%x" % a)
print("\nspec for the device: %s" % " ".join(specs))
print("RESULT=ok")
return 0
def explain_cr(kind, cr):
"""Decode a WCR/BCR value into its ARM DDI 0487 field meanings."""
if kind == "watch":
bas = (cr >> WCR_BAS_SHIFT) & 0xFF
lsc = (cr >> 3) & 0b11
mode = {1: "load", 2: "store", 3: "load+store"}[lsc]
return "BAS=0x%02x LSC=%s PAC=EL0 E=1" % (bas, mode)
return "BAS=all-ones PMC=EL0 BT=unlinked-addr E=1"
def cmd_emit(args):
"""emit subcommand: write the on-device C helper and its README."""
out = os.path.abspath(args.out)
os.makedirs(out, exist_ok=True)
c_path = os.path.join(out, "hwwatch_helper.c")
with open(c_path, "w", encoding="utf-8") as fh:
fh.write(HWWATCH_C)
with open(os.path.join(out, "README.md"), "w", encoding="utf-8") as fh:
fh.write(HELPER_README)
print("wrote %s" % c_path)
print("wrote %s" % os.path.join(out, "README.md"))
print("RESULT=ok")
return 0
def find_ndk_clang():
"""Best-effort NDK clang discovery; env ANDROID_NDK_HOME wins."""
env = os.environ.get("ANDROID_NDK_HOME") or os.environ.get("ANDROID_NDK")
roots = []
if env:
roots.append(env)
for base in (os.path.expanduser("~/Android/Sdk/ndk"),
os.path.expanduser("~/Android/sdk/ndk"),
"/opt/android-sdk/ndk"):
if os.path.isdir(base):
roots.extend(os.path.join(base, n) for n in sorted(os.listdir(base), reverse=True))
for root in roots:
for pre in ("toolchains/llvm/prebuilt",):
pre_dir = os.path.join(root, pre)
if not os.path.isdir(pre_dir):
continue
host = os.listdir(pre_dir)[0]
clang = os.path.join(pre_dir, host, "bin", "aarch64-linux-android24-clang")
if os.path.isfile(clang):
return clang
clang2 = os.path.join(pre_dir, host, "bin", "clang")
if os.path.isfile(clang2):
return clang2 + " --target=aarch64-linux-android24"
return None
def cmd_gates(args):
"""gates subcommand: host probes plus the device-side gates."""
rows = []
adb = shutil.which("adb")
rows.append(("adb on PATH", "yes" if adb else "NO -- device orchestration (run) unavailable here"))
clang = find_ndk_clang()
rows.append(("NDK arm64 clang",
clang if clang else "NO -- pass --clang, or build on device with Termux clang"))
cc = shutil.which("gcc") or shutil.which("cc") or shutil.which("clang")
rows.append(("host C syntax check", cc if cc else "NO -- emitted C goes unchecked on this host"))
if args.json:
print(json.dumps({"status": "ok" if adb else "partial",
"gates": [{"gate": g, "value": v} for g, v in rows]},
ensure_ascii=False))
return 0 if adb else 0
print("== hw_watch gates (host) ==")
for g, v in rows:
print(" %-18s %s" % (g + ":", v))
print("\ndevice-side gates -- run these via scripts/devsh.py and read them back:")
print(" cat /proc/<pid>/status | grep TracerPid # must be 0: nobody else is tracing")
print(" ls /proc/<pid>/task # thread count to arm")
print(" su -c 'hwwatch <pid> w:0x..:8:rw --max-hits 1' # READY must report slots>0")
print("RESULT=%s" % ("ok" if adb else "partial"))
return 0 if adb else 0
def adb_cmd(adb, serial, *argv, **kw):
"""Run one adb command; return its CompletedProcess unchecked."""
cmd = [adb]
if serial:
cmd += ["-s", serial]
return subprocess.run(cmd + list(argv), capture_output=True, text=kw.pop("text", True), **kw)
def cmd_run(args):
"""run subcommand: build, push and run the helper; stream hits."""
adb = shutil.which("adb")
if not adb:
print("RESULT=blocked")
print("no adb on PATH: the run path needs a rooted device (see gates)", file=sys.stderr)
return 3
# 1. locate / build the helper
helper_dir = os.path.abspath(args.helper_dir or os.path.join(".", "hwwatch_work"))
src = os.path.join(helper_dir, "hwwatch_helper.c")
if not os.path.isfile(src):
os.makedirs(helper_dir, exist_ok=True)
with open(src, "w", encoding="utf-8") as fh:
fh.write(HWWATCH_C)
if args.helper:
local_bin = args.helper
else:
clang_argv = (args.clang or find_ndk_clang()).split() if (args.clang or find_ndk_clang()) else None
if args.on_device_build:
local_bin = None # built on the device below
elif not clang_argv:
print("RESULT=blocked")
print("no arm64 compiler: pass --clang, --on-device-build, or --helper",
file=sys.stderr)
return 3
else:
local_bin = os.path.join(helper_dir, "hwwatch")
built = subprocess.run(clang_argv + ["-O2", "-static", "-o", local_bin, src],
capture_output=True, text=True)
if built.returncode != 0:
print("RESULT=blocked")
print("helper build failed:\n%s" % built.stderr, file=sys.stderr)
return 3
# 2. resolve the pid
if args.pid:
pid = args.pid
elif args.pkg:
out = adb_cmd(adb, args.serial, "shell", "pidof %s" % args.pkg)
pid = (out.stdout or "").strip().split()
if not pid:
print("RESULT=blocked")
print("no running process for %s (launch it first)" % args.pkg, file=sys.stderr)
return 3
pid = pid[0]
else:
print("RESULT=blocked")
print("need --pid or --pkg", file=sys.stderr)
return 2
# 3. build the spec list and validate locally, before touching the device
try:
specs = []
if args.addr is not None:
encode_watch(parse_int(args.addr), args.len, args.access)
specs.append("w:%s:%d:%s" % (args.addr, args.len, args.access))
for b in (args.break_addr or []):
encode_break(parse_int(b))
specs.append("b:%s" % b)
if not specs:
raise PlanError("nothing to watch: give --addr or --break")
except PlanError as exc:
print("RESULT=refused")
print("refused: %s" % exc, file=sys.stderr)
return 2
remote = "/data/local/tmp/hwwatch"
def fail(step, detail):
"""Report RESULT=blocked with the step's output; return exit 3."""
print("RESULT=blocked")
print("%s failed: %s" % (step, (detail or "").strip()[:300]), file=sys.stderr)
return 3
if local_bin:
r = adb_cmd(adb, args.serial, "push", local_bin, remote)
if r.returncode != 0:
return fail("push", r.stderr or r.stdout)
else:
r = adb_cmd(adb, args.serial, "push", src, remote + ".c")
if r.returncode != 0:
return fail("push", r.stderr or r.stdout)
build = adb_cmd(adb, args.serial, "shell",
"su -c 'clang -O2 -static -o %s %s.c'" % (remote, remote))
if build.returncode != 0:
return fail("on-device build (Termux clang)", build.stdout or build.stderr)
r = adb_cmd(adb, args.serial, "shell", "chmod 755 %s" % remote)
if r.returncode != 0:
return fail("chmod", r.stderr or r.stdout)
argv_remote = [remote, str(pid)] + specs
if args.max_hits:
argv_remote += ["--max-hits", str(args.max_hits)]
if args.seconds:
argv_remote += ["--seconds", str(args.seconds)]
quoted = " ".join(argv_remote)
print("running: su -c '%s'" % quoted)
proc = subprocess.Popen(
[adb] + (["-s", args.serial] if args.serial else []) + ["shell", "su -c '%s'" % quoted],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
out_path = args.out or "hwwatch_hits.log"
hits = 0
with open(out_path, "w", encoding="utf-8") as log:
for line in proc.stdout:
log.write(line)
print(line.rstrip())
if line.startswith("HIT"):
hits += 1
rc = proc.wait()
if rc != 0:
# a denied su, a failed push/build above, or a helper FATAL exits
# nonzero: reporting `ok-no-hits` here would call a dead capture a
# quiet one -- exactly the false negative a control run exists to catch
print("RESULT=blocked")
print("capture did not complete: adb shell exited %d -- read the log above "
"for the helper's FATAL/READY lines" % rc, file=sys.stderr)
return 3
print("\nhits=%d log=%s" % (hits, out_path))
print("decode with: python hw_watch.py decode --hits %s --maps <maps.txt>" % out_path)
print("RESULT=ok" if hits else "RESULT=ok-no-hits")
return 0
HIT_KEYS = ("pc", "addr", "sp", "lr", "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7")
def parse_hits(path):
"""Parse a hit log into dicts; tolerant of interleaved ARM/READY/DONE lines."""
hits = []
with open(path, "r", encoding="utf-8", errors="replace") as fh:
for line in fh:
line = line.strip()
if not line.startswith("HIT"):
continue
row = {}
for tok in line.split():
if "=" in tok:
k, v = tok.split("=", 1)
if k in ("tid", "kind") or k in HIT_KEYS:
row[k] = v
if row.get("pc"):
hits.append(row)
return hits
def parse_maps(path):
"""Accept /proc/<pid>/maps text or lib_map.py JSON; return [(start,end,path)]."""
ranges = []
with open(path, "r", encoding="utf-8", errors="replace") as fh:
first = fh.read(4096)
fh.seek(0)
if first.lstrip().startswith("{"):
doc = json.load(fh)
rows = doc if isinstance(doc, list) else doc.get("rows", [])
for row in rows:
base = row.get("base")
path = row.get("path", "?")
if isinstance(base, int):
# one range per library, base..base+8MB heuristic is wrong;
# lib_map carries per-path base only -- flag it as such.
ranges.append((base, base + 0x1000, path + " (base only, no size in json)"))
return ranges
for line in fh:
parts = line.split(None, 5)
if len(parts) >= 5 and "-" in parts[0]:
start, _, end = parts[0].partition("-")
try:
path = parts[5].strip() if len(parts) > 5 else ""
ranges.append((int(start, 16), int(end, 16), path))
except ValueError:
continue
return ranges
def module_bases(ranges):
"""Per-path module base = that module's LOWEST mapping start.
A library maps as several ranges (r--p, r-xp, rw-p); a PC hit in the
r-xp range must still report its offset from the module base -- the
number IDA/Ghidra expects -- not from the current mapping's start, which
resets the offset at every mapping boundary."""
bases = {}
for start, _end, path in ranges:
if not path:
continue
if path not in bases or start < bases[path]:
bases[path] = start
return bases
def resolve(addr, ranges, bases=None):
"""Name a runtime address as path+offset, offset taken from the module base."""
if bases is None:
bases = module_bases(ranges)
for start, end, path in ranges:
if start <= addr < end:
base = bases.get(path, start) # anon ranges keep their own start
return "%s+0x%x" % (path or "anon", addr - base)
return None
def cmd_decode(args):
"""decode subcommand: hit log plus maps -> module+offset rows."""
hits = parse_hits(args.hits)
ranges = parse_maps(args.maps) if args.maps else []
bases = module_bases(ranges)
rows = []
unresolved = 0
for h in hits:
pc = parse_int(h["pc"])
site = resolve(pc, ranges, bases) if ranges else None
if site is None:
unresolved += 1
rows.append({"tid": h.get("tid"), "kind": h.get("kind"), "pc": h["pc"],
"site": site or "unresolved",
"watch_addr": h.get("addr")})
if args.json:
print(json.dumps({"status": "ok", "hits": len(rows),
"unresolved_pc": unresolved, "rows": rows}))
return 0
print("hits=%d unresolved_pc=%d%s" % (len(rows), unresolved,
"" if ranges else " (no --maps: PCs stay raw)"))
for r in rows:
print(" tid=%s kind=%-5s pc=%s -> %s" % (r["tid"], r["kind"], r["pc"], r["site"]))
if r["kind"] == "watch":
print(" watched addr=%s" % r["watch_addr"])
print("RESULT=ok")
return 0
def main():
"""CLI entry: subcommand dispatch."""
ap = argparse.ArgumentParser(
description="Invisible memory watchpoints / execution breakpoints on arm64 Android "
"via hardware debug registers (plan, emit, gates, run, decode).",
epilog="references/hardware-watchpoints.md owns the procedure, its stealth ledger, "
"and the control run you owe any hit before you trust it.")
sub = ap.add_subparsers(dest="cmd", required=True)
p = sub.add_parser("plan", help="encode WCR/BCR for an address and validate alignment rules")
p.add_argument("--addr", help="watchpoint data address (0x..)")
p.add_argument("--len", type=int, default=8, choices=range(1, 9),
help="watch length in bytes, 1..8 (default 8)")
p.add_argument("--access", default="rw", choices=["r", "w", "rw"],
help="trap on read / write / both (default rw)")
p.add_argument("--break", dest="break_addr", metavar="ADDR",
help="execution breakpoint PC (4-byte aligned) instead of / in addition to a watch")
p.add_argument("--json", action="store_true")
p.set_defaults(fn=cmd_plan)
p = sub.add_parser("emit", help="write the self-contained on-device C helper")
p.add_argument("--out", required=True, help="directory to write into")
p.set_defaults(fn=cmd_emit)
p = sub.add_parser("gates", help="host probe: adb, arm64 compiler; plus device-side checks")
p.add_argument("--json", action="store_true")
p.set_defaults(fn=cmd_gates)
p = sub.add_parser("run", help="build, push, run the helper on a rooted device, stream hits")
p.add_argument("--pid", help="target pid (or --pkg)")
p.add_argument("--pkg", help="resolve pid by package name")
p.add_argument("--addr", help="watchpoint data address")
p.add_argument("--len", type=int, default=8, choices=range(1, 9))
p.add_argument("--access", default="rw", choices=["r", "w", "rw"])
p.add_argument("--break", dest="break_addr", metavar="ADDR", action="append",
help="execution breakpoint PC (repeatable)")
p.add_argument("--seconds", type=int, default=30, help="capture window (default 30)")
p.add_argument("--max-hits", type=int, default=0, help="stop after N hits (0 = no cap)")
p.add_argument("--out", default="hwwatch_hits.log", help="local hit log")
p.add_argument("--serial", help="adb device serial")
p.add_argument("--clang", help="explicit arm64 compiler command (quoted)")
p.add_argument("--on-device-build", action="store_true",
help="build the helper with Termux clang on the device")
p.add_argument("--helper", help="path to an already-built helper binary")
p.add_argument("--helper-dir", help="where the generated source lives (default ./hwwatch_work)")
p.set_defaults(fn=cmd_run)
p = sub.add_parser("decode", help="module+offset for each HIT in a log")
p.add_argument("--hits", required=True, help="hit log from run/")
p.add_argument("--maps", help="/proc/<pid>/maps text or lib_map.py JSON")
p.add_argument("--json", action="store_true")
p.set_defaults(fn=cmd_decode)
args = ap.parse_args()
try:
return args.fn(args)
except BrokenPipeError:
return 0
if __name__ == "__main__":
sys.exit(main())
@@ -0,0 +1,611 @@
#!/usr/bin/env python3
"""kernel_instrument_scaffold.py -- generate a kernel-side instrumentation scaffold:
invisible watchpoints and event injection from a driver.
What "event injection" means here, and what this generates
---------------------------------------------------------
The escalation this scaffold exists for: you want to observe or alter a target
process with **no ptrace** (so `TracerPid` stays 0), **no injected thread**,
**no code modification** (so page checksums stay clean) and **no DBI cache**.
Only a kernel-side driver can do all four at once, because the hardware debug
registers can be programmed on the target's behalf from the kernel, and the
overflow callback runs with the interrupted user context's `pt_regs` in hand:
watch(addr,len,access) -> a hardware watchpoint armed on the target task
through the kernel perf/breakpoint API; the
callback logs pc + data address (dmesg)
break(pc) + set-reg -> an execution breakpoint whose callback rewrites a
register in the interrupted frame: flip a branch
decision without touching one byte of code
read/write memory -> `access_process_vm` on the target from the driver
(patch data, dump buffers), no process_vm_* syscalls
Three artifacts, one per route, each with its gate (all from
`references/kernel-and-environment-hardening.md` §4a):
kpm KernelPatch/APatch KPM -- needs a KernelPatch-patched boot image
plus a bare-metal ARM64 toolchain (aarch64-none-elf-gcc, NOT NDK)
lkm out-of-tree module -- needs kernel source matching the device's
exact vermagic, and on >= 5.7 a way to resolve unexported symbols
userspace a Magisk/KernelSU-format module skeleton -- an honest carrier for
the config and the install path; it CANNOT itself arm a watchpoint
on another process, and says so in its own README
Measured vs unverified
----------------------
`gates`, `generate` and `verify` run on the host
(docs/tool-verification/EXTENSION-kernel-instrument.md). **No kernel-side file
has been compiled or loaded anywhere, by anyone in this repository's passes.**
Every generated kernel file says so in its own header. The register-rewrite
action is the most experimental part of the design: rewriting user pt_regs in a
perf overflow callback is mechanically what a ptrace stop does, but this
repository never ran it, and on some paths the callback runs with interrupts
disabled where you must not sleep. Treat the template as a starting point that
removes the blank page, never as a weapon that has been fired.
Examples
python kernel_instrument_scaffold.py gates
python kernel_instrument_scaffold.py generate --out work/kwatch --pid 12345 \\
--watch 0x7b2000123456:8:w
python kernel_instrument_scaffold.py generate --out work/kwatch --pid 12345 \\
--break 0x7b20001234c0 --set-reg x0=0x1
python kernel_instrument_scaffold.py verify --dir work/kwatch
"""
import argparse
import json
import os
import re
import shutil
import sys
UNVERIFIED = (
"TEMPLATE, NOT COMPILED AND NOT LOADED. This file was generated by "
"skills/apk-reverse/scripts/kernel_instrument_scaffold.py and has never "
"been built or loaded on any device by this repository. Verify every "
"symbol name, signature and export against the kernel/KPM headers you "
"build against before trusting it."
)
GATES = [
{"route": "KernelPatch / APatch KPM",
"gate": "KernelPatch-patched boot image + aarch64-none-elf-gcc",
"why": "KPMs load through kpimg injected into the kernel image; building "
"one needs a bare-metal ARM64 compiler, not the NDK.",
"check": "ls /data/adb/ | grep -i apatch ; which aarch64-none-elf-gcc"},
{"route": "Out-of-tree LKM",
"gate": "kernel source matching exact vermagic",
"why": "register_hw_breakpoint/perf_event_create_kernel_counter are "
"GPL-exported, but find_task_by_vpid and friends are not exported "
"on every kernel, and a vermagic mismatch is refused at load time.",
"check": "uname -r ; ls /proc/sys/kernel/ ; find matching kernel source"},
{"route": "userspace carrier module",
"gate": "any root (Magisk/KernelSU)",
"why": "carries config and documents the route; cannot itself program "
"another process's debug registers.",
"check": "ls /data/adb/modules"},
{"route": "perf hw-breakpoint slots",
"gate": "the kernel reserves debug slots for perf (CONFIG_HW_PERF_EVENTS)",
"why": "the kernel's perf subsystem owns the arm64 debug registers; if "
"perf's breakpoint layer is not compiled in, register_hw_breakpoint "
"does not exist and this whole route closes.",
"check": "grep -c hw_breakpoint /proc/kallsyms (root) ; ls /sys/bus/event_source/devices/breakpoint"},
]
def parse_spec_watch(tok):
"""w:0xADDR:LEN:ACCESS -> dict; validated with hw_watch-style rules."""
parts = tok.split(":")
if len(parts) != 4 or parts[0] != "w":
raise ValueError("watch spec must be w:0xADDR:LEN:ACCESS, got %r" % tok)
addr = int(parts[1], 0)
length = int(parts[2], 0)
access = parts[3]
if length not in range(1, 9):
raise ValueError("watch length must be 1..8")
if access not in ("r", "w", "rw"):
raise ValueError("watch access must be r/w/rw")
if (addr & 7) + length > 8:
raise ValueError("watch 0x%x+%d crosses its aligned 8-byte window"
% (addr, length))
return {"addr": "0x%x" % addr, "len": length, "access": access}
def parse_spec_break(tok, set_reg=None):
"""b:0xADDR[:reg=value] -> dict."""
parts = tok.split(":")
if not parts or parts[0] != "b" or len(parts) < 2:
raise ValueError("break spec must be b:0xADDR, got %r" % tok)
addr = int(parts[1], 0)
if addr & 3:
raise ValueError("break PC 0x%x is not 4-byte aligned" % addr)
row = {"pc": "0x%x" % addr}
if set_reg:
m = re.match(r"^(x\d+)=(0x[0-9a-fA-F]+|\d+)$", set_reg)
if not m or int(m.group(1)[1:]) > 30:
# x31 is sp's slot in user_pt_regs and the array ends at x30: an
# index past 30 writes sp, pc, pstate, or off the frame. sp/pc are
# refused too: the template has no rewrite path for them, and a
# generate that "succeeds" while silently dropping the action is
# the misleading kind of success
raise ValueError("--set-reg must be x0..x30=<value> (sp/pc have no "
"rewrite path in the template), got %r" % set_reg)
row["set_reg"] = m.group(1)
row["set_value"] = m.group(2)
return row
# ---------------------------------------------------------------------------
# templates
# ---------------------------------------------------------------------------
KPM_C = r'''// SPDX-License-Identifier: GPL-2.0
/*
* kwatch.c -- invisible hardware watchpoints + register rewrite at trap,
* as a KernelPatch / APatch KPM.
*
* {unverified}
*
* Design notes that are load-bearing:
* - The arm64 debug registers are owned by the kernel perf/breakpoint
* layer. A KPM does not poke DBGWVR/DBGWCR directly (it cannot, from
* kernel C code safely); it asks perf to create a breakpoint event on the
* target task, and perf programs the slots at context switch.
* - The overflow callback receives the interrupted context's pt_regs. That
* is where the "event injection" half lives: rewriting regs->regs[n]
* changes what the userspace instruction stream sees after the trap.
* This is mechanically what a ptrace stop does -- but it was never run
* here, and on some interrupt contexts you must not sleep or allocate.
* LOGGING IS SAFE; REGISTER REWRITING IS EXPERIMENTAL.
* - TracerPid stays 0: no ptrace relationship is ever created.
*/
#include <linux/types.h>
#include <linux/printk.h>
#include <linux/perf_event.h>
#include <linux/hw_breakpoint.h>
#include <kpmodule.h>
KPM_NAME("{id}");
KPM_VERSION("{version}");
KPM_LICENSE("GPL");
KPM_AUTHOR("apk-reverse");
KPM_DESCRIPTION("config-driven invisible watchpoints on one task");
/* kfunc declarations must NOT carry `extern` (see kernelsu_syscall_mask.py's
* KPM notes): tentative definitions allocate .bss slots the loader fills. */
int kfunc_def(find_task_by_vpid)(int vnr);
int kfunc_def(perf_event_create_kernel_counter)(
struct perf_event_attr *attr, int cpu, void *task,
void *overflow_handler, void *context);
int kfunc_def(perf_event_release_kernel)(void *event);
static struct perf_event *events[{max_events}];
static int nevents = 0;
struct wp_cfg {{
u64 addr; int len; int bp_type; const char *label;
}};
/* PERF_TYPE_BREAKPOINT = 5; bp_type: 1=read 2=write 4=execute (uapi) */
static struct wp_cfg cfg[] = {{
{cfg_rows}
}};
static void wp_hit(struct perf_event *event, void *data, struct pt_regs *regs)
{{
/* Runs on every hit, in interrupt context on the hitting CPU, for EVERY
* event registered below -- watch and break alike. The generated body is
* an UNFILTERED printk: add a cheap first-statement filter (event/addr)
* before treating any hit as signal on a hot path. */
printk(KERN_INFO "KWATCH hit pc=0x%llx x0=0x%llx x1=0x%llx\n",
(unsigned long long)regs->pc,
(unsigned long long)regs->regs[0],
(unsigned long long)regs->regs[1]);
{inject_rows}
}}
static int kwatch_init(void) {{
struct task_struct *task = (void *)(unsigned long)kfunc_find_task_by_vpid({pid});
if (!task) {{
printk(KERN_ERR "KWATCH: no task pid={pid}\n");
return -1;
}}
for (int i = 0; i < sizeof(cfg)/sizeof(cfg[0]); i++) {{
struct perf_event_attr attr;
memset(&attr, 0, sizeof(attr));
attr.type = 5 /* PERF_TYPE_BREAKPOINT */;
attr.sample_period = 1; /* hw_breakpoint_init's firing line: a period
* of 0 is a non-sampling event and wp_hit
* never runs -- armed but silent */
attr.bp_type = cfg[i].bp_type;
attr.bp_addr = cfg[i].addr;
attr.bp_len = cfg[i].len;
attr.size = sizeof(attr);
attr.disabled = 0;
attr.inherit = 0;
events[nevents] = (void *)(unsigned long)
kfunc_perf_event_create_kernel_counter(
&attr, -1 /* any cpu */, task, wp_hit, NULL);
if (IS_ERR_OR_NULL(events[nevents])) {{
printk(KERN_ERR "KWATCH: slot %d failed (no slots left? perf bp layer off?)\n", i);
events[nevents] = NULL;
}} else {{
nevents++;
}}
}}
printk(KERN_INFO "KWATCH armed %d events on pid={pid}\n", nevents);
return 0;
}}
static void kwatch_exit(void) {{
for (int i = 0; i < nevents; i++)
if (events[i])
kfunc_perf_event_release_kernel(events[i]);
printk(KERN_INFO "KWATCH released %d events\n", nevents);
}}
KPM_INIT(kwatch_init);
KPM_EXIT(kwatch_exit);
'''
LKM_C = r'''// SPDX-License-Identifier: GPL-2.0
/*
* kwatch.c -- invisible hardware watchpoints on one task, as an out-of-tree LKM.
*
* {unverified}
*
* Gates that decide whether this file is even buildable: kernel source
* matching the device's exact vermagic; and on >= 5.7 the unexported-symbol
* problem (find_task_by_vpid is not exported on every tree -- resolve it or
* take the pid from a syscall parameter instead).
* Build: make -C <kernel-src> M=$PWD modules
*/
#include <linux/module.h>
#include <linux/perf_event.h>
#include <linux/hw_breakpoint.h>
#include <linux/sched.h>
#include <linux/pid.h>
#include <linux/printk.h>
struct wp_cfg {{
u64 addr; int len; int bp_type;
}};
static struct wp_cfg cfg[] = {{
{cfg_rows}
}};
static struct perf_event *events[{max_events}];
static int nevents = 0;
static void wp_hit(struct perf_event *event, struct perf_sample_data *data,
struct pt_regs *regs)
{{
printk(KERN_INFO "KWATCH hit pc=0x%llx x0=0x%llx x1=0x%llx\n",
(unsigned long long)regs->pc,
(unsigned long long)regs->regs[0],
(unsigned long long)regs->regs[1]);
{inject_rows}
}}
static int __init kwatch_init(void)
{{
struct task_struct *task;
rcu_read_lock();
task = pid_task(find_vpid({pid}), PIDTYPE_PID);
if (task)
/* pin the task past the RCU section: registration below can sleep,
* and an unpinned pointer can be freed after a task exit in between */
get_task_struct(task);
rcu_read_unlock();
if (!task) {{
pr_err("KWATCH: no task pid={pid}\n");
return -ESRCH;
}}
for (int i = 0; i < ARRAY_SIZE(cfg); i++) {{
struct perf_event_attr attr;
/* sets size/type/pinned/sample_period=1: a zero period is a
* non-sampling event whose overflow callback (wp_hit) never runs */
hw_breakpoint_init(&attr);
attr.bp_type = cfg[i].bp_type;
attr.bp_addr = cfg[i].addr;
attr.bp_len = cfg[i].len;
/* the user-task API is (attr, handler, context, task): the per-cpu
* `register_wide_hw_breakpoint` family cannot arm on one target */
events[nevents] = register_user_hw_breakpoint(
&attr, wp_hit, NULL, task);
if (IS_ERR(events[nevents])) {{
pr_err("KWATCH: slot %d failed\n", i);
events[nevents] = NULL;
}} else {{
nevents++;
}}
}}
pr_info("KWATCH armed %d events on pid={pid}\n", nevents);
put_task_struct(task);
return 0;
}}
module_init(kwatch_init);
static void __exit kwatch_exit(void)
{{
int i;
for (i = 0; i < nevents; i++)
if (events[i])
unregister_hw_breakpoint(events[i]);
pr_info("KWATCH released %d events\n", nevents);
}}
module_exit(kwatch_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("config-driven invisible watchpoints on one task");
'''
BP_TYPE = {"r": "1 /* read */", "w": "2 /* write */", "rw": "3 /* rw */", "x": "4 /* execute */"}
INJECT_LOG = " /* action: log only (safe in any context) */"
INJECT_SET = (" /* action: set-reg -- EXPERIMENTAL, never run by this repository.\n"
" * Rewriting the interrupted frame's register is what a ptrace\n"
" * stop does mechanically; doing it from a perf overflow callback\n"
" * was not tested anywhere here. The rewrite is guarded by its\n"
" * breakpoint's own event so a watchpoint hit in this same callback\n"
" * cannot run it. */\n"
" if (event && (event->attr.bp_type & 4 /* HW_BREAKPOINT_X */)\n"
" && event->attr.bp_addr == {addr}) {{\n"
" regs->regs[{idx}] = {value};\n"
" }}")
def render_cfg_rows(watches, breaks, for_kpm):
"""Spec dicts -> wp_cfg C rows for the kernel template."""
rows = []
for w in watches:
bp = BP_TYPE[w["access"]]
rows.append(' { .addr = %s, .len = %d, .bp_type = %s }, /* watch %s:%s */'
% (w["addr"], w["len"], bp, w["addr"], w["access"]))
for b in breaks:
# wp_cfg declares addr/len/bp_type and has no `pc` member; an execution
# breakpoint is .addr with bp_type HW_BREAKPOINT_X (4) and the machine
# word (8 on arm64) as its length
rows.append(' { .addr = %s, .len = 8, .bp_type = 4 /* HW_BREAKPOINT_X */ },'
' /* execute breakpoint */'
% b["pc"])
return "\n".join(rows)
def render_inject_rows(breaks):
"""Break actions -> guarded callback body rows."""
parts = [INJECT_LOG]
for b in breaks:
if b.get("set_reg"):
reg = b["set_reg"]
idx = reg[1:] if reg.startswith("x") else None
if idx is None:
parts.append(" /* set-reg on %s not expressible in pt_regs by index; "
"extend the template */" % reg)
continue
parts.append(INJECT_SET.format(addr=b["pc"], idx=idx, value=b["set_value"]))
return "\n".join(parts)
KPM_MAKEFILE = '''# {unverified}
all: {id}.kpm
{id}.kpm: src/{id}.c
\taarch64-none-elf-gcc -O2 -fno-stack-protector -fPIC -c src/{id}.c -o {id}.o
\t# link per the KernelPatch module docs of your revision into a relocatable ELF
clean:
\trm -f *.o *.kpm
'''
LKM_MAKEFILE = '''# {unverified}
obj-m += {id}.o
all:
\tmake -C $KSRC M=$$PWD modules
'''
MODULE_PROP = """id={id}
name={name}
version={version}
versionCode=1
author=apk-reverse
description={description}
"""
USERSPACE_README = """# {name} (userspace carrier)
This module exists to carry `config/instrument.json` and the build/load notes
for the kernel-side artifact. **It cannot arm a watchpoint on another process**
and it does not try: nothing in module.prop/post-fs-data.sh/service.sh ever
runs in the kernel's path. The kernel half (kpm/ or lkm/) is the part that
programs the target's debug registers; it is a template that has never been
compiled or loaded by this repository.
Layout:
config/instrument.json the watch/break spec + pid (edit, then regenerate
the kernel file rather than hand-editing it)
kpm/, lkm/ generated kernel-side templates + Makefiles
post-fs-data.sh prints the config path into the log, nothing else
Discipline (references/kernel-event-injection.md): a template is `unverified`
until a device with the matching gate compiles and loads it; TracerPid stays 0
only because no ptrace relationship exists -- verify that claim on the device
before relying on it.
"""
def cmd_gates(args):
"""gates subcommand: host probes plus the device-side gates."""
clang = shutil.which("aarch64-none-elf-gcc")
if args.json:
print(json.dumps({"status": "ok",
"gates": GATES,
"aarch64_none_elf_gcc": bool(clang)}, indent=1))
return 0
print("== kernel-side instrumentation routes and their gates ==\n")
for g in GATES:
print(" %-32s %s" % (g["route"] + ":", g["gate"]))
print(" why: %s" % g["why"])
print(" check: %s\n" % g["check"])
print("== host probe ==")
print(" aarch64-none-elf-gcc: %s" % ("FOUND" if clang else "NOT FOUND -- KPM route cannot be built from this host"))
print("\nRESULT=ok")
return 0
def cmd_generate(args):
"""generate subcommand: write the scaffold under --out."""
if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._-]*", args.id or ""):
# --id becomes file names, Makefile targets and module.prop entries;
# a path separator or leading dot would write outside --out
print("RESULT=refused")
print("--id must be a module id ([A-Za-z0-9][A-Za-z0-9._-]*): no path "
"separators, no leading dot, got %r" % args.id, file=sys.stderr)
return 2
watches = [parse_spec_watch(w) for w in (args.watch or [])]
breaks = [parse_spec_break(b, args.set_reg) for b in (args.break_ or [])]
if not watches and not breaks:
print("RESULT=refused")
print("nothing to instrument: give --watch and/or --break", file=sys.stderr)
return 2
if not args.pid:
print("RESULT=refused")
print("the kernel side needs a target --pid", file=sys.stderr)
return 2
out = os.path.abspath(args.out)
os.makedirs(out, exist_ok=True)
targets = [t.strip() for t in args.targets.split(",") if t.strip()]
cfg_rows = render_cfg_rows(watches, breaks, for_kpm=True)
inject_rows = render_inject_rows(breaks)
n_events = max(1, len(watches) + len(breaks))
written = []
def put(rel, body):
"""Write one generated file under the root; record it."""
path = os.path.join(out, rel)
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as fh:
fh.write(body)
written.append(rel)
if "kpm" in targets:
put("kpm/src/%s.c" % args.id, KPM_C.format(
id=args.id, version=args.version, unverified=UNVERIFIED,
cfg_rows=cfg_rows, inject_rows=inject_rows, pid=args.pid,
max_events=n_events))
put("kpm/Makefile", KPM_MAKEFILE.format(id=args.id, unverified=UNVERIFIED))
if "lkm" in targets:
put("lkm/%s.c" % args.id, LKM_C.format(
unverified=UNVERIFIED, cfg_rows=cfg_rows,
inject_rows=inject_rows, pid=args.pid, max_events=n_events))
put("lkm/Makefile", LKM_MAKEFILE.format(id=args.id, unverified=UNVERIFIED))
if "userspace" in targets:
put("module.prop", MODULE_PROP.format(
id=args.id, name=args.name, version=args.version,
description=args.description))
put("README.md", USERSPACE_README.format(name=args.name))
put("post-fs-data.sh", "#!/system/bin/sh\n"
"# carrier only -- see README.md; cannot arm watchpoints from here\n"
"MODDIR=${0%/*}\n"
"echo \"$(date) config at $MODDIR/config/instrument.json\" "
">> /data/adb/" + args.id + ".log\n")
put("config/instrument.json", json.dumps({
"pid": args.pid,
"watches": watches,
"breaks": breaks,
"note": "regenerate the kernel-side file rather than hand-editing it",
}, indent=1))
print("wrote %s" % out)
for rel in written:
print(" %s" % rel)
print("RESULT=ok")
return 0
def cmd_verify(args):
"""verify subcommand: consistency checks over a generated dir."""
problems = []
root = os.path.abspath(args.dir)
expected = ["config/instrument.json"]
for rel in expected:
if not os.path.isfile(os.path.join(root, rel)):
problems.append("missing %s" % rel)
kernel_files = []
for sub in ("kpm", "lkm"):
d = os.path.join(root, sub)
if not os.path.isdir(d):
continue
for base, _dirs, files in os.walk(d):
for fn in files:
if fn.endswith(".c") or fn == "Makefile":
kernel_files.append(os.path.relpath(os.path.join(base, fn), root))
for rel in kernel_files:
with open(os.path.join(root, rel), "r", encoding="utf-8") as fh:
body = fh.read()
if "NOT COMPILED AND NOT LOADED" not in body:
problems.append("%s lost its unverified banner" % rel)
cfg_path = os.path.join(root, "config", "instrument.json")
if os.path.isfile(cfg_path):
try:
with open(cfg_path, "r", encoding="utf-8") as fh:
cfg = json.load(fh)
if not cfg.get("pid"):
problems.append("config carries no pid")
except ValueError as exc:
problems.append("config/instrument.json is not JSON: %s" % exc)
if args.json:
print(json.dumps({"status": "ok" if not problems else "problems",
"problems": problems, "kernel_files": kernel_files}))
else:
for p in problems:
print(" problem: %s" % p)
print("verified %d kernel-side file(s), %d problem(s)"
% (len(kernel_files), len(problems)))
print("RESULT=%s" % ("ok" if not problems else "problems"))
return 0 if not problems else 1
def main():
"""CLI entry: subcommand dispatch."""
ap = argparse.ArgumentParser(
description="Generate the kernel-side invisible-instrumentation scaffold: "
"watchpoint/breakpoint templates (KPM/LKM) + userspace carrier, "
"each with its gate and an explicit unverified label.",
epilog="references/kernel-event-injection.md owns the primitive map and the "
"composition recipes; kernel-and-environment-hardening.md owns the "
"hiding-vs-instrumentation split and the stop rules.")
sub = ap.add_subparsers(dest="cmd", required=True)
p = sub.add_parser("gates", help="print the route/gate table and probe this host")
p.add_argument("--json", action="store_true")
p.set_defaults(fn=cmd_gates)
p = sub.add_parser("generate", help="write the scaffold")
p.add_argument("--out", required=True, help="directory to write into")
p.add_argument("--pid", type=int, help="target pid (kernel side needs it)")
p.add_argument("--watch", action="append", metavar="w:0xADDR:LEN:ACCESS",
help="hardware watchpoint spec (repeatable)")
p.add_argument("--break", dest="break_", action="append", metavar="b:0xADDR",
help="execution breakpoint PC (repeatable)")
p.add_argument("--set-reg", metavar="REG=VALUE",
help="rewrite this register at the breakpoint trap (e.g. x0=0x1) -- "
"the injection action; experimental, see the script header")
p.add_argument("--id", default="kwatch", help="module id / artifact name")
p.add_argument("--name", default="Kernel Watch", help="human-readable module name")
p.add_argument("--version", default="0.1.0")
p.add_argument("--description", default="config-driven invisible watchpoints + "
"event injection templates")
p.add_argument("--targets", default="kpm,lkm,userspace",
help="comma list from kpm,lkm,userspace")
p.set_defaults(fn=cmd_generate)
p = sub.add_parser("verify", help="read-only consistency checks over a generated dir")
p.add_argument("--dir", required=True)
p.add_argument("--json", action="store_true")
p.set_defaults(fn=cmd_verify)
args = ap.parse_args()
try:
return args.fn(args)
except ValueError as exc:
print("RESULT=refused")
print(str(exc), file=sys.stderr)
return 2
if __name__ == "__main__":
sys.exit(main())
@@ -0,0 +1,407 @@
#!/usr/bin/env python3
"""trace_addr2sym.py -- turn a stream of runtime addresses into symbols and static offsets.
What question this answers
--------------------------
Every hardware-layer tool in this skill ends with a list of addresses: `hw_watch.py` hits,
ETE/SPE block streams, `trace_diff.py` divergences, BRBE branch records. An address is not a
finding until it names **which library, which function, which static offset** -- the last part
being what you paste into IDA/Ghidra, where ASLR never existed. This script closes that loop
with a zero-dependency ELF64 reader (no pyelftools, no readelf): PT_LOAD segments for
runtime-address -> vaddr -> file-offset conversion, `.dynsym` first then `.symtab`, and a
stripped-library fallback that still gives section + static offsets.
PIE and the two offset spaces, stated once
------------------------------------------
- **runtime address** = what the trace/hit carries (ASLR-affected)
- **vaddr** = runtime - load_bias, where load_bias is derived from the module's
lowest mapping aligned against its PT_LOAD (bias = map_start - (map_foff -
p_offset) - p_vaddr): it equals the mapping base exactly when the lowest
PT_LOAD has p_vaddr 0, the normal PIE case (`lib_map.py`/maps give you this)
- **file offset** = what the disassembler wants; vaddr -> file goes through PT_LOAD again,
and it is **not** always the identity: p_vaddr != p_offset happens in hand-linked and
self-decrypting payloads, and silently wrong offsets are exactly the failure this tool
exists to prevent.
- `mod+0xoff` keys (stalker logs, trace_diff output) are runtime - base == vaddr under the
same assumption; use `--trace --mod <name> --elf <path>` for that input shape.
Measured vs unverified: the ELF walker is host-tested against a real cross-compiled
aarch64 shared library (gcc, `.dynsym` + `.symtab`) and against a hand-built minimal ELF
(tests/unit/test_trace_addr2sym.py) -- `observed`. Nothing here touches a device.
Examples
python trace_addr2sym.py --hits hwwatch_hits.log --maps maps.txt --elf liba.so
python trace_addr2sym.py --trace traceA.log --mod liba.so --elf liba.so
python trace_addr2sym.py --elf liba.so --addrs 0x7b20001234c0 --base 0x7b2000000000
python trace_addr2sym.py --elf liba.so --offs 0x1234c0 # module offsets directly
"""
import argparse
import bisect
import json
import os
import struct
import sys
PT_LOAD = 1
SHT_SYMTAB = 2
SHT_DYNSYM = 11
STT_FUNC = 2
STT_OBJECT = 1
class ElfError(ValueError):
pass
class Elf64:
"""Zero-dependency ELF64 little-endian reader: segments, sections, symbols."""
def __init__(self, path):
"""Read the file; build segments, sections and the symbol table."""
with open(path, "rb") as fh:
self.data = fh.read()
if self.data[:4] != b"\x7fELF":
raise ElfError("%s is not an ELF" % path)
if self.data[4] != 2 or self.data[5] != 1:
raise ElfError("%s is not ELF64 little-endian" % path)
(self.e_type, self.e_machine) = struct.unpack_from("<HH", self.data, 16)
(e_phoff,) = struct.unpack_from("<Q", self.data, 0x20)
(e_shoff,) = struct.unpack_from("<Q", self.data, 0x28)
(e_phentsize, e_phnum) = struct.unpack_from("<HH", self.data, 0x36)
(e_shentsize, e_shnum, e_shstrndx) = struct.unpack_from("<HHH", self.data, 0x3A)
self.segments = []
# per-segment loaded spans (memsz included: .bss objects live past
# filesz). The gate must test each segment: real shared objects leave
# unmapped gaps between PT_LOADs, and a whole-span check would admit a
# PC sitting in one of those gaps
self.mem_ranges = []
for i in range(e_phnum):
off = e_phoff + i * e_phentsize
p_type, _flags, p_offset, p_vaddr = struct.unpack_from("<IIQQ", self.data, off)
p_filesz, p_memsz = struct.unpack_from("<QQ", self.data, off + 0x20)
if p_type == PT_LOAD:
self.segments.append((p_vaddr, p_vaddr + p_filesz, p_offset))
self.mem_ranges.append((p_vaddr, p_vaddr + p_memsz))
self.sections = []
if e_shoff:
shstr_off = self._sh_off(e_shoff, e_shentsize, e_shstrndx, self.data)
for i in range(e_shnum):
off = e_shoff + i * e_shentsize
(sh_name, sh_type) = struct.unpack_from("<II", self.data, off)
(sh_offset, sh_size) = struct.unpack_from("<QQ", self.data, off + 0x18)
(sh_link, _sh_info, _sh_align, sh_entsize) = struct.unpack_from(
"<IIQQ", self.data, off + 0x28)
name = self._cstr(shstr_off + sh_name) if shstr_off else ""
self.sections.append({
"name": name, "type": sh_type, "offset": sh_offset,
"size": sh_size, "link": sh_link, "entsize": sh_entsize,
})
self.symbols = self._read_symbols()
# function/object symbols with a value, best-candidate-first ordering later
self.funcs = sorted(
[(s["value"], s["size"], s["name"]) for s in self.symbols
if s["value"] and s["type"] in (STT_FUNC, STT_OBJECT)],
key=lambda t: t[0])
# cached for resolve(): trace rows can name thousands of PCs and a
# per-call list build is O(symbols) each time
self.symbol_addresses = [f[0] for f in self.funcs]
@staticmethod
def _sh_off(e_shoff, e_shentsize, idx, data):
"""File offset of the shstrtab section header, or 0."""
if idx == 0:
return 0
(sh_offset,) = struct.unpack_from("<Q", data, e_shoff + idx * e_shentsize + 0x18)
return sh_offset
def _cstr(self, off):
"""NUL-terminated string at a file offset."""
end = self.data.find(b"\x00", off)
return self.data[off:end].decode("utf-8", "replace") if end > off else ""
def _read_symbols(self):
"""Flatten .symtab/.dynsym into symbol rows."""
out = []
for sec in self.sections:
if sec["type"] not in (SHT_SYMTAB, SHT_DYNSYM) or not sec["entsize"]:
continue
strtab = None
if 0 < sec["link"] < len(self.sections):
strtab = self.sections[sec["link"]]["offset"]
count = sec["size"] // sec["entsize"] if sec["entsize"] else 0
for i in range(count):
off = sec["offset"] + i * sec["entsize"]
(st_name, st_info) = struct.unpack_from("<IB", self.data, off)
(st_value, st_size) = struct.unpack_from("<QQ", self.data, off + 8)
name = self._cstr(strtab + st_name) if strtab is not None else ""
if name:
out.append({"name": name, "value": st_value, "size": st_size,
"type": st_info & 0xF})
return out
# -- conversions ---------------------------------------------------------
def contains_vaddr(self, vaddr):
"""True when vaddr lies inside the loaded span of some PT_LOAD."""
return any(lo <= vaddr < hi for lo, hi in self.mem_ranges)
def vaddr_to_file(self, vaddr):
"""vaddr -> file offset through a PT_LOAD, or None."""
for p_vaddr, end, p_offset in self.segments:
if p_vaddr <= vaddr < end:
return p_offset + (vaddr - p_vaddr)
return None
def file_to_vaddr(self, foff):
"""file offset -> vaddr through a PT_LOAD, or None."""
for p_vaddr, end, p_offset in self.segments:
if p_offset <= foff < p_offset + (end - p_vaddr):
return p_vaddr + (foff - p_offset)
return None
def section_of(self, vaddr):
"""(section name, offset inside it) for a vaddr."""
for s in self.sections:
# section addr is not stored in this minimal reader; fall back by file offset
foff = self.vaddr_to_file(vaddr)
if foff is not None and s["offset"] <= foff < s["offset"] + s["size"] and s["name"]:
return s["name"], foff - s["offset"]
return None, None
def resolve(self, vaddr):
"""-> (symbol, sym_off) or (None, None). Nearest symbol at-or-below
vaddr, addr2line-style: a PC between two sized symbols reports the
lower one, because a trace PC in a gap (alignment padding, cold tails,
stripped locals) still owes you its neighbourhood. Callers that must
not attribute foreign PCs should gate on contains_vaddr() first."""
if not self.funcs:
return None, None
i = bisect.bisect_right(self.symbol_addresses, vaddr) - 1
if i < 0:
return None, None
value, _size, name = self.funcs[i]
return name, vaddr - value
def load_bias_from_maps(maps_path, module_hint, elf):
"""Load bias for the module matched by full pathname or basename ONLY.
A substring match would also admit `liba.so`'s neighbours
(`liba.so.v2`, `xliba.so`) and take their mapping. And the bias is not
simply the lowest mapping start -- that holds only when the lowest
PT_LOAD has p_vaddr 0. Align the mapping's file offset with a PT_LOAD
segment instead: bias = map_start - (map_foff - p_offset) - p_vaddr.
"""
best = None # (lowest mapping start, its file offset)
with open(maps_path, "r", encoding="utf-8", errors="replace") as fh:
for line in fh:
parts = line.split(None, 5)
if len(parts) >= 6:
path = parts[5].strip()
if module_hint == path or module_hint == os.path.basename(path):
start = int(parts[0].split("-")[0], 16)
foff = int(parts[2], 16)
if best is None or start < best[0]:
best = (start, foff)
if best is None:
return None
start, foff = best
for p_vaddr, end, p_offset in elf.segments:
if p_offset <= foff < p_offset + (end - p_vaddr):
return start - (foff - p_offset) - p_vaddr
return None # maps and ELF disagree: the file offset is not one of its segments
def read_hw_hits(path):
"""hw_watch.py HIT log -> ('hits', pc) rows."""
rows = []
with open(path, "r", encoding="utf-8", errors="replace") as fh:
for line in fh:
if line.startswith("HIT"):
for tok in line.split():
if tok.startswith("pc="):
rows.append(("hits", tok[3:]))
return rows
def read_addr_lines(path):
"""One address per line -> ('addrs', tok) rows."""
rows = []
with open(path, "r", encoding="utf-8", errors="replace") as fh:
for line in fh:
toks = line.split()
if not toks:
continue
tok = None
for t in reversed(toks):
s = t[2:] if t.lower().startswith("0x") else t
try:
int(s, 16)
tok = t
break
except ValueError:
continue
if tok:
rows.append(("addrs", tok))
return rows
def read_off_lines(path):
"""One module offset per line -> ('offs', tok) rows."""
rows = []
with open(path, "r", encoding="utf-8", errors="replace") as fh:
for line in fh:
for tok in line.split():
s = tok[2:] if tok.lower().startswith("0x") else tok
try:
int(s, 16)
except ValueError:
continue
rows.append(("offs", tok))
break
return rows
def read_modoff_trace(path, mod):
"""mod+0xoff keys from a stalker/trace log -- only keys for `mod`."""
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import trace_diff
rows = []
try:
blocks = trace_diff.read_blocks(path)
except Exception as exc: # noqa: BLE001 -- surfaced as a usage error below
raise ElfError(str(exc))
for key in blocks:
if "+" in key:
name, _, off = key.rpartition("+")
if name == mod or os.path.basename(name) == mod:
rows.append(("offs", off))
return rows
def describe(elf, vaddr):
"""Name one vaddr, refusing PCs that are outside this ELF's span: pinning
another module's PC on this ELF's last symbol is the misleading output,
not a helpful guess."""
if not elf.contains_vaddr(vaddr):
return ("<outside-elf> vaddr 0x%x (outside every PT_LOAD span of this ELF "
"-- another module's PC, or a wrong base?)" % vaddr)
sym, sym_off = elf.resolve(vaddr)
foff = elf.vaddr_to_file(vaddr)
sec, sec_off = elf.section_of(vaddr)
if sym:
return "%s+0x%x [vaddr 0x%x file 0x%s%s]" % (
sym, sym_off, vaddr,
("%x" % foff) if foff is not None else "?",
(" %s+0x%x" % (sec, sec_off)) if sec else "")
if sec:
return "<stripped> %s+0x%x [vaddr 0x%x file 0x%s]" % (
sec, sec_off, vaddr, ("%x" % foff) if foff is not None else "?")
return "<no mapping> vaddr 0x%x (outside every PT_LOAD -- wrong base?)" % vaddr
def cmd_main(args):
"""Resolve input rows against the ELF; text or --json output."""
rows = []
if args.hits:
rows = read_hw_hits(args.hits)
elif args.trace:
rows = read_modoff_trace(args.trace, args.mod)
elif args.addrs:
rows = [("addrs", a) for a in args.addrs]
elif args.offs:
rows = [("offs", a) for a in args.offs]
elif args.addr_file:
rows = read_addr_lines(args.addr_file)
elif args.off_file:
rows = read_off_lines(args.off_file)
if not rows:
print("RESULT=refused")
print("no addresses: give --hits/--trace/--addrs/--offs/--addr-file/--off-file",
file=sys.stderr)
return 2
try:
elf = Elf64(args.elf)
except (OSError, ElfError) as exc:
print("RESULT=refused")
print(str(exc), file=sys.stderr)
return 2
base = args.base
if base is not None:
base = int(base, 0)
if base is None and args.maps:
hint = args.mod or os.path.basename(args.elf)
base = load_bias_from_maps(args.maps, hint, elf)
if base is None:
print("RESULT=refused")
print("no load bias for %r in %s (no exact mapping matched, or its file "
"offset does not align with %s's PT_LOADs)"
% (hint, args.maps, os.path.basename(args.elf)), file=sys.stderr)
return 2
if args.trace and base is None:
base = 0 # mod+off keys already are module-relative: treat them as vaddr
out = []
for kind, tok in rows:
val = int(tok, 16) if not tok.lower().startswith("0x") else int(tok, 16)
if kind in ("offs",):
vaddr = val if not args.offs_are_file else elf.file_to_vaddr(val)
if vaddr is None:
out.append({"input": tok, "error": "file offset outside PT_LOAD"})
continue
else:
if base is None:
print("RESULT=refused")
print("absolute addresses need --base or --maps (ASLR: the trace address "
"means nothing without the module's load base)", file=sys.stderr)
return 2
vaddr = val - base
out.append({"input": tok, "vaddr": vaddr, "where": describe(elf, vaddr)})
if args.json:
# a pure JSON document on stdout -- the status field carries what
# RESULT= would say, and appending the marker would break every
# json.loads(stdout) consumer (the convention check_commands.py set)
print(json.dumps({"status": "ok", "elf": args.elf,
"base": base, "resolved": out}, indent=1))
else:
print("elf=%s base=%s%s" % (args.elf, hex(base) if base is not None else "?",
" (from --offs: offsets treated as %s)"
% ("file offsets" if args.offs_are_file else "vaddrs")
if args.offs else ""))
for row in out:
if "error" in row:
print(" %-16s ERROR %s" % (row["input"], row["error"]))
else:
print(" %-18s -> %s" % (row["input"], row["where"]))
print("RESULT=ok")
return 0
def main():
"""CLI entry: subcommand dispatch."""
ap = argparse.ArgumentParser(
description="Runtime addresses -> symbol + static offset, zero-dependency ELF64: "
"PT_LOAD conversion, .dynsym/.symtab, stripped fallback.",
epilog="references/hardware-sampling-and-crash.md owns the crash/address loop this "
"closes; the offset-space rules are in this header because a silently wrong "
"offset is the failure mode.")
ap.add_argument("--elf", required=True, help="the .so/.o to resolve against")
ap.add_argument("--hits", help="hw_watch.py HIT log (absolute PCs)")
ap.add_argument("--trace", help="stalker/trace log; use keys for --mod only")
ap.add_argument("--addrs", nargs="*", help="absolute runtime addresses (0x..)")
ap.add_argument("--offs", nargs="*", help="module offsets (vaddr space, or --offs-are-file)")
ap.add_argument("--addr-file", help="file of one address per line")
ap.add_argument("--off-file", help="file of one module offset per line")
ap.add_argument("--mod", help="module name for --trace keys / --maps matching")
ap.add_argument("--maps", help="/proc/<pid>/maps to find the load base from")
ap.add_argument("--base", help="explicit load base (overrides --maps)")
ap.add_argument("--offs-are-file", action="store_true",
help="--offs carries file offsets, not vaddrs")
ap.add_argument("--json", action="store_true")
args = ap.parse_args()
try:
return cmd_main(args)
except BrokenPipeError:
return 0
if __name__ == "__main__":
sys.exit(main())
+325
View File
@@ -0,0 +1,325 @@
#!/usr/bin/env python3
"""trace_diff.py -- differential execution-trace reduction: two runs, one answer.
What question this answers
--------------------------
Not "what executed" but "**where did run A stop behaving like run B**". You run
the same operation twice with exactly one input changed -- the password's last
character, license valid versus invalid, key 0x00... versus key 0xff... -- and
diff the two execution traces. Every common block is machinery; every divergent
block is a decision that *your one-bit input difference* reached. Against OLLVM
control-flow flattening the divergences name the dispatcher states actually
taken; against a VM/VMP interpreter they name the handler sequence for the two
inputs; against whitebox-style crypto they name the key-dependent branch set.
This is the "differential" half of `references/ete-diff-tracing.md`, and it is
producer-agnostic by design.
What it accepts (auto-detected, or forced with --format)
-------------------------------------------------------
stalker `stalker_trace.js` logs: `BLK <seq> <mod>+0x<off>` block events
(BB/CALL/DONE lines ignored), including logs wrapped by
`run_probe.py` as `[host ts] [dev ts] TRACE BLK ...`.
plain any text where each line carries an address -- the last
whitespace token that parses as hex (`0x7f...`, `7f...`, and
`mod+0xoff` keys kept verbatim so modules stay comparable).
Decoded CoreSight/ETE `perf script` output fits this shape, as do
hand-scrapped disassembly walks.
What it produces
----------------
reduce the run-length form of one trace: ordered `(block, count)` runs plus
the execution histogram -- the same reductions `stalker_report.py`
prints, in a form the differ consumes.
diff an alignment of the two run-length sequences (difflib on the block
keys): identical prefix/suffix lengths, **first divergence**, each
divergent region with per-side counts, and the blocks unique to each
side ranked by count. The unique-to-B list is where "what does the
valid path do that the invalid path does not" is answered.
Discipline the tool does not enforce for you
--------------------------------------------
Same build and same address space (module+offset keys, never raw pointers,
unless ASLR is off); same input length; same capture window; and a *pair* whose
only difference is the input bit you chose. A diff across two builds or two
code versions is noise, and the tool cannot tell -- garbage in, confident
nonsense out.
Measured status: the parsers and the reducer/differ are host-tested on
synthetic flattened-dispatcher fixtures (tests/unit/test_trace_diff.py). No
ETE-produced trace has been fed to it by this repository's passes -- see
references/ete-diff-tracing.md for the producer side and its device gates.
Examples
python trace_diff.py reduce --log traceA.log
python trace_diff.py diff --a traceA.log --b traceB.log --top 15
python trace_diff.py diff --a wrong.log --b right.log --json
"""
import argparse
import difflib
import json
import re
import sys
from collections import Counter
# A block key is a hex address: `0x`-prefixed (unambiguous), or a bare run
# of at least 8 hex digits that is NOT pure decimal -- an all-digit run is
# indistinguishable from a date, a PID range or a count, and a log holding
# only those must stay refused (an empty trace is a capture failure, not a
# result). Shorter bare runs would swallow PIDs and timestamp fragments.
HEX_TOK = re.compile(r"^(?:0x[0-9a-fA-F]+|(?=[0-9a-fA-F]*[a-fA-F])[0-9a-fA-F]{8,})$")
MOD_OFF = re.compile(r"^[A-Za-z0-9_.+-]+\+0x[0-9a-fA-F]+$")
BLK_LINE = re.compile(r"(?:^|\s)BLK\s+\d+\s+(\S+)(?:\s|$)")
class TraceError(ValueError):
pass
# ---------------------------------------------------------------------------
# parsing
# ---------------------------------------------------------------------------
def parse_line_stalker(line):
"""One BLK line -> its block key, or None."""
m = BLK_LINE.search(line)
return m.group(1) if m else None
def parse_line_plain(line):
"""One plain trace line -> its block key, or None."""
toks = line.split()
for tok in reversed(toks):
if MOD_OFF.match(tok):
return tok
if HEX_TOK.match(tok):
return tok.lower()
return None
PARSERS = {"stalker": parse_line_stalker, "plain": parse_line_plain}
def read_blocks(path, fmt=None):
"""Ordered block keys from a trace log. Empty input raises: a diff against
nothing is a plumbing failure, not an analysis result (the zero-event
lesson from native-dbi-and-deobfuscation.md applies here too)."""
blocks = []
with open(path, "r", encoding="utf-8", errors="replace") as fh:
for line in fh:
if fmt:
key = PARSERS[fmt](line)
else:
key = parse_line_stalker(line) or parse_line_plain(line)
if key:
blocks.append(key)
if not blocks:
raise TraceError(
"no block keys parsed from %s -- an empty trace is a capture "
"failure, not a result; see the zero-event diagnostics in "
"references/native-dbi-and-deobfuscation.md" % path)
return blocks
def run_length(blocks):
"""[(key, run_len)] preserving order -- D D D R1 becomes (D,3),(R1,1)."""
runs = []
for b in blocks:
if runs and runs[-1][0] == b:
runs[-1][1] += 1
else:
runs.append([b, 1])
return [(k, n) for k, n in runs]
# ---------------------------------------------------------------------------
# reduction report
# ---------------------------------------------------------------------------
def reduce_trace(blocks):
"""Histogram and run-length summary of one block list."""
hist = Counter(blocks)
return {
"events": len(blocks),
"distinct_blocks": len(hist),
"runs": len(run_length(blocks)),
"top": hist.most_common(),
"sequence": run_length(blocks),
}
# ---------------------------------------------------------------------------
# differential
# ---------------------------------------------------------------------------
def diff_traces(a_blocks, b_blocks):
"""Structural diff: first divergence, regions, uniques."""
a_runs, b_runs = run_length(a_blocks), run_length(b_blocks)
a_keys = [k for k, _ in a_runs]
b_keys = [k for k, _ in b_runs]
sm = difflib.SequenceMatcher(a=a_keys, b=b_keys, autojunk=False)
identical = sum(n for i, j, n in sm.get_matching_blocks())
regions = []
first_divergence = None
a_hist, b_hist = Counter(a_blocks), Counter(b_blocks)
for tag, i1, i2, j1, j2 in sm.get_opcodes():
if tag == "equal":
continue
if first_divergence is None:
first_divergence = {
"run_index_a": i1, "run_index_b": j1,
"event_index_a": sum(n for _, n in a_runs[:i1]),
"event_index_b": sum(n for _, n in b_runs[:j1]),
"last_common_block": a_runs[i1 - 1][0] if i1 > 0 else None,
}
regions.append({
"tag": tag,
"a_runs": a_runs[i1:i2],
"b_runs": b_runs[j1:j2],
})
only_a = sorted(((k, v) for k, v in (a_hist - b_hist).items()),
key=lambda kv: -kv[1])
only_b = sorted(((k, v) for k, v in (b_hist - a_hist).items()),
key=lambda kv: -kv[1])
common = a_hist & b_hist
return {
"a_events": len(a_blocks), "b_events": len(b_blocks),
"a_distinct": len(a_hist), "b_distinct": len(b_hist),
"shared_blocks": sum(common.values()),
"identical_run_span": identical,
"divergent_regions": len(regions),
"first_divergence": first_divergence,
"regions": regions,
"unique_to_a": only_a,
"unique_to_b": only_b,
}
def render_diff(d, top=10, context=3):
"""Human-readable rendering of a diff_traces document."""
out = []
out.append("run A: %d events, %d distinct blocks" % (d["a_events"], d["a_distinct"]))
out.append("run B: %d events, %d distinct blocks" % (d["b_events"], d["b_distinct"]))
out.append("shared block executions: %d divergent regions: %d"
% (d["shared_blocks"], d["divergent_regions"]))
fd = d["first_divergence"]
if fd is None:
out.append("\nNO DIVERGENCE: the two runs executed the same block sequence.")
out.append("Either the input difference never reached control flow (pure data),")
out.append("or the two logs came from the same run. Check the producer.")
else:
out.append("")
out.append("first divergence: after run #%d (A) / #%d (B), "
"%d/%d events in, last common block %s"
% (fd["run_index_a"], fd["run_index_b"],
fd["event_index_a"], fd["event_index_b"],
fd["last_common_block"] or "<start>"))
for idx, r in enumerate(d["regions"][:top], 1):
out.append("")
out.append("region %d (%s)" % (idx, r["tag"]))
out.append(" A: " + _fmt_runs(r["a_runs"]))
out.append(" B: " + _fmt_runs(r["b_runs"]))
if d["unique_to_a"] or d["unique_to_b"]:
out.append("")
out.append("blocks unique to A (input-dependent, A-side), top %d:" % top)
for k, n in d["unique_to_a"][:top]:
out.append(" %6d %s" % (n, k))
out.append("blocks unique to B (input-dependent, B-side), top %d:" % top)
for k, n in d["unique_to_b"][:top]:
out.append(" %6d %s" % (n, k))
return "\n".join(out)
def _fmt_runs(runs):
"""Format a run list for the region renderer."""
if not runs:
return "<nothing>"
return " -> ".join("%s%s" % (k, ("*%d" % n) if n > 1 else "") for k, n in runs[:12]) + \
(" ... (%d runs)" % len(runs) if len(runs) > 12 else "")
# ---------------------------------------------------------------------------
# subcommands
# ---------------------------------------------------------------------------
def cmd_reduce(args):
"""reduce subcommand."""
try:
blocks = read_blocks(args.log, args.format)
except (OSError, TraceError) as exc:
print("RESULT=refused")
print(str(exc), file=sys.stderr)
return 2
rep = reduce_trace(blocks)
if args.json:
print(json.dumps({"status": "ok", "log": args.log,
"events": rep["events"],
"distinct_blocks": rep["distinct_blocks"],
"runs": rep["runs"],
"top": rep["top"][:args.top]}, indent=1))
return 0
print("%s: %d events, %d distinct blocks, %d runs"
% (args.log, rep["events"], rep["distinct_blocks"], rep["runs"]))
print("\ntop %d blocks (dispatcher candidate = the wide-margin top):" % args.top)
for k, n in rep["top"][:args.top]:
print(" %6d %s" % (n, k))
print("RESULT=ok")
return 0
def cmd_diff(args):
"""diff subcommand."""
try:
a = read_blocks(args.a, args.format)
b = read_blocks(args.b, args.format)
except (OSError, TraceError) as exc:
print("RESULT=refused")
print(str(exc), file=sys.stderr)
return 2
d = diff_traces(a, b)
if args.json:
doc = dict(d)
doc["status"] = "ok"
doc["regions"] = d["regions"][:args.top]
doc["unique_to_a"] = d["unique_to_a"][:args.top]
doc["unique_to_b"] = d["unique_to_b"][:args.top]
print(json.dumps(doc, indent=1))
return 0
print(render_diff(d, top=args.top))
print("RESULT=ok")
return 0
def main():
"""CLI entry: subcommand dispatch."""
ap = argparse.ArgumentParser(
description="Differential reduction of two execution traces (stalker or plain "
"address logs): first divergence, divergent regions, unique blocks.",
epilog="references/ete-diff-tracing.md owns the method: same build, same "
"window, one changed input bit -- and what the divergences mean "
"against OLLVM flattening and VMP handler loops.")
sub = ap.add_subparsers(dest="cmd", required=True)
p = sub.add_parser("reduce", help="run-length + histogram of one trace")
p.add_argument("--log", required=True, help="trace log (stalker or plain)")
p.add_argument("--format", choices=sorted(PARSERS), help="force a parser (default: auto)")
p.add_argument("--top", type=int, default=20)
p.add_argument("--json", action="store_true")
p.set_defaults(fn=cmd_reduce)
p = sub.add_parser("diff", help="align two traces and report the divergences")
p.add_argument("--a", required=True, help="trace log A (e.g. the failing/invalid run)")
p.add_argument("--b", required=True, help="trace log B (e.g. the passing/valid run)")
p.add_argument("--format", choices=sorted(PARSERS), help="force a parser (default: auto)")
p.add_argument("--top", type=int, default=10, help="regions / unique blocks to show")
p.add_argument("--json", action="store_true")
p.set_defaults(fn=cmd_diff)
args = ap.parse_args()
try:
return args.fn(args)
except BrokenPipeError:
return 0
if __name__ == "__main__":
sys.exit(main())
@@ -0,0 +1,275 @@
#!/usr/bin/env python3
"""vm_handler_slice.py -- slice a VM/VMP interpreter trace into handler segments
and recover the guest-opcode trajectory.
What question this answers
--------------------------
A trace of a virtualized function is a trace of the interpreter: one dispatch loop,
executed once per guest instruction, with the guest's work hidden inside per-opcode
handler code. Reading that trace block-by-block tells you nothing -- but **slicing it at
dispatcher entries** turns it back into a program:
D h_a h_b D h_c D h_a h_b D ... (D = dispatcher, h_* = handler blocks)
-> segment "h_a h_b" (count 2), segment "h_c" (count 1)
-> trajectory: S(ab) S(c) S(ab) ... == the guest bytecode, one signature per opcode
With a handler->opcode map (see below), the trajectory becomes the guest program's
opcode sequence: the disassembly of the VM you cannot read statically.
Calibration source -- an open IR-level VM, in the shape this tool expects
------------------------------------------------------------------------
This tool's segment model is calibrated against a public LLVM-IR virtualizer
(dpkg123/ollvm-aosp-toolchain, commits 63fec41f and 9b7cf75e, 2026-09 -- the `xvmp`
generic pass; source facts below are read from that patch, `inferred` for runtime
behaviour, `observed` for the source text):
- each virtualized function becomes a stub calling ``vm_exec(code, size, vm_args, num)``:
the bytecode array plus a boxed-argument array live in the module, so the *inputs*
(arguments, and any key material passed in) are observable at the ``vm_args`` array
with a hardware watchpoint, before interpretation starts;
- the bytecode is a little-endian stream of one-byte opcodes with two operand spaces
(``VT_CONST`` inline constants, ``VT_SLOT`` slot references): OP_ALLOCA 0x01,
OP_LOAD 0x02, OP_STORE 0x03, OP_GEP 0x05, OP_CMP 0x06, OP_CAST 0x07, OP_BR 0x08,
OP_CALL 0x09, OP_RET 0x0A, OP_ARITH 0x10 (19 arithmetic sub-ops), OP_PARAMMAP 0x11;
- pointer constants are NOT inlined: they materialize through a generated dispatch
(HandleCallTool's const/call chain, `K_Addr/K_IntBits/K_FPBits`) at first use --
a small set of extra blocks in the trace that is *not* the main interpreter loop,
and a high-value observation point (the returned i64 is the constant's bits);
- an interpreter is a loop, so the dispatcher dominates the block histogram, and each
guest opcode contributes a stable, repeating block subsequence -- which is exactly
the signature clustering below.
Because that implementation's opcode table is public, `--opcode-map` can name every
segment; for a closed VMP the same slicing still yields the trajectory, and the map
becomes a manual labelling task over the (usually few dozen) unique segments.
How to build --opcode-map
-------------------------
Run once, read the segment table (each segment's first block = handler entry), and map
handler-entry addresses to opcodes. For the open implementation above, match the entry
block to the code generated for each `case OP_*` by disassembling the `.so` around the
dispatch switch. Map file format: ``{"<first-block-key>": "OP_LOAD", ...}``.
Inputs and behaviour
--------------------
- `--log` accepts the same grammars as `trace_diff.py` (stalker `BLK` lines, wrapped or
not, and any address-per-line stream), by importing its parsers.
- `--dispatcher` picks the slice boundary: `auto` (the histogram's wide-margin top --
the same dispatcher-identification rule as `references/native-dbi-and-deobfuscation.md`
§5 From a trace to a deobfuscated function (inferred)) or an explicit block key.
- Segments are the block runs between dispatcher hits; identical runs get one signature
(sha1[:10]). Blocks before the first / after the last dispatcher hit are reported as
`preamble`/`tail`, never silently merged.
- Diagnostics guard the two ways this goes wrong: a dispatcher that is really a spin
loop (every segment length 1, unique-segment ratio ~1.0), and a trace that is not a
VM at all (no dominant block; native-dbi-and-deobfuscation.md's "trace of a VM" note).
Measured status: host-tested on synthetic interpreter traces in the calibrated shape
(tests/unit/test_vm_handler_slice.py). No real xvmp-hardened binary was ever traced by
this repository -- the calibration facts are from the public source, and the first run
against a real sample is a measurement, not a confirmation.
Examples
python vm_handler_slice.py --log vm_trace.log
python vm_handler_slice.py --log vm_trace.log --dispatcher liba.so+0x9f6c0 --top 15
python vm_handler_slice.py --log vm_trace.log --opcode-map map.json
"""
import argparse
import hashlib
import json
import sys
from collections import Counter
sys.path.insert(0, __file__.rsplit("/", 1)[0])
import trace_diff # noqa: E402 (sibling import is the repo's convention: dexutil et al)
def pick_dispatcher(blocks):
"""Dispatcher identification, same rules as native-dbi-and-deobfuscation.md's
trace-to-CFG step: the histogram's top, WITH many distinct predecessors (every
handler tail jumps back to it). The margin alone is not a test: in a stack-shaped
VM the hottest handler can execute on every guest instruction, exactly as often
as the dispatcher, so a 4x rule refuses real interpreter traces."""
hist = Counter(blocks)
if not hist:
raise trace_diff.TraceError("empty trace")
top = hist.most_common()
cand = top[0][0]
preds = {blocks[i - 1] for i in range(1, len(blocks)) if blocks[i] == cand}
if len(top) > 1 and len(preds) < 3:
# not the only block, and entered from fewer than three predecessors:
# a plain hot loop (or a two-cycle tie), not a dispatch loop. A pure
# frequency margin cannot rescue it -- in a stack-shaped VM the hottest
# handler runs exactly as often as the dispatcher, so frequency is the
# one signal this rule refuses to trust
return None
return cand
def slice_segments(blocks, dispatcher):
"""-> (segments, preamble, tail): segments = list of (run, closed).
The run after the LAST dispatcher hit is an open segment -- never closed by a
following dispatch (exit path, or a truncated capture window) -- so it joins
the trajectory but carries closed=False and is flagged in the output."""
idxs = [i for i, b in enumerate(blocks) if b == dispatcher]
if not idxs:
raise trace_diff.TraceError("dispatcher block never executed")
preamble = blocks[:idxs[0]]
tail = blocks[idxs[-1] + 1:]
segments = []
for a, b in zip(idxs, idxs[1:]):
run = blocks[a + 1:b]
if run: # a dispatcher hit followed immediately by another
segments.append((run, True)) # contributes nothing (miss/no-op path)
if tail:
segments.append((tail, False))
return segments, preamble, tail
def signature(run):
"""A segment's run list -> its comparison signature."""
return hashlib.sha1("\x00".join(run).encode("utf-8")).hexdigest()[:10]
def cluster(segments):
"""-> ordered trajectory of signatures + per-signature stats."""
stats = {}
trajectory = []
for i, (run, closed) in enumerate(segments):
sig = signature(run)
trajectory.append(sig)
if sig not in stats:
stats[sig] = {"sig": sig, "count": 0, "first": i,
"blocks": run, "len": len(run), "open_tail": False}
stats[sig]["count"] += 1
if not closed:
stats[sig]["open_tail"] = True
return trajectory, stats
def diagnostics(blocks, dispatcher, segments, stats):
"""Sanity notes (spin loops, degenerate shapes)."""
n = len(blocks)
dcount = blocks.count(dispatcher)
avg_len = sum(len(s) for s in segments) / len(segments) if segments else 0
unique_ratio = len(stats) / len(segments) if segments else 1.0
notes = []
if avg_len <= 1.05:
notes.append("every segment is a single block: the 'dispatcher' is probably a spin "
"loop or the trace granularity is wrong, not an interpreter")
if unique_ratio > 0.9 and len(segments) > 8:
notes.append("almost every segment is unique: not a repeating opcode-handler "
"structure -- either wrong dispatcher or not a VM trace")
if dcount * 1.0 / n < 0.15:
notes.append("dispatcher is under 15%% of blocks (%d/%d): the loop does not "
"dominate; re-check with trace_diff.py reduce's histogram"
% (dcount, n))
return {"blocks": n, "dispatcher_hits": dcount,
"dispatcher_share": round(dcount * 100.0 / n, 1),
"segments": len(segments), "unique_segments": len(stats),
"avg_segment_len": round(avg_len, 2), "notes": notes}
def apply_map(trajectory, stats, map_path):
"""Map handler keys to opcode labels; report unmapped blocks."""
with open(map_path, "r", encoding="utf-8") as fh:
table = json.load(fh)
named, unmapped = [], []
for sig in trajectory:
run = stats[sig]["blocks"]
key = run[0]
if key in table:
named.append(table[key])
else:
named.append("?")
unmapped.append(key)
return named, unmapped
def cmd_main(args):
"""Resolve input rows against the ELF; text or --json output."""
try:
blocks = trace_diff.read_blocks(args.log, args.format)
except (OSError, trace_diff.TraceError) as exc:
print("RESULT=refused")
print(str(exc), file=sys.stderr)
return 2
dispatcher = args.dispatcher or pick_dispatcher(blocks)
if dispatcher is None:
print("RESULT=refused")
print("no wide-margin top block: this trace does not look like an interpreter "
"loop (see trace_diff.py reduce, and the VM-trace note in "
"references/native-dbi-and-deobfuscation.md)", file=sys.stderr)
return 2
try:
segments, preamble, tail = slice_segments(blocks, dispatcher)
except trace_diff.TraceError as exc:
print("RESULT=refused")
print(str(exc), file=sys.stderr)
return 2
trajectory, stats = cluster(segments)
diag = diagnostics(blocks, dispatcher, segments, stats)
named = unmapped = None
if args.opcode_map:
named, unmapped = apply_map(trajectory, stats, args.opcode_map)
if args.json:
doc = {"status": "ok", "dispatcher": dispatcher, "diagnostics": diag,
"preamble": preamble, "tail": tail,
"segments": sorted(stats.values(), key=lambda s: -s["count"]),
"trajectory": trajectory}
if named is not None:
doc["opcode_trajectory"] = named
doc["unmapped_handler_blocks"] = sorted(set(unmapped))
print(json.dumps(doc, indent=1))
return 0
print("dispatcher: %s (%s%% of %d blocks, %d hits)"
% (dispatcher, diag["dispatcher_share"], diag["blocks"],
diag["dispatcher_hits"]))
print("segments: %d total, %d unique, avg len %.2f%s%s"
% (diag["segments"], diag["unique_segments"], diag["avg_segment_len"],
(" preamble=%d blocks" % len(preamble)) if preamble else "",
" [last segment UNCLOSED: exit path or truncated window]"
if any(s.get("open_tail") for s in stats.values()) else ""))
for note in diag["notes"]:
print(" note: %s" % note)
print("\nsegment table (top %d by count):" % args.top)
for s in sorted(stats.values(), key=lambda s: (-s["count"], s["first"]))[:args.top]:
print(" %5dx sig=%s len=%d %s" % (s["count"], s["sig"], s["len"],
" -> ".join(s["blocks"][:8])
+ (" ..." if s["len"] > 8 else "")))
print("\ntrajectory (first %d): %s" % (args.top, " ".join(trajectory[:args.top])))
if named is not None:
print("opcode trajectory (first %d): %s" % (args.top, " ".join(named[:args.top])))
if unmapped:
print("unmapped handler blocks: %d distinct -- extend --opcode-map"
% len(set(unmapped)))
print("\nRESULT=ok")
return 0
def main():
"""CLI entry: subcommand dispatch."""
ap = argparse.ArgumentParser(
description="Slice a VM interpreter trace into handler segments: dispatcher "
"detection, segment signatures, trajectory, optional handler->opcode map.",
epilog="Calibrated against the public xvmp IR virtualizer (vm_exec + slot machine); "
"references/ete-diff-tracing.md owns the capture, this tool owns the "
"de-virtualization half of the reading.")
ap.add_argument("--log", required=True, help="interpreter trace (stalker or plain)")
ap.add_argument("--format", choices=sorted(trace_diff.PARSERS),
help="force a parser (default: auto, as trace_diff.py)")
ap.add_argument("--dispatcher", help="explicit dispatcher block key (default: auto)")
ap.add_argument("--opcode-map", help="JSON {handler-first-block: opcode-name}")
ap.add_argument("--top", type=int, default=20)
ap.add_argument("--json", action="store_true")
args = ap.parse_args()
try:
return cmd_main(args)
except BrokenPipeError:
return 0
if __name__ == "__main__":
sys.exit(main())