OLLVM4.0+NDK編譯環境搭建

來源:互聯網
上載者:User
0x00 項目簡介

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).

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.
源碼地址:https://github.com/obfuscator-llvm/obfuscator/ 0x01 編譯環境 Ubuntu 16.04 32位 obfuscator-llvm4.0 android-ndk-r10b-linux-x86.tar.bz2 0x02 安裝ndk

將ndk解壓到/opt/android/ndk/解壓後的目錄/opt/android/ndk/android-ndk-r10b$ sudo gedit /etc/profile#在檔案尾添加以下內容設定NDK環境變數export NDK_HOME=/opt/android/ndk/android-ndk-r10bexport PATH=$NDK_HOME:$PATH$ source /etc/profile   使之生效如果環境變數未生效可以嘗試將內容添加在“~/.bashrc”檔案內$sudo gedit ~/.bashrc

配置好環境變數之後,需要驗證一下是否搭建成功,在命令列下輸入ndk-build,有如下提示則表示搭建成功了。
0x03 安裝編譯環境並編譯OLLVM

安裝編譯工具apt-get install cmakeapt-get install g++編譯ollvm源碼git clone -b obfuscator-llvm-4.0 https://github.com/obfuscator-llvm/obfuscator.gitmkdir buildcd buildcmake -DCMAKE_BUILD_TYPE:String=Release ../make -j7在build目錄下會產生編譯後的程式,我們只會用到bin和lib目錄下的檔案
0x04 添加字串混淆功能

字串混淆功能添加方法參考此處 0x05 整合工具鏈到NDK中

cd /opt/android/ndk/android-ndk-r10b/toolchains/mkdir obfuscator-llvm-4.0

將toolchains目錄下的llvm-3.3目錄中的下列目錄和檔案拷貝到obfuscator-llvm-4.0中

1.prebuilt目錄和檔案 2.config.mk3.setup.mk4.setup-common.mk

將obfuscator-llvm-4.0/prebuilt/linux-x86下的bin和lib替換為我們編譯好的bin和lib
然後將下面檔案複製一份,改名稱如下

arm-linux-androideabi-clang3.4->arm-linux-androideabi-obfuscator4.0mipsel-linux-android-clang3.4-> mipsel-linux-android-obfuscator4.0x86-clang3.4-> x86-obfuscator4.0

分別將以上三個拷貝後的檔案夾中的的 setup.mk 中的 LLVM_NAME 的值更改為

    LLVM_NAME := obfuscator-llvm-$(LLVM_VERSION)
0x06 使用ollvm產生混淆過的程式

0.混淆參數詳解

-mllvm -fla 開啟控制流程平台化-mllvm -sub 開啟指令替換-mllvm -bcf 開啟虛假控制流程-mllvm -sobf 開啟字串混淆-mllvm -seed=0xdeadbeaf 指定隨機數種子產生器bcf可以配合下面參數使用-mllvm -bcf_loop=3 設定函數混淆次數為3次 不加此選項預設為1次-mllvm -bcf_prob=40 設定代碼塊被混淆的機率是40%,預設30%[瞭解更多](https://github.com/obfuscator-llvm/obfuscator/wiki) 

1.建立工程目錄 此處為hello
2.建立子目錄jni
3.在jni目錄下建立Android.mk並根據需求編寫mk檔案

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := helloLOCAL_SRC_FILES := hello.cLOCAL_CFLAGS += -mllvm -sub -mllvm -bcf -mllvm -bcf_loop=3  -mllvm -bcf_prob=40 -mllvm -fla -mllvm -split_num=10 -mllvm -sobfLOCAL_ARM_MODE := arminclude $(BUILD_EXECUTABLE)

4.在jni目錄下建立Application.mk並根據需求編寫mk檔案

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)APP_ABI := armeabiNDK_TOOLCHAIN_VERSION := obfuscator4.0include $(BUILD_EXECUTABLE)

5.在jni目錄下建立代碼檔案hello.c

#include <stdio.h>int main(int argc, char** argv){    int a=1;    int b=0;    if(a>b)    {        printf("snow:%d\n",a);    }    else{        printf("test:%d\n",b);    }    return 0;       }

6.在工程目錄下執行ndk-build命令

7.未混淆的效果

8.混淆後的效果

字串混淆效果

0x07 問題及解決方案 bcf不支援Invoke指令
在實際使用過程中,發現ollvm目前不支援@synchronized、try…catch等少數文法,然後導致bcf報錯。這些文法會產生invoke指令,目前可以在bcf前過濾包含InvokeInst的方法,具體代碼可以參考該Github地址。 0x08 參考文檔

http://fighting300.com/2017/09/18/ollvm-with-StringObfuscate/
https://github.com/fighting300/obfuscator/commit/ae0e5acd873cd9a8c839a013a635422022fd0d6b
https://github.com/GoSSIP-SJTU/Armariris
https://github.com/obfuscator-llvm/obfuscator/wiki
http://blog.csdn.net/wangbaochu/article/details/45370543

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.