Unity3d 重新编译mono防止翻译

支付宝捐赠

打赏红包

  1. 下载mono :https://github.com/Unity-Technologies/mono/tree/unity-5.6
  2. 复制 mono-unity-5.6/external/buildscripts 1. build_runtime_android.sh 2. build_runtime_android_x86.sh 到 mono-unity-5.6文件夹

build_runtime_android.sh

// ubuntu 32 位

// 安装软件
apt-get install autoconf automake bison build-essential gettext git libglib2.0 libtool* perl

// 环境变量
export PATH=$PATH:/home/circle/data/ndkr10e

// 增加NDK_ROOT
export ANDROID_PLATFORM=android-9
export ANDROID_NDK_ROOT=/home/circle/data/ndkr10e
// -g 修改 -O2
-fpic -O2 -funwind-tables \

// 去掉不用的
#clean_build "$CCFLAGS_ARMv5_CPU" "$LDFLAGS_ARMv5" "$OUTDIR/armv5"
#clean_build "$CCFLAGS_ARMv6_VFP" "$LDFLAGS_ARMv5" "$OUTDIR/armv6_vfp"
clean_build "$CCFLAGS_ARMv7_VFP" "$LDFLAGS_ARMv7" "$OUTDIR/armv7a"

#!/bin/bash

# NB! Prereq : ANDROID_NDK_ROOT=/usr/local/android-ndk-xxx or similar
# Todo: set appropriate ARM flags for hard floats

export ANDROID_PLATFORM=android-9
export ANDROID_NDK_ROOT=/home/circle/data/ndkr10e
GCC_PREFIX=arm-linux-androideabi-
GCC_VERSION=4.8
OUTDIR=builds/embedruntimes/android
CWD="$(pwd)"
PREFIX="$CWD/builds/android"
BUILDSCRIPTSDIR=external/buildscripts

perl ${BUILDSCRIPTSDIR}/PrepareAndroidSDK.pl -ndk=r10e -env=envsetup.sh && source envsetup.sh

NDK_ROOT=`cd $ANDROID_NDK_ROOT && pwd`

if [ ! -f $NDK_ROOT/GNUmakefile ]; then
	echo "Failed to locate Android NDK; is ANDROID_NDK_ROOT correctly set?"
	exit 1
fi

HOST_ENV=`uname -s`
case "$HOST_ENV" in
    Darwin)
        HOST_ENV=darwin
        ;;
    Linux)
        HOST_ENV=linux
        ;;
    CYGWIN*|*_NT-*)
        HOST_ENV=windows
        ;;
	*)
		echo "Failed to locate supported host environment; HOST_ENV = $HOST_ENV ..."
		exit 1
		;;
esac

PLATFORM_ROOT=$NDK_ROOT/platforms/$ANDROID_PLATFORM/arch-arm
TOOLCHAIN=$NDK_ROOT/toolchains/$GCC_PREFIX$GCC_VERSION/prebuilt/$HOST_ENV

if [ ! -d $TOOLCHAIN ]; then
	TOOLCHAIN=${TOOLCHAIN}-x86
	if [ ! -d $TOOLCHAIN ]; then
		TOOLCHAIN=${TOOLCHAIN}_64
	fi
fi

if [ ! -a $TOOLCHAIN -o ! -a $PLATFORM_ROOT ]; then
	NDK_NAME=`basename $NDK_ROOT`
	echo "Failed to locate toolchain/platform; $NDK_NAME | $HOST_ENV | $GCC_PREFIX$GCC_VERSION | $ANDROID_PLATFORM"
	exit 1
fi

