From 6c3d98702c9853003230e7463680562c37848f19 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 12 Oct 2019 21:58:54 +0100 Subject: [PATCH] Remove reset methods --- .../java/dev/openrs2/deob/Deobfuscator.java | 2 + .../deob/transform/ResetTransformer.java | 128 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 deob/src/main/java/dev/openrs2/deob/transform/ResetTransformer.java diff --git a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.java b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.java index 0272602d38..4d31c2ed4a 100644 --- a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.java +++ b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.java @@ -23,6 +23,7 @@ import dev.openrs2.deob.transform.FieldOrderTransformer; import dev.openrs2.deob.transform.OpaquePredicateTransformer; import dev.openrs2.deob.transform.OriginalNameTransformer; import dev.openrs2.deob.transform.RemapTransformer; +import dev.openrs2.deob.transform.ResetTransformer; import dev.openrs2.deob.transform.UnusedArgTransformer; import org.objectweb.asm.tree.analysis.AnalyzerException; import org.slf4j.Logger; @@ -45,6 +46,7 @@ public final class Deobfuscator { .add(new DummyArgTransformer()) .add(new DummyLocalTransformer()) .add(new UnusedArgTransformer()) + .add(new ResetTransformer()) .add(new AccessTransformer()) .build(); diff --git a/deob/src/main/java/dev/openrs2/deob/transform/ResetTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/ResetTransformer.java new file mode 100644 index 0000000000..0fbe0c8f1f --- /dev/null +++ b/deob/src/main/java/dev/openrs2/deob/transform/ResetTransformer.java @@ -0,0 +1,128 @@ +package dev.openrs2.deob.transform; + +import java.util.HashSet; +import java.util.Set; + +import dev.openrs2.asm.InsnNodeUtils; +import dev.openrs2.asm.MemberRef; +import dev.openrs2.asm.MethodNodeUtils; +import dev.openrs2.asm.classpath.ClassPath; +import dev.openrs2.asm.classpath.Library; +import dev.openrs2.asm.transform.Transformer; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.InsnNode; +import org.objectweb.asm.tree.LdcInsnNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.objectweb.asm.tree.VarInsnNode; +import org.objectweb.asm.tree.analysis.AnalyzerException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class ResetTransformer extends Transformer { + private static final Logger logger = LoggerFactory.getLogger(ResetTransformer.class); + + private static MemberRef findMasterReset(MethodNode method) { + AbstractInsnNode shutdownLdc = null; + + for (var insn : method.instructions) { + if (insn.getOpcode() != Opcodes.LDC) { + continue; + } + + var ldc = (LdcInsnNode) insn; + if (ldc.cst.equals("Shutdown complete - clean:")) { + shutdownLdc = ldc; + break; + } + } + + for (var insn = shutdownLdc; insn != null; insn = insn.getPrevious()) { + if (insn.getOpcode() != Opcodes.ALOAD) { + continue; + } + + var load = (VarInsnNode) insn; + if (load.var != 0) { + continue; + } + + var nextInsn = InsnNodeUtils.nextReal(insn); + if (nextInsn.getOpcode() != Opcodes.INVOKEVIRTUAL) { + continue; + } + + var invoke = (MethodInsnNode) nextInsn; + if (!invoke.desc.equals("()V")) { + continue; + } + + return new MemberRef(invoke.owner, invoke.name, invoke.desc); + } + + return null; + } + + private static void findResetMethods(Set resetMethods, ClassNode clazz, MethodNode method) throws AnalyzerException { + MethodNodeUtils.removeDeadCode(clazz.name, method); + + for (var insn : method.instructions) { + if (insn.getOpcode() == -1 || insn.getOpcode() != Opcodes.INVOKESTATIC) { + continue; + } + + var invoke = (MethodInsnNode) insn; + resetMethods.add(new MemberRef(invoke.owner, invoke.name, invoke.desc)); + } + } + + private final Set resetMethods = new HashSet<>(); + + @Override + protected void preTransform(ClassPath classPath) throws AnalyzerException { + resetMethods.clear(); + + for (var library : classPath.getLibraries()) { + for (var clazz : library) { + for (var method : clazz.methods) { + if ((method.access & (Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT)) != 0) { + continue; + } + + var masterReset = findMasterReset(method); + if (masterReset == null) { + continue; + } + + logger.info("Identified master reset method: {}", masterReset); + + var resetClass = classPath.getNode("client"); + var resetMethod = resetClass.methods.stream() + .filter(m -> m.name.equals(masterReset.getName()) && m.desc.equals(masterReset.getDesc())) + .findAny() + .orElseThrow(); + + findResetMethods(resetMethods, resetClass, resetMethod); + + resetMethod.instructions.clear(); + resetMethod.tryCatchBlocks.clear(); + + resetMethod.instructions.add(new InsnNode(Opcodes.RETURN)); + } + } + } + } + + @Override + protected boolean transformClass(ClassPath classPath, Library library, ClassNode clazz) { + clazz.methods.removeIf(m -> resetMethods.contains(new MemberRef(clazz.name, m.name, m.desc))); + return false; + } + + @Override + protected void postTransform(ClassPath classPath) { + logger.info("Removed {} reset methods", resetMethods.size()); + } +}