Search in sources :

Example 36 with LdcInsnNode

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

the class InstructionPattern method main.

public static void main(String[] args) {
    AbstractInsnNode[] ains = new AbstractInsnNode[] { new LdcInsnNode("ldc"), new VarInsnNode(ASTORE, 0), new LdcInsnNode("ldc") };
    InstructionPattern pattern = new InstructionPattern(new AbstractInsnNode[] { new LdcInsnNode("ldc"), new VarInsnNode(-1, -1) });
    for (AbstractInsnNode ain : ains) {
        if (pattern.accept(ain)) {
            System.out.println(Arrays.toString(pattern.getLastMatch()));
        }
    }
}
Also used : LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 37 with LdcInsnNode

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

the class ReplaceStrings method scanClassNode.

public void scanClassNode(ClassNode classNode) {
    for (Object o : classNode.fields.toArray()) {
        FieldNode f = (FieldNode) o;
        Object v = f.value;
        if (v instanceof String) {
            String s = (String) v;
            if (contains) {
                if (s.contains(originalLDC))
                    f.value = ((String) f.value).replaceAll(originalLDC, newLDC);
            } else {
                if (s.equals(originalLDC))
                    f.value = newLDC;
            }
        }
        if (v instanceof String[]) {
            for (int i = 0; i < ((String[]) v).length; i++) {
                String s = ((String[]) v)[i];
                if (contains) {
                    if (s.contains(originalLDC)) {
                        f.value = ((String[]) f.value)[i].replaceAll(originalLDC, newLDC);
                        String ugh = s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r");
                        frame.appendText(classNode.name + "." + f.name + "" + f.desc + " -> \"" + ugh + "\" replaced with \"" + s.replaceAll(originalLDC, newLDC) + "\"");
                    }
                } else {
                    if (s.equals(originalLDC)) {
                        ((String[]) f.value)[i] = newLDC;
                        String ugh = s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r");
                        frame.appendText(classNode.name + "." + f.name + "" + f.desc + " -> \"" + ugh + "\" replaced with \"" + newLDC + "\"");
                    }
                }
            }
        }
    }
    for (Object o : classNode.methods.toArray()) {
        MethodNode m = (MethodNode) o;
        InsnList iList = m.instructions;
        for (AbstractInsnNode a : iList.toArray()) {
            if (a instanceof LdcInsnNode) {
                if (((LdcInsnNode) a).cst instanceof String) {
                    final String s = (String) ((LdcInsnNode) a).cst;
                    if (contains) {
                        if (s.contains(originalLDC)) {
                            ((LdcInsnNode) a).cst = ((String) ((LdcInsnNode) a).cst).replaceAll(originalLDC, newLDC);
                            String ugh = s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r");
                            frame.appendText(classNode.name + "." + m.name + "" + m.desc + " -> \"" + ugh + "\" replaced with \"" + s.replaceAll(originalLDC, newLDC).replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r") + "\"");
                        }
                    } else {
                        if (s.equals(originalLDC)) {
                            ((LdcInsnNode) a).cst = newLDC;
                            String ugh = s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r");
                            frame.appendText(classNode.name + "." + m.name + "" + m.desc + " -> \"" + ugh + "\" replaced with \"" + newLDC.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r") + "\"");
                        }
                    }
                }
            }
        }
    }
}
Also used : LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 38 with LdcInsnNode

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

the class AllatoriStringDecrypter method scanMethodNode.