KRAIT_PATCH_PATH="${CWD}/android_krait_signal_handler/build"
PATH="$TOOLCHAIN/bin:$PATH"
CC="$TOOLCHAIN/bin/${GCC_PREFIX}gcc --sysroot=$PLATFORM_ROOT"
CXX="$TOOLCHAIN/bin/${GCC_PREFIX}g++ --sysroot=$PLATFORM_ROOT"
CPP="$TOOLCHAIN/bin/${GCC_PREFIX}cpp"
CXXCPP="$TOOLCHAIN/bin/${GCC_PREFIX}cpp"
CPATH="$PLATFORM_ROOT/usr/include"
LD=$TOOLCHAIN/bin/${GCC_PREFIX}ld
AS=$TOOLCHAIN/bin/${GCC_PREFIX}as
AR=$TOOLCHAIN/bin/${GCC_PREFIX}ar
RANLIB=$TOOLCHAIN/bin/${GCC_PREFIX}ranlib
STRIP=$TOOLCHAIN/bin/${GCC_PREFIX}strip
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 -O2 -funwind-tables \
-ffunction-sections -fdata-sections"
CXXFLAGS=$CFLAGS
CPPFLAGS=$CFLAGS
LDFLAGS="\
-Wl,--wrap,sigaction \
-L${KRAIT_PATCH_PATH}/obj/local/armeabi -lkrait-signal-handler \
-Wl,--no-undefined \
-Wl,--gc-sections \
-Wl,-rpath-link=$PLATFORM_ROOT/usr/lib \
-ldl -lm -llog -lc"

CONFIG_OPTS="\
--prefix=$PREFIX \
--cache-file=android_cross.cache \
--host=arm-eabi-linux \
--disable-mcs-build \
--disable-parallel-mark \
--disable-shared-handles \
--with-sigaltstack=no \
--with-tls=pthread \
--with-glib=embedded \
--enable-nls=no \
mono_cv_uscore=yes"

if [ ${UNITY_THISISABUILDMACHINE:+1} ]; then
        echo "Erasing builds folder to make sure we start with a clean slate"
        rm -rf builds
fi

function clean_build_krait_patch
{
       local KRAIT_PATCH_REPO="git://github.com/Unity-Technologies/krait-signal-handler.git"
       if [ ${UNITY_THISISABUILDMACHINE:+1} ]; then
               echo "Trusting TC to have cloned krait patch repository for us"
       elif [ -d "$KRAIT_PATCH_PATH" ]; then
               echo "Krait patch repository already cloned"
       else
               git clone --branch "master" "$KRAIT_PATCH_REPO" "$KRAIT_PATCH_PATH"
       fi
       (cd "$KRAIT_PATCH_PATH" && ./build.pl)
}

function clean_build
{
	make clean && make distclean
	rm android_cross.cache

	pushd eglib
	autoreconf -i
	popd
	autoreconf -i

	./configure $CONFIG_OPTS \
	PATH="$PATH" CC="$CC" CXX="$CXX" CPP="$CPP" CXXCPP="$CXXCPP" \
	CFLAGS="$CFLAGS $1" CPPFLAGS="$CPPFLAGS $1" CXXFLAGS="$CXXFLAGS $1" LDFLAGS="$LDFLAGS $2" \
	LD=$LD AR=$AR AS=$AS RANLIB=$RANLIB STRIP=$STRIP CPATH="$CPATH"

	if [ "$?" -ne "0" ]; then 
		echo "Configure FAILED!"
		exit 1
	fi

	make && echo "Build SUCCESS!" || exit 1

	mkdir -p $3
	cp mono/mini/.libs/libmono.a $3
	cp mono/mini/.libs/libmono.so $3
}

CCFLAGS_ARMv5_CPU="-DARM_FPU_NONE=1 -march=armv5te -mtune=xscale -msoft-float"
CCFLAGS_ARMv6_VFP="-DARM_FPU_VFP=1  -march=armv6 -mtune=xscale -msoft-float -mfloat-abi=softfp -mfpu=vfp -DHAVE_ARMV6=1"
CCFLAGS_ARMv7_VFP="-DARM_FPU_VFP=1  -march=armv7-a                            -mfloat-abi=softfp -mfpu=vfp -DHAVE_ARMV6=1"
LDFLAGS_ARMv5=""
LDFLAGS_ARMv7="-Wl,--fix-cortex-a8"

rm -rf $OUTDIR

clean_build_krait_patch

