mirror of
https://codeberg.org/dabao1955/apk-reverse.git
synced 2026-11-30 03:50:22 +08:00
find_refs: read .dex directly, and stop reporting unreadable input as zero refs
The docstring promised "a smali tree or a directory of dex files" and delivered neither: iter_files() yielded only .smali, a direct .dex was read as UTF-8 text and regex-matched against smali syntax, and a dex-only directory yielded nothing at all. A real dex, a dex directory and a typo path all printed [total refs] 0 and a note blaming the needle, so the one tool the skill recommends for judging blast radius before patching returned a confident false zero. A .dex is now decoded through dexutil (no baksmali needed), a directory is walked for .smali and .dex alike, an archive is opened for its classes*.dex, input that cannot be read is refused with exit 2 and a reason, and every run prints [scanned] N file(s) so the two cases can never print identically again. Also corrects the recorded exit code for this defect: it was 1, not 0, which moves it from "no signal at all" to "the signal was there and the message contradicted it". Verified against a fixture whose call graph is known by construction (docs/tool-verification/fixtures/make_fixture_dex.py, committed as the evidence): the .dex and the equivalent smali tree both return 3/2/2, matching what was written. And against the real target: Ljava/lang/String;->length gives 356 refs in 2.3 s where it previously gave 0; a five-dex directory gives 9,472; a typo path exits 2. Docs updated with it: F9 records the opcode-table finding the fix exposed, F2/F3 and TOOL-VERDICTS.md are corrected, and dex-patching.md now tells the reader to look at the [scanned] line before the count.
This commit is contained in:
1 parent
03485faa2a
commit
5fa80a5bea
7 files changed
+664
-56
No files matched your search
@@ -489,7 +489,7 @@ All scripts are parameterized and path-agnostic; pass paths explicitly. Run `--h
|
||||
| `scripts/dart_pprefs.py` | Build/query the object-pool offset -> code-site index for a Dart AOT snapshot (arithmetic decode; seconds, not minutes) |
|
||||
| `scripts/dart_pool_strings.py` | Recover string literals from a Dart AOT snapshot: framed entries, the one-byte vs UTF-16 split, file offsets, and a run-length noise filter |
|
||||
| `scripts/dart_disasm.py` | Annotated windowed disassembly of Dart AOT code (pool + boolean annotations) plus a B/BL caller index |
|
||||
| `scripts/find_refs.py` | Count and list callers of a method/field (blast-radius check) |
|
||||
| `scripts/find_refs.py` | Count and list callers of a method/field (blast-radius check). Takes a smali tree, a `.dex`, a directory of either, or an `.apk`. Always prints what it scanned, so "the input was unreadable" cannot be mistaken for "nothing references it" |
|
||||
| `scripts/repack.py` | Rebuild an APK with replaced dex, strip only signatures, keep `META-INF/services/`, **write a 4-byte-aligned archive** (`resources.arsc` STORED+aligned, which Android R+ refuses to install without), sign, and verify |
|
||||
| `scripts/dexpatch/` | dexlib2 method-level rewriter (for changes that genuinely need new instructions) + build notes |
|
||||
| `scripts/devsh.py` | Quoting-safe ADB shell helper for rooted devices |
|
||||
|
||||
@@ -33,9 +33,15 @@ Ads and gates can be suppressed at several layers. Higher in this list = smaller
|
||||
## Before you patch a method: blast-radius check
|
||||
|
||||
```bash
|
||||
python scripts/find_refs.py <smali_tree_or_dex_dir> 'Lcom/pkg/Helper;->methodName(args)RetType'
|
||||
python scripts/find_refs.py <smali_tree|dex|dir|apk> 'Lcom/pkg/Helper;->methodName(args)RetType'
|
||||
```
|
||||
|
||||
Read the `[scanned]` line before the count. It reports how many inputs were actually opened, and
|
||||
`[scanned] 0 file(s)` exits 2 with the reason: an unsupported path, a typo, or a directory of archives
|
||||
is **not** the same answer as "read 2 dex files and found nothing", and the two once printed
|
||||
identically — which put a false zero on exactly the decision this check exists to protect. A dex input
|
||||
is decoded directly (`dexutil.py`), so baksmali is not required.
|
||||
|
||||
- **1–3 callers, all in the same feature area** → safe to patch.
|
||||
- **Many callers, or callers across unrelated packages** → it is a general utility. Do not patch it. Go one level up and patch the specific caller instead.
|
||||
- **Look at the parameters too.** If the signature mentions `Modifier`, `ContentScale`, `ColorFormat`, `Shape`, `View`, or a content-generic model type, it is not specific to your target.
|
||||
|
||||
@@ -8,17 +8,17 @@ ad-specific but is actually a general utility with dozens of callers.
|
||||
|
||||
Usage
|
||||
-----
|
||||
# references to a method, from a smali tree or a directory of dex files
|
||||
python find_refs.py <tree_or_dex_dir> 'Lcom/pkg/Helper;->showAd(Landroid/app/Activity;)V'
|
||||
# a smali tree, a single .dex, a directory holding either, or an .apk
|
||||
python find_refs.py <smali_tree|dex|dir|apk> 'Lcom/pkg/Helper;->showAd(Landroid/app/Activity;)V'
|
||||
|
||||
# a whole class (all of its members)
|
||||
python find_refs.py <tree_or_dex_dir> 'Lcom/pkg/Helper;'
|
||||
python find_refs.py <smali_tree|dex|dir|apk> 'Lcom/pkg/Helper;'
|
||||
|
||||
# a field
|
||||
python find_refs.py <tree_or_dex_dir> 'Lcom/pkg/Helper;->count:I'
|
||||
python find_refs.py <smali_tree|dex|dir|apk> 'Lcom/pkg/Helper;->count:I'
|
||||
|
||||
# just the counts, no listing
|
||||
python find_refs.py <tree_or_dex_dir> 'Lcom/pkg/Helper;->showAd' --count-only
|
||||
python find_refs.py <smali_tree|dex|dir|apk> 'Lcom/pkg/Helper;->showAd' --count-only
|
||||
|
||||
Reading the result
|
||||
------------------
|
||||
@@ -27,6 +27,20 @@ Reading the result
|
||||
Go one level up and patch the
|
||||
specific caller instead.
|
||||
|
||||
The first line is part of the answer, not decoration. `[scanned] 0 file(s)` means
|
||||
the input was never read -- an unsupported path, a typo, a directory with nothing
|
||||
this script can open -- and it is a different result from "read N files and found
|
||||
nothing". The two used to print identically, which put a false zero on exactly the
|
||||
decision this script exists to protect. Input that cannot be read now exits 2 and
|
||||
says why.
|
||||
|
||||
Input forms, precisely:
|
||||
.smali file text scan (no decoder needed)
|
||||
.dex file decoded with dexutil (no baksmali/jadx required)
|
||||
directory every .smali and .dex found under it, recursively
|
||||
.apk/.zip/... every classes*.dex inside the archive
|
||||
anything else refused with an error, rather than reported as "no references"
|
||||
|
||||
Also inspect the signature. If it mentions Modifier / ContentScale / Shape / View /
|
||||
ColorScheme or a content-generic model type, it is shared UI, not your target.
|
||||
"""
|
||||
@@ -34,19 +48,67 @@ import argparse
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from dexutil import ( # noqa: E402
|
||||
IFIELD_OPS, INVOKE_OPS, PFIELD_OPS, SFIELD_OPS, insn_units, load_dex, u16,
|
||||
)
|
||||
|
||||
TEXT_EXT = ('.smali',)
|
||||
DEX_EXT = ('.dex',)
|
||||
ARCHIVE_EXT = ('.apk', '.zip', '.jar', '.xapk', '.apks', '.apkm')
|
||||
REF_RE_TMPL = r'invoke[^\n]*%s|sget[^\n]*%s|sput[^\n]*%s|new-instance[^\n]*%s|check-cast[^\n]*%s'
|
||||
|
||||
FIELD_OPS = tuple(IFIELD_OPS) + tuple(PFIELD_OPS) + tuple(SFIELD_OPS)
|
||||
# new-instance (0x22) and check-cast (0x1F) both carry a type index at byte 2,
|
||||
# which is where the 21c format puts it.
|
||||
TYPE_OPS = (0x1F, 0x22)
|
||||
# classes.dex, classes2.dex, ... in an archive.
|
||||
ARCHIVE_DEX_RE = re.compile(r'(?:^|/)classes\d*\.dex$')
|
||||
|
||||
def iter_files(root):
|
||||
EXIT_FOUND = 0
|
||||
EXIT_NO_MATCH = 1
|
||||
EXIT_BAD_INPUT = 2
|
||||
|
||||
|
||||
def gather(root):
|
||||
"""Return (smali_paths, dex_paths, archive_paths).
|
||||
|
||||
Raises ValueError for input this script cannot read, so that "nothing was
|
||||
scanned" can never be mistaken for "nothing references it".
|
||||
"""
|
||||
if os.path.isdir(root):
|
||||
for dp, _, fs in os.walk(root):
|
||||
for fn in fs:
|
||||
smali, dexs = [], []
|
||||
for dp, _dirs, files in os.walk(root):
|
||||
for fn in sorted(files):
|
||||
path = os.path.join(dp, fn)
|
||||
if fn.endswith(TEXT_EXT):
|
||||
yield os.path.join(dp, fn)
|
||||
else:
|
||||
yield root
|
||||
smali.append(path)
|
||||
elif fn.endswith(DEX_EXT):
|
||||
dexs.append(path)
|
||||
if not smali and not dexs:
|
||||
raise ValueError(
|
||||
'no .smali and no .dex files under %s.\n'
|
||||
' A directory of .apk archives is not scanned; pass the '
|
||||
'.apk itself, or disassemble first (%s).'
|
||||
% (root, 'smtool.py'))
|
||||
return smali, dexs, []
|
||||
|
||||
if not os.path.exists(root):
|
||||
raise ValueError('no such file or directory: %s' % root)
|
||||
|
||||
low = root.lower()
|
||||
if low.endswith(TEXT_EXT):
|
||||
return [root], [], []
|
||||
if low.endswith(DEX_EXT):
|
||||
return [], [root], []
|
||||
if low.endswith(ARCHIVE_EXT):
|
||||
return [], [], [root]
|
||||
raise ValueError(
|
||||
'%s is not a .smali file, a .dex, a directory, or an archive.\n'
|
||||
' Accepted: .smali, .dex, a directory holding either, '
|
||||
'%s' % (root, '/'.join(ARCHIVE_EXT)))
|
||||
|
||||
|
||||
def method_owner(text, idx):
|
||||
@@ -58,10 +120,89 @@ def method_owner(text, idx):
|
||||
return text[head:end].strip()
|
||||
|
||||
|
||||
def scan_smali(path, pat):
|
||||
"""Return [(offset, owner, matched_text)] for one smali file."""
|
||||
try:
|
||||
with open(path, encoding='utf-8', errors='replace') as fh:
|
||||
text = fh.read()
|
||||
except OSError:
|
||||
return []
|
||||
return [(m.start(), method_owner(text, m.start()), m.group(0).strip())
|
||||
for m in pat.finditer(text)]
|
||||
|
||||
|
||||
def walk_insns(dex, code_off):
|
||||
"""Yield (offset, opcode) for one method body.
|
||||
|
||||
Deliberately not dex.decode(): this walks every instruction of every method
|
||||
in the dex, and the per-instruction dict decode() builds is not needed to
|
||||
read an operand index out of the raw bytes.
|
||||
"""
|
||||
info = dex.code_info(code_off)
|
||||
pos = info['insns_off']
|
||||
end = pos + info['insns_size'] * 2
|
||||
while pos < end:
|
||||
op = dex.d[pos]
|
||||
units = insn_units(op, dex.d, pos, end)
|
||||
if units < 1 or pos + units * 2 > end:
|
||||
return
|
||||
yield pos, op
|
||||
pos += units * 2
|
||||
|
||||
|
||||
def scan_dex(dex, needle):
|
||||
"""Return [(insn_off, owner, rendered)] for references inside one dex.
|
||||
|
||||
The rendered form is the same string the smali side matches textually
|
||||
(`Lcom/pkg/Helper;->showAd(Landroid/app/Activity;)V`), so both input forms
|
||||
answer the same question the same way.
|
||||
"""
|
||||
out = []
|
||||
for i in range(dex.header['class_defs_size']):
|
||||
cd_off = dex.header['class_defs_off'] + i * 32
|
||||
for _section, _midx, cls, name, desc, code_off in dex.methods_at(cd_off):
|
||||
if not code_off:
|
||||
continue
|
||||
owner = '%s->%s%s' % (cls, name, desc)
|
||||
for pos, op in walk_insns(dex, code_off):
|
||||
if op in INVOKE_OPS:
|
||||
cls2, nm, ds = dex.method(u16(dex.d, pos + 2))
|
||||
rendered = '%s->%s%s' % (cls2, nm, ds)
|
||||
elif op in FIELD_OPS:
|
||||
cls2, nm, ty = dex.field(u16(dex.d, pos + 2))
|
||||
rendered = '%s->%s:%s' % (cls2, nm, ty)
|
||||
elif op in TYPE_OPS:
|
||||
rendered = dex.type_(u16(dex.d, pos + 2))
|
||||
else:
|
||||
continue
|
||||
if needle in rendered:
|
||||
out.append((pos, owner, rendered))
|
||||
return out
|
||||
|
||||
|
||||
def dex_sources(path):
|
||||
"""Return [(label, Dex)] for a .dex file or every classes*.dex in an archive."""
|
||||
if path.lower().endswith(DEX_EXT):
|
||||
dex, _entry = load_dex(path)
|
||||
return [(path, dex)]
|
||||
with zipfile.ZipFile(path) as z:
|
||||
names = sorted(n for n in z.namelist() if ARCHIVE_DEX_RE.search(n))
|
||||
if not names:
|
||||
raise ValueError('no classes*.dex inside %s' % path)
|
||||
out = []
|
||||
for name in names:
|
||||
dex, _entry = load_dex(path, entry=name)
|
||||
out.append(('%s!%s' % (path, name), dex))
|
||||
return out
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(add_help=True)
|
||||
ap.add_argument('root')
|
||||
ap.add_argument('needle')
|
||||
ap = argparse.ArgumentParser(
|
||||
description='Count and list references to a method/field/class, in a smali '
|
||||
'tree, a .dex, a directory of either, or an .apk.')
|
||||
ap.add_argument('root', help='.smali file, .dex file, directory, or archive')
|
||||
ap.add_argument('needle', help='e.g. Lcom/pkg/Helper;->showAd(...)V, '
|
||||
'Lcom/pkg/Helper;, or Lcom/pkg/Helper;->count:I')
|
||||
ap.add_argument('--count-only', action='store_true')
|
||||
ap.add_argument('--max-print', type=int, default=60)
|
||||
a = ap.parse_args()
|
||||
@@ -70,42 +211,75 @@ def main():
|
||||
# match the needle plus one more char so we do not match a longer prefix
|
||||
pat = re.compile(REF_RE_TMPL % (esc, esc, esc, esc, esc))
|
||||
|
||||
total = 0
|
||||
per_file = []
|
||||
printed = 0
|
||||
for fp in iter_files(a.root):
|
||||
try:
|
||||
t = open(fp, encoding='utf-8', errors='replace').read()
|
||||
except Exception:
|
||||
continue
|
||||
hits = list(pat.finditer(t))
|
||||
if not hits:
|
||||
continue
|
||||
total += len(hits)
|
||||
per_file.append((len(hits), fp, hits, t))
|
||||
try:
|
||||
smali_paths, dex_paths, archives = gather(a.root)
|
||||
except ValueError as exc:
|
||||
print('[error] %s' % exc)
|
||||
return EXIT_BAD_INPUT
|
||||
|
||||
per_file.sort(reverse=True)
|
||||
per_file = [] # (count, label, hits)
|
||||
scanned = 0
|
||||
unreadable = []
|
||||
|
||||
for path in smali_paths:
|
||||
hits = scan_smali(path, pat)
|
||||
scanned += 1
|
||||
if hits:
|
||||
per_file.append((len(hits), path, hits))
|
||||
|
||||
for path in dex_paths + archives:
|
||||
try:
|
||||
sources = dex_sources(path)
|
||||
except (ValueError, OSError, zipfile.BadZipFile) as exc:
|
||||
unreadable.append('%s (%s)' % (path, exc))
|
||||
continue
|
||||
for label, dex in sources:
|
||||
problems = dex.check()
|
||||
if problems:
|
||||
unreadable.append('%s (%s)' % (label, problems[0]))
|
||||
continue
|
||||
hits = scan_dex(dex, a.needle)
|
||||
scanned += 1
|
||||
if hits:
|
||||
per_file.append((len(hits), label, hits))
|
||||
|
||||
if scanned == 0:
|
||||
print('[error] nothing readable was scanned under %s' % a.root)
|
||||
for u in unreadable:
|
||||
print(' unreadable: %s' % u)
|
||||
print(' Input was refused rather than reported as zero references, '
|
||||
'because the two are not the same answer.')
|
||||
return EXIT_BAD_INPUT
|
||||
|
||||
total = sum(c for c, _l, _h in per_file)
|
||||
per_file.sort(key=lambda row: row[0], reverse=True)
|
||||
print('[scanned] %d file(s) (%d smali, %d dex)'
|
||||
% (scanned, len(smali_paths), scanned - len(smali_paths)))
|
||||
print('[total refs] %d across %d files' % (total, len(per_file)))
|
||||
for u in unreadable:
|
||||
print('[unreadable] %s' % u)
|
||||
print()
|
||||
for cnt, fp, hits, t in per_file:
|
||||
rel = fp
|
||||
print('%-70s %d' % (rel, cnt))
|
||||
|
||||
printed = 0
|
||||
for cnt, label, hits in per_file:
|
||||
print('%-70s %d' % (label, cnt))
|
||||
if a.count_only:
|
||||
continue
|
||||
for m in hits:
|
||||
for off, owner, rendered in hits:
|
||||
if printed >= a.max_print:
|
||||
print(' ... (truncated)')
|
||||
return 0
|
||||
owner = method_owner(t, m.start())
|
||||
return EXIT_FOUND
|
||||
print(' in %s' % owner)
|
||||
print(' %s' % m.group(0).strip())
|
||||
print(' %s' % rendered)
|
||||
printed += 1
|
||||
|
||||
if total == 0:
|
||||
print('[note] no references found. Check the descriptor prefix "L...;" and '
|
||||
'exact obfuscated names, or disassemble the dex first with smtool.py.')
|
||||
return 1
|
||||
return 0
|
||||
print('[note] scanned %d file(s) and found no reference to %r. Check the '
|
||||
'descriptor prefix "L...;", the exact obfuscated name, and whether the '
|
||||
'behaviour lives in another dex or in native code.'
|
||||
% (scanned, a.needle))
|
||||
return EXIT_NO_MATCH
|
||||
return EXIT_FOUND
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in new issue
Block a user