fix(defender): ObjC 方法基线改用构建期签名清单,修复误报 110 导致的延迟闪退
- 构建期生成随包签名的 IGMethods.bin(仅主程序),运行时按镜像 UUID、 入口偏移与函数边界内指令摘要校验;导出审计复核清单与最终镜像一致 - 系统函数、调度与解包保护的代码前缀按 LC_FUNCTION_STARTS 限定在函数边界内 - 边界内识别 B / LDR+BR / ADRP+ADD+BR / MOVK+BR 蹦床并确认目标归属,避免系统尾调用误报 - 登记前 IMP 已落入匿名内存判为改写;清单读取改走 IG_dispatch_open - 函数边界长度按入口缓存;镜像未重新链接时复用基线;Debug 附加调试器时不按入口摘要判定改写 - 补充宿主测试;删除已过时的 README 与 docs 历史文档,保留 THIRD_PARTY_NOTICES
This commit is contained in:
@@ -7,9 +7,7 @@
|
||||
*
|
||||
* 特性:
|
||||
* - 同时支持实例方法(-)与类方法(+);
|
||||
* - 前缀快照扩大到 32 字节(4 字节对齐读取,兼容最小 16 字节回退);
|
||||
* - 支持早晚双段登记(C ctor + Swift 运行期追加去重 ig_objc_baseline_append);
|
||||
* - 登记瞬间同步检测「前置劫持」(IMP 已落入非声明宿主镜像,或函数窗已被改为跳转蹦床)。
|
||||
* - 从随包签名的基线校验镜像、方法入口与函数边界内的指令。
|
||||
*/
|
||||
#include "IGObjCBaseline.h"
|
||||
#include "IGAntiHook.h"
|
||||
@@ -22,6 +20,7 @@
|
||||
#include <mach/mach.h>
|
||||
#include <objc/objc.h>
|
||||
#include <objc/runtime.h>
|
||||
#include "IGMethodManifest.h"
|
||||
|
||||
#if defined(__has_feature)
|
||||
# if __has_feature(ptrauth_calls)
|
||||
@@ -31,7 +30,6 @@
|
||||
|
||||
#define IG_OBJC_MAX_WATCH 64u
|
||||
#define IG_PREFIX_BYTES 32u
|
||||
#define IG_PREFIX_FALLBACK 16u
|
||||
|
||||
typedef struct {
|
||||
const char *cls;
|
||||
@@ -39,13 +37,41 @@ typedef struct {
|
||||
uint8_t is_cls_method;
|
||||
uint8_t pre_hooked;
|
||||
void *imp;
|
||||
char prov[192];
|
||||
unsigned char prefix[IG_PREFIX_BYTES];
|
||||
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)
|
||||
@@ -85,21 +111,13 @@ static void ig_emit_objc(IGFindingSink sink, void *ctx, int *stop, int *hits,
|
||||
if (sink && sink(&f, ctx) != 0) *stop = 1;
|
||||
}
|
||||
|
||||
static size_t ig_read_prefix(const void *addr, unsigned char *out) {
|
||||
static size_t ig_read_prefix(const void *addr, unsigned char *out, size_t length) {
|
||||
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;
|
||||
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) {
|
||||
@@ -145,14 +163,13 @@ size_t ig_objc_baseline_append(const IGObjCWatch *watches, size_t count) {
|
||||
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);
|
||||
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++];
|
||||
@@ -162,25 +179,18 @@ size_t ig_objc_baseline_append(const IGObjCWatch *watches, size_t count) {
|
||||
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 (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)) {
|
||||
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;
|
||||
@@ -190,6 +200,7 @@ size_t ig_objc_baseline_append(const IGObjCWatch *watches, size_t count) {
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -199,13 +210,18 @@ size_t ig_objc_baseline_count(void) {
|
||||
}
|
||||
|
||||
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_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
|
||||
@@ -213,7 +229,7 @@ int ig_objc_baseline_check(IGFindingSink sink, void *ctx) {
|
||||
#endif
|
||||
if (b->pre_hooked) {
|
||||
#if IG_DIAGNOSTIC_STRINGS
|
||||
snprintf(ev, sizeof(ev), "基线捕获时已异常(%s @%p)", b->prov[0] ? b->prov : "?", b->imp);
|
||||
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);
|
||||
@@ -245,23 +261,33 @@ int ig_objc_baseline_check(IGFindingSink sink, void *ctx) {
|
||||
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);
|
||||
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 > 0u) {
|
||||
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);
|
||||
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;
|
||||
}
|
||||
if (ig_memcmp(now, b->prefix, b->prefix_len) != 0) {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user