mirror of
https://codeberg.org/dabao1955/apk-reverse.git
synced 2026-11-30 11:50:23 +08:00
Adopt rasc (the Rust ASC) as the indexer, with its measured blind spot written down
The Python `droidasc` indexer works and is one `pip install` away, but the same project has a Rust
re-implementation on its `rust` branch, and this pass answers whether it can be used instead of the
Python one. It can, with one documented caveat. Everything below was measured here; nothing is taken
from the upstream README's numbers.
**What was built.** `rasc` publishes no artifact -- no release asset, and `cargo install rasc`
fetches an unrelated maths parser -- so adopting it means building it. rustup (gnu host, rustc 1.98.1)
plus a 64-bit MinGW-w64 gcc 16.1.0 as the linker, `cargo build --release` in **117 s**, producing a
2.13 MB binary from `MG1937/ASC@e809c7b` (rust branch, sha256 `1becdb86…c0264b`). The build traps are
recorded because they cost time here: the host's existing MinGW is **32-bit** and fails with
`sorry, unimplemented: 64-bit mode not compiled in`, E:\vs2022 has no VC tools, and the registered WSL2
distribution cannot start (`HCS_E_SERVICE_NOT_AVAILABLE`) -- so neither the MSVC nor the Linux path
was available.
**What it answers.** Same CLI shape as the Python tool (`classes` / `manifest` / `getclass` /
`findrefs {string,type,method,field}`), and on this repository's archives the answers match:
MASTG UnCrackable-Level3 (1.4 MB) classes 5.6x findrefs string 15.6x findrefs type 16.2x
a real 34.8 MB app (30,768 classes) classes 4.0x -- class sets IDENTICAL, 0 differences either way
`scripts/rasc_build.py` wraps the whole path -- `--check`, `--build`, `--verify <apk>` -- so the
verification travels with the kit instead of living in a note: `--verify` compares the class-definition
set against `droidasc` and fails on any difference. It resolves the repository root by walking up
rather than assuming a layout, prepends a non-default `CARGO_HOME` to PATH, and reports a missing
toolchain as exit 3 with the install step.
**The blind spot, and why the rule is a rule.** The rasc project ships its own JADX-parity harness.
Run here on a fair sample (app-like classes; the harness refuses to judge a class JADX cannot
decompile), 17 of 20 were judged and 16 matched literal-for-literal with 0 reorderings. The one
difference is not cosmetic: `org.bouncycastle.crypto.PasswordConverter` is an enum whose constants
override a method, JADX emits the three anonymous subclasses with their `convert` / `getType` bodies,
and **rasc emits a bare constant list -- the bodies are gone and no warning is emitted**. Clean,
plausible, incomplete output is precisely the failure shape this repository catalogues, so:
use rasc to locate, and to read ordinary classes; confirm an enum that shows no bodies with a
second reader; keep droidasc and JADX as the cross-checks the skill already requires.
The automated verdict said "literals rasc lost: 1", which understates it -- counting literals cannot
see a dropped body. That is recorded in the evidence file rather than smoothed over.
**What changed in the kit.** `references/rasc-and-droidsaw.md` (the measurements, the failing class,
the build traps, provenance); `scripts/rasc_build.py`; `toolchain.md` Tier 1 now lists `rasc` first
with `droidasc` as the fallback and the cross-check; README structure and script tables; the evidence
record `docs/tool-verification/EXTENSION-rasc.md`.
**One inconsistency this exposed and fixed.** `check_repo.py` looked for a reference or script name in
`SKILL.md` + the README only, while this repository's reference inventory lives in
`references/routing.md` -- so a correctly registered file was reported as orphaned, and the script
half of that check was not actually running. The registration surface is now SKILL.md + README +
`references/routing.md`, nested reference directories are read too, and a planted unregistered script
was used to confirm the check still fires.
Gates: check_repo 0 problem, check_refs 0 dangling / 0 warning, check_routing coherent, check_budget
0 failure / 0 warning, check_commands 154 checked / 0 drift, pytest 625 passed / 163 skipped /
25 xfailed.
This commit is contained in:
1 parent
b098904228
commit
9b159253c6
8 files changed
+614
-3
No files matched your search
@@ -0,0 +1,295 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build and verify `rasc`, the Rust re-implementation of ASC.
|
||||
|
||||
Why this script exists: `rasc` has no prebuilt artifact anywhere -- no GitHub release asset, no
|
||||
crate, and `cargo install rasc` fetches an unrelated maths parser. Adopting it therefore means
|
||||
building it, and a build that only one machine has ever performed is a build nobody else can
|
||||
reproduce. This wraps the whole path: toolchain check, clone, build, and the smoke tests that decide
|
||||
whether the binary is usable rather than merely present.
|
||||
|
||||
What it is for, and where it stops: `rasc` answers the same *location* questions as the Python
|
||||
`droidasc` -- which classes exist, where a string/type/method/field is referenced, what the manifest
|
||||
says, and the decompiled source of one class -- several times faster, with a Rust DEX decompiler
|
||||
(droidsaw) behind `getclass`. It is not a replacement for a full decompiler on every class shape;
|
||||
see `skills/apk-reverse/references/rasc-and-droidsaw.md` for the measured boundary.
|
||||
|
||||
Usage:
|
||||
python rasc_build.py --check # is a usable rasc present? (fast)
|
||||
python rasc_build.py --build # clone + build into a local tools/ tree
|
||||
python rasc_build.py --verify <apk> # compare against droidasc on a real APK
|
||||
python rasc_build.py --json
|
||||
|
||||
Requires, to build: git, and a Rust toolchain (rustup). On Windows the GNU host toolchain needs a
|
||||
64-bit MinGW-w64 gcc on PATH -- the 32-bit MinGW that ships with some setups cannot link a 64-bit
|
||||
binary and fails with "64-bit mode not compiled in".
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
def _repo_root():
|
||||
"""Walk up from this script to the repository root, or None when there is not one.
|
||||
|
||||
The script lives at `<repo>/skills/apk-reverse/scripts/`, so `tools/_work` is three levels up --
|
||||
but a skill installed by `npx skills add` has no repository around it at all. Resolving
|
||||
`tools/_work` relative to the *script* directory (the first version of this did) looks for
|
||||
`skills/apk-reverse/scripts/tools/_work`, which never exists, and then reports a working binary
|
||||
as missing.
|
||||
"""
|
||||
cur = HERE
|
||||
for _ in range(6):
|
||||
if os.path.isdir(os.path.join(cur, 'tools', '_work')) or \
|
||||
os.path.isdir(os.path.join(cur, '.git')):
|
||||
return cur
|
||||
parent = os.path.dirname(cur)
|
||||
if parent == cur:
|
||||
break
|
||||
cur = parent
|
||||
return None
|
||||
|
||||
|
||||
ROOT = _repo_root()
|
||||
# Falls back to a user-level directory when the skill runs outside a checkout, so the tool is not
|
||||
# pinned to a layout the caller may not have.
|
||||
DEFAULT_WORK = (os.path.join(ROOT, 'tools', '_work') if ROOT
|
||||
else os.path.join(os.path.expanduser('~'), '.apk-reverse', 'work'))
|
||||
|
||||
# A cargo/rustc installed to a non-default CARGO_HOME is invisible to a non-interactive PATH, which
|
||||
# is how this was first run: the toolchain was at <work>/rust/cargo/bin and the check said "missing".
|
||||
TOOLCHAIN_HINTS = (
|
||||
os.path.join(DEFAULT_WORK, 'rust', 'cargo', 'bin'),
|
||||
os.path.expanduser('~/.cargo/bin'),
|
||||
)
|
||||
for _hint in TOOLCHAIN_HINTS:
|
||||
if os.path.isdir(_hint) and _hint not in os.environ.get('PATH', ''):
|
||||
os.environ['PATH'] = _hint + os.pathsep + os.environ.get('PATH', '')
|
||||
|
||||
REPO = 'https://github.com/MG1937/ASC.git'
|
||||
BRANCH = 'rust'
|
||||
RASC_HINTS = (
|
||||
os.environ.get('RASC', ''),
|
||||
os.path.join(DEFAULT_WORK, 'rust', 'target', 'release', 'rasc'),
|
||||
os.path.join(DEFAULT_WORK, 'rust', 'target', 'release', 'rasc.exe'),
|
||||
'rasc',
|
||||
)
|
||||
|
||||
EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_CAPABILITY, EXIT_INTERNAL = 0, 1, 2, 3, 4
|
||||
|
||||
|
||||
def find_rasc():
|
||||
for hint in RASC_HINTS:
|
||||
if not hint:
|
||||
continue
|
||||
if os.path.isabs(hint):
|
||||
if os.path.isfile(hint):
|
||||
return hint
|
||||
continue
|
||||
found = shutil.which(hint)
|
||||
if found:
|
||||
return found
|
||||
return None
|
||||
|
||||
|
||||
def rasc_version(path):
|
||||
try:
|
||||
out = subprocess.run([path, '--version'], capture_output=True, text=True, timeout=60)
|
||||
except (OSError, subprocess.SubprocessError) as exc:
|
||||
return None, str(exc)
|
||||
if out.returncode != 0:
|
||||
return None, (out.stderr or out.stdout).strip()[:200]
|
||||
return (out.stdout or '').strip(), ''
|
||||
|
||||
|
||||
def toolchain():
|
||||
"""What this machine has that a build needs, reported rather than assumed."""
|
||||
report = {}
|
||||
for name in ('git', 'cargo', 'rustc', 'cc', 'gcc'):
|
||||
report[name] = shutil.which(name)
|
||||
if report['gcc']:
|
||||
try:
|
||||
out = subprocess.run([report['gcc'], '-dumpmachine'], capture_output=True, text=True,
|
||||
timeout=60)
|
||||
report['gcc_target'] = (out.stdout or '').strip()
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
report['gcc_target'] = 'unknown'
|
||||
return report
|
||||
|
||||
|
||||
def build(workdir, jobs=None):
|
||||
repo = os.path.join(workdir, 'rasc')
|
||||
if not os.path.isdir(os.path.join(repo, '.git')):
|
||||
os.makedirs(workdir, exist_ok=True)
|
||||
cmd = ['git', 'clone', '--branch', BRANCH, '--depth', '1', REPO, repo]
|
||||
print('+ %s' % ' '.join(cmd))
|
||||
rc = subprocess.run(cmd).returncode
|
||||
if rc != 0:
|
||||
print('clone failed (rc=%d)' % rc, file=sys.stderr)
|
||||
return None
|
||||
env = dict(os.environ)
|
||||
env.setdefault('CARGO_TARGET_DIR', os.path.join(workdir, 'rust', 'target'))
|
||||
cmd = ['cargo', 'build', '--release'] + (['--jobs', str(jobs)] if jobs else [])
|
||||
print('+ %s (in %s)' % (' '.join(cmd), repo))
|
||||
t0 = time.perf_counter()
|
||||
rc = subprocess.run(cmd, cwd=repo, env=env).returncode
|
||||
print('build finished in %.1f s (rc=%d)' % (time.perf_counter() - t0, rc))
|
||||
if rc != 0:
|
||||
return None
|
||||
for name in ('rasc', 'rasc.exe'):
|
||||
candidate = os.path.join(env['CARGO_TARGET_DIR'], 'release', name)
|
||||
if os.path.isfile(candidate):
|
||||
return candidate
|
||||
return None
|
||||
|
||||
|
||||
def smoke(binary, apk):
|
||||
"""Ask the same question of rasc and of the Python `droidasc`, and compare the answers.
|
||||
|
||||
Presence is not capability: the check that matters is whether the class-definition set agrees on
|
||||
a real archive. A build that runs but answers differently is a build that must not be adopted,
|
||||
which is why this lives next to the build rather than in a note.
|
||||
"""
|
||||
if not os.path.isfile(apk):
|
||||
return {'result': 'usage', 'detail': 'apk not found: %s' % apk}
|
||||
ours = subprocess.run([binary, 'classes', apk], capture_output=True, timeout=1800)
|
||||
if ours.returncode != 0:
|
||||
return {'result': 'failed', 'detail': 'rasc classes rc=%d %s'
|
||||
% (ours.returncode, ours.stderr.decode('utf-8', 'replace')[:200])}
|
||||
py = shutil.which('droidasc')
|
||||
if not py:
|
||||
n = len(re.findall(rb'L[^;|\s]+;', ours.stdout))
|
||||
return {'result': 'partial', 'rasc_classes': n,
|
||||
'detail': 'droidasc not on PATH, so only the rasc side was run'}
|
||||
env = dict(os.environ, PYTHONIOENCODING='utf-8', PYTHONUTF8='1')
|
||||
theirs = subprocess.run([py, 'listclass', apk], capture_output=True, timeout=1800, env=env)
|
||||
if theirs.returncode != 0:
|
||||
return {'result': 'partial', 'detail': 'droidasc rc=%d (its output is not UTF-8 safe on a '
|
||||
'non-UTF-8 console without PYTHONUTF8=1)'
|
||||
% theirs.returncode}
|
||||
|
||||
def descriptors(blob):
|
||||
out = set()
|
||||
for line in blob.decode('utf-8', 'replace').splitlines():
|
||||
for tok in re.split(r'[\s|]+', line.strip()):
|
||||
if re.fullmatch(r'L[^;]+;', tok):
|
||||
out.add(tok)
|
||||
break
|
||||
return out
|
||||
|
||||
a, b = descriptors(ours.stdout), descriptors(theirs.stdout)
|
||||
return {'result': 'ok' if a == b else 'failed',
|
||||
'rasc_classes': len(a), 'droidasc_classes': len(b),
|
||||
'only_rasc': len(a - b), 'only_droidasc': len(b - a)}
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
ap = argparse.ArgumentParser(
|
||||
prog='rasc_build.py',
|
||||
description='Build and verify rasc, the Rust ASC re-implementation.',
|
||||
epilog='exit codes: 0 ok, 1 verification failed, 2 usage, 3 a required capability is '
|
||||
'missing, 4 internal error\n'
|
||||
'examples:\n'
|
||||
' rasc_build.py --check\n'
|
||||
' rasc_build.py --build --work tools/_work\n'
|
||||
' rasc_build.py --verify tools/_work/apks/sample.apk\n')
|
||||
ap.add_argument('--check', action='store_true', help='report whether a usable rasc is present')
|
||||
ap.add_argument('--build', action='store_true', help='clone and build rasc')
|
||||
ap.add_argument('--verify', metavar='APK', help='compare rasc against droidasc on this APK')
|
||||
ap.add_argument('--work', default=DEFAULT_WORK, help='where to clone and build (default: %s)'
|
||||
% DEFAULT_WORK)
|
||||
ap.add_argument('--jobs', type=int, default=None, help='parallel build jobs')
|
||||
ap.add_argument('--json', action='store_true')
|
||||
args = ap.parse_args(argv)
|
||||
|
||||
if not (args.check or args.build or args.verify):
|
||||
ap.print_help()
|
||||
print('RESULT=usage')
|
||||
return EXIT_USAGE
|
||||
|
||||
payload = {'toolchain': {k: v for k, v in toolchain().items()}, 'work': args.work}
|
||||
binary = find_rasc()
|
||||
if binary:
|
||||
version, err = rasc_version(binary)
|
||||
payload['rasc'] = {'path': binary, 'version': version, 'error': err or None}
|
||||
else:
|
||||
payload['rasc'] = None
|
||||
|
||||
if args.build:
|
||||
missing = [k for k in ('git', 'cargo', 'rustc') if not payload['toolchain'][k]]
|
||||
if missing:
|
||||
payload['result'] = 'capability_missing'
|
||||
payload['next_action'] = ('install Rust (rustup) and git; missing: %s' % ', '.join(missing))
|
||||
if args.json:
|
||||
print(json.dumps(payload, indent=2))
|
||||
print('cannot build: missing %s' % ', '.join(missing), file=sys.stderr)
|
||||
print('RESULT=capability_missing')
|
||||
return EXIT_CAPABILITY
|
||||
target = payload['toolchain'].get('gcc_target') or ''
|
||||
built = build(args.work, args.jobs)
|
||||
if not built:
|
||||
payload['result'] = 'build_failed'
|
||||
payload['next_action'] = ('read the cargo output above; on Windows the GNU host '
|
||||
'toolchain needs a 64-bit MinGW-w64 gcc, and a 32-bit MinGW '
|
||||
'fails with "64-bit mode not compiled into"')
|
||||
if args.json:
|
||||
print(json.dumps(payload, indent=2))
|
||||
print('RESULT=build_failed')
|
||||
return EXIT_FAILED
|
||||
binary = built
|
||||
version, _ = rasc_version(binary)
|
||||
payload['rasc'] = {'path': built, 'version': version, 'error': None}
|
||||
|
||||
if args.verify:
|
||||
if not binary:
|
||||
payload['result'] = 'capability_missing'
|
||||
payload['next_action'] = 'run --build first, or set RASC to an existing binary'
|
||||
if args.json:
|
||||
print(json.dumps(payload, indent=2))
|
||||
print('RESULT=capability_missing')
|
||||
return EXIT_CAPABILITY
|
||||
payload['verify'] = smoke(binary, args.verify)
|
||||
payload['result'] = payload['verify']['result']
|
||||
|
||||
if args.check and 'result' not in payload:
|
||||
payload['result'] = 'ok' if binary else 'capability_missing'
|
||||
if not binary:
|
||||
payload['next_action'] = 'run --build (needs git + rustup), or set RASC=<path>'
|
||||
|
||||
if args.json:
|
||||
print(json.dumps(payload, indent=2))
|
||||
else:
|
||||
tc = payload['toolchain']
|
||||
print('toolchain : git=%s cargo=%s rustc=%s gcc=%s%s'
|
||||
% (tc.get('git') or '-', tc.get('cargo') or '-', tc.get('rustc') or '-',
|
||||
tc.get('gcc') or '-',
|
||||
('(%s)' % tc['gcc_target']) if tc.get('gcc_target') else ''))
|
||||
print('rasc : %s' % (('%s %s' % (payload['rasc']['path'], payload['rasc']['version']))
|
||||
if payload['rasc'] else 'not found'))
|
||||
if 'verify' in payload:
|
||||
print('verify : %s' % payload['verify'])
|
||||
print('result : %s' % payload['result'])
|
||||
if payload.get('next_action'):
|
||||
print('next : %s' % payload['next_action'])
|
||||
|
||||
print('RESULT=%s' % payload['result'])
|
||||
return {'ok': EXIT_OK, 'build_failed': EXIT_FAILED, 'failed': EXIT_FAILED,
|
||||
'usage': EXIT_USAGE, 'capability_missing': EXIT_CAPABILITY,
|
||||
'partial': EXIT_OK}.get(payload['result'], EXIT_INTERNAL)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if hasattr(sys.stdout, 'reconfigure'):
|
||||
try:
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
except (ValueError, OSError):
|
||||
pass
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user