Pages

Friday, January 25, 2013

Installing Android SDK and Eclipse on Linux (Ubuntu)


I would recommend you to follow each step as it is. If it asks you create a directory with specific name, do it to avoid any hassle during installation.

I will provide a link for setting up Android development environment just before at the end of post. Following steps in the link, you will be asked to download softwares from product home pages and it's sometimes confusing what product exactly do you need. So remember these links as reference for downloading softwares.

Step 1:

Download Android SDK. Don't click on first button you see there, instead click on 'DOWNLOAD FOR OTHER PLATFORMS', go to table 'SDK Tools Only' and choose the appropriate binaries for your OS.

Step 2:
 
Download Eclipse. I downloaded 'Eclipse IDE for Java EE Developers', however if you know better which other version can be more suitable for you, go happily for it.

Step 3:
 
When it asks you to 'Install ADT plugin for eclipse', browse to  'opt ==> eclipse' directory and run eclipse, remember not to click on icon.pxm.

Step 4:

And when it asks you to open eclipse and go to 'Window => Android SDK and AVD Manager', you might not see 'Android SDK and AVD Manager' under Window. It was annoying for me not to see this option as I was following the steps like a religious person.

Anyway, Go to Window => Customize Perspective and click on tab 'Command Groups Availability'. Enable the check 'Android SDK and AVD Manager'.

Now, here is the excellent guide for setting up Android development environment.

P.S. If you have installed Java-5 for compiling android source code, you might face a problem that you wont be able to see Android after clicking on Preferences menu item. And reason I have seen it on the internet is that JRE in Eclipse is pointing to JDK/JRE 5 which should be pointing to 6 or 7. I am not sure right now. If I found some other reason, I would try to update this asap.


Friday, January 18, 2013

Installing Ubuntu 12.04/12.10 on 64-bit machine


I was stuck at one thing again during installation of Ubuntu on my machine, so I decided to write this post and took some screenshots during installation.

There are two ways to install Ubuntu I have tried on my laptop, one is within Windows 7 using wubi.exe and second is through burning CD/DVD and install it. During my experience, I found first wubi.exe installation very easy but it gave me error about grup this time after installing wubi.exe and rebooting the machine.

Anyway, I shall be doing installation through DVD burning in this post.

Step 1:

Download iso file from here. Download LTS version as it is more stable than the other one. In my case, I downloaded 64-bit version of 12.04.

Step 2:

Right click on iso file and 'open with' it with 'Windows Disk Image Burner'.



Step 3:

Select DVD drive, select the checkbox titled 'Verify disc after burning' and click on 'Burn' button. Let it burn.





Step 4:

Before proceeding further, you should have at least one drive empty for Ubuntu installation.

If you don't know, you can use Windows Disk Management by go to Start => Make a right click on My Computer icon => Manage => Disk Management. If you don't want to follow this path, you can use Administrative Tools in Control Panel. Empty a drive and now delete this partition.

Remember it's size and it's better to have unique size with respect to other partitions which helps us later.

Step 5:

Keep this DVD in the DVD drive and restart your machine. It will start doing ubuntu installation automatically after detecting bootable DVD otherwise you may have to change boot preferences to boot from CD/DVD.

Step 6:

Ubuntu will check internet connection and other memory requirements. If you have wireless connection, connect it before proceeding. Click Continue to proceed.



Step 7:

It will select language 'English' by default. Click on 'install Ubuntu'. In my case, its not showing it because I used 'try ubuntu' to take screen shots.


Step 8:

Ubuntu asks for installation types and select the following configurations.


Step 9:

Now from steps 4, we know the size of the drive for ubuntu installation. I don't know other way, so I used this trick. In my machine, I had reserved ~40GB for it.

Select that sda/drive with your specific size and click on 'change' button. This is just one way to identity, you can probably name it also.




Step 10:

You will see following screen.



Step 11:

Select 'xfs journaling file system' (I dont have any reason why at this time) and do the following configurations.





Step 12:
You will see checkbox selected against our drive. Now click on Install button.



Step 13:

You may see following page. Just click on 'Continue'








Step 14:
 
