import com.jpexs.decompiler.flash.SWF; import com.jpexs.decompiler.flash.abc.ABC; import com.jpexs.decompiler.flash.abc.ScriptPack; import com.jpexs.decompiler.flash.abc.avm2.AVM2Code; import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction; import com.jpexs.decompiler.flash.abc.types.MethodBody; import com.jpexs.decompiler.flash.abc.types.traits.TraitMethodGetterSetter; import com.jpexs.decompiler.flash.tags.Tag; import java.io.*; import java.nio.file.*; import java.security.MessageDigest; import java.util.*; import java.util.zip.InflaterInputStream; /** Read-only loader audit. Writes only stdout; never saves a SWF. */ public final class LoaderAudit_20261107_01 { static final Set DUMP = Set.of("fixComplete", "mainByteLoadComplete", "mainComplete", "loaderStart", "startVersionCheck", "fixVersionFileAutoLoadComplete", "fixVersionComplete", "addToStageHandler", "getStorageSrcIfExist", "getStorageFileIfExist"); static String sha(byte[] b) throws Exception { return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(b)); } static byte[] uncompressed(byte[] b) throws Exception { if (b[0] == 'F') return b; if (b[0] != 'C') throw new IOException("Unsupported SWF compression"); ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(b, 0, 8); try (var in = new InflaterInputStream(new ByteArrayInputStream(b, 8, b.length - 8))) { in.transferTo(out); } return out.toByteArray(); } static int[] find(byte[] hay, byte[] needle) { if (needle.length < 8) return new int[0]; ArrayList found = new ArrayList<>(); outer: for (int i = 0; i <= hay.length - needle.length; i++) { if (hay[i] != needle[0]) continue; for (int j = 1; j < needle.length; j++) if (hay[i+j] != needle[j]) continue outer; found.add(i); } return found.stream().mapToInt(Integer::intValue).toArray(); } static List offsets(MethodBody body) { AVM2Code code = body.getCode(); Set boundaries = new HashSet<>(); for (var ins : code.code) boundaries.add(ins.getAddress()); List problems = new ArrayList<>(); for (var ins : code.code) for (long target : ins.getOffsets()) { if (!boundaries.contains(target)) problems.add("BRANCH @" + ins.getAddress() + " -> " + target); } for (var ex : body.exceptions) { if (!boundaries.contains((long)ex.start) || (ex.end != body.getCodeBytes().length && !boundaries.contains((long)ex.end)) || !boundaries.contains((long)ex.target) || ex.start >= ex.end || ex.end > body.getCodeBytes().length) problems.add("EXCEPTION " + ex.start + ":" + ex.end + ":" + ex.target); } return problems; } static void dump(ABC abc, MethodBody body, byte[] rawSwf) throws Exception { byte[] bytes = body.getCodeBytes().clone(); AVM2Code code = body.getCode(); System.out.println("RAW_CODE " + HexFormat.of().formatHex(bytes)); System.out.println("RAW_CODE_SWF_OFFSETS " + Arrays.toString(find(rawSwf, bytes))); System.out.println("SERIALIZATION_IDENTICAL " + Arrays.equals(bytes, code.getBytes())); Map heights = new HashMap<>(); Map scopes = new HashMap<>(); Map instructions = new HashMap<>(); for (var ins : code.code) instructions.put(ins.getAddress(), ins); ArrayDeque work = new ArrayDeque<>(); heights.put(0L, 0); scopes.put(0L, body.init_scope_depth); work.add(0L); int maximum = 0, maximumScope = body.init_scope_depth; List problems = new ArrayList<>(); while (!work.isEmpty()) { long at = work.removeFirst(); var ins = instructions.get(at); if (ins == null) { problems.add("MISSING_INSTRUCTION " + at); continue; } int before = heights.get(at), scope = scopes.get(at); int pops = ins.definition.getStackPopCount(ins, abc), pushes = ins.definition.getStackPushCount(ins, abc); if (before < pops) problems.add("UNDERFLOW @" + at + " " + before + "<" + pops); int after = before - pops + pushes; int scopeAfter = scope + ins.definition.getScopeStackDelta(ins, abc); maximum = Math.max(maximum, Math.max(before, after)); maximumScope = Math.max(maximumScope, scopeAfter); List successors = new ArrayList<>(ins.getOffsets()); if (!ins.isExit() && !ins.isJump() && !ins.definition.instructionName.equals("lookupswitch")) successors.add(at + ins.getBytesLength()); for (long to : successors) { Integer prev = heights.putIfAbsent(to, after); scopes.putIfAbsent(to, scopeAfter); if (prev == null) work.add(to); else if (prev != after) problems.add("STACK_MERGE @" + to + " " + prev + " vs " + after); } } System.out.println("STACK_ANALYSIS computed_max=" + maximum + " declared_max=" + body.max_stack + " computed_scope=" + maximumScope + " declared_scope=" + body.max_scope_depth + " problems=" + problems); for (int i = 0; i < code.code.size(); i++) { var ins = code.code.get(i); int pop = ins.definition.getStackPopCount(ins, abc), push = ins.definition.getStackPushCount(ins, abc); Integer h = heights.get(ins.getAddress()); System.out.printf("%03d @%04x (%3d) %-22s stack=%s pop=%d push=%d %-85s %s%n", i, ins.getAddress(), ins.getAddress(), HexFormat.of().formatHex(ins.getBytes()), h == null ? "?" : h + "->" + (h-pop+push), pop, push, ins.toStringNoAddress(abc.constants, new ArrayList<>()), ins.getOffsets().isEmpty() ? "" : "TARGETS=" + ins.getOffsets()); } } public static void main(String[] args) throws Exception { for (String arg : args) { byte[] file = Files.readAllBytes(Path.of(arg)); byte[] rawSwf = uncompressed(file); System.out.println("FILE " + arg + " bytes=" + file.length + " sha256=" + sha(file) + " signature=" + new String(file, 0, 3, java.nio.charset.StandardCharsets.US_ASCII)); try (var in = new BufferedInputStream(new ByteArrayInputStream(file))) { SWF swf = new SWF(in, false); Map> packs = new LinkedHashMap<>(); for (ScriptPack pack : swf.getAS3Packs()) { String name = pack.getClassPath().toString(); packs.computeIfAbsent(pack.abc, k -> new ArrayList<>()).add(name); if (name.startsWith("net.pixvi.myrloader.")) System.out.println("LOADER_CLASS " + name); } // Main logic artifacts are inspected only for loader definitions, never battle bodies. if (arg.endsWith(".app") || arg.contains("_ui_inventory_")) { System.out.println("MAIN_LOADER_CLASS_INVENTORY_COMPLETE packs=" + packs.values().stream().mapToInt(List::size).sum()); continue; } int totalBodies = 0, branches = 0, exceptions = 0, bad = 0; for (var entry : packs.entrySet()) { ABC abc = entry.getKey(); Tag tag = (Tag)abc.parentTag; System.out.println("ABC tagPos=" + tag.getPos() + " rawBytes=" + tag.getOriginalDataLength() + " rawSha256=" + sha(tag.getOriginalData()) + " packs=" + entry.getValue()); for (int bi = 0; bi < abc.bodies.size(); bi++) { var body = abc.bodies.get(bi); String name = abc.constants.getString(abc.method_info.get(body.method_info).name_index); byte[] originalCode = body.getCodeBytes().clone(); List errors = offsets(body); totalBodies++; exceptions += body.exceptions.length; bad += errors.size(); for (var ins : body.getCode().code) branches += ins.getOffsets().size(); System.out.println("BODY tagPos=" + tag.getPos() + " body=" + bi + " method=" + body.method_info + " name=" + name + " bytes=" + originalCode.length + " sha256=" + sha(originalCode) + " maxStack=" + body.max_stack + " regs=" + body.max_regs + " scope=" + body.init_scope_depth + ":" + body.max_scope_depth + " exceptions=" + Arrays.toString(body.exceptions) + " offsetErrors=" + errors); boolean targeted = entry.getValue().stream().anyMatch(n -> n.equals("net.pixvi.myrloader.MYRLoaderStarter") || n.equals("net.pixvi.myrloader.MYRLoader_dyd") || n.equals("net.pixvi.myrloader.center.MyrUpdateCenter")); if (targeted && name != null && DUMP.contains(name)) dump(abc, body, rawSwf); } if (entry.getValue().contains("net.pixvi.myrloader.MYRLoaderStarter")) { int lookup = abc.findMethodBodyByName("net.pixvi.myrloader.MYRLoaderStarter", "fixComplete"); System.out.println("FIX_COMPLETE_NAME_LOOKUP body=" + lookup); for (var inst : abc.instance_info) for (var trait : inst.instance_traits.traits) if (trait instanceof TraitMethodGetterSetter tm && abc.constants.getString(abc.method_info.get(tm.method_info).name_index).equals("fixComplete")) System.out.println("FIX_COMPLETE_ACTUAL_TRAIT method=" + tm.method_info + " body=" + abc.findBodyIndex(tm.method_info)); } } System.out.println("OFFSET_SUMMARY bodies=" + totalBodies + " branchTargets=" + branches + " exceptionEntries=" + exceptions + " invalidOffsets=" + bad); } } } }