"""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("> 6, rec & 63 if size == 63: size, = struct.unpack_from("