Search in sources :

Example 11 with InvokeStatic

use of net.runelite.asm.attributes.code.instructions.InvokeStatic in project runelite by runelite.

the class InjectInvoker method injectInvoker.

private void injectInvoker(ClassFile clazz, java.lang.reflect.Method method, Method deobfuscatedMethod, Method invokeMethod, String garbage) {
    if (clazz.findMethod(method.getName(), deobfuscatedMethod.getDescriptor()) != null) {
        logger.warn("Not injecting method {} because it already exists!", method);
        // this can happen from exporting a field and method with the same name
        return;
    }
    assert invokeMethod.isStatic() == deobfuscatedMethod.isStatic();
    assert invokeMethod.isStatic() || invokeMethod.getClassFile() == clazz;
    Type lastGarbageArgumentType = null;
    if (deobfuscatedMethod.getDescriptor().getArguments().size() != invokeMethod.getDescriptor().getArguments().size()) {
        // allow for obfuscated method to have a single bogus signature at the end
        assert deobfuscatedMethod.getDescriptor().size() + 1 == invokeMethod.getDescriptor().size();
        List<Type> arguments = invokeMethod.getDescriptor().getArguments();
        lastGarbageArgumentType = arguments.get(arguments.size() - 1);
    }
    // Injected method signature is always the same as the API
    Signature apiSignature = inject.javaMethodToSignature(method);
    Method invokerMethodSignature = new Method(clazz, method.getName(), apiSignature);
    invokerMethodSignature.setAccessFlags(ACC_PUBLIC);
    // create code attribute
    Code code = new Code(invokerMethodSignature);
    invokerMethodSignature.setCode(code);
    Instructions instructions = code.getInstructions();
    List<Instruction> ins = instructions.getInstructions();
    // this + arguments
    code.setMaxStack(1 + invokeMethod.getDescriptor().size());
    // load function arguments onto the stack.
    int index = 0;
    if (!invokeMethod.isStatic()) {
        // this
        ins.add(new ALoad(instructions, index++));
    } else {
        // this method is always non static
        ++index;
    }
    for (int i = 0; i < deobfuscatedMethod.getDescriptor().size(); ++i) {
        Type type = deobfuscatedMethod.getDescriptor().getTypeOfArg(i);
        Instruction loadInstruction = inject.createLoadForTypeIndex(instructions, type, index);
        ins.add(loadInstruction);
        Signature invokeDesc = invokeMethod.getDescriptor();
        Type obType = invokeDesc.getTypeOfArg(i);
        if (!type.equals(obType)) {
            CheckCast checkCast = new CheckCast(instructions);
            checkCast.setType(obType);
            ins.add(checkCast);
        }
        if (loadInstruction instanceof DLoad || loadInstruction instanceof LLoad) {
            index += 2;
        } else {
            index += 1;
        }
    }
    if (lastGarbageArgumentType != null) {
        // if garbage is null here it might just be an unused parameter, not part of the obfuscation
        if (garbage == null) {
            garbage = "0";
        }
        switch(lastGarbageArgumentType.toString()) {
            case "Z":
            case "B":
            case "C":
                ins.add(new BiPush(instructions, Byte.parseByte(garbage)));
                break;
            case "S":
                ins.add(new SiPush(instructions, Short.parseShort(garbage)));
                break;
            case "I":
                ins.add(new LDC(instructions, Integer.parseInt(garbage)));
                break;
            case "D":
                ins.add(new LDC(instructions, Double.parseDouble(garbage)));
                break;
            case "F":
                ins.add(new LDC(instructions, Float.parseFloat(garbage)));
                break;
            case "J":
                ins.add(new LDC(instructions, Long.parseLong(garbage)));
                break;
            default:
                throw new RuntimeException("Unknown type");
        }
    }
    if (invokeMethod.isStatic()) {
        ins.add(new InvokeStatic(instructions, invokeMethod.getPoolMethod()));
    } else {
        ins.add(new InvokeVirtual(instructions, invokeMethod.getPoolMethod()));
    }
    Type returnValue = invokeMethod.getDescriptor().getReturnValue();
    InstructionType returnType;
    if (returnValue.isPrimitive() && returnValue.getDimensions() == 0) {
        switch(returnValue.toString()) {
            case "Z":
            case "I":
                returnType = InstructionType.IRETURN;
                break;
            case "J":
                returnType = InstructionType.LRETURN;
                break;
            case "F":
                returnType = InstructionType.FRETURN;
                break;
            case "D":
                returnType = InstructionType.DRETURN;
                break;
            case "V":
                returnType = InstructionType.RETURN;
                break;
            default:
                assert false;
                return;
        }
    } else {
        returnType = InstructionType.ARETURN;
    }
    ins.add(new Return(instructions, returnType));
    clazz.addMethod(invokerMethodSignature);
}
Also used : SiPush(net.runelite.asm.attributes.code.instructions.SiPush) LLoad(net.runelite.asm.attributes.code.instructions.LLoad) Return(net.runelite.asm.attributes.code.instructions.Return) InstructionType(net.runelite.asm.attributes.code.InstructionType) DLoad(net.runelite.asm.attributes.code.instructions.DLoad) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) Method(net.runelite.asm.Method) CheckCast(net.runelite.asm.attributes.code.instructions.CheckCast) Instruction(net.runelite.asm.attributes.code.Instruction) BiPush(net.runelite.asm.attributes.code.instructions.BiPush) Code(net.runelite.asm.attributes.Code) InstructionType(net.runelite.asm.attributes.code.InstructionType) Type(net.runelite.asm.Type) InvokeVirtual(net.runelite.asm.attributes.code.instructions.InvokeVirtual) Signature(net.runelite.asm.signature.Signature) ALoad(net.runelite.asm.attributes.code.instructions.ALoad) InvokeStatic(net.runelite.asm.attributes.code.instructions.InvokeStatic)

