Search in sources :

Example 91 with MethodNode

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

the class ClassNodeDecompiler method decompile.

public static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, List<String> decompiledClasses, ClassNode cn) {
    List<String> unableToDecompile = new ArrayList<>();
    decompiledClasses.add(cn.name);
    sb.append(getAccessString(cn.access));
    sb.append(" ");
    sb.append(cn.name);
    if (cn.superName != null && !cn.superName.equals("java/lang/Object")) {
        sb.append(" extends ");
        sb.append(cn.superName);
    }
    int amountOfInterfaces = cn.interfaces.size();
    if (amountOfInterfaces > 0) {
        sb.append(" implements ");
        sb.append(cn.interfaces.get(0));
        for (int i = 1; i < amountOfInterfaces; i++) {
            sb.append(", ");
            sb.append(cn.interfaces.get(i));
        }
    }
    sb.append(" {");
    sb.append(nl);
    sb.append("     ");
    sb.append("<ClassVersion=" + cn.version + ">");
    sb.append(nl);
    if (cn.sourceDebug != null) {
        sb.append("     ");
        sb.append("<SourceDebug=" + cn.sourceDebug + ">");
        sb.append(nl);
    }
    if (cn.sourceFile != null) {
        sb.append("     ");
        sb.append("<SourceFile=" + cn.sourceFile + ">");
        sb.append(nl);
    }
    if (cn.signature != null) {
        sb.append("     ");
        sb.append("<Sig=" + cn.signature + ">");
    }
    for (FieldNode fn : cn.fields) {
        sb.append(nl);
        sb.append("     ");
        FieldNodeDecompiler.decompile(sb, fn);
    }
    if (cn.fields.size() > 0) {
        sb.append(nl);
    }
    for (MethodNode mn : cn.methods) {
        sb.append(nl);
        MethodNodeDecompiler.decompile(sb, mn, cn);
    }
    for (InnerClassNode o : cn.innerClasses) {
        String innerClassName = o.name;
        if ((innerClassName != null) && !decompiledClasses.contains(innerClassName)) {
            decompiledClasses.add(innerClassName);
            ClassNode cn1 = BytecodeViewer.blindlySearchForClassNode(innerClassName);
            if (cn1 != null) {
                sb.appendPrefix("     ");
                sb.append(nl + nl);
                sb = decompile(sb, decompiledClasses, cn1);
                sb.trimPrefix(5);
                sb.append(nl);
            } else {
                unableToDecompile.add(innerClassName);
            }
        }
    }
    if (!unableToDecompile.isEmpty()) {
        sb.append("// The following inner classes couldn't be decompiled: ");
        for (String s : unableToDecompile) {
            sb.append(s);
            sb.append(" ");
        }
        sb.append(nl);
    }
    if (cn.attrs != null) {
        sb.append(nl);
        for (Attribute attr : cn.attrs) {
            // TODO: finish attributes
            // + attr.content.toString());
            sb.append(attr.type + ": ");
        }
    }
    // sb.append(BytecodeViewer.nl);
    sb.append("}");
    // " with prefix length: " + sb.prefix.length());
    return sb;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) InnerClassNode(org.objectweb.asm.tree.InnerClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) Attribute(org.objectweb.asm.Attribute) ArrayList(java.util.ArrayList) InnerClassNode(org.objectweb.asm.tree.InnerClassNode)

Example 92 with MethodNode

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

the class RenameMethods method obfuscate.

@Override
public void obfuscate() {
    int stringLength = getStringLength();
    System.out.println("Obfuscating method names...");
    for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
        for (Object o : c.methods.toArray()) {
            MethodNode m = (MethodNode) o;
            /* As we dont want to rename native dll methods */
            if ((m.access & Opcodes.ACC_NATIVE) != 0)
                continue;
            if (m.access != Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_STATIC && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_STATIC + Opcodes.ACC_PRIVATE && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_STATIC + Opcodes.ACC_PROTECTED && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_PUBLIC && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_PRIVATE && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_PROTECTED) {
                if (!m.name.equals("main") && !m.name.equals("<init>") && !m.name.equals("<clinit>")) {
                    String newName = generateUniqueName(stringLength);
                    BytecodeViewer.refactorer.getHooks().addMethod(new MethodMappingData(c.name, new MappingData(m.name, newName), m.desc));
                /*ASMUtil_OLD.renameMethodNode(c.name, m.name, m.desc,
								null, newName, null);*/
                }
            }
        }
    }
    System.out.println("Obfuscated method names.");
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodMappingData(the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MethodMappingData) MappingData(the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MappingData) MethodMappingData(the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MethodMappingData)

Example 93 with MethodNode

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

the class LDCSearch method search.

