Files
iOSGuard/defender/anti_hook/IGObjCBaseline.c
T
shenlei 704ee66834 feat: 集成 LLVM IR 混淆体系、Route 2 全量合规与系统调用调度加固
- 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
2026-11-24 15:00:07 +09:00

276 lines
9.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* IGObjCBaseline.c — ObjC 关键方法 IMP 基线探针实现。
*
* 全部公开 API:objc_getClass / sel_registerName / class_getInstanceMethod /
* class_getClassMethod / method_getImplementation / class_getImageName
* (objc/runtime.h,纯 C)、dladdr(提供方)、vm_read_overwrite(代码前缀)。
*
* 特性:
* - 同时支持实例方法(-)与类方法(+);
* - 前缀快照扩大到 32 字节(4 字节对齐读取,兼容最小 16 字节回退);
* - 支持早晚双段登记(C ctor + Swift 运行期追加去重 ig_objc_baseline_append);
* - 登记瞬间同步检测「前置劫持」(IMP 已落入非声明宿主镜像,或函数窗已被改为跳转蹦床)。
*/
#include "IGObjCBaseline.h"
#include "IGAntiHook.h"
#include "IGInline.h"
#if IG_DIAGNOSTIC_STRINGS
# include <stdio.h>
#endif
#include <dlfcn.h>
#include <mach/mach.h>
#include <objc/objc.h>
#include <objc/runtime.h>
#if defined(__has_feature)
# if __has_feature(ptrauth_calls)
# include <ptrauth.h>
# endif
#endif
#define IG_OBJC_MAX_WATCH 64u
#define IG_PREFIX_BYTES 32u
#define IG_PREFIX_FALLBACK 16u
typedef struct {
const char *cls;
const char *sel;
uint8_t is_cls_method;
uint8_t pre_hooked;
void *imp;
char prov[192];
unsigned char prefix[IG_PREFIX_BYTES];
size_t prefix_len;
} IGObjCSlot;
static IGObjCSlot g_slots[IG_OBJC_MAX_WATCH];
static size_t g_nslots = 0;
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;
ig_memset(&f, 0, sizeof(f));
f.status = st;
f.reason = IG_REASON_OBJC_IMP_CHANGED;
#if IG_DIAGNOSTIC_STRINGS
if (subject) {
size_t n = ig_strlen(subject);
if (n >= sizeof(f.subject)) n = sizeof(f.subject) - 1u;
ig_memcpy(f.subject, subject, n);
}
if (evidence) {
size_t n = ig_strlen(evidence);
if (n >= sizeof(f.evidence)) n = sizeof(f.evidence) - 1u;
ig_memcpy(f.evidence, evidence, n);
}
#else
(void)subject;
(void)evidence;
#endif
if (st == IG_DETECTED) (*hits)++;
if (sink && sink(&f, ctx) != 0) *stop = 1;
}
static size_t ig_read_prefix(const void *addr, unsigned char *out) {
vm_size_t copied = 0;
vm_address_t target = (vm_address_t)ig_strip(addr);
if (vm_read_overwrite(mach_task_self(), target,
IG_PREFIX_BYTES, (vm_address_t)out, &copied) == KERN_SUCCESS &&
copied == IG_PREFIX_BYTES) {
return IG_PREFIX_BYTES;
}
copied = 0;
if (vm_read_overwrite(mach_task_self(), target,
IG_PREFIX_FALLBACK, (vm_address_t)out, &copied) == KERN_SUCCESS &&
copied == IG_PREFIX_FALLBACK) {
return IG_PREFIX_FALLBACK;
}
return 0u;
}
static Method ig_resolve_method(Class c, const char *raw_sel, uint8_t *is_cls_out, const char **bare_sel_out) {
const char *bare = raw_sel;
if (bare[0] == '+' || bare[0] == '-') {
int want_cls = (bare[0] == '+');
++bare;
if (!bare[0]) return NULL;
*bare_sel_out = bare;
*is_cls_out = want_cls ? 1u : 0u;
SEL s = sel_registerName(bare);
return want_cls ? class_getClassMethod(c, s) : class_getInstanceMethod(c, s);
}
*bare_sel_out = bare;
SEL s = sel_registerName(bare);
Method m = class_getInstanceMethod(c, s);
if (m) {
*is_cls_out = 0u;
return m;
}
m = class_getClassMethod(c, s);
if (m) {
*is_cls_out = 1u;
return m;
}
return NULL;
}
static int ig_slot_exists(const char *cls, const char *bare_sel, uint8_t is_cls) {
for (size_t i = 0; i < g_nslots; ++i) {
if (g_slots[i].is_cls_method == is_cls &&
ig_strcmp(g_slots[i].cls, cls) == 0 &&
ig_strcmp(g_slots[i].sel, bare_sel) == 0) {
return 1;
}
}
return 0;
}
size_t ig_objc_baseline_append(const IGObjCWatch *watches, size_t count) {
if (!watches) return 0u;
size_t added = 0u;
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;
uint8_t is_cls = 0u;
const char *bare_sel = NULL;
Method m = ig_resolve_method(c, watches[i].selector, &is_cls, &bare_sel);
if (!m) continue;
if (ig_slot_exists(watches[i].class_name, bare_sel, is_cls)) continue;
IMP imp = method_getImplementation(m);
if (!imp) continue;
const void *stripped = ig_strip((const void *)imp);
IGObjCSlot *slot = &g_slots[g_nslots++];
ig_memset(slot, 0, sizeof(*slot));
slot->cls = watches[i].class_name;
slot->sel = bare_sel;
slot->is_cls_method = is_cls;
slot->imp = (void *)stripped;
Dl_info info;
if (dladdr(stripped, &info) && info.dli_fname) {
size_t plen = ig_strlen(info.dli_fname);
if (plen >= sizeof(slot->prov)) plen = sizeof(slot->prov) - 1u;
ig_memcpy(slot->prov, info.dli_fname, plen);
}
const char *cls_img = class_getImageName(c);
if (cls_img && slot->prov[0] && ig_strcmp(cls_img, slot->prov) != 0) {
slot->pre_hooked = 1u;
}
slot->prefix_len = ig_read_prefix(stripped, slot->prefix);
if (slot->prefix_len >= 8u) {
uint32_t words[IG_PREFIX_BYTES / sizeof(uint32_t)];
size_t nwords = slot->prefix_len / sizeof(uint32_t);
ig_memcpy(words, slot->prefix, nwords * sizeof(uint32_t));
if (ig_arm64_suspicious_window(words, nwords)) {
slot->pre_hooked = 1u;
}
}
++added;
}
return added;
}
size_t ig_objc_baseline_capture(const IGObjCWatch *watches, size_t count) {
g_nslots = 0u;
if (!watches || count == 0u) return 0u;
return ig_objc_baseline_append(watches, count);
}
size_t ig_objc_baseline_count(void) {
return g_nslots;
}
int ig_objc_baseline_check(IGFindingSink sink, void *ctx) {
if (g_nslots == 0u) return 0;
int hits = 0, stop = 0;
#if IG_DIAGNOSTIC_STRINGS
char subject[192], ev[256];
#else
const char *subject = NULL;
#endif
for (size_t i = 0; i < g_nslots && !stop; ++i) {
const IGObjCSlot *b = &g_slots[i];
#if IG_DIAGNOSTIC_STRINGS
snprintf(subject, sizeof(subject), "%c[%s %s]", b->is_cls_method ? '+' : '-', b->cls, b->sel);
#endif
if (b->pre_hooked) {
#if IG_DIAGNOSTIC_STRINGS
snprintf(ev, sizeof(ev), "基线捕获时已异常(%s @%p)", b->prov[0] ? b->prov : "?", b->imp);
ig_emit_objc(sink, ctx, &stop, &hits, IG_DETECTED, subject, ev);
#else
ig_emit_objc(sink, ctx, &stop, &hits, IG_DETECTED, subject, NULL);
#endif
continue;
}
Class c = objc_getClass(b->cls);
if (!c) {
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject,
IG_DIAG_STR("基线登记后类不可解析(被卸载或运行时异常)"));
continue;
}
SEL s = sel_registerName(b->sel);
Method m = b->is_cls_method ? class_getClassMethod(c, s) : class_getInstanceMethod(c, s);
if (!m || !method_getImplementation(m)) {
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject,
IG_DIAG_STR("方法实现不可解析(转发桩或已移除)"));
continue;
}
IMP imp = method_getImplementation(m);
const void *cur = ig_strip((const void *)imp);
if (cur != b->imp) {
#if IG_DIAGNOSTIC_STRINGS
char cur_prov[192] = "?";
Dl_info info;
if (dladdr(cur, &info) && info.dli_fname) {
size_t plen = ig_strlen(info.dli_fname);
if (plen >= sizeof(cur_prov)) plen = sizeof(cur_prov) - 1u;
ig_memcpy(cur_prov, info.dli_fname, plen);
cur_prov[plen] = '\0';
}
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);
#else
ig_emit_objc(sink, ctx, &stop, &hits, IG_DETECTED, subject, NULL);
#endif
continue;
}
if (b->prefix_len > 0u) {
unsigned char now[IG_PREFIX_BYTES];
size_t now_len = ig_read_prefix(cur, now);
if (now_len < b->prefix_len) {
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject,
IG_DIAG_STR("代码前缀不可读"));
continue;
}
if (ig_memcmp(now, b->prefix, b->prefix_len) != 0) {
#if IG_DIAGNOSTIC_STRINGS
snprintf(ev, sizeof(ev), "IMP %p 不变但代码前缀变化(函数体被改写)", cur);
ig_emit_objc(sink, ctx, &stop, &hits, IG_DETECTED, subject, ev);
#else
ig_emit_objc(sink, ctx, &stop, &hits, IG_DETECTED, subject, NULL);
#endif
}
}
}
return hits;
}