Android安全之防止被反编译

来源:互联网 发布:中华网络传奇一条龙 编辑:程序博客网 时间:2024/06/05 06:43

关于防止android apk被反编译的技术,主要介绍以下几种:

1、加壳技术(推荐)

2、运行时修改字节码

3、伪加密

4、对抗JD-GUI(推荐)

5、完整性校验(推荐)


防止apk反编译的技术1---加壳技术

一、什么是加壳?

       加壳是在二进制的程序中植入一段代码,在运行的时候优先取得程序的控制权,做一些额外的工作。


大多数病毒就是基于此原理。PC EXE文件加壳的过程如下:


     

二、加壳作用

       加壳的程序可以有效阻止对程序的反汇编分析,以达到它不可告人的目的。这种技术也常用来保护软件版权,防止被软件破解。



android apk加壳实现

  要想实现加壳需要解决的技术点如下:

1)怎么第一时间执行我们的加壳程序?

首先根据上面的原理,想要优先取得程序的控制权,Application会被系统第一时间调用,而我们的程序也会放在这里执行。

2)怎么将我们的加壳程序和原有的android apk文件合并到一起?

我们知道android apk最终会打包生成dex文件(关于dex详细介绍请参照这里),我们可以将我们的程序生成dex文件后,将我们要进行加壳的apk和我们dex文件合并成一个文件,然后修改dex文件头中的checksumsignature file_size的信息,并且要附加加壳的apk的长度信息在dex文件中,以便我们进行解壳保证原来apk的正常运行。加完壳后整个文件的结构如下:


3)怎么将原来的apk正常的运行起来?

按照(2)中的合并方式在当我们的程序首先运行起来后,逆向读取dex文件获取原来的apk文件通过DexClassLoader动态加载。

具体实现如下:

1)修改原来apkAndroidMainfest.xml文件,假如原来apkAndroidMainfest.xml文件内容如下:

1.  <application  

2.      android:icon="@drawable/ic_launcher"  

3.      android:label="@string/app_name"  

4.      android:theme="@style/AppTheme" android:name="com.android.MyApplication" >  

5.  </application>

修改后的内容如下:

1.  <application  

2.      android:icon="@drawable/ic_launcher"  

3.      android:label="@string/app_name"  

4.      android:theme="@style/AppTheme" android:name="com.android.shellApplication" >  

5.  <meta-data android:name="APPLICATION_CLASS_NAME" android:value="com.android.MyApplication"/>

6.  </application> 

com.android.shellApplication这个就是我们的程序的的application的名称,而

7.  <meta-data android:name="APPLICATION_CLASS_NAME" android:value="com.android.MyApplication"/>

是原来的apkapplication名称。

2)合并文件代码实现如下:

public class ShellTool {  /**   * 合并代码实现   * @param args   */  public static void main(String[] args) {         try {                File payloadSrcFile = new File("payload.apk");//我们要加壳的apk文件                File unShellDexFile = new File("classes.dex");//我们的程序生成的dex文件                byte[] payloadArray = encrpt(readFileBytes(payloadSrcFile));                byte[] unShellDexArray = readFileBytes(unShellDexFile);                int payloadLen = payloadArray.length;                int unShellDexLen = unShellDexArray.length;                int totalLen = payloadLen + unShellDexLen +4;                byte[] newdex = new byte[totalLen];                //添加我们程序的dex                System.arraycopy(unShellDexArray, 0, newdex, 0, unShellDexLen);                //添加要加壳的apk文件                System.arraycopy(payloadArray, 0, newdex, unShellDexLen, payloadLen);                //添加apk文件长度                System.arraycopy(intToByte(payloadLen), 0, newdex, totalLen-4, 4);                //修改DEX file size文件头                fixFileSizeHeader(newdex);                //修改DEX SHA1 文件头                fixSHA1Header(newdex);                //修改DEX CheckSum文件头                fixCheckSumHeader(newdex);                 String str = "outdir/classes.dex";                File file = new File(str);                if (!file.exists()) {                       file.createNewFile();                }                                FileOutputStream localFileOutputStream = new FileOutputStream(str);                localFileOutputStream.write(newdex);                localFileOutputStream.flush();                localFileOutputStream.close();         } catch (Exception e) {                e.printStackTrace();         }  }    //直接返回数据,读者可以添加自己加密方法  private static byte[] encrpt(byte[] srcdata){         return srcdata;  }   private static void fixCheckSumHeader(byte[] dexBytes) {         Adler32 adler = new Adler32();         adler.update(dexBytes, 12, dexBytes.length - 12);         long value = adler.getValue();         int va = (int) value;         byte[] newcs = intToByte(va);         byte[] recs = new byte[4];         for (int i = 0; i < 4; i++) {                recs[i] = newcs[newcs.length - 1 - i];                System.out.println(Integer.toHexString(newcs[i]));         }         System.arraycopy(recs, 0, dexBytes, 8, 4);         System.out.println(Long.toHexString(value));         System.out.println();  }    public static byte[] intToByte(int number) {         byte[] b = new byte[4];         for (int i = 3; i >= 0; i--) {           b[i] = (byte) (number % 256);           number >>= 8;         }         return b;  }    private static void fixSHA1Header(byte[] dexBytes) throws NoSuchAlgorithmException {         MessageDigest md = MessageDigest.getInstance("SHA-1");         md.update(dexBytes, 32, dexBytes.length - 32);         byte[] newdt = md.digest();         System.arraycopy(newdt, 0, dexBytes, 12, 20);         String hexstr = "";         for (int i = 0; i < newdt.length; i++) {                hexstr += Integer.toString((newdt[i] & 0xff) + 0x100, 16).substring(1);         }         System.out.println(hexstr);  }    private static void fixFileSizeHeader(byte[] dexBytes) {         byte[] newfs = intToByte(dexBytes.length);         System.out.println(Integer.toHexString(dexBytes.length));         byte[] refs = new byte[4];         for (int i = 0; i < 4; i++) {            refs[i] = newfs[newfs.length - 1 - i];            System.out.println(Integer.toHexString(newfs[i]));         }         System.arraycopy(refs, 0, dexBytes, 32, 4);  }    private static byte[] readFileBytes(File file) throws IOException {         byte[] arrayOfByte = new byte[1024];         ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();         FileInputStream fis = new FileInputStream(file);         while (true) {                int i = fis.read(arrayOfByte);                if (i != -1) {                       localByteArrayOutputStream.write(arrayOfByte, 0, i);                } else {                       return localByteArrayOutputStream.toByteArray();                }         }  } }

3)在我们的程序中加载运行原来的apk文件,代码如下:

public class shellApplication extends Application {   private static final String appkey = "APPLICATION_CLASS_NAME";  private String apkFileName;  private String odexPath;  private String libPath;   protected void attachBaseContext(Context base) {         super.attachBaseContext(base);         try {                File odex = this.getDir("payload_odex", MODE_PRIVATE);                File libs = this.getDir("payload_lib", MODE_PRIVATE);                odexPath = odex.getAbsolutePath();                libPath = libs.getAbsolutePath();                apkFileName = odex.getAbsolutePath() + "/payload.apk";                File dexFile = new File(apkFileName);                if (!dexFile.exists())                       dexFile.createNewFile();                // 读取程序classes.dex文件                byte[] dexdata = this.readDexFileFromApk();                // 分离出解壳后的apk文件已用于动态加载                this.splitPayLoadFromDex(dexdata);                // 配置动态加载环境                Object currentActivityThread = RefInvoke.invokeStaticMethod(                              "android.app.ActivityThread", "currentActivityThread",                              new Class[] {}, new Object[] {});                String packageName = this.getPackageName();                HashMap mPackages = (HashMap) RefInvoke.getFieldOjbect(                              "android.app.ActivityThread", currentActivityThread,                              "mPackages");                WeakReference wr = (WeakReference) mPackages.get(packageName);                DexClassLoader dLoader = new DexClassLoader(apkFileName, odexPath,                              libPath, (ClassLoader) RefInvoke.getFieldOjbect(                                            "android.app.LoadedApk", wr.get(), "mClassLoader"));                RefInvoke.setFieldOjbect("android.app.LoadedApk", "mClassLoader",                              wr.get(), dLoader);          } catch (Exception e) {                e.printStackTrace();         }  }    public void onCreate() {         {                // 如果源应用配置有Appliction对象,则替换为源应用Applicaiton,以便不影响源程序逻辑。                String appClassName = null;                try {                       ApplicationInfo ai = this.getPackageManager()                                     .getApplicationInfo(this.getPackageName(),                                                   PackageManager.GET_META_DATA);                       Bundle bundle = ai.metaData;                       if (bundle != null                                     && bundle.containsKey("APPLICATION_CLASS_NAME")) {                              appClassName = bundle.getString("APPLICATION_CLASS_NAME");                       } else {                              return;                       }                } catch (NameNotFoundException e) {                       e.printStackTrace();                }                  Object currentActivityThread = RefInvoke.invokeStaticMethod(                              "android.app.ActivityThread", "currentActivityThread",                              new Class[] {}, new Object[] {});                Object mBoundApplication = RefInvoke.getFieldOjbect(                              "android.app.ActivityThread", currentActivityThread,                              "mBoundApplication");                Object loadedApkInfo = RefInvoke.getFieldOjbect(                              "android.app.ActivityThread$AppBindData",                              mBoundApplication, "info");                RefInvoke.setFieldOjbect("android.app.LoadedApk", "mApplication",                              loadedApkInfo, null);                Object oldApplication = RefInvoke.getFieldOjbect(                              "android.app.ActivityThread", currentActivityThread,                              "mInitialApplication");                ArrayList<Application> mAllApplications = (ArrayList<Application>) RefInvoke                              .getFieldOjbect("android.app.ActivityThread",                                            currentActivityThread, "mAllApplications");                mAllApplications.remove(oldApplication);                ApplicationInfo appinfo_In_LoadedApk = (ApplicationInfo) RefInvoke                              .getFieldOjbect("android.app.LoadedApk", loadedApkInfo,                                            "mApplicationInfo");                ApplicationInfo appinfo_In_AppBindData = (ApplicationInfo) RefInvoke                              .getFieldOjbect("android.app.ActivityThread$AppBindData",                                            mBoundApplication, "appInfo");                appinfo_In_LoadedApk.className = appClassName;                appinfo_In_AppBindData.className = appClassName;                Application app = (Application) RefInvoke.invokeMethod(                              "android.app.LoadedApk", "makeApplication", loadedApkInfo,                              new Class[] { boolean.class, Instrumentation.class },                              new Object[] { false, null });                RefInvoke.setFieldOjbect("android.app.ActivityThread",                              "mInitialApplication", currentActivityThread, app);                  HashMap mProviderMap = (HashMap) RefInvoke.getFieldOjbect(                              "android.app.ActivityThread", currentActivityThread,                              "mProviderMap");                Iterator it = mProviderMap.values().iterator();                while (it.hasNext()) {                       Object providerClientRecord = it.next();                       Object localProvider = RefInvoke.getFieldOjbect(                                     "android.app.ActivityThread$ProviderClientRecord",                                     providerClientRecord, "mLocalProvider");                       RefInvoke.setFieldOjbect("android.content.ContentProvider",                                     "mContext", localProvider, app);                }                app.onCreate();         }  }    private void splitPayLoadFromDex(byte[] data) throws IOException {         byte[] apkdata = decrypt(data);         int ablen = apkdata.length;         byte[] dexlen = new byte[4];         System.arraycopy(apkdata, ablen - 4, dexlen, 0, 4);         ByteArrayInputStream bais = new ByteArrayInputStream(dexlen);         DataInputStream in = new DataInputStream(bais);         int readInt = in.readInt();         System.out.println(Integer.toHexString(readInt));         byte[] newdex = new byte[readInt];         System.arraycopy(apkdata, ablen - 4 - readInt, newdex, 0, readInt);         File file = new File(apkFileName);         try {                FileOutputStream localFileOutputStream = new FileOutputStream(file);                localFileOutputStream.write(newdex);                localFileOutputStream.close();         } catch (IOException localIOException) {                throw new RuntimeException(localIOException);         }          ZipInputStream localZipInputStream = new ZipInputStream(                       new BufferedInputStream(new FileInputStream(file)));         while (true) {                ZipEntry localZipEntry = localZipInputStream.getNextEntry();                if (localZipEntry == null) {                       localZipInputStream.close();                       break;                }                String name = localZipEntry.getName();                if (name.startsWith("lib/") && name.endsWith(".so")) {                       File storeFile = new File(libPath + "/"                                     + name.substring(name.lastIndexOf('/')));                       storeFile.createNewFile();                       FileOutputStream fos = new FileOutputStream(storeFile);                       byte[] arrayOfByte = new byte[1024];                       while (true) {                              int i = localZipInputStream.read(arrayOfByte);                              if (i == -1)                                     break;                              fos.write(arrayOfByte, 0, i);                       }                       fos.flush();                       fos.close();                }                localZipInputStream.closeEntry();         }         localZipInputStream.close();  }    private byte[] readDexFileFromApk() throws IOException {         ByteArrayOutputStream dexByteArrayOutputStream = new ByteArrayOutputStream();         ZipInputStream localZipInputStream = new ZipInputStream(                       new BufferedInputStream(new FileInputStream(                                     this.getApplicationInfo().sourceDir)));         while (true) {                ZipEntry localZipEntry = localZipInputStream.getNextEntry();                if (localZipEntry == null) {                       localZipInputStream.close();                       break;                }                if (localZipEntry.getName().equals("classes.dex")) {                       byte[] arrayOfByte = new byte[1024];                       while (true) {                              int i = localZipInputStream.read(arrayOfByte);                              if (i == -1)                                     break;                              dexByteArrayOutputStream.write(arrayOfByte, 0, i);                       }                }                localZipInputStream.closeEntry();         }         localZipInputStream.close();         return dexByteArrayOutputStream.toByteArray();  }    // //直接返回数据,读者可以添加自己解密方法  private byte[] decrypt(byte[] data) {         return data;  }

根据上面的讲述相信大家对apk的加壳技术有了一定的了解。





防止apk反编译的技术2---运行时修改字节码

我们知道apk生成后所有的java生成的class文件都被dx命令整合成了一个classes.dex文件,当apk运行时dalvik虚拟机加载classes.dex文件并且用dexopt命令进行进一步的优化成odex文件。我们的方法就是在这个过程中修改dalvik指令来达到我们的目的。

一、dex文件格式

dex的文件格式通常有7个主要部分和数据區组成,格式如下:


 

header部分记录了主要的信息其他的部分只是索引,索引的内容存在data区域Header部分结构如下:

字段名称

偏移值

长度

描述

magic

0x0

8

'Magic'值,即魔数字段,格式如”dex/n035/0”,其中的035表示结构的版本。

checksum

0x8

4

校验码。

signature

0xC

20

SHA-1签名。

file_size

0x20

4

Dex文件的总长度。

header_size

0x24

4

文件头长度,009版本=0x5C,035版本=0x70

endian_tag

0x28

4

标识字节顺序的常量,根据这个常量可以判断文件是否交换了字节顺序,缺省情况下=0x78563412

link_size

0x2C

4

连接段的大小,如果为0就表示是静态连接。

link_off

0x30

4

连接段的开始位置,从本文件头开始算起。如果连接段的大小为0,这里也是0

map_off

0x34

4

map数据基地址。

string_ids_size

0x38

4

字符串列表的字符串个数。

string_ids_off

0x3C

4

字符串列表表基地址。

type_ids_size

0x40

4

类型列表里类型个数。

type_ids_off

0x44

4

类型列表基地址。

proto_ids_size

0x48

4

原型列表里原型个数。

proto_ids_off

0x4C

4

原型列表基地址。

field_ids_size

0x50

4

字段列表里字段个数。

field_ids_off

0x54

4

字段列表基地址。

method_ids_size

0x58

4

方法列表里方法个数。

method_ids_off

0x5C

4

方法列表基地址。

class_defs_size

0x60

4

类定义类表中类的个数。

class_defs_off

0x64

4

类定义列表基地址。

data_size

0x68

4

数据段的大小,必须以4字节对齐。

data_off

0x6C

4

数据段基地址

dexclass文件相比的一个优势,就是将所有的常量字符串集统一管理起来了,这样就可以减少冗余,最终的dex文件size也能变小一些。详细的dex文件介绍就不说了,有兴趣的可以查看android 源码dalvik/docs目录下的dex-format.html文件有详细介绍。不过我记得在android4.0版本后就没有了这个文件。

根据上面的dex文件的格式结构,dalvik虚拟机运行dex文件执行的字节码就存在method_ids区域里面。我们查看dalvik虚拟机源码会有一个