Example 12 with InvokeStatic

use of net.runelite.asm.attributes.code.instructions.InvokeStatic in project runelite by runelite.

the class MultiplicationDeobfuscatorTest method test8.

@Test
public void test8() {
    ClassGroup group = ClassGroupFactory.generateGroup();
    Code code = group.findClass("test").findMethod("func").getCode();
    Instructions ins = code.getInstructions();
    code.setMaxStack(2);
    Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0) };
    for (Instruction i : prepareVariables) {
        ins.addInstruction(i);
    }
    LDC constant1 = new LDC(ins, -1616202347);
    LDC constant2 = new LDC(ins, 2747837);
    Label label1 = new Label(ins), label2 = new Label(ins), label3 = new Label(ins);
    Instruction[] body = { constant1, constant2, new IMul(ins), new ILoad(ins, 0), new LDC(ins, 42), new IfEq(ins, label1), new Goto(ins, label2), label1, new LDC(ins, -1), new Goto(ins, label3), label2, new LDC(ins, 0), new Goto(ins, label3), label3, new InvokeStatic(ins, group.findClass("test").findMethod("func2").getPoolMethod()), new VReturn(ins) };
    for (Instruction i : body) {
        ins.addInstruction(i);
    }
    Execution e = new Execution(group);
    e.populateInitialMethods();
    e.run();
    assert constant1.getConstantAsInt() * constant2.getConstantAsInt() == 1;
    Deobfuscator d = new MultiplicationDeobfuscator();
    d.run(group);
    Assert.assertEquals(1, constant1.getConstantAsInt());
    Assert.assertEquals(1, constant2.getConstantAsInt());
}
Also used : IStore(net.runelite.asm.attributes.code.instructions.IStore) Goto(net.runelite.asm.attributes.code.instructions.Goto) ILoad(net.runelite.asm.attributes.code.instructions.ILoad) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) IfEq(net.runelite.asm.attributes.code.instructions.IfEq) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) VReturn(net.runelite.asm.attributes.code.instructions.VReturn) Deobfuscator(net.runelite.deob.Deobfuscator) Execution(net.runelite.asm.execution.Execution) ClassGroup(net.runelite.asm.ClassGroup) IMul(net.runelite.asm.attributes.code.instructions.IMul) InvokeStatic(net.runelite.asm.attributes.code.instructions.InvokeStatic) Test(org.junit.Test)

Example 13 with InvokeStatic

use of net.runelite.asm.attributes.code.instructions.InvokeStatic in project runelite by runelite.

the class ReflectionTransformer method transformFindClass.

