前言:
純粹為了學(xué)習(xí),所以引用了很多人的資料诽里,不是為了指導(dǎo)他人,只是記錄這段時(shí)間的學(xué)習(xí)成果
一: 開發(fā)pass 前的準(zhǔn)備
1: 獲取LLVM源碼
git clone [http://llvm.org/git/llvm.git](http://llvm.org/git/llvm.git)
git clone [http://llvm.org/git/clang.git](http://llvm.org/git/clang.git) llvm/tools/clang
git clone [http://llvm.org/git/clang-tools-extra.git](http://llvm.org/git/clang-tools-extra.git) llvm/tools/clang/tools/extra
git clone [http://llvm.org/git/compiler-rt.git](http://llvm.org/git/compiler-rt.git) llvm/projects/compiler-rt
或者也可以獲取Ollvm 的源碼
https://github.com/obfuscator-llvm/obfuscator
其他的還有交大維護(hù)的孤挺花(Armariris)項(xiàng)目
clone git@github.com:gossip-sjtu/Armariris.git
最好的選擇是張總的Hikari 項(xiàng)目
https://github.com/HikariObfuscator/Hikari/releases
2:編譯成xcode項(xiàng)目
mkdir build
cd build
// 使用編譯器cmake 創(chuàng)建xcode 項(xiàng)目赏殃,項(xiàng)目地址在llvm目錄下
cmake -G Xcode CMAKE_BUILD_TYPE="Debug" ../llvm
open LLVM.xcodeproj
其中ollvm的編譯流程
mkdir obf
cd obf
clone [git@github.com](mailto:git@github.com):gossip-sjtu/Armariris.git
cmake -DCMAKE_BUILD_TYPE:String=Release ./Armariris
make -j4
3: pass 中結(jié)構(gòu)劃分
在LLVM中
Module: 程序的基本單位是模塊(Module),比如一個(gè).c或.cpp文件
Function: 函數(shù)是模塊的基本組成單位华嘹,代表文件中的一個(gè)函數(shù)奖地,所以一個(gè)Module由一個(gè)或多個(gè)函數(shù)組成橄唬。
BasicBlock: 函數(shù)的基本組成單位 (每個(gè)函數(shù)會(huì)被劃分為一些block,它的劃分標(biāo)準(zhǔn)是:
一個(gè)block只有一個(gè)入口和一個(gè)出口所以一個(gè)Function由一個(gè)或多個(gè)Basic Block組成)
Instructions: 指令是Basic Block的基本組成單位参歹,所以一個(gè)Basic Block由一個(gè)或多個(gè)Instructions組成
二: 開發(fā)流程
1 我們需要繼承指定的pass
比如:
struct functionObfuscation : public FunctionPass
2 實(shí)現(xiàn)其中的方法
virtual bool runOnFunction(Function &F)
3 需要將我們自己寫的pass 注冊(cè)
static RegisterPass<functionObfuscation> X(“funcobf", "Hello World Pass");
4 編譯我們自己的pass稱為動(dòng)態(tài)庫(kù)
opt -load path/to/LLVMPassDemo.dylib -funcobf -time-passes -disable-output test.bc
其中動(dòng)態(tài)庫(kù)就是存放編譯后我們寫的pass, funcobf選項(xiàng)就是指運(yùn)行functionObfuscation這個(gè)pass