CVE-2024-3400: PAN-OS Command Injection Vulnerability in GlobalProtect Gateway. Learn More

CVE-2024-3400: PAN-OS Command Injection Vulnerability in GlobalProtect Gateway. Learn More

Services
Capture
Managed Detection & Response

Eliminate active threats with 24/7 threat detection, investigation, and response.

twi-managed-portal-color
Co-Managed SOC (SIEM)

Maximize your SIEM investment, stop alert fatigue, and enhance your team with hybrid security operations support.

twi-briefcase-color-svg
Advisory & Diagnostics

Advance your cybersecurity program and get expert guidance where you need it most.

tw-laptop-data
Penetration Testing

Test your physical locations and IT infrastructure to shore up weaknesses before exploitation.

twi-database-color-svg
Database Security

Prevent unauthorized access and exceed compliance requirements.

twi-email-color-svg
Email Security

Stop email threats others miss and secure your organization against the #1 ransomware attack vector.

tw-officer
Digital Forensics & Incident Response

Prepare for the inevitable with 24/7 global breach response in-region and available on-site.

tw-network
Firewall & Technology Management

Mitigate risk of a cyberattack with 24/7 incident and health monitoring and the latest threat intelligence.

Solutions
BY TOPIC
Offensive Security
Solutions to maximize your security ROI
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
About Us
Awards and Accolades
Trustwave SpiderLabs Team
Trustwave Fusion Security Operations Platform
Trustwave Security Colony
Partners
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:

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.

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 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

Latest SpiderLabs Blogs

Fake Dialog Boxes to Make Malware More Convincing

Let’s explore how SpiderLabs created and incorporated user prompts, specifically Windows dialog boxes into its malware loader to make it more convincing to phishing targets during a Red Team...

Read More

The Secret Cipher: Modern Data Loss Prevention Solutions

This is Part 7 in my ongoing project to cover 30 cybersecurity topics in 30 weekly blog posts. The full series can be found here. Far too many organizations place Data Loss Prevention (DLP) and Data...

Read More

CVE-2024-3400: PAN-OS Command Injection Vulnerability in GlobalProtect Gateway

Overview A command injection vulnerability has been discovered in the GlobalProtect feature within Palo Alto Networks PAN-OS software for specific versions that have distinct feature configurations...

Read More