Unity3d DLL Encryption

Source: Internet
Author: User
Tags unpack

Unity3d Packaging an Android application, if the DLL is not encrypted, it is easy to decompile, resulting in code leaks. The usual practice is to encrypt the DLL or confuse the code. This article is to discuss the encryption of the DLL to protect, and detailed record of the cryptographic operation process.


Main references

The post of rain pine: http://www.xuanyusong.com/archives/3553  

http://csftech.logdown.com/posts/452269-android-unity-encryption

These two articles have described the encryption process in detail, but some pits and some operations are not given.


Principle explanation

All the code is compiled after the apk\assets\bin\data\managed\ Assembly-csharp.dll, to do is to encrypt this DLL, Assembly-csharp.dll loaded by libmono.so, so you need to decrypt the encrypted assembly-csharp.dll in libmono.so, fortunately the unit Y provides the code for mono to be compiled and modified. Of course there is also the risk of anti-compilation for libmono.so, this article is not considered.


Get ready

  • Linux systems. This article chooses to adopt the Ubuntu14.04, the virtual machine also may, additionally may use the Windows + Cygwin to compile, but considers this to be possible to trample the pit more, decisively abandons.

  • Unity Mono Source Code, can download in Https://github.com/Unity-Technologies/mono, branch select unity4.6, directly under the zip package, or git down can, download down the ZIP package for Mono-unity-4.6.zip.

  • Unity3d 4.6 version, this test is 4.6 of the compilation, note must be installed 4.6.6+ version, otherwise the libmono.so will be re-compiled error ( pit one ).

  • Android NDK, version can be downloaded according to the version of Unity-mono, see unity-mono/external/buildscripts/build_runtime_android.sh, search ndk= can be found, This article uses the r10e, download down the NDK for Android-ndk-r10e-linux-x86_64.bin.

  • Apktools, for the APK to unpack the signature package, more than 2.0 version, or packaging will be an error.


Compiling mono

1) in order to easily compile with root, Ubuntu root is not turned on by default, you can use:

sudo passwd root

After losing two times the password

Su-

To sign in


2) NDK Installation

Installation 7z

apt-get Install P7zip-full

Extract

7z x Android-ndk-r10e-linux-x86.bin

Configure environment variables, there are many configuration methods, you can modify/etc/profile or ~/.BASHRC, here directly under the shell to add temporary environment variables, do not add the following mono will not find the NDK

Export android_ndk_root=/home/xubo/unity-dev/android-ndk-r10e

Export path= $ANDROID _ndk_root: $PATH


3) Check the NDK version used by mono

VI Open mono-unity-4.6/external/buildscripts/build_runtime_android.sh can be found

Perl ${buildscriptsdir}/prepareandroidsdk.pl -ndk=r10e -env=envsetup.sh && Source envsetup.sh

Here you can determine the current unity mono using r10e to compile the


4) Install some packages that must be compiled

Apt-get Install autoconf automake bison build-essential gettext git libglib2.0 libtool perl


5) Try to compile for the first time

./external/buildscripts/build_runtime_android.sh

Error:

/usr/bin/env:perl-w: No such file or directory

Here Unity-mono compile time will go to git a package android_krait_signal_handler, in external directory, is this packet error, this package error problem many, is a giant pit (pit two).


Open android_krait_signal_handler/build.pl, the first line

#!/usr/bin/env perl-w

Switch

#!/usr/bin/perl-w


The following line

PREPAREANDROIDSDK::GETANDROIDSDK (undef, undef, "R9");

Change to the actual NDK used

PREPAREANDROIDSDK::GETANDROIDSDK (undef, undef, "r10e");


Replace the buildscripts/prepareandroidsdk.pm with android_krait_signal_handler/prepareandroidsdk.pm


Open the Jni/application.mk and erase the next two lines.    

App_platform: = android-9ndk_toolchain_version: = clang3.3

Otherwise the following error will be reported

MAKE:EXECVP:/home/xubo/unity-dev/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8:permission denied


6) Try a second compilation

Configure not pass, open Config.log discovery

./configure:line 4546:/home/xubo/unity-dev/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/ Linux-x86/bin/arm-linux-androideabi-gcc:no such file or directory

Check the directory, the discovery file exists, this is because although the NDK is 64-bit, but the cross-compilation tool chain is 32-bit, installed, and the compiler used in this article is 64-bit, install a 64-bit run 32-bit executable package

