- 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
181 lines
8.5 KiB
C
181 lines
8.5 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[512]; int detected; int unavailable; } Findings;
|
|
static int collect(const IGFinding *f, void *context) {
|
|
Findings *out = context;
|
|
if (f->reason >= 0 && f->reason < 512) 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 existing = objc_getClass("IGFixtureHookTarget");
|
|
if (existing) return existing;
|
|
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;
|
|
}
|
|
Class meta = object_getClass((id)c);
|
|
if (!meta || !class_addMethod(meta, sel_registerName("classValue"), (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);
|
|
/* 越狱文件探针(400/401)在 macOS 宿主必然命中:mac 自带完整 Unix userland
|
|
* (/usr/sbin/sshd、/bin/bash、/bin/sh…),按 iOS 标准宿主本身即“越狱”。
|
|
* 这是宿主/iOS 环境差异,非真实误报——iOS 非越狱设备上这些路径不存在/沙盒不可达,
|
|
* 其反向零命中由真机 R 组验证(docs/19)。故宿主 clean 只对非越狱探针断言零 DETECTED。 */
|
|
int jb = f.reasons[IG_REASON_JAILBREAK_FILE] + f.reasons[IG_REASON_JAILBREAK_SANDBOX];
|
|
int non_jb_detected = f.detected - jb;
|
|
return hits!=f.detected || non_jb_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;
|
|
const IGObjCWatch cls_watch[] = { { "IGFixtureHookTarget", "classValue" } };
|
|
if (ig_objc_baseline_append(cls_watch,1)!=1 || ig_objc_baseline_count()!=2) return 12;
|
|
if (ig_objc_baseline_append(watch,1)!=0 || ig_objc_baseline_count()!=2) return 13;
|
|
/* 未登记的方法在 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);
|
|
ig_objc_baseline_append(cls_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" },
|
|
{ "IGFixtureHookTarget", "+classValue" }
|
|
};
|
|
if (ig_objc_baseline_capture(watch,2)!=2) return 10;
|
|
/* class_replaceMethod:实例方法与类方法同时换人 → 期望 DETECTED(110) x2。 */
|
|
class_replaceMethod(c, sel_registerName("value"), (IMP)imp_replaced, "i16@0:8");
|
|
class_replaceMethod(object_getClass((id)c), sel_registerName("classValue"), (IMP)imp_replaced, "i16@0:8");
|
|
int hits=ig_objc_baseline_check(collect,&f);
|
|
return hits!=2 || f.reasons[IG_REASON_OBJC_IMP_CHANGED]!=2;
|
|
}
|
|
return 2;
|
|
}
|