Skip to content

Commit 25beaeb

Browse files
committed
feat(llvm): LLVM 17.0.2 support obfuscation
1 parent 0a92537 commit 25beaeb

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
@@ -256,6 +256,16 @@
256256
#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
257257
#include "llvm/Transforms/Vectorize/VectorCombine.h"
258258
#include <optional>
259+
// 引用 Obfuscation 相关文件
260+
#include "Obfuscation/BogusControlFlow.h" // 虚假控制流
261+
#include "Obfuscation/Flattening.h" // 控制流平坦化
262+
#include "Obfuscation/SplitBasicBlock.h" // 基本块分割
263+
#include "Obfuscation/Substitution.h" // 指令替换
264+
#include "Obfuscation/StringEncryption.h" // 字符串加密
265+
#include "Obfuscation/IndirectGlobalVariable.h" // 间接全局变量
266+
#include "Obfuscation/IndirectBranch.h" // 间接跳转
267+
#include "Obfuscation/IndirectCall.h" // 间接调用
268+
#include "Obfuscation/Utils.h" // 为了控制函数名混淆开关 (bool obf_function_name_cmd;)
259269

260270
using namespace llvm;
261271

@@ -396,6 +406,17 @@ class TriggerCrashPass : public PassInfoMixin<TriggerCrashPass> {
396406

397407
} // namespace
398408

409+
// 添加命令行支持
410+
static cl::opt<bool> s_obf_split("split", cl::init(false), cl::desc("SplitBasicBlock: split_num=3(init)"));
411+
static cl::opt<bool> s_obf_sobf("sobf", cl::init(false), cl::desc("String Obfuscation"));
412+
static cl::opt<bool> s_obf_fla("fla", cl::init(false), cl::desc("Flattening"));
413+
static cl::opt<bool> s_obf_sub("sub", cl::init(false), cl::desc("Substitution: sub_loop"));
414+
static cl::opt<bool> s_obf_bcf("bcf", cl::init(false), cl::desc("BogusControlFlow: application number -bcf_loop=x must be x > 0"));
415+
static cl::opt<bool> s_obf_ibr("ibr", cl::init(false), cl::desc("Indirect Branch"));
416+
static cl::opt<bool> s_obf_igv("igv", cl::init(false), cl::desc("Indirect Global Variable"));
417+
static cl::opt<bool> s_obf_icall("icall", cl::init(false), cl::desc("Indirect Call"));
418+
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_)"));
419+
399420
PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO,
400421
std::optional<PGOOptions> PGOOpt,
401422
PassInstrumentationCallbacks *PIC)
@@ -431,6 +452,30 @@ PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO,
431452
PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
432453
#include "PassRegistry.def"
433454
}
455+
456+
//outs() << "[obf] registerPipelineStartEPCallback\n"; // 优化前
457+
//outs() << "[obf] registerOptimizerLastEPCallback\n"; // 优化后
458+
this->registerOptimizerLastEPCallback(
459+
[](llvm::ModulePassManager &MPM,
460+
llvm::OptimizationLevel Level) {
461+
outs() << "[obf] run.registerOptimizerLastEPCallback\n";
462+
obf_function_name_cmd = s_obf_fn_name_cmd;
463+
if (obf_function_name_cmd) {
464+
outs() << "[obf] enable function name control obfuscation(_ + command + _ | example: function_fla_)\n";
465+
}
466+
MPM.addPass(StringEncryptionPass(s_obf_sobf)); // 先进行字符串加密 出现字符串加密基本块以后再进行基本块分割和其他混淆 加大解密难度
467+
llvm::FunctionPassManager FPM;
468+
FPM.addPass(IndirectCallPass(s_obf_icall)); // 间接调用
469+
FPM.addPass(SplitBasicBlockPass(s_obf_split)); // 优先进行基本块分割
470+
FPM.addPass(FlatteningPass(s_obf_fla)); // 对于控制流平坦化
471+
FPM.addPass(SubstitutionPass(s_obf_sub)); // 指令替换
472+
FPM.addPass(BogusControlFlowPass(s_obf_bcf)); // 虚假控制流
473+
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
474+
MPM.addPass(IndirectBranchPass(s_obf_ibr)); // 间接指令 理论上间接指令应该放在最后
475+
MPM.addPass(IndirectGlobalVariablePass(s_obf_igv)); // 间接全局变量
476+
MPM.addPass(RewriteSymbolPass()); // 根据yaml信息 重命名特定symbols
477+
}
478+
);
434479
}
435480

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

0 commit comments

Comments
 (0)