Search code examples
javabytecodejava-bytecode-asmbytecode-manipulation

Transforming FieldInsnNode's names and information


I'm attempting to deobfuscate a .jar's code. I've created a module that successfully renames the methods (including return type), the class and its superclass, and its fields.

My issue now is in the actual bytecode. I'm attempting to modify the FieldInsnNode and MethodInsnNode's values in order to change their names. Example: g.y.x(); should be node.next.generateHash().

MethodInsnNode min = (MethodInsnNode) insn;

min.name = remappedNames.getOrDefault(min.owner + "." + min.name + min.desc, min.name);
min.owner = remappedNames.getOrDefault(min.owner, min.owner);
min.desc = transformMethodDesc(min.desc);

I think I'm misunderstanding what the name, owner, and desc are. Here's my current understanding:

name = the actual name of the variable. E.g. in my earlier example, next's field obfuscated name value is y.

The owner is a little more confusing, I'm not too sure exactly what that is, I'm guessing its the class that the variable responds to. For example the owner here would be g.

Would appreciate any clarification, as even when I do something like min.name = "TEST" I'm not noticing any difference in the decompiled output.


Solution

  • I ended up figuring out the answer to the question.

    Field/Method nodes are a representation of the class, but obviously changing them won't change anything in the class itself. In order to change the classes you have to output it back to the disk using something like a JarOutputStream and a ClassWriter.

    Simple example:

    JarOutputStream jos = ...
    ClassWriter cw = new ClassWriter();
    cw.accept(myClassNode);
    byte[] bytes = cw.toByteArray();
    

    Then you can write the bytes back to disk

    Hope that helps!