- 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
315 lines
11 KiB
C++
315 lines
11 KiB
C++
#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
|