OLLVM + NDK 混淆編譯環境搭建

代碼混淆能起到很好的反逆向分析,相似於 java 的 proguard 混淆和 dex 文件的 dexguard 混淆工具,c/c++ 也有對應的 ollvm 混淆組件。java

0x00 OLLVM 簡介

Obfuscator-LLVM is a project initiated in June 2010 by the information security group of the University of Applied Sciences and Arts Western Switzerland of Yverdon-les-Bains (HEIG-VD).linux

The aim of this project is to provide an open-source fork of the LLVM compilation suite able to provide increased software security through code obfuscation and tamper-proofing. As we currently mostly work at the Intermediate Representation (IR) level, our tool is compatible with all programming languages (C, C++, Objective-C, Ada and Fortran) and target platforms (x86, x86-64, PowerPC, PowerPC-64, ARM, Thumb, SPARC, Alpha, CellSPU, MIPS, MSP430, SystemZ, and XCore) currently supported by LLVM.android

源碼地址:https://github.com/obfuscator-llvm/obfuscator/c++

0x01 環境信息

系統環境:MacOS
系統版本:10.12.5
NDK版本:android-ndk-r14bgit

0x02 下載編譯

github 上最新版本是4.0,能夠直接 clone github 項目編譯,可參考提供的 wiki 文檔:Installationgithub

$ git clone -b llvm-4.0 https://github.com/obfuscator-llvm/obfuscator.git
$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=Release ../obfuscator/
$ make -j7

這裏編譯的時候須要 cmake 工具,能夠經過 Homebrew 直接安裝。編譯成功後,生成的文件在 build/bin 下bash

0x03 與 NDK 整合

新建編譯鏈

在 android-ndk-r14b/toolchains 下新建目錄 ollvm-4.0/prebuilt/darwin-x86_64,把前一步編譯生成的結果拷貝到此目錄下(主要是bin和lib)。架構

<font color=red>這裏最好直接複製 llvm 目錄,而後進行修改。手動建立的時候遇到過編譯失敗,直接複製修改就沒這種問題。</font>ide

[armeabi-v7a] Compile++ thumb: native &lt;= jni_export.cpp
make: execvp: /Users/xiangqing/Documents/code/ollvm/build/bin: Permission denied
make: *** [/Users/xiangqing/Documents/project/droid-ndk/example/src/main/obj/local/armeabi-v7a/objs/native/jni_export.o] Error 127

配置編譯鏈

在 android-ndk-r14b/build/core/toolchains 目錄下,新建目錄 arm-linux-androideabi-clang-ollvm4.0,拷貝目錄 arm-linux-androideabi-clang 下的文件 config.mk 與 setup.mk 到 arm-linux-androideabi-clang-ollvm4.0 中,修改setup.mk文件。工具

#
# Override the toolchain prefix
#

############################ 原始配置 ############################
#LLVM_TOOLCHAIN_PREBUILT_ROOT := $(call get-toolchain-root,llvm)
#LLVM_TOOLCHAIN_PREFIX := $(LLVM_TOOLCHAIN_PREBUILT_ROOT)/bin/
#################################################################

############################ 修改後 #############################
OLLVM_NAME := ollvm-4.0
LLVM_TOOLCHAIN_PREBUILT_ROOT := $(call get-toolchain-root,$(OLLVM_NAME))
LLVM_TOOLCHAIN_PREFIX := $(LLVM_TOOLCHAIN_PREBUILT_ROOT)/bin/

#其餘配置不作修改
......

config.mk 保存的是該編譯鏈對應的 CPU 架構,因此上面修改完只能編譯 armeabi 和 armeabi-v7a 架構的 so。若是須要編譯其餘架構須要作相應的修改。文件夾名也必須按照嚴格的格式,如 mips 的須要添加文件夾:mipsel-linux-android-clang-ollvm4.0,修改相應的 setup.mk 文件。

0x04 OLLVM 使用

使用 ollvm 進行 ndk 的編譯須要對 Application.mk 和 Android.mk 文件作相應的修改。

Android.mk 中添加混淆編譯參數:

LOCAL_CFLAGS += -mllvm -sub -mllvm -bcf -mllvm -fla

參數相關的文檔能夠看 github 上的wiki: https://github.com/obfuscator-llvm/obfuscator/wiki

Application.mk 中配置 NDK_TOOLCHAIN_VERSION

APP_ABI := x86 armeabi-v7a x86_64 arm64-v8a mips armeabi mips64
NDK_TOOLCHAIN_VERSION := clang-ollvm4.0

參考文章:

原文地址:OLLVM + NDK 混淆編譯環境搭建

相關文章
相關標籤/搜索