在 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>
160 lines
7.1 KiB
C
160 lines
7.1 KiB
C
/* 仅宿主测试进程内操作;不修改系统函数代码。 */
|
||
#include "IGDefender.h"
|
||
#include "anti_hook/IGAntiHook.h"
|
||
#include "anti_hook/IGObjCBaseline.h"
|
||
#include "anti_debug/IGAntiDebug.h"
|
||
#include "anti_frida/IGAntiInject.h"
|
||
#include <dlfcn.h>
|
||
#include <objc/objc.h>
|
||
#include <objc/runtime.h>
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include <unistd.h>
|
||
#include <mach/mach.h>
|
||
#include <mach-o/dyld.h>
|
||
#include <mach-o/loader.h>
|
||
#include <mach-o/nlist.h>
|
||
|
||
typedef struct { int reasons[256]; int detected; int unavailable; } Findings;
|
||
static int collect(const IGFinding *f, void *context) {
|
||
Findings *out = context;
|
||
if (f->reason >= 0 && f->reason < 256) out->reasons[f->reason]++;
|
||
if (f->status == IG_DETECTED) out->detected++;
|
||
if (f->status == IG_UNAVAILABLE) out->unavailable++;
|
||
printf("status=%d reason=%d subject=%s evidence=%s\n",
|
||
f->status, f->reason, f->subject, f->evidence);
|
||
return 0;
|
||
}
|
||
static int fake_getpid(void) { return 123; }
|
||
/* 函数地址表达式确保本测试镜像包含 getpid 的 GOT 引用。 */
|
||
__attribute__((noinline)) static void *getpid_address(void) { return (void *)getpid; }
|
||
|
||
/* ---- ObjC IMP 基线 fixture:运行期创建类,两条实现可互换 ---- */
|
||
static int imp_original(void *self, SEL _cmd) { (void)self; (void)_cmd; return 1; }
|
||
static int imp_replaced(void *self, SEL _cmd) { (void)self; (void)_cmd; return 2; }
|
||
static Class make_fixture_class(void) {
|
||
Class c = objc_allocateClassPair(objc_getClass("NSObject"), "IGFixtureHookTarget", 0);
|
||
if (!c) return NULL;
|
||
if (!class_addMethod(c, sel_registerName("value"), (IMP)imp_original, "i16@0:8")) {
|
||
objc_disposeClassPair(c);
|
||
return NULL;
|
||
}
|
||
objc_registerClassPair(c);
|
||
return c;
|
||
}
|
||
|
||
static void **getpid_slot(void) {
|
||
const struct mach_header_64 *h = (const void *)_dyld_get_image_header(0);
|
||
intptr_t slide = _dyld_get_image_vmaddr_slide(0);
|
||
const struct segment_command_64 *le = NULL;
|
||
const struct symtab_command *sy = NULL;
|
||
const struct dysymtab_command *dy = NULL;
|
||
const unsigned char *p = (const void *)(h+1);
|
||
for (unsigned i=0; i<h->ncmds; i++) {
|
||
const struct load_command *lc=(const void *)p;
|
||
if (lc->cmd==LC_SEGMENT_64 && !strcmp(((const struct segment_command_64 *)p)->segname,"__LINKEDIT")) le=(const void *)p;
|
||
if (lc->cmd==LC_SYMTAB) sy=(const void *)p;
|
||
if (lc->cmd==LC_DYSYMTAB) dy=(const void *)p;
|
||
p+=lc->cmdsize;
|
||
}
|
||
if (!le || !sy || !dy) return NULL;
|
||
uintptr_t base=slide+le->vmaddr-le->fileoff;
|
||
const struct nlist_64 *symbols=(const void *)(base+sy->symoff);
|
||
const char *strings=(const void *)(base+sy->stroff);
|
||
const uint32_t *indirect=(const void *)(base+dy->indirectsymoff);
|
||
p=(const void *)(h+1);
|
||
for (unsigned i=0; i<h->ncmds; i++) {
|
||
const struct load_command *lc=(const void *)p;
|
||
if (lc->cmd==LC_SEGMENT_64) {
|
||
const struct segment_command_64 *seg=(const void *)p;
|
||
const struct section_64 *s=(const void *)(seg+1);
|
||
for (unsigned j=0; j<seg->nsects; j++) {
|
||
if ((s[j].flags & SECTION_TYPE)!=S_NON_LAZY_SYMBOL_POINTERS) continue;
|
||
for (unsigned k=0; k<s[j].size/sizeof(void *); k++) {
|
||
uint32_t n=indirect[s[j].reserved1+k];
|
||
if (n & (INDIRECT_SYMBOL_LOCAL|INDIRECT_SYMBOL_ABS)) continue;
|
||
if (!strcmp(strings+symbols[n].n_un.n_strx,"_getpid"))
|
||
return (void **)(slide+s[j].addr)+k;
|
||
}
|
||
}
|
||
}
|
||
p+=lc->cmdsize;
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
int main(int argc, char **argv) {
|
||
if (argc<2) return 2;
|
||
Findings f={0};
|
||
if (!strcmp(argv[1],"clean")) {
|
||
int hits=ig_defender_scan(NULL,0,collect,&f);
|
||
return hits!=f.detected || f.detected || f.unavailable;
|
||
}
|
||
if (!strcmp(argv[1],"unresolved")) {
|
||
const char *names[]={"IG_fixture_symbol_that_does_not_exist_123"};
|
||
int hits=ig_anti_hook_check(names,1,collect,&f);
|
||
return hits || f.unavailable!=1 || f.reasons[IG_REASON_SYMBOL_UNRESOLVED]!=1;
|
||
}
|
||
if (!strcmp(argv[1],"verify")) {
|
||
if (ig_verify_function(NULL,"null",collect,&f)!=IG_UNAVAILABLE) return 3;
|
||
if (ig_verify_function(getpid_address(),"getpid",collect,&f)!=IG_CLEAR) return 4;
|
||
return ig_verify_function((void *)fake_getpid,"local_fixture",collect,&f)!=IG_DETECTED
|
||
|| f.reasons[IG_REASON_SYMBOL_PROVENANCE]!=1;
|
||
}
|
||
if (!strcmp(argv[1],"env")) {
|
||
setenv("DYLD_INSERT_LIBRARIES","/tmp/ig_fixture_a:/tmp/ig_fixture_b",1);
|
||
ig_anti_inject_check(NULL,collect,&f);
|
||
unsetenv("DYLD_INSERT_LIBRARIES");
|
||
return f.reasons[IG_REASON_DYLD_INSERT]!=2;
|
||
}
|
||
if (!strcmp(argv[1],"image")) {
|
||
if (argc<3 || !dlopen(argv[2],RTLD_NOW)) return 5;
|
||
ig_anti_inject_check(NULL,collect,&f);
|
||
return f.reasons[IG_REASON_INJECTED_IMAGE]!=1;
|
||
}
|
||
if (!strcmp(argv[1],"got")) {
|
||
const char *names[]={"getpid"};
|
||
if (!getpid_address() || ig_anti_hook_check(names,1,collect,&f)!=0) return 6;
|
||
void **slot=getpid_slot();
|
||
if (!slot) { fprintf(stderr,"未找到测试 GOT 槽\n"); return 7; }
|
||
void *original=*slot;
|
||
vm_size_t size=getpagesize();
|
||
vm_address_t page=(uintptr_t)slot & ~(size-1);
|
||
if (vm_protect(mach_task_self(),page,size,0,VM_PROT_READ|VM_PROT_WRITE|VM_PROT_COPY)!=KERN_SUCCESS) return 8;
|
||
*slot=(void *)fake_getpid;
|
||
int hits=ig_anti_hook_check(names,1,collect,&f);
|
||
*slot=original;
|
||
return hits!=1 || f.reasons[IG_REASON_GOT_REBIND]!=1;
|
||
}
|
||
if (!strcmp(argv[1],"debugger")) {
|
||
/* 正常(无调试器)环境:clear 且 sysctl 可判定。
|
||
* 正向(P_TRACED 命中)由 lldb 场景验证,docs/12 已实测。 */
|
||
ig_anti_debug_check(collect,&f);
|
||
return f.detected || f.unavailable;
|
||
}
|
||
if (!strcmp(argv[1],"objc-clean")) {
|
||
if (!make_fixture_class()) return 9;
|
||
const IGObjCWatch watch[] = { { "IGFixtureHookTarget", "value" } };
|
||
if (ig_objc_baseline_capture(watch,1)!=1) return 10;
|
||
/* 未登记的方法在 check 中不产生条目;登记方法未变 → 0 detected。 */
|
||
const IGObjCWatch none[] = { { "IGClassThatNeverExists123", "x" } };
|
||
ig_objc_baseline_capture(none,1);
|
||
if (ig_objc_baseline_check(collect,&f)!=0) return 11; /* 类不可解析不登记 → 仍 no-op */
|
||
ig_objc_baseline_capture(watch,1);
|
||
int hits=ig_objc_baseline_check(collect,&f);
|
||
return hits!=0 || f.detected || f.unavailable;
|
||
}
|
||
if (!strcmp(argv[1],"objc-swap")) {
|
||
Class c=make_fixture_class();
|
||
if (!c) return 9;
|
||
const IGObjCWatch watch[] = { { "IGFixtureHookTarget", "value" } };
|
||
if (ig_objc_baseline_capture(watch,1)!=1) return 10;
|
||
/* class_replaceMethod:IMP 换人(等价 swizzle 效果)→ 期望 DETECTED(110)。 */
|
||
class_replaceMethod(c, sel_registerName("value"), (IMP)imp_replaced, "i16@0:8");
|
||
int hits=ig_objc_baseline_check(collect,&f);
|
||
return hits!=1 || f.reasons[IG_REASON_OBJC_IMP_CHANGED]!=1;
|
||
}
|
||
return 2;
|
||
}
|