#clean_build "$CCFLAGS_ARMv5_CPU" "$LDFLAGS_ARMv5" "$OUTDIR/armv5"
#clean_build "$CCFLAGS_ARMv6_VFP" "$LDFLAGS_ARMv5" "$OUTDIR/armv6_vfp"
clean_build "$CCFLAGS_ARMv7_VFP" "$LDFLAGS_ARMv7" "$OUTDIR/armv7a"

# works only with ndk-r6b and later
source ${BUILDSCRIPTSDIR}/build_runtime_android_x86.sh dontclean

NUM_LIBS_BUILT=`ls -AlR $OUTDIR | grep libmono | wc -l`
if [ $NUM_LIBS_BUILT -eq 8 ]; then
	echo "Android STATIC/SHARED libraries are found here: $OUTDIR"
else
	echo "Build failed? Android STATIC/SHARED library cannot be found... Found $NUM_LIBS_BUILT libs under $OUTDIR"
	ls -Al $OUTDIR
	exit 1
fi
  1. build_runtime_android_x86.sh
// 增加NDK_ROOT
export ANDROID_PLATFORM=android-9
export ANDROID_NDK_ROOT=/home/circle/data/ndkr10e

// 去掉 -g
-fpic \

#!/bin/bash

# NB! Prereq : ANDROID_NDK_ROOT=/usr/local/android-ndk-xxx or similar
# Todo: set appropriate ARM flags for hard floats

export ANDROID_PLATFORM=android-9
export ANDROID_NDK_ROOT=/home/circle/data/ndkr10e
GCC_PREFIX=i686-linux-android-
GCC_VERSION=4.8
OUTDIR=builds/embedruntimes/android
PREFIX=`pwd`/builds/android

NDK_ROOT=`cd $ANDROID_NDK_ROOT && pwd`

if [ ! -f $NDK_ROOT/GNUmakefile ]; then
	echo "Failed to locate Android NDK; is ANDROID_NDK_ROOT correctly set?"
	exit 1
fi

HOST_ENV=`uname -s`
case "$HOST_ENV" in
    Darwin)
        HOST_ENV=darwin
        ;;
    Linux)
        HOST_ENV=linux
        ;;
    CYGWIN*|*_NT-*)
        HOST_ENV=windows
        ;;
	*)
		echo "Failed to locate supported host environment; HOST_ENV = $HOST_ENV ..."
		exit 1
		;;
esac

PLATFORM_ROOT=$NDK_ROOT/platforms/$ANDROID_PLATFORM/arch-x86
TOOLCHAIN=$NDK_ROOT/toolchains/x86-$GCC_VERSION/prebuilt/$HOST_ENV

if [ ! -d $TOOLCHAIN ]; then
	TOOLCHAIN=${TOOLCHAIN}-x86
	if [ ! -d $TOOLCHAIN ]; then
		TOOLCHAIN=${TOOLCHAIN}_64
	fi
fi

if [ ! -a $TOOLCHAIN -o ! -a $PLATFORM_ROOT ]; then
	NDK_NAME=`basename $NDK_ROOT`
	echo "Failed to locate toolchain/platform; $NDK_NAME | $HOST_ENV | $GCC_VERSION | $ANDROID_PLATFORM"
	echo "Toolchain = $TOOLCHAIN"
	echo "Platform = $PLATFORM_ROOT"
	exit 1
fi

PATH="$TOOLCHAIN/bin:$PATH"
CC="$TOOLCHAIN/bin/${GCC_PREFIX}gcc --sysroot=$PLATFORM_ROOT"
CXX="$TOOLCHAIN/bin/${GCC_PREFIX}g++ --sysroot=$PLATFORM_ROOT"
CPP="$TOOLCHAIN/bin/${GCC_PREFIX}cpp"
CXXCPP="$TOOLCHAIN/bin/${GCC_PREFIX}cpp"
CPATH="$PLATFORM_ROOT/usr/include"
LD=$TOOLCHAIN/bin/${GCC_PREFIX}ld
AS=$TOOLCHAIN/bin/${GCC_PREFIX}as
AR=$TOOLCHAIN/bin/${GCC_PREFIX}ar
RANLIB=$TOOLCHAIN/bin/${GCC_PREFIX}ranlib
STRIP=$TOOLCHAIN/bin/${GCC_PREFIX}strip
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 \
-ffunction-sections -fdata-sections"
CXXFLAGS=$CFLAGS
CPPFLAGS=$CFLAGS
LDFLAGS="\
-Wl,--no-undefined -Wl,--gc-sections \
-ldl -lm -llog -lc -lgcc"