public void scanMethodNode(ClassNode classNode, MethodNode methodNode) throws Exception {
    InsnList iList = methodNode.instructions;
    log("Scanning method " + methodNode.name + " of " + classNode.name);
    LdcInsnNode laststringldconstack = null;
    for (AbstractInsnNode i : iList.toArray()) {
        if (i instanceof LdcInsnNode) {
            LdcInsnNode ldcI = (LdcInsnNode) i;
            if (ldcI.cst instanceof String)
                laststringldconstack = ldcI;
            continue;
        } else if (i instanceof MethodInsnNode) {
            MethodInsnNode methodI = (MethodInsnNode) i;
            // Decryption is always a static call - 0xb8 - invokestatic
            if (laststringldconstack != null && methodI.getOpcode() == 0xb8) {
                String decrypterClassName = methodI.owner;
                String decrypterMethodName = methodI.name;
                // Decrypter is always a static method of other class's inner class
                if (decrypterClassName.contains("$")) {
                    byte[] decrypterFileContents = activeContainer.getFileContents(decrypterClassName + ".class");
                    // We have to create new node for editing
                    // Also, one decrypter method could be used for multiple methods in code, what gives us only part of string decrypted
                    ClassNode decrypterClassNode = ASMUtil.bytesToNode(decrypterFileContents);
                    MethodNode decryptermethodnode = ASMUtil.getMethodByName(decrypterClassNode, decrypterMethodName);
                    if (decryptermethodnode != null) {
                        String keyString = (getConstantPoolSize(classNode.name) + classNode.name + methodNode.name + getConstantPoolSize(classNode.name));
                        int newHashCode = keyString.hashCode();
                        scanDecrypter(decryptermethodnode, newHashCode);
                        try {
                            System.out.println("Loading " + decrypterClassName);
                            Class<?> decrypterClassList = BCV.loadClassIntoClassLoader(decrypterClassNode);
                            String decrypted = invokeDecrypter(decrypterClassList, decrypterMethodName, (String) laststringldconstack.cst);
                            if (decrypted != null) {
                                log("Succesfully invoked decrypter method: " + decrypted);
                                laststringldconstack.cst = decrypted;
                                iList.remove(methodI);
                            }
                        } catch (IndexOutOfBoundsException | ClassNotFoundException | IOException e) {
                            e.printStackTrace();
                            log("Could not load decrypter class: " + decrypterClassName);
                        }
                    } else {
                        log("Could not find decrypter method (" + decrypterMethodName + ") of class " + decrypterClassName);
                    }
                }
            }
        } else if (i instanceof InvokeDynamicInsnNode) {
            InvokeDynamicInsnNode methodi = (InvokeDynamicInsnNode) i;
            if (methodi.getOpcode() == 0xba) {
            // TODO: Safe-reflection deobfuscator here
            // Allatori replaces invokeinterface and invokestatic with invokedynamic
            // log(methodi.bsm.getOwner()+" dot "+methodi.bsm.getName());
            // iList.set(methodi, new MethodInsnNode(0xb8, methodi.bsm.getOwner(), methodi.bsm.getName(), methodi.bsm.getDesc(), false));
            }
        }
        laststringldconstack = null;
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) InvokeDynamicInsnNode(org.objectweb.asm.tree.InvokeDynamicInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 39 with LdcInsnNode

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

the class AllatoriStringDecrypter method scanDecrypter.

private boolean scanDecrypter(MethodNode decryptermethodnode, int newHashCode) {
    InsnList iList = decryptermethodnode.instructions;
    AbstractInsnNode insn = null, removeInsn;
    for (AbstractInsnNode i : iList.toArray()) {
        if (i instanceof MethodInsnNode) {
            MethodInsnNode methodi = ((MethodInsnNode) i);
            if (// find code form this instruction
            "currentThread".equals(methodi.name)) {
                insn = i;
                break;
            }
        }
    }
    if (insn == null)
        return false;
    while (insn != null) {
        if (insn instanceof MethodInsnNode) {
            MethodInsnNode methodi = ((MethodInsnNode) insn);
            if (// to this instruction
            "hashCode".equals(methodi.name))
                break;
        }
        removeInsn = insn;
        insn = insn.getNext();
        // and remove it
        iList.remove(removeInsn);
    }
    if (insn == null)
        return false;
    // then replace it with pre-computed key LDC
    iList.set(insn, new LdcInsnNode(newHashCode));
    return true;
}
Also used : LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 40 with LdcInsnNode

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

the class ShowAllStrings method execute.

@Override
public void execute(List<ClassNode> classNodeList) {
    PluginConsole frame = new PluginConsole("Show All Strings");
    StringBuilder sb = new StringBuilder();
    for (ClassNode classNode : classNodeList) {
        for (Object o : classNode.fields.toArray()) {
            FieldNode f = (FieldNode) o;
            Object v = f.value;
            if (v instanceof String) {
                String s = (String) v;
                if (!s.isEmpty())
                    sb.append(classNode.name).append(".").append(f.name).append(f.desc).append(" -> \"").append(s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r")).append("\"").append(nl);
            }
            if (v instanceof String[]) {
                for (int i = 0; i < ((String[]) v).length; i++) {
                    String s = ((String[]) v)[i];
                    if (!s.isEmpty())
                        sb.append(classNode.name).append(".").append(f.name).append(f.desc).append("[").append(i).append("] -> \"").append(s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r")).append("\"").append(nl);
                }
            }
        }
        for (Object o : classNode.methods.toArray()) {
            MethodNode m = (MethodNode) o;
            InsnList iList = m.instructions;
            for (AbstractInsnNode a : iList.toArray()) {
                if (a instanceof LdcInsnNode) {
                    if (((LdcInsnNode) a).cst instanceof String) {
                        final String s = (String) ((LdcInsnNode) a).cst;
                        if (!s.isEmpty())
                            sb.append(classNode.name).append(".").append(m.name).append(m.desc).append(" -> \"").append(s.replaceAll("\\n", "\\\\n").replaceAll("\\r", "\\\\r")).append("\"").append(nl);
                    }
                }
            }
        }
    }
    frame.setText(sb.toString());
    frame.setVisible(true);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) PluginConsole(the.bytecode.club.bytecodeviewer.api.PluginConsole) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

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