Android开源:StringFog-对dex文件中的字符串进行加密Android插件

xopj1410 7年前
   <p>一款自动对dex文件中的字符串进行加密Android插件工具,正如名字所言,给字符串加上一层雾霭,使人难以窥视其真面目。</p>    <h3>原理</h3>    <p>在java文件编译成class字节码后,class字节码压缩成dex文件前,利用ASM库对class字节码中的字符串进行加密,同时将解密调用自动写进class字节码中,做到在运行时还原字符串内容,举个栗子:</p>    <ul>     <li>原文:</li>    </ul>    <pre>  <code class="language-java">String a = "This is a string!";  </code></pre>    <ul>     <li>加密:</li>    </ul>    <pre>  <code class="language-java">String a = Decoder.decode("ABCDEFGHIJKLMN");  </code></pre>    <h3>优缺点</h3>    <p>将dex中的字符串进行加密,可以提高反编译的难度,对于类似appId、appKey等敏感字符串,进行自动加密后,逆向应用时在使用jadx这一类反编译工具定位查找这些字符串的难度将加大,比如微信的appId前缀是"wx",不加密基本上直接就可以搜索定位出来。当然,这里需要声明一点,没有绝对的安全,由于解密Key和算法是同样写在dex里面的,逆向时处理一下是可以还原出字符串内容的。另外,在运行时解密字符串会相应地降低性能,不过由于算法简单,影响不大。</p>    <h3>演示</h3>    <p>加密前:</p>    <p><img src="https://simg.open-open.com/show/0ffbd186656aa6bc264dca29b3798ce9.png"></p>    <p>加密后:</p>    <p><img src="https://simg.open-open.com/show/42f8aa8d5ee8c8dcda10d96e68503aff.png"></p>    <h3>使用</h3>    <p>由于开发了gradle插件,所以在集成时非常简单,不会影响到打包的配置。插件已经上传到jcenter,直接引用依赖就可以。</p>    <p>1、在根目录build.gradle中引入插件依赖</p>    <pre>  <code class="language-java">buildscript {      repositories {          jcenter()      }      dependencies {          ...          classpath 'com.github.megatronking.stringfog:gradle-plugin:1.0.0'      }  }  </code></pre>    <p>2、在app的build.gradle中配置插件</p>    <pre>  <code class="language-java">apply plugin: 'stringfog'    stringfog {      key 'Hello World'  // 这是加解密key,可以自由定义      enable true // 开关  }  </code></pre>    <p>3、在app的build.gradle中引入加解密库依赖</p>    <pre>  <code class="language-java">dependencies {        ...        compile 'com.github.megatronking.stringfog:lib:1.0.0'  }  </code></pre>    <h3>补充</h3>    <p>由于没有必要对dex中所有字符串加密,所以我们采取了白名单机制,对于以下常用的库和类,处理过程中会自动忽略:</p>    <pre>  <code class="language-java">// default packages in white list.  addWhiteList("android.support", FLAG_PACKAGE);  addWhiteList("com.google", FLAG_PACKAGE);  addWhiteList("com.非死book", FLAG_PACKAGE);  addWhiteList("com.baidu", FLAG_PACKAGE);  addWhiteList("com.alipay", FLAG_PACKAGE);  addWhiteList("com.alibaba", FLAG_PACKAGE);  addWhiteList("com.tencent", FLAG_PACKAGE);  addWhiteList("de.greenrobot", FLAG_PACKAGE);  addWhiteList("com.qq", FLAG_PACKAGE);    // default short class names in white list.  addWhiteList("BuildConfig", FLAG_CLASS);  addWhiteList("R", FLAG_CLASS);  </code></pre>    <p>当然,如果开发者有不需要自动加密的类,可以使用注解StringFogIgnore来忽略:</p>    <pre>  <code class="language-java">@StringFogIgnore  public class Test {      ...  }  </code></pre>    <pre>  <code class="language-java">Copyright (C) 2017, Megatron King    Licensed under the Apache License, Version 2.0 (the "License");  you may not use this file except in compliance with the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0    Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and  limitations under the License.  </code></pre>    <p> </p>    <p> </p>