CONFIG_OPTS="\
--prefix=$PREFIX \
--cache-file=android_cross.cache \
--host=i686-unknown-linux \
--disable-mcs-build \
--disable-parallel-mark \
--with-sigaltstack=no \
--with-tls=pthread \
--with-glib=embedded \
--enable-nls=no \
mono_cv_uscore=yes"

function clean_build
{
	make clean && make distclean
	rm android_cross.cache

	pushd eglib
	autoreconf -i
	popd
	autoreconf -i

	./configure $CONFIG_OPTS \
	PATH="$PATH" CC="$CC" CXX="$CXX" CPP="$CPP" CXXCPP="$CXXCPP" \
	CFLAGS="$CFLAGS $1" CXXFLAGS="$CXXFLAGS $1" CPPFLAGS="$CPPFLAGS $1" LDFLAGS="$LDFLAGS $2" \
	LD=$LD AR=$AR AS=$AS RANLIB=$RANLIB STRIP=$STRIP CPATH="$CPATH"

	if [ "$?" -ne "0" ]; then 
		echo "Configure FAILED!"
		exit 1
	fi

	make && echo "Build SUCCESS!" || exit 1

	mkdir -p $3
	cp mono/mini/.libs/libmono.a $3
	cp mono/mini/.libs/libmono.so $3
}

if [ x$1 != x"dontclean" ]; then
	rm -rf $OUTDIR
fi

clean_build "" "" "$OUTDIR/x86"

if [ x$1 != x"dontclean" ]; then
NUM_LIBS_BUILT=`ls -AlR $OUTDIR | grep libmono | wc -l`
if [ $NUM_LIBS_BUILT -eq 2 ]; then
	echo "Android STATIC/SHARED libraries are found here: $OUTDIR"
else
	echo "Build failed? Android STATIC/SHARED library cannot be found... Found $NUM_LIBS_BUILT libs under $OUTDIR"
	ls -AlR $OUTDIR
	exit 1
fi
fi
  1. android_krait_signal_handler/build/build.pl
// 修改
#!/usr/bin/perl -w
PrepareAndroidSDK::GetAndroidSDK(undef, undef, "r10e");

#!/usr/bin/perl -w

use PrepareAndroidSDK;
use File::Path;
use strict;
use warnings;

sub BuildAndroid
{
	PrepareAndroidSDK::GetAndroidSDK(undef, undef, "r10e");
	system('$ANDROID_NDK_ROOT/ndk-build clean');
	system('$ANDROID_NDK_ROOT/ndk-build');
}

sub ZipIt
{
	system("mkdir -p build/temp/include") && die("Failed to create temp directory.");

	# write build info
	my $git_info = qx(git symbolic-ref -q HEAD && git rev-parse HEAD);
	open(BUILD_INFO_FILE, '>', "obj/local/armeabi-v7a/build.txt") or die("Unable to write build information to build/temp/build.txt");
	print BUILD_INFO_FILE "$git_info";
	close(BUILD_INFO_FILE);

	system("cd obj/local/armeabi-v7a && zip ../../../builds.zip -r *.a build.txt") && die("Failed to package libraries into zip file.");
}

BuildAndroid();
ZipIt();
  1. PrepareAndroidSDK.pm
    复制 mono-unity-5.6/external/buildscripts/PrepareAndroidSDK.pm 到 android_krait_signal_handler/build
  2. jni/Application.mk
APP_PLATFORM := android-9
NDK_TOOLCHAIN_VERSION := 4.8
  • Windows vs2010 新建Encrypt文件夹

下载加密代码:https://github.com/xxtea/xxtea-c

新建 EncryptManage.c

gcc xxtea.c EncryptManage.c –o EncryptManage
 #include <stdio.h>  
  #include <string.h>  
  #include <stdlib.h>  
  #include "xxtea.h"  
  #define SIZE 1024*1024*10  
  void main()  
  {  
      FILE* infp = 0;  
      if((infp=fopen("Assembly-CSharp.dll","rb"))==NULL)  
      {  
          printf("Assembly-CSharp.dll Read Error\n");//打开操作不成功  
          return;//结束程序的执行  
      }  
    
      //char buffer[SIZE];  
      char* buffer = (char*)malloc(sizeof(char)*SIZE);  
      memset(buffer,0,sizeof(char)*SIZE);  
    
      int rc = 0;  
      int total_len = 0;  
    
      total_len = fread(buffer , sizeof(unsigned char) , SIZE , infp);  
      printf("Read Assembly-CSharp Successfully and total_len : %d \n" , total_len);  
    
      //加密DLL  
      size_t len;  
      char* key = "666666";  //此处位密钥。可自由更改
      char *encrypt_data = xxtea_encrypt(buffer,total_len, key, &len);  
    
      printf("Encrypt Dll Successfully and len : %d\n" , len);  
    
      //写Dll  
      FILE* outfp = 0;  
      if((outfp=fopen("Assembly-CSharp_encrypt.dll","wb+"))==NULL)  
      {  
          printf("Assembly-CSharp_encrypt.dll Read Error\n");//打开操作不成功  
          return;//结束程序的执行  
      }  
    
      int rstCount = fwrite(encrypt_data , sizeof(unsigned char) , len , outfp);  
        
      fflush(outfp);  
    
      printf("Write len : %d\n", rstCount);  
    
      fclose(infp);  
      fclose(outfp);  
    
      free(buffer);  
      free(encrypt_data);  
  } 

编译vs2010 工程

mono-unity-5.6\msvc\mono.sln

msbuild.exe mono.sln /p:Configuration=Release_eglib
  • 加密 so 文件
gcc -o encry ./shellAdder1.c
./encry libmono.so
  • shellAdder1.c
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
 
 
/* 32-bit ELF base types. */
typedef unsigned int Elf32_Addr;
typedef unsigned short Elf32_Half;
typedef unsigned int Elf32_Off;
typedef signed int Elf32_Sword;
typedef unsigned int Elf32_Word;
 
 
 
 
#define EI_NIDENT 16
 
/*
 * ELF header.
 */
 
typedef struct {
  unsigned char  e_ident[EI_NIDENT];  /* File identification. */
  Elf32_Half  e_type;    /* File type. */
  Elf32_Half  e_machine;  /* Machine architecture. */
  Elf32_Word  e_version;  /* ELF format version. */
  Elf32_Addr  e_entry;  /* Entry point. */
  Elf32_Off  e_phoff;  /* Program header file offset. */
  Elf32_Off  e_shoff;  /* Section header file offset. */
  Elf32_Word  e_flags;  /* Architecture-specific flags. */
  Elf32_Half  e_ehsize;  /* Size of ELF header in bytes. */
  Elf32_Half  e_phentsize;  /* Size of program header entry. */
  Elf32_Half  e_phnum;  /* Number of program header entries. */
  Elf32_Half  e_shentsize;  /* Size of section header entry. */
  Elf32_Half  e_shnum;  /* Number of section header entries. */
  Elf32_Half  e_shstrndx;  /* Section name strings section. */
} Elf32_Ehdr;
 
/*
 * Section header.
 */
 
