- 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
282 lines
9.2 KiB
C
282 lines
9.2 KiB
C
#include "IGDispatch.h"
|
|
#include "../resolve/IGResolve.h"
|
|
#include "../anti_hook/IGAntiHook.h"
|
|
#include "../primitives/IGInline.h"
|
|
#include <pthread.h>
|
|
#include <stdatomic.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <fcntl.h>
|
|
#include <stdarg.h>
|
|
#include <unistd.h>
|
|
#include <mach/mach.h>
|
|
|
|
/* 编码只减少常量区明文,不作为秘密或强加密。缓存与基线仍可被同进程攻击者修改。 */
|
|
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[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[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)
|
|
#endif
|
|
|
|
static int read_prefix(uintptr_t address, unsigned char out[16]) {
|
|
vm_size_t copied=0;
|
|
return vm_read_overwrite(mach_task_self(),address,16,(vm_address_t)out,&copied)==KERN_SUCCESS
|
|
&& copied==16;
|
|
}
|
|
|
|
static void initialize(void) {
|
|
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;
|
|
if (!read_prefix(address,gPrefixes[i])) { gInitialStatus[i]=IG_UNAVAILABLE; continue; }
|
|
gExpected[i]=address;
|
|
atomic_store_explicit(&gSlots[i],address,memory_order_release);
|
|
}
|
|
}
|
|
|
|
static int prepare(void) {
|
|
/* dlsym/装载初始化过程中若重入受保护入口,拒绝而不是等待自身 once 锁。 */
|
|
if (gResolving) return 0;
|
|
gResolving=1;
|
|
int rc=pthread_once(&gOnce,initialize);
|
|
gResolving=0;
|
|
return rc==0;
|
|
}
|
|
|
|
static IGStatus validate(unsigned i, uintptr_t *address, int *reason) {
|
|
if (gInitialStatus[i]!=IG_CLEAR) {
|
|
*reason=IG_REASON_DISPATCH_UNAVAILABLE;
|
|
return gInitialStatus[i];
|
|
}
|
|
uintptr_t current=atomic_load_explicit(&gSlots[i],memory_order_acquire);
|
|
if (!current || current!=gExpected[i]) {
|
|
*reason=IG_REASON_DISPATCH_TAMPER;
|
|
return IG_DETECTED;
|
|
}
|
|
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;
|
|
}
|
|
unsigned char bytes[16];
|
|
if (!read_prefix(current,bytes)) {
|
|
*reason=IG_REASON_VERIFICATION_UNAVAILABLE;
|
|
return IG_UNAVAILABLE;
|
|
}
|
|
if (ig_memcmp(bytes,gPrefixes[i],16)!=0) {
|
|
*reason=IG_REASON_DISPATCH_TAMPER;
|
|
return IG_DETECTED;
|
|
}
|
|
*address=current;
|
|
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;
|
|
IGStatus status=validate(i,&address,&reason);
|
|
if (status!=IG_CLEAR) { errno=status==IG_DETECTED ? EACCES : ENOSYS; return 0; }
|
|
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;
|
|
IG_COUNT(gAttempted,0);
|
|
uintptr_t address=checked_address(0);
|
|
if (!address) return -1;
|
|
IG_COUNT(gDispatched,0);
|
|
errno=incoming;
|
|
ssize_t (*function)(int,const void *,size_t)=(void *)address;
|
|
return function(fd,buffer,count);
|
|
}
|
|
|
|
__attribute__((used,noinline))
|
|
int IG_dispatch_close(int fd) {
|
|
int incoming=errno;
|
|
IG_COUNT(gAttempted,1);
|
|
uintptr_t address=checked_address(1);
|
|
if (!address) return -1;
|
|
IG_COUNT(gDispatched,1);
|
|
errno=incoming;
|
|
int (*function)(int)=(void *)address;
|
|
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<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;
|
|
#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;
|
|
}
|
|
errno=saved;
|
|
return hits;
|
|
}
|
|
|
|
#ifdef IG_OBSERVATION
|
|
IGDispatchObservation ig_dispatch_observation(void) {
|
|
IGDispatchObservation value={
|
|
atomic_load_explicit(&gAttempted[0],memory_order_relaxed),
|
|
atomic_load_explicit(&gDispatched[0],memory_order_relaxed),
|
|
atomic_load_explicit(&gAttempted[1],memory_order_relaxed),
|
|
atomic_load_explicit(&gDispatched[1],memory_order_relaxed)
|
|
};
|
|
return value;
|
|
}
|
|
#endif
|
|
|
|
#ifdef IG_DISPATCH_TESTING
|
|
void ig_dispatch_test_override(unsigned i, void *address) {
|
|
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
|