Search in sources :

Example 41 with LdcInsnNode

use of org.objectweb.asm.tree.LdcInsnNode in project bytecode-viewer by Konloch.

the class EZInjection method execute.

@Override
public void execute(List<ClassNode> classNodeList) {
    if (console)
        new PluginConsole("EZ Injection v" + version);
    if (accessModifiers)
        print("Setting all of the access modifiers to public/public static.");
    if (injectHooks)
        print("Injecting hook...");
    if (debugHooks)
        print("Hooks are debugging.");
    else if (injectHooks)
        print("Hooks are not debugging.");
    else
        print("Hooks are disabled completely.");
    if (useProxy)
        print("Forcing proxy as '" + proxy + "'.");
    if (launchKit)
        print("Launching the Graphicial Reflection Kit upon a succcessful invoke of the main method.");
    // force everything to be public
    for (ClassNode classNode : classNodeList) {
        for (Object o : classNode.fields.toArray()) {
            FieldNode f = (FieldNode) o;
            if (accessModifiers) {
                if (f.access == Opcodes.ACC_PRIVATE || f.access == Opcodes.ACC_PROTECTED)
                    f.access = Opcodes.ACC_PUBLIC;
                if (f.access == Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC || f.access == Opcodes.ACC_PROTECTED + Opcodes.ACC_STATIC)
                    f.access = Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC;
                if (f.access == Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL || f.access == Opcodes.ACC_PROTECTED + Opcodes.ACC_FINAL)
                    f.access = Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL;
                if (f.access == Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC || f.access == Opcodes.ACC_PROTECTED + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC)
                    f.access = Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC;
            }
        }
        for (Object o : classNode.methods.toArray()) {
            MethodNode m = (MethodNode) o;
            if (accessModifiers) {
                if (m.access == Opcodes.ACC_PRIVATE || m.access == Opcodes.ACC_PROTECTED)
                    m.access = Opcodes.ACC_PUBLIC;
                if (m.access == Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC || m.access == Opcodes.ACC_PROTECTED + Opcodes.ACC_STATIC)
                    m.access = Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC;
                if (m.access == Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL || m.access == Opcodes.ACC_PROTECTED + Opcodes.ACC_FINAL)
                    m.access = Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL;
                if (m.access == Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC || m.access == Opcodes.ACC_PROTECTED + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC)
                    m.access = Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC;
            }
            if (injectHooks && m.access != Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_PRIVATE + Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_PROTECTED + Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_FINAL + Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_PROTECTED + Opcodes.ACC_FINAL + Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC + Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC + Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_PROTECTED + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC + Opcodes.ACC_ABSTRACT) {
                boolean inject = true;
                if (m.instructions.size() >= 2 && m.instructions.get(1) instanceof MethodInsnNode) {
                    MethodInsnNode mn = (MethodInsnNode) m.instructions.get(1);
                    // already been injected
                    if (mn.owner.equals(EZInjection.class.getName().replace(".", "/")))
                        inject = false;
                }
                if (inject) {
                    // make this function grab parameters eventually
                    m.instructions.insert(new MethodInsnNode(Opcodes.INVOKESTATIC, EZInjection.class.getName().replace(".", "/"), "hook", "(Ljava/lang/String;)V"));
                    m.instructions.insert(new LdcInsnNode(classNode.name + "." + m.name + m.desc));
                }
            }
        }
    }
    if (useProxy) {
        try {
            String[] split = proxy.split(":");
            setProxy(split[0], split[1]);
        } catch (Exception e) {
        // ignore
        }
    }
    print("Done setting up.");
    setFinished();
    if (invokeMethod) {
        // start print debugging
        BytecodeViewer.sm.setPrinting(true);
        // load all the classnodes into the classloader
        for (ClassNode cn : BytecodeViewer.getLoadedClasses()) BCV.getClassNodeLoader().addClass(cn);
        print("Attempting to find " + invokeMethodInformation + ":" + nl + nl);
        for (ClassNode classNode : classNodeList) {
            for (Object o : classNode.methods.toArray()) {
                MethodNode m = (MethodNode) o;
                String methodInformation = classNode.name + "." + m.name + m.desc;
                if (invokeMethodInformation.equals(methodInformation)) {
                    for (Method m2 : BCV.getClassNodeLoader().nodeToClass(classNode).getMethods()) {
                        if (m2.getName().equals(m.name)) {
                            print("Invoking " + invokeMethodInformation + ":" + nl + nl);
                            GraphicalReflectionKit kit = launchKit ? new GraphicalReflectionKit() : null;
                            try {
                                if (kit != null)
                                    kit.setVisible(true);
                                m2.invoke(classNode.getClass().getDeclaredConstructor().newInstance(), (Object[]) new String[1]);
                                print("Finished running " + invokeMethodInformation);
                            } catch (Exception e) {
                                StringWriter sw = new StringWriter();
                                e.printStackTrace(new PrintWriter(sw));
                                e.printStackTrace();
                                print(sw.toString());
                            } finally {
                                // disable print debugging
                                BytecodeViewer.sm.setPrinting(false);
                                if (kit != null)
                                    kit.setVisible(false);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) Method(java.lang.reflect.Method) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) GraphicalReflectionKit(the.bytecode.club.bytecodeviewer.gui.plugins.GraphicalReflectionKit) MethodNode(org.objectweb.asm.tree.MethodNode) StringWriter(java.io.StringWriter) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) PluginConsole(the.bytecode.club.bytecodeviewer.api.PluginConsole) PrintWriter(java.io.PrintWriter)

Example 42 with LdcInsnNode

use of org.objectweb.asm.tree.LdcInsnNode in project bytecode-viewer by Konloch.

the class MalwareCodeScanner method scanMethods.

@Override
public void scanMethods(MalwareScan scan, ClassNode cn, MethodNode[] methods) {
    for (MethodNode method : methods) {
        InsnList instructionList = method.instructions;
        // scan each instruction
        for (AbstractInsnNode instruction : instructionList.toArray()) {
            scanMethodInstruction(scan, cn, method, instruction);
            if (instruction instanceof LdcInsnNode) {
                if (((LdcInsnNode) instruction).cst instanceof String) {
                    final String string = (String) ((LdcInsnNode) instruction).cst;
                    scanMethodString(scan, cn, method, new SearchableString(string));
                }
            }
        }
    }
}
Also used : LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) SearchableString(the.bytecode.club.bytecodeviewer.malwarescanner.util.SearchableString) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) SearchableString(the.bytecode.club.bytecodeviewer.malwarescanner.util.SearchableString)

Example 43 with LdcInsnNode

use of org.objectweb.asm.tree.LdcInsnNode in project malmo by Microsoft.

the class OverclockingClassTransformer method removeInterpolation.

private static void removeInterpolation(ClassNode node, boolean isObfuscated) {
    // We're attempting to turn this line from EntityOtherPlayerMP.func_180426_a:
    // this.otherPlayerMPPosRotationIncrements = p_180426_9_;
    // into this:
    // this.otherPlayerMPPosRotationIncrements = 1;
    final String methodName = "func_180426_a";
    // double x/y/z, float yaw/pitch, int increments, bool (unused), returns void.
    final String methodDescriptor = "(DDDFFIZ)V";
    System.out.println("MALMO: Found EntityOtherPlayerMP, attempting to transform it");
    for (MethodNode method : node.methods) {
        if (method.name.equals(methodName) && method.desc.equals(methodDescriptor)) {
            System.out.println("MALMO: Found EntityOtherPlayerMP.func_180426_a() method, attempting to transform it");
            for (AbstractInsnNode instruction : method.instructions.toArray()) {
                if (instruction.getOpcode() == Opcodes.ILOAD) {
                    LdcInsnNode newNode = new LdcInsnNode(new Integer(1));
                    method.instructions.insert(instruction, newNode);
                    method.instructions.remove(instruction);
                    return;
                }
            }
        }
    }
}
Also used : LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 44 with LdcInsnNode

use of org.objectweb.asm.tree.LdcInsnNode in project evosuite by EvoSuite.

the class Instrumenter method instrumentGETXXXFieldAccesses.

private void instrumentGETXXXFieldAccesses(final ClassNode cn, final String internalClassName, final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;
    AbstractInsnNode ins = null;
    FieldInsnNode fieldIns = null;
    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;
            /*
				 * Is field referencing outermost instance? if yes, ignore it
				 * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
				 */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }
            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.GETFIELD || opcode == Opcodes.GETSTATIC) {
                final InsnList il = new InsnList();
                if (opcode == Opcodes.GETFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        il.add(new InsnNode(Opcodes.SWAP));
                    } else if (fieldType.getSize() == 2) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        // v
                        // GETFIELD
                        // v, w
                        il.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        il.add(new InsnNode(Opcodes.POP2));
                    // w, v
                    // -> Call
                    // w
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));
                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));
                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.FieldRegistry.class), "notifyReadAccess", "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));
                i += il.size();
                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 45 with LdcInsnNode

