Dynamically Debugging an APK with IDA

1. Unpack the APK with apktools

1
java -jar apktools.jar d <yourapk> -f -o <output_dir>

The command expands the APK into the output directory. If the directory structure is unfamiliar, this APK reverse-analysis guide provides an overview.

Most of the Java-layer logic is in classes.dex. Since that is awkward to read directly, I convert it to a JAR next.

2. Decompile the Java layer with dex2jar

1
d2j-dex2jar.sh -d <yourapk>

Drag the resulting JAR into java-decompiler. Reading the Java code first helps identify which functions in the native SO library are worth investigating.

3. Load the relevant SO into IDA for static analysis

Look in the unpacked APK’s lib directory for the SO built for your target architecture. An x86 library is convenient for debugging in an Android Studio emulator. For ARM, use the corresponding IDA debug server: android_server64 for arm64-v8a or android_server for armeabi-v7a. IDA includes them in dbgsrv.

4. Connect a device with adb and set up the environment

First check that adb can see the device:

1
adb devices

Then copy the appropriate debug server to the device and make it executable:

1
2
adb push android_server /data/local/tmp/
adb shell chmod 755 /data/local/tmp/android_server

The author’s non-root setup uses run-as to start the debugger in the app’s context:

1
2
run-as <package-name>
/data/local/tmp/android_server64

In another terminal, forward the debugger port to the device:

1
adb forward tcp:23946 tcp:23946

Install the APK:

1
adb install <yourapk>

Launch its activity in debug mode:

1
adb shell am start -D -n <packageName>/.MainActivity

The app should now wait for a debugger. Add the JDK’s bin directory to your environment variables so that jdb is available, then list the debuggable process IDs:

1
adb jdwp

adb jdwp keeps running. Once you have the PID, stop it with Ctrl+C and forward that process to a local port:

1
adb forward tcp:8700 jdwp:<pid>

Connect jdb to the forwarded port:

1
jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8700

The app should now continue running on the device. In IDA, set the debugger hostname and port, choose attach process, and attach to the running app. You can then set breakpoints in the native code.