Search in sources :

Example 1 with Goto

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

the class MenuActionDeobfuscator method insert.

private void insert(Method method, List<Comparison> comparisons) {
    Instructions instructions = method.getCode().getInstructions();
    List<Instruction> ins = instructions.getInstructions();
    // replace all if(var == constant) with a jump to the false branch
    // then, insert before the first jump the ifs to jump to the old
    // true branch
    // 
    // this is probably actually lookupswitch but it isn't mappable
    // currently...
    int min = -1;
    for (Comparison comp : comparisons) {
        if (min == -1) {
            min = ins.indexOf(comp.lvt);
        } else {
            min = Math.min(min, ins.indexOf(comp.lvt));
        }
        if (comp.cmp.getType() == InstructionType.IF_ICMPEQ) {
            If cmp = (If) comp.cmp;
            // remove
            instructions.remove(comp.ldc);
            instructions.remove((Instruction) comp.lvt);
            instructions.remove(comp.cmp);
            comp.next = cmp.getJumps().get(0);
        } else if (comp.cmp.getType() == InstructionType.IF_ICMPNE) {
            // replace with goto dest
            If cmp = (If) comp.cmp;
            int idx = ins.indexOf(cmp);
            assert idx != -1;
            comp.next = instructions.createLabelFor(ins.get(idx + 1));
            instructions.remove(comp.ldc);
            instructions.remove((Instruction) comp.lvt);
            instructions.replace(comp.cmp, new Goto(instructions, cmp.getJumps().get(0)));
        } else {
            throw new IllegalStateException();
        }
    }
    assert min != -1;
    // sort comparisons - but if they jump to the same address, they are equal..
    List<Comparison> sortedComparisons = new ArrayList<>(comparisons);
    Collections.sort(sortedComparisons, (c1, c2) -> compare(comparisons, c1, c2));
    // reinsert jumps
    for (int i = 0; i < sortedComparisons.size(); ++i) {
        Comparison comp = sortedComparisons.get(i);
        Instruction lvt = (Instruction) comp.lvt;
        lvt.setInstructions(instructions);
        comp.ldc.setInstructions(instructions);
        instructions.addInstruction(min++, lvt);
        instructions.addInstruction(min++, comp.ldc);
        // use if_icmpeq if what follows also jumps to the same location
        boolean multiple = i + 1 < sortedComparisons.size() && sortedComparisons.get(i + 1).next == comp.next;
        if (multiple) {
            instructions.addInstruction(min++, new IfICmpEq(instructions, comp.next));
        } else {
            // fernflower decompiles a series of if_icmpeq as chains of not equal expressions
            Label label = instructions.createLabelFor(ins.get(min));
            instructions.addInstruction(min++, new IfICmpNe(instructions, label));
            instructions.addInstruction(min++, new Goto(instructions, comp.next));
            // go past label
            ++min;
        }
    }
}
Also used : IfICmpEq(net.runelite.asm.attributes.code.instructions.IfICmpEq) Goto(net.runelite.asm.attributes.code.instructions.Goto) ArrayList(java.util.ArrayList) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) IfICmpNe(net.runelite.asm.attributes.code.instructions.IfICmpNe) If(net.runelite.asm.attributes.code.instructions.If)

Example 2 with Goto

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

the class MultiplyOneDeobfuscatorTest method test2.

// iconst_1
// iconst_m1
// iload                 5
// if_icmpeq             LABEL0x56d1
// iload                 5
// iconst_1
// if_icmpne             LABEL0x56e8
// goto                  LABEL0x56d1
// LABEL0x56d1:
// getstatic             class139/field2130 I
// ldc_w                 -1440517609
// imul
// goto                  LABEL0x5708
// LABEL0x56e8:
// getstatic             class139/field2130 I
// ldc_w                 -1440517609
// imul
// getstatic             client/field377 I
// iadd
// iconst_2
// idiv
// LABEL0x5708:
// imul
// putstatic             client/field377 I
// 
// client.field377 = 1 * (-1 != var5 && var5 != 1?(class139.field2130 + client.field377) / 2:class139.field2130);
@Test
public void test2() {
    ClassGroup group = ClassGroupFactory.generateGroup();
    Code code = group.findClass("test").findMethod("func").getCode();
    Instructions ins = code.getInstructions();
    code.setMaxStack(2);
    // vars[0] = 3
    Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0), new LDC(ins, 2), new IStore(ins, 1) };
    for (Instruction i : prepareVariables) ins.addInstruction(i);
    Label label = new Label(ins), label2 = new Label(ins), label3 = new Label(ins);
    LDC one = new LDC(ins, 1);
    IMul mul = new IMul(ins);
    Instruction[] body = { one, new LDC(ins, -1), new ILoad(ins, 0), new IfICmpEq(ins, label), new Goto(ins, label2), label, new ILoad(ins, 1), new LDC(ins, -1440517609), new IDiv(ins), new Goto(ins, label3), label2, new ILoad(ins, 1), new LDC(ins, -1440517609), new IDiv(ins), label3, mul, new VReturn(ins) };
    for (Instruction i : body) ins.addInstruction(i);
    // check execution runs ok
    Execution e = new Execution(group);
    e.populateInitialMethods();
    e.run();
    Deobfuscator d = new MultiplyOneDeobfuscator(false);
    d.run(group);
    Assert.assertTrue(one.getInstructions() == null);
    Assert.assertTrue(mul.getInstructions() == null);
}
Also used : IfICmpEq(net.runelite.asm.attributes.code.instructions.IfICmpEq) 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) IDiv(net.runelite.asm.attributes.code.instructions.IDiv) 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) Test(org.junit.Test)

Example 3 with Goto

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

the class ControlFlowDeobfuscator method runJumpLabel.

/**
 * remove jumps followed immediately by the label they are jumping to
 *
 * @param code
 */
private void runJumpLabel(Code code) {
    Instructions ins = code.getInstructions();
    List<Instruction> instructions = ins.getInstructions();
    for (int i = 0; i < instructions.size() - 1; ++i) {
        Instruction i1 = instructions.get(i), i2 = instructions.get(i + 1);
        if (!(i1 instanceof Goto)) {
            continue;
        }
        Goto g = (Goto) i1;
        assert g.getJumps().size() == 1;
        if (g.getJumps().get(0) != i2) {
            continue;
        }
        // remove jump
        ins.remove(i1);
        ++removedJumps;
    // i now points to i2, so next loop we go to next instruction
    }
}
Also used : Goto(net.runelite.asm.attributes.code.instructions.Goto) Instructions(net.runelite.asm.attributes.code.Instructions) Instruction(net.runelite.asm.attributes.code.Instruction)

Example 4 with Goto

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

the class ControlFlowDeobfuscator method split.

/**
 * Add gotos at the end of blocks without terminal instructions
 *
 * @param code
 */
private void split(Code code) {
    Instructions ins = code.getInstructions();
    Exceptions exceptions = code.getExceptions();
    ControlFlowGraph graph = new ControlFlowGraph.Builder().build(code);
    List<Exception> exc = new ArrayList<>(exceptions.getExceptions());
    // Must clear this before ins.clear() runs
    exceptions.clear();
    ins.clear();
    // insert jumps where blocks flow into others
    for (Block block : graph.getBlocks()) {
        if (block.getFlowsInto() == null) {
            continue;
        }
        Block into = block.getFlowsInto();
        assert into.getFlowsFrom() == block;
        Instruction first = into.getInstructions().get(0);
        Label label;
        if (!(first instanceof Label)) {
            label = new Label(null);
            into.addInstruction(0, label);
        } else {
            label = (Label) first;
        }
        Goto g = new Goto(null, label);
        block.addInstruction(g);
        block.setFlowsInto(null);
        into.setFlowsFrom(null);
        ++insertedJump;
    }
    // Readd instructions from modified blocks
    for (Block block : graph.getBlocks()) {
        for (Instruction i : block.getInstructions()) {
            assert i.getInstructions() == null;
            // I shouldn't have to do this here
            i.setInstructions(ins);
            ins.addInstruction(i);
        }
    }
    // Readd exceptions
    for (Exception ex : exc) {
        exceptions.add(ex);
    }
}
Also used : Goto(net.runelite.asm.attributes.code.instructions.Goto) Exceptions(net.runelite.asm.attributes.code.Exceptions) ArrayList(java.util.ArrayList) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) Instruction(net.runelite.asm.attributes.code.Instruction) Exception(net.runelite.asm.attributes.code.Exception)

Example 5 with Goto

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

Aggregations

Instruction (net.runelite.asm.attributes.code.Instruction)12 Goto (net.runelite.asm.attributes.code.instructions.Goto)12 Instructions (net.runelite.asm.attributes.code.Instructions)11 Label (net.runelite.asm.attributes.code.Label)9 LVTInstruction (net.runelite.asm.attributes.code.instruction.types.LVTInstruction)6 ComparisonInstruction (net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction)5 InvokeInstruction (net.runelite.asm.attributes.code.instruction.types.InvokeInstruction)5 JumpingInstruction (net.runelite.asm.attributes.code.instruction.types.JumpingInstruction)5 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)5 IfEq (net.runelite.asm.attributes.code.instructions.IfEq)5 LDC (net.runelite.asm.attributes.code.instructions.LDC)5 ArrayList (java.util.ArrayList)4 ClassGroup (net.runelite.asm.ClassGroup)4 SetFieldInstruction (net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction)4 If (net.runelite.asm.attributes.code.instructions.If)4 Execution (net.runelite.asm.execution.Execution)4 Deobfuscator (net.runelite.deob.Deobfuscator)4 Code (net.runelite.asm.attributes.Code)3 GetFieldInstruction (net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)3 MappableInstruction (net.runelite.asm.attributes.code.instruction.types.MappableInstruction)3