During a recent test, Iencountered a native JNI library used by an Androidapplication. I needed to understand this library and what it did, so the firststep was to load the library in IDA to see what it looked like. It did not takelong until I realized I was looking at obfuscated code that was doing a lot ofmanipulation on the stack. Understanding the library through static analysisalone would take a long time, so the best way forward would be to combinestatic and dynamic analysis.
Having debugged a lotof 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 formy Android reversing requirements.
Some research lead meto a discussion at xda-developers about Android debugging through the remotedebugging functionality in IDA.
Below is a descriptionof 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 justas well on a rooted device.
- Create anAndroid Virtual Device (AVD) for use in the emulator and make sure it works.
- Start theemulator, and when the emulator is running, use the command "adb devices" tomake 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 isconnected, so if you see multiple devices, disconnect the extra ones.
- Start theemulator 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.
- Run thefollowing commands.
adb push android_server /system/
su (probably not needed for the emulator but is necessary if doing this on a rooted device)
chmod 755 android_server
The commands above will begin by mounting the system partitionas read-write, and then push the file android_server to the /system folder. This file can be found in the dbgsrv folder of IDA. Next, it willcreate a shell, make the android_server file executable andthen run it.
- Minimizethe windows where android_server is running and open a new command prompt /terminal, and type this command: "adb forward tcp:23946 tcp:23946". This willallow 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, movedthe instruction pointer to the next instruction to get past the breakpoint, and thenjump directly into the function I wanted to debug. This part will be describedin detail in upcoming post.
- To attach IDAto a running process, just load the library in IDA, go into the debuggeroptions and select the "Remote ARM Linux/Android debugger", go into "ProcessOptions" in the debugger menu, and set the hostname to localhost. If you usedthe same ports as above, you should not need to change anything else.
- Go intothe debugger menu again, this time choose "Attach to process…" and you shouldsee a list of all running processes. Select the one you are interested in,attach and start debugging.
Depending on what isbeing analyzed, it is possible to do a memory dump from the process into IDAand save the IDB. This may be good for future analysis, and there is always arisk of a crash somewhere that can terminate the debug session.
Also, if you have thelibrary or file to be analyzed open in IDA before the debugger is attached, IDAshould identify the memory address shift (due to ASLR) and ask if you want torebase the file / library.