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

    
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames #包明不混合大小写
-dontskipnonpubliclibraryclasses #不去忽略非公共的库类
-verbose 

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize #优化
-dontpreverify #预校验
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation* #保护注解
-keep public class com.google.vending.licensing.ILicensingService #保护指定的类
-keep public class com.android.vending.licensing.ILicensingService 

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
#不混淆jni方法
-keepclasseswithmembernames class * { 
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
#不混淆Parcelable的子类,防止android.os.BadParcelableException
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
#不混淆资源类
-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

 

  原理图,经过压缩->优化->混淆->预校验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常见反编译工具



转帖请注明
本文出自云海之巅的博客(http://www.cnblogs.com/waylife/),谢谢!
微博:http://weibo.com/waylife365
posted @ 2014-07-15 23:15  Wsf5918  阅读(492)  评论(0编辑  收藏  举报