Android reverse engineering - decompile APK and change code

If you want todecompile 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 of all, 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/

The translation is like this:

I created a new folder of "apktool decompilation tool" on the E drive, and copied the two files (apktool.jar) & ;(apktool.bat) is placed inside, and then configure the system environment variable Path:

Then bring up the cmd command line window, and you can freely use the apktool command~
Related commands:

//apk解析
apktool d -f E:\gongju\apktool反编译工具\app.apk -o E:\gongju\apktool反编译工具\test

The parsed directory structure is like this:

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: AndroidManifest.xml

//打包成apk
apktool b -f E:\gongju\apktool反编译工具\test -o E:\gongju\apktool反编译工具\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 the 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 read it. You know, if you get these assembly files, you can use apktool or dex2jar toolkit (there are many on the Internet) to decompile the classes.dex file, and you can get files with the smali 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.
.smail 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 to use:

  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:
    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 to use:

  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. You can confirm the location of the smail file 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 do you install Android Studio), you can find it in the following directory:

2. Enter the current situation through the cmd command line Directory:
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(此处替换你想输入dex文件的位置) E:\Project\jhsdk\build\intermediates\bundles\default\classes.jar(此处替换你的jar文件所在的位置)

5. baksmali.jar

Download link:
https://bitbucket.org/JesusFreke/smali/downloads/
Command:
java -jar baksmali.jar(此处为baksmail.jar文件的路径) -o E:\work\out\smali(输出smail文件的位置) E:\work\out\dex\classes.dex(dex文件的位置)

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 Build directly reports an error when building the apk, or fails to install the apk.
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

Guess you like

Origin blog.csdn.net/gqg_guan/article/details/133949578