Category Archives: Android

Reverse engineering android code

I never guessed reverse engineering will be so easy. Is it true for all java code or just android?
For android apps decompilation overall is so simple. Although don’t bank on understanding the code as the app providers make their code cryptic most probably due to this reason(the process is called obfuscation).

That being said you can decompile almost every app on android. It is just vagueness of code which is going to save you(for the app developers) and make life tough for the hackers.

So basically .apk is nothing but zipped version of all your resources and compiled classes. You can extract all this from .apk the use tool like
dex2jar
or even simpler use some online website which does so.

java decompilation is also so easy I guess till Java 1.5?

http://stackoverflow.com/questions/3593420/android-getting-source-code-from-an-apk-file Stack Overflow link.

Web Server on android

ideas

I never knew that I will need a complete web-server on android. I was also in the league of people thinking why would someone need a server on their phone. What purpose can it serve apart from just the joy that “Holla!! I have a full-fledged web server in my pocket!!”.
For my current product it seemed such a useful thing that now I can think of multiple ways where running server on android can be so helpful and it can be used for so many ways. Just think it as a cheap Web Server with its own small network when WiFi hotspot mode is on. You can create your own server client environment, specific to your needs. Like for sharing things may be in a conference room setup.

Getting the server going

Now coming to original point of the blog how to do it.
First the major part is getting the web-server. There are so many open source products I like the PAW server
http://paw-android.fun2code.de/ Other options are i-jetty
https://code.google.com/p/i-jetty/
nanohttpd

https://github.com/NanoHttpd/nanohttpd
JWS

http://tjws.sourceforge.net/
etc. After you get the webserver running you will need to put your own webpages in proper places as for PAW server I just deleted everything inside the folder /sdcard/paw/html and put my own pages as I like them. You just need to copy your web space assuming this is the starting directory. I used PAW because it needed least configuration. You can use whatever suits you. Just overwrite everything they have in that directory.
After getting the server running you can see your webpage getting displayed at http://192.168.43.1:port_no. Of-course you will need to turn on hotspot and connect your device(from where you want to browse) to the hotspot.
For PAW server it is http://192.168.43.1:8080 . Where 192.168.43.1 is default ip for android hotspot if not changed by manufacturer. If your devices are on same network you have to know the ip of the android device hosting web.

This surely is not good if you want the web-server serving many people. You want to remove the ip address and the port make it something like http://www.myserver.com huh? or http://myandroid.com with or without www replacing anything in place of com. Just for fun or say aha!! Now I can host anything without paying.
For removing IP from url you just need small changes but your device should be rooted.

Making it more real

Port forwarded is a great app to forward traffic to specified port. Since http requests come to port 80 you will need to create a rule in port forwarder to forward all incoming traffic on port 80 to port 8080 or the port number your server is running on. This will remove the port specification from url.
Finally we have to remove the ip address typing in URL. For this you just need to edit one file and add your own domain name and address. Mount your system in write mode:
mount -o rw,remount /system
location of system directory is device specific.
You can get the ip address of the device by connecting to hotspot and checking the default gateway. In general it is 192.168.43.1

Change /system/etc/hosts file and add below line

192.168.43.1 com myphone.com

com is my domain name and myphone is my hostname you can put whatever you like. This just indicate that given device address

Now change or create /etc/dnsmasq.conf file and add domain name like this:

local=/com/
domain=com

now you can even connect via adb like this:
adb connect android.com
cool huh!!.
Just restart hotspot and you are done :).

Android JNI

It is not like I hate java so much. I mean who wants to write all those verbose and repetitive stuff  all throw your code that’s all. That been said I always thought JNI as a mystery box and never tried it.

Now that I have tried JNI it seems so handy. Feels like I will transfer all the logic to JNI and just bother about small stuff and GUI for java code. Anyhow in this post I just want to iterate through how easy it is to use JNI. There are so many help page to do so. I just want to write so that I will remember it.
Step 1:

You just need to create a folder name jni in your eclipse(android) project. In this jni folder keep all your c or c++ files. ndk-tool is used to build these files inside jni folder. Just download ndk tool and add the “ndk-build” batch file path in your PATH environment variable so that you can use this command to compile jni folder content anywhere.

Step 2:
In Java program where you want to call the native c code function declare the function definition like this :
public static native return_type func(args);

public static native int add_two_nums(int first_num, int second_num);

To tell java where these functions are defined import the C/C++ library using below statement:
This is how you load the c library:

/** Load jni .so on initialization */
static {
System.loadLibrary("jni_module_library_name"); /* Note the name written here is without .so extension */
}
Step 3:

Now create the c file with the functions you want to call and other c helper functions: imported functions has special name signature which always starts with JNIEXPORT

JNIEXPORT j_appended_return_type JNICALL Java_package_name_class_name_func(JNIEnv *, jclass, args);
JNIEXPORT j_appended_return_type JNICALL Java_com_example_hello_jni_MainActivity_add_two_nums(JNIEnv *, jclass, jint first, jint second);

com.example.hello_jni is the name of the package dot(.) is replaced by underscore(_). Next name is the class name where you want to call the function from rest is actual function name.
jstring, jint etc are data types.
Above part is little bit complex but this is just needed for one or two function calls where you want the interaction from java to C/C++ rest is complete C/C++ as you like it. You can create server/client with C/C++ netdb socket includes use OpenMX AL to play audio video. include  #include for threads assert.h for assert debugging.
Always remember to include #include <jni.h> for macro definition and jni magic
#include <android/log.h>
#define TAG “MyAppTag”
#define LOGVERBOSE(…) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
Now you can just add LOGVERBOSE(“Any string message if you need to print integer use %u”, integer);
 
Step 4:
You will also need to write one make file so that ndk-build can actually go through all the c files and build your project. Java program will be build separately.
create android.mk file inside jni folder like this:
Code:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := jni_module_library_name

LOCAL_SRC_FILES := file_name.c

include $(BUILD_SHARED_LIBRARY)

Other useful flags in make file which you can use:

LOCAL_CFLAGS    := -Werror

LOCAL_LDLIBS    := -llog
 
Step 5:
Finally you will need to compile jni folder code by command ndk-build from root folder of the project. Don’t compile from inside the jni folder but one directory up. This is because ndk-build read information from AndroidManifest.xml and project.properties. This will create obj folder with architecture type .o and .so it will also create one libs folder.

 

What ndk tool does for you? It compiles the architecture dependent C/C++ code for you for the architecture you need it for

You can run any c/c++ program from android shell. You just need to cross compile it with ndk-build and then push the executable in android device using adb push command. By default sdcard is mounted in non-executable format so it is good to push in /data/local and change the permission of the executable to x (execute) then just run.


chmod 755 /data/local/executable_name.out

./name_of_executable.out

You can remount sdcard in execute mode using

mount -o remount,rw /mnt/sdcard

To create executable using ndk tool you will need to change the Android.mk file:

include $(BUILD_SHARED_LIBRARY) to include $(BUILD_EXECUTABLE).

You can also change the LOCAL_MODULE line to the name you want for your executable(executable_name)

Scratch pad:

# for native multimedia
LOCAL_LDLIBS += -lOpenMAXAL
# for logging
LOCAL_LDLIBS += -llog
# for native windows
LOCAL_LDLIBS += -landroid

LOCAL_CFLAGS += -UNDEBUG

Only required directories and files are: jni folder, AndroidManifest.xml, project.properties which is created automatically and has android target ndk information
libs and obj will be created
Good JNI tutorial: