在 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>
147 lines
5.7 KiB
C
147 lines
5.7 KiB
C
/*
|
||
* IGObjCBaseline.c — ObjC 关键方法 IMP 基线探针实现。
|
||
*
|
||
* 全部公开 API:objc_getClass / sel_registerName / class_getMethodImplementation
|
||
* (objc/runtime.h,纯 C)、dladdr(提供方)、vm_read_overwrite(代码前缀)。
|
||
* method_getImplementation 返回"当前会被实际调用的实现"(已解析 swizzle),
|
||
* 与基线不一致即说明方法被交换/替换——这正是要抓的形态。
|
||
*
|
||
* 线程模型:capture 由策略层在确定时点(如首帧后)调用一次;check 在扫描时点调用。
|
||
* 两者并发属策略层误用,本模块不加锁(保持零依赖、可嵌入任意 target)。
|
||
*/
|
||
#include "IGObjCBaseline.h"
|
||
#include "IGInline.h"
|
||
|
||
#include <stdio.h>
|
||
#include <string.h>
|
||
#include <dlfcn.h>
|
||
#include <mach/mach.h>
|
||
#include <objc/objc.h>
|
||
#include <objc/runtime.h>
|
||
|
||
|
||
#define IG_OBJC_MAX_WATCH 64
|
||
#define IG_PREFIX_BYTES 16
|
||
|
||
typedef struct {
|
||
const char *cls; /* 登记时的类名 */
|
||
const char *sel; /* 登记时的选择子 */
|
||
void *imp; /* 基线 IMP(去 PAC 后) */
|
||
char prov[192]; /* 基线 IMP 提供方镜像路径 */
|
||
unsigned char prefix[IG_PREFIX_BYTES];
|
||
int have_prefix;
|
||
} IGObjCSlot;
|
||
|
||
static IGObjCSlot g_slots[IG_OBJC_MAX_WATCH];
|
||
static size_t g_nslots;
|
||
|
||
static const void *ig_strip(const void *p) {
|
||
#if defined(__has_feature)
|
||
# if __has_feature(ptrauth_calls)
|
||
return ptrauth_strip(p, ptrauth_key_asia);
|
||
# else
|
||
return p;
|
||
# endif
|
||
#else
|
||
return p;
|
||
#endif
|
||
}
|
||
|
||
static void ig_emit_objc(IGFindingSink sink, void *ctx, int *stop, int *hits,
|
||
IGStatus st, const char *subject, const char *evidence) {
|
||
if (*stop) return;
|
||
IGFinding f;
|
||
memset(&f, 0, sizeof(f));
|
||
f.status = st;
|
||
f.reason = IG_REASON_OBJC_IMP_CHANGED;
|
||
strncpy(f.subject, subject, sizeof(f.subject) - 1);
|
||
strncpy(f.evidence, evidence, sizeof(f.evidence) - 1);
|
||
if (st == IG_DETECTED) (*hits)++;
|
||
if (sink && sink(&f, ctx) != 0) *stop = 1;
|
||
}
|
||
|
||
/* 读取函数前 N 字节;失败返回 0(调用方按 unavailable 语义处理)。 */
|
||
static int ig_read_prefix(const void *addr, unsigned char *out) {
|
||
vm_size_t copied = 0;
|
||
if (vm_read_overwrite(mach_task_self(), (vm_address_t)ig_strip(addr),
|
||
IG_PREFIX_BYTES, (vm_address_t)out, &copied) != KERN_SUCCESS
|
||
|| copied != IG_PREFIX_BYTES)
|
||
return 0;
|
||
return 1;
|
||
}
|
||
|
||
size_t ig_objc_baseline_capture(const IGObjCWatch *watches, size_t count) {
|
||
g_nslots = 0;
|
||
if (!watches) return 0;
|
||
size_t ok = 0;
|
||
for (size_t i = 0; i < count && g_nslots < IG_OBJC_MAX_WATCH; ++i) {
|
||
if (!watches[i].class_name || !watches[i].selector) continue;
|
||
Class c = objc_getClass(watches[i].class_name);
|
||
if (!c) continue;
|
||
Method m = class_getInstanceMethod(c, sel_registerName(watches[i].selector));
|
||
if (!m) continue; /* 类存在但方法不可解析:不登记(check 语义与登记时一致) */
|
||
IMP imp = method_getImplementation(m);
|
||
if (!imp) continue;
|
||
IGObjCSlot *slot = &g_slots[g_nslots++];
|
||
slot->cls = watches[i].class_name;
|
||
slot->sel = watches[i].selector;
|
||
slot->imp = (void *)imp;
|
||
slot->prov[0] = '\0';
|
||
Dl_info info;
|
||
if (dladdr((void *)imp, &info) && info.dli_fname)
|
||
strncpy(slot->prov, info.dli_fname, sizeof(slot->prov) - 1);
|
||
slot->have_prefix = ig_read_prefix((void *)imp, slot->prefix);
|
||
++ok;
|
||
}
|
||
return ok;
|
||
}
|
||
|
||
int ig_objc_baseline_check(IGFindingSink sink, void *ctx) {
|
||
if (g_nslots == 0) return 0; /* 未接入:clear no-op */
|
||
int hits = 0, stop = 0;
|
||
char subject[192], ev[256];
|
||
for (size_t i = 0; i < g_nslots && !stop; ++i) {
|
||
const IGObjCSlot *b = &g_slots[i];
|
||
snprintf(subject, sizeof(subject), "-[%s %s]", b->cls, b->sel);
|
||
Class c = objc_getClass(b->cls);
|
||
if (!c) {
|
||
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject,
|
||
"基线登记后类不可解析(被卸载或运行时异常)");
|
||
continue;
|
||
}
|
||
Method m = class_getInstanceMethod(c, sel_registerName(b->sel));
|
||
if (!m || !method_getImplementation(m)) {
|
||
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject,
|
||
"方法实现不可解析(转发桩或已移除)");
|
||
continue;
|
||
}
|
||
IMP imp = method_getImplementation(m);
|
||
const void *cur = ig_strip(imp);
|
||
if (cur != b->imp) {
|
||
/* IMP 换人:典型 swizzle / class_replaceMethod / exchange。 */
|
||
char cur_prov[192] = "?";
|
||
Dl_info info;
|
||
if (dladdr((void *)cur, &info) && info.dli_fname)
|
||
strncpy(cur_prov, info.dli_fname, sizeof(cur_prov) - 1);
|
||
snprintf(ev, sizeof(ev), "IMP %p(%s) → %p(%s)(实现被替换)",
|
||
b->imp, b->prov[0] ? b->prov : "?", cur, cur_prov);
|
||
ig_emit_objc(sink, ctx, &stop, &hits, IG_DETECTED, subject, ev);
|
||
continue;
|
||
}
|
||
/* IMP 未变但代码前缀变:实现函数体被 inline patch。 */
|
||
if (b->have_prefix) {
|
||
unsigned char now[IG_PREFIX_BYTES];
|
||
if (!ig_read_prefix((void *)cur, now)) {
|
||
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject,
|
||
"代码前缀不可读");
|
||
continue;
|
||
}
|
||
if (memcmp(now, b->prefix, IG_PREFIX_BYTES) != 0) {
|
||
snprintf(ev, sizeof(ev), "IMP %p 不变但代码前缀变化(函数体被改写)", cur);
|
||
ig_emit_objc(sink, ctx, &stop, &hits, IG_DETECTED, subject, ev);
|
||
}
|
||
}
|
||
}
|
||
return hits;
|
||
}
|