DEX加壳加密解决方案的设计与实现.doc_第1页
DEX加壳加密解决方案的设计与实现.doc_第2页
DEX加壳加密解决方案的设计与实现.doc_第3页
DEX加壳加密解决方案的设计与实现.doc_第4页
DEX加壳加密解决方案的设计与实现.doc_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、什么是加壳? 加壳是在二进制的程序中植入一段代码,在运行的时候优先取得程序的控制权,做一些额外的工作。大多数病毒就是基于此原理。PC EXE文件加壳的过程如下: 二、加壳作用 加壳的程序可以有效阻止对程序的反汇编分析,以达到它不可告人的目的。这种技术也常用来保护软件版权,防止被软件破解。三、Android Dex文件加壳原理 PC平台现在已存在大量的标准的加壳和解壳工具,但是Android作为新兴平台还未出现APK加壳工具。Android Dex文件大量使用引用给加壳带来了一定的难度,但是从理论上讲,Android APK加壳也是可行的。 在这个过程中,牵扯到三个角色: 1、加壳程序:加密源程序为解壳数据、组装解壳程序和解壳数据 2、解壳程序:解密解壳数据,并运行时通过DexClassLoader动态加载 3、源程序:需要加壳处理的被保护代码 阅读该文章,需要您对DEX文件结构有所了解,您可以通过以下网址了解相关信息: /jiazhijun/article/details/8664778 根据解壳数据在解壳程序DEX文件中的不同分布,本文将提出两种Android Dex加壳的实现方案。 (一)解壳数据位于解壳程序文件尾部 该种方式简单实用,合并后的DEX文件结构如下。 加壳程序工作流程: 1、加密源程序APK文件为解壳数据 2、把解壳数据写入解壳程序Dex文件末尾,并在文件尾部添加解壳数据的大小。 3、修改解壳程序DEX头中checksum、signature 和file_size头信息。 4、修改源程序AndroidMainfest.xml文件并覆盖解壳程序AndroidMainfest.xml文件。 解壳DEX程序工作流程: 1、读取DEX文件末尾数据获取借壳数据长度。 2、从DEX文件读取解壳数据,解密解壳数据。以文件形式保存解密数据到a.APK文件 3、通过DexClassLoader动态加载a.apk。(二)解壳数据位于解壳程序文件头 该种方式相对比较复杂, 合并后DEX文件结构如下: 加壳程序工作流程: 1、加密源程序APK文件为解壳数据 2、计算解壳数据长度,并添加该长度到解壳DEX文件头末尾,并继续解壳数据到文件头末尾。 (插入数据的位置为0x70处) 3、修改解壳程序DEX头中checksum、signature、file_size、header_size、string_ids_off、type_ids_off、proto_ids_off、field_ids_off、 method_ids_off、class_defs_off和data_off相关项。 分析map_off 数据,修改相关的数据偏移量。 4、修改源程序AndroidMainfest.xml文件并覆盖解壳程序AndroidMainfest.xml文件。 解壳DEX程序工作流程: 1、从0x70处读取解壳数据长度。 2、从DEX文件读取解壳数据,解密解壳数据。以文件形式保存解密数据到a.APK 3、通过DexClassLoader动态加载a.APK。一、序言 在上篇“Android APK加壳技术方案”(/jiazhijun/article/details/8678399)博文中,根据加壳数据在解壳程序Dex文件所处的位置,我提出了两种Android Dex加壳技术实现方案,本片博文将对方案1代码实现进行讲解。博友可以根据方案1的代码实现原理对方案2自行实现。 在方案1的代码实现过程中,各种不同的问题接踵出现,最初的方案也在不同问题的出现、解决过程中不断的得到调整、优化。 本文的代码实现了对整个APK包的加壳处理。加壳程序不会对源程序有任何的影响。二、代码实现 本程序基于Android2.3代码实现,因为牵扯到系统代码的反射修改,本程序不保证在其它android版本正常工作,博友可以根据实现原理,自行实现对其它Android版本的兼容性开发。 1、加壳程序流程及代码实现 1、加密源程序APK为解壳数据 2、把解壳数据写入解壳程序DEX文件末尾,并在文件尾部添加解壳数据的大小。 3、修改解壳程序DEX头中checksum、signature 和file_size头信息。 代码实现如下:javaview plaincopy1. packagecom.android.dexshell;2. importjava.io.ByteArrayOutputStream;3. importjava.io.File;4. importjava.io.FileInputStream;5. importjava.io.FileOutputStream;6. importjava.io.IOException;7. importjava.security.MessageDigest;8. importjava.security.NoSuchAlgorithmException;9. importjava.util.zip.Adler32;10. 11. publicclassDexShellTool12. /*13. *paramargs14. */15. publicstaticvoidmain(Stringargs)16. /TODOAuto-generatedmethodstub17. try18. FilepayloadSrcFile=newFile(g:/payload.apk);19. FileunShellDexFile=newFile(g:/unshell.dex);20. bytepayloadArray=encrpt(readFileBytes(payloadSrcFile);21. byteunShellDexArray=readFileBytes(unShellDexFile);22. intpayloadLen=payloadArray.length;23. intunShellDexLen=unShellDexArray.length;24. inttotalLen=payloadLen+unShellDexLen+4;25. bytenewdex=newbytetotalLen;26. /添加解壳代码27. System.arraycopy(unShellDexArray,0,newdex,0,unShellDexLen);28. /添加加密后的解壳数据29. System.arraycopy(payloadArray,0,newdex,unShellDexLen,30. payloadLen);31. /添加解壳数据长度32. System.arraycopy(intToByte(payloadLen),0,newdex,totalLen-4,4);33. /修改DEXfilesize文件头34. fixFileSizeHeader(newdex);35. /修改DEXSHA1文件头36. fixSHA1Header(newdex);37. /修改DEXCheckSum文件头38. fixCheckSumHeader(newdex);39. 40. 41. Stringstr=g:/classes.dex;42. Filefile=newFile(str);43. if(!file.exists()44. file.createNewFile();45. 46. 47. FileOutputStreamlocalFileOutputStream=newFileOutputStream(str);48. localFileOutputStream.write(newdex);49. localFileOutputStream.flush();50. localFileOutputStream.close();51. 52. 53. catch(Exceptione)54. /TODOAuto-generatedcatchblock55. e.printStackTrace();56. 57. 58. 59. /直接返回数据,读者可以添加自己加密方法60. privatestaticbyteencrpt(bytesrcdata)61. returnsrcdata;62. 63. 64. 65. privatestaticvoidfixCheckSumHeader(bytedexBytes)66. Adler32adler=newAdler32();67. adler.update(dexBytes,12,dexBytes.length-12);68. longvalue=adler.getValue();69. intva=(int)value;70. bytenewcs=intToByte(va);71. byterecs=newbyte4;72. for(inti=0;i=0;i-)85. bi=(byte)(number%256);86. number=8;87. 88. returnb;89. 90. 91. 92. privatestaticvoidfixSHA1Header(bytedexBytes)93. throwsNoSuchAlgorithmException94. MessageDigestmd=MessageDigest.getInstance(SHA-1);95. md.update(dexBytes,32,dexBytes.length-32);96. bytenewdt=md.digest();97. System.arraycopy(newdt,0,dexBytes,12,20);98. Stringhexstr=;99. for(inti=0;inewdt.length;i+)100. hexstr+=Integer.toString(newdti&0xff)+0x100,16)101. .substring(1);102. 103. System.out.println(hexstr);104. 105. 106. 107. privatestaticvoidfixFileSizeHeader(bytedexBytes)108. 109. 110. bytenewfs=intToByte(dexBytes.length);111. System.out.println(Integer.toHexString(dexBytes.length);112. byterefs=newbyte4;113. for(inti=0;i4;i+)114. refsi=newfsnewfs.length-1-i;115. System.out.println(Integer.toHexString(newfsi);116. 117. System.arraycopy(refs,0,dexBytes,32,4);118. 119. 120. 121. privatestaticbytereadFileBytes(Filefile)throwsIOException122. bytearrayOfByte=newbyte1024;123. ByteArrayOutputStreamlocalByteArrayOutputStream=newByteArrayOutputStream();124. FileInputStreamfis=newFileInputStream(file);125. while(true)126. inti=fis.read(arrayOfByte);127. if(i!=-1)128. localByteArrayOutputStream.write(arrayOfByte,0,i);129. else130. returnlocalByteArrayOutputStream.toByteArray();131. 132. 133. 134. 135. 136. 2、解壳程序流程及代码实现 在解壳程序的开发过程中需要解决如下几个关键的技术问题: (1)解壳代码如何能够第一时间执行? Android程序由不同的组件构成,系统在有需要的时候启动程序组件。因此解壳程序必须在Android系统启动组件之前运行,完成对解壳数 据的解壳及APK文件的动态加载,否则会使程序出现加载类失败的异常。 Android开发者都知道Applicaiton做为整个应用的上下文,会被系统第一时间调用,这也是应用开发者程序代码的第一执行点。因此通过对 AndroidMainfest.xml的application的配置可以实现解壳代码第一时间运行。htmlview plaincopy1. application2. android:icon=drawable/ic_launcher3. android:label=string/app_name4. android:theme=style/AppThemeandroid:name=com.android.dexunshell.ProxyApplication5. (2)如何替换回源程序原有的Application? 当在AndroidMainfest.xml文件配置为解壳代码的Application时。源程序原有的Applicaiton将被替换,为了不影响源程序代码逻辑,我们需要 在解壳代码运行完成后,替换回源程序原有的Application对象。我们通过在AndroidMainfest.xml文件中配置原有Applicaiton类信息来达到我们 的目的。解壳程序要在运行完毕后通过创建配置的Application对象,并通过反射修改回原Application。htmlview plaincopy1. application2. android:icon=drawable/ic_launcher3. android:label=string/app_name4. android:theme=style/AppThemeandroid:name=com.android.dexunshell.ProxyApplication5. 6. (3)如何通过DexClassLoader实现对apk代码的动态加载。 我们知道DexClassLoader加载的类是没有组件生命周期的,也就是说即使DexClassLoader通过对APK的动态加载完成了对组件类的加载, 当系统启动该组件时,还会出现加载类失败的异常。为什么组件类被动态加载入虚拟机,但系统却出现加载类失败呢? 通过查看Android源代码我们知道组件类的加载是由另一个ClassLoader来完成的,DexClassLoader和系统组件ClassLoader并不存在关 系,系统组件ClassLoader当然找不到由DexClassLoader加载的类,如果把系统组件ClassLoader的parent修改成DexClassLoader,我们就可 以实现对apk代码的动态加载。 (4)如何使解壳后的APK资源文件被代码动态引用。 代码默认引用的资源文件在最外层的解壳程序中,因此我们要增加系统的资源加载路径来实现对借壳后APK文件资源的加载。 解壳实现代码:javaview plaincopy1. packagecom.android.dexunshell;2. 3. importjava.io.BufferedInputStream;4. importjava.io.ByteArrayInputStream;5. importjava.io.ByteArrayOutputStream;6. importjava.io.DataInputStream;7. importjava.io.File;8. importjava.io.FileInputStream;9. importjava.io.FileOutputStream;10. importjava.io.IOException;11. importjava.lang.ref.WeakReference;12. importjava.util.ArrayList;13. importjava.util.HashMap;14. importjava.util.Iterator;15. importjava.util.zip.ZipEntry;16. importjava.util.zip.ZipInputStream;17. 18. importdalvik.system.DexClassLoader;19. importandroid.app.Application;20. importandroid.content.pm.ApplicationInfo;21. importandroid.content.pm.PackageManager;22. importandroid.content.pm.PackageManager.NameNotFoundException;23. importandroid.os.Bundle;24. publicclassProxyApplicationextendsApplication25. 26. 27. privatestaticfinalStringappkey=APPLICATION_CLASS_NAME;28. privateStringapkFileName;29. privateStringodexPath;30. privateStringlibPath;31. 32. 33. protectedvoidattachBaseContext(Contextbase)34. super.attachBaseContext(base);35. try36. Fileodex=this.getDir(payload_odex,MODE_PRIVATE);37. Filelibs=this.getDir(payload_lib,MODE_PRIVATE);38. odexPath=odex.getAbsolutePath();39. libPath=libs.getAbsolutePath();40. apkFileName=odex.getAbsolutePath()+/payload.apk;41. FiledexFile=newFile(apkFileName);42. if(!dexFile.exists()43. dexFile.createNewFile();44. /读取程序classes.dex文件45. bytedexdata=this.readDexFileFromApk();46. /分离出解壳后的apk文件已用于动态加载47. this.splitPayLoadFromDex(dexdata);48. /配置动态加载环境49. ObjectcurrentActivityThread=RefInvoke.invokeStaticMethod(50. android.app.ActivityThread,currentActivityThread,51. newClass,newObject);52. StringpackageName=this.getPackageName();53. HashMapmPackages=(HashMap)RefInvoke.getFieldOjbect(54. android.app.ActivityThread,currentActivityThread,55. mPackages);56. WeakReferencewr=(WeakReference)mPackages.get(packageName);57. DexClassLoaderdLoader=newDexClassLoader(apkFileName,odexPath,58. libPath,(ClassLoader)RefInvoke.getFieldOjbect(59. android.app.LoadedApk,wr.get(),mClassLoader);60. RefInvoke.setFieldOjbect(android.app.LoadedApk,mClassLoader,61. wr.get(),dLoader);62. 63. 64. catch(Exceptione)65. /TODOAuto-generatedcatchblock66. e.printStackTrace();67. 68. 69. 70. 71. publicvoidonCreate()72. 73. 74. 75. /如果源应用配置有Appliction对象,则替换为源应用Applicaiton,以便不影响源程序逻辑。76. StringappClassName=null;77. try78. ApplicationInfoai=this.getPackageManager()79. .getApplicationInfo(this.getPackageName(),80. PackageManager.GET_META_DATA);81. Bundlebundle=ai.metaData;82. if(bundle!=null83. &bundle.containsKey(APPLICATION_CLASS_NAME)84. appClassName=bundle.getString(APPLICATION_CLASS_NAME);85. else86. return;87. 88. catch(NameNotFoundExceptione)89. /TODOAuto-generatedcatchblock90. e.printStackTrace();91. 92. 93. 94. ObjectcurrentActivityThread=RefInvoke.invokeStaticMethod(95. android.app.ActivityThread,currentActivityThread,96. newClass,newObject);97. ObjectmBoundApplication=RefInvoke.getFieldOjbect(98. android.app.ActivityThread,currentActivityThread,99. mBoundApplication);100. ObjectloadedApkInfo=RefInvoke.getFieldOjbect(101. android.app.ActivityThread$AppBindData,102. mBoundApplication,info);103. RefInvoke.setFieldOjbect(android.app.LoadedApk,mApplication,104. loadedApkInfo,null);105. ObjectoldApplication=RefInvoke.getFieldOjbect(106. android.app.ActivityThread,currentActivityThread,107. mInitialApplication);108. ArrayListmAllApplications=(ArrayList)RefInvoke109. .getFieldOjbect(android.app.ActivityThread,110. currentActivityThread,mAllApplications);111. mAllApplications.remove(oldApplication);112. ApplicationInfoappinfo_In_LoadedApk=(ApplicationInfo)RefInvoke113. .getFieldOjbect(android.app.LoadedApk,loadedApkInfo,114. mApplicationInfo);115. ApplicationInfoappinfo_In_AppBindData=(ApplicationInfo)RefInvoke116. .getFieldOjbect(android.app.ActivityThread$AppBindData,117. mBoundApplication,appInfo);118. appinfo_In_LoadedApk.className=appClassName;119. appinfo_In_AppBindData.className=appClassName;120. Applicationapp=(Application)RefInvoke.invokeMethod(121. android.app.LoadedApk,makeApplication,loadedApkInfo,122. newClassboolean.class,Instrumentation.class,123. newObjectfalse,null);124. RefInvoke.setFieldOjbect(android.app.ActivityThread,125. mInitialApplication,currentActivityThread,app);126. 127. 128. HashMapmProviderMap=(HashMap)RefInvoke.getFieldOjbect(129. android.app.ActivityThread,currentActivityThread,130. mProviderMap);131. Iteratorit=mProviderMap.values().iterator();132. while(it.hasNext()133. ObjectproviderClientRecord=it.next();134. ObjectlocalProvider=RefInvoke.getFieldOjbect(135. android.app.ActivityThread$ProviderClientRecord,136. providerClientRecord,mLocalProvider);137. RefInvoke.setFieldOjbect(android.content.ContentProvider,138. mContext,localProvider,app);139. 140. app.onCreate();141. 142. 143. 144. 145. privatevoidsplitPayLoadFromDex(bytedata)throwsIOException146. byteapkdata=decrypt(data);147. intablen=apkdata.length;148. bytedexlen=newbyte4;149. System.arraycopy(apkdata,ablen-4,dexlen,0,4);150. ByteArrayInputStreambais=newByteArrayInputStream(dexlen);151. DataInputStreamin=newDataInputStream(bais);152. intreadInt=in.readInt();153. System.out.println(Integer.toHexString(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论