android混淆和反编译

混淆

Android Studio: 只需在build.gradle(Module:app)中的buildTypes中增加release的编译选项即可,如下:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> buildTypes {
        release {
            minifyEnabled <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>
            proguardFiles getDefaultProguardFile(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'proguard-android.txt'</span>), <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'proguard-rules.pro'</span>
        }
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

这个proguard-android.txt是sdk中groguard默认的文件,具体地址在:/opt/sdk/tools/proguard/proguard-android.txt  而proguard-rules.pro是AS中专用的proguard配置文件,其实只是后缀名不同,与Eclipse中的proguard-project.txt是一样的,配置规则相同,后面会详细提到。  老版本开启混淆的命令是runProguard,现在统一用minifyEnabled命令了,将其设为true就好了。  编译的时候可以使用命令:

<code class="hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">./gradlew assembleRelease</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

或者用上一篇生成签名apk的办法都可。

Eclipse: 在project.properties文件中开启proguard配置(放开注释),如下:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">proguard<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.config</span>=${sdk<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.dir</span>}/tools/proguard/proguard-android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>:proguard-project<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

同样,生成签名时代码就会被混淆。

混淆语法 请参考${sdk.dir}/tools/proguard/proguard-android.txt文件,需要注意的是文件中虽然有了不混淆Parcelable的语句,如下:

<code class="hljs php has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-keep <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> * <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">implements</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">android</span>.<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">os</span>.<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Parcelable</span> {</span>
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> android.os.Parcelable<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$Creator</span> *;
}
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

但是还是要自己把继承自Parcelable的类写进来避免混淆,否则会出现BadParcelableException异常。

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-keep class <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.linc</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.datatype</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.XXInfo</span> {*<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;}</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

为微信分享而引入的jar包,我们不需要对其进行混淆,也需要在proguard-android.txt中注明,如下:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-keep class <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tencent</span>.** { *<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; }</span>
-keep class <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tencent</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mm</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sdk</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.openapi</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WXMediaMessage</span> {*<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;}</span>
-keep class <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tencent</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mm</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sdk</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.openapi</span>.** implements <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tencent</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mm</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sdk</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.openapi</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WXMediaMessage</span>$IMediaObject {*<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;}</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

为了验证是否混淆成功,可以使用下面的反编译工具验证。

反编译

主要用到三个工具:  dex2jar:将dex文件转为jar文件  jd-gui:反编译jar文件  AXMLPrinter2.jar:反编译xml文件

使用方法参见《反编译apk文件,得到其源代码的方法》

对于Ubuntu64位,运行jd-gui或许会报错:  尝试解决如下:

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">sudo</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">apt-get</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">install</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libgtk2</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0-0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libnss3</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libcurl3-gnutls</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libidn11</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libpango1</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0-0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libpangox-1</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0-0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libpangoxft-1</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0-0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">librtmp0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libxft2</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span>
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

又报错:

<code class="hljs vhdl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ /opt/sdk/tools/jd-gui: error <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> loading <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">shared</span> libraries: libXxf86vm.so<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.1</span>: cannot <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">open</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">shared</span> object <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span>: No such <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">or</span> directory</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

解决办法如下:

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">sudo</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">apt-get</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">install</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libgtk2</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0-0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libxxf86vm1</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libsm6</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">lib32stdc</span>++6

</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Youngxj

php在线扫描ip端口工具源码分享

3617
来自专栏施炯的IoT开发专栏

使用托管代码开发WINCE自带数据库

    《在Windows Mobile上使用WINCE自带数据库》中,讲述了在Windows Mobile上使用native code来开发WINCE自带数据...

2526
来自专栏一场梦

Emlog加cdn获取不到真实IP的解决办法

1932
来自专栏smy

php获取用户真实IP和防刷机制

X-Forwarded-For(XFF):  是用来识别通过HTTP代理或负载均衡方式连接到Web服务器的客户端最原始的IP地址的HTTP请求头字段, 格式:c...

4702
来自专栏大前端_Web

git submodule 完整用法整理

版权声明:本文为吴孔云博客原创文章,转载请注明出处并带上链接,谢谢。 https://blog.csdn.net/wkyseo/articl...

4842
来自专栏nummy

maven安装与配置

1153
来自专栏代码GG之家

封装之路(四) BaseFragment BaseViewModel BaseModel

框架实时变更,一直在调整,也是自己的实践过程,继续奋斗了。估计大家对实现没啥兴趣,这节结束,后面直接去github地址看更新了,就不再开贴说了。当这个框架完成时...

22110
来自专栏Youngxj

PHP获取用户访问真实IP地址 - Emlog程序IP黑名单插件

2075
来自专栏区块链

腾讯云乐固加固FAQ

注册腾讯云账号成功后找到账号信息,选择绑定QQ或者微信,以后使用PC工具和登录官网时就可以三方快速登录了。

1.4K2
来自专栏PingCAP的专栏

使用 Ansible 安装部署 TiDB

多机部署 TiDB 太繁琐?快来尝试我们的 TiDB 一键安装吧。本文介绍基于 Ansible Playbook 实现 TiDB 和监控组件的自动化安装和配置,...

1.4K0

扫码关注云+社区

领取腾讯云代金券