Connect with our team of offensive security, AI security and pen testing experts at Black Hat Europe 2023. Learn More

Connect with our team of offensive security, AI security and pen testing experts at Black Hat Europe 2023. Learn More

Managed Detection & Response

Eradicate cyberthreats with world-class intel and expertise

Managed Security Services

Expand your team’s capabilities and strengthen your security posture

Consulting & Professional Services

Tap into our global team of tenured cybersecurity specialists

Penetration Testing

Subscription- or project-based testing, delivered by global experts

Database Security

Get ahead of database risk, protect data and exceed compliance requirements

Email Security & Management

Catch email threats others miss with layered security & maximum control

Co-Managed SOC (SIEM)

Eliminate alert fatigue, focus your SecOps team, stop threats fast, and reduce cyber risk

Microsoft Exchange Server Attacks
Stay protected against emerging threats
Rapidly Secure New Environments
Security for rapid response situations
Securing the Cloud
Safely navigate and stay protected
Securing the IoT Landscape
Test, monitor and secure network objects
Why Trustwave
The Trustwave Approach
Awards and Accolades
Trustwave SpiderLabs Team
Trustwave Fusion Platform
SpiderLabs Fusion Center
Security Operations Centers
Technology Alliance Partners
Key alliances who align and support our ecosystem of security offerings
Trustwave PartnerOne Program
Join forces with Trustwave to protect against the most advance cybersecurity threats
SpiderLabs Blog

Exploring and Modifying Android and Java Applications for Security Research

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:


Where MYAPPNAME is the result from the first command locating the APK. It's usually something like

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: 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++) {
if (declared_methods[i].getLongName().equals(method_name)) {
method_to_patch = declared_methods[i];

if (method_to_patch == null) {
throw new Exception("Method " + method_name + " not found.");

method_to_patch.insertBefore("android.util.Log.i(\"MRAKHMANOV\", $1 + \":\" + $2);");

javac -cp ~/Downloads/platforms/android-28/android.jar:javassist.jar:.
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 .. 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.

Sidebar: prerequisites

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
export JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules'
./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

Latest SpiderLabs Blogs

The 2023 Retail Services Sector Threat Landscape: A Trustwave Threat Intelligence Briefing

The annual holiday shopping season is poised for a surge in spending, a fact well-known to retailers, consumers, and cybercriminals alike. The latter group, however, is poised to exploit any...

Read More

Pwning Electroencephalogram (EEG) Medical Devices by Default

Overall Analysis of Vulnerability Identification – Default Credentials Leading to Remote Code Execution During internal network testing, a document was discovered titled the “XL Security Site...

Read More

Hidden Data Exfiltration Using Time, Literally

I was looking at my watch last week and my attention was moved towards the seconds over at the right of the watch face, incrementing nicely along as you’d expect. Now, I don’t know if I’d just spent...

Read More