Skip to content

Commit 2b2993a

Browse files
committed
feat(llvm): LLVM 18.0.3 support obfuscation
1 parent f1d6c8f commit 2b2993a

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

llvm-project/llvm/lib/Passes/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ add_llvm_component_library(LLVMPasses
66
PassPlugin.cpp
77
StandardInstrumentations.cpp
88

9+
Obfuscation/Utils.cpp
10+
Obfuscation/CryptoUtils.cpp
11+
Obfuscation/ObfuscationOptions.cpp
12+
Obfuscation/BogusControlFlow.cpp
13+
Obfuscation/IPObfuscationContext.cpp
14+
Obfuscation/Flattening.cpp
15+
Obfuscation/StringEncryption.cpp
16+
Obfuscation/SplitBasicBlock.cpp
17+
Obfuscation/Substitution.cpp
18+
Obfuscation/IndirectBranch.cpp
19+
Obfuscation/IndirectCall.cpp
20+
Obfuscation/IndirectGlobalVariable.cpp
21+
922
ADDITIONAL_HEADER_DIRS
1023
${LLVM_MAIN_INCLUDE_DIR}/llvm
1124
${LLVM_MAIN_INCLUDE_DIR}/llvm/Passes

llvm-project/llvm/lib/Passes/PassBuilder.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,16 @@
282282
#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
283283
#include "llvm/Transforms/Vectorize/VectorCombine.h"
284284
#include <optional>
285+
// 引用 Obfuscation 相关文件
286+
#include "Obfuscation/BogusControlFlow.h" // 虚假控制流
287+
#include "Obfuscation/Flattening.h" // 控制流平坦化
288+
#include "Obfuscation/SplitBasicBlock.h" // 基本块分割
289+
#include "Obfuscation/Substitution.h" // 指令替换
290+
#include "Obfuscation/StringEncryption.h" // 字符串加密
291+
#include "Obfuscation/IndirectGlobalVariable.h" // 间接全局变量
292+
#include "Obfuscation/IndirectBranch.h" // 间接跳转
293+
#include "Obfuscation/IndirectCall.h" // 间接调用
294+
#include "Obfuscation/Utils.h" // 为了控制函数名混淆开关 (bool obf_function_name_cmd;)
285295

286296
using namespace llvm;
287297

@@ -448,6 +458,17 @@ class TriggerVerifierErrorPass
448458

449459
} // namespace
450460

461+
// 添加命令行支持
462+
static cl::opt<bool> s_obf_split("split", cl::init(false), cl::desc("SplitBasicBlock: split_num=3(init)"));
463+
static cl::opt<bool> s_obf_sobf("sobf", cl::init(false), cl::desc("String Obfuscation"));
464+
static cl::opt<bool> s_obf_fla("fla", cl::init(false), cl::desc("Flattening"));
465+
static cl::opt<bool> s_obf_sub("sub", cl::init(false), cl::desc("Substitution: sub_loop"));
466+
static cl::opt<bool> s_obf_bcf("bcf", cl::init(false), cl::desc("BogusControlFlow: application number -bcf_loop=x must be x > 0"));
467+
static cl::opt<bool> s_obf_ibr("ibr", cl::init(false), cl::desc("Indirect Branch"));
468+
static cl::opt<bool> s_obf_igv("igv", cl::init(false), cl::desc("Indirect Global Variable"));
469+
static cl::opt<bool> s_obf_icall("icall", cl::init(false), cl::desc("Indirect Call"));
470+
static cl::opt<bool> s_obf_fn_name_cmd("fncmd", cl::init(false), cl::desc("use function name control obfuscation(_ + command + _ | example: function_fla_bcf_)"));
471+
451472
PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO,
452473
std::optional<PGOOptions> PGOOpt,
453474
PassInstrumentationCallbacks *PIC)
@@ -483,6 +504,30 @@ PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO,
483504
PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
484505
#include "PassRegistry.def"
485506
}
507+
508+
//outs() << "[obf] registerPipelineStartEPCallback\n"; // 优化前
509+
//outs() << "[obf] registerOptimizerLastEPCallback\n"; // 优化后
510+
this->registerOptimizerLastEPCallback(
511+
[](llvm::ModulePassManager &MPM,
512+
llvm::OptimizationLevel Level) {
513+
outs() << "[obf] run.registerOptimizerLastEPCallback\n";
514+
obf_function_name_cmd = s_obf_fn_name_cmd;
515+
if (obf_function_name_cmd) {
516+
outs() << "[obf] enable function name control obfuscation(_ + command + _ | example: function_fla_)\n";
517+
}
518+
MPM.addPass(StringEncryptionPass(s_obf_sobf)); // 先进行字符串加密 出现字符串加密基本块以后再进行基本块分割和其他混淆 加大解密难度
519+
llvm::FunctionPassManager FPM;
520+
FPM.addPass(IndirectCallPass(s_obf_icall)); // 间接调用
521+
FPM.addPass(SplitBasicBlockPass(s_obf_split)); // 优先进行基本块分割
522+
FPM.addPass(FlatteningPass(s_obf_fla)); // 对于控制流平坦化
523+
FPM.addPass(SubstitutionPass(s_obf_sub)); // 指令替换
524+
FPM.addPass(BogusControlFlowPass(s_obf_bcf)); // 虚假控制流
525+
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
526+
MPM.addPass(IndirectBranchPass(s_obf_ibr)); // 间接指令 理论上间接指令应该放在最后
527+
MPM.addPass(IndirectGlobalVariablePass(s_obf_igv)); // 间接全局变量
528+
MPM.addPass(RewriteSymbolPass()); // 根据yaml信息 重命名特定symbols
529+
}
530+
);
486531
}
487532

488533
void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {

0 commit comments

Comments
 (0)