Search in sources :

Example 21 with ClassGroup

use of net.runelite.asm.ClassGroup in project runelite by runelite.

the class InjectHookMethod method process.

public void process(Method method) throws InjectionException {
    Annotations an = method.getAnnotations();
    if (an == null) {
        return;
    }
    Annotation a = an.find(DeobAnnotations.HOOK);
    if (a == null) {
        return;
    }
    // Method is hooked
    // String hookName = DeobAnnotations.getHookName(an); // hook name
    // Find equivalent method in vanilla, and insert callback at the beginning
    ClassFile cf = method.getClassFile();
    String obfuscatedMethodName = DeobAnnotations.getObfuscatedName(an), obfuscatedClassName = DeobAnnotations.getObfuscatedName(cf.getAnnotations());
    // might be a constructor
    if (obfuscatedMethodName == null && method.getName().equals("<init>")) {
        obfuscatedMethodName = "<init>";
    }
    assert obfuscatedClassName != null : "hook on method in class with no obfuscated name";
    assert obfuscatedMethodName != null : "hook on method with no obfuscated name";
    Signature obfuscatedSignature = inject.getMethodSignature(method);
    ClassGroup vanilla = inject.getVanilla();
    ClassFile vanillaClass = vanilla.findClass(obfuscatedClassName);
    Method vanillaMethod = vanillaClass.findMethod(obfuscatedMethodName, obfuscatedSignature);
    // Insert instructions at beginning of method
    injectHookMethod(a, method, vanillaMethod);
}
Also used : Annotations(net.runelite.asm.attributes.Annotations) DeobAnnotations(net.runelite.deob.DeobAnnotations) ClassFile(net.runelite.asm.ClassFile) ClassGroup(net.runelite.asm.ClassGroup) Signature(net.runelite.asm.signature.Signature) Method(net.runelite.asm.Method) Annotation(net.runelite.asm.attributes.annotation.Annotation)

Example 22 with ClassGroup

use of net.runelite.asm.ClassGroup in project runelite by runelite.

the class Deob method main.

public static void main(String[] args) throws IOException {
    if (args == null || args.length < 2) {
        System.err.println("Syntax: input_jar output_jar");
        System.exit(-1);
    }
    logger.info("Deobfuscator revision {}", DeobProperties.getRevision());
    Stopwatch stopwatch = Stopwatch.createStarted();
    ClassGroup group = JarUtil.loadJar(new File(args[0]));
    // remove except RuntimeException
    run(group, new RuntimeExceptions());
    run(group, new ControlFlowDeobfuscator());
    run(group, new RenameUnique());
    // remove unused methods - this leaves Code with no instructions,
    // which is not valid, so unused methods is run after
    run(group, new UnreachedCode());
    run(group, new UnusedMethods());
    // remove illegal state exceptions, frees up some parameters
    run(group, new IllegalStateExceptions());
    // remove constant logically dead parameters
    run(group, new ConstantParameter());
    // remove unhit blocks
    run(group, new UnreachedCode());
    run(group, new UnusedMethods());
    // remove unused parameters
    run(group, new UnusedParameters());
    // remove unused fields
    run(group, new UnusedFields());
    run(group, new FieldInliner());
    // order uses class name order for sorting fields/methods,
    // so run it before removing classes below
    run(group, new Order());
    run(group, new UnusedClass());
    runMath(group);
    run(group, new ExprArgOrder());
    run(group, new Lvt());
    run(group, new CastNull());
    run(group, new EnumDeobfuscator());
    new OpcodesTransformer().transform(group);
    // run(group, new PacketHandlerOrder());
    // run(group, new PacketWriteDeobfuscator());
    run(group, new MenuActionDeobfuscator());
    new GetPathTransformer().transform(group);
    new ClientErrorTransformer().transform(group);
    new ReflectionTransformer().transform(group);
    new MaxMemoryTransformer().transform(group);
    // new RuneliteBufferTransformer().transform(group);
    JarUtil.saveJar(group, new File(args[1]));
    stopwatch.stop();
    logger.info("Done in {}", stopwatch);
}
Also used : IllegalStateExceptions(net.runelite.deob.deobfuscators.IllegalStateExceptions) ClientErrorTransformer(net.runelite.deob.deobfuscators.transformers.ClientErrorTransformer) UnusedParameters(net.runelite.deob.deobfuscators.UnusedParameters) Stopwatch(com.google.common.base.Stopwatch) EnumDeobfuscator(net.runelite.deob.deobfuscators.EnumDeobfuscator) Lvt(net.runelite.deob.deobfuscators.Lvt) ConstantParameter(net.runelite.deob.deobfuscators.constparam.ConstantParameter) RenameUnique(net.runelite.deob.deobfuscators.RenameUnique) MaxMemoryTransformer(net.runelite.deob.deobfuscators.transformers.MaxMemoryTransformer) FieldInliner(net.runelite.deob.deobfuscators.FieldInliner) ExprArgOrder(net.runelite.deob.deobfuscators.exprargorder.ExprArgOrder) Order(net.runelite.deob.deobfuscators.Order) UnusedMethods(net.runelite.deob.deobfuscators.UnusedMethods) UnreachedCode(net.runelite.deob.deobfuscators.UnreachedCode) UnusedClass(net.runelite.deob.deobfuscators.UnusedClass) OpcodesTransformer(net.runelite.deob.deobfuscators.transformers.OpcodesTransformer) GetPathTransformer(net.runelite.deob.deobfuscators.transformers.GetPathTransformer) RuntimeExceptions(net.runelite.deob.deobfuscators.RuntimeExceptions) ControlFlowDeobfuscator(net.runelite.deob.deobfuscators.cfg.ControlFlowDeobfuscator) UnusedFields(net.runelite.deob.deobfuscators.UnusedFields) ClassGroup(net.runelite.asm.ClassGroup) CastNull(net.runelite.deob.deobfuscators.CastNull) MenuActionDeobfuscator(net.runelite.deob.deobfuscators.menuaction.MenuActionDeobfuscator) ExprArgOrder(net.runelite.deob.deobfuscators.exprargorder.ExprArgOrder) ReflectionTransformer(net.runelite.deob.deobfuscators.transformers.ReflectionTransformer) File(java.io.File)