Keep clicking on Continue and Next untill everything is installed successfully.

Step 15:

Remove DVD from the drive and reboot your machine.

Happy ubuntuing. =)


Sunday, January 13, 2013

Android system calls hooking to trace system calls


There are definitely many tutorials on the internet which you can use for system calls hooking but now I try to keep track of all the things I am doing in my own words.

Following tutorial will help you to hook system calls for Android 2.6.29 and then you can trace system calls against any APK installed on your emulator.

I assume, you already know how to build and compile Android source code and you have done it, so I will go one step further from here now.

Basically, our_sys_table is pointing to a memory location where it stores all the system call numbers and pointers to each of those system calls. So if we get pointer to this  memory address, we can easily get pointers to all the system calls using predefined macros like __NR_<sysCall>. Then we can modify these pointers to point to our own system call routines and in this routine, we will call original system call routine so that original functionality may still work.

In this tutorial, I will only talk about read() system call and all of the other system calls will work the same way. And if you are reading this post, I assume, you are a programmer and you can easily understand the code given in trapcall.c file after reading the above given explanation. One hint: this code starts from init_module() method.

Step 1:

Create a directory 'LKM' in the home directory.

Step 2:

Copy the code given below and save in a file called 'trapcall.c'.

I have moved code from here to trapcall.c . This is the final version of the code.


Step 3:

Put this file in LKM directory such that its location is: 

/home/<userName>/LKM/trapcall.c

Step 4:

Go to /home/kernel-source/goldfish/ and open System.map file in a text editor. Find the entry 'sys_call_table' and copy the address. For example, in following case:

c0028aa4 T sys_call_table

address is c0028aa4

Step 5:

Open trapcall.c in a editor and go to the first line of function: init_module(). Change the address in the first line with the one copied from System.map in step 4. Do not forget to put 0x infront of the number copied.


Step 6:

Create a file named exactly 'Makefile' without 
quotes and extension like .txt and copy given below content in it. Save this file in the LKM directory.

Makefile

obj-m += trapcall.o

all:
 make -C /home/<userName>/kernel-source/goldfish/ M=$(PWD) modules

clean:
 make -C /home/<userName>/kernel-source/goldfish/ M=$(PWD) clean
 
Step 7:

Replace <userName> with your account name and save this file.

Step 8:

From terminal, navigate to the directory : /home/<userName>/LKM
Use the following command to compile the module:


$ make ARCH=arm CROSS_COMPILE=/home/<userName>/android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-

If you don't get any error, your module is ready to use.


Step 9:

Open a new terminal and run emulator via following command. I assume, you already have an avd named 'test'.



$ cd kernel-source/goldfish
$ emulator -avd test -kernel /home/<userName>/kernel-source/goldfish/arch/arm/boot/zImage -show-kernel -verbose

Step 10:

After the emulator is loaded, go to /home/<userName>/LKM from the first terminal and give the command:

$ adb push trapcall.ko /data/trapcall.ko
$ adb shell

Step 11:

 When you get the shell of emulator, type:

# cd data

# sudo insmod trapcall.ko


When you issue insmod command on the first terminal, you will start getting list of system calls on the second terminal where you started the emulator from.

Troubleshooting:

If you get some error related to memory write permissions because of modifying the function pointers, use accepted solution on the stackoverflow.

Filtering system calls based on UID:

Right now we have only one system call (read), but when we have all the system calls, we would be getting damn lot of system calls. So I would suggest to increase the buffer limit of the terminal to unlimited to get the complete list.

There is one more problem: after looking at the system calls list, we cannot find which system call belongs to which APK. So we can exploit the fact that each app gets a unique UID and PID on its installation time but if we run the emulator second time with installed app remains there or we reinstall it by running our project on Eclipse, we will find the same UID but different PID. Which means, if we get the uid, we can use a conditional statement for printk().

There is a file packages.xml in the emulator which you find through DDMS perspective in Eclipse or by using ls command on the terminal. Anyway, once you find the location of the file, download it and find uid of your APK in the file. In my case, uid =100028.

And your final trapcall.c will look like this.

Due credit to my frient Sarker Rumee who actually told me the basic steps.