Android security attack, anti-compilation and obfuscation technology fully resolved (on)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/guolin_blog/article/details/49738023
have been hesitant to write this article, after all, to decompile the program is not anything to be proud of things. But technically speaking, mastering the Anti-compilation function is really a very useful skill, it may not usually be used, but once it really needs to be used, and you do not, it is very headache. In addition, since others can decompile the program, we certainly have reason to have a certain degree of protection of the program, so code obfuscation is also a technology that we have to master. So in the last two articles, we're going to do a full resolution around the two topics of anti-compilation and obfuscation.

Anti-compilation

We all know that the Android program after the package is an APK file, this file can be installed directly on any Android phone, our anti-compilation is actually the anti-compilation of this apk file. Android's anti-compilation is divided into two parts, one is the code of the Anti-compilation, one is to the resources of the anti-compilation, we immediately to learn a bit.
Before beginning to learn, we need to prepare an APK file, in order to respect all developers, I do not take any of the software on the market to demonstrate, but rather write a demo for testing.
Here I want the code to be as simple as possible, so we set up a new project, add a button to the activity, and when the button is clicked it pops up a toast, which is as simple as the code:

 Public  class mainactivity extends appcompatactivity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Button button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {Toast.maketext (mainactivity). This,"You clicked Button", Toast.length_short). Show ();    }        }); }}

The resources in Activity_main.xml are as follows:

<?xml version= "1.0" encoding= "Utf-8"?><relativelayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" match_parent " android:layout_height  =" match_parent " android:paddingbottom  =" @dimen/activity_vertical_margin " android:paddingleft  = "@dimen/activity_horizontal_ Margin " android:paddingright  =" @dimen/ Activity_horizontal_margin " android:paddingtop  =  "@dimen/activity_vertical_margin" ;     <buttonandroid:id="@+id/button"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:text=" button "/ >                                </relativelayout>

Then we put the code into an APK package, named demo.apk, and then installed it on the phone, the results are as follows:


Well, getting ready for work here is basically done, so let's start by deserializing the demo program.

Anti-compilation Code

To decompile the code in the APK file, we need to use the following two tools:

    • Dex2jar This tool is used to convert Dex files into jar files
      : http://sourceforge.net/projects/dex2jar/files/
    • Jd-gui This tool to convert a jar file into Java code
      : http://jd.benow.ca/

Download the two tools and unzip them, and then we'll start to decompile the demo program. After unpacking the Dex2jar compression pack, you will find a number of files as shown in:


One of the things we want to use is D2j-dex2jar.bat this file, of course, if you are a Linux or Mac system, you need to use d2j-dex2jar.sh this file.
Then we will demo.apk file also decompression, if you do not know how to directly unzip the file can be renamed to Demo.zip, and then open with the decompression software. After decompression you will find a Classes.dex file in it, as shown in:

This Classes.dex file is where all the Java code is stored, we copy it to the Dex2jar extracted directory, and in cmd it goes to the same directory, and then executes:

d2j-dex2jar classes.dex

The execution results are as follows:


No errors were reported, which means that we have successfully converted. Now look at the Dex2jar directory and you will find one more file as shown in:

As you can see, the Classes-dex2jar.jar file is the jar file that we successfully converted after using the tool. But for us, the jar file is not readable, so you need to use the Jd-gui tool to convert the jar file into Java code.
The following is very simple, using the Jd-gui tool to open Classes-dex2jar.jar this file, as shown in the results:

OK, this shows that our code anti-compilation work has been successful, mainactivity in the code is very clear, basically has done more than 90% of the restoration work. But if you want to do 100% of the code is very difficult to restore, because like the Setcontentview () method passed in the parameters, is actually a resource ID value, then the anti-compilation here can only be the corresponding ID value to restore, Instead of becoming an intuitive code display like R.layout.activity_main.
Also, in addition to mainactivity, there are many other codes that have been recompiled because the current project has a package that references Support-v4 and support-v7. These referenced libraries are also packaged into the Classes.dex file as part of the code, so the code is restored together when it is recompiled.
Okay, we're done with the anti-compilation code, so let's look at how to decompile the resource.

