Friday, 10 July 2015

HB Blog 82: How To Extract Source Code From Android Apk???

Reverse engineering, also called back engineering, is the processes of extracting knowledge or design information from anything man-made and re-producing it or reproducing anything based on the extracted information. The process often involves disassembling something (a mechanical device, electronic component, computer program, or biological, chemical, or organic matter) and analyzing its components and workings in detail.

In this post, I will show how to extract source code (java and xml ) files from android apk.
To get the source code from APK file, we will need these tools:
1. Download dex2jar
2. Download java decompiler
3. Download apktool

Procedure for decoding .apk files, step-by-step method:
Step 1:
  1. Make a new folder and copy over the .apk file that you want to decode.
  2. Now rename the extension of this .apk file to .zip (e.g. rename from filename.apk to filename.zip) and save it. Now you can access the classes.dex files, etc. At this stage you are able to see drawables but not xml and java files, so continue.
Step 2:
  1. Now extract this .zip file in the same folder (or NEW FOLDER).
  2. Download dex2jar and extract it to the same folder (or NEW FOLDER).
  3. Move the classes.dex file into the dex2jar folder.
  4. Now open command prompt and change directory to that folder (or NEW FOLDER). Then write d2j-dex2jar classes.dex (for mac terminal or ubuntu write ./d2j-dex2jar.sh classes.dex) and press enter. You now have the classes.dex.dex2jar file in the same folder.
  5. Download java decompiler, double click on jd-gui, click on open file, and open classes.dex.dex2jar file from that folder: now you get class files.
  6. Save all of these class files (In jd-gui, click File -> Save All Sources) by src name. At this stage you get the java source but the .xml files are still unreadable, so continue.
 Step 3:
  1. Now open another new folder.
  2. Put in the .apk file which you want to decode.
  3. Download the latest version of apktool AND apktool install window (both can be downloaded from the same link) and place them in the same folder
  4. Download framework-res.apk and put it in the same folder (Not all apk file need this file)
  5. Open a command window
  6. Navigate to the root directory of APKtool and type the following command: apktool if framework-res.apk
  7. apktool d myApp.apk (where myApp.apk denotes the filename that you want to decode)
  8. Now you get a file folder in that folder and can easily read the apk's xml files. 
  9. Just copy the contents of both folder(in this case both new folder)to the single one.
Finally, you can enjoy the source code...

No comments:

Post a Comment