Sometimes pentesters and security researchers need to modify existing Java application but have no access to its source. For example, it might be necessary to adjust the logic a bit to see how the application works in certain specific conditions. However, if the application is complex, it might not be easy to decompile it back to source form to make changes. Additionally, many of existing Java decompilers (jad, JD-GUI, fernflower) fail in some cases because of heavy obfuscation or complex code structures.
Here is a solution for such cases: just use binary patching of Java classes. There are several options there, but we will use the Javassist library which allows for code modifications. In our example, we will modify a hypothetical Android application to log (print) one of its method input values to the system log. In reality, this might help spot security issues like untrusted input passed to certain functions.
First, we need to get a copy of the application APK file. To do that, we connect the phone with the installed application to a computer with installed Android SDK platform tools via USB. We then run the following commands:
~/Downloads/platform-tools/adb shell
We locate the APK file using the shell:
pm list package MYAPP
We find the path to the APK:
pm path MYAPPNAME
Where MYAPPNAME is the result from the first command locating the APK. It's usually something like com.abc.efg.
Finally, exit the shell and execute:
~/Downloads/platform-tools/adb pull PATH_TO_APK
An APK file is just an archive so we can unpack it using the "jar" command:
mkdir Unpacked
cd Unpacked
jar xvf ../base.apk
Our first task is to remove the META-INF/*.RSA files so that newly signed APK will be valid.
Now we have a directory structure with unpacked classes and other files. The classes are in "dex" format - so we need to convert them to jar files with classes first. Download the dex2jar toolkit and do:
d2j-dex2jar.sh classes3.dex
mv classes3-dex2jar.jar classes3.jar
jar xvf classes3.jar
You should now be able to locate the class you need to patch. We will patch "okhttp3.Request" as an example and do the change with the following Java helper application:
import javassist.*;
public class patch_okhttp3 {
public static void main(String[] argv) throws Exception {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get("okhttp3.Request$Builder");
CtMethod method_to_patch = null;
String method_name = "okhttp3.Request$Builder.addHeader(java.lang.String,java.lang.String)";
CtMethod[] declared_methods = cc.getDeclaredMethods();
for (int i = 0; i < declared_methods.length; i++) {
System.out.println(declared_methods[i].getLongName());
if (declared_methods[i].getLongName().equals(method_name)) {
method_to_patch = declared_methods[i];
break;
}
}
if (method_to_patch == null) {
throw new Exception("Method " + method_name + " not found.");
}
method_to_patch.insertBefore("android.util.Log.i(\"MRAKHMANOV\", $1 + \":\" + $2);");
cc.writeFile();
}
}
javac -cp ~/Downloads/platforms/android-28/android.jar:javassist.jar:. patch_okhttp3.java
java -cp ~/Downloads/platforms/android-28/android.jar:javassist.jar:. patch_okhttp3
Once the above is executed, "okhttp3.Request"'s method addHeader will be patched to log its two arguments to the system log. You will need "android.jar" reference to resolve Log.i call, so make sure you have android-28 downloaded.
Now we will pack the files back to "jar" and convert it back to "dex":
jar cvf ../classes3.jar .
cd ..
d2j-jar2dex.sh classes3.jar .
mv classes3-jar2dex.dex classes3.dex
Replace original classes3.dex with your new patched copy and then produce a new APK:
jar cvf ~/demo.apk .
We'll then need to sign it and align:
jarsigner -keystore ~/Documents/my-release-key.jks ~/demo.apk mykey
~/Downloads/build-tools/28.0.3/zipalign -v -p 4 ~/demo.apk ~/demo-aligned.apk
Now that we have the new, modified APK we can upload it to the device. Here we use an SD card as the destination since it is writable:
~/Downloads/platform-tools/adb push ~/demo-aligned.apk /sdcard/demo.apk
Finally we replace existing application on the device:
~/Downloads/platform-tools/adb shell "pm install -r /sdcard/demo.apk"
We can now launch the application and observe the changes. If we had a regular Java application, then most of the above applies too with exception of not needing to upload or install it on the device.
You may need to install some tools and create a self-signed certificate first. Below are steps tested on macOS.
Steps to get the zipalign tool. You should do it in some temp directory and replace versions with current:
curl -v -O https://dl.google.com/android/repository/sdk-tools-darwin-4333796.zip
unzip sdk-tools-darwin-4333796.zip
export JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'
./tools/bin/sdkmanager "build-tools;28.0.3"
Note that there is build-tools directory which contains the zipalign tool. Similar for the platform download:
./tools/bin/sdkmanager "platforms;android-28"
Self-signed certificate generation:
keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias