feat: 集成 LLVM IR 混淆体系、Route 2 全量合规与系统调用调度加固

- defender: 扩展 IGDispatch/IGArchiveGuard/IGAntiHook/IGObjCBaseline/IGResolve,支持 Route 2 静态库桥接 (IGThreadID.c) 与防重签名检测
- llvm_passes: 新增 IGObfuscator LLVM IR 插件源码(控制流平坦化、字符串加密、间接跳转/调用、不透明常量/字段、MBA、函数外提、基本块拆分、反 Hook 序言)
- tools & config: 新增 Xcode 归档编译/链接包装器、LLVM 自愈工具链 (ig_toolchain_resolver)、IPA 审计门禁、崩溃符号还原及最新 ig_llvm_policy.json(文件级匹配 + 3.0 全业务 Handler 平坦化)
- tests & docs: 补齐 M0~M3、Pass 单测及系统调用调度/Route 2/LLVM 方案文档;.gitignore 忽略 *.bc 与 .obfuscate_extraction_cache.json
This commit is contained in:
shenlei
2026-11-24 15:00:07 +09:00
parent 1094cc2760
commit 704ee66834
106 changed files with 27487 additions and 286 deletions
+142
View File
@@ -0,0 +1,142 @@
#ifndef IG_OBFUSCATOR_PASS_UTILS_H
#define IG_OBFUSCATOR_PASS_UTILS_H
#include "IGPolicyConfig.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SHA256.h"
#include "llvm/TargetParser/Triple.h"
#include <algorithm>
#include <string>
#include <vector>
namespace igobf {
inline llvm::StringRef stripObjCMarker(llvm::StringRef Name) {
if (!Name.empty() && Name.front() == '\x01')
return Name.drop_front(1);
return Name;
}
inline bool isMachOAArch64NonArm64e(const llvm::Module &M) {
if (M.getTargetTriple().empty())
return true;
llvm::Triple TT(M.getTargetTriple());
return TT.isOSBinFormatMachO() && TT.isAArch64() &&
TT.getArchName() != "arm64e";
}
inline std::string computeShortModuleTag(llvm::StringRef Seed,
llvm::StringRef ModId,
llvm::StringRef SourcePath) {
llvm::SHA256 Hasher;
Hasher.update(Seed);
Hasher.update("|");
Hasher.update(ModId);
Hasher.update("|");
Hasher.update(SourcePath);
auto Digest = Hasher.final();
return llvm::toHex(llvm::ArrayRef<uint8_t>(Digest.data(), 4),
/*LowerCase=*/true);
}
inline bool isCompilerSynthesizedFunction(const llvm::Function &F,
const IGPolicyConfig &Policy) {
if (Policy.hasAnnotation(F, "ig_flatten"))
return false;
llvm::StringRef Name = stripObjCMarker(F.getName());
if (Name.starts_with("block_copy_helper") ||
Name.starts_with("block_destroy_helper") ||
Name.starts_with("__copy_helper_block_") ||
Name.starts_with("___copy_helper_block_") ||
Name.starts_with("__destroy_helper_block_") ||
Name.starts_with("___destroy_helper_block_") ||
Name.starts_with("objectdestroy") ||
Name.ends_with(" .cxx_destruct]") ||
Name.contains(".resume.")) {
return true;
}
if (Name.starts_with("$s") || Name.starts_with("_$s")) {
if (Name.ends_with("Tm") || Name.ends_with("Wl") ||
Name.ends_with("WL") || Name.ends_with("CMa") ||
Name.ends_with("VMa") || Name.ends_with("OMa") ||
Name.ends_with("WOh") || Name.ends_with("WOy") ||
Name.ends_with("WOe") || Name.ends_with("WOc") ||
Name.ends_with("WOb") || Name.ends_with("WOd") ||
Name.ends_with("TR") || Name.ends_with("TA")) {
return true;
}
}
if (const llvm::Module *M = F.getParent()) {
if (M->getNamedMetadata("llvm.dbg.cu") != nullptr) {
const llvm::DISubprogram *SP = F.getSubprogram();
if (!SP || SP->isArtificial() ||
SP->getFilename().contains("<compiler-generated>")) {
return true;
}
}
}
return false;
}
inline bool matchesAnySubstring(const llvm::Function &F,
const std::vector<std::string> &Patterns) {
llvm::StringRef CleanName = stripObjCMarker(F.getName());
for (const std::string &Pat : Patterns) {
if (!Pat.empty() &&
(CleanName.contains(Pat) || F.getName().contains(Pat))) {
return true;
}
}
return false;
}
inline bool isFunctionSafeForTransform(
const llvm::Function &F, const IGPolicyConfig &Policy,
const std::vector<std::string> *ExtraExemptSubstrings = nullptr) {
if (F.isDeclaration() || Policy.isFunctionExempt(F))
return false;
if (llvm::GlobalValue::isWeakForLinker(F.getLinkage()) ||
F.hasAvailableExternallyLinkage()) {
return false;
}
if (F.isPresplitCoroutine() || F.hasFnAttribute(llvm::Attribute::Naked))
return false;
if (F.getCallingConv() == llvm::CallingConv::SwiftTail)
return false;
if (F.hasSection()) {
llvm::StringRef Sec = F.getSection();
if (Sec.contains("__mod_init_func") || Sec.contains("__objc_init_func") ||
Sec.contains("__mod_term_func")) {
return false;
}
}
if (isCompilerSynthesizedFunction(F, Policy))
return false;
if (ExtraExemptSubstrings &&
matchesAnySubstring(F, *ExtraExemptSubstrings)) {
return false;
}
return true;
}
inline llvm::DebugLoc resolveInstDebugLoc(const llvm::Instruction *I,
const llvm::Function &F) {
if (I && I->getDebugLoc())
return I->getDebugLoc();
if (llvm::DISubprogram *SP = F.getSubprogram()) {
unsigned Line = SP->getScopeLine() ? SP->getScopeLine() : SP->getLine();
return llvm::DILocation::get(F.getContext(), Line, 0, SP);
}
return llvm::DebugLoc();
}
} // namespace igobf
#endif
+314
View File
@@ -0,0 +1,314 @@
#ifndef IG_OBFUSCATOR_POLICY_CONFIG_H
#define IG_OBFUSCATOR_POLICY_CONFIG_H
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include <array>
#include <cstdint>
#include <optional>
#include <string>
#include <vector>
namespace llvm {
class Function;
class Module;
} // namespace llvm
namespace igobf {
enum class ObfMode {
Off,
Noop,
StrOnly,
Full,
};
enum class PassFilter {
All,
StrOnly,
FlaOnly,
IcallOnly,
OpaqueConstOnly,
ArithOnly,
IbrOnly,
AntiHookOnly,
OutlineOnly,
FieldOnly,
BBSplitOnly,
};
enum class FlattenTopology {
Polymorphic,
StarSwitch,
};
struct TargetRule {
std::string Id;
std::string PathContains;
std::string FunctionContains;
bool WholeModuleIfNoDebug = false;
std::optional<unsigned> Rounds;
std::optional<unsigned> MaxPerFn;
std::optional<unsigned> Probability;
std::optional<bool> NonlinearMBA;
std::optional<bool> OpaqueZero;
};
struct StringReplaceRule {
std::string Exact;
std::string Replace;
};
struct SkippedFunctionRecord {
std::string Name;
std::string Reason;
};
struct FlattenedFunctionDetail {
std::string Name;
std::string Topology;
std::string Encoding;
unsigned Blocks = 0;
};
struct IGAuditRecord {
std::string Mode;
std::string PassFilter;
std::string Seed;
std::string SourcePath;
std::string ModuleId;
std::vector<std::string> FunctionsFlattened;
std::vector<FlattenedFunctionDetail> FunctionsFlattenedDetails;
std::vector<SkippedFunctionRecord> FunctionsSkipped;
unsigned StringsEncryptedCount = 0;
std::vector<std::string> StringsEncryptedFunctions;
unsigned StringsStackEncryptedCount = 0;
unsigned ObjCStringsEncryptedCount = 0;
unsigned SwiftStringsEncryptedCount = 0;
unsigned StringsReplacedCount = 0;
unsigned StringsSkippedCount = 0;
unsigned PathsScrubbedCount = 0;
unsigned IndirectCallsCount = 0;
std::vector<std::string> IndirectCallsFunctions;
bool IndirectCallDualTable = false;
unsigned OpaqueConstantsCount = 0;
std::vector<std::string> OpaqueConstantsFunctions;
unsigned ArithmeticRewritesCount = 0;
std::vector<std::string> ArithmeticFunctions;
unsigned IndirectBranchesCount = 0;
std::vector<std::string> IndirectBranchesFunctions;
unsigned AntiHookFunctionsCount = 0;
std::vector<std::string> AntiHookFunctions;
unsigned FunctionsOutlinedCount = 0;
std::vector<std::string> OutlinedFunctions;
unsigned OpaqueFieldAccessCount = 0;
std::vector<std::string> OpaqueFieldFunctions;
unsigned BBSplitCount = 0;
std::vector<std::string> BBSplitFunctions;
unsigned NonlinearMBACount = 0;
unsigned OpaqueZeroCount = 0;
std::vector<std::string> AnnotatedTargets;
bool writeToFile(llvm::StringRef OutPath) const;
};
class IGDeterministicRNG {
public:
IGDeterministicRNG(llvm::StringRef Seed, llvm::StringRef ModuleId,
llvm::StringRef ItemId);
std::array<uint8_t, 32> nextBlock();
uint32_t nextU32();
uint32_t nextNonZeroU32();
uint64_t nextU64();
uint64_t nextNonZeroU64();
uint8_t nextNonZeroByte();
template <typename VecT> void shuffle(VecT &Items) {
if (Items.size() < 2)
return;
for (size_t i = Items.size() - 1; i > 0; --i) {
size_t j = static_cast<size_t>(nextU32() % static_cast<uint32_t>(i + 1));
if (i != j)
std::swap(Items[i], Items[j]);
}
}
private:
std::string Prefix_;
uint64_t Counter_ = 0;
std::array<uint8_t, 32> Buffer_{};
size_t Offset_ = 32;
};
class IGPolicyConfig {
public:
static IGPolicyConfig loadFromCLI(const llvm::Module &M,
IGAuditRecord &AuditOut);
ObfMode mode() const { return Mode_; }
PassFilter passFilter() const { return Filter_; }
llvm::StringRef seed() const { return Seed_; }
llvm::StringRef sourcePath() const { return SourcePath_; }
llvm::StringRef swiftModuleName() const { return SwiftModuleName_; }
llvm::StringRef auditOutPath() const { return AuditOutPath_; }
bool isStringEncryptionEnabled() const;
bool isIndirectCallEnabled() const;
bool isFlatteningEnabled() const;
bool isOpaqueConstantsEnabled() const;
bool isArithmeticEnabled() const;
bool isIndirectBranchEnabled() const;
bool isAntiHookingEnabled() const;
bool isFunctionOutlineEnabled() const;
bool isOpaqueFieldEnabled() const;
bool isBBSplitEnabled() const;
FlattenTopology flattenTopology() const { return FlattenTopology_; }
unsigned bogusBlockCount() const { return BogusBlockCount_; }
bool splitLinearBlocks() const { return SplitLinearBlocks_; }
unsigned opaqueConstMaxPerFn() const { return OpaqueConstMaxPerFn_; }
bool opaqueConstOpaqueZero() const { return OpaqueConstOpaqueZero_; }
unsigned arithmeticRounds() const { return ArithmeticRounds_; }
unsigned arithmeticMaxPerFn() const { return ArithmeticMaxPerFn_; }
bool arithmeticNonlinearMBA() const { return ArithmeticNonlinearMBA_; }
unsigned stringEncryptStackMaxLen() const { return StringEncryptStackMaxLen_; }
bool isIndirectCallDualTableEnabled() const { return IndirectCallDualTable_; }
unsigned functionOutlineMaxPerFn() const { return FunctionOutlineMaxPerFn_; }
unsigned functionOutlineMinInsts() const { return FunctionOutlineMinInsts_; }
unsigned opaqueFieldMaxPerFn() const { return OpaqueFieldMaxPerFn_; }
unsigned bbSplitMaxPerFn() const { return BBSplitMaxPerFn_; }
unsigned bbSplitMinInsts() const { return BBSplitMinInsts_; }
bool isModuleExempt(const llvm::Module &M) const;
bool isFunctionExempt(const llvm::Function &F) const;
bool shouldEncryptStringsInFunction(const llvm::Function &F) const;
bool shouldEncryptObjCStringsInFunction(const llvm::Function &F) const;
bool shouldEncryptSwiftStringsInFunction(const llvm::Function &F) const;
bool shouldStackEncryptStringsInFunction(const llvm::Function &F) const;
const std::vector<StringReplaceRule> &stringReplaceRules() const {
return StringReplaceRules_;
}
bool shouldIndirectCallInFunction(const llvm::Function &F) const;
bool shouldFlattenFunction(const llvm::Function &F) const;
bool shouldObfuscateConstantsInFunction(const llvm::Function &F) const;
bool shouldObfuscateArithmeticInFunction(const llvm::Function &F) const;
bool shouldIndirectBranchInFunction(const llvm::Function &F) const;
bool shouldAntiHookFunction(const llvm::Function &F) const;
bool shouldOutlineFunction(const llvm::Function &F) const;
bool shouldObfuscateFieldAccessInFunction(const llvm::Function &F) const;
bool shouldSplitBasicBlocksInFunction(const llvm::Function &F) const;
const TargetRule *findArithmeticRule(const llvm::Function &F) const;
const TargetRule *findOpaqueConstRule(const llvm::Function &F) const;
const TargetRule *findBBSplitRule(const llvm::Function &F) const;
const std::vector<std::string> &indirectCallSameImagePrefixes() const {
return IndirectCallSameImagePrefixes_;
}
const std::vector<std::string> &exemptFunctionPrefixes() const {
return ExemptFunctionPrefixes_;
}
const std::vector<std::string> &scrubPathSubstrings() const {
return ScrubPathSubstrings_;
}
const std::vector<std::string> &opaqueConstExemptFunctions() const {
return OpaqueConstExemptFunctions_;
}
const std::vector<std::string> &arithmeticExemptFunctions() const {
return ArithmeticExemptFunctions_;
}
const std::vector<std::string> &indirectBranchExemptFunctions() const {
return IndirectBranchExemptFunctions_;
}
const std::vector<std::string> &antiHookingExemptFunctions() const {
return AntiHookingExemptFunctions_;
}
const std::vector<std::string> &functionOutlineExemptFunctions() const {
return FunctionOutlineExemptFunctions_;
}
const std::vector<std::string> &opaqueFieldExemptFunctions() const {
return OpaqueFieldExemptFunctions_;
}
const std::vector<std::string> &bbSplitExemptFunctions() const {
return BBSplitExemptFunctions_;
}
void collectAnnotations(const llvm::Module &M);
bool hasAnnotation(const llvm::Function &F, llvm::StringRef Tag) const;
static std::string resolveFunctionSourcePath(const llvm::Function &F,
llvm::StringRef FallbackPath);
private:
const TargetRule *findMatchingRule(const std::vector<TargetRule> &Rules,
const llvm::Function &F) const;
bool matchesAnyRule(const std::vector<TargetRule> &Rules,
const llvm::Function &F) const;
ObfMode Mode_ = ObfMode::Full;
PassFilter Filter_ = PassFilter::All;
FlattenTopology FlattenTopology_ = FlattenTopology::Polymorphic;
unsigned BogusBlockCount_ = 0;
bool SplitLinearBlocks_ = false;
unsigned OpaqueConstMaxPerFn_ = 64;
bool OpaqueConstOpaqueZero_ = false;
unsigned ArithmeticRounds_ = 1;
unsigned ArithmeticMaxPerFn_ = 128;
bool ArithmeticNonlinearMBA_ = false;
unsigned StringEncryptStackMaxLen_ = 128;
bool IndirectCallDualTable_ = false;
unsigned FunctionOutlineMaxPerFn_ = 2;
unsigned FunctionOutlineMinInsts_ = 6;
unsigned OpaqueFieldMaxPerFn_ = 32;
unsigned BBSplitMaxPerFn_ = 4;
unsigned BBSplitMinInsts_ = 8;
std::string Seed_ = "m0_seed_20261119_v1";
std::string SourcePath_;
std::string SwiftModuleName_;
std::string AuditOutPath_;
std::vector<std::string> ExemptPathSubstrings_;
std::vector<std::string> ExemptFunctionPrefixes_;
std::vector<std::string> ScrubPathSubstrings_;
std::vector<TargetRule> StringEncryptTargets_;
std::vector<TargetRule> StringEncryptObjCTargets_;
std::vector<TargetRule> StringEncryptSwiftTargets_;
std::vector<TargetRule> StringEncryptStackTargets_;
std::vector<StringReplaceRule> StringReplaceRules_;
std::vector<TargetRule> IndirectCallTargets_;
std::vector<std::string> IndirectCallSameImagePrefixes_;
std::vector<TargetRule> FlattenTargets_;
std::vector<TargetRule> OpaqueConstTargets_;
std::vector<std::string> OpaqueConstExemptFunctions_;
std::vector<TargetRule> ArithmeticTargets_;
std::vector<std::string> ArithmeticExemptFunctions_;
std::vector<TargetRule> IndirectBranchTargets_;
std::vector<std::string> IndirectBranchExemptFunctions_;
std::vector<TargetRule> AntiHookingTargets_;
std::vector<std::string> AntiHookingExemptFunctions_;
std::vector<TargetRule> FunctionOutlineTargets_;
std::vector<std::string> FunctionOutlineExemptFunctions_;
std::vector<TargetRule> OpaqueFieldTargets_;
std::vector<std::string> OpaqueFieldExemptFunctions_;
std::vector<TargetRule> BBSplitTargets_;
std::vector<std::string> BBSplitExemptFunctions_;
llvm::StringSet<> AnnotatedFlatten_;
llvm::StringSet<> AnnotatedStr_;
llvm::StringSet<> AnnotatedIcall_;
llvm::StringSet<> AnnotatedConst_;
llvm::StringSet<> AnnotatedArith_;
llvm::StringSet<> AnnotatedIbr_;
llvm::StringSet<> AnnotatedAntiHook_;
llvm::StringSet<> AnnotatedOutline_;
llvm::StringSet<> AnnotatedField_;
llvm::StringSet<> AnnotatedBBSplit_;
llvm::StringSet<> AnnotatedExempt_;
};
} // namespace igobf
#endif