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:
+145
-13
@@ -6,19 +6,42 @@
|
||||
#include <stdatomic.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
#include <mach/mach.h>
|
||||
|
||||
/* 编码只减少常量区明文,不作为秘密或强加密。缓存与基线仍可被同进程攻击者修改。 */
|
||||
static const unsigned char kNames[2][5] = {{0x4b,0x4e,0x55,0x48,0x59},
|
||||
{0x5f,0x50,0x53,0x4f,0x59}};
|
||||
enum { IG_DISPATCH_COUNT = 11 };
|
||||
static const unsigned char kNames[IG_DISPATCH_COUNT][6] = {
|
||||
{0x4b,0x4e,0x55,0x48,0x59},
|
||||
{0x5f,0x50,0x53,0x4f,0x59},
|
||||
{0x4e,0x59,0x5d,0x58},
|
||||
{0x53,0x4c,0x59,0x52},
|
||||
{0x49,0x52,0x50,0x55,0x52,0x57},
|
||||
{0x5b,0x59,0x48,0x4c,0x55,0x58},
|
||||
{0x5a,0x53,0x4c,0x59,0x52},
|
||||
{0x59,0x44,0x55,0x48},
|
||||
{0x5a,0x53,0x4e,0x57},
|
||||
{0x58,0x50,0x53,0x4c,0x59,0x52},
|
||||
{0x58,0x50,0x4f,0x45,0x51},
|
||||
};
|
||||
static const size_t kLengths[IG_DISPATCH_COUNT] = {5,5,4,4,6,6,5,4,4,6,5};
|
||||
#if IG_DIAGNOSTIC_STRINGS
|
||||
static const char *const kLabels[IG_DISPATCH_COUNT] = {
|
||||
"dispatch-write", "dispatch-close", "dispatch-read",
|
||||
"dispatch-open", "dispatch-unlink", "dispatch-getpid", "dispatch-fopen",
|
||||
"dispatch-exit", "dispatch-fork", "dispatch-dlopen", "dispatch-dlsym"
|
||||
};
|
||||
#endif
|
||||
static pthread_once_t gOnce=PTHREAD_ONCE_INIT;
|
||||
static _Atomic(uintptr_t) gSlots[2];
|
||||
static uintptr_t gExpected[2];
|
||||
static unsigned char gPrefixes[2][16];
|
||||
static IGStatus gInitialStatus[2]={IG_UNAVAILABLE,IG_UNAVAILABLE};
|
||||
static _Atomic(uintptr_t) gSlots[IG_DISPATCH_COUNT];
|
||||
static uintptr_t gExpected[IG_DISPATCH_COUNT];
|
||||
static unsigned char gPrefixes[IG_DISPATCH_COUNT][16];
|
||||
static IGStatus gInitialStatus[IG_DISPATCH_COUNT];
|
||||
static _Thread_local int gResolving;
|
||||
#ifdef IG_OBSERVATION
|
||||
static _Atomic(uint64_t) gAttempted[2], gDispatched[2];
|
||||
static _Atomic(uint64_t) gAttempted[IG_DISPATCH_COUNT], gDispatched[IG_DISPATCH_COUNT];
|
||||
#define IG_COUNT(slot,index) atomic_fetch_add_explicit(&(slot)[index],1,memory_order_relaxed)
|
||||
#else
|
||||
#define IG_COUNT(slot,index) ((void)0)
|
||||
@@ -31,8 +54,8 @@ static int read_prefix(uintptr_t address, unsigned char out[16]) {
|
||||
}
|
||||
|
||||
static void initialize(void) {
|
||||
for (unsigned i=0;i<2;i++) {
|
||||
IGResolveResult r=ig_resolve_verified(NULL,kNames[i],5,NULL,NULL);
|
||||
for (unsigned i=0;i<IG_DISPATCH_COUNT;i++) {
|
||||
IGResolveResult r=ig_resolve_verified(NULL,kNames[i],kLengths[i],NULL,NULL);
|
||||
gInitialStatus[i]=r.status;
|
||||
if (r.status!=IG_CLEAR || !r.address) continue;
|
||||
uintptr_t address=(uintptr_t)r.address;
|
||||
@@ -61,7 +84,7 @@ static IGStatus validate(unsigned i, uintptr_t *address, int *reason) {
|
||||
*reason=IG_REASON_DISPATCH_TAMPER;
|
||||
return IG_DETECTED;
|
||||
}
|
||||
IGStatus status=ig_verify_function((void *)current,"dispatch-target",NULL,NULL);
|
||||
IGStatus status=ig_verify_function((void *)current,IG_DIAG_STR("dispatch-target"),NULL,NULL);
|
||||
if (status!=IG_CLEAR) {
|
||||
*reason=status==IG_DETECTED ? IG_REASON_INLINE_HOOK : IG_REASON_VERIFICATION_UNAVAILABLE;
|
||||
return status;
|
||||
@@ -79,6 +102,7 @@ static IGStatus validate(unsigned i, uintptr_t *address, int *reason) {
|
||||
return IG_CLEAR;
|
||||
}
|
||||
|
||||
__attribute__((noinline))
|
||||
static uintptr_t checked_address(unsigned i) {
|
||||
if (!prepare()) { errno=EDEADLK; return 0; }
|
||||
uintptr_t address=0; int reason=0;
|
||||
@@ -87,6 +111,11 @@ static uintptr_t checked_address(unsigned i) {
|
||||
return address;
|
||||
}
|
||||
|
||||
__attribute__((constructor))
|
||||
static void ig_dispatch_warmup(void) {
|
||||
(void)prepare();
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
ssize_t IG_dispatch_write(int fd, const void *buffer, size_t count) {
|
||||
int incoming=errno;
|
||||
@@ -111,17 +140,120 @@ int IG_dispatch_close(int fd) {
|
||||
return function(fd);
|
||||
}
|
||||
|
||||
/* Each typed entry preserves libc results and errno; no retry or short-I/O loop.
|
||||
* Not async-signal-safe: validation uses pthread_once/dlsym/Mach APIs. */
|
||||
__attribute__((used,noinline))
|
||||
ssize_t IG_dispatch_read(int fd, void *buffer, size_t count) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(2);
|
||||
if (!address) return -1;
|
||||
errno=incoming;
|
||||
ssize_t (*function)(int,void *,size_t)=(void *)address;
|
||||
return function(fd,buffer,count);
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
int IG_dispatch_open(const char *path, int flags, ...) {
|
||||
int incoming=errno;
|
||||
/* Darwin mode_t is promoted to int in the variadic argument list.
|
||||
* Do not read a nonexistent third argument for ordinary two-argument open. */
|
||||
int mode=0;
|
||||
if (flags & O_CREAT) {
|
||||
va_list args; va_start(args,flags); mode=va_arg(args,int); va_end(args);
|
||||
}
|
||||
uintptr_t address=checked_address(3);
|
||||
if (!address) return -1;
|
||||
errno=incoming;
|
||||
int (*function)(const char *,int,...)=(void *)address;
|
||||
return (flags & O_CREAT) ? function(path,flags,mode) : function(path,flags);
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
int IG_dispatch_unlink(const char *path) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(4);
|
||||
if (!address) return -1;
|
||||
errno=incoming;
|
||||
int (*function)(const char *)=(void *)address;
|
||||
return function(path);
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
pid_t IG_dispatch_getpid(void) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(5);
|
||||
if (!address) return (pid_t)-1;
|
||||
errno=incoming;
|
||||
pid_t (*function)(void)=(void *)address;
|
||||
return function();
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
FILE *IG_dispatch_fopen(const char *path, const char *mode) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(6);
|
||||
if (!address) return NULL;
|
||||
errno=incoming;
|
||||
FILE *(*function)(const char *,const char *)=(void *)address;
|
||||
return function(path,mode);
|
||||
}
|
||||
|
||||
__attribute__((used,noinline,noreturn))
|
||||
void IG_dispatch_exit(int status) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(7);
|
||||
if (address) {
|
||||
errno=incoming;
|
||||
void (*function)(int)=(void *)address;
|
||||
function(status);
|
||||
}
|
||||
_exit(status);
|
||||
__builtin_trap();
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
pid_t IG_dispatch_fork(void) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(8);
|
||||
if (!address) return (pid_t)-1;
|
||||
errno=incoming;
|
||||
pid_t (*function)(void)=(void *)address;
|
||||
return function();
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
void *IG_dispatch_dlopen(const char *path, int mode) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(9);
|
||||
if (!address) return NULL;
|
||||
errno=incoming;
|
||||
void *(*function)(const char *,int)=(void *)address;
|
||||
return function(path,mode);
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
void *IG_dispatch_dlsym(void *handle, const char *symbol) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(10);
|
||||
if (!address) return NULL;
|
||||
errno=incoming;
|
||||
void *(*function)(void *,const char *)=(void *)address;
|
||||
return function(handle,symbol);
|
||||
}
|
||||
|
||||
int ig_dispatch_check(IGFindingSink sink, void *ctx) {
|
||||
int saved=errno, hits=0;
|
||||
int ready=prepare();
|
||||
for (unsigned i=0;i<2;i++) {
|
||||
for (unsigned i=0;i<IG_DISPATCH_COUNT;i++) {
|
||||
uintptr_t address=0; int reason=IG_REASON_DISPATCH_UNAVAILABLE;
|
||||
IGStatus status=ready ? validate(i,&address,&reason) : IG_UNAVAILABLE;
|
||||
if (status==IG_CLEAR) continue;
|
||||
IGFinding f={0}; f.status=status; f.reason=reason;
|
||||
const char *label=i==0 ? "dispatch-write" : "dispatch-close";
|
||||
#if IG_DIAGNOSTIC_STRINGS
|
||||
const char *label=kLabels[i];
|
||||
strncpy(f.subject,label,sizeof(f.subject)-1);
|
||||
strncpy(f.evidence,"调度地址、提供方或代码前缀校验未通过",sizeof(f.evidence)-1);
|
||||
#endif
|
||||
if (status==IG_DETECTED) hits++;
|
||||
if (sink && sink(&f,ctx)!=0) break;
|
||||
}
|
||||
@@ -143,7 +275,7 @@ IGDispatchObservation ig_dispatch_observation(void) {
|
||||
|
||||
#ifdef IG_DISPATCH_TESTING
|
||||
void ig_dispatch_test_override(unsigned i, void *address) {
|
||||
if (i<2 && prepare()) atomic_store_explicit(&gSlots[i],(uintptr_t)address,memory_order_release);
|
||||
if (i<IG_DISPATCH_COUNT && prepare()) atomic_store_explicit(&gSlots[i],(uintptr_t)address,memory_order_release);
|
||||
}
|
||||
void ig_dispatch_test_reentry(int enabled) { gResolving=enabled; }
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user