Search in sources :

Example 1 with IfEq

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

the class RuneliteBufferTransformer method injectLengthHeader.

/**
 * inject the length header after the packet opcode
 *
 * @param group
 */
private void injectLengthHeader(ClassGroup group) {
    RWOpcodeFinder rw = new RWOpcodeFinder(group);
    rw.find();
    Method writeOpcode = rw.getWriteOpcode();
    Code code = writeOpcode.getCode();
    Instructions instructions = code.getInstructions();
    List<Instruction> ins = instructions.getInstructions();
    Instruction start = ins.get(0);
    Instruction end = ins.stream().filter(i -> i.getType() == RETURN).findFirst().get();
    Label labelForStart = instructions.createLabelFor(start);
    Label labelForEnd = instructions.createLabelFor(end);
    final net.runelite.asm.pool.Field runelitePacketField = new net.runelite.asm.pool.Field(new net.runelite.asm.pool.Class(findClient(group).getName()), RUNELITE_PACKET, Type.BOOLEAN);
    int idx = ins.indexOf(labelForStart);
    instructions.addInstruction(idx++, new GetStatic(instructions, runelitePacketField));
    instructions.addInstruction(idx++, new IfEq(instructions, labelForStart));
    net.runelite.asm.pool.Method method = new net.runelite.asm.pool.Method(new net.runelite.asm.pool.Class(writeOpcode.getClassFile().getName()), RUNELITE_FINISH_PACKET, new Signature("()V"));
    instructions.addInstruction(idx++, new ALoad(instructions, 0));
    instructions.addInstruction(idx++, new InvokeVirtual(instructions, method));
    idx = ins.indexOf(labelForEnd);
    instructions.addInstruction(idx++, new GetStatic(instructions, runelitePacketField));
    instructions.addInstruction(idx++, new IfEq(instructions, labelForEnd));
    method = new net.runelite.asm.pool.Method(new net.runelite.asm.pool.Class(writeOpcode.getClassFile().getName()), RUNELITE_INIT_PACKET, new Signature("()V"));
    instructions.addInstruction(idx++, new ALoad(instructions, 0));
    instructions.addInstruction(idx++, new InvokeVirtual(instructions, method));
    logger.info("Injected finish/init packet calls into {}", writeOpcode);
}
Also used : RWOpcodeFinder(net.runelite.deob.c2s.RWOpcodeFinder) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) IfEq(net.runelite.asm.attributes.code.instructions.IfEq) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) Field(net.runelite.asm.Field) GetStatic(net.runelite.asm.attributes.code.instructions.GetStatic) InvokeVirtual(net.runelite.asm.attributes.code.instructions.InvokeVirtual) Signature(net.runelite.asm.signature.Signature) ALoad(net.runelite.asm.attributes.code.instructions.ALoad)

Example 2 with IfEq

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

the class MultiplicationDeobfuscatorTest method test4.

@Test
public void test4() {
    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, 1807370871);
    LDC constant2 = new LDC(ins, 981643079);
    Label label1 = new Label(ins);
    Instruction[] body = { new ILoad(ins, 0), new LDC(ins, 2), new IMul(ins), new LDC(ins, 0), new IfEq(ins, label1), new Pop(ins), new LDC(ins, 3), label1, constant1, new IMul(ins), constant2, new IMul(ins), 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) 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) Pop(net.runelite.asm.attributes.code.instructions.Pop) Execution(net.runelite.asm.execution.Execution) ClassGroup(net.runelite.asm.ClassGroup) IMul(net.runelite.asm.attributes.code.instructions.IMul) Test(org.junit.Test)

Example 3 with IfEq

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

the class PacketHandlerOrder method insertPacketLength.