Anti-compilation resources

In fact, careful friend may have observed, just demo.apk the decompression directory is not already have the resources files, there are androidmanifest.xml files, there is res directory. Enter the Res directory, as shown in the following:


This is not all the resource files are here? In fact, these resource files are compiled at the time of packaging, we directly open the words can not see the clear text, do not believe that we open the Androidmanifest.xml file to see, the content as shown:

As you can see, this code is completely unreadable. Of course, if you open the Activity_main.xml to see, the results will not be good where to go:

Thus, it is not possible to extract the APK package directly from its original resource file, so we also need to decompile the resource.
To decompile the resources in the APK file, another tool is used:

    • Apktool This tool is used to maximize the restoration of 9-patch images, layouts, strings, etc. in the apk file.
      : http://ibotpeaches.github.io/Apktool/install/

About the download of this tool I would like to add a few more words, we need is apktool.bat and Apktool.jar these two files. Currently the latest version of Apktool.jar is 2.0.3, here I will download the latest, and then rename Apktool_2.0.3.jar to Apktool.jar, and put them in the same folder can be, as shown in:


The next task is very simple, we copy the demo.apk to the same directory as the two files, then cmd enters the directory, and executes the following command in CMD:

apktool d Demo.apk

where d is decode means that we want to decode the demo.apk file. In addition to this basic usage, we can add some additional parameters to control decode's behavior:

    • - F If the destination folder already exists, force the deletion of the existing folder (default decoding fails if the destination folder already exists).
    • - o Specifies the name of the decoded destination folder (the target folder is named by default using the APK file name).
    • - s does not decompile the Dex file, which means that the Classes.dex file is retained (by default, the Dex file is decoded into the Smali file).
    • - R does not decompile the resource file, which means that the RESOURCES.ARSC file is retained (by default, the RESOURCES.ARSC is decoded into a specific resource file).

The usual usage is that much, so the result of the above command is as follows:


This means that the anti-compilation resource has succeeded.
Of course, even if you are doing exactly the same as me, there is a possibility that the anti-compilation will fail here, such as the following error:

The reason for this error is most likely that you have previously used Apktool's old version of the anti-compilation operation, and then Apktool will be in your system'sC:\Users\Administrator\apktool\frameworkThis directory generates a name for the1.apkCache file, delete the cache file, and then re-execute the anti-compilation command should be successful.
Now you will find that in the current directory there is a more Demo folder, this folder is the result of the anti-compilation. We can open the androidmanifest.xml to take a look, as shown in:

What do you think? Then you can see the Activity_main.xml file in Res/layout, as shown in:

As you can see, the content in Activity_main.xml is basically consistent with the content in the source code, the outer layer is a relativelayout, and inside is a button. You can go to other directories to look at other resources, basically can be restored normally, so that we have the method of anti-compilation resources have been mastered.

Re-packaging

So for the anti-compiled folder, can we re-package it into an apk file? The answer is yes, but I can't think of any justification for that. Some people will say the Chinese, yes, the Chinese way is indeed to decompile an apk, and then translate the resources to re-packaging, but anyway this is still the other people's program to crack, so I do not think this is what glorious thing. So we're not going to talk about the right or wrong side of the story, just stand on a technical point of view to learn about repackaging.
First, let's look at the Apktool-post-decompile package directory situation, as shown in:


Where the original folder is stored under the non-decompile, The original Androidmanifest.xml file, the Res folder is stored in the anti-compilation of all resources, Smali folder is stored in all the anti-compiled code, Androidmanifest.xml is an anti-compilation after the restored manifest file. Here is worth mentioning is the Smali folder, if you go into this folder you will find its directory structure and our source SRC directory structure is almost the same, the main difference is that all the Java files have become Smali files. The Smali file is really the source code, but its syntax and Java are completely different, it is a bit like the assembly of the syntax, is the Android virtual machine used by the register language, the syntax structure is probably as follows:

