NDK-based Android anti-cracking

Source: Internet
Author: User

Android Program anti-cracking is a very important issue to be considered when releasing the app , it is common practice to add confusion to the code to increase the difficulty of cracking. But even so, the Java code after the obfuscation can still be cracked in a variety of ways. In the NDK-based Android contains the corresponding main.cpp to be used as the entrance to the application, so there are some anti-cracking, the corresponding crack difficulty will increase a lot (relative to Java code).

During the entire Android export process, the build. Dex phase is the basis for the entire package release operation, including the corresponding Java source code, The external library files are compiled to link to the. dex file, and any changes to the code after it is rebuilt. Dex, which are all different from the original file, can be used to MD5 the. dex file as the app is cracked. For Android App,dex source file security is the most important, therefore, this Dex source encryption protection is actually necessary, in this respect, can do there is love encryption this platform, different types of applications also have different encryption protection scheme, details can be learned here:/http Www.ijiami.cn/appprotect_mobile_games

Basic Flow:

1. Package release phase (only once): After the package build process gets. Dex, it calculates the MD5 string for the. dex file and writes it to the NDK project's main.cpp as the final version of the standard string. This process can be added to the Ant Automation Packaging release as a follow-up phase in generating. Dex.

2. Dynamic running phase (each start): Add dynamic. Dex MD5 Calculations at the MAIN.CPP program start entry, and compare it with the standard MD5 string stored in the code, if the two do not match then the program has been cracked, immediately exit.

Phase 1: Computes the MD5 string of the. dex file and writes it to the corresponding main.cpp, the corresponding ant operation is generally as follows (not complete).

Generate the MD5 for Dex and store it in a file:

<target name= "Predexmd5" depends= "dex" >

<exe cexecutable= "${dexmd5tool}" failonerror= "true" >

<arg value= "${dexmd5tempfile}"/>

<arg value= "${dex-ospath}"/>

</exec>

</target>

Read the corresponding MD5 string from the external file and store it in an ant variable:

<target name= "Dexmd5" depends= "PREDEXMD5" >

<loadfile srcfile= "${dexmd5tempfile}" property= "Dexmd5sign"/>

</target>

Write the MD5 string of the. dex file into main.cpp:

<targetnametargetname= "Setmaincpp" depends= "DEXMD5" >

<replace file= "${maincppfile}" token= "Ant_dexmd5sign" value= "${dexmd5sign}"/>

</target>

The use of Dexmd5tool is a self-implemented external EXE, the main implementation of arbitrary files to calculate its corresponding MD5 and save the string value to a specified file. It is necessary to save the MD5 string as a file in order to hit the file in Ant and read the string into the ant variable (and do not find any other method to write the corresponding MD5 code directly into the ant variable, thus doing this kind of tactfully implementation). Writing MD5 strings to main.cpp is mostly done by using Ant's String substitution mechanism.

After updating main.cpp, you need to recompile the project with the NDK (mainly by recompiling the C + + code that has changed here, which must be done)

Call NDKBUIKD to complete the appropriate recompilation:

<targetnametargetname= "Ndkbuild" depends= "Setmaincpp" >

<exec executable= "${basedir}/ndkbuild.bat" failonerror= "true" >

</exec>

</target>

The relevant content in Ndkbuild.bat is the same as the compiled parameters configured in eclipse:
X:/cygwin/bin/bash.exe--login-c "Cd/cygdrive/xxx/xxx/android/jni && $NDK/ndk-build"

Phase 2: Calculate the appropriate MD5 for Dex and compare it to start in main.cpp.

You need to dynamically get the. dex file from the current APK package in the app's startup run and perform a MD5 calculation and verification. The direct implementation here is not so easy, with the help of a third-party package, Libzip, which can be chained to the NDK project in. So, and unzip the specified ZIP package (apk package) to return all the files in binary form. As a result, the binary stream of the Dex of the current APK package can be run, and the MD5 code that computes binary is added to the project to be able to perform dynamic validation of the main.cpp at startup. Dex's MD5 value.

If the. dex file in the current APK package MD5 code matches the MD5 code stored in the main.cpp (Phase 1), the program will run legally;

NDK-based Android anti-cracking

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.