1. 程式人生 > >Android程式碼混淆(ProGuard),資源混淆(微信 AndResGuard),SO混淆(obfuscator-llvm)

Android程式碼混淆(ProGuard),資源混淆(微信 AndResGuard),SO混淆(obfuscator-llvm)

> 程式碼混淆
  Android編譯的大致流程如下:Java Code(.java) -> Java Bytecode(.class) -> Dalvik Bytecode(classes.dex)。
 第三方的軟體可以用來混淆我們的Android應用,常見的有:Proguard;DashO;Dexguard;DexProtector;ApkProtect;Shield4j;Stringer;Allitori
  通過ProGuard或者DexGuard來保護我們的程式碼,從而實現相對的程式碼安全。
  反混淆,將對應的oat檔案通過ota2dex將其解包獲得dex檔案,之後通過dex2jar和jd-gui檢視原始碼。

餓了麼的團隊提供gradle 外掛,用來無傷混淆 Activity 和 View,Mess:https://github.com/eleme/Mess
Android Studio程式碼混淆外掛- http://www.cnblogs.com/common1140/p/5569934.html
Android Studio 程式碼混淆- https://my.oschina.net/kooeasy/blog/479083?p=1 
Android studio 使用心得(五)---程式碼混淆和破解apk- https://my.oschina.net/aibenben/blog/371889/
android studio程式碼混淆注意問題-http://blog.csdn.net/jdsjlzx/article/details/51861460
android studio 程式碼混淆打包錯誤 The same input jar is specified twice - http://www.jianshu.com/p/b39af6175b79
Android Studio混淆模板及常用第三方混淆- http://blog.csdn.net/a120705230/article/details/51791995
Android 混淆程式碼總結- http://blog.csdn.net/lovexjyong/article/details/24652085
apkprotect(免費android程式碼混淆、加密保護工具)- http://bbs.pediy.com/showthread.php?p=1183956
【安卓安全】淺談安卓開發程式碼混淆技術--

https://www.douban.com/note/565763793/
Android 混淆程式碼的原理與實施: http://blog.csdn.net/kepoon/article/details/7482058

proguard優化:http://developer.android.com/tools/help/proguard.html

Android混淆打包:http://blog.csdn.net/hudashi/article/details/9016805

Android混淆打包那些事兒:http://www.jianshu.com/p/44e3b127393e#

-- 在Android專案中,相應混淆檔案引用,並在proguard-project檔案下配置: 


# Project target.
target=android-8
proguard.config=${sdk.dir}\tools\proguard\proguard-android.txt:proguard-project.txt

-- android混淆打包- http://www.cnblogs.com/0616--ataozhijia/p/3727310.html
android 簽名、混淆打包- http://blog.csdn.net/feng4656/article/details/8685335
 你不可能只做一個APP,你可能有一個巨集偉的戰略工程,想要在生活,服務,遊戲,系統各個領域都想插足的話,你不可能只做一個APP,谷歌建議你把你所有的APP都使用同一個簽名證書。
 1) App升級。 使用相同簽名的升級軟體可以正常覆蓋老版本的軟體,否則系統比較發現新版本的簽名證書和老版本的簽名證書不一致,不會允許新版本安裝成功的。
 2) App模組化。android系統允許具有相同的App執行在同一個程序中,如果執行在同一個程序中,則他們相當於同一個App,但是你可以單獨對他們升級更新,這是一種App級別的模組化思路。
 3) 允許程式碼和資料共享。android中提供了一個基於簽名的Permission標籤。通過允許的設定,我們可以實現對不同App之間的訪問和共享,如下AndroidManifest.xml:<permission android:protectionLevel="normal" />

--   從ProGuard的工作流來看,ProGuard做的工作基本主要包含:壓縮、優化、混淆以及最後的校驗。體現到程式碼層面上做的事情就是:混淆類名、方法名、欄位名,修改方法、欄位訪問性,刪除方法(上例中內部類的構造方法),方法的內聯,甚至是減少方法的引數(這就改變了方法簽名)等等。大體可以總結為三大問題:混淆、優化、內聯,其中優化相關操作,比如說改變方法簽名和刪除方法,我們可以把這類問題劃歸到內聯,因為在優化後的程式碼裡面這些方法和內聯的方法一樣,都消失了。

> 資源混淆

   Android資源管理框架來管理資源,而Android資源管理框架實際是由AssetManager和Resources兩個類來實現的。其中,Resources類可以根據ID來查詢資源,而AssetManager類根據檔名來查詢資源。事實上,如果一個資源ID對應的是一個檔案,那麼Resources類是先根據ID來找到資原始檔名稱,然後再將該檔名稱交給AssetManager類來開啟對應的檔案的。
