Search in sources :

Example 6 with Label

use of net.runelite.asm.attributes.code.Label 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 7 with Label

use of net.runelite.asm.attributes.code.Label in project runelite by runelite.

the class LookupSwitch method execute.

@Override
public InstructionContext execute(Frame frame) {
    InstructionContext ins = new InstructionContext(this, frame);
    Stack stack = frame.getStack();
    StackContext value = stack.pop();
    ins.pop(value);
    // from the non-default branch
    for (Label i : branchi) {
        Frame other = frame.dup();
        other.jump(ins, i);
        ins.branch(other);
    }
    frame.jump(ins, defi);
    return ins;
}
Also used : InstructionContext(net.runelite.asm.execution.InstructionContext) Frame(net.runelite.asm.execution.Frame) StackContext(net.runelite.asm.execution.StackContext) Label(net.runelite.asm.attributes.code.Label) Stack(net.runelite.asm.execution.Stack)

Example 8 with Label

use of net.runelite.asm.attributes.code.Label in project runelite by runelite.

the class DrawAfterWidgets method injectDrawAfterWidgets.

private void injectDrawAfterWidgets() throws InjectionException {
    /*
			This call has to be injected using raw injection because the
			drawWidgets method gets inlined in some revisions. If it wouldn't be,
			mixins would be used to add the call to the end of drawWidgets.

			--> This hook depends on the positions of "if (535573958 * kl != -1)" and "jz.db();".


			Revision 153 - client.ll():
			______________________________________________________

			if (535573958 * kl != -1) {
				me = 0;
				var1 = kl * 1593233303;
				var2 = 523525871 * q;
				var3 = -1668171507 * h.n;
				if (!bo.p(var1, (byte)111)) {
					for(var4 = 0; var4 < 100; ++var4) {
						mb[var4] = true;
					}
				} else {
					z.lj = null;
					bl.hz(fa.g[var1], -1, 0, 0, var2, var3, 0, 0, -1, 1505114436);
					if (z.lj != null) {
						bl.hz(z.lj, -1412584499, 0, 0, var2, var3, 1475313862 * bq.la, aq.lc * 1205565233, -1, 2123188146);
						z.lj = null;
					}
				}
			}

			// <-- INJECT CALL HERE

			jz.db(); <-- noClip method
			______________________________________________________
		 */
    boolean injected = false;
    Method noClip = findStaticMethod("noClip");
    if (noClip == null) {
        throw new InjectionException("Mapped method \"noClip\" could not be found.");
    }
    net.runelite.asm.pool.Method poolNoClip = noClip.getPoolMethod();
    for (ClassFile c : inject.getVanilla().getClasses()) {
        for (Method m : c.getMethods()) {
            if (m.getCode() == null) {
                continue;
            }
            Instructions instructions = m.getCode().getInstructions();
            Set<Label> labels = new HashSet<>();
            // Let's find "invokestatic <some class>.noClip()" and its label
            ListIterator<Instruction> labelIterator = instructions.getInstructions().listIterator();
            while (labelIterator.hasNext()) {
                Instruction i = labelIterator.next();
                if (!(i instanceof InvokeStatic)) {
                    continue;
                }
                InvokeStatic is = (InvokeStatic) i;
                if (!is.getMethod().equals(poolNoClip)) {
                    continue;
                }
                labelIterator.previous();
                Instruction i2 = labelIterator.previous();
                labelIterator.next();
                labelIterator.next();
                // Find the label that marks the code path for the instruction
                if (!(i2 instanceof Label)) {
                    continue;
                }
                // There can be several noClip invocations in a method, so let's catch them all
                labels.add((Label) i2);
            }
            if (labels.isEmpty()) {
                // If we get here, we're either in the wrong method
                // or Jagex has removed the "if (535573958 * kl != -1)"
                logger.debug("Could not find the label for jumping to the " + noClip + " call in " + m);
                continue;
            }
            Set<Label> labelsToInjectAfter = new HashSet<>();
            ListIterator<Instruction> jumpIterator = instructions.getInstructions().listIterator();
            while (jumpIterator.hasNext()) {
                Instruction i = jumpIterator.next();
                if (!(i instanceof JumpingInstruction)) {
                    continue;
                }
                JumpingInstruction ji = (JumpingInstruction) i;
                Label label = null;
                for (Label l : labels) {
                    if (ji.getJumps().contains(l)) {
                        label = l;
                        break;
                    }
                }
                if (label == null) {
                    continue;
                }
                jumpIterator.previous();
                Set<Instruction> insns = new HashSet<>();
                insns.add(jumpIterator.previous());
                insns.add(jumpIterator.previous());
                insns.add(jumpIterator.previous());
                insns.add(jumpIterator.previous());
                // Get the iterator back to i's position
                jumpIterator.next();
                jumpIterator.next();
                jumpIterator.next();
                jumpIterator.next();
                jumpIterator.next();
                /*
						Check that these instruction types are passed into the if-statement:

						ICONST_M1
						GETSTATIC client.kr : I
						LDC 634425425
						IMUL

						We cannot depend on the order of these because of the obfuscation,
						so let's make it easier by just checking that they are there.
					 */
                if (insns.stream().filter(i2 -> i2 instanceof PushConstantInstruction).count() != 2 || insns.stream().filter(i2 -> i2 instanceof IMul).count() != 1 || insns.stream().filter(i2 -> i2 instanceof GetStatic).count() != 1) {
                    continue;
                }
                // At this point, we have found the real injection point
                labelsToInjectAfter.add(label);
            }
            for (Label l : labelsToInjectAfter) {
                InvokeStatic invoke = new InvokeStatic(instructions, new net.runelite.asm.pool.Method(new net.runelite.asm.pool.Class(HOOKS), "drawAfterWidgets", new Signature("()V")));
                instructions.addInstruction(instructions.getInstructions().indexOf(l) + 1, invoke);
                logger.info("injectDrawAfterWidgets injected a call after " + l);
                injected = true;
            }
        }
    }
    if (!injected) {
        throw new InjectionException("injectDrawAfterWidgets failed to inject!");
    }
}
Also used : DeobAnnotations(net.runelite.deob.DeobAnnotations) Logger(org.slf4j.Logger) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) ListIterator(java.util.ListIterator) HOOKS(net.runelite.injector.InjectHookMethod.HOOKS) IMul(net.runelite.asm.attributes.code.instructions.IMul) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Inject(net.runelite.injector.Inject) InvokeStatic(net.runelite.asm.attributes.code.instructions.InvokeStatic) HashSet(java.util.HashSet) ClassFile(net.runelite.asm.ClassFile) Label(net.runelite.asm.attributes.code.Label) Method(net.runelite.asm.Method) Instructions(net.runelite.asm.attributes.code.Instructions) JumpingInstruction(net.runelite.asm.attributes.code.instruction.types.JumpingInstruction) InjectionException(net.runelite.injector.InjectionException) Signature(net.runelite.asm.signature.Signature) Instruction(net.runelite.asm.attributes.code.Instruction) GetStatic(net.runelite.asm.attributes.code.instructions.GetStatic) ClassFile(net.runelite.asm.ClassFile) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) JumpingInstruction(net.runelite.asm.attributes.code.instruction.types.JumpingInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) InjectionException(net.runelite.injector.InjectionException) JumpingInstruction(net.runelite.asm.attributes.code.instruction.types.JumpingInstruction) GetStatic(net.runelite.asm.attributes.code.instructions.GetStatic) Signature(net.runelite.asm.signature.Signature) IMul(net.runelite.asm.attributes.code.instructions.IMul) HashSet(java.util.HashSet) InvokeStatic(net.runelite.asm.attributes.code.instructions.InvokeStatic)

