2、LLVM 函数名称加密 及3种PASS的实现

sudo usermod -a -G vboxsf nowind   nowind是你的虚拟机登录的用户名解决virtualbox 虚拟机共享文件夹不能使用的问题

第一种:源码内实现pass:

实现EncodeFunctionName 的pass,核心代码如下

相关文件的修改:

因为后面同样用到ollvm,所以我已经放到一起了,资源下载:

https://download.csdn.net/download/ahjxly/88609122

因为全部都是修改llvm目录下的文件,替换include和lib文件夹

替换完成后 cd cd llvm/llvm-project-9.0.1/llvm/cmake-build-debug/,执行ninja -j8,重新编译下。这次编译会快很多。

然后单独执行 ninja LLVMEncodeFunctionName,生成so文件,进行使用。

使用方式和之前的文章一样,执行

opt -load '/home/nowind/llvm/llvm-project-9.0.1/llvm/cmake-build-release/lib/LLVMEncodeFunctionName.so' -encode hello_clang.ll -o hello_clang_encode.bc

clang hello_clang_encode.bc -o hello_clang_encode

第二种:在源码外实现EncodeFunctionName2,修改根目录下的CMakeList.txt:

set(LLVM_DIR /home/nowind/llvm/llvm-project-9.0.1/llvm/cmake-build-debug/lib/cmake/llvm/),这里为你自己的路径

执行结果如下:

/home/nowind/llvm/CLion-2023.3/clion-2023.3/bin/cmake/linux/x64/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=/usr/bin/ninja -G Ninja -S /home/nowind/llvm/pro/pro2/outPass -B /home/nowind/llvm/pro/pro2/outPass/cmake-build-debug
-- Linker detection: GNU ld
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/nowind/llvm/pro/pro2/outPass/cmake-build-debug

[Finished]
/home/nowind/llvm/CLion-2023.3/clion-2023.3/bin/cmake/linux/x64/bin/cmake --build /home/nowind/llvm/pro/pro2/outPass/cmake-build-debug --target all -j 6
[2/2] Linking CXX shared module EncodeFunctionName2/LLVMEncodeFunctionName2.so

Build finished

生成的文件在

/home/nowind/llvm/pro/pro2/outPass/cmake-build-debug/EncodeFunctionName2/LLVMEncodeFunctionName2.so

使用opt 命令

