Files
apk-reverse/skills/apk-reverse/scripts/vm_handler_slice.py
T
root ab22b225ff Hardware pass: hardware watchpoints, ETE differential tracing, kernel-side event injection
Add the layer under the software tooling, in the repository's shape: what runs
on a host is measured, and every device-bound or kernel-bound piece is a gated,
labelled template.

- scripts/hw_watch.py: arm64 hardware watchpoints/execution breakpoints. WCR/
  BCR encoders with the 8-byte-window and alignment rules (unit-tested to the
  canonical 0x1FFD/0x3F05), a self-contained on-device ptrace helper that
  compiles clean as a static aarch64 ELF (observed: cross gcc 16.2), adb
  orchestration, and hit decoding to module+offset. The stealth ledger is
  stated three times on purpose: the ptrace route leaves TracerPid visible.
- scripts/ete_capture.py: CoreSight ETE capture with the five gates (kernel
  era, coresight sysfs, cs_etm PMU, perf-with-CORESIGHT, permission), the
  perf/OpenCSD build recipe, and the per-thread record wrapper whose
  --control one-instruction run must pass before any real capture.
- scripts/trace_diff.py: differential reduction of two execution traces into
  first-divergence / divergent regions / blocks-unique-to-each-side.
  Producer-agnostic: Stalker BLK logs (raw or run_probe-wrapped) and decoded
  cs_etm address streams alike, host-tested on synthetic flattened-dispatcher
  fixtures.
- scripts/kernel_instrument_scaffold.py: KPM/LKM watchpoint + register-rewrite
  templates plus an honest userspace carrier; every kernel file carries a
  NOT COMPILED AND NOT LOADED banner that verify enforces, and the
  register-rewrite action is fenced as experimental in the template itself.
- references/hardware-watchpoints.md, references/ete-diff-tracing.md,
  references/kernel-event-injection.md: the mechanism, the gates, the traps,
  and the composition recipes (watch the key buffer; diff two runs; flip the
  decision at the trap), with hand-offs instead of duplication.
- Registration: 3 symptom rows (SKILL.md + routing mirror), 3 reference rows,
  4 script rows, coverage/route-inventory/evidence rows, capability-matrix
  entries, known-limitations entry, eval case 4, README trees and coverage
  paragraphs in both languages, and three EXTENSION evidence records stating
  exactly what was measured on this host (encoders, compile, fixtures,
  generate/verify) and what was not (no watchpoint armed, no cs_etm stream,
  nothing kernel-side compiled or loaded anywhere).

Hardware pass 2: SPE/BRBE crash forensics, address symbolization, VM de-virtualization

Calibrated against the two public obfuscator implementations the user supplied
(dpkg123/ollvm-aosp-toolchain: Hikari-family and Pluto-family flattening, the
xvmp IR virtualizer at 63fec41f/9b7cf75e; fetched read-only into
tools/_work/refs, git-ignored). Source facts observed, runtime inferred.

- scripts/trace_addr2sym.py: runtime addresses -> symbol + static offset,
  zero-dependency ELF64 (PT_LOAD vaddr<->file-off conversion, .dynsym/.symtab,
  stripped section+off fallback). Host-verified against a real cross-compiled
  aarch64 .so with readelf as the independent cross-check, plus a hand-built
  minimal ELF covering vaddr!=offset; accepts hw_watch hits, trace mod+off
  keys, or raw address lists. Fixes on the way: --base int coercion, and
  nearest-below symbol semantics for PCs in inter-symbol gaps.
- scripts/vm_handler_slice.py: slice an interpreter trace into handler
  segments (dispatcher detection = histogram top + predecessor diversity; the
  first 4x-margin rule refused the calibrated fixture itself and was replaced),
  signature clustering, open-tail handling, and the guest-opcode trajectory via
  an optional handler->opcode map. Calibrated against xvmp's documented shape:
  vm_exec(code,size,vm_args,num) stubs, boxed arguments observable before
  interpretation, 11 opcodes + 19 arithmetic sub-ops, const-materialization
  dispatch as a distinct segment.
- references/hardware-sampling-and-crash.md: SPE (data flow without a known
  address; PC + data address per sample), BRBE (branch records when a crash
  stack cannot be trusted), the crash loop (tombstone -> branch records ->
  symbolize), and what each adds to the OLLVM/VMP routes. All
  documented-and-gated: no SPE sample or BRBE record was captured here.
- Flattening-recipe corrections from source: Hikari keeps a frame-resident
  switchVar written through an indirect pointer with scramble32-permuted cases;
  Pluto stores switchVar/switchXor rolling-XOR so memory only ever holds noise
  -- hardware-watchpoints.md's state-variable recipe now distinguishes the
  families, and ete-diff-tracing.md carries the family table plus the xvmp
  shape. AntiDebugging injects raw svc inline asm with randomized fields, so
  byte signatures cannot survive it; semantic site-location can.
