|
282 | 282 | #include "llvm/Transforms/Vectorize/SLPVectorizer.h" |
283 | 283 | #include "llvm/Transforms/Vectorize/VectorCombine.h" |
284 | 284 | #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;) |
285 | 295 |
|
286 | 296 | using namespace llvm; |
287 | 297 |
|
@@ -448,6 +458,17 @@ class TriggerVerifierErrorPass |
448 | 458 |
|
449 | 459 | } // namespace |
450 | 460 |
|
| 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 | + |
451 | 472 | PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO, |
452 | 473 | std::optional<PGOOptions> PGOOpt, |
453 | 474 | PassInstrumentationCallbacks *PIC) |
@@ -483,6 +504,30 @@ PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO, |
483 | 504 | PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); |
484 | 505 | #include "PassRegistry.def" |
485 | 506 | } |
| 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 | + ); |
486 | 531 | } |
487 | 532 |
|
488 | 533 | void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) { |
|
0 commit comments