typedef struct {
  Elf32_Word  sh_name;  /* Section name (index into the
             section header string table). */
  Elf32_Word  sh_type;  /* Section type. */
  Elf32_Word  sh_flags; /* Section flags. */
  Elf32_Addr  sh_addr;  /* Address in memory image. */
  Elf32_Off sh_offset;  /* Offset in file. */
  Elf32_Word  sh_size;  /* Size in bytes. */
  Elf32_Word  sh_link;  /* Index of a related section. */
  Elf32_Word  sh_info;  /* Depends on section type. */
  Elf32_Word  sh_addralign; /* Alignment in bytes. */
  Elf32_Word  sh_entsize; /* Size of each entry in section. */
} Elf32_Shdr;
 
 
int main(int argc, char** argv){
  char target_section[] = ".mytext";
  char *shstr = NULL;
  char *content = NULL;
  Elf32_Ehdr ehdr;
  Elf32_Shdr shdr;
  int i;
  unsigned int base, length;
  unsigned short nblock;
  unsigned short nsize;
  unsigned char block_size = 16;
  
  int fd;
  
  if(argc < 2){
    puts("Input .so file");
    return -1;
  }
  
  fd = open(argv[1], O_RDWR);
  if(fd < 0){
    printf("open %s failed\n", argv[1]);
    goto _error;
  }
  
  if(read(fd, &ehdr, sizeof(Elf32_Ehdr)) != sizeof(Elf32_Ehdr)){
    puts("Read ELF header error");
    goto _error;
  }
  
  lseek(fd, ehdr.e_shoff + sizeof(Elf32_Shdr) * ehdr.e_shstrndx, SEEK_SET);
  
  if(read(fd, &shdr, sizeof(Elf32_Shdr)) != sizeof(Elf32_Shdr)){
    puts("Read ELF section string table error");
    goto _error;
  }
  
  if((shstr = (char *) malloc(shdr.sh_size)) == NULL){
    puts("Malloc space for section string table failed");
    goto _error;
  }
  
  lseek(fd, shdr.sh_offset, SEEK_SET);
  if(read(fd, shstr, shdr.sh_size) != shdr.sh_size){
    puts("Read string table failed");
    goto _error;
  }
  
  lseek(fd, ehdr.e_shoff, SEEK_SET);
  for(i = 0; i < ehdr.e_shnum; i++){
    if(read(fd, &shdr, sizeof(Elf32_Shdr)) != sizeof(Elf32_Shdr)){
      puts("Find section .text procedure failed");
      goto _error;
    }
    if(strcmp(shstr + shdr.sh_name, target_section) == 0){
      base = shdr.sh_offset;
      length = shdr.sh_size;
      printf("Find section %s\n", target_section);
      break;
    }
  }
  
  lseek(fd, base, SEEK_SET);
  content = (char*) malloc(length);
  if(content == NULL){
    puts("Malloc space for content failed");
    goto _error;
  }
  if(read(fd, content, length) != length){
    puts("Read section .text failed");
    goto _error;
  }
  
  nblock = length / block_size;
  nsize = base / 4096 + (base % 4096 == 0 ? 0 : 1);
  printf("base = %d, length = %d\n", base, length);
  printf("nblock = %d, nsize = %d\n", nblock, nsize);
  
  ehdr.e_entry = (length << 16) + nsize;
  ehdr.e_shoff = base;
  
  
  
  for(i=0;i<length;i++){
    content[i] = ~content[i];
  }
  
  
  
  lseek(fd, 0, SEEK_SET);
  if(write(fd, &ehdr, sizeof(Elf32_Ehdr)) != sizeof(Elf32_Ehdr)){
    puts("Write ELFhead to .so failed");
    goto _error;
  }
  
  
  lseek(fd, base, SEEK_SET);
  if(write(fd, content, length) != length){
    puts("Write modified content to .so failed");
    goto _error;
  }
  
  
  puts("Completed");
_error:
  free(content);
  free(shstr);
  close(fd);
  return 0;
}
  • image.c 这个地方需要注意 编译Android工程 xxtea.h xxtea.c 需要放在image.c image.h 里面
//SO---------------加密----------------------
 
#include <sys/types.h>
#include <elf.h>
#include <sys/mman.h>
 
