In Android app development, when we've developed the software, we don't want others to be able to decompile our application and not modify our code logic. In fact, in the security mechanism of the application, we want our application security high, through a variety of cryptographic operations, etc. to increase the competitor's anti-compilation cracking cost. It takes 10 days for a competitor to develop a similar application, and 100 days to hack our software program, which is bound to dispel the idea of hacking programmers cracking our application. How to increase the cost of the opponent's cracking, we need to test the security of our application how high, encryption technology is strong. An excellent application that not only benefits the user, but also protects its core technology.
This article is mainly from the following aspects of the Android application to briefly introduce the protection mechanism.
One, do not let the program installed on the simulator
In the previous article, software Security and reverse analysis in Android [a]-apk anti-compilation hack as well as the Java assembler code read and write the most of the operations described in the Android static anti-compilation hack apk file. Some applications have some confusion, starting from SDK2.3 we can see a Proguard folder under Android-sdk-windows\tools\, Proguard is a Java code obfuscation tool, specifically refer to this blog post, Make the code logic more complex, at this time if we want to static anti-compilation crack, we need to insert some Smali code dynamic debugging to observe the APK logic. So how do you protect your applications from anti-compilation cracking? We need to stop other programmers from recompiling and dynamically debug my app. In other words, it is necessary to prevent the hacker programmer from installing my application on the emulator.
One way to do this is to not let your application run on the emulator, and then commit suicide when the application finds itself installed on the emulator.
PackageCom.example.cracktest;Importandroid.app.Activity;ImportAndroid.os.Build;ImportAndroid.os.Bundle; Public class MainActivity1 extends Activity { protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);if(Isemulator ()) {//immediately commit suicide, find himself running on the simulator, hurriedly commit suicideAndroid.os.Process.killProcess (Android.os.Process.myPid ()); } setcontentview (R.layout.activity_main); }/** * Determine if the application is running on the emulator * * @return * * Public Boolean Isemulator() {//Model:android SDK built for x86 //As long as it is in the simulator, no matter what version of the emulator, in its model information will be the keyword parameter SDK if(Build.MODEL.contains ("SDK") || Build.MODEL.contains ("SDK")) {return true; }Else{return false; } }}
The above application can be deployed to a real machine, but it is not open on the emulator, which prevents the hacker programmer from dynamically debugging our application.
Ii. Detection of application integrity
The second security mechanism for protecting applications is to detect the integrity of the application, and we can use JNI technology to verify the integrity of the application, or we can use digital signatures to detect the integrity of the application. We know that when an apk file is recompiled and the code logic is modified, the APK will be signed with the Jarsigner tool to run the modified APK file. The signature of an application is to identify a developer's unique identity, and if an application is recompiled by someone else, the signature of the application will definitely change. When the signature of the program is found to change, we can prevent the program from causing the loss by committing suicide. Here's how to detect the integrity of your application by introducing a digital signature.
PackageCom.example.cracktest;Importandroid.app.Activity;ImportAndroid.content.pm.PackageInfo;ImportAndroid.content.pm.PackageManager;ImportAndroid.content.pm.PackageManager.NameNotFoundException;ImportAndroid.os.Bundle; Public class MainActivity2 extends Activity { protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); System.out.println (Getsignature ()); }/** * Get digital signature of application * * @return */ PublicStringgetsignature() {Packagemanager pm = Getpackagemanager ();Try{//Get the signature of the current applicationPackageInfo info = pm.getpackageinfo (Getpackagename (), packagemanager.get_signatures);returninfo.signatures[0].tocharsstring (); }Catch(Namenotfoundexception e) {E.printstacktrace ();return ""; } }}
The above simple instance code gets the digital signature of the application,
Any action on the original APK will modify the original APK digital signature, in order to make it easy to judge in the program, we take the digital signature of the MD5 hash value to judge.
PackageCom.example.cracktest;ImportJava.security.MessageDigest;ImportJava.security.NoSuchAlgorithmException; Public class MD5 { /** * MD5 Encryption of strings * @param content * @return */ Public StaticStringgetMD5(String content) {Try{MessageDigest Digest = messagedigest.getinstance ("MD5"); Digest.update (Content.getbytes ());returnGethashstring (Digest); }Catch(NoSuchAlgorithmException e) {E.printstacktrace (); }return NULL; }Private StaticStringgethashstring(MessageDigest Digest) {StringBuilder Builder =NewStringBuilder (); for(byteB:digest.digest ()) {Builder.append (integer.tohexstring (b >>4) &0xf)); Builder.append (integer.tohexstring (b &0xf)); }returnBuilder.tostring (); }}
When we run the application, we determine whether the current digital signature's MD5 hash is equal to the hash value in the above image, and if it is equal to, legitimately run, if not equal, will be able to prove that the apk file was decompile cracked, this time we 1.5s after the suicide program.
PackageCom.example.cracktest;Importandroid.app.Activity;ImportAndroid.content.pm.PackageInfo;ImportAndroid.content.pm.PackageManager;ImportAndroid.content.pm.PackageManager.NameNotFoundException;ImportAndroid.os.Bundle;ImportAndroid.widget.Toast; Public class MainActivity2 extends Activity { protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); System.out.println (Getsignature ());//The correct signature information is 77E980CAE813E5DACC11FDB4C67988FD if("77E980CAE813E5DACC11FDB4C67988FD". Equals (Getsignature ())) {Toast.maketext ( This,"The self-calibration is complete, the legitimate operation." ",0). Show (); }Else{Toast.maketext ( This,"You dare to hack my Code!" ",0). Show ();NewThread () { Public void Run() {Try{Thread.Sleep ( the); }Catch(Interruptedexception e) {E.printstacktrace (); } android.os.Process.killProcess (Android.os.Process.myPid ()); }; }.start (); } }/** * Get digital signature of application * * @return */ PublicStringgetsignature() {Packagemanager pm = Getpackagemanager ();Try{//Get the signature of the current applicationPackageInfo info = pm.getpackageinfo (Getpackagename (), packagemanager.get_signatures);returnMD5.GETMD5 (info.signatures[0].tocharsstring ()); }Catch(Namenotfoundexception e) {E.printstacktrace ();return ""; } }}
The specific anti-compilation process can be seen in the previous article software security and reverse analysis in Android [a]-apk anti-compilation hack as well as Java assembler Code read and write, when the anti-compilation apk, modify the Smali code, after the APK signed, deployed to the emulator to run, found 1.5s after the program suicide.
Test package name, version name and version number, and then make a judgment, and the detection of digital signature method is similar. Interested can study on their own.
Three, dynamic byte code technology
The application's running bytecode is not local, but on the network side, that is, the key core code of our application should not be placed locally, should be placed on the network server side. And how does the bytecode in the network end get it? We download the bytecode locally from the server via URLClassLoader and then compile and run the code. This dynamic technique of acquiring bytecode from the network can prevent hacker programmers from locally statically decompile our application.
Four, zip package encryption Five, flower instruction Shell Technology
Development programmers first make a so shell, as the application of the environment, the shell runs our program code, the equivalent of a Java virtual machine on the upper layer of a code monitor added. It can also be said that running our application in an encrypted virtual machine adds security.
Vi. references
- Verification of signature information and protection of sensitive information for Android applications using JNI technology
- Anti-compilation tips for Android
- Technical analysis on preventing Android APK anti-compilation
- How Android prevents the APK program from being recompiled Proguard
Not to be continued ...
Software security and reverse analysis in Android [two]-apk anti-cracking technology and security protection mechanism