Example 23 with ClassGroup

use of net.runelite.asm.ClassGroup in project runelite by runelite.

the class UnusedParameters method removeParameter.

public void removeParameter(ClassGroup group, List<Method> methods, Signature signature, Execution execution, int paramIndex, int lvtIndex) {
    int slots = signature.getTypeOfArg(paramIndex).getSize();
    for (ClassFile cf : group.getClasses()) {
        for (Method m : cf.getMethods()) {
            Code c = m.getCode();
            if (c == null) {
                continue;
            }
            for (Instruction i : new ArrayList<>(c.getInstructions().getInstructions())) {
                if (!(i instanceof InvokeInstruction)) {
                    continue;
                }
                InvokeInstruction ii = (InvokeInstruction) i;
                if (!ii.getMethods().stream().anyMatch(me -> methods.contains(me))) {
                    continue;
                }
                // remove parameter from instruction
                ii.removeParameter(paramIndex);
                Collection<InstructionContext> ics = invokes.get(i);
                assert ics != null;
                if (ics != null) {
                    for (InstructionContext ins : ics) {
                        // index from top of stack of parameter. 0 is the last parameter
                        int pops = signature.size() - paramIndex - 1;
                        StackContext sctx = ins.getPops().get(pops);
                        if (sctx.getPushed().getInstruction().getInstructions() == null) {
                            continue;
                        }
                        // remove parameter from stack
                        ins.removeStack(pops);
                    }
                }
            }
        }
    }
    for (Method method : methods) {
        if (method.getCode() != null) // adjust lvt indexes to get rid of idx in the method
        {
            for (Instruction ins : method.getCode().getInstructions().getInstructions()) {
                if (ins instanceof LVTInstruction) {
                    LVTInstruction lins = (LVTInstruction) ins;
                    int i = lins.getVariableIndex();
                    // current unused variable detection just looks for no accesses
                    assert i != lvtIndex;
                    // reassign
                    if (i > lvtIndex) {
                        assert i > 0;
                        assert i >= lvtIndex + slots;
                        Instruction newIns = lins.setVariableIndex(i - slots);
                        assert ins == newIns;
                    }
                }
            }
        }
    }
    for (Method method : methods) {
        method.getDescriptor().remove(paramIndex);
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Code(net.runelite.asm.attributes.Code) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) ClassGroup(net.runelite.asm.ClassGroup) StackContext(net.runelite.asm.execution.StackContext) HashMultimap(com.google.common.collect.HashMultimap) Method(net.runelite.asm.Method) Map(java.util.Map) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) VirtualMethods(net.runelite.asm.signature.util.VirtualMethods) ImmutableSet(com.google.common.collect.ImmutableSet) DeobAnnotations(net.runelite.deob.DeobAnnotations) Logger(org.slf4j.Logger) Deob(net.runelite.deob.Deob) Collection(java.util.Collection) Set(java.util.Set) Deobfuscator(net.runelite.deob.Deobfuscator) InstructionContext(net.runelite.asm.execution.InstructionContext) Sets(com.google.common.collect.Sets) Execution(net.runelite.asm.execution.Execution) List(java.util.List) ClassFile(net.runelite.asm.ClassFile) Signature(net.runelite.asm.signature.Signature) Instruction(net.runelite.asm.attributes.code.Instruction) Collections(java.util.Collections) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) InstructionContext(net.runelite.asm.execution.InstructionContext) ClassFile(net.runelite.asm.ClassFile) StackContext(net.runelite.asm.execution.StackContext) ArrayList(java.util.ArrayList) Method(net.runelite.asm.Method) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) Code(net.runelite.asm.attributes.Code)

Example 24 with ClassGroup

use of net.runelite.asm.ClassGroup in project runelite by runelite.

the class PutField method getMyField.