        struct DexCode {  

        u2  registersSize;  

        u2  insSize;  

        u2  outsSize;  

        u2  triesSize;  

        u4  debugInfoOff;       /* file offset to debug info stream */  

        u4  insnsSize;          /* size of the insns array, in u2 units */  

        u2  insns[1];  

        /* followed by optional u2 padding */  

        /* followed by try_item[triesSize] */  

        /* followed by uleb128 handlersSize */  

        /* followed by catch_handler_item[handlersSize] */  

    };  

这样一个结构,这里的insns数组存放的就是dalvik的字节码。我们只要定位到相关类方法的DexCode数据段,即可通过修改insns数组,从而实现我们的目的。


二、odex文件格式

        apk安装或启动时,会通过dexopt来将dex生成优化的odex文件。过程是将apk中的classes.dex解压后,用dexopt处理并保存为/data/dalvik-cache/data@app@<package-name>-X.apk@classes.dex文件。

 odex文件结构如下:


从上图中我们发现dex文件作为优化后的odex的一部分,我们只需要从odex中找出dex的部分即可以了。

 

三、方法实现

        要实现修改字节码,就需要先定位到想要修改得代码的位置,这就需要先解析dex文件。dex文件的解析在dalvik源码的dexDump.cpp给出了我们具体的实现,根据它的实现我们可以查找我们需要的类及方法。具体实现步骤如下:

(1)    找到我们apk生成的odex文件,获得odex文件在内存中的映射地址和大小。实现代码如下:

      void *base = NULL;  int module_size = 0;  char filename[512];  // simple test code  here!  for(int i=0; i<2; i++){         sprintf(filename,"/data/dalvik-cache/data@app@%s-%d.apk@classes.dex", "com.android.dex", i+1);base = get_module_base(-1, filename);//获得odex文件在内存中的映射地址  if(base != NULL){         break;  }  } module_size = get_module_size(-1, filename); //获得odex文件大小

(2)    知道dex文件在odex中的偏移,以便解析dex文件。代码如下:

// search dex from odex  void *dexBase = searchDexStart(base);  if(checkDexMagic(dexBase) == false){         ALOGE("Error! invalid dex format at: %p", dexBase);         return;  }

(3)    找到dex偏移以后就可以解析dex文件,从而查找我们要进行替换的方法所在的类,然后在该类中找到该方法并返回该方法对应的DexCode结构体。函数实现如下:

static const DexCode *dexFindClassMethod(DexFile *dexFile, const char *clazz, const char *method){    DexClassData* classData = dexFindClassData(dexFile, clazz);    if(classData == NULL) return NULL;    const DexCode* code = dexFindMethodInsns(dexFile, classData, method);    if(code != NULL) {        dumpDexCode(code);    }    return code;}

(4)    找到DexCode后就可以进行指令替换了。实现如下:

const DexCode  *code =    dexFindClassMethod(&gDexFile, "Lcom/android/dex/myclass;", "setflagHidden");const DexCode*code2 = dexFindClassMethod(&gDexFile, "Lcom/android/dex/myclass;", "setflag");    // remap!!!!    if(mprotect(base, module_size, PROT_READ | PROT_WRITE | PROT_EXEC) == 0){    DexCode *pCode = (DexCode *)code2;    // Modify!    pCode->registersSize = code->registersSize;        for(u4 k=0; k<code->insnsSize; k++){               pCode->insns[k] = code->insns[k];        }      mprotect(base, module_size, PROT_READ | PROT_EXEC);}

注意:由于是在运行时修改的dalvik指令,这是进程的内存映射为只读的,所以需要调用mprotect函数将只读改为读写才能进行指令的修改。

 



防止apk反编译的技术3---对apk进行伪加密

一、伪加密技术原理

我们知道android apk本质上是zip格式的压缩包,我们将android应用程序的后缀.apk改为.zip就可以用解压软件轻松的将android应用程序解压缩。在日常生活或者工作中,我们通常为了保护我们自己的文件在进行压缩式都会进行加密处理。这样的方法对于android apk同样适用。原理很简单,在zip的文件格式中有一个位用来标示该zip压缩文件中的文件是否被加密,我们只要找到该标志位将其置1就可以实现我们的目的。而android的包安装服务(PackageManagerService)在进行apk安装时并不关心这个加密位(暂时我们就这么叫它吧)可以进行正常的安装并且也不会影响apk的运行。注意:该方法不适用android 4.2.x版本及以后系统,系统会拒绝安装这种加密的apk

二、zip文件格式

zip的文件格式通常有三个部分组成:压缩文件源数据、压缩目录源数据、目录结束标识。这三个部分中和我们说的加密位有关的是压缩目录源数据部分,我们接下来详细介绍这一部分。

压缩目录源数据部分记录着所有的压缩目录源数据。其结构如下:

Central directory file header

 



Offset

Bytes

Description[18]

0

4

Central directory file header signature =0x02014b50

核心目录文件header标识=0x02014b50

4

2

Version made by

压缩所用的pkware版本

6

2

Version needed to extract (minimum)

解压所需pkware的最低版本

8

2

General purpose bit flag

通用位标记

10

2

Compression method

压缩方法

12

2

File last modification time

文件最后修改时间

14

2

File last modification date

文件最后修改日期

16

4

CRC-32

CRC-32算法

20

4

Compressed size

压缩后大小

24

4

Uncompressed size

未压缩的大小

28

2

File name length (n)

文件名长度

30

2

Extra field length (m)

扩展域长度

32

2

File comment length (k)

文件注释长度

34

2

Disk number where file starts

文件开始位置的磁盘编号

36

2

Internal file attributes

内部文件属性

38

4

External file attributes

外部文件属性

42

4

Relative offset of local file header. This is the number of bytes between the start of the first disk on which the file occurs, and the start of the local file header. This allows software reading the central directory to locate the position of the file inside the ZIP file.

本地文件header的相对位移。

46

n

File name

目录文件名

46+n

m

Extra field

扩展域

46+n+m

k

File comment

文件注释内容

该结构中的General purpose bit flag部分的第0位如果置1,标识该压缩包被加密;置为0标识该压缩包没有被加密。

三、具体实施

我们可以利用ZipCenOp.jar这个jar包对apk进行加密和解密操作(也有用python实现这个操作的这里我们不做介绍)。

(1)    apk进行加密


加密后,我们用解压缩软件进行解密会看如下的提示信息:


 

apktool进行反编译会提示如下的错误信息:

 

加密后apk是可以正常的安装和运行的。

(2)    apk进行解密


解密对我们来说没有多大的用途,只是了解一下。





防止apk反编译的技术4---对抗JD-GUI查看源码

一、对抗JD-GUI原理

通常在对apk进行反编译的时候用到的最多的两个工具就是apk-tooldex2jar。利用这两个工具将apk首先反编译成classes.dex然后再将classes.dex反编译成jar文件或者将apk直接反编译成jar文件;得到jar文件以后就可以利用JD-GUI将得到的jar文件打开就可以直接查看apkjava源码了。我们花了那么大心思写的程序就这么容易被别人拿到源码是不是很不甘心。

对抗JD-GUI查看源码的方法:JD-GUI查看源码时,有些函数根本看不到源码,直接提示error错误,我们就利用这点来保护我们的apk

原来,JD-GUI在将经过混淆处理的jar里面的class字节码文件转成java文件时,遇到函数中走不到的分支的特殊实现时,会提示函数error。只要查到引起这些提示error的文件或者函数的代码,并将它们加到我们的类中即可

二、原理实现

(1)假如我们的apk onCreate的函数实现如下:

 @Override    protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);    }

(2)将我们的apk经过混淆处理后经过签名导出我们的apk,我们用dex2jar工具将我们的apk转换成jar文件


(3)JD-GUI打开我们的jar文件就可以看到我们的apk onCreate函数的源码了。如下:


(4)这时我们在apk onCreate函数里面加上不可能执行的特殊分支语句,代码如下:

 @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       switch(0)       {       case 1:           JSONObject jsoObj;           String date=null;           String second=null;           try           {              jsoObj=new JSONObject();              date=jsoObj.getString("date");               second=jsoObj.getString("second");           }           catch(JSONException e)           {              e.printStackTrace();           }           test.settime(date,second);           break;       }    }class test{   public static void settime(String a,String b){}}

(5)我们用(2)中同样的方法将apk转成jar文件,然后JD-GUI打开会看到提示error错误。如下:


根据上面的讲述相信大家对对抗JD-GUI的方法有了一定的了解,我只是举了其中的一个方法,之所以说是特殊的分支语句是因为不是所有的分支语句都可以让JD-GUI提示error。我们可以根据原理多注意一些这样的特殊分支语句以便用来保护我们的apk

PS:还可以使用以下测试代码:

BufferedReader in = null;InputStream ins = null;InputStreamReader inr = null;try {ins = context.getAssets().open("Config.txt");inr = new InputStreamReader(ins);in = new BufferedReader(inr, 1024);} catch (Exception e) {} finally {try {ins.close();} catch (IOException e1) {}try {inr.close();} catch (IOException e1) {}if (in != null) {try {in.close();in = null;} catch (Exception e) {}}}




防止apk反编译的技术5---完整性校验

一、完整性校验原理 

所谓完整性校验就是用各种算法来计算一个文件的完整性,防止这个文件被修改。其中常用的方法就是计算一个文件的CRC32的值或者计算一个文件的哈希值。我们知道apk生成的classes.dex主要由java文件生成的,它是整个apk的逻辑实现。所以我们可以对classes.dex文件或整个APK文件进行完整性校验,来保证整个程序的逻辑不被修改。

二、用crc32对classes.dex文件的完整性进行校验 

(1)可以打印出来我们的apk生的classes.dex文件的crc32的值,代码如下: 

public class MainActivity extendsActivity {@Overrideprotected void onCreate(BundlesavedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    String apkPath = getPackageCodePath();    Long dexCrc = Long.parseLong(getString(R.string.classesdex_crc));    try    {        ZipFile zipfile = new ZipFile(apkPath);        ZipEntry dexentry = zipfile.getEntry("classes.dex");        Log.i("verification","classes.dexcrc="+dexentry.getCrc());        if(dexentry.getCrc() != dexCrc) {           Log.i("verification","Dexhas been modified!");        } else {           Log.i("verification","Dex hasn't been modified!");        }    } catch (IOException e) {        e.printStackTrace();    }   }}
注意:R.string.classesdex_crc的值现在可以是个随机数。

(2)运行程序打印结果,我的apk程序的classes.dex的crc32的值为713769644 

(3)将上面程序的classes.dex文件的crc32的值,保存在资源文件字符串中classesdex_crc中(当然也可以保存在服务器上,然后通过网络获取校验),再运行上面的apk程序,打印如下: 

Dex hasn't beenmodified!

(4)在上面的代码中随便加一行或者一个空格,重新编译运行,会看到我们的程序的crc32的值改变了。程序打印如下: 

Dex has beenmodified!

三、用哈希值对整个apk完整性进行校验 

如果要对整个apk的完整性进行校验,那么算出哈希值就不能存在资源文件中了,因为apk中任何的改动,都会引起最终apk生成的哈希值的不同。

(1)首先实现apk中计算自身哈希值的代码,如下: 

public class MainActivity extendsActivity {@Overrideprotectedvoid onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     String apkPath = getPackageCodePath();     MessageDigest msgDigest = null;     try {        msgDigest = MessageDigest.getInstance("SHA-1");        byte[] bytes = new byte[1024];        int byteCount;        FileInputStream fis = new FileInputStream(new File(apkPath));        while ((byteCount = fis.read(bytes)) > 0)        {            msgDigest.update(bytes, 0, byteCount);        }        BigInteger bi = new BigInteger(1, msgDigest.digest());        String sha = bi.toString(16);        fis.close();        //这里添加从服务器中获取哈希值然后进行对比校验        } catch (Exception e) {            e.printStackTrace();        }    }}

(2)用linux下的shasum命令计算我们的apk的哈希值,命令如下:

计算SHA1值 :shasum <文件路径> (指令和文件路径之间有空格)PS:1、直接把文件拖到终端窗口可以自动添加文件路径。2、计算MD5值同理  :md5 <文件路径>  

(3)将(2)中生成的哈希值存到服务器上,然后使用apk中的函数代码,从服务器获取进行完整性比较。 值得提醒的是:哈希值在客户端与服务端之间通信,存在泄漏风险,因此为避免该问题,传输时应该加上随机数,保证每次都不一样。



说明:部分资料参考互联网,如有冒犯,请联系博主,谢谢!

0 0