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
+81
@@ -0,0 +1,81 @@
|
||||
#include <assert.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <mach/mach.h>
|
||||
#include <mach-o/dyld.h>
|
||||
#include <mach-o/loader.h>
|
||||
#include <mach-o/nlist.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
assert(argc == 3);
|
||||
void *sdk = dlopen(argv[1], RTLD_NOW); if (!sdk) { puts(dlerror()); return 1; }
|
||||
int (*io)(const char *,const char *,size_t) = dlsym(sdk,"archive_fixture_io");
|
||||
int (*check)(void) = dlsym(sdk,"ig_archive_check");
|
||||
uint64_t (*counter)(unsigned) = dlsym(sdk,"ig_archive_counter");
|
||||
assert(io && check && counter && check() == 0);
|
||||
assert(!io(argv[2],"abcdef",6));
|
||||
for (unsigned i=0;i<4;i++) assert(counter(i*3)>0 && counter(i*3)==counter(i*3+1) && !counter(i*3+2));
|
||||
const struct mach_header_64 *header = NULL;
|
||||
intptr_t slide = 0;
|
||||
for (uint32_t i=0;i<_dyld_image_count();i++) {
|
||||
const char *name = _dyld_get_image_name(i);
|
||||
if (name && strstr(name,"/SSZipArchive.framework/SSZipArchive")) {
|
||||
header=(const void *)_dyld_get_image_header(i); slide=_dyld_get_image_vmaddr_slide(i); break;
|
||||
}
|
||||
}
|
||||
assert(header);
|
||||
const struct symtab_command *symtab=NULL;
|
||||
const struct dysymtab_command *dysymtab=NULL;
|
||||
uintptr_t base=0;
|
||||
const uint8_t *command=(const void *)(header+1);
|
||||
for (uint32_t i=0;i<header->ncmds;i++) {
|
||||
const struct load_command *lc=(const void *)command;
|
||||
if (lc->cmd==LC_SYMTAB) symtab=(const void *)lc;
|
||||
if (lc->cmd==LC_DYSYMTAB) dysymtab=(const void *)lc;
|
||||
if (lc->cmd==LC_SEGMENT_64) {
|
||||
const struct segment_command_64 *seg=(const void *)lc;
|
||||
if (!strcmp(seg->segname,SEG_LINKEDIT)) base=slide+seg->vmaddr-seg->fileoff;
|
||||
}
|
||||
command+=lc->cmdsize;
|
||||
}
|
||||
assert(base && symtab && dysymtab);
|
||||
const struct nlist_64 *symbols=(const void *)(base+symtab->symoff);
|
||||
const char *strings=(const void *)(base+symtab->stroff);
|
||||
const uint32_t *indirect=(const void *)(base+dysymtab->indirectsymoff);
|
||||
uintptr_t *slot=NULL;
|
||||
command=(const void *)(header+1);
|
||||
for (uint32_t i=0;i<header->ncmds;i++) {
|
||||
const struct load_command *lc=(const void *)command;
|
||||
if (lc->cmd==LC_SEGMENT_64) {
|
||||
const struct segment_command_64 *seg=(const void *)lc;
|
||||
const struct section_64 *sections=(const void *)(seg+1);
|
||||
for (uint32_t j=0;j<seg->nsects;j++) {
|
||||
const struct section_64 *s=sections+j;
|
||||
if ((s->flags&SECTION_TYPE)!=S_NON_LAZY_SYMBOL_POINTERS) continue;
|
||||
for (uint64_t k=0;k<s->size/8;k++) {
|
||||
uint32_t n=indirect[s->reserved1+k];
|
||||
if (n&(INDIRECT_SYMBOL_LOCAL|INDIRECT_SYMBOL_ABS)) continue;
|
||||
if (!strcmp(strings+symbols[n].n_un.n_strx,"_ig_a0")) slot=(void *)(slide+s->addr+k*8);
|
||||
}
|
||||
}
|
||||
}
|
||||
command+=lc->cmdsize;
|
||||
}
|
||||
assert(slot);
|
||||
uintptr_t original=*slot;
|
||||
vm_address_t page=(uintptr_t)slot & ~((uintptr_t)vm_page_size-1);
|
||||
assert(vm_protect(mach_task_self(),page,vm_page_size,0,VM_PROT_READ|VM_PROT_WRITE|VM_PROT_COPY)==KERN_SUCCESS);
|
||||
*slot=(uintptr_t)dlsym(RTLD_DEFAULT,"fopen");
|
||||
assert(check()==1);
|
||||
FILE *(*close_entry)(const char *,const char *) = dlsym(sdk,"ig_a0");
|
||||
assert(close_entry && !close_entry("unused","rb") && errno==EACCES);
|
||||
*slot=original;
|
||||
assert(check()==0);
|
||||
assert(!io(argv[2],"ghijkl",6)); unlink(argv[2]);
|
||||
puts("真实 chained binding 转发、GOT 篡改检测及拒绝、恢复均通过");
|
||||
return 0;
|
||||
}
|
||||
Vendored
+59
@@ -0,0 +1,59 @@
|
||||
#include "dispatch/IGArchiveGuard.h"
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static unsigned fake_calls;
|
||||
static FILE *fake_open(const char *a, const char *b) { (void)a; (void)b; fake_calls++; return NULL; }
|
||||
static void semantics(void) {
|
||||
char path[] = "/tmp/ig-archive-XXXXXX";
|
||||
int fd = mkstemp(path); assert(fd >= 0); close(fd);
|
||||
errno = EDOM;
|
||||
FILE *out = ig_a0(path, "wb"); assert(out && errno == EDOM);
|
||||
assert(ig_a2("abcdef", 2, 3, out) == 3);
|
||||
assert(ig_a2("", 0, 4, out) == 0);
|
||||
assert(ig_a3(out) == 0);
|
||||
FILE *in = ig_a0(path, "rb"); assert(in);
|
||||
char buffer[16] = {0};
|
||||
assert(ig_a1(buffer, 2, 4, in) == 3);
|
||||
assert(!memcmp(buffer, "abcdef", 6) && feof(in) && !ferror(in));
|
||||
assert(ig_a1(buffer, 1, 1, in) == 0);
|
||||
assert(ig_a3(in) == 0); unlink(path);
|
||||
errno = 0; assert(!ig_a0(path, "rb") && errno == ENOENT);
|
||||
FILE *system = tmpfile(); assert(system);
|
||||
assert(ig_a2("abc", 1, 3, system) == 3);
|
||||
rewind(system); assert(ig_a1(buffer, 1, 3, system) == 3); assert(ig_a3(system) == 0);
|
||||
assert(ig_archive_check() == 0);
|
||||
}
|
||||
static void *thread_run(void *ctx) { (void)ctx; semantics(); return NULL; }
|
||||
int main(int argc, char **argv) {
|
||||
assert(argc == 2);
|
||||
if (!strcmp(argv[1], "semantics")) semantics();
|
||||
else if (!strcmp(argv[1], "threads")) {
|
||||
pthread_t t[6];
|
||||
for (unsigned i=0;i<6;i++) assert(!pthread_create(t+i,NULL,thread_run,NULL));
|
||||
for (unsigned i=0;i<6;i++) assert(!pthread_join(t[i],NULL));
|
||||
} else if (!strcmp(argv[1], "tamper")) {
|
||||
ig_archive_test_override(0, (void *)fake_open);
|
||||
assert(ig_a0("unused", "rb") == NULL && errno == EACCES && !fake_calls);
|
||||
errno = EDOM; assert(ig_archive_check() == 1 && errno == EDOM);
|
||||
assert(ig_archive_counter(0) == 1 && ig_archive_counter(1) == 0 && ig_archive_counter(2) == 1);
|
||||
} else if (!strcmp(argv[1], "reentry")) {
|
||||
ig_archive_test_reentry(1);
|
||||
assert(!ig_a0("unused", "rb") && errno == EDEADLK);
|
||||
assert(ig_archive_check() == -1);
|
||||
ig_archive_test_reentry(0); semantics();
|
||||
} else if (!strcmp(argv[1], "rejection")) {
|
||||
FILE *f = tmpfile(); assert(f);
|
||||
char b;
|
||||
for (unsigned i=1;i<4;i++) ig_archive_test_override(i, (void *)fake_open);
|
||||
assert(ig_a1(&b,1,1,f)==0 && errno==EACCES);
|
||||
assert(ig_a2(&b,1,1,f)==0 && errno==EACCES);
|
||||
assert(ig_a3(f)==EOF && errno==EACCES);
|
||||
assert(!fake_calls); fclose(f);
|
||||
} else assert(0);
|
||||
return 0;
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
/* 动态 SDK fixture:必须通过真实的四个导入完成一次 stdio 往返。 */
|
||||
int archive_fixture_io(const char *path, const char *bytes, size_t count) {
|
||||
FILE *file = fopen(path, "wb");
|
||||
if (!file) return 1;
|
||||
size_t written = fwrite(bytes, 1, count, file);
|
||||
if (fclose(file) || written != count) return 2;
|
||||
file = fopen(path, "rb");
|
||||
if (!file) return 3;
|
||||
char buffer[64] = {0};
|
||||
size_t read = fread(buffer, 1, sizeof(buffer), file);
|
||||
int closed = fclose(file);
|
||||
return closed || read != count || memcmp(bytes, buffer, count) ? 4 : 0;
|
||||
}
|
||||
Vendored
+159
@@ -0,0 +1,159 @@
|
||||
/* 仅宿主测试进程内操作;不修改系统函数代码。 */
|
||||
#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;
|
||||
}
|
||||
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
/* 独立诊断进程:只在自身进程测试,不连接业务、不改系统代码。 */
|
||||
#import <Foundation/Foundation.h>
|
||||
#include "IGDefender.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int ig_dispatch_fixture_main(int argc, char **argv);
|
||||
static int findings, unavailable;
|
||||
static int collect(const IGFinding *f, void *ctx) {
|
||||
(void)ctx;
|
||||
if (f->status==IG_DETECTED) findings++;
|
||||
if (f->status==IG_UNAVAILABLE) unavailable++;
|
||||
NSLog(@"IG_DEVICE_FINDING status=%d reason=%d subject=%s evidence=%s",
|
||||
f->status,f->reason,f->subject,f->evidence);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc,char **argv) {
|
||||
(void)argc;(void)argv;
|
||||
@autoreleasepool {
|
||||
NSLog(@"IG_DEVICE_MAIN");
|
||||
const char *modes[]={"semantics","short-write","eintr","threads","reentry","resolver","prologue","tamper"};
|
||||
int hits=ig_defender_scan(NSBundle.mainBundle.bundlePath.UTF8String,0,collect,NULL);
|
||||
NSLog(@"IG_DEVICE_BASELINE detected=%d unavailable=%d hits=%d",findings,unavailable,hits);
|
||||
for (unsigned i=0;i<sizeof(modes)/sizeof(modes[0]);i++) {
|
||||
char *args[]={"dispatch-device",(char *)modes[i],NULL};
|
||||
int rc=ig_dispatch_fixture_main(2,args);
|
||||
NSLog(@"IG_DEVICE_CASE %s %s",modes[i],rc==0 ? "PASS" : "FAIL");
|
||||
if (rc) return rc;
|
||||
}
|
||||
NSLog(@"IG_DEVICE_SUITE PASS cases=8 baseline_detected=%d baseline_unavailable=%d",findings,unavailable);
|
||||
/* 诊断进程退出便于 console 收集退出码;不编入业务 App。 */
|
||||
return findings || unavailable ? 3 : 0;
|
||||
}
|
||||
}
|
||||
Vendored
+131
@@ -0,0 +1,131 @@
|
||||
#include "dispatch/IGDispatch.h"
|
||||
#include "resolve/IGResolve.h"
|
||||
#include "anti_hook/IGAntiHook.h"
|
||||
#include <assert.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int early_ok;
|
||||
__attribute__((constructor)) static void early(void) {
|
||||
#ifdef IG_EXPECT_INIT_FAILURE
|
||||
early_ok=1;
|
||||
#else
|
||||
int fd=open("/dev/null",O_WRONLY);
|
||||
early_ok=fd>=0 && IG_dispatch_write(fd,"early",5)==5 && IG_dispatch_close(fd)==0;
|
||||
#endif
|
||||
}
|
||||
void *ig_fixture_missing_resolve(void *handle, const char *name) { (void)handle; (void)name; return NULL; }
|
||||
static int reasons[256], unavailable;
|
||||
static int collect(const IGFinding *f,void *ctx) {
|
||||
(void)ctx;
|
||||
if (f->reason>=0 && f->reason<256) reasons[f->reason]++;
|
||||
if (f->status==IG_UNAVAILABLE) unavailable++;
|
||||
return 0;
|
||||
}
|
||||
static int fake_close(int fd) { (void)fd; abort(); }
|
||||
static void *writer(void *ctx) {
|
||||
(void)ctx;
|
||||
int fd=open("/dev/null",O_WRONLY);
|
||||
assert(fd>=0);
|
||||
for (int i=0;i<100;i++) assert(IG_dispatch_write(fd,"abc",3)==3);
|
||||
assert(IG_dispatch_close(fd)==0);
|
||||
return NULL;
|
||||
}
|
||||
static void signal_handler(int sig) { (void)sig; }
|
||||
static pthread_t main_thread;
|
||||
static void *interruptor(void *ctx) { (void)ctx; usleep(30000); pthread_kill(main_thread,SIGUSR1); return NULL; }
|
||||
|
||||
int main(int argc,char **argv) {
|
||||
assert(argc==2 && early_ok);
|
||||
if (!strcmp(argv[1],"observation")) {
|
||||
#ifdef IG_OBSERVATION
|
||||
IGDispatchObservation before=ig_dispatch_observation();
|
||||
errno=0; assert(IG_dispatch_write(-1,"x",1)==-1 && errno==EBADF);
|
||||
errno=0; assert(IG_dispatch_close(-1)==-1 && errno==EBADF);
|
||||
IGDispatchObservation after=ig_dispatch_observation();
|
||||
assert(after.write_attempted==before.write_attempted+1);
|
||||
assert(after.write_dispatched==before.write_dispatched+1);
|
||||
assert(after.close_attempted==before.close_attempted+1);
|
||||
assert(after.close_dispatched==before.close_dispatched+1);
|
||||
ig_dispatch_test_override(1,(void *)fake_close);
|
||||
assert(IG_dispatch_close(-1)==-1 && errno==EACCES);
|
||||
IGDispatchObservation rejected=ig_dispatch_observation();
|
||||
assert(rejected.close_attempted==after.close_attempted+1);
|
||||
assert(rejected.close_dispatched==after.close_dispatched);
|
||||
#else
|
||||
return 2;
|
||||
#endif
|
||||
} else if (!strcmp(argv[1],"init-failure")) {
|
||||
assert(IG_dispatch_write(-1,"x",1)==-1 && errno==ENOSYS);
|
||||
assert(IG_dispatch_close(-1)==-1 && errno==ENOSYS);
|
||||
assert(ig_dispatch_check(collect,NULL)==0 && unavailable==2);
|
||||
assert(reasons[IG_REASON_DISPATCH_UNAVAILABLE]==2);
|
||||
} else if (!strcmp(argv[1],"semantics")) {
|
||||
assert(ig_dispatch_check(collect,NULL)==0 && unavailable==0);
|
||||
errno=0; assert(IG_dispatch_write(-1,"x",1)==-1 && errno==EBADF);
|
||||
errno=0; assert(IG_dispatch_close(-1)==-1 && errno==EBADF);
|
||||
int p[2]; assert(pipe(p)==0);
|
||||
errno=EDOM; assert(IG_dispatch_write(p[1],"abc",3)==3 && errno==EDOM);
|
||||
char b[4]={0}; assert(read(p[0],b,3)==3 && !memcmp(b,"abc",3));
|
||||
assert(IG_dispatch_close(p[1])==0 && IG_dispatch_close(p[0])==0);
|
||||
} else if (!strcmp(argv[1],"short-write")) {
|
||||
int p[2]; assert(pipe(p)==0);
|
||||
assert(fcntl(p[1],F_SETFL,O_NONBLOCK)==0);
|
||||
const size_t count=1024*1024;
|
||||
char *bytes=calloc(count,1);assert(bytes);
|
||||
ssize_t n=IG_dispatch_write(p[1],bytes,count);
|
||||
assert(n>0 && n<(ssize_t)count);
|
||||
while (IG_dispatch_write(p[1],bytes,count)>0) {}
|
||||
assert(errno==EAGAIN);
|
||||
IG_dispatch_close(p[1]); IG_dispatch_close(p[0]);
|
||||
free(bytes);
|
||||
} else if (!strcmp(argv[1],"eintr")) {
|
||||
int p[2]; assert(pipe(p)==0);
|
||||
assert(fcntl(p[1],F_SETFL,O_NONBLOCK)==0);
|
||||
char bytes[4096]={0}; while (write(p[1],bytes,sizeof(bytes))>0) {}
|
||||
assert(errno==EAGAIN && fcntl(p[1],F_SETFL,0)==0);
|
||||
struct sigaction action={0};action.sa_handler=signal_handler;sigemptyset(&action.sa_mask);
|
||||
assert(sigaction(SIGUSR1,&action,NULL)==0);
|
||||
main_thread=pthread_self();pthread_t thread;
|
||||
assert(pthread_create(&thread,NULL,interruptor,NULL)==0);
|
||||
assert(IG_dispatch_write(p[1],bytes,sizeof(bytes))==-1 && errno==EINTR);
|
||||
pthread_join(thread,NULL);IG_dispatch_close(p[1]);IG_dispatch_close(p[0]);
|
||||
} else if (!strcmp(argv[1],"threads")) {
|
||||
pthread_t threads[8];
|
||||
for (unsigned i=0;i<8;i++) assert(pthread_create(&threads[i],NULL,writer,NULL)==0);
|
||||
for (unsigned i=0;i<8;i++) pthread_join(threads[i],NULL);
|
||||
} else if (!strcmp(argv[1],"tamper")) {
|
||||
ig_dispatch_test_override(1,(void *)fake_close);
|
||||
errno=0; assert(IG_dispatch_close(-1)==-1 && errno==EACCES);
|
||||
assert(ig_dispatch_check(collect,NULL)==1 && reasons[IG_REASON_DISPATCH_TAMPER]==1);
|
||||
} else if (!strcmp(argv[1],"reentry")) {
|
||||
ig_dispatch_test_reentry(1);
|
||||
assert(IG_dispatch_close(-1)==-1 && errno==EDEADLK);
|
||||
ig_dispatch_test_reentry(0);
|
||||
} else if (!strcmp(argv[1],"resolver")) {
|
||||
unsigned char encoded[300];memset(encoded,'x',sizeof(encoded));char out[4];
|
||||
assert(!ig_decrypt(encoded,1,out,0));assert(!ig_decrypt(NULL,1,out,sizeof(out)));
|
||||
assert(!ig_decrypt(encoded,4,out,sizeof(out)) && out[0]==0);
|
||||
encoded[0]=IG_ENC_KEY;assert(!ig_decrypt(encoded,1,out,sizeof(out)));
|
||||
assert(ig_resolve_verified(NULL,encoded,300,NULL,NULL).status==IG_UNAVAILABLE);
|
||||
const char *name="IG_missing_symbol_774444";
|
||||
for (unsigned i=0;i<strlen(name);i++) encoded[i]=name[i]^IG_ENC_KEY;
|
||||
assert(ig_resolve_verified(NULL,encoded,strlen(name),collect,NULL).status==IG_UNAVAILABLE);
|
||||
assert(reasons[IG_REASON_SYMBOL_UNRESOLVED]==1);
|
||||
assert(ig_verify_function((void *)1,"invalid",collect,NULL)==IG_UNAVAILABLE);
|
||||
assert(reasons[IG_REASON_VERIFICATION_UNAVAILABLE]==1);
|
||||
} else if (!strcmp(argv[1],"prologue")) {
|
||||
assert(ig_arm64_suspicious_prologue(0x14000001,0));
|
||||
assert(ig_arm64_suspicious_prologue(0x58000050,0xd61f0200));
|
||||
assert(!ig_arm64_suspicious_prologue(0x58000050,0xd61f0220));
|
||||
assert(!ig_arm64_suspicious_prologue(0x18000050,0xd61f0200));
|
||||
assert(!ig_arm64_suspicious_prologue(0xd503245f,0xd65f03c0));
|
||||
} else return 2;
|
||||
return 0;
|
||||
}
|
||||
Vendored
+112
@@ -0,0 +1,112 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern FILE *sdk_open(const char *path, const char *mode);
|
||||
|
||||
#ifdef IG_TEST_ALL_BRIDGES
|
||||
extern int sdk_fclose(FILE *);
|
||||
extern size_t sdk_fread(void *, size_t, size_t, FILE *);
|
||||
extern size_t sdk_fwrite(const void *, size_t, size_t, FILE *);
|
||||
extern ssize_t sdk_read(int, void *, size_t);
|
||||
extern ssize_t sdk_write(int, const void *, size_t);
|
||||
extern int sdk_close(int);
|
||||
#define fclose sdk_fclose
|
||||
#define fread sdk_fread
|
||||
#define fwrite sdk_fwrite
|
||||
|
||||
static void exercise_posix(void) {
|
||||
int descriptors[2];
|
||||
char buffer[4096] = {0};
|
||||
assert(pipe(descriptors) == 0);
|
||||
assert(sdk_write(descriptors[1], "X", 1) == 1);
|
||||
assert(sdk_read(descriptors[0], buffer, sizeof(buffer)) == 1);
|
||||
assert(buffer[0] == 'X');
|
||||
assert(sdk_write(descriptors[1], buffer, 0) == 0);
|
||||
assert(sdk_read(descriptors[0], buffer, 0) == 0);
|
||||
assert(sdk_close(descriptors[1]) == 0);
|
||||
assert(sdk_read(descriptors[0], buffer, sizeof(buffer)) == 0);
|
||||
assert(sdk_close(descriptors[0]) == 0);
|
||||
assert(sdk_close(-1) == -1 && errno == EBADF);
|
||||
assert(sdk_read(-1, buffer, 1) == -1 && errno == EBADF);
|
||||
assert(sdk_write(-1, buffer, 1) == -1 && errno == EBADF);
|
||||
|
||||
assert(pipe(descriptors) == 0);
|
||||
int flags = fcntl(descriptors[1], F_GETFL);
|
||||
assert(flags >= 0 && fcntl(descriptors[1], F_SETFL, flags | O_NONBLOCK) == 0);
|
||||
ssize_t result = 0;
|
||||
for (int i = 0; i < 1024; ++i) {
|
||||
result = sdk_write(descriptors[1], buffer, sizeof(buffer));
|
||||
if (result < 0) break;
|
||||
}
|
||||
assert(result == -1 && (errno == EAGAIN || errno == EWOULDBLOCK));
|
||||
assert(sdk_close(descriptors[0]) == 0);
|
||||
assert(sdk_close(descriptors[1]) == 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void exercise(const char *path) {
|
||||
FILE *file = sdk_open(path, "wb");
|
||||
assert(file != NULL);
|
||||
const unsigned char payload[] = {0, 1, 255, 10};
|
||||
assert(fwrite(payload, 1, sizeof(payload), file) == sizeof(payload));
|
||||
assert(fclose(file) == 0);
|
||||
|
||||
file = sdk_open(path, "ab");
|
||||
assert(file != NULL);
|
||||
assert(fwrite("X", 1, 1, file) == 1);
|
||||
assert(fclose(file) == 0);
|
||||
|
||||
file = sdk_open(path, "rb");
|
||||
assert(file != NULL);
|
||||
unsigned char result[8] = {0};
|
||||
assert(fread(result, 1, sizeof(result), file) == 5);
|
||||
assert(memcmp(result, payload, sizeof(payload)) == 0);
|
||||
assert(result[4] == 'X');
|
||||
assert(feof(file));
|
||||
assert(fclose(file) == 0);
|
||||
|
||||
file = sdk_open(path, "wb");
|
||||
assert(file != NULL);
|
||||
assert(fclose(file) == 0);
|
||||
file = sdk_open(path, "rb");
|
||||
assert(file != NULL);
|
||||
assert(fgetc(file) == EOF);
|
||||
assert(fclose(file) == 0);
|
||||
assert(unlink(path) == 0);
|
||||
|
||||
FILE *system_result = fopen(path, "rb");
|
||||
int system_error = errno;
|
||||
assert(system_result == NULL);
|
||||
errno = 0;
|
||||
FILE *bridge_result = sdk_open(path, "rb");
|
||||
int bridge_error = errno;
|
||||
assert(bridge_result == NULL);
|
||||
assert(bridge_error == system_error && bridge_error == ENOENT);
|
||||
}
|
||||
|
||||
static void *thread_test(void *path) {
|
||||
for (int i = 0; i < 20; ++i) exercise(path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
assert(argc == 2);
|
||||
#ifdef IG_TEST_ALL_BRIDGES
|
||||
exercise_posix();
|
||||
#endif
|
||||
pthread_t threads[4];
|
||||
char paths[4][4096];
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
int length = snprintf(paths[i], sizeof(paths[i]), "%s/文件-%d.bin", argv[1], i);
|
||||
assert(length > 0 && (size_t)length < sizeof(paths[i]));
|
||||
assert(pthread_create(&threads[i], NULL, thread_test, paths[i]) == 0);
|
||||
}
|
||||
for (int i = 0; i < 4; ++i) assert(pthread_join(threads[i], NULL) == 0);
|
||||
puts("file semantics: passed");
|
||||
return 0;
|
||||
}
|
||||
Vendored
+393
@@ -0,0 +1,393 @@
|
||||
/*
|
||||
* hook_device.m — 真机 HOOK 正向验证诊断进程(step11)。
|
||||
*
|
||||
* 目的:在非越狱真机的自身进程内构造四种受控攻击形态,验证 Defender 探针
|
||||
* 在真机环境(真实 dyld、真实内存权限、真实线程/镜像枚举)下能命中对应 reason:
|
||||
* 1. GOT 重绑定(fishhook 同款页权限改写自身 __got 槽)→ 期望 reason=102
|
||||
* 2. 蹦床序言(导出符号首指令为 B / LDR-literal+BR x16)→ 期望 reason=100+101
|
||||
* 3. frida 特征线程名(gum-js-loop)→ 期望 reason=203
|
||||
* 4. 容器外注入 dylib(Documents 下 dlopen;或宿主以 DYLD_INSERT_LIBRARIES 启动)
|
||||
* → 期望 reason=200(+201)(dlopen 路径)/ reason=202(插入库路径)
|
||||
*
|
||||
* 边界(如实声明):
|
||||
* - 不修改任何系统代码页(非越狱不可行):inline-hook 用例针对受控导出符号的
|
||||
* 真实代码字节验证「读取+分类」,不等同于对系统函数的真实 patch。
|
||||
* - 攻击操作(可写 GOT、装载模拟 dylib、特征线程名)只存在于本诊断源码,
|
||||
* 绝不编入业务库;诊断进程结束即退出,不触碰业务 App。
|
||||
*/
|
||||
#import <Foundation/Foundation.h>
|
||||
#include "IGDefender.h"
|
||||
#include "anti_hook/IGAntiHook.h"
|
||||
#include "anti_hook/IGObjCBaseline.h"
|
||||
#include "anti_frida/IGAntiInject.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <mach-o/dyld.h>
|
||||
#include <mach-o/loader.h>
|
||||
#include <mach-o/nlist.h>
|
||||
#include <mach/mach.h>
|
||||
#include <mach/mach_time.h>
|
||||
#include <objc/objc.h>
|
||||
#include <objc/runtime.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* ---------- 发现收集:只记 DETECTED,逐条打日志 ---------- */
|
||||
|
||||
#define IG_MAXF 64
|
||||
static IGFinding g_findings[IG_MAXF];
|
||||
static int g_nfindings;
|
||||
|
||||
static int collect(const IGFinding *f, void *ctx) {
|
||||
(void)ctx;
|
||||
if (f->status == IG_DETECTED && g_nfindings < IG_MAXF)
|
||||
g_findings[g_nfindings++] = *f;
|
||||
NSLog(@"IG_HOOK_FINDING status=%d reason=%d subject=%s evidence=%s",
|
||||
f->status, f->reason, f->subject, f->evidence);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void reset_findings(void) { g_nfindings = 0; }
|
||||
static const IGFinding *first_reason(int reason) {
|
||||
for (int i = 0; i < g_nfindings; ++i)
|
||||
if (g_findings[i].reason == reason) return &g_findings[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *g_bundle; /* 主 bundle 路径,供各探针使用 */
|
||||
|
||||
/* 完整聚合扫描(anti_hook + 注入 + frida 线程)。 */
|
||||
static int scan_all(void) {
|
||||
reset_findings();
|
||||
return ig_defender_scan(g_bundle, 0, collect, NULL);
|
||||
}
|
||||
|
||||
/* ---------- 用例 1:GOT 重绑定(102)----------
|
||||
* fishhook 同款走法:定位主可执行文件 __got 中绑定 getpid 的槽,
|
||||
* 临时以 VM_PROT_COPY 改页为可写,写入本进程函数地址,恢复只读。
|
||||
* 只写自身进程数据页,不改任何代码。 */
|
||||
|
||||
static int ig_rebind_stub(void) { return 4242; } /* 指给 GOT 的假实现 */
|
||||
|
||||
static void **find_main_got_slot(const char *bare_name) {
|
||||
const struct mach_header_64 *mh =
|
||||
(const struct mach_header_64 *)_dyld_get_image_header(0);
|
||||
if (!mh || mh->magic != MH_MAGIC_64) return NULL;
|
||||
intptr_t slide = _dyld_get_image_vmaddr_slide(0);
|
||||
const struct segment_command_64 *linkedit = NULL;
|
||||
const struct symtab_command *symtab = NULL;
|
||||
const struct dysymtab_command *dysymtab = NULL;
|
||||
const uint8_t *cmd = (const uint8_t *)(mh + 1);
|
||||
for (uint32_t i = 0; i < mh->ncmds; ++i, cmd += ((const struct load_command *)cmd)->cmdsize) {
|
||||
const struct load_command *lc = (const struct load_command *)cmd;
|
||||
if (lc->cmd == LC_SEGMENT_64) {
|
||||
const struct segment_command_64 *seg = (const struct segment_command_64 *)lc;
|
||||
if (strcmp(seg->segname, SEG_LINKEDIT) == 0) linkedit = seg;
|
||||
} else if (lc->cmd == LC_SYMTAB) {
|
||||
symtab = (const struct symtab_command *)lc;
|
||||
} else if (lc->cmd == LC_DYSYMTAB) {
|
||||
dysymtab = (const struct dysymtab_command *)lc;
|
||||
}
|
||||
}
|
||||
if (!linkedit || !symtab || !dysymtab) return NULL;
|
||||
uintptr_t le_base = (uintptr_t)slide + linkedit->vmaddr - linkedit->fileoff;
|
||||
const struct nlist_64 *syms = (const struct nlist_64 *)(le_base + symtab->symoff);
|
||||
const char *strtab = (const char *)(le_base + symtab->stroff);
|
||||
const uint32_t *indirect = (const uint32_t *)(le_base + dysymtab->indirectsymoff);
|
||||
size_t nlen = strlen(bare_name);
|
||||
cmd = (const uint8_t *)(mh + 1);
|
||||
for (uint32_t i = 0; i < mh->ncmds; ++i, cmd += ((const struct load_command *)cmd)->cmdsize) {
|
||||
const struct load_command *lc = (const struct load_command *)cmd;
|
||||
if (lc->cmd != LC_SEGMENT_64) continue;
|
||||
const struct segment_command_64 *seg = (const struct segment_command_64 *)lc;
|
||||
const struct section_64 *sect = (const struct section_64 *)(seg + 1);
|
||||
for (uint32_t s = 0; s < seg->nsects; ++s) {
|
||||
if ((sect[s].flags & SECTION_TYPE) != S_NON_LAZY_SYMBOL_POINTERS) continue;
|
||||
size_t n = sect[s].size / sizeof(void *);
|
||||
const uint32_t *idxs = indirect + sect[s].reserved1;
|
||||
void **slots = (void **)((uintptr_t)slide + sect[s].addr);
|
||||
for (size_t k = 0; k < n; ++k) {
|
||||
uint32_t si = idxs[k];
|
||||
if (si == INDIRECT_SYMBOL_ABS || si == INDIRECT_SYMBOL_LOCAL) continue;
|
||||
const char *sym = strtab + syms[si].n_un.n_strx;
|
||||
/* sym 形如 "_getpid":比较去下划线后的部分 */
|
||||
if (sym[0] == '_' && strncmp(sym + 1, bare_name, nlen) == 0 &&
|
||||
sym[1 + nlen] == '\0')
|
||||
return &slots[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* 改写自身 GOT 槽:VM_PROT_COPY 写副本页,写完恢复只读。返回 0 成功。 */
|
||||
static int rebind_got_slot(void **slot, void *to) {
|
||||
vm_address_t page = (vm_address_t)(uintptr_t)slot & ~0xfffull;
|
||||
if (vm_protect(mach_task_self(), page, 0x1000, 0,
|
||||
VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY) != KERN_SUCCESS)
|
||||
return -1;
|
||||
*slot = to;
|
||||
vm_protect(mach_task_self(), page, 0x1000, 0, VM_PROT_READ);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ---------- 用例 2:蹦床序言导出符号(100+101)----------
|
||||
* 两个文件级汇编符号,首指令分别是「无条件 B」与「LDR-literal x16 + BR x16」,
|
||||
* 与 Frida/Dobby 蹦床形态一致。仅被 dlsym 解析和字节读取,从不执行。 */
|
||||
|
||||
asm(".globl _ig_hook_target_b\n"
|
||||
".align 2\n"
|
||||
"_ig_hook_target_b:\n"
|
||||
" b .+64\n"
|
||||
" ret\n");
|
||||
|
||||
asm(".globl _ig_hook_target_lb\n"
|
||||
".align 2\n"
|
||||
"_ig_hook_target_lb:\n"
|
||||
" ldr x16, #8\n"
|
||||
" br x16\n"
|
||||
" .quad 0\n");
|
||||
|
||||
/* ---------- 用例 3:frida 特征线程名(203)---------- */
|
||||
|
||||
static void *gum_thread(void *arg) {
|
||||
(void)arg;
|
||||
pthread_setname_np("gum-js-loop");
|
||||
usleep(600000); /* 确保扫描期间线程存活 */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ---------- 用例 6:ObjC 方法 IMP 基线(110)----------
|
||||
* 运行期创建类并采集基线,class_replaceMethod 换实现后扫描 → 期望 110。 */
|
||||
|
||||
static int ig_objc_imp_original(void *self, SEL _cmd) { (void)self; (void)_cmd; return 1; }
|
||||
static int ig_objc_imp_swapped(void *self, SEL _cmd) { (void)self; (void)_cmd; return 2; }
|
||||
|
||||
static Class make_objc_fixture_class(void) {
|
||||
Class c = objc_allocateClassPair(objc_getClass("NSObject"), "IGHookFixtureTarget", 0);
|
||||
if (!c) return NULL;
|
||||
if (!class_addMethod(c, sel_registerName("value"), (IMP)ig_objc_imp_original, "i16@0:8")) {
|
||||
objc_disposeClassPair(c);
|
||||
return NULL;
|
||||
}
|
||||
objc_registerClassPair(c);
|
||||
return c;
|
||||
}
|
||||
|
||||
/* ---------- 输出协议(宿主 run_hook_device.py 解析)---------- */
|
||||
|
||||
static int g_pass = 0, g_fail = 0;
|
||||
static void mark(const char *name, int pass, const char *detail) {
|
||||
NSLog(@"IG_HOOK_CASE %s %s%s%s", name, pass ? "PASS" : "FAIL",
|
||||
detail && *detail ? " detail=" : "", detail ? detail : "");
|
||||
if (pass) ++g_pass; else ++g_fail;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
(void)argc; (void)argv;
|
||||
@autoreleasepool {
|
||||
NSLog(@"IG_HOOK_MAIN");
|
||||
g_bundle = NSBundle.mainBundle.bundlePath.UTF8String;
|
||||
const char *insert = getenv("DYLD_INSERT_LIBRARIES");
|
||||
|
||||
if (insert && insert[0]) {
|
||||
/* 相位 B:宿主经 DYLD_INSERT_LIBRARIES 启动本进程 → 真实 dyld 注入。 */
|
||||
NSLog(@"IG_HOOK_INSERT_MODE value=%s", insert);
|
||||
int hits = scan_all();
|
||||
int r202 = first_reason(IG_REASON_DYLD_INSERT) != NULL;
|
||||
int r200 = first_reason(IG_REASON_INJECTED_IMAGE) != NULL;
|
||||
NSLog(@"IG_HOOK_INSERT_SUMMARY detected=%d hits=%d reason202=%d reason200=%d",
|
||||
g_nfindings, hits, r202, r200);
|
||||
mark("dyld_insert", r202 || r200,
|
||||
r202 ? "202+环境注入命中" : (r200 ? "200(插入镜像命中特征名)" : "未命中"));
|
||||
NSLog(@"IG_HOOK_SUITE %s cases=1", g_fail ? "FAIL" : "PASS");
|
||||
return g_fail ? 4 : 0;
|
||||
}
|
||||
|
||||
/* ---- 相位 A:干净进程内的受控攻击形态 ---- */
|
||||
|
||||
/* 0. 基线:干净环境 0 命中。 */
|
||||
int base = scan_all();
|
||||
int base_unavail = 0; /* unavailable 已在 sink 日志逐条体现,不判失败 */
|
||||
NSLog(@"IG_HOOK_BASELINE detected=%d", base);
|
||||
mark("baseline_clean", g_nfindings == 0,
|
||||
g_nfindings ? "干净环境出现 DETECTED(误报)" : "");
|
||||
|
||||
/* 1. 耗时小样本(5 次完整聚合扫描)。 */
|
||||
{
|
||||
mach_timebase_info_data_t tb;
|
||||
mach_timebase_info(&tb);
|
||||
double scale = (double)tb.numer / (double)tb.denom / 1e6; /* → ms */
|
||||
double total = 0, best = 1e9;
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
uint64_t t0 = mach_absolute_time();
|
||||
scan_all();
|
||||
double ms = (mach_absolute_time() - t0) * scale;
|
||||
total += ms;
|
||||
if (ms < best) best = ms;
|
||||
}
|
||||
NSLog(@"IG_HOOK_TIMING n=5 avg_ms=%.2f min_ms=%.2f", total / 5.0, best);
|
||||
}
|
||||
|
||||
/* 2. GOT 重绑定 → 102;恢复 → 干净。 */
|
||||
{
|
||||
/* 确保可执行文件真实导入 getpid(否则 __got 无该槽)。 */
|
||||
if (getpid() == -7) NSLog(@"unreachable");
|
||||
void **slot = find_main_got_slot("getpid");
|
||||
if (!slot) {
|
||||
mark("got_rebind", 0, "未找到主程序 getpid 的 __got 槽(构建异常)");
|
||||
} else {
|
||||
void *orig = *slot;
|
||||
NSLog(@"IG_HOOK_GOT slot=%p orig=%p", (void *)slot, orig);
|
||||
if (rebind_got_slot(slot, (void *)ig_rebind_stub) != 0) {
|
||||
mark("got_rebind", 0, "vm_protect 改写失败");
|
||||
} else {
|
||||
scan_all();
|
||||
const IGFinding *f = first_reason(IG_REASON_GOT_REBIND);
|
||||
int hit = f && strstr(f->subject, "getpid") != NULL;
|
||||
mark("got_rebind", hit, f ? f->evidence : "未命中 102");
|
||||
rebind_got_slot(slot, orig); /* 恢复,双写均经同一权限路径 */
|
||||
int again = scan_all();
|
||||
int clean = g_nfindings == 0;
|
||||
NSLog(@"IG_HOOK_GOT_RESTORE detected=%d hits=%d", g_nfindings, again);
|
||||
if (!clean)
|
||||
mark("got_restore_clean", 0, "恢复后仍有 DETECTED");
|
||||
else
|
||||
mark("got_restore_clean", 1, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 3. 蹦床序言(经 dlsym+vm_read_overwrite 的完整探针链)→ 100+101。 */
|
||||
{
|
||||
const char *const names[] = {"ig_hook_target_b", "ig_hook_target_lb"};
|
||||
reset_findings();
|
||||
ig_anti_hook_check(names, 2, collect, NULL);
|
||||
int b100 = 0, b101 = 0, lb100 = 0, lb101 = 0;
|
||||
for (int i = 0; i < g_nfindings; ++i) {
|
||||
const char *subj = g_findings[i].subject;
|
||||
if (strstr(subj, "ig_hook_target_b")) {
|
||||
b100 |= g_findings[i].reason == IG_REASON_SYMBOL_PROVENANCE;
|
||||
b101 |= g_findings[i].reason == IG_REASON_INLINE_HOOK;
|
||||
} else if (strstr(subj, "ig_hook_target_lb")) {
|
||||
lb100 |= g_findings[i].reason == IG_REASON_SYMBOL_PROVENANCE;
|
||||
lb101 |= g_findings[i].reason == IG_REASON_INLINE_HOOK;
|
||||
}
|
||||
}
|
||||
mark("prologue_b_branch", b100 && b101, b101 ? "首指令 B 被识别" : "未命中 101");
|
||||
mark("prologue_ldr_br", lb100 && lb101, lb101 ? "LDR+BR 蹦床被识别" : "未命中 101");
|
||||
}
|
||||
|
||||
/* 4. frida 特征线程名 → 203;join 后 → 干净。
|
||||
* join 后内核回收线程对象存在延迟(task_threads 短期内仍可能列出已终止
|
||||
* 线程),在 2s 窗口内轮询重扫:既能容忍回收延迟,又验证探针无粘滞缓存。 */
|
||||
{
|
||||
pthread_t t;
|
||||
pthread_create(&t, NULL, gum_thread, NULL);
|
||||
usleep(200000); /* 等线程命名完成 */
|
||||
reset_findings();
|
||||
ig_anti_frida_check(0, collect, NULL);
|
||||
mark("frida_thread", first_reason(IG_REASON_FRIDA_THREAD) != NULL, "");
|
||||
pthread_join(t, NULL);
|
||||
int clean = 0;
|
||||
for (int i = 0; i < 10 && !clean; ++i) {
|
||||
usleep(200000);
|
||||
reset_findings();
|
||||
ig_anti_frida_check(0, collect, NULL);
|
||||
clean = (g_nfindings == 0);
|
||||
}
|
||||
mark("frida_thread_gone", clean,
|
||||
clean ? "join 后 2s 内恢复干净" : "join 后 2s 仍残留线程名(回收异常缓慢)");
|
||||
}
|
||||
|
||||
/* 5. 注入证据两种形态:
|
||||
* a) Documents/libfrida-sim.dylib —— 文件名命中注入特征 → 期望 200;
|
||||
* (探针对命中特征的镜像即上报 200,不再重复报 Bundle 外,故不期望 201)
|
||||
* b) Documents/libiosutil-sim.dylib —— 无特征命名的 Bundle 外镜像 → 期望 201。
|
||||
* 相位 A 成功路径回显给宿主,供相位 B 的 DYLD_INSERT 使用。 */
|
||||
{
|
||||
NSString *res = [[NSBundle mainBundle] pathForResource:@"libfrida-sim"
|
||||
ofType:@"dylib"];
|
||||
NSString *docs = [NSHomeDirectory()
|
||||
stringByAppendingPathComponent:@"Documents"];
|
||||
NSString *dstA = [docs stringByAppendingPathComponent:@"libfrida-sim.dylib"];
|
||||
NSString *dstB = [docs stringByAppendingPathComponent:@"libiosutil-sim.dylib"];
|
||||
const char *chosen = NULL;
|
||||
NSFileManager *fm = [NSFileManager defaultManager];
|
||||
if (res) {
|
||||
[fm removeItemAtPath:dstA error:nil];
|
||||
[fm removeItemAtPath:dstB error:nil];
|
||||
if ([fm copyItemAtPath:res toPath:dstA error:nil] &&
|
||||
[fm copyItemAtPath:res toPath:dstB error:nil]) {
|
||||
void *ha = dlopen(dstA.fileSystemRepresentation, RTLD_LOCAL);
|
||||
if (ha) {
|
||||
chosen = dstA.fileSystemRepresentation;
|
||||
reset_findings();
|
||||
ig_anti_inject_check(g_bundle, collect, NULL);
|
||||
mark("injected_image",
|
||||
first_reason(IG_REASON_INJECTED_IMAGE) != NULL,
|
||||
"Documents 特征名装载命中 200");
|
||||
} else {
|
||||
const char *e = dlerror();
|
||||
NSLog(@"IG_HOOK_DLOPEN_DOC_FAIL %s", e ? e : "?");
|
||||
}
|
||||
void *hb = dlopen(dstB.fileSystemRepresentation, RTLD_LOCAL);
|
||||
if (hb) {
|
||||
reset_findings();
|
||||
ig_anti_inject_check(g_bundle, collect, NULL);
|
||||
mark("foreign_image",
|
||||
first_reason(IG_REASON_FOREIGN_IMAGE) != NULL,
|
||||
"Documents 无特征名装载命中 201(弱信号)");
|
||||
}
|
||||
} else {
|
||||
NSLog(@"IG_HOOK_COPY_FAIL %@", docs);
|
||||
}
|
||||
}
|
||||
if (!chosen) {
|
||||
/* 回退:从 .app 内 dlopen(Bundle 内只应命中 200)。 */
|
||||
void *h = dlopen(res.fileSystemRepresentation, RTLD_LOCAL);
|
||||
if (h) {
|
||||
chosen = res.fileSystemRepresentation;
|
||||
reset_findings();
|
||||
ig_anti_inject_check(g_bundle, collect, NULL);
|
||||
int r200 = first_reason(IG_REASON_INJECTED_IMAGE) != NULL;
|
||||
int r201 = first_reason(IG_REASON_FOREIGN_IMAGE) != NULL;
|
||||
mark("injected_image", r200 && !r201,
|
||||
"容器外装载不可用,回退 Bundle 内(200 命中、201 不应命中)");
|
||||
} else {
|
||||
const char *e = dlerror();
|
||||
mark("injected_image", 0, e ? e : "装载失败");
|
||||
}
|
||||
}
|
||||
NSLog(@"IG_HOOK_DYLIB_PATH %s", chosen ? chosen : "none");
|
||||
}
|
||||
|
||||
/* 6. ObjC IMP 基线:采集 → class_replaceMethod → 期望 110;未接入时聚合为 no-op。 */
|
||||
{
|
||||
Class c = make_objc_fixture_class();
|
||||
if (!c) {
|
||||
mark("objc_imp_baseline", 0, "运行期建类失败");
|
||||
} else {
|
||||
const IGObjCWatch watch[] = { { "IGHookFixtureTarget", "value" } };
|
||||
size_t n = ig_objc_baseline_capture(watch, 1);
|
||||
reset_findings();
|
||||
int before = ig_objc_baseline_check(collect, NULL);
|
||||
class_replaceMethod(c, sel_registerName("value"),
|
||||
(IMP)ig_objc_imp_swapped, "i16@0:8");
|
||||
reset_findings();
|
||||
int after = ig_objc_baseline_check(collect, NULL);
|
||||
mark("objc_imp_baseline", n == 1 && before == 0 && after == 1 &&
|
||||
first_reason(IG_REASON_OBJC_IMP_CHANGED) != NULL,
|
||||
after == 1 ? "替换后命中 110" : "未命中 110");
|
||||
}
|
||||
}
|
||||
|
||||
NSLog(@"IG_HOOK_SUITE %s cases=%d baseline_unavailable_logged=%d",
|
||||
g_fail ? "FAIL" : "PASS", g_pass + g_fail, base_unavail);
|
||||
/* 诊断进程退出便于 console 收集退出码;不编入业务 App。 */
|
||||
return g_fail ? 4 : 0;
|
||||
}
|
||||
}
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* hook_sim_dylib.c — 空壳模拟 dylib(step11 诊断专用)。
|
||||
*
|
||||
* 唯一用途:被诊断进程 dlopen / 被 DYLD_INSERT_LIBRARIES 加载,构成
|
||||
* 「注入镜像 / Bundle 外镜像 / 插入库」探针的受控证据源。不实现任何业务功能,
|
||||
* 不导出会被业务引用的符号。仅用于诊断 App,绝不编入业务库。
|
||||
*/
|
||||
#include <stddef.h>
|
||||
|
||||
__attribute__((visibility("default")))
|
||||
int ig_frida_sim_marker(void) { return 0x46726461; /* "Frda" */ }
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
/* 无 SDK 头依赖的 iOS 链接样本:两个外部调用和一个本地改写目标。 */
|
||||
extern int getpid(void);
|
||||
extern long write(int, const void *, unsigned long);
|
||||
int fixture_calls(void) { return getpid() + (int)write(-1, "x", 1); }
|
||||
int fixture_redirect(void) { return 123; }
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
FILE *sdk_open(const char *path, const char *mode) { return fopen(path, mode); }
|
||||
int sdk_fclose(FILE *stream) { return fclose(stream); }
|
||||
size_t sdk_fread(void *buffer, size_t size, size_t count, FILE *stream) {
|
||||
return fread(buffer, size, count, stream);
|
||||
}
|
||||
size_t sdk_fwrite(const void *buffer, size_t size, size_t count, FILE *stream) {
|
||||
return fwrite(buffer, size, count, stream);
|
||||
}
|
||||
ssize_t sdk_read(int fd, void *buffer, size_t count) { return read(fd, buffer, count); }
|
||||
ssize_t sdk_write(int fd, const void *buffer, size_t count) { return write(fd, buffer, count); }
|
||||
int sdk_close(int fd) { return close(fd); }
|
||||
Reference in New Issue
Block a user