//注意上面说解密算法里面的断.mytext就是这里,
//这里把getKey进行了加密,这样对方拿不到你的密钥都没法破解你的dll了
int getKey() __attribute__((section (".mytext")));
int getKey(){
	return 666666;
};
//这里就是.so初始化的时候,这里进行mytext断的解密工作
void init_getKey() __attribute__((constructor));
unsigned long getLibAddr();
 
void init_getKey(){
  char name[15];
  unsigned int nblock;
  unsigned int nsize;
  unsigned long base;
  unsigned long text_addr;
  unsigned int i;
  Elf32_Ehdr *ehdr;
  Elf32_Shdr *shdr;
  
  base = getLibAddr();
  
  ehdr = (Elf32_Ehdr *)base;
  text_addr = ehdr->e_shoff + base;
  
  nblock = ehdr->e_entry >> 16;
  nsize = ehdr->e_entry & 0xffff;
  
  g_message("momo: nblock = %d\n", nblock);
  
 
  if(mprotect((void *) base, 4096 * nsize, PROT_READ | PROT_EXEC | PROT_WRITE) != 0){
    g_message("momo: mem privilege change failed");
 
  }
  //注意这里就是解密算法, 要和加密算法完全逆向才行不然就解不开了。
  for(i=0;i< nblock; i++){  
    char *addr = (char*)(text_addr + i);
    *addr = ~(*addr);
  }
  
  if(mprotect((void *) base, 4096 * nsize, PROT_READ | PROT_EXEC) != 0){
    g_message("momo: mem privilege change failed");
  }
  g_message("momo: Decrypt success");
}
 
unsigned long getLibAddr(){
  unsigned long ret = 0;
  char name[] = "libmono.so";
  char buf[4096], *temp;
  int pid;
  FILE *fp;
  pid = getpid();
  sprintf(buf, "/proc/%d/maps", pid);
  fp = fopen(buf, "r");
  if(fp == NULL)
  {
    g_message("momo: open failed");
    goto _error;
  }
  while(fgets(buf, sizeof(buf), fp)){
    if(strstr(buf, name)){
      temp = strtok(buf, "-");
      ret = strtoul(temp, NULL, 16);
      break;
    }
  }
_error:
  fclose(fp);
  return ret;
}
 
//SO---------------加密----------------------

// xxtea.c
// Todo 这里是xxtea.c 代码


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;

	if (!data || !data_len) {
		if (status)
			*status = MONO_IMAGE_IMAGE_INVALID;
		return NULL;
	}

	//if(strstr(name,"Assembly-CSharp.dll")){
        //        //这里就能取到密钥,那么这个函数被加密了。
        //        //IDA就看不到它了
	//	g_message("momo: key = %d\n", getKey());
	//}

	//这是添加的代码,开始   你也可以换成自己想要的解密方法
     if(name != NULL)
     {
      if (strstr(name, "Assembly-CSharp.dll")) {  
          //char* key = "666666";     //此处密钥需要对应加密时候所创建的密钥
	
	char buf[128] = {0};
	int nVal = getKey();
	sprintf(buf, "%d", nVal);

	 char* key = buf;
          size_t len;  
          char* decryptData = (char *)xxtea_decrypt(data, data_len,key, &len);  
          int i = 0;  
          for (i = 0; i < len; ++i)  
          {  
              data[i] = decryptData[i];  
          }  
          g_free(decryptData);  
    
          data_len = len;  
      } 
    }
     //这是添加的代码,结束


	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);
	}

	image = g_new0 (MonoImage, 1);
	image->raw_data = datac;
	image->raw_data_len = data_len;
	image->raw_data_allocated = need_copy;
	image->name = (name == NULL) ? g_strdup_printf ("data-%p", datac) : g_strdup(name);
	iinfo = g_new0 (MonoCLIImageInfo, 1);
	image->image_info = iinfo;
	image->ref_only = refonly;
	image->ref_count = 1;

	image = do_mono_image_load (image, status, TRUE, TRUE);
	if (image == NULL)
		return NULL;

	return register_image (image);
}

支付宝捐赠

打赏红包

你可能感兴趣的:(Unity3d)