- 构建期生成随包签名的 IGMethods.bin(仅主程序),运行时按镜像 UUID、 入口偏移与函数边界内指令摘要校验;导出审计复核清单与最终镜像一致 - 系统函数、调度与解包保护的代码前缀按 LC_FUNCTION_STARTS 限定在函数边界内 - 边界内识别 B / LDR+BR / ADRP+ADD+BR / MOVK+BR 蹦床并确认目标归属,避免系统尾调用误报 - 登记前 IMP 已落入匿名内存判为改写;清单读取改走 IG_dispatch_open - 函数边界长度按入口缓存;镜像未重新链接时复用基线;Debug 附加调试器时不按入口摘要判定改写 - 补充宿主测试;删除已过时的 README 与 docs 历史文档,保留 THIRD_PARTY_NOTICES
302 lines
11 KiB
C
302 lines
11 KiB
C
/*
|
||
* 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(代码前缀)。
|
||
*
|
||
* 特性:
|
||
* - 同时支持实例方法(-)与类方法(+);
|
||
* - 从随包签名的基线校验镜像、方法入口与函数边界内的指令。
|
||
*/
|
||
#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>
|
||
#include "IGMethodManifest.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
|
||
|
||
typedef struct {
|
||
const char *cls;
|
||
const char *sel;
|
||
uint8_t is_cls_method;
|
||
uint8_t pre_hooked;
|
||
void *imp;
|
||
unsigned char digest[CC_SHA256_DIGEST_LENGTH];
|
||
size_t prefix_len;
|
||
} IGObjCSlot;
|
||
|
||
static IGObjCSlot g_slots[IG_OBJC_MAX_WATCH];
|
||
static size_t g_nslots = 0;
|
||
static IGObjCWatch g_unresolved[IG_OBJC_MAX_WATCH];
|
||
static size_t g_unresolved_count;
|
||
|
||
static void ig_set_unresolved(IGObjCWatch watch, int missing) {
|
||
for (size_t i = 0; i < g_unresolved_count; ++i) {
|
||
if (ig_strcmp(watch.class_name, g_unresolved[i].class_name) == 0 &&
|
||
ig_strcmp(watch.selector, g_unresolved[i].selector) == 0) {
|
||
if (!missing) g_unresolved[i] = g_unresolved[--g_unresolved_count];
|
||
return;
|
||
}
|
||
}
|
||
if (missing && g_unresolved_count < IG_OBJC_MAX_WATCH)
|
||
g_unresolved[g_unresolved_count++] = watch;
|
||
}
|
||
|
||
#ifdef DEBUG
|
||
#include <sys/sysctl.h>
|
||
/* 仅 Debug:lldb 软断点会把入口指令改成 brk,构建摘要必然不一致。
|
||
* 调试器附加时不据摘要判定改写;IMP 替换与登记前替换仍照常检测。 */
|
||
static int ig_objc_debugger_attached(void) {
|
||
pid_t pid = IG_dispatch_getpid();
|
||
if (pid <= 0) return 0;
|
||
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
|
||
struct kinfo_proc info;
|
||
size_t size = sizeof(info);
|
||
ig_memset(&info, 0, sizeof(info));
|
||
return sysctl(mib, 4, &info, &size, NULL, 0) == 0 && (info.kp_proc.p_flag & P_TRACED) != 0;
|
||
}
|
||
#endif
|
||
|
||
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, size_t length) {
|
||
vm_size_t copied = 0;
|
||
if (!length || length > IG_PREFIX_BYTES) return 0;
|
||
if (vm_read_overwrite(mach_task_self(), (vm_address_t)ig_strip(addr), length,
|
||
(vm_address_t)out, &copied) == KERN_SUCCESS && copied == length)
|
||
return length;
|
||
return 0;
|
||
}
|
||
|
||
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);
|
||
uint8_t is_cls = 0u;
|
||
const char *bare_sel = NULL;
|
||
Method m = c ? ig_resolve_method(c, watches[i].selector, &is_cls, &bare_sel) : NULL;
|
||
IMP imp = m ? method_getImplementation(m) : NULL;
|
||
ig_set_unresolved(watches[i], imp == NULL);
|
||
if (!imp) continue;
|
||
if (ig_slot_exists(watches[i].class_name, bare_sel, is_cls)) 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;
|
||
|
||
const IGMethodRecord *expected = ig_expected_method(c, sel_registerName(bare_sel), is_cls, m);
|
||
Dl_info info;
|
||
if (expected && expected->length && expected->length % 4 == 0 && expected->length <= IG_PREFIX_BYTES) {
|
||
const uint8_t *uuid = dladdr(stripped, &info) ? ig_method_image_uuid(info.dli_fbase) : NULL;
|
||
if (!uuid) {
|
||
slot->pre_hooked = 1u;
|
||
} else {
|
||
uintptr_t offset = (uintptr_t)stripped - (uintptr_t)info.dli_fbase;
|
||
slot->pre_hooked = ig_memcmp(uuid, expected->uuid, 16) != 0 ||
|
||
offset != expected->offset;
|
||
slot->prefix_len = expected->length;
|
||
ig_memcpy(slot->digest, expected->digest, sizeof(slot->digest));
|
||
}
|
||
}
|
||
++added;
|
||
}
|
||
return added;
|
||
}
|
||
|
||
size_t ig_objc_baseline_capture(const IGObjCWatch *watches, size_t count) {
|
||
g_nslots = 0u;
|
||
g_unresolved_count = 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) {
|
||
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_unresolved_count && !stop; ++i) {
|
||
#if IG_DIAGNOSTIC_STRINGS
|
||
snprintf(subject, sizeof(subject), "%s %s", g_unresolved[i].class_name, g_unresolved[i].selector);
|
||
#endif
|
||
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject, IG_DIAG_STR("登记时无法解析方法"));
|
||
}
|
||
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), "实现偏离构建基线(@%p)", 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 → %p(%s)(实现被替换)",
|
||
b->imp, 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) {
|
||
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject,
|
||
IG_DIAG_STR("缺少可验证的构建基线"));
|
||
continue;
|
||
}
|
||
{
|
||
unsigned char now[IG_PREFIX_BYTES];
|
||
size_t now_len = ig_read_prefix(cur, now, b->prefix_len);
|
||
if (now_len < b->prefix_len) {
|
||
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject,
|
||
IG_DIAG_STR("代码前缀不可读"));
|
||
continue;
|
||
}
|
||
unsigned char digest[CC_SHA256_DIGEST_LENGTH];
|
||
CC_SHA256(now, (CC_LONG)b->prefix_len, digest);
|
||
if (ig_memcmp(digest, b->digest, sizeof(digest)) != 0) {
|
||
#ifdef DEBUG
|
||
if (ig_objc_debugger_attached()) continue;
|
||
#endif
|
||
#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;
|
||
}
|