windows上编译ollvm的踩坑记录
源码部分
网上的教程还是很多的 ,这里记录一下自己的编译之路
llvm源码
用到的ollvm源码目前感觉还不错的主要是以下三个
- https://github.com/heroims/obfuscator
- https://github.com/GoSSIP-SJTU/Armariris
- https://github.com/amimo/goron
- https://github.com/0x3f97/ollvm-12.x
- https://github.com/bluesadi/Pluto-Obfuscator
这里我用到的是第一个 heroims-ollvm
NDK版本:21.1.6352462
CMake版本:3.10.2.4988404
heroims-ollvm:https://github.com/heroims/obfuscator/tree/llvm-9.0.1
首先还是下载源码
git clone -b llvm-9.0.1 https://github.com/heroims/obfuscator.git
再或者直接 download 都行
编译源码
windows下的编译就以下两条路
使用CMake生成 Visual Studio工程 或是 MinGW Makefiles
使用mingw进行编译 需要使用版本8.1,若使用6.x 必报错
建议 Visual Studio 进行编译 https://github.com/heroims 12+以上的ollvm
12+ 以上的源码 独立拆开了 clang
参见:https://llvm.org/docs/GettingStarted.html
在12 以前 clang 作为 tools 放在 llvm 中
在12 以后是独立作为 plugin
// 生成 Visual Studio 工程
cmake -G "Visual Studio 16 2019" -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE=Release ../llvm
cmake -DLLVM_ENABLE_PROJECTS="clang;libcxx" -G "Visual Studio 16 2019" -A x64 -Thost=x64 -DCMAKE_BUILD_TYPE=Release ..\llvm
// 生成 Ninja 工程 (速度是真的比其他两个快很多)
cmake -G "Ninja" -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" -DBUILD_SHARED_LIBS=OFF ../llvm
// 生成 MinGW Makefiles
cmake -G "MinGW Makefiles" -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE=Release -DLLVM_INCLUDE_TESTS=OFF ../llvm
// mingw编译
cmake --build .
编译完成后,从 obfuscator-llvm-9.0.1\build\lib\clang\9.0.1\include 复制
stdarg.h
stddef.h
__stddef_max_align_t.h
float.h
到 21.1.6352462\toolchains\llvm\prebuilt\windows-x86_64\sysroot\usr\include 即可
对应替换bin目录下的对应的 三个 exe
clang.exe
clang++.exe
clang-format.exe
注意事项
- 编译不成功 : versionhelpers.h : not such file or directory
cmake加个参数:-DLLVM_INCLUDE_BENCHMARKS=OFF 或者修改cmakelists.txt中的LLVM_INCLUDE_BENCHMARKS ON 改成OFF - 编译90%退出
... z3/bin/libz3.lib: error adding symbols: File format not recognized
collect2.exe: error: ld returned 1 exit status
安装一个z3 ,和你平台对应,32位还是64位
OLLVM 的使用
-
Instructions Substitution (指令替换)
- -mllvm -sub: 启用instructions substitution
- -mllvm -sub_loop=3: 对每个函数混淆3次,默认1词
-
Control Flow Flattening (控制流平坦化)
- -mllvm -fla: 启用control flow flattening
- -mllvm -split: 启用block切分,提升平展程度
- -mllvm -split_num=3: 对每个block混淆3次,默认1词
-
Bogus Control Flow (虚假控制流)
- -mllvm -bcf: 启用 bogus control flow
- -mllvm -bcf_loop=3: 对一个函数混淆3次,默认1次
- -mllvm -bcf_prob=40: 代码块被混淆的概率是40%,默认30%
-
-mllvm -sobf: 字符串加密
-
函数注解的使用
//Functions annotations
int foo() __attribute((annotate(("sub"))));
int foo() {
return 2;
}
CMakeLists.txt
#debug and release all enable
set(CMAKE_CXX_FLAGS "-mllvm -fla -mllvm -sub -mllvm -sobf ")
#so体积优化
#SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
#SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -Os -Wall -s")
#设置llvm debug模式混淆编译
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mllvm -fla")
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -mllvm -fla")
#设置llvm release模式混淆编译
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -mllvm -fla -mllvm -sub -mllvm -bcf")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -mllvm -fla -mllvm -sub -mllvm -bcf")
#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s -O3 -Wall -fvisibility=hidden -mllvm -fla")
参考
https://sourceforge.net/projects/mingw-w64/files/mingw-w64/
ps:若在编译的成品 clang 运行出现了一下情况,请使用 vs studio 重新编译(不要使用mingw32-make)
NDK版本对应的clang版本
版本:21.1.6352462
Android (6317467 based on r365631c1) clang version 9.0.8 (https://android.googlesource.com/toolchain/llvm-project e0caee08e5f09b374a27a676d04978c81fcb1928) (based on LLVM 9.0.8svn)
Target: x86_64-w64-windows-gnu
Thread model: posix
------------------------------------------------------------------------------------
版本:21.2.6472646
Android (6454773 based on r365631c2) clang version 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 98c855489587874b2a325e7a516b99d838599c6f) (based on LLVM 9.0.8svn)
Target: x86_64-w64-windows-gnu
Thread model: posix
------------------------------------------------------------------------------------
版本:21.3.6528147
Android (6454773 based on r365631c2) clang version 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 98c855489587874b2a325e7a516b99d838599c6f) (based on LLVM 9.0.8svn)
Target: x86_64-w64-windows-gnu
Thread model: posix
------------------------------------------------------------------------------------
版本:21.4.7075529
Android (7019983 based on r365631c3) clang version 9.0.9 (https://android.googlesource.com/toolchain/llvm-project a2a1e703c0edb03ba29944e529ccbf457742737b) (based on LLVM 9.0.9svn)
Target: x86_64-w64-windows-gnu
Thread model: posix
------------------------------------------------------------------------------------
版本:22.0.7026061
Android (6875598, based on r399163b) clang version 11.0.5 (https://android.googlesource.com/toolchain/llvm-project 87f1315dfbea7c137aa2e6d362dbb457e388158d)
Target: x86_64-w64-windows-gnu
Thread model: posix
------------------------------------------------------------------------------------
版本:22.1.7171670
Android (7155654, based on r399163b1) clang version 11.0.5 (https://android.googlesource.com/toolchain/llvm-project 87f1315dfbea7c137aa2e6d362dbb457e388158d)
Target: x86_64-w64-windows-gnu
Thread model: posix
------------------------------------------------------------------------------------
版本:23.0.7599858
Android (7284624, based on r416183b) clang version 12.0.5 (https://android.googlesource.com/toolchain/llvm-project c935d99d7cf2016289302412d708641d52d2f7ee)
Target: x86_64-w64-windows-gnu
Thread model: posix
------------------------------------------------------------------------------------
版本:23.1.7779620
Android (7714059, based on r416183c1) clang version 12.0.8 (https://android.googlesource.com/toolchain/llvm-project c935d99d7cf2016289302412d708641d52d2f7ee)
Target: x86_64-w64-windows-gnu
Thread model: posix
------------------------------------------------------------------------------------
版本:24.0.8215888
Android (8075178, based on r437112b) clang version 14.0.1 (https://android.googlesource.com/toolchain/llvm-project 8671348b81b95fc603505dfc881b45103bee1731)
Target: x86_64-w64-windows-gnu
Thread model: posix