Looks a little disoriented, doesn't it? But if you can read the Smali file, you can do horrible things-you can modify the logic inside the application and hack it!
But I don't have much interest in this kind of black technology, so I haven't done any specific research, but even so, it's possible to make some changes to the logic of the program. For example, when we click the button, it pops up.you clicked buttonSuch a toast, the logic is written in the Mainactivity button click the Anonymous class of the event, so this code after the anti-compilation must be in the Mainactivity$1.smali this file, let us Open to see, some of the code is as follows:

Although most of the code I do not understand, but the 47th line is too obvious, the toast display is not defined here, then if we want to hack the demo program, we can change this string, for example, I changed it toYour app is been hacked。
About Smali Grammar, the online information is also very much, if you are very interested in this technology can be directly online to search, here I just briefly introduced, no longer in-depth knowledge of the relevant.
After changing a code, we will change a resource, for example, here want to replace the application icon, then first we have to prepare a new picture, as shown:

Then from the Androidmanifest.xml file can be seen, the app icon is using ic_launcher.png This picture, then we will the above basketball this picture named Ic_launcher.png, and then copied to all to res/ Complete the Replace operation in the folder beginning with Mipmap.
After making two changes, we now have to re-package the anti-compiled demo folder into the APK bar, in fact, it is very simple, only need to execute the following command in CMD:

apktool b Demo -o New_Demo.apk

where B is the meaning of build, means that we want to package the Demo folder into an apk file,-O is used to specify the newly generated APK file name, where the new file is called new_demo.apk. The execution results are as follows:


Now you will find a new APK file generated under the sibling directory:

But don't be happy too early, the new_demo.apk still can't be installed, because it hasn't been signed yet. So if this is someone else's program, where can we get the original signature file? Obviously, there's no way to get it, so we can only re-sign the apk file with our own signature file, but it also shows that the software we repackaged is a full pirated software. Here everybody learns the technique to be good, hoped that does not have any person to do any bad thing.
So here I'm going to use a previously generated signature file, and using either Android Studio or Eclipse, you can easily generate a signature file.
With the signature file, execute the signature command in CMD to sign the command in the following format:

-verbose-sigalg-digestalg-keystore-storepass 签名密码 待签名的APK文件名 签名的别名

Where the jarsigner command file is stored in the bin directory of the JDK, the bin directory needs to be configured in the system's environment variables to be able to execute the command at any location.
The apk file after signing is now ready to be installed on the phone, but before that, Android is also highly recommended for one-time alignment of the signed APK file, as this will allow our program to run faster on Android. The alignment operation uses the Zipalign tool, which is stored in the <Android SDK>/build-tools/<version> directory and is configured in the system environment variable to execute the command in any location. The command format is as follows:

4 New_Demo.apk New_Demo_aligned.apk

4 of these are fixed values that cannot be changed, followed by an APK file name to be aligned and an aligned APK file name. After you run this command, a new_demo_aligned.apk file is generated, as follows:


The new_demo_aligned.apk is the one that we repackaged the signature-aligned file, and now installs it on the phone, as shown in the following:

As you can see, the app icon has been successfully changed to basketball, and the hint of toast that pops up after the click of the button has changed into our revised text, stating that the repackaging operation has indeed succeeded.

OK, we have the anti-compilation code, anti-compilation resources, repackaging the contents of the three main topics have been mastered, about the anti-compilation related to here, the next article will introduce the Android code obfuscation related technology, please look forward to.

Follow my public number, the first time to get a blog update reminders, there are many other technical information to share to everyone
Sweep the QR code below or search number Guolin_blog to follow:

Android security attack, anti-compilation and obfuscation technology fully resolved (on)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.