apt-get Install lib32z1 lib32ncurses5 lib32bz2-1.0


7) Try the third compile, so we should be able to compile successfully, but has not been involved in the decryption, note that the compilation needs to be in the root directory of mono. The final display of the following is OK:

Build success! Android static/shared Libraries is found here:builds/embedruntimes/android


Encryption Program

The encryption process can refer to the above link, that is, Assembly-csharp.dll as a normal file, whatever language to write an encrypted code, simple can modify a few bytes, do offset What, create a new Assembly-csharp.dll, replace the original, so that the general crack software is out of the way.


Mono decryption

The above just experimented with Mono's compilation, and the addition of the decrypted code to mono has not been done yet.


Open mono-unity-5.3/mono/metadata/image.c and find the Mono_image_open_from_data_with_name function modified as follows

monoimage *mono_image_open_from_data_with_name  (char *data, guint32 data_len,  gboolean need_copy, monoimageopenstatus *status, gboolean refonly, const  Char *name) {        monocliimageinfo *iinfo;         monoimage *image;        char  *datac;                / /Add the following code         if (name != null)          {            if (Strstr ( Name, "Assembly-csharp.dll")) {                 //write down your decryption code here, the data is read from the Assembly-csharp.dll read file                  //encrypted raw data, generate a new data          through your decryption code    }        }                 if  (!data | |  !data_len)  {                 if  (status)                          *status = mono_image_image_invalid ;                return  Null;        }        datac  = data;        if  (need_copy)  {                 datac = g_try_malloc  (Data_len);                 if  (!DATAC)  {                          if  (status)                                  * status = mono_image_error_errno;                         return NULL;                 }                 memcpy  (datac, data,  Data_len);         } 


Mono formally compiled

Before formally compiling mono, there are two places to modify, not to modify the compiled debug version, libmono.so has 8 m,

Open build_runtime_android.sh, the following red-G to remove, compile release version

cflags= "-dandroid-dplatform_android-dlinux-d__linux__-dhave_usr_include_malloc_h-dpage_size=0x1000-d_posix_ Path_max=256-ds_iwrite=s_iwusr-dhave_pthread_mutex_timedlock-fpic-g-funwind-tables \

The same build_runtime_android_x86.sh also removed inside


Unity3d Signature

Don't forget, need unity4.6.6+ version, this article is under unity4.6.9 test OK.

Make a signature and use it to build the game using this signature when you re-apktool the package with a.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7C/A1/wKiom1bT_7CxRO8WAAD5dUcSwDo177.jpg "title=" Untitled. jpg "alt=" wkiom1bt_7cxro8waad5ducswdo177.jpg "/>


Apktool Unpacking Package

1) (under Windows Operations) determine Apktool directory with Aapt.exe,apktool.bat,apktool.jar, OK version is 2.0+

2) Copy the generated package, such as 1.apk, to apktool/

3) cmd command line, enter the Apktool directory, execute Apktool d 1.apk to unpack, will be generated under Apktool with the package name of the same folder 1/

4) Overwrite the encrypted assembly-csharp.dll 1\assets\bin\data\managed\assembly-csharp.dll

5) will compile the libmono.so, note here Select armv7a/, and x86/under, respectively covering 1\lib\armeabi-v7a and 1\lib\x86\ under libmono.so

6) The Package command line execution Apktool B-f 1, will be generated in 1/under the Dist file, inside is the new package, renamed 2.APK, and copied to the apktool/

7) signature, hidden is you want to fill in the signature file name, and alias

Jarsigner-verbose-keystore ****.keystore -signedjar 2_s.apk 2.apk * * * *

8) 2_s.apk is your encrypted package for installation testing


libmono.so encryption

Rain Pine also mentioned libmono.so encryption, here first not involved bar, strip Dynamic library, may be able to play the same effect.


Summary

This encryption after testing is OK, you can prevent the general anti-compilation software to crack, for the master may also be unable to resist, in addition to compile mono a little frightened,Android_krait_signal_handler This project is a pit, Still a little worried about the libmono.so have what hidden trouble, so it needs to be in a variety of Android on the machine to test more.

This article is from the "quiet down Slowly" blog, please be sure to keep this source http://snailwalk.blog.51cto.com/3127248/1745970

Unity3d DLL Encryption

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.