Android reverse engineering – decompile APK and change code

If you want to decompile a simple APK file and modify the code inside, then you may wish to try it, it is not difficult.
Why is it said to be a simple APK file? Because for more complex ones, such as apps from large companies, it is impossible for you to get what you want. They usually use complex subcontracting and obfuscation to make it impossible for you to obtain the readability of the code, not to mention that you can’t even read it. It’s time to change the code~

System: Windows

1. apktool

The function of apktool tool:

  1. We can use apktool to view the AndroidManifest, XML file and image resource file of the apk.
  2. You can modify the code in the apk, but it is not the source code, but the smail file, which will be discussed later.

First, we need to download the apktool tool. We need to go to the apktool official website. The link is: https://ibotpeaches.github.io/Apktool/install/
\
Translated it looks like this:
\
I created a new “apktool decompilation tool” folder on the E drive, placed two files (apktool.jar) & (apktool.bat) inside, and then configured the system environment variable Path:
\
Then bring up the cmd command line window and you can freely use the apktool command~
Related commands:

//apk analysis
apktool d -f E:\gongju\apktool decompilation tool\app.apk -o E:\gongju\apktool decompilation tool\test

The parsed directory structure is as follows:
” “>
Here we can view:
assets: resource files
lib: jni related so package
original: original AndroidManifest.xml file and signature text
res: xml and image resources
smail: The decompiled code, the directory structure in the smail folder is the same as the source code package
AndroidManifest: Just AndroidManifest.xml

//Package into apk
apktool b -f E:\gongju\apktool decompilation tool\test -o E:\gongju\apktool decompilation tool\packed.apk

In addition, you can modify the smail file and then use the apktool tool to repackage it. This avoids the need to remodify, compile, and package due to some small changes during the development process.
So the question is, what is an smail file?
The Dalvik virtual machine, like Jvm, also has its own set of instructions, which is similar to assembly language, but much simpler than assembly. As long as you know Java and know about Android, you can easily understand it. If you get these assembly files, Use apktool or dex2jar toolkit (there are many on the Internet) to decompile the classes.dex file, and you can get files with smali as the suffix. These smali files are Dalvik’s register language.
The above is an excerpt. Open it and see what it looks like. How about it? I don’t really understand it anyway.
.s mail file

How to change the code? Do I still need to learn the syntax of this assembly language? In fact, it is not necessary, as long as you can read a little.
First we need to locate the location of the code to be changed. The tools used here are:

  • dex2jar: A tool to convert dex files into jar files
  • jd-gui: decompilation tool for viewing jar files
2. dex2jar

Download address: https://sourceforge.net/projects/dex2jar/postdownload
Steps for usage:

  1. Change the suffix of app (application name).apk to zip or rar, that is, change it to app.zip or app.rar, right-click and “unzip” it. The directory structure is as follows:
    ” t=”Get the dex file”>
  2. Copy the classes.dex file to the dex2jar folder and execute the command:
    d2j-dex2jar classes.dex
    At this point, the dex file is converted into a jar file.
     image.png
3. jd-gui

Download address: http://java-decompiler.github.io/

Steps for usage:

  1. Open the tool and view the jar package information, and you can locate the place that needs to be modified. Suppose I want to change the URL address of a request, and I can locate it immediately:
  2. The location of the smail file can be confirmed according to the path, as shown in the figure. Then go to the corresponding smail folder (here is com\game\sdk\util\Constans.smail) and change it.
    This is just a simple modification. Similarly, if you have the source code of the package that this project depends on, you can change the source code of this package. Compile it into a jar file, then convert it into dex (through dx.bat), then convert it into smail (baksmali.jar), and finally replace the relevant smail in this project.
4.dx.bat

1. This is the tool that comes with Android (how you installed Android Studio). You can find it in the following directory:
\
2. Enter the current directory through the cmd command line:
cd C:\Users\Administrator\AppData\Local\Android\Sdk\build-tools\28.0.3
Enter this command to get the dex file:
dx.bat --dex --no-strict --no-warning --output=E:\work\out\dex\classes.dex (replace the location where you want to enter the dex file here ) E:\Project\jhsdk\build\intermediates\bundles\default\classes.jar (replace the location of your jar file here)

5. baksmali.jar

Download link:
https://bitbucket.org/JesusFreke/smali/downloads/
Order:
java -jar baksmali.jar (here is the path to the baksmail.jar file) -o E:\work\out\smali (the location of the output smail file) E:\work\out\ dex\classes.dex (location of dex file)

6. Supplement

The above are all modifications at the code level. If we want to change the resources, it is easier. We can modify them directly in the res file. However, we must be careful not to change them at will. Most of the resources are referenced. Deleting them will cause us to directly modify them when building the apk. An error is reported, or the apk installation fails.
In addition, what if we want to change the package name and version of the apk? The package name is simple and only needs to be modified in AndroidManifest. Where to modify the sdk version? here:
 apktool.yml

Reprint: https://blog.csdn.net/baidu_34928905/article/details/96735356