feat(defender): 可集成静态库 + readoor 接入方案(检测→分级→数据毒化闭环)
在 iOSGuard 侧产出可集成产物与接入文档,不修改 readoor 代码。 - defender/: anti_hook/anti_frida/anti_debug/anti_jailbreak 探针 + syscall 直发、 policy 分级层、poison 数据依赖毒化、dispatch/resolve/call_bridge 等子模块 - poison: 三副本冗余、无分支掩码;健康恒等、毒化破坏 key(替代 if-exit 处置) - policy: STRONG/MEDIUM/WEAK 分级,守误报底线,仅 DETECTED 参与、组合命中才毒化 - anti_jailbreak: IGSyscall 直发 svc 检测越狱文件(400)/沙盒逃逸(401),绕 libc hook - tools/build_defender_lib.sh: 一键交叉编译打包 libIGDefender.a(arm64) + umbrella 头 - docs/18_READOOR_DEFENDER_INTEGRATION.md: 精确到文件/bridging/替换点/代码片段 - docs/05-17 + evidence + tests + macho_guard/static_sdk_redirect 工具链 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
ff0aee7f47
commit
599e1da5ce
@@ -0,0 +1,149 @@
|
||||
#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 <mach/mach.h>
|
||||
|
||||
/* 编码只减少常量区明文,不作为秘密或强加密。缓存与基线仍可被同进程攻击者修改。 */
|
||||
static const unsigned char kNames[2][5] = {{0x4b,0x4e,0x55,0x48,0x59},
|
||||
{0x5f,0x50,0x53,0x4f,0x59}};
|
||||
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 _Thread_local int gResolving;
|
||||
#ifdef IG_OBSERVATION
|
||||
static _Atomic(uint64_t) gAttempted[2], gDispatched[2];
|
||||
#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<2;i++) {
|
||||
IGResolveResult r=ig_resolve_verified(NULL,kNames[i],5,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,"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;
|
||||
}
|
||||
|
||||
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__((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);
|
||||
}
|
||||
|
||||
int ig_dispatch_check(IGFindingSink sink, void *ctx) {
|
||||
int saved=errno, hits=0;
|
||||
int ready=prepare();
|
||||
for (unsigned i=0;i<2;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";
|
||||
strncpy(f.subject,label,sizeof(f.subject)-1);
|
||||
strncpy(f.evidence,"调度地址、提供方或代码前缀校验未通过",sizeof(f.evidence)-1);
|
||||
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<2 && 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