How to use custom renamer with FernFlower?
I currently have a working FernFlower setup that looks like this:
I have a file fernflower.jar
and fern.bat
whose contents are:
title FernFlower
java -jar fernflower.jar -dgs=1 -ren=1 C:\Users\bernhardkiv\Desktop\test\test-obf.jar C:\Users\bernhardkiv\Desktop\test\src
And the argument -ren 1
makes it so that any methods and fields whose names are below 3 characters (or something like that) are renamed to func_blah
or field_blah
(essentially, methods are named func_
and fields are named field_
.
The readme of fernflower has this section:
5. Renaming identifiers
Some obfuscators give classes and their member elements short, meaningless and above all
ambiguous names. Recompiling of such code leads to a great number of conflicts. Therefore it is
advisable to let the decompiler rename elements in its turn, ensuring uniqueness of each identifier.
Option 'ren' (i.e. -ren=1) activates renaming functionality. Default renaming strategy goes as follows:
- rename an element if its name is a reserved word or is shorter than 3 characters
- new names are built according to a simple pattern: (class|method|field)_<consecutive unique number>
You can overwrite this rules by providing your own implementation of the 4 key methods invoked by the decompiler while renaming. Simply pass a class that implements de.fernflower.main.extern.IIdentifierRenamer in the option 'urc' (e.g. -urc=com.mypackage.MyRenamer) to Fernflower.
The class must be available on the application classpath.
Through which I read and understood how it works, but I am having problems adding my own implementation of the rename function.
I have created a class that implements IIdentifierRenamer, the contents of which are as follows:
public class MyRenamer implements IIdentifierRenamer {
@Override
public String getNextClassname(String arg0, String arg1) {
return "_" + arg0 + "_" + arg1;
}
@Override
public String getNextFieldname(String arg0, String arg1, String arg2) {
return "_" + arg0 + "_" + arg1 + "_" + arg2;
}
@Override
public String getNextMethodname(String arg0, String arg1, String arg2) {
return "_" + arg0 + "_" + arg1 + "_" + arg2;
}
@Override
public boolean toBeRenamed(int arg0, String arg1, String arg2, String arg3) {
System.out.println(arg0 + ", " + arg1 + ", " + arg2 + ", arg3");
return true;
}
}
First of all, before asking the question why I am returning all the arguments for the nextMethod / Class / Field methods, it is so that I could study and see what those arguments actually are, because there is absolutely no documentation AFAIK.
I compiled this class into a named jar custre.jar
and put it in the same folder as mine fern.bat
.
Now I'm not sure what to do to continue, please reply if you know anything about FernFlower and if you can help me.
source to share
First, you need to add your custer.jar to your classpath. Then you need to specify a custom renamer class for FernFlower:
java -cp /path/to/custre.jar -jar fernflower.jar -dgs=1 -ren=1 -urc=MyRenamer C:\Users\bernhardkiv\Desktop\test\test-obf.jar C:\Users\bernhardkiv\Desktop\test\src
(Replace /path/to/custre.jar
with the actual path on your system)
source to share
Ok, it took a while to figure it out. First, notice that there is a class in the fernflower source that implements the default rename:
src/org/jetbrains/java/decompiler/modules/renamer/ConverterHelper.java
Copy this file as a template to your location, customize it according to your needs. I changed the package to:
package jimm3rs.renamer;
And added a debug message output to the method toBeRenamed
:
System.out.println("Custom Renamer: processing [" + classname + "]");
Then compiled:
javac -cp ~/fernflower/trunk/fernflower.jar ConverterHelper.java
The next thing is to tell fernflower to use this custom renamer:
java -classpath fernflower.jar:/home/martin/mycode/ org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler -dgs=1 -ren=1 -urc=jimm3rs.renamer.ConverterHelper /home/martin/Downloads/idea-IC-141.1532.4/lib/boot.jar .
Note that the code directory above /home/martin/mycode/
contains a named subdirectory jimm3rs
, which in turn contains a named directory renamer
that stores our custom renamer class. This can be changed to use a jar if you like, with an additional step.
One helpful tip when debugging class loading is to provide a parameter -Dsun.misc.URLClassPath.debug=true
for java.
Finally, I would like to say that using the -jar syntax to run fernflower.jar did not do the trick: the custom class specified via -classpath
is not visible for fernflower.
source to share