Worked end-to-end on a real unobfuscated commercial app (single-dex, no packer,
server-driven launch screen) and folded back everything that cost time. Net +17
files, no new dependencies, no sample-specific identifiers.
New references
byte-level-patching.md
The technique that was missing entirely. Equal-length in-place edits beat
method rebuilding (measured on a real sample: method rewrite grew debug_info
924 B -> 22.8 KB and the dex 4.32 MB -> 7.73 MB), but four failure modes are
silent: same-length is not enough, branch polarity, verifier legality, and
stale dex header fields. Covers locating an instruction's exact offset (why
listings and byte-pattern guesses both fail), the instruction width table and
the two traps that desynchronise a decode, neutralise-a-branch vs redirect-it,
and the header's signature-then-checksum order.
server-config-and-updates.md
The most common shape of "ad" in a modern app, and the one usually
mis-diagnosed: the server supplies UI the client renders. The two-layer fetch
(local fallback then remote override) that proves it, finding the config DTOs
through the field names data classes keep, why you patch the decision and not
the data, deciding the scope of "remove", and remote re-enable / cached config
durability.
New scripts
dexutil.py dependency-free dex reader: structural walk, exact
instruction decode, dex header recompute/verify in the
correct order, branch-target and operand helpers
dex_find_insn.py locate an instruction by decoded semantics, print its exact
byte offset with context and BOTH sides of any branch
dex_patch_bytes.py equal-length byte patches from a JSON spec: semantic match,
polarity pin via expect_next, equal-length enforcement,
verifier check, header recompute, re-decode to prove it landed
dex_check_verifier.py does any conditional branch target a move-result (bypassing
its producer)? Compares builds and separates pre-existing
findings from regressions the patch introduced
coldstart.py cold-launch capture: timed screenshot burst + logcat signals
+ installed-build facts + launch timing, and a warning when
the foreground activity is not the app
check_repo.py maintenance check: every script parses and answers --help,
every referenced path resolves, every file is documented
Fixes found while running the above
repack.py wrote archives with a plain zipfile writer, which cannot satisfy
the Android R+ install gate: resources.arsc must be STORED *and*
4-byte aligned, and a zip extra record cannot express a 1-3 byte
pad (a stored filler entry is inserted instead). Also fixes the
central-directory pointer and adds an alignment gate to the output.
usb_net_proxy.py, dex_strpatch.py
crashed on --help with a traceback instead of printing usage.
Updated
toolchain.md ddc added (single-binary dex->Java with query subcommands,
with measured timings and its real limits); "not on PATH is
not not-installed" with the search commands; which signer
to use and why jarsigner breaks alignment
repack-and-sign.md alignment requirement and its arithmetic; dex header
recompute; jarsigner hazard; vendor install interception
([N] with no symbolic constant -> root pm install); the
installer can keep owning the screen after an install
recon.md resolve package identity from a tool, not from hand-read
AXML strings (a wrong package sends all later device queries
nowhere); snapshot the config surface during recon
long-task-discipline.md captures you never looked at are not evidence (incl. the
consistent-but-wrong-window variant); long-context decay and
the record shape that survives it
dynamic-frida.md when live attach cannot work at all: static instrumentation,
static read, and self-recorded evidence, with the control
build caveat each one carries
ad-removal.md server-driven config is usually the EASIEST case, not the
hardest; a three-way zero result is a positive finding;
scope note on local brand splash vs promotional content
pitfalls.md P37 stale dex header checksum, P38 [-124] alignment,
P39 bare-numeric install failure, P40 wrong-window evidence,
P41 inverted branch polarity, P42 decode desynchronisation
SKILL.md, README.md symptom index, reference/script indexes and requirements
updated for all of the above
134 lines
5.0 KiB
Python
134 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Byte-level, in-place patch of a string constant inside a dex — no smali round-trip.
|
|
|
|
Why not a smali round-trip: a disassemble -> reassemble of a whole tree can damage
|
|
R8-optimized output in ways the class table does not show. Observed symptom: a synthetic
|
|
access bridge lost its interface/class relationship, producing
|
|
IncompatibleClassChangeError ("Found interface X, but class was expected") at runtime even
|
|
though class_defs and access_flags looked untouched. See references/pitfalls.md P3.
|
|
|
|
What this script does instead (zero structural change to the dex):
|
|
1. Replaces the target string with another of **exactly equal byte length**. Equal length
|
|
keeps the string_data_item uleb128 length prefix unchanged, so offsets, indices and
|
|
every table stay exactly where they were.
|
|
2. Recomputes the dex header signature (SHA-1, from offset 32) and checksum (adler32,
|
|
from offset 12).
|
|
|
|
Use only when the replacement can be equal-length. Otherwise use a method-level dex API
|
|
rewrite (references/dex-patching.md, technique 1).
|
|
|
|
Usage: python dex_strpatch.py <in.dex> <out.dex> <old_str> <new_str>
|
|
Requires len(new) == len(old) in UTF-8 bytes, and exactly one occurrence of old in the dex.
|
|
"""
|
|
import hashlib
|
|
import struct
|
|
import sys
|
|
import zlib
|
|
|
|
|
|
def _uleb128(data, off):
|
|
result = 0
|
|
shift = 0
|
|
while True:
|
|
b = data[off]
|
|
off += 1
|
|
result |= (b & 0x7F) << shift
|
|
if not (b & 0x80):
|
|
break
|
|
shift += 7
|
|
return result, off
|
|
|
|
|
|
def _read_strings(data):
|
|
off = struct.unpack('<I', data[0x3C:0x40])[0]
|
|
size = struct.unpack('<I', data[0x38:0x3C])[0]
|
|
out = []
|
|
for i in range(size):
|
|
sdata_off = struct.unpack('<I', data[off + i * 4: off + i * 4 + 4])[0]
|
|
n, p = _uleb128(data, sdata_off)
|
|
out.append((sdata_off, bytes(data[p:p + n])))
|
|
return out
|
|
|
|
|
|
def _order_ok(data, off, ob, nb):
|
|
"""Check whether replacing `ob` with `nb` still satisfies the string_ids ordering rule.
|
|
|
|
Note: string_ids stores the offset of each string_data_item — i.e. the position of the
|
|
**length prefix** (a few bytes ahead of the UTF-8 payload), not of the text itself — so
|
|
matching by string-data offset directly is not possible; this matches the entry by
|
|
content instead.
|
|
"""
|
|
entries = _read_strings(data)
|
|
idx = None
|
|
for i, (_sdata_off, raw) in enumerate(entries):
|
|
if raw == ob:
|
|
idx = i
|
|
break
|
|
if idx is None:
|
|
print('[order] target string not found in string_ids, skip check')
|
|
return True
|
|
prev = entries[idx - 1][1] if idx > 0 else None
|
|
nxt = entries[idx + 1][1] if idx + 1 < len(entries) else None
|
|
if prev is not None and nb <= prev:
|
|
print('[order] FAIL: new %r <= prev %r' % (nb, prev))
|
|
return False
|
|
if nxt is not None and nb >= nxt:
|
|
print('[order] FAIL: new %r >= next %r' % (nb, nxt))
|
|
return False
|
|
print('[order] OK: %r < %r < %r' % (prev, nb, nxt))
|
|
return True
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) > 1 and sys.argv[1] in ('-h', '--help'):
|
|
print(__doc__)
|
|
return 0
|
|
if len(sys.argv) < 5:
|
|
print('error: needs 4 arguments, got %d\n' % (len(sys.argv) - 1))
|
|
print(__doc__)
|
|
return 2
|
|
src, dst, old, new = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
|
|
ob, nb = old.encode('utf-8'), new.encode('utf-8')
|
|
if len(ob) != len(nb):
|
|
print('[FAIL] length mismatch: %d vs %d (must be equal)' % (len(ob), len(nb)))
|
|
return 1
|
|
data = bytearray(open(src, 'rb').read())
|
|
if data[:8] != b'dex\n035\x00' and data[:4] != b'dex\n':
|
|
print('[warn] unexpected magic: %r' % bytes(data[:8]))
|
|
n = data.count(ob)
|
|
print('[info] occurrences of %r = %d' % (old, n))
|
|
if n != 1:
|
|
print('[FAIL] expected exactly 1 occurrence')
|
|
return 1
|
|
off = data.find(ob)
|
|
|
|
# The dex spec requires string_ids to be ordered by string content. An equal-length
|
|
# replacement moves no offset, but it does change where this entry sits in that ordering
|
|
# — and once it lands out of order the whole dex is rejected by the ClassLoader
|
|
# (symptom: ClassNotFoundException naming the Application class). So validate the
|
|
# interval before writing.
|
|
if not _order_ok(data, off, ob, nb):
|
|
print('[FAIL] replacement would break string_ids ordering. '
|
|
'Pick a string that stays between its two neighbours.')
|
|
return 1
|
|
|
|
data[off:off + len(ob)] = nb
|
|
print('[ok] patched at offset 0x%x: %r -> %r' % (off, old, new))
|
|
|
|
# signature = SHA-1 over data[32:], stored at 12..32
|
|
sig = hashlib.sha1(bytes(data[32:])).digest()
|
|
data[12:32] = sig
|
|
# checksum = adler32 over data[12:], stored at 8..12 (little endian)
|
|
chk = zlib.adler32(bytes(data[12:])) & 0xFFFFFFFF
|
|
data[8:12] = struct.pack('<I', chk)
|
|
print('[ok] signature=%s checksum=0x%08x' % (sig.hex(), chk))
|
|
|
|
open(dst, 'wb').write(bytes(data))
|
|
print('[ok] wrote %s (%d bytes)' % (dst, len(data)))
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|