Android開源:StringFog-對dex文件中的字符串進行加密Android插件
一款自動對dex文件中的字符串進行加密Android插件工具,正如名字所言,給字符串加上一層霧靄,使人難以窺視其真面目。
原理
在java文件編譯成class字節碼后,class字節碼壓縮成dex文件前,利用ASM庫對class字節碼中的字符串進行加密,同時將解密調用自動寫進class字節碼中,做到在運行時還原字符串內容,舉個栗子:
- 原文:
String a = "This is a string!";
- 加密:
String a = Decoder.decode("ABCDEFGHIJKLMN");
優缺點
將dex中的字符串進行加密,可以提高反編譯的難度,對于類似appId、appKey等敏感字符串,進行自動加密后,逆向應用時在使用jadx這一類反編譯工具定位查找這些字符串的難度將加大,比如微信的appId前綴是"wx",不加密基本上直接就可以搜索定位出來。當然,這里需要聲明一點,沒有絕對的安全,由于解密Key和算法是同樣寫在dex里面的,逆向時處理一下是可以還原出字符串內容的。另外,在運行時解密字符串會相應地降低性能,不過由于算法簡單,影響不大。
演示
加密前:
加密后:
使用
由于開發了gradle插件,所以在集成時非常簡單,不會影響到打包的配置。插件已經上傳到jcenter,直接引用依賴就可以。
1、在根目錄build.gradle中引入插件依賴
buildscript {
repositories {
jcenter()
}
dependencies {
...
classpath 'com.github.megatronking.stringfog:gradle-plugin:1.0.0'
}
}
2、在app的build.gradle中配置插件
apply plugin: 'stringfog'
stringfog {
key 'Hello World' // 這是加解密key,可以自由定義
enable true // 開關
}
3、在app的build.gradle中引入加解密庫依賴
dependencies {
...
compile 'com.github.megatronking.stringfog:lib:1.0.0'
}
補充
由于沒有必要對dex中所有字符串加密,所以我們采取了白名單機制,對于以下常用的庫和類,處理過程中會自動忽略:
// 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);
當然,如果開發者有不需要自動加密的類,可以使用注解StringFogIgnore來忽略:
@StringFogIgnore
public class Test {
...
}
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.
本文由用戶 xopj1410 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!