use of org.objectweb.asm.tree.LdcInsnNode in project evosuite by EvoSuite.

the class Instrumenter method addCaptureCall.

private InsnList addCaptureCall(final boolean isStatic, final String internalClassName, final String methodName, final String methodDesc, final Type[] argTypes) {
    // construction of
    // Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams)
    // call
    final InsnList il = new InsnList();
    il.add(new LdcInsnNode(this.captureId));
    // --- load receiver argument
    int varIndex;
    if (isStatic) {
        // static method invocation
        il.add(new LdcInsnNode(internalClassName));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(CaptureUtil.class), "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
        varIndex = 0;
    } else {
        // non-static method call
        il.add(new VarInsnNode(Opcodes.ALOAD, 0));
        varIndex = 1;
    }
    // --- load method name argument
    il.add(new LdcInsnNode(methodName));
    // --- load method description argument
    il.add(new LdcInsnNode(methodDesc));
    // --- load methodParams arguments
    // load methodParams length
    // TODO ICONST_1 to ICONST_5 would be more efficient
    il.add(new IntInsnNode(Opcodes.BIPUSH, argTypes.length));
    // create array object
    il.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));
    for (int i = 0; i < argTypes.length; i++) {
        il.add(new InsnNode(Opcodes.DUP));
        // TODO ICONST_1 to ICONST_5 would be more efficient
        il.add(new IntInsnNode(Opcodes.BIPUSH, i));
        // check for primitives
        this.loadAndConvertToObject(il, argTypes[i], varIndex++);
        il.add(new InsnNode(Opcodes.AASTORE));
        // long/double take two registers
        if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
            varIndex++;
        }
    }
    // --- construct Capture.capture() call
    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.Capturer.class), "capture", "(ILjava/lang/Object;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V"));
    return il;
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Aggregations

LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)50 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)32 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)30 InsnList (org.objectweb.asm.tree.InsnList)22 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)18 InsnNode (org.objectweb.asm.tree.InsnNode)18 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)18 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)15 Type (org.objectweb.asm.Type)14 MethodNode (org.objectweb.asm.tree.MethodNode)10 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)9 LabelNode (org.objectweb.asm.tree.LabelNode)8 IntInsnNode (org.objectweb.asm.tree.IntInsnNode)7 Label (org.objectweb.asm.Label)6 ClassNode (org.objectweb.asm.tree.ClassNode)5 FieldNode (org.objectweb.asm.tree.FieldNode)4 Mutation (org.evosuite.coverage.mutation.Mutation)3 IincInsnNode (org.objectweb.asm.tree.IincInsnNode)3 LookupSwitchInsnNode (org.objectweb.asm.tree.LookupSwitchInsnNode)3 TableSwitchInsnNode (org.objectweb.asm.tree.TableSwitchInsnNode)3