opt -load /home/nowind/llvm/pro/pro2/outPass/cmake-build-debug/EncodeFunctionName2/LLVMEncodeFunctionName2.so  -encode2 hello_clang.ll
WARNING: You're attempting to print out a bitcode file.
This is inadvisable as it may cause display problems. If
you REALLY want to taste LLVM bitcode first-hand, you
can force output with the `-f' option.

EncodeFunctionName22: test_hello1 -> 9c119247aefaa12cdd417eb3d57d5b2a
EncodeFunctionName22: main -> main
EncodeFunctionName22: test_hello2 -> af0aaac8b98b759ace7b9eacbd2238a6

得到同样的结果。

clang使用自定义的pass,生成可执行文件

 clang -Xclang -load -Xclang /home/nowind/llvm/pro/pro2/outPass/cmake-build-release/EncodeFunctionName2/LLVMEncodeFunctionName2.so -encode2 hello_clang.ll -o hello_clang_encode3 -Wno-unused-command-line-argument

第三种:EncodeFunctionName注册到clang中:

/home/nowind/llvm/llvm-project-9.0.1/llvm/lib/Transforms/IPO/PassManagerBuilder.cpp populateModulePassManager方法中增加 MPM.add(createEncodeFunctionName());

重新编译clang: 这里用的release,因此PATH要改:

ninja clang

export PATH=/home/nowind/llvm/llvm-project-9.0.1/llvm/cmake-build-release/bin:$PATH

clang -mllvm -encode_function_name hello_clang.bc

切换到debug:

修改EncodeFunction中:CMakeLists.txt set(LLVM_DIR /home/nowind/llvm/llvm-project-9.0.1/llvm/cmake-build-debug/lib/cmake/llvm/)

cd cmake-build-debug

ninja opt 重新编译opt

opt -load /home/nowind/llvm/pro/pro2/outPass/cmake-build-debug/EncodeFunctionName2/LLVMEncodeFunctionName2.so -encode2 hello_clang.ll -o hello_clang_encode.bc

注意pass不能重名

opt 断点:

OutPass中配置EncodeFunctionName2的opt为

/home/nowind/llvm/llvm-project-9.0.1/llvm/cmake-build-debug/bin/opt

参数配置为:

-load /home/nowind/llvm/pro/pro2/outPass/cmake-build-debug/EncodeFunctionName2/LLVMEncodeFunctionName2.so -encode2 /home/nowind/llvm/pro/pro2/hello_clang.ll -o /home/nowind/llvm/pro/pro2/hello_clang_encode.bc

但这样配置完毕后发现一个问题,就是日志在clion控制台中看不到,只有换行,勾选了

Emulate terminal in the output console才能看到,奇怪的很,但也不影响什么

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
污点分析是一静态程序分析技术,用于确定程序中哪些变量可以被恶意输入或其他安全漏洞所利用。LLVM是一个广泛使用的编译器基础设施,可以用于实现污点分析。下面是一个简单的LLVM Pass,它实现了简单的污点分析。 首先,我们需要定义一个Pass类,该类继承自llvm::FunctionPass。然后,我们需要在runOnFunction函数实现我们的污点分析逻辑。在这个例子中,我们将通过检查函数的参数和指令来确定哪些变量是受污染的。 ```c++ #include "llvm/IR/Function.h" #include "llvm/Pass.h" #include "llvm/Support/raw_ostream.h" #include "llvm/IR/Instructions.h" using namespace llvm; namespace { struct TaintAnalysis : public FunctionPass { static char ID; TaintAnalysis() : FunctionPass(ID) {} bool runOnFunction(Function &F) override { // 遍历函数的所有基本块 for (auto &BB : F) { // 遍历基本块的所有指令 for (auto &I : BB) { // 如果指令是一个存储指令 if (auto *SI = dyn_cast<StoreInst>(&I)) { // 如果存储指令的源操作数是一个指针类型 if (auto *Ptr = dyn_cast<PointerType>(SI->getOperand(1)->getType())) { // 如果指针指向的类型是整数类型 if (auto *IntTy = dyn_cast<IntegerType>(Ptr->getElementType())) { // 如果整数类型的位宽为8 if (IntTy->getBitWidth() == 8) { // 输出受污染的指针值和存储的值 errs() << "Tainted pointer value: " << *SI->getOperand(1) << "\n"; errs() << "Tainted value: " << *SI->getOperand(0) << "\n"; } } } } } } return false; } }; } char TaintAnalysis::ID = 0; static RegisterPass<TaintAnalysis> X("taint-analysis", "Taint Analysis Pass"); ``` 我们在runOnFunction函数中遍历函数的所有基本块和指令。我们检查每个存储指令,以确定它是否存储了一个指向整数类型的指针,并且该整数类型具有8位的位宽。如果是的话,我们输出受污染的指针值和存储的值。 最后,我们将该Pass注册到LLVM中,以便在编译时运行。我们使用static RegisterPass来注册我们的Pass,并将其命名为“taint-analysis”。 现在,我们可以使用LLVM编译器运行我们的Pass,以便对C或C++程序进行污点分析。例如,假设我们有以下C程序: ```c++ #include <stdio.h> void foo(int *ptr) { int x = *ptr; printf("The value of x is: %d\n", x); } int main() { int y = 42; foo(&y); return 0; } ``` 我们可以使用以下命令编译程序并运行我们的Pass: ``` clang -Xclang -load -Xclang MyPass.so -c test.c ``` 这将生成一个名为“test.o”的目标文件,并使用我们的Pass进行污点分析。如果程序中存在受污染的指针,我们的Pass将输出它们的值。在这个例子中,我们应该得到以下输出: ``` Tainted pointer value: i32* %ptr Tainted value: i32 42 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值