@Override
public net.runelite.asm.Field getMyField() {
    Class clazz = field.getClazz();
    ClassGroup group = this.getInstructions().getCode().getMethod().getClassFile().getGroup();
    ClassFile cf = group.findClass(clazz.getName());
    if (cf == null) {
        return null;
    }
    net.runelite.asm.Field f2 = cf.findFieldDeep(field.getName(), field.getType());
    return f2;
}
Also used : ClassFile(net.runelite.asm.ClassFile) ClassGroup(net.runelite.asm.ClassGroup) Class(net.runelite.asm.pool.Class)

Example 25 with ClassGroup

use of net.runelite.asm.ClassGroup in project runelite by runelite.

the class InjectHookMethodTest method testProcess.

@Test
public void testProcess() throws IOException, InjectionException {
    InputStream in = getClass().getResourceAsStream("Actor.class");
    ClassFile cf = ClassUtil.loadClass(in);
    cf.setName("Actor");
    cf.findMethod("bar").setDescriptor(new Signature("(LActor;I)I"));
    ClassGroup deobfuscated = new ClassGroup();
    deobfuscated.addClass(cf);
    in = getClass().getResourceAsStream("Obfuscated.class");
    ClassFile obcf = ClassUtil.loadClass(in);
    obcf.setName("Obfuscated");
    obcf.findMethod("foo").setDescriptor(new Signature("(LObfuscated;I)I"));
    ClassGroup obfuscated = new ClassGroup();
    obfuscated.addClass(obcf);
    Method method = cf.findMethod("bar");
    assert method != null;
    Inject inject = new Inject(deobfuscated, obfuscated);
    InjectHookMethod injectHookMethod = new InjectHookMethod(inject);
    injectHookMethod.process(method);
    method = obcf.findMethod("foo");
    assert method != null;
    Code code = method.getCode();
    List<InvokeStatic> invokeIns = code.getInstructions().getInstructions().stream().filter(i -> i instanceof InvokeStatic).map(i -> (InvokeStatic) i).filter(i -> i.getMethod().getClazz().getName().equals(HOOKS)).collect(Collectors.toList());
    assertTrue(!invokeIns.isEmpty());
    assertEquals(2, invokeIns.size());
    InvokeStatic invokeStatic = invokeIns.get(0);
    Signature signature = invokeStatic.getMethod().getType();
    // this + patamers
    assertEquals(3, signature.size());
    Type arg = signature.getTypeOfArg(1);
    assertEquals(RL_API_PACKAGE_BASE.replace('.', '/') + "Actor", arg.getInternalName());
}
Also used : ObfuscatedName(net.runelite.mapping.ObfuscatedName) HOOKS(net.runelite.injector.InjectHookMethod.HOOKS) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Code(net.runelite.asm.attributes.Code) RL_API_PACKAGE_BASE(net.runelite.injector.Inject.RL_API_PACKAGE_BASE) Test(org.junit.Test) Type(net.runelite.asm.Type) Collectors(java.util.stream.Collectors) InvokeStatic(net.runelite.asm.attributes.code.instructions.InvokeStatic) ClassGroup(net.runelite.asm.ClassGroup) List(java.util.List) ClassFile(net.runelite.asm.ClassFile) Method(net.runelite.asm.Method) ObfuscatedSignature(net.runelite.mapping.ObfuscatedSignature) ClassUtil(net.runelite.asm.ClassUtil) Hook(net.runelite.mapping.Hook) Signature(net.runelite.asm.signature.Signature) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) Type(net.runelite.asm.Type) ClassFile(net.runelite.asm.ClassFile) InputStream(java.io.InputStream) ClassGroup(net.runelite.asm.ClassGroup) ObfuscatedSignature(net.runelite.mapping.ObfuscatedSignature) Signature(net.runelite.asm.signature.Signature) Method(net.runelite.asm.Method) Code(net.runelite.asm.attributes.Code) InvokeStatic(net.runelite.asm.attributes.code.instructions.InvokeStatic) Test(org.junit.Test)

Aggregations

ClassGroup (net.runelite.asm.ClassGroup)66 Test (org.junit.Test)41 Code (net.runelite.asm.attributes.Code)29 ClassFile (net.runelite.asm.ClassFile)28 Instruction (net.runelite.asm.attributes.code.Instruction)28 Instructions (net.runelite.asm.attributes.code.Instructions)28 LDC (net.runelite.asm.attributes.code.instructions.LDC)28 VReturn (net.runelite.asm.attributes.code.instructions.VReturn)26 IStore (net.runelite.asm.attributes.code.instructions.IStore)23 Deobfuscator (net.runelite.deob.Deobfuscator)22 ILoad (net.runelite.asm.attributes.code.instructions.ILoad)21 Execution (net.runelite.asm.execution.Execution)21 IMul (net.runelite.asm.attributes.code.instructions.IMul)18 Method (net.runelite.asm.Method)12 IAdd (net.runelite.asm.attributes.code.instructions.IAdd)11 Pop (net.runelite.asm.attributes.code.instructions.Pop)11 File (java.io.File)9 Label (net.runelite.asm.attributes.code.Label)9 Type (net.runelite.asm.Type)8 Signature (net.runelite.asm.signature.Signature)8