private void insertPacketLength(ClassGroup group, PacketTypeFinder ptf) {
    PacketLengthFinder pfl = new PacketLengthFinder(group, ptf);
    pfl.find();
    GetStatic getArray = pfl.getGetArray();
    // instruction to store packet length
    PutStatic ps = pfl.getStore();
    Instructions instructions = ps.getInstructions();
    List<Instruction> ins = instructions.getInstructions();
    Label getArrayLabel = instructions.createLabelFor(getArray);
    Label storeLabel = instructions.createLabelFor(ps);
    int idx = ins.indexOf(getArray);
    assert idx != -1;
    // to go before label, which must exist
    --idx;
    net.runelite.asm.pool.Field field = new net.runelite.asm.pool.Field(new net.runelite.asm.pool.Class(findClient(group).getName()), RUNELITE_PACKET, Type.BOOLEAN);
    instructions.addInstruction(idx++, new GetStatic(instructions, field));
    instructions.addInstruction(idx++, new IfEq(instructions, getArrayLabel));
    // 2 byte length
    instructions.addInstruction(idx++, new LDC(instructions, -2));
    instructions.addInstruction(idx++, new Goto(instructions, storeLabel));
}
Also used : Goto(net.runelite.asm.attributes.code.instructions.Goto) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) PacketLengthFinder(net.runelite.deob.deobfuscators.packethandler.PacketLengthFinder) IfEq(net.runelite.asm.attributes.code.instructions.IfEq) PutStatic(net.runelite.asm.attributes.code.instructions.PutStatic) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) ComparisonInstruction(net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) JumpingInstruction(net.runelite.asm.attributes.code.instruction.types.JumpingInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) MappableInstruction(net.runelite.asm.attributes.code.instruction.types.MappableInstruction) Field(net.runelite.asm.Field) GetStatic(net.runelite.asm.attributes.code.instructions.GetStatic)

Example 4 with IfEq

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

the class MultiplicationDeobfuscatorTest method test3.

@Test
public void test3() {
    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, 1381104939), constant2 = new LDC(ins, 1381104939), constant3 = new LDC(ins, 981643079), constant4 = new LDC(ins, 1807370871), constant5 = new LDC(ins, 981643079);
    Label label1 = new Label(ins);
    Instruction[] body = { constant4, constant1, new ILoad(ins, 0), new IMul(ins), new LDC(ins, 0), new IfEq(ins, label1), constant2, new IMul(ins), label1, constant3, new IMul(ins), // constant4
    new IMul(ins), constant5, new IMul(ins), new Pop(ins), new VReturn(ins) };
    for (Instruction i : body) {
        ins.addInstruction(i);
    }
    Execution e = new Execution(group);
    e.populateInitialMethods();
    e.run();
    assert constant4.getConstantAsInt() * constant5.getConstantAsInt() == 1;
    // {
    // Collection<InstructionContext> ctxs = e.getInstructonContexts(body[3]);
    // assert ctxs.size() == 1;
    // 
    // InstructionContext ictx = ctxs.iterator().next();
    // boolean onlyPath = MultiplicationDeobfuscator.isOnlyPath(e, ictx);
    // Assert.assertFalse(onlyPath);
    // }
    Deobfuscator d = new MultiplicationDeobfuscator();
    d.run(group);
    Assert.assertEquals(1381104939, constant1.getConstantAsInt());
    Assert.assertEquals(1381104939, constant2.getConstantAsInt());
    Assert.assertEquals(1, constant3.getConstantAsInt());
    Assert.assertEquals(1, constant4.getConstantAsInt());
    // assumes result is moved to the end here.
    Assert.assertEquals(981643079, constant5.getConstantAsInt());
}
Also used : IStore(net.runelite.asm.attributes.code.instructions.IStore) 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) Pop(net.runelite.asm.attributes.code.instructions.Pop) Execution(net.runelite.asm.execution.Execution) ClassGroup(net.runelite.asm.ClassGroup) IMul(net.runelite.asm.attributes.code.instructions.IMul) Test(org.junit.Test)

Example 5 with IfEq

use of net.runelite.asm.attributes.code.instructions.IfEq 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)

Aggregations

Instruction (net.runelite.asm.attributes.code.Instruction)8 Instructions (net.runelite.asm.attributes.code.Instructions)8 Label (net.runelite.asm.attributes.code.Label)8 IfEq (net.runelite.asm.attributes.code.instructions.IfEq)8 Code (net.runelite.asm.attributes.Code)6 LDC (net.runelite.asm.attributes.code.instructions.LDC)6 ClassGroup (net.runelite.asm.ClassGroup)5 ILoad (net.runelite.asm.attributes.code.instructions.ILoad)5 IMul (net.runelite.asm.attributes.code.instructions.IMul)5 IStore (net.runelite.asm.attributes.code.instructions.IStore)5 VReturn (net.runelite.asm.attributes.code.instructions.VReturn)5 Execution (net.runelite.asm.execution.Execution)5 Deobfuscator (net.runelite.deob.Deobfuscator)5 Test (org.junit.Test)5 Goto (net.runelite.asm.attributes.code.instructions.Goto)4 GetStatic (net.runelite.asm.attributes.code.instructions.GetStatic)3 Field (net.runelite.asm.Field)2 InvokeInstruction (net.runelite.asm.attributes.code.instruction.types.InvokeInstruction)2 LVTInstruction (net.runelite.asm.attributes.code.instruction.types.LVTInstruction)2 SetFieldInstruction (net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction)2