SpiderLabs Blog

Debugging Android Libraries using IDA

Written by Neal Hindocha | Jun 22, 2013 1:33:00 PM

During a recent test, I encountered a native JNI library used by an Android application. I needed to understand this library and what it did, so the first step was to load the library in IDA to see what it looked like. It did not take long until I realized I was looking at obfuscated code that was doing a lot of manipulation on the stack. Understanding the library through static analysis alone would take a long time, so the best way forward would be to combine static and dynamic analysis.

Having debugged a lot of iOS apps using GDB, I started looking at debugging Android apps with GDB. Until this point, DEX2JAR, Smali and some other tools had been sufficient for my Android reversing requirements.

Some research lead me to a discussion at xda-developers about Android debugging through the remote debugging functionality in IDA.

Below is a description of the steps I had to take before I could start debugging the library in question. In this case I used the Android emulator, but this should work just as well on a rooted device.

  1. Create anAndroid Virtual Device (AVD) for use in the emulator and make sure it works.

  2. Start the emulator, and when the emulator is running, use the command "a db devices" to make sure the Android Debug Bridge (adb) is working properly. You should seethe emulator listed if it works. It is also best if only one device is connected, so if you see multiple devices, disconnect the extra ones.

  3. Start the emulator again using the following command: "emulator –avd [AVD_NAME] –partition-size512". It is possible to use a partition size lower(or higher) than 512 although I did not test this.

  4. Run the following commands.

    a db remount
    a db push android_server /system/
    a db shell
    su (probably not needed for the emulator but is necessary if doing this on a rooted device)
    cd /system
    chmod 755 android_server
    ./android_server

    The commands above will begin by mounting the system partition as read-write, and then push the file android_server to the /system folder. This file can be found in the db gsrv folder of IDA. Next, it will create a shell, make the android_server file executable and then run it.

  5. Minimize the windows where android_server is running and open a new command prompt /terminal, and type this command: "adb forward tcp:23946 tcp:23946". This will allow connections to localhost:23946 and forward those to the emulator.

    In my case, I wanted to debug specific functions in the library. Therefore, I used a custom library loader that loaded the library and then stopped execution. I then attached IDA to the running process, moved the instruction pointer to the next instruction to get past the breakpoint, and then jump directly into the function I wanted to debug. This part will be described in detail in upcoming post.

  6. To attach IDA to a running process, just load the library in IDA, go into the debugger options and select the "Remote ARM Linux/Android debugger", go into "Process Options" in the debugger menu, and set the hostname to localhost. If you used the same ports as above, you should not need to change anything else.

  7. Go into the debugger menu again, this time choose "Attach to process…" and you should see a list of all running processes. Select the one you are interested in, attach and start debugging.

Depending on what is being analyzed, it is possible to do a memory dump from the process into IDA and save the IDB. This may be good for future analysis, and there is always a risk of a crash somewhere that can terminate the debug session.

Also, if you have the library or file to be analyzed open in IDA before the debugger is attached, IDA should identify the memory address shift (due to ASLR) and ask if you want to rebase the file / library.