Tuesday, June 23, 2015

apktool

Apktool:

apktool is one of the tool to decompile the android application .apk file and get the smali/baksmali code. It was originally developed by Ryszard Wisniewski and Connor Tumbleson is the current maintainer. This tool is also used for reverse engineering the apk files be it for profit or for fun or research or any other purpose.
I used this tool for one of my semester project where I did static analysis and code injection on one of the application from the google market. With this experience I am presenting here the very basic steps to decompile the apk files.

Basic steps to install and perform decompilation of apk files

1. Install apktool:    

for detailed steps visit the link below under section "Installation for Apktool 2.x":
http://ibotpeaches.github.io/Apktool/install/
As told in detailed steps, put the apktool.jar into system or root directory. For windows, put the file in C://Windows


2. Place the apk file you want to decompile i.e. target apk file in desktop. (If you want to place the apk file in other places its fine but, problem might occur due to permission issue )

3. In case you need to deal with some android system then you need framework of that android system. For e.g: you need file called "lge-res.apk" if you are using LG android phone. To get the framework:

C:\> adb pull /system/framework/lge-res.apk


4.  Install framework:


 LGE_FRAMEWORK (above in install framework) is the tag I gave to the framework file so that I can use this tag name later on that will work as an alias for lge-res.apk file.

5. To decompile the target apk file


C:\> apktool d C:\Users\AnjilaTam\Desktop\com.mytarget.apk -t LGE_FRAMEWORK
Replace the com.mytarget with the name of the your target apk file.

and you will get the decompiled folder in the desktop.

source files of the target apk file i.e. samli codes are under smali folder.



Now you have the source code you can perform whatever your goal is.
Then after making the changes ,if you want to rebuild these changes into the target apk file follow from step 6

6. To build the amali code into apk file
C:\>apktool build com.mytarget

after this you will get .apk file under dist directory in the target apk decompiled folder. This is new .apk file with changes you wanted on the target .apk file.

7. Now you can reinstall the app on your phone:


Aha you got the error because we have not assigned the certificate to the app. If you have certificate you can use that. If you do not have certificate, google allows us to use self-signed certificate for the purpose of testing. So I will be using self-signed certificate below:
C:\>Users\AnjilaTam>keytool -genkey -v -keystore pixin_release-key.keystore -alias wallpaper –keyalg RSA -validity 10000 -keysize 2048



8. To check if the app has been signed with the certificate: 
C:\com.mytarget\dist>jarsigner -verify -verbose -certs com.mytarget.apk


  The above command shows that the app has not been signed. 

9. We have just created the signature on step 7 with which we will sign the app below:

jarsigner -verbose -keystore pixin_release-key.keystore com.mytarget.apk wallpaper

10. Align the final APK package using zipalign:
Copy zipalign.exe from C:\android-sdk-windows\build-tools\22.0.1 to C:\android-sdk-windows\tools





Then run zipalign:
C:\com.mytarget\dist>zipalign -v 4 com.mytarget.apk changedTarget.apk

under dist folder you will get the new .apk file with all the changes with name changedTarget.apk. You can change name "changedTarget.apk" above and you will get your desired name.

10. now install the app in your phone:
adb install C:\com.mytarget\dist\changedTarget.apk
To uninstall the app from the phone:
adb uninstall com.mytarget  

At last, you learned how to pull a target apk, decompile it, modify it and rebuild it reflecting the modifications.

Thank you.