Example 9 with Label

use of net.runelite.asm.attributes.code.Label 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 10 with Label

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

Label (net.runelite.asm.attributes.code.Label)25 Instruction (net.runelite.asm.attributes.code.Instruction)19 Instructions (net.runelite.asm.attributes.code.Instructions)17 ClassGroup (net.runelite.asm.ClassGroup)10 Code (net.runelite.asm.attributes.Code)10 LDC (net.runelite.asm.attributes.code.instructions.LDC)10 Goto (net.runelite.asm.attributes.code.instructions.Goto)9 ILoad (net.runelite.asm.attributes.code.instructions.ILoad)9 IStore (net.runelite.asm.attributes.code.instructions.IStore)9 IfEq (net.runelite.asm.attributes.code.instructions.IfEq)9 VReturn (net.runelite.asm.attributes.code.instructions.VReturn)9 Test (org.junit.Test)9 IMul (net.runelite.asm.attributes.code.instructions.IMul)8 Execution (net.runelite.asm.execution.Execution)8 ArrayList (java.util.ArrayList)7 Deobfuscator (net.runelite.deob.Deobfuscator)7 Method (net.runelite.asm.Method)6 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)6 JumpingInstruction (net.runelite.asm.attributes.code.instruction.types.JumpingInstruction)5 LVTInstruction (net.runelite.asm.attributes.code.instruction.types.LVTInstruction)5