|
256 | 256 | #include "llvm/Transforms/Vectorize/SLPVectorizer.h" |
257 | 257 | #include "llvm/Transforms/Vectorize/VectorCombine.h" |
258 | 258 | #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;) |
259 | 269 |
|
260 | 270 | using namespace llvm; |
261 | 271 |
|
@@ -396,6 +406,17 @@ class TriggerCrashPass : public PassInfoMixin<TriggerCrashPass> { |
396 | 406 |
|
397 | 407 | } // namespace |
398 | 408 |
|
| 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 | + |
399 | 420 | PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO, |
400 | 421 | std::optional<PGOOptions> PGOOpt, |
401 | 422 | PassInstrumentationCallbacks *PIC) |
@@ -431,6 +452,30 @@ PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO, |
431 | 452 | PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); |
432 | 453 | #include "PassRegistry.def" |
433 | 454 | } |
| 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 | + ); |
434 | 479 | } |
435 | 480 |
|
436 | 481 | void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) { |
|
0 commit comments