Search code examples
javareverse-engineeringbytecodejava-bytecode-asm

Java reverse all Strings in a Jar with ASM


I tried to make a program that reverse every string in a jar and then do the same again, to make it be like a string obfuscation. eg.

Normal Code: new String("example");

After running: new String(new StringBuilder().append("elpmaxe").reverse().toString()

My Code:

public class Main {
static String obfuscationFile;

public static void main(String[] args) throws IOException {
    obfuscationFile = "C:\\Users\\Leonhard\\Desktop\\CrackingTools-v1.0.jar"; // TODO:
                                                                                // args[0]
    File jar = new File(obfuscationFile);
    Map<String, byte[]> out = JarUtil.loadNonClassEntries(jar);
    Map<String, ClassNode> nodes = JarUtil.loadClasses(jar);
    for (ClassNode cn : nodes.values()) {
        for (Object mn : cn.methods) {
            MethodNode mnode = (MethodNode) mn;
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            cn.accept(cw);
            if (mnode.name.startsWith("")) {
                for (Integer i : reverse(mnode, cw)) {
                    // TODO: Not needed..
                }
            }
            out.put(cn.name, cw.toByteArray());
        }
    }
    JarUtil.saveAsJarAndClasses(out, nodes, jar.getAbsolutePath().replace(".jar", "") + "_Reverse" + ".jar");
}

private static ArrayList<Integer> reverse(MethodNode method, ClassWriter cw) {
    ArrayList<Integer> i = new ArrayList<Integer>();
    int e = 0;
    for (AbstractInsnNode ain : method.instructions.toArray()) {
        e++;
        if (ain.getOpcode() == Opcodes.LDC) {
            if (ain instanceof LdcInsnNode) {
                LdcInsnNode ldc = (LdcInsnNode) ain;
                if (ldc.cst instanceof String) {
                    i.add(e);
                    ldc.cst = new StringBuilder().append(ldc.cst).reverse().toString();
                    MethodVisitor mv = null;
                    mv = cw.visitMethod(method.access, method.name, method.desc, method.signature,
                            (String[]) method.exceptions.toArray(new String[method.exceptions.size()]));
                    if (mv != null) {
                        // mv.visitLineNumber(e, new Label());
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                                "(Ljava/lang/String;)Ljava/lang/StringBuilder;", true);
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "reverse",
                                "()Ljava/lang/StringBuilder;", true);
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString",
                                "()Ljava/lang/String;", true);
                        mv.visitMaxs(0, 0);
                        mv.visitEnd();
                        // System.out.println(method.name + " " +
                        // method.desc);
                    }
                }
            }
        }
    }
    return i;
}

(Using ASM 5.0.4)

Where is my mistake / what did i do wrong with mv.visitMethodInsn()?

EDIT: I noticed, it's working with -noverify (but all strings are reversed). What can I do to get it working without it?


Solution

  • It's hard to tell exactly what's wrong without any information about the errors or problems you are experiencing. However, there is one mistake I noticed:

                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                            "(Ljava/lang/String;)Ljava/lang/StringBuilder;", true);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "reverse",
                            "()Ljava/lang/StringBuilder;", true);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString",
                            "()Ljava/lang/String;", true);
    

    In this code, you forgot to actually create the StringBuilder you are using. You'll need to insert a new and invokespecial instruction to create a new StringBuilder.

    On a side note, the append call is unnecessary. You can pass a string directly to the ctor of StringBuilder.