- Registration: 3 symptom rows (untrustworthy crash stack; runtime addresses
  needing symbols; interpreter loop reading as noise), reference/script rows,
  coverage/evidence/capability-matrix entries, README trees in both languages,
  and EXTENSION-sampling-crash.md recording exactly what was measured (real .so
  symbolization, slicer fixtures, the slicer's self-found dispatcher-rule bug)
  and what was not (no SPE/BRBE capture, no hardened binary ever traced).

Fix all PR #1 review findings by severity; un-entrench the tests that pinned them

Major (device-breaking / wrong results):
- hw_watch: match the arm64 user_hwdebug_state slot ABI (address register
  first, control second -- the swap armed nothing correctly)
- hw_watch: every post-seize error path now clears slots and detaches;
  PTRACE_O_EXITKILL turned an analysis failure into a SIGKILL of the target
- hw_watch: forward genuine target SIGTRAPs (raise/tgkill/int3); only ptrace
  event stops are consumed -- the reference's signal-preservation promise
- hw_watch: run checks push/build/chmod and the helper's exit status; a dead
  capture is RESULT=blocked, never ok-no-hits
- hw_watch: decode offsets from the module base, not each mapping's start
- ete_capture: --control now requires the control record to DECODE
  (perf script event lines), the documented gate, not just a zero exit
- ete_capture: -o before the -- workload separator in --cmd mode; all
  values shell-quoted into su -c; stale remote perf.data removed first;
  perf exit status checked (timeout's 124 is the normal window end)
- kernel scaffold: --set-reg restricted to x0..x30; the rewrite is gated by
  its own breakpoint event so a watchpoint hit cannot run it; LKM pins the
  task (get/put_task_struct) across registration and calls the four-argument
  register_user_hw_breakpoint; --break rows initialize .addr/.len/.bp_type
  (there is no .pc member); --id validated against the module-id grammar
- trace_addr2sym: exact module matching (path or basename, never substring);
  load bias aligned against the PT_LOAD (bias = start - (foff - p_offset)
  - p_vaddr); PCs outside the ELF span are refused instead of landing on the
  last symbol; --json stdout is a pure document (RESULT= in text mode only)
- trace_diff: block keys need 0x or >=8 hex digits (PIDs/counts no longer
  parse); vm_handler_slice: dispatcher needs predecessor diversity, no
  frequency-margin fallback; both JSON outputs pure (check_commands' rule)

Minor/nitpick:
- capability-matrix + evidence-summary: trace reduction status ok->partial
  (no real-target producer ran it; the file's own definition of ok)
- hardware-sampling-and-crash.md and the script examples carry --elf
- kernel-event-injection.md no longer claims the callback's first statement
  is a filter (it prints unfiltered; the filter is a required change)
- Elf64 caches symbol addresses for per-row bisect
- docstring coverage of the touched surface: 16.67% -> 100%

Tests: every json.loads workaround stripping RESULT= is gone; new regression
tests cover the slot ABI, release-on-error, SIGTRAP forwarding, non-first
mapping offsets, -o placement and su -c quoting, the control decode gate,
x31/--id refusals, the guarded rewrite, outside-ELF PCs and bias math,
short-token refusal and the dispatcher margin rule. The minimal-ELF fixture
now packs a full 8-field Elf64_Phdr (its 7-field pack silently shifted
p_memsz onto p_align). 739 passed; flake8/mypy and all five gates clean.

Address the follow-up review on 0bd8dfe (6 findings)

- ete_capture: two-layer quoting for the control-record, decode and cleanup
  commands -- shlex.quote spliced between literal single quotes broke out of
  su -c for any spaced output name; --seconds must be positive (timeout 0
  disables the timer), refused before any adb check
- kernel scaffold: --set-reg now refuses sp/pc as well (the template has no
  rewrite path for them; a generate that silently drops the action is the
  misleading kind of success); both templates arm with a sampling period
  (LKM via hw_breakpoint_init, KPM spells out sample_period=1) -- a zero
  period is a non-sampling event whose callback never fires: armed, silent
- trace_addr2sym: contains_vaddr tests each PT_LOAD's loaded span instead
  of the whole-file extent; a PC in the unmapped gap between segments is
  refused rather than named after the nearest symbol below
- trace_diff: a bare all-digit token is indistinguishable from a date or
  count, so block keys need 0x or a non-decimal hex digit; a date-only log
  stays refused as a capture failure

Tests: two-layer quoting with a spaced control name, --seconds refusal,
sp/pc in the rejection list, sampling-period assertions for both templates,
a two-segment gap fixture, and a date-only parser case. 743 passed; flake8,
mypy and all five gates clean.
2026-10-15 15:33:28 +08:00

276 lines
13 KiB
Python

#!/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())