【Android】Android程序保护与破解浅析

此文源自组内成员分享的PPT,其他成员的文档由于没有得到授权,暂不公开。

本文命令如果没有特殊注明,均为windows 7环境。

本文只涉及大概的知识点,不涉及具体的细节,需要注意。

 

反编译

  apktool

    可反编译资源文件(xml,点九图)以及代码为smali代码

    使用命令:apktool d xxx.apk output_filepath

  dex2jar

    反编译dex文件(解压apk获得的classes.dex)为jar

    使用命令:dex2jar xxx.dex

  jd-gui

    查看jar文件代码

    使用方法,直接打开jar文件即可

  AXMLPrinter2 单个xml文件

    java -jar AXMLPrinter2.jar xxx.xml >output.xml

 

反编译的应对

  •代码混淆

  •增加会引起反编译器异常的代码

  •关键代码使用NDK

  •软件加壳(如UPX)

  •检测模拟器、调试器对抗动态调试

  •检查签名、检验保护(classes.dex hash值)防止重编译

 

混淆

  •混淆原理

    在应用程序保持语句含义不变的前提下从程序P转换到了P'。

    混淆技术是指对拟发布的应用程序进行保持语义的变换,使得变换后的程序和原来的程序在功能上相同或相近,但是更难以被逆向工程所攻击。

  •常见方法

    代码外形混淆(改名)

    控制命令混淆(改变程序判断条件或者增加可控制条件以及其他对程序的结构以及流程进行调整)

    内部数据混淆(数据结构的变换,变量的分裂、合并,数据结构变换,静态数据动态生成,类继承转换)

    预防混淆(增加某些特定反编译反编译时会出错的代码)

  •评价指标

    强度,混淆算法对程序增加的复杂度

    弹性,混淆后程序的抗机器攻击能力

    开销,由代码转换带来的额外开销

Proguard

  •代码外形混淆

  •sdkpath\tools\proguard \proguard-android.txt

  •项目proguard-project.txt

    
 1 # This is a configuration file for ProGuard.
 2 # http://proguard.sourceforge.net/index.html#manual/usage.html
 3 
 4 -dontusemixedcaseclassnames #包明不混合大小写
 5 -dontskipnonpubliclibraryclasses #不去忽略非公共的库类
 6 -verbose 
 7 
 8 # Optimization is turned off by default. Dex does not like code run
 9 # through the ProGuard optimize and preverify steps (and performs some
10 # of these optimizations on its own).
11 -dontoptimize #优化
12 -dontpreverify #预校验
13 # Note that if you want to enable optimization, you cannot just
14 # include optimization flags in your own project configuration file;
15 # instead you will need to point to the
16 # "proguard-android-optimize.txt" file instead of this one from your
17 # project.properties file.
18 
19 -keepattributes *Annotation* #保护注解
20 -keep public class com.google.vending.licensing.ILicensingService #保护指定的类
21 -keep public class com.android.vending.licensing.ILicensingService 
22 
23 # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
24 #不混淆jni方法
25 -keepclasseswithmembernames class * { 
26     native <methods>;
27 }
28 
29 # keep setters in Views so that animations can still work.
30 # see http://proguard.sourceforge.net/manual/examples.html#beans
31 -keepclassmembers public class * extends android.view.View {
32    void set*(***);
33    *** get*();
34 }
35 
36 # We want to keep methods in Activity that could be used in the XML attribute onClick
37 -keepclassmembers class * extends android.app.Activity {
38    public void *(android.view.View);
39 }
40 
41 # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
42 -keepclassmembers enum * {
43     public static **[] values();
44     public static ** valueOf(java.lang.String);
45 }
46 #不混淆Parcelable的子类,防止android.os.BadParcelableException
47 -keep class * implements android.os.Parcelable {
48   public static final android.os.Parcelable$Creator *;
49 }
50 #不混淆资源类
51 -keepclassmembers class **.R$* {
52     public static <fields>;
53 }
54 
55 # The support library contains references to newer platform versions.
56 # Don't warn about those in case this app is linking against an older
57 # platform version.  We know about them, and they are safe.
58 -dontwarn android.support.**
View Code

 

  原理图,经过压缩->优化->混淆->预校验4个步骤,默认优化以及预校验是没有打开的

 

  •混淆注意事项

    避免混淆泛型(fastjson)

      -keepattributes Signature

    排除反射、序列化相关的类

    排除native方法,以及AndroidManifest.xml提到的类

    忽略警告

      -ignorewarnings

      -dontwarn android.support.**

    保留一个完整的包

      -keep class com.sogou.appmall.**{*;}

  •调试与bug追踪

    1.dump.txt apk包内所有class的内部结构

    2.mapping.txt 混淆前后的映射

    3.seeds.txt 未混淆的类和成员

    4.usage.txt 列出从apk中删除的代码

  •还原日志

    retrace.bat|retrace.sh [-verbose] mapping.txt [<stacktrace_file>]

    比如:retrace.bat -verbose mapping.txt obfuscated_trace.txt

    如果需要输出的日志有行号,需要添加

      -renamesourcefileattribute SourceFile

      -keepattributes SourceFile,LineNumberTable #输出错误信息行号

 

更多

  •1. http://proguard.sourceforge.net/

  •2.http://developer.android.com/tools/help/proguard.html

  •3.Proguard简要语法手册

  •4.Android常见反编译工具

posted @ 2014-04-19 22:42  云海之巅  阅读(2059)  评论(0编辑  收藏  举报