// invokestatic          java/lang/Class/forName(Ljava/lang/String;)Ljava/lang/Class;
// to
// invokestatic          net/runelite/rs/Reflection/findClass(Ljava/lang/String;)Ljava/lang/Class;
private void transformFindClass(Instruction i) {
    if (!(i instanceof InvokeStatic)) {
        return;
    }
    InvokeStatic is = (InvokeStatic) i;
    if (is.getMethod().getClazz().getName().equals("java/lang/Class") && is.getMethod().getName().equals("forName")) {
        is.setMethod(new net.runelite.asm.pool.Method(new net.runelite.asm.pool.Class("net/runelite/rs/Reflection"), "findClass", new Signature("(Ljava/lang/String;)Ljava/lang/Class;")));
        logger.info("Transformed Class.forName call");
    }
}
Also used : Signature(net.runelite.asm.signature.Signature) InvokeStatic(net.runelite.asm.attributes.code.instructions.InvokeStatic)

Example 14 with InvokeStatic

use of net.runelite.asm.attributes.code.instructions.InvokeStatic in project runelite by runelite.

the class ReflectionTransformer method transformMethodName.

// invokevirtual         java/lang/reflect/Method/getName()Ljava/lang/String;
// to
// invokestatic          net/runelite/rs/Reflection/getMethodName(Ljava/lang/reflect/Method;)Ljava/lang/String;
private void transformMethodName(Instructions instructions, Instruction i) {
    if (!(i instanceof InvokeVirtual)) {
        return;
    }
    InvokeVirtual iv = (InvokeVirtual) i;
    if (iv.getMethod().getClazz().getName().equals("java/lang/reflect/Method") && iv.getMethod().getName().equals("getName")) {
        InvokeStatic is = new InvokeStatic(instructions, new net.runelite.asm.pool.Method(new net.runelite.asm.pool.Class("net/runelite/rs/Reflection"), "getMethodName", new Signature("(Ljava/lang/reflect/Method;)Ljava/lang/String;")));
        instructions.replace(iv, is);
        logger.info("Transformed Method.getName call");
    }
}
Also used : InvokeVirtual(net.runelite.asm.attributes.code.instructions.InvokeVirtual) Signature(net.runelite.asm.signature.Signature) InvokeStatic(net.runelite.asm.attributes.code.instructions.InvokeStatic)

Example 15 with InvokeStatic

use of net.runelite.asm.attributes.code.instructions.InvokeStatic in project runelite by runelite.

the class ReflectionTransformer method transformGetInt.

// invokevirtual         java/lang/reflect/Field/getInt(Ljava/lang/Object;)I
private void transformGetInt(Instructions instructions, Instruction i) {
    if (!(i instanceof InvokeVirtual)) {
        return;
    }
    InvokeVirtual iv = (InvokeVirtual) i;
    if (iv.getMethod().getClazz().getName().equals("java/lang/reflect/Field") && iv.getMethod().getName().equals("getInt")) {
        InvokeStatic is = new InvokeStatic(instructions, new net.runelite.asm.pool.Method(new net.runelite.asm.pool.Class("net/runelite/rs/Reflection"), "getInt", new Signature("(Ljava/lang/reflect/Field;Ljava/lang/Object;)I")));
        instructions.replace(iv, is);
        logger.info("Transformed Field.getInt call");
    }
}
Also used : InvokeVirtual(net.runelite.asm.attributes.code.instructions.InvokeVirtual) Signature(net.runelite.asm.signature.Signature) InvokeStatic(net.runelite.asm.attributes.code.instructions.InvokeStatic)

Aggregations

InvokeStatic (net.runelite.asm.attributes.code.instructions.InvokeStatic)19 Signature (net.runelite.asm.signature.Signature)14 InvokeVirtual (net.runelite.asm.attributes.code.instructions.InvokeVirtual)8 Method (net.runelite.asm.Method)6 Instruction (net.runelite.asm.attributes.code.Instruction)6 Instructions (net.runelite.asm.attributes.code.Instructions)6 Type (net.runelite.asm.Type)5 Code (net.runelite.asm.attributes.Code)5 ClassFile (net.runelite.asm.ClassFile)4 ALoad (net.runelite.asm.attributes.code.instructions.ALoad)4 ListIterator (java.util.ListIterator)3 ClassGroup (net.runelite.asm.ClassGroup)3 Label (net.runelite.asm.attributes.code.Label)3 InvokeInstruction (net.runelite.asm.attributes.code.instruction.types.InvokeInstruction)3 LDC (net.runelite.asm.attributes.code.instructions.LDC)3 DeobAnnotations (net.runelite.deob.DeobAnnotations)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2