Files
myrzx_bot/analysis/loader_audit/raw_compare_20261107_01.py

119 lines
5.8 KiB
Python

"""Compare immutable loader inputs and write a separate three-byte diagnostic candidate."""
from pathlib import Path
import hashlib
import json
import re
import struct
import zipfile
root = Path(__file__).resolve().parents[2]
outdir = Path(__file__).parent
audit_text = (outdir / "bytecode_20261107_01.txt").read_text(encoding="utf-8-sig")
sections = re.split(r"(?m)^FILE ", audit_text)[1:]
sections = {section.split(" bytes=", 1)[0]: section for section in sections}
sha = lambda data: hashlib.sha256(data).hexdigest()
original = (root / "analysis/MYRDyd.swf").read_bytes()
modified = (root / "analysis/MYRDyd_packaged_main.swf").read_bytes()
assert original[:3] == modified[:3] == b"FWS"
def body_rows(section):
return {(int(tag), int(body)): (method, name, size, digest, stack, regs, scope, exceptions)
for tag, body, method, name, size, digest, stack, regs, scope, exceptions in re.findall(
r"(?m)^BODY tagPos=(\d+) body=(\d+) method=(\d+) name=(.*?) bytes=(\d+) sha256=(\w+) maxStack=(\d+) regs=(\d+) scope=(\S+) exceptions=(.*?) offsetErrors=", section)}
left = body_rows(sections["analysis/MYRDyd.swf"])
right = body_rows(sections["analysis/MYRDyd_packaged_main.swf"])
changed = [{"tag_pos": key[0], "body": key[1], "original": left[key], "modified": right[key]}
for key in left.keys() | right.keys() if left.get(key) != right.get(key)]
assert len(changed) == 1 and changed[0]["body"] == 43
def fix_bytes(section):
return bytes.fromhex(re.search(r"(?m)^BODY .*name=fixComplete .*\nRAW_CODE ([0-9a-f]+)", section)[1])
oldcode = fix_bytes(sections["analysis/MYRDyd.swf"])
badcode = fix_bytes(sections["analysis/MYRDyd_packaged_main.swf"])
start = original.index(oldcode)
assert original.find(oldcode, start + 1) == -1 and modified.index(badcode) == start
assert original[start - 6:start] == bytes([44, 4, 3, 0, 1, 104])
assert modified[start - 6:start] == bytes([44, 4, 3, 0, 1, 101])
assert original[start + 0x22:start + 0x25] == bytes.fromhex("46 2b 01")
candidate = bytearray(original)
candidate[start + 0x22:start + 0x25] = bytes.fromhex("2b 29 02")
changed_offsets = [i for i, (before, after) in enumerate(zip(original, candidate)) if before != after]
assert changed_offsets == list(range(start + 0x22, start + 0x25))
destination = outdir / "MYRDyd_resolver_bypass_diagnostic_20261107_01.swf"
with destination.open("xb") as stream:
stream.write(candidate)
def tags(data):
nbits = data[8] >> 3
pos = 8 + (5 + 4 * nbits + 7) // 8 + 4
result = []
while pos < len(data):
tag_offset = pos
rec, = struct.unpack_from("<H", data, pos)
pos += 2
kind, size = rec >> 6, rec & 63
if size == 63:
size, = struct.unpack_from("<I", data, pos)
pos += 4
payload = data[pos:pos + size]
item = {"kind": kind, "tag_offset": tag_offset, "payload_offset": pos, "length": size, "sha256": sha(payload)}
if kind == 82:
zero = payload.index(0, 4)
item.update(abc_name=payload[4:zero].decode("utf-8"), abc_start=pos + zero + 1)
if kind == 76:
count, = struct.unpack_from("<H", payload)
at = 2
symbols = []
for _ in range(count):
symbol, = struct.unpack_from("<H", payload, at)
at += 2
end = payload.index(0, at)
symbols.append({"symbol_id": symbol, "name": payload[at:end].decode("utf-8")})
at = end + 1
item["symbols"] = symbols
result.append(item)
pos += size
if kind == 0:
break
assert pos == len(data)
return result
tag_rows = {"original": tags(original), "modified": tags(modified), "candidate": tags(candidate)}
original_apk = next(p for p in sorted(root.glob("*.apk")) if p.name == "牧羊人之心_1.9.68.apk")
with zipfile.ZipFile(original_apk) as archive:
ui_name = "assets/ui/myrLoader.swf"
ui = archive.read(ui_name)
ui_path = outdir / "myrLoader_ui_inventory_20261107_01.swf"
with ui_path.open("xb") as stream:
stream.write(ui)
ui_members = []
for path in [*sorted(root.glob("*.apk")), root / "analysis/device_after_two_mods.apk"]:
with zipfile.ZipFile(path) as archive:
data = archive.read(ui_name)
ui_members.append({"apk": str(path.relative_to(root)), "size": len(data), "sha256": sha(data), "matches_original": data == ui})
result = {
"changed_method_bodies": changed,
"code_start": start, "code_start_hex": hex(start),
"method_header_offset": start - 6, "max_stack_offset": start - 5,
"method_header_original": original[start - 6:start].hex(" "),
"method_header_modified": modified[start - 6:start].hex(" "),
"original_size": len(original), "modified_size": len(modified),
"original_sha256": sha(original), "modified_sha256": sha(modified),
"method_region_original": oldcode[0x1a:0x27].hex(" "),
"method_region_modified": badcode[0x1a:0x24].hex(" "),
"prefix_identical_except_header_and_lengths": [(i, a, b) for i, (a, b) in enumerate(zip(original[:start], modified[:start])) if a != b],
"suffix_identical_after_method": original[start + len(oldcode):] == modified[start + len(badcode):],
"candidate": {"path": str(destination.relative_to(root)), "sha256": sha(candidate), "size": len(candidate),
"changed_offsets": changed_offsets, "changed_offsets_hex": [hex(i) for i in changed_offsets],
"before": original[start + 0x22:start + 0x25].hex(" "), "after": candidate[start + 0x22:start + 0x25].hex(" ")},
"tags": tag_rows,
"ui_comparison": ui_members,
}
with (outdir / "raw_compare_20261107_01.json").open("x", encoding="utf-8") as stream:
json.dump(result, stream, indent=2)
print(json.dumps({key: value for key, value in result.items() if key not in ("tags", "ui_comparison")}, indent=2))
print("UI_COMPARISON " + json.dumps(ui_members, ensure_ascii=True))