美團Android資源混淆保護實踐- http://tech.meituan.com/mt-android-resource-obfuscation.html
美團Android資源混淆保護實踐- http://blog.csdn.net/easyer2012/article/details/50547480
微信Android資源混淆打包工具- http://mp.weixin.qq.com/s?__biz=MzAwNDY1ODY2OQ==&mid=208135658&idx=1&sn=ac9bd6b4927e9e82f9fa14e396183a8f#rd
微信Android資源混淆AndResGuard- https://github.com/shwenzhang/AndResGuard/blob/master/README.zh-cn.md
微信Android資源混淆AndResGuard- https://github.com/shwenzhang/AndResGuard
美團Android資源混淆保護實踐- http://tech.meituan.com/mt-android-resource-obfuscation.html
 -- AAPT是Android Asset Packaging Tool,打包的流程:
 1.把"assets"和"res/raw"目錄下的所有資源進行打包(會根據不同的檔案字尾選擇壓縮或不壓縮),而"res/"目錄下的其他資源進行編譯或者其他處理(具體處理方式視檔案字尾不同而不同,例如:".xml"會編譯成二進位制檔案,".png"檔案會進行優化等等)後才進行打包;
 2.會對除了assets資源之外所有的資源賦予一個資源ID常量,並且會生成一個資源索引表resources.arsc;
 3.編譯AndroidManifest.xml成二進位制的XML檔案;
 4.把上面3個步驟中生成結果儲存在一個*.ap_檔案,並把各個資源ID常量定義在一個R.java中;

  Proguard Obfuscator方式,對APK中資原始檔名使用簡短無意義名稱進行替換,給破解者製造困難,從而做到資源的相對安全;通過上面分析,我們可以看出通過修改AAPT在生成resources.arsc和*.ap_時把資原始檔的名稱進行替換,從而保護資源。通過修改AAPT,我們可以在程式碼零修改的基礎下就能做到相對的資源安全,當然安全是相對的.
  通過閱讀AAPT編譯資源的程式碼,我們發現修改AAPT在處理資原始檔相關的原始碼是能夠做到資原始檔名的替換,下面是Resource.cpp中makeFileResources()的修改的程式碼片段:
static status_t makeFileResources(Bundle* bundle, const sp<AaptAssets>& assets,
                                      ResourceTable* table,
                                      const sp<ResourceTypeSet>& set,
                                      const char* resType)
    {
        String8 type8(resType);
        String16 type16(resType);

        bool hasErrors = false;
        ResourceDirIterator it(set, String8(resType));
        ssize_t res;
        while ((res=it.next()) == NO_ERROR) {
            if (bundle->getVerbose()) {
                printf("    (new resource id %s from %s)\n",
                       it.getBaseName().string(), it.getFile()->getPrintableSource().string());
            }
            String16 baseName(it.getBaseName());
            const char16_t* str = baseName.string();
            const char16_t* const end = str + baseName.size();
            while (str < end) {
                if (!((*str >= 'a' && *str <= 'z')
                        || (*str >= '0' && *str <= '9')
                        || *str == '_' || *str == '.')) {
                    fprintf(stderr, "%s: Invalid file name: must contain only [a-z0-9_.]\n",
                            it.getPath().string());
                    hasErrors = true;
                }
                str++;
            }
            String8 resPath = it.getPath();
            resPath.convertToResPath();

            String8 obfuscationName;
            String8 obfuscationPath = getObfuscationName(resPath, obfuscationName);

            table->addEntry(SourcePos(it.getPath(), 0), String16(assets->getPackage()),
                            type16,
                            baseName, // String16(obfuscationName),
                            String16(obfuscationPath), // resPath
                            NULL,
                            &it.getParams());
            assets->addResource(it.getLeafName(), obfuscationPath/*resPath*/, it.getFile(), type8);
        }
        return hasErrors ? UNKNOWN_ERROR : NO_ERROR;
    }

  混淆C專案程式碼:obfuscator-LLVM;使用O-LLVM和NDK對Android應用進行混淆。android的native程式碼,也就是so和linux的c/c++程式碼均可使用的混淆工具ollvm的編譯,混淆,反混淆,和反反混淆。相對於Dalvik虛擬機器層次的混淆而言,原生語言(C/C++)元件的程式碼混淆選擇並不多,Obfuscator-LLVM工程是一個值得關注的例外。

(1)LLVM是lowlevel virtual machine的簡稱,是一個編譯器框架。蘋果公司的Xcode 4.0之後用的都是LLVM編譯器。  LLVM是lowlevel virtual machine的簡稱,是一個編譯器框架。蘋果公司的Xcode 4.0之後用的都是LLVM編譯器。目的是為了增加逆向工程的難度。Obfuscator-llvm最新版本集成了LLVM-3.4編譯器,可移植性很高,並且相容LLVM支援的所有語言(C,C++, Objective-C, Ada and Fortran)和平臺(x86, x86-64, PowerPC, PowerPC-64,ARM, Thumb, SPARC, Alpha, CellSPU, MIPS, MSP430, SystemZ,and XCore)。

Android應用obfuscator進行混淆- https://github.com/obfuscator-llvm/obfuscator/
引數相關的文件可以看 github 上的wiki: https://github.com/obfuscator-llvm/obfuscator/wiki
混淆器 obfuscator-llvm- https://github.com/obfuscator-llvm/obfuscator/tree/llvm-3.6.1
利用OLLVM混淆Android Native程式碼篇一 https://geneblue.github.io/2016/10/09/%E5%88%A9%E7%94%A8OLLVM%E6%B7%B7%E6%B7%86Android%20Native%E4%BB%A3%E7%A0%81%E7%AF%87%E4%B8%80/
使用O-LLVM和NDK對Android應用進行混淆- http://android.jobbole.com/81230/
使用obfuscator llvm 和andorid NDK(Cmake)進行程式碼混淆- https://blog.csdn.net/huang_yx005/article/details/76636219
Obfuscator-LLVM 可以混淆C程式碼- https://github.com/obfuscator-llvm/obfuscator/tree/llvm-4.0
使用O-LLVM和NDK對Android應用進行混淆- http://blog.csdn.net/asce1885/article/details/47175621
OLLVM(Obfuscator-LLVM)- https://github.com/Fuzion24/AndroidObfuscation-NDK 
OLLVM(Obfuscator-LLVM)- https://github.com/obfuscator-llvm/obfuscator 
Android程式碼混淆技術總結(一)- http://bobao.360.cn/learning/detail/3704.html