Pages

Saturday, December 15, 2012

Finding system calls of Android APKs without rooted device


You can use strace command to find out all the system calls being made from your Android application on emulator if you have source code of the application and running it on Eclipse. I have explained it here. However, if you want to get traces from any application running on your device, you cannot do this by following the steps given in the link. And the reason is that 'strace' command on the device does not have root privileges to execute and so, you will get error 'permission denied' in that case.

There is one work around for this scenario by pushing your APK on Android emulator and then use 'strace' over there. There may be some limitations of emulator which can limit system calls information, but at this stage, I don't know much.

1) Get APK for your application. If you already have installed application which you want to strace, you can get your APK via this method. If you have not installed it and don't want to do either, follow this method or this one, though I would recommend the first one because of easiness.


2) Go to Eclipse => Window => AVD Manager and start any emulator.

3) If you have installed Android SDK with the default settings, you should be able to find 'adb.exe' at "C:\Program Files (x86)\Android\android-sdk\platform-tools" location.

4) Copy your APK file in 'platform-tools' folder on your computer.

5) Go to Start button on your Windows machine, type 'cmd' and you will see command window opened up. Now type 'cd C:\Program Files (x86)\Android\android-sdk\platform-tools' on the command window and you will be directed to platform-tools directory. Type 'ls' to see content of this folder and you can find adb.exe here.

6) Now type 'adb devices' which will show you all the emulators and devices attached with the adb debugger. Since we have just one emulator running in this case, we will find only one emulator information. For example, on my screen, following information is displayed.

List of devices attached
emulator-5554   device

emulator-5554 is emulator ID here. If you have devices attached, you would see their serial numbers here.

7) Type 'adb install <myApp.apk>'. It will take some time and your file will be installed on your emulator.


8) Type 'adb -s emulator-5554 shell' on the command window if you have more than one emulator running otherwise just type 'adb shell'. You will get shell of the emulator or your device. Now if you type 'ls' on the shell, you can see files of your emulator or the current directory of your smart phone.

9) Type 'ps' on the command window to find out which processes are running on your device, and one of them is our uploaded application. It might be hard to find myApp.apk application there, but if you know package name (com.mysite.myapp, for example), you will see the package name in the output of 'ps' command. Just note down is PID. Remember our goal is to find out system calls against our application, so we will use PID there.

10) In my case, I wanted to know trace for system calls in a file for later analysis but my running application was constantly outputting something on the screen, making it harder to analyze on the spot. So I needed some file on the emulator where I could put my data but the problem with the emulator was that I was not able to create a file by simply running 'touch' or 'vim' command. So I used a work around.

Create a file trace.txt on your computer. Go to Eclipse ==>  Windows => Other Perspective => DDMS.

 11) Under the 'File Browser' tab, select 'sdcard' folder, though you can click on any folder.


12) Now upload trace.txt file by clicking on 'Push a file to a device' button.

13) On the command window, move to 'sdcard' or other folder where you have uploaded 'trace.txt' file using 'cd sdcard' command.

Now type 'strace -p <pid> -o trace.txt'. All system calls will be placed in this file when you are playing with the application. When you want to finish stracing your APK, press Ctrl+C when you are on command window to stop this process.

14) You can download this trace.txt file to your computer the way you uploaded this file by clicking on 'Pull a file from the device' button. You can analyze the traces of your APK with this file now.

Happy stracing. =)



No comments:

Post a Comment