public void search(final ResourceContainer container, final String resourceWorkingName, final ClassNode node, boolean caseSensitive) {
    final Iterator<MethodNode> methods = node.methods.iterator();
    final String srchText = searchText.getText();
    final String srchTextLowerCase = searchText.getText().toLowerCase();
    if (srchText.isEmpty())
        return;
    while (methods.hasNext()) {
        final MethodNode method = methods.next();
        final InsnList insnlist = method.instructions;
        for (AbstractInsnNode insnNode : insnlist) {
            if (insnNode instanceof LdcInsnNode) {
                final LdcInsnNode ldcObject = ((LdcInsnNode) insnNode);
                final String ldcString = ldcObject.cst.toString();
                // TODO re-add this at some point when the search pane is redone
                boolean exact = false;
                final boolean exactMatch = exact && ldcString.equals(srchText);
                final boolean caseInsensitiveMatch = !exact && caseSensitive && ldcString.contains(srchText);
                final boolean caseSensitiveMatch = !exact && !caseSensitive && ldcString.toLowerCase().contains(srchTextLowerCase);
                final boolean anyMatch = exactMatch || caseInsensitiveMatch || caseSensitiveMatch;
                if (anyMatch) {
                    BytecodeViewer.viewer.searchBoxPane.treeRoot.add(new LDCSearchTreeNodeResult(container, resourceWorkingName, node, method, null, ldcString, ldcObject.cst.getClass().getCanonicalName()));
                }
            }
        }
    }
    final Iterator<FieldNode> fields = node.fields.iterator();
    while (methods.hasNext()) {
        final FieldNode field = fields.next();
        if (field.value instanceof String) {
            BytecodeViewer.viewer.resourcePane.treeRoot.add(new LDCSearchTreeNodeResult(container, resourceWorkingName, node, null, field, String.valueOf(field.value), field.value.getClass().getCanonicalName()));
        }
    }
}
Also used : LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) FieldNode(org.objectweb.asm.tree.FieldNode) LDCSearchTreeNodeResult(the.bytecode.club.bytecodeviewer.searching.LDCSearchTreeNodeResult) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 94 with MethodNode

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

the class ShowMainMethods method execute.

@Override
public void execute(List<ClassNode> classNodeList) {
    PluginConsole frame = new PluginConsole("Show Main Methods");
    StringBuilder sb = new StringBuilder();
    for (ClassNode classNode : classNodeList) {
        for (Object o : classNode.methods.toArray()) {
            MethodNode m = (MethodNode) o;
            if ((m.access & (PUBLIC_STATIC)) == PUBLIC_STATIC) {
                if (m.name.equals("main") && m.desc.equals("([Ljava/lang/String;)V")) {
                    sb.append(classNode.name);
                    sb.append(".");
                    sb.append(m.name);
                    sb.append(m.desc);
                    sb.append("\n");
                }
            }
        }
    }
    if (sb.length() == 0)
        frame.appendText("No main methods found.");
    else
        frame.appendText(sb.toString());
    frame.setVisible(true);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) PluginConsole(the.bytecode.club.bytecodeviewer.api.PluginConsole)

Example 95 with MethodNode

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

the class RenameMethods method obfuscate.

@Override
public void obfuscate() {
    int stringLength = getStringLength();
    System.out.println("Obfuscating method names...");
    for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
        for (Object o : c.methods.toArray()) {
            MethodNode m = (MethodNode) o;
            if (m.access != Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_STATIC && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_STATIC + Opcodes.ACC_PRIVATE && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_STATIC + Opcodes.ACC_PROTECTED && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_PUBLIC && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_PRIVATE && m.access != Opcodes.ACC_ABSTRACT + Opcodes.ACC_PROTECTED) {
                if (!m.name.equals("main") && !m.name.equals("<init>") && !m.name.equals("<clinit>")) {
                    String newName = generateUniqueName(stringLength);
                    ASMResourceUtil.renameMethodNode(c.name, m.name, m.desc, null, newName, null);
                }
            }
        }
    }
    System.out.println("Obfuscated method names.");
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode)

Aggregations

MethodNode (org.objectweb.asm.tree.MethodNode)322 ClassNode (org.objectweb.asm.tree.ClassNode)123 Test (org.junit.Test)94 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)59 ClassReader (org.objectweb.asm.ClassReader)57 InsnList (org.objectweb.asm.tree.InsnList)49 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)47 Label (org.objectweb.asm.Label)44 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)41 InsnNode (org.objectweb.asm.tree.InsnNode)34 ClassWriter (org.objectweb.asm.ClassWriter)26 FieldNode (org.objectweb.asm.tree.FieldNode)26 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 ArrayList (java.util.ArrayList)24 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)24 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)21 LabelNode (org.objectweb.asm.tree.LabelNode)19 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)19 List (java.util.List)17 Type (org.objectweb.asm.Type)17