Share how to do anti-hack for NDK-based Android apps!

Source: Internet
Author: User

          android program anti-cracking is a very important issue to consider when publishing your app, and it's common practice to add confusion to your 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, so 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,   http:// Www.ijiami.cn/appprotect_mobile_games

    • Package Release phase (only once) : The MD5 string for the. dex file is computed after the package build process, and is written to the main.cpp of the NDK project as a standard string for the final version. This process can be added to the Ant Automation Packaging release as a follow-up phase in generating. Dex.
    • dynamic Run phase (per boot) : Add dynamic. Dex MD5 calculation at the MAIN.CPP program start entry and compare it with the standard MD5 string stored in the code, and if the two do not match then the program has been cracked and exited immediately.

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:

[HTML] View plaincopy
  • <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:

[HTML] View plaincopy
    • <target name=" Dexmd5 "depends=" Predexmd5 ">   
    •          <loadfile srcfile= "${dexmd5tempfile}" property= "Dexmd5sign"/>&NBSP;&NBSP;
    • </target>   

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

[HTML] View plaincopy
    • <targetnametargetname=" Setmaincpp "depends=" Dexmd5 ">&NBSP;&NBSP;
    •         <replace file= "${maincppfile}" token= "Ant_dexmd5sign" value= "${dexmd5sign}"/>  
    • </TARGET>&NBSP;&NBSP;

        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 update main.cpp need to use NDK to recompile the project (mainly to recompile the changed C + + code, the step must be )

Call NDKBUIKD to complete the appropriate recompilation:

[HTML] View plaincopy
    • <targetnametargetname=" Ndkbuild "depends=" Setmaincpp ">&NBSP;&NBSP;
    •          <exec executable= "${basedir}/ndkbuild.bat" failonerror= "true" >&NBSP;&NBSP;
    •         </exec>  
    • </TARGET>&NBSP;&NBSP;

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

Phase 2: The corresponding MD5 is computed for Dex and is compared to the start-up in main.cpp.

        Here you will need to dynamically get the. dex file from the current APK package in the app every time it starts running and perform a MD5 calculation and verification. The direct implementation here is not very easy, so with the help of a third Party package Libzip (http://www.ijiami.cn/tlogin) It can be chained to the NDK project in. So and the specified Zip package (apk package) is decompressed to return all of 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;

Share how to do anti-hack for NDK-based Android apps!

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.