mirror of
https://github.com/newliver666/apk-reverse.git
synced 2026-09-28 21:56:59 +02:00
2beade67f2
An external audit of this repository landed on four structural findings, and this pass is the
response to them. Everything below was re-measured on this tree before being acted on; two of the
audit's claims needed a correction of my own, and both corrections are noted in place.
**The gates were optimistic, and one of them did not read content at all.**
doctor.py the capability table was hard-coded (`repack + sign = bool(java) and python`), so a
machine with a JDK and no zipalign/apksigner was told it could re-sign; the script
inventory was a hand-written list of 28 against 51 files on disk, omitting the nine
most important scripts. It now scans the directory (`registered/checked = 52 / 52`,
unjudgeable entries counted as `unknown`, never skipped) and resolves capability as a
real requires-closure, reporting BLOCKED with the missing piece and an installable
next step. Measured: the old doctor reports `[OK] repack + sign` on a JDK-only PATH
where keytool, jarsigner, zipalign, apksigner and all eight smali jars are absent.
The report is allowed to look bad; that is the point.
check_commands.py (new)
anchor checks prove a section exists, never that a command runs. This parses each
script's argparse table with `ast` and checks every documented invocation against it.
146 commands / 41 interfaces, 0 drift. It caught `sig_probe.py --live --pkg`, which
does not exist -- the reference had been wrong since it was written.
check_routing.py (new)
the symptom index now has one home (`SKILL.md`, because symptom -> file must stay one
hop) and the inventory has another (`references/routing.md`). This gate proves the
mirror agrees, that every reference file is named, and that every script is named.
**so_constpatch.py was corrupting APKs it rebuilt.** The writer copied filename and timestamp onto a
fresh ZipInfo and recompressed the whole archive: 435-entry real package, `extra` lost on 205 entries,
`external_attr` rewritten on all 435, 132 untouched entries recompressed, and `resources.arsc` pushed
to an unaligned offset. Android's own `zipalign -c -v 4` fails the old output and passes the new one;
a 26-case fixture suite pins it, and `extractNativeLibs="false"` is now refused by default with a
pointer at `repack.py`. Fixing it exposed the same defect in `repack.py`, where `^lib/[^/]+\.so$`
cannot match `lib/arm64-v8a/*.so` -- so its alignment handling had never once fired on a real APK.
**The package was not self-contained.** 59 references pointed at the repository root, which `npx
skills add` does not install. They now converge on `references/evidence-summary.md` (capability,
conclusion, strength, evidence that ships) plus `evidence/` with the machine-readable companions: 8
references remain and each is explicitly labelled "repository root, not shipped". A drill in which
the new files were copied to an isolated directory and asked three questions answered all three.
**SKILL.md: 572 lines / 15,733 tokens -> 398 lines / ~10,900 tokens**, without losing the two things
that had to stay. The symptom index remains inline; the reference and script indexes moved to the
on-demand routing file. The freed budget went into making the rules *stronger*, not shorter: the
constraints section is now a table of thirteen non-negotiable constraints each paired with the check
that catches a violation, and the tooling obligations are instructions whose violation is a defect
(hedging language in SKILL.md: 1 line, down from a paragraph of hedged advice). Long references gained
a navigation head so a reader can decide from the top whether to load one.
**New: an executable test layer.** `tests/` (unit, CLI contract, no-device integration) plus
`pytest.ini`, `tests/run_tests.py`, a minimal CI job, and `evals/evals.json` written but never run --
stated as such in the file. 608 passed / 158 skipped / 25 xfailed. The 25 xfails are a work order, not
a pass: thirteen scripts report a missing input by exiting 1 instead of 2, seven let a traceback
escape, four treat `--help` as an error, and `preflight --json` lacks a `status` field. A regression
row for `dex_patch_bytes.py` reproduces a previous artifact byte-for-byte
(`67ae1e58e7a44777e5ac8f9f823ab21e…`) and additionally pins the four bytes that may move.
**Two real defects the new tests found in the tooling itself.** `check_repo.py` decoded subprocess
output with the locale encoding, so on a GBK console a script printing an em dash in its usage line
was reported as crashed; it now decodes UTF-8 explicitly. And a CLI contract test drove a *real
attached device* through `coldstart.py` -- logcat, screenshots, five orphan `adb logcat` processes --
which is fixed twice over: child processes run in a private temporary cwd, and the fifteen
device-bound scripts are never executed beyond `--help`/argument parsing.
**Quoting on the device side is now one implementation.** `devsh.su` escaped only double quotes, so
`;`, `$( )` and backticks in a caller-supplied value still reached the device shell while
`coldstart.Device.shell` quoted correctly. Both use `scripts/device_shell.py` now: POSIX single-quote
escaping, argv arrays by default, and identifier validation that refuses a malformed package, path or
component with exit 2 instead of a traceback. The integration test that used to pin the *defect* (it
asserted `su -c "…"` with `\"` escapes) now asserts the property, across five metacharacter payloads.
Gates: check_repo 0 problems (including the content-reading identity scan over git ls-files),
check_refs 0 dangling / 0 warning, check_routing coherent (53 symptom / 45 reference / 54 script rows),
check_budget 0 failure / 0 warning with the always-loaded body now measured in tokens rather than in
"narrative lines with the index excluded", check_commands 146 checked / 0 drift, pytest 608 passed.
Not done, and stated rather than implied: CI has never run on a runner; the evals have never been
executed; the 25 xfails are open; no device-side install/launch was performed in this pass.
217 lines
10 KiB
Python
217 lines
10 KiB
Python
"""The established regression for scripts/dex_patch_bytes.py.
|
|
|
|
This is the repository's pre-existing red line, not a test invented here: the
|
|
l1l3 benchmark row recorded that patching the first root check in
|
|
`MainActivity.onCreate` produces a specific file, and any refactor of the dex
|
|
reader, the spec matcher, the header fixer or the writer must reproduce it
|
|
byte for byte.
|
|
|
|
`tools/_work/bench/**` is git-ignored, so these tests skip with a printed reason
|
|
on a fresh checkout. That is deliberate: a regression marker that turns red
|
|
because a sample is absent is a marker people learn to ignore.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from conftest import L1L3_PATCHED_SHA256, load_script, require_path, run_script
|
|
|
|
dexutil = load_script("dexutil.py")
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
SPEC_NAME = "l1_root.spec.json"
|
|
SITE_OFF = 0x9D0
|
|
OLD_BYTES = "39000e00"
|
|
NEW_BYTES = "00000000"
|
|
|
|
|
|
@pytest.fixture
|
|
def sample(bench_l1l3):
|
|
"""Paths to the recorded input, spec and expected output."""
|
|
dex = require_path(bench_l1l3 / "dex" / "classes.dex",
|
|
"git-ignored bench tree")
|
|
spec = require_path(bench_l1l3 / SPEC_NAME, "git-ignored bench tree")
|
|
expected = require_path(bench_l1l3 / "dex" / "classes.patched.dex",
|
|
"git-ignored bench tree")
|
|
return {"dex": dex, "spec": spec, "expected": expected}
|
|
|
|
|
|
def patch(sample, tmp_path, extra=()):
|
|
out = tmp_path / "out.patched.dex"
|
|
report = tmp_path / "report.json"
|
|
result = run_script("dex_patch_bytes.py",
|
|
[str(sample["dex"]), "--spec", str(sample["spec"]),
|
|
"-o", str(out), "--report", str(report)] + list(extra))
|
|
return result, out, report
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# the hash marker
|
|
# --------------------------------------------------------------------------- #
|
|
class TestRecordedRegression:
|
|
def test_the_patch_runs_and_exits_zero(self, sample, tmp_path):
|
|
result, _out, _report = patch(sample, tmp_path)
|
|
assert not result.timed_out, repr(result)
|
|
assert not result.has_traceback, result.output
|
|
assert result.returncode == 0, result.output
|
|
|
|
def test_output_matches_the_recorded_sha256(self, sample, tmp_path):
|
|
_result, out, _report = patch(sample, tmp_path)
|
|
digest = hashlib.sha256(out.read_bytes()).hexdigest()
|
|
assert digest.startswith(L1L3_PATCHED_SHA256), (
|
|
"the recorded marker moved: got %s, expected prefix %s"
|
|
% (digest, L1L3_PATCHED_SHA256))
|
|
|
|
def test_output_is_byte_identical_to_the_stored_expectation(self, sample, tmp_path):
|
|
_result, out, _report = patch(sample, tmp_path)
|
|
assert out.read_bytes() == sample["expected"].read_bytes()
|
|
|
|
def test_only_the_four_patch_bytes_and_the_header_change(self, sample, tmp_path):
|
|
"""Pin *where* the difference is, so a stray write cannot pass as a hash match.
|
|
|
|
The allowed window is bytes 8..32 (checksum + signature, recomputed) plus
|
|
the four bytes of the patched instruction. Two of those four keep their
|
|
original value (`0e00` -> `0000` clears one byte and leaves the zero
|
|
byte), and a recomputed checksum may keep a byte by coincidence, so the
|
|
assertion is containment plus the changes that must occur.
|
|
"""
|
|
_result, out, _report = patch(sample, tmp_path)
|
|
before = sample["dex"].read_bytes()
|
|
after = out.read_bytes()
|
|
assert len(before) == len(after), "an equal-length patch must not resize the file"
|
|
|
|
diffs = {i for i, (a, b) in enumerate(zip(before, after)) if a != b}
|
|
window = set(range(8, 32)) | {SITE_OFF, SITE_OFF + 1, SITE_OFF + 2, SITE_OFF + 3}
|
|
|
|
assert diffs <= window, "bytes changed outside the owned window: %r" % sorted(
|
|
diffs - window)
|
|
# The two bytes that differ between 39000e00 and 00000000 must have moved.
|
|
assert {SITE_OFF, SITE_OFF + 2} <= diffs
|
|
# Both header fields are recomputed; each must have changed somewhere.
|
|
assert diffs & set(range(8, 12)), "no checksum byte changed"
|
|
assert diffs & set(range(12, 32)), "no signature byte changed"
|
|
|
|
def test_the_written_dex_self_verifies(self, sample, tmp_path):
|
|
_result, out, _report = patch(sample, tmp_path)
|
|
data = bytearray(out.read_bytes())
|
|
assert dexutil.verify_dex_header(data) == (True, True)
|
|
|
|
def test_the_result_still_parses_and_decodes_cleanly(self, sample, tmp_path):
|
|
_result, out, _report = patch(sample, tmp_path)
|
|
dex, _entry = dexutil.load_dex(str(out))
|
|
assert dex.check() == []
|
|
found = dex.find_method("Lsg/vantagepoint/uncrackable1/MainActivity;",
|
|
"onCreate", "(Landroid/os/Bundle;)V")
|
|
assert found
|
|
_section, _idx, code_off = found
|
|
_insns, clean, expected = dex.decode_all(code_off)
|
|
assert clean, "the patched method must still decode to exactly insns_size units"
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# the report the tool writes
|
|
# --------------------------------------------------------------------------- #
|
|
class TestPatchReport:
|
|
def test_report_records_the_site_and_the_polarity_check(self, sample, tmp_path):
|
|
_result, _out, report = patch(sample, tmp_path)
|
|
reports = json.loads(report.read_text(encoding="utf-8"))
|
|
assert len(reports) == 1
|
|
entry = reports[0]
|
|
assert entry["ok"] is True
|
|
assert entry["site_off"] == SITE_OFF
|
|
assert entry["old_bytes"] == OLD_BYTES
|
|
assert entry["new_bytes"] == NEW_BYTES
|
|
assert entry["expect_next_ok"] is True
|
|
assert entry["size_delta"] == 0
|
|
|
|
def test_report_confirms_the_post_edit_instruction_boundary(self, sample, tmp_path):
|
|
_result, _out, report = patch(sample, tmp_path)
|
|
entry = json.loads(report.read_text(encoding="utf-8"))[0]
|
|
assert entry["post_decode_clean"] is True
|
|
assert entry["verifier_edges"] == 0
|
|
|
|
def test_stdout_states_the_polarity_check_passed(self, sample, tmp_path):
|
|
result, _out, _report = patch(sample, tmp_path)
|
|
assert "polarity: expect_next satisfied" in result.stdout
|
|
|
|
def test_dry_run_reports_without_writing(self, sample, tmp_path):
|
|
out = tmp_path / "should-not-exist.dex"
|
|
result = run_script("dex_patch_bytes.py",
|
|
[str(sample["dex"]), "--spec", str(sample["spec"]),
|
|
"-o", str(out), "--dry-run"])
|
|
assert result.returncode == 0, result.output
|
|
assert "dry run: nothing written" in result.stdout
|
|
assert not out.exists()
|
|
|
|
def test_default_output_name_is_derived_from_the_input(self, sample, tmp_path):
|
|
"""Run on a copy: the bench tree is evidence and is never written by this suite."""
|
|
local = tmp_path / "classes.dex"
|
|
local.write_bytes(sample["dex"].read_bytes())
|
|
result = run_script("dex_patch_bytes.py",
|
|
[str(local), "--spec", str(sample["spec"])])
|
|
assert result.returncode == 0, result.output
|
|
derived = tmp_path / "classes.patched.dex"
|
|
assert derived.exists(), result.output
|
|
assert hashlib.sha256(derived.read_bytes()).hexdigest() == \
|
|
hashlib.sha256(sample["expected"].read_bytes()).hexdigest()
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# refusal paths: a wrong spec must fail loudly, not write a plausible file
|
|
# --------------------------------------------------------------------------- #
|
|
class TestRefusals:
|
|
def test_a_dry_run_against_a_missing_match_fails(self, sample, tmp_path):
|
|
spec = tmp_path / "bad.spec.json"
|
|
spec.write_text(json.dumps([{
|
|
"name": "no such site",
|
|
"class": "Lsg/vantagepoint/uncrackable1/MainActivity;",
|
|
"method": "onCreate",
|
|
"desc": "(Landroid/os/Bundle;)V",
|
|
"match": {"kind": "if-testz", "bytes": "deadbeef"},
|
|
"replace": {"kind": "nops"},
|
|
}]), encoding="utf-8")
|
|
result = run_script("dex_patch_bytes.py",
|
|
[str(sample["dex"]), "--spec", str(spec), "--dry-run"])
|
|
assert result.returncode == 1, result.output
|
|
assert "no instruction matched" in result.output
|
|
|
|
def test_a_wrong_polarity_pin_is_refused(self, sample, tmp_path):
|
|
"""expect_next exists to make the wrong side of a branch impossible to patch."""
|
|
spec = tmp_path / "bad-polarity.spec.json"
|
|
spec.write_text(json.dumps([{
|
|
"name": "wrong expected neighbour",
|
|
"class": "Lsg/vantagepoint/uncrackable1/MainActivity;",
|
|
"method": "onCreate",
|
|
"desc": "(Landroid/os/Bundle;)V",
|
|
"match": {"kind": "if-testz", "bytes": OLD_BYTES},
|
|
"expect_next": {"kind": "return"},
|
|
"replace": {"kind": "nops"},
|
|
}]), encoding="utf-8")
|
|
result = run_script("dex_patch_bytes.py",
|
|
[str(sample["dex"]), "--spec", str(spec), "--dry-run"])
|
|
assert result.returncode == 1, result.output
|
|
assert "expect_next not satisfied" in result.output
|
|
|
|
def test_a_missing_dex_is_a_usage_error(self, tmp_path, sample):
|
|
result = run_script("dex_patch_bytes.py",
|
|
[str(tmp_path / "nope.dex"), "--spec", str(sample["spec"])])
|
|
assert result.returncode == 2, result.output
|
|
assert "no such file" in result.output
|
|
|
|
def test_a_missing_spec_argument_is_a_usage_error(self, sample):
|
|
result = run_script("dex_patch_bytes.py", [str(sample["dex"])])
|
|
assert result.returncode == 2, result.output
|
|
|
|
def test_a_non_dex_input_is_refused_with_code_1(self, tmp_path, sample):
|
|
"""Structurally invalid input is a target-side failure (1), not a usage error (2)."""
|
|
bogus = tmp_path / "bogus.dex"
|
|
bogus.write_bytes(b"not a dex at all" * 8)
|
|
result = run_script("dex_patch_bytes.py",
|
|
[str(bogus), "--spec", str(sample["spec"]), "--dry-run"])
|
|
assert result.returncode == 1, result.output
|
|
assert "not a dex" in result.output
|