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:
+157
-30
@@ -7,8 +7,8 @@
|
||||
*
|
||||
* 覆盖:
|
||||
* 1. 符号提供方归属(provenance):Export Trie 解析地址须落入系统镜像路径。
|
||||
* 2. inline hook(32 字节指令窗 + 启动期基线快照):首指令 B、全通用寄存器 x0~x30 的
|
||||
* LDR Xt/BR Xt、ADRP+ADD+BR、MOVK+BR 蹦床,以及相对 constructor 基线前 32 字节变化。
|
||||
* 2. 在函数边界内对比系统导出的启动快照,并识别边界内的跳转蹦床(B / LDR+BR / ADRP+ADD+BR / MOVK+BR,
|
||||
* 对可解码目标确认非系统镜像归属,避免合法系统尾调用误报)。
|
||||
* 3. GOT 重绑定:扫本进程非系统镜像的非惰性 __got/__auth_got 符号指针槽。
|
||||
*
|
||||
* 边界(如实声明,unavailable 不等于安全):
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <mach-o/nlist.h>
|
||||
#include <mach/mach.h>
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
#if defined(__has_feature)
|
||||
# if __has_feature(ptrauth_calls)
|
||||
@@ -218,7 +219,6 @@ static int ig_is_br_reg(uint32_t insn, uint32_t reg) {
|
||||
}
|
||||
|
||||
int ig_arm64_suspicious_prologue(uint32_t first, uint32_t second) {
|
||||
if ((first & 0xfc000000u) == 0x14000000u) return 1;
|
||||
uint32_t rt = first & 31u;
|
||||
return rt < 31u &&
|
||||
(first & 0xff000000u) == 0x58000000u &&
|
||||
@@ -227,7 +227,6 @@ int ig_arm64_suspicious_prologue(uint32_t first, uint32_t second) {
|
||||
|
||||
int ig_arm64_suspicious_window(const uint32_t *ins, size_t count) {
|
||||
if (!ins || count == 0u) return 0;
|
||||
if ((ins[0] & 0xfc000000u) == 0x14000000u) return 1;
|
||||
if (count < 2u) return 0;
|
||||
if (ig_arm64_suspicious_prologue(ins[0], ins[1])) return 1;
|
||||
|
||||
@@ -258,24 +257,147 @@ int ig_arm64_suspicious_window(const uint32_t *ins, size_t count) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t ig_read_code_window(const void *addr, uint32_t words[IG_HOOK_PREFIX_WORDS]) {
|
||||
vm_size_t copied = 0;
|
||||
vm_address_t target = (vm_address_t)ig_strip(addr);
|
||||
if (vm_read_overwrite(mach_task_self(), target, IG_HOOK_PREFIX_BYTES,
|
||||
(vm_address_t)words, &copied) == KERN_SUCCESS &&
|
||||
copied == IG_HOOK_PREFIX_BYTES) {
|
||||
return IG_HOOK_PREFIX_WORDS;
|
||||
/* 只在登记时解析已加载镜像的 LC_FUNCTION_STARTS。无完整边界时只取首条指令。 */
|
||||
size_t ig_function_prefix_size(const void *address, size_t limit) {
|
||||
const size_t first = sizeof(uint32_t);
|
||||
Dl_info info;
|
||||
address = ig_strip(address);
|
||||
if (limit < first || !dladdr(address, &info)) return 0;
|
||||
const struct mach_header_64 *mh = info.dli_fbase;
|
||||
if (!mh || mh->magic != MH_MAGIC_64) return first;
|
||||
const uint8_t *cmds = (const uint8_t *)(mh + 1);
|
||||
const struct segment_command_64 *text = NULL, *link = NULL;
|
||||
const struct linkedit_data_command *starts = NULL;
|
||||
size_t remaining = mh->sizeofcmds;
|
||||
for (uint32_t i = 0; i < mh->ncmds && remaining >= sizeof(struct load_command); ++i) {
|
||||
const struct load_command *cmd = (const void *)cmds;
|
||||
if (cmd->cmdsize < sizeof(*cmd) || cmd->cmdsize > remaining) return first;
|
||||
if (cmd->cmd == LC_SEGMENT_64 && cmd->cmdsize >= sizeof(*text)) {
|
||||
const struct segment_command_64 *seg = (const void *)cmd;
|
||||
if (!ig_strncmp(seg->segname, SEG_TEXT, 16)) text = seg;
|
||||
if (!ig_strncmp(seg->segname, SEG_LINKEDIT, 16)) link = seg;
|
||||
} else if (cmd->cmd == LC_FUNCTION_STARTS && cmd->cmdsize == sizeof(*starts)) {
|
||||
starts = (const void *)cmd;
|
||||
}
|
||||
remaining -= cmd->cmdsize;
|
||||
cmds += cmd->cmdsize;
|
||||
}
|
||||
copied = 0;
|
||||
if (vm_read_overwrite(mach_task_self(), target, 2u * sizeof(uint32_t),
|
||||
(vm_address_t)words, &copied) == KERN_SUCCESS &&
|
||||
copied == 2u * sizeof(uint32_t)) {
|
||||
return 2u;
|
||||
if (!text || !link || !starts || starts->dataoff < link->fileoff) return first;
|
||||
uint64_t off = starts->dataoff - link->fileoff;
|
||||
if (off > link->filesize || starts->datasize > link->filesize - off ||
|
||||
off > link->vmsize || starts->datasize > link->vmsize - off) return first;
|
||||
const uint8_t *p = (const void *)((uintptr_t)mh + link->vmaddr - text->vmaddr + off);
|
||||
const uint8_t *end = p + starts->datasize;
|
||||
uintptr_t pos = (uintptr_t)mh, target = (uintptr_t)address;
|
||||
while (p < end && *p) {
|
||||
uintptr_t delta = 0;
|
||||
unsigned shift = 0;
|
||||
uint8_t byte;
|
||||
do {
|
||||
if (p == end || shift >= 63) return first;
|
||||
byte = *p++;
|
||||
delta |= (uintptr_t)(byte & 127u) << shift;
|
||||
shift += 7;
|
||||
} while (byte & 128u);
|
||||
if (pos == target && delta >= first && delta % first == 0)
|
||||
return delta < limit ? delta : limit;
|
||||
if (!delta || delta > UINTPTR_MAX - pos || (pos += delta) > target) break;
|
||||
}
|
||||
return 0u;
|
||||
return first;
|
||||
}
|
||||
|
||||
/* ---- 启动期 32 字节基线快照 ---- */
|
||||
/* 函数边界在进程内不变。热路径(如解包保护的 fread/fwrite)按入口缓存,
|
||||
* 避免每次调用都 dladdr 并遍历 LC_FUNCTION_STARTS。地址与长度打包在同一原子字中,读写无需加锁。 */
|
||||
#define IG_PREFIX_CACHE_SLOTS 64u
|
||||
#define IG_PREFIX_CACHE_ADDR_MASK 0x00ffffffffffffffull
|
||||
static _Atomic(uint64_t) g_prefix_cache[IG_PREFIX_CACHE_SLOTS];
|
||||
|
||||
static size_t ig_cached_prefix_size(const void *addr, size_t limit) {
|
||||
uint64_t key = (uint64_t)(uintptr_t)ig_strip(addr);
|
||||
if (!key || (key & ~IG_PREFIX_CACHE_ADDR_MASK) || limit > 0xffu)
|
||||
return ig_function_prefix_size(addr, limit);
|
||||
_Atomic(uint64_t) *slot = &g_prefix_cache[(key >> 2) % IG_PREFIX_CACHE_SLOTS];
|
||||
uint64_t cached = atomic_load_explicit(slot, memory_order_relaxed);
|
||||
if ((cached & IG_PREFIX_CACHE_ADDR_MASK) == key) {
|
||||
size_t len = (size_t)(cached >> 56);
|
||||
if (len && len <= limit) return len;
|
||||
}
|
||||
size_t len = ig_function_prefix_size(addr, limit);
|
||||
if (len) atomic_store_explicit(slot, key | ((uint64_t)len << 56), memory_order_relaxed);
|
||||
return len;
|
||||
}
|
||||
|
||||
static int ig_read_code_window(const void *addr, void *out, size_t len) {
|
||||
vm_size_t copied = 0;
|
||||
return len && vm_read_overwrite(mach_task_self(), (vm_address_t)ig_strip(addr), len,
|
||||
(vm_address_t)out, &copied) == KERN_SUCCESS && copied == len;
|
||||
}
|
||||
|
||||
#if defined(__arm64__) || defined(__aarch64__)
|
||||
static int ig_is_non_system_target(const void *dest) {
|
||||
if (!dest) return 1;
|
||||
Dl_info info;
|
||||
return !dladdr(dest, &info) || !info.dli_fname || !ig_is_system_path(info.dli_fname);
|
||||
}
|
||||
#endif
|
||||
|
||||
int ig_arm64_trampoline_to_foreign(const void *addr, const uint32_t *ins, size_t count) {
|
||||
#if defined(__arm64__) || defined(__aarch64__)
|
||||
if (!addr || !ins || count == 0u) return 0;
|
||||
uintptr_t base = (uintptr_t)ig_strip(addr);
|
||||
/* 系统函数合法尾调用很常见。首条即无条件直接跳转时只校验分支目标,不继续扫后续不可达字节。 */
|
||||
if ((ins[0] & 0xfc000000u) == 0x14000000u) {
|
||||
uint32_t imm26 = ins[0] & 0x03ffffffu;
|
||||
uint64_t u = (imm26 & 0x02000000u) ? (imm26 | ~(uint64_t)0x03ffffffu) : (uint64_t)imm26;
|
||||
const void *dest = (const void *)(base + (uintptr_t)(intptr_t)(u << 2));
|
||||
return ig_is_non_system_target(dest);
|
||||
}
|
||||
for (size_t i = 0; i + 1u < count; ++i) {
|
||||
uint32_t w0 = ins[i];
|
||||
uint32_t w1 = ins[i + 1u];
|
||||
uint32_t rt = w0 & 31u;
|
||||
if (rt >= 31u) continue;
|
||||
/* LDR Xt, #imm19; BR Xt */
|
||||
if ((w0 & 0xff000000u) == 0x58000000u && ig_is_br_reg(w1, rt)) {
|
||||
uint32_t imm19 = (w0 >> 5) & 0x7ffffu;
|
||||
uint64_t u = (imm19 & 0x40000u) ? (imm19 | ~(uint64_t)0x7ffffu) : (uint64_t)imm19;
|
||||
uintptr_t lit_addr = base + i * sizeof(uint32_t) + (uintptr_t)(intptr_t)(u << 2);
|
||||
uint64_t raw_dest = 0;
|
||||
if (!ig_read_code_window((const void *)lit_addr, &raw_dest, sizeof(raw_dest))) {
|
||||
return 1;
|
||||
}
|
||||
if (ig_is_non_system_target(ig_strip((const void *)(uintptr_t)raw_dest))) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* MOVK Xt, #imm16, lsl #s; BR Xt */
|
||||
if ((w0 & 0xff800000u) == 0xf2800000u && ig_is_br_reg(w1, rt)) {
|
||||
return 1;
|
||||
}
|
||||
/* ADRP Xt, #page; ADD Xt, Xt, #imm12[, lsl #12]; BR Xt */
|
||||
if (i + 2u < count && (w0 & 0x9f000000u) == 0x90000000u) {
|
||||
uint32_t w2 = ins[i + 2u];
|
||||
if ((w1 & 0xff800000u) == 0x91000000u &&
|
||||
(w1 & 31u) == rt &&
|
||||
((w1 >> 5) & 31u) == rt &&
|
||||
ig_is_br_reg(w2, rt)) {
|
||||
uint32_t imm21 = (((w0 >> 5) & 0x7ffffu) << 2) | ((w0 >> 29) & 3u);
|
||||
uint64_t u = (imm21 & 0x100000u) ? (imm21 | ~(uint64_t)0x1fffffu) : (uint64_t)imm21;
|
||||
uintptr_t pc_page = (base + i * sizeof(uint32_t)) & ~(uintptr_t)0xfffu;
|
||||
uintptr_t addend = (uintptr_t)((w1 >> 10) & 0xfffu) << (((w1 >> 22) & 1u) ? 12u : 0u);
|
||||
const void *dest = (const void *)(pc_page + (uintptr_t)(intptr_t)(u << 12) + addend);
|
||||
if (ig_is_non_system_target(dest)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
(void)addr; (void)ins; (void)count;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void ig_capture_baselines_once(void) {
|
||||
#if defined(__arm64__) || defined(__aarch64__)
|
||||
@@ -293,11 +415,11 @@ static void ig_capture_baselines_once(void) {
|
||||
Dl_info info;
|
||||
if (!dladdr(addr, &info) || !info.dli_fname || !ig_is_system_path(info.dli_fname)) continue;
|
||||
uint32_t words[IG_HOOK_PREFIX_WORDS];
|
||||
size_t nwords = ig_read_code_window(addr, words);
|
||||
if (nwords < 2u || ig_arm64_suspicious_window(words, nwords)) continue;
|
||||
size_t len = ig_function_prefix_size(addr, sizeof(words));
|
||||
if (!ig_read_code_window(addr, words, len)) continue;
|
||||
IGHookBaselineSlot *slot = &g_hook_baselines[g_hook_baseline_count++];
|
||||
slot->addr = ig_strip(addr);
|
||||
slot->len = nwords * sizeof(uint32_t);
|
||||
slot->len = len;
|
||||
ig_memcpy(slot->prefix, words, slot->len);
|
||||
}
|
||||
#endif
|
||||
@@ -353,29 +475,34 @@ static IGStatus ig_check_prologue(const char *name, void *addr, IGFindingSink si
|
||||
int *stop, int *hits) {
|
||||
#if defined(__arm64__) || defined(__aarch64__)
|
||||
uint32_t code[IG_HOOK_PREFIX_WORDS];
|
||||
size_t nwords = ig_read_code_window(addr, code);
|
||||
if (nwords < 2u) {
|
||||
const IGHookBaselineSlot *base = ig_find_baseline(addr);
|
||||
size_t len = base ? base->len : ig_cached_prefix_size(addr, sizeof(code));
|
||||
if (len < sizeof(uint32_t) || len > sizeof(code)) {
|
||||
ig_emit(sink, ctx, stop, hits, IG_UNAVAILABLE, IG_REASON_VERIFICATION_UNAVAILABLE,
|
||||
name, IG_DIAG_STR("无法确定函数代码边界"));
|
||||
return IG_UNAVAILABLE;
|
||||
}
|
||||
if (!ig_read_code_window(addr, code, len)) {
|
||||
ig_emit(sink, ctx, stop, hits, IG_UNAVAILABLE, IG_REASON_VERIFICATION_UNAVAILABLE,
|
||||
name, IG_DIAG_STR("无法读取函数代码"));
|
||||
return IG_UNAVAILABLE;
|
||||
}
|
||||
if (ig_arm64_suspicious_window(code, nwords)) {
|
||||
if (ig_arm64_trampoline_to_foreign(addr, code, len / sizeof(uint32_t))) {
|
||||
#if IG_DIAGNOSTIC_STRINGS
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev), "指令窗命中跳转蹦床 @%p [%08x %08x]", addr, code[0], code[1]);
|
||||
snprintf(ev, sizeof(ev), "指令窗命中外部跳转蹦床 @%p [%08x %08x]",
|
||||
addr, code[0], len >= 2u * sizeof(uint32_t) ? code[1] : 0u);
|
||||
ig_emit(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_INLINE_HOOK, name, ev);
|
||||
#else
|
||||
ig_emit(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_INLINE_HOOK, name, NULL);
|
||||
#endif
|
||||
return IG_DETECTED;
|
||||
}
|
||||
const IGHookBaselineSlot *base = ig_find_baseline(addr);
|
||||
if (base && nwords * sizeof(uint32_t) >= base->len &&
|
||||
ig_memcmp(code, base->prefix, base->len) != 0) {
|
||||
if (base && ig_memcmp(code, base->prefix, len) != 0) {
|
||||
#if IG_DIAGNOSTIC_STRINGS
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev), "前%zu字节偏离启动基线 @%p [%08x %08x]",
|
||||
base->len, addr, code[0], code[1]);
|
||||
snprintf(ev, sizeof(ev), "前%zu字节偏离启动基线 @%p [%08x]",
|
||||
base->len, addr, code[0]);
|
||||
ig_emit(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_INLINE_HOOK, name, ev);
|
||||
#else
|
||||
ig_emit(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_INLINE_HOOK, name, NULL);
|
||||
|
||||
@@ -38,10 +38,16 @@ int ig_anti_hook_check_default(IGFindingSink sink, void *ctx);
|
||||
*/
|
||||
IGStatus ig_verify_function(void *addr, const char *name, IGFindingSink sink, void *ctx);
|
||||
|
||||
/* 登记启动快照时计算函数边界内可读取的字节数。 */
|
||||
size_t ig_function_prefix_size(const void *address, size_t limit);
|
||||
|
||||
/* ARM64 序言与 32 字节指令窗形态分类,可独立测试;命中只是一条证据,不覆盖所有 Hook。 */
|
||||
int ig_arm64_suspicious_prologue(uint32_t first, uint32_t second);
|
||||
int ig_arm64_suspicious_window(const uint32_t *ins, size_t count);
|
||||
|
||||
/* 在函数边界内检测跳转蹦床,并对可直接解码的目标确认是否落入非系统镜像。 */
|
||||
int ig_arm64_trampoline_to_foreign(const void *addr, const uint32_t *ins, size_t count);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/* 构建时由 objc_method_baseline.py 从签名前的镜像生成。 */
|
||||
#ifndef IG_METHOD_MANIFEST_H
|
||||
#define IG_METHOD_MANIFEST_H
|
||||
#include "../dispatch/IGDispatch.h"
|
||||
#include <CommonCrypto/CommonDigest.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <mach-o/dyld.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef struct {
|
||||
unsigned char key[32], uuid[16];
|
||||
uint64_t offset;
|
||||
uint32_t length;
|
||||
unsigned char digest[CC_SHA256_DIGEST_LENGTH], reserved[4];
|
||||
} IGMethodRecord;
|
||||
_Static_assert(sizeof(IGMethodRecord) == 96, "IGMethods row layout");
|
||||
|
||||
static const IGMethodRecord *ig_records;
|
||||
static size_t ig_record_count;
|
||||
static pthread_once_t ig_manifest_once = PTHREAD_ONCE_INIT;
|
||||
|
||||
static const uint8_t *ig_method_image_uuid(const struct mach_header *header) {
|
||||
if (!header || header->magic != MH_MAGIC_64) return NULL;
|
||||
const uint8_t *p = (const uint8_t *)header + sizeof(struct mach_header_64);
|
||||
const uint8_t *end = p + header->sizeofcmds;
|
||||
for (uint32_t i = 0; i < header->ncmds && (size_t)(end-p) >= sizeof(struct load_command); ++i) {
|
||||
const struct load_command *cmd = (const void *)p;
|
||||
if (cmd->cmdsize < sizeof(*cmd) || cmd->cmdsize > (size_t)(end-p)) return NULL;
|
||||
if (cmd->cmd == LC_UUID && cmd->cmdsize == sizeof(struct uuid_command))
|
||||
return ((const struct uuid_command *)cmd)->uuid;
|
||||
p += cmd->cmdsize;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void ig_load_method_manifest(void) {
|
||||
char path[PATH_MAX];
|
||||
uint32_t size = sizeof(path);
|
||||
if (_NSGetExecutablePath(path, &size)) return;
|
||||
char *slash = strrchr(path, '/');
|
||||
if (!slash || sizeof("IGMethods.bin") > sizeof(path)-(size_t)(slash+1-path)) return;
|
||||
memcpy(slash+1, "IGMethods.bin", sizeof("IGMethods.bin"));
|
||||
int fd = IG_dispatch_open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
|
||||
if (fd < 0) return;
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) && st.st_size >= 24 &&
|
||||
(st.st_size-24) % sizeof(IGMethodRecord) == 0) {
|
||||
size_t len = (size_t)st.st_size;
|
||||
void *mem = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (mem != MAP_FAILED) {
|
||||
const uint8_t *bytes = mem;
|
||||
const uint8_t *uuid = ig_method_image_uuid(_dyld_get_image_header(0));
|
||||
if (uuid && !memcmp(bytes, "IGMB0001", 8) && !memcmp(bytes+8, uuid, 16)) {
|
||||
ig_records = (const void *)(bytes+24);
|
||||
ig_record_count = (len-24) / sizeof(IGMethodRecord);
|
||||
} else {
|
||||
munmap(mem, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
IG_dispatch_close(fd);
|
||||
}
|
||||
|
||||
static int ig_compare_method_key(const void *key, const void *record) {
|
||||
return memcmp(key, ((const IGMethodRecord *)record)->key, 32);
|
||||
}
|
||||
|
||||
static const IGMethodRecord *ig_expected_method(Class cls, SEL sel, int class_method, Method method) {
|
||||
pthread_once(&ig_manifest_once, ig_load_method_manifest);
|
||||
if (!ig_records) return NULL;
|
||||
Class owner = class_method ? object_getClass((id)cls) : cls;
|
||||
Class parent;
|
||||
while ((parent = class_getSuperclass(owner)) &&
|
||||
class_getInstanceMethod(parent, sel) == method)
|
||||
owner = parent;
|
||||
char name[1024];
|
||||
int n = snprintf(name, sizeof(name), "%c%s %s", class_method ? '+' : '-',
|
||||
class_getName(owner), sel_getName(sel));
|
||||
if (n < 0 || (size_t)n >= sizeof(name)) return NULL;
|
||||
unsigned char key[CC_SHA256_DIGEST_LENGTH];
|
||||
CC_SHA256(name, (CC_LONG)n, key);
|
||||
return bsearch(key, ig_records, ig_record_count, sizeof(*ig_records), ig_compare_method_key);
|
||||
}
|
||||
#endif
|
||||
@@ -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);
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
*
|
||||
* 背景(docs/05 §4 P1):IGAntiHook 只覆盖 C 符号层(序言/GOT/提供方),对 ObjC 层
|
||||
* method_exchangeImplementations / class_replaceMethod / method_setImplementation
|
||||
* 等劫持手段此前零覆盖。本模块由策略层指定关键方法清单,启动后采集 IMP 基线
|
||||
* (实现地址 + 提供方镜像 + 代码前缀),扫描时比对——IMP 换人或函数体被改即上报。
|
||||
* 等劫持手段此前零覆盖。本模块由策略层指定关键方法清单,登记时记录 IMP,并加载同一构建的镜像 UUID、
|
||||
* 入口偏移与函数边界内的指令,扫描时比对——IMP 换人或函数体被改即上报。
|
||||
*
|
||||
* 边界(如实声明):
|
||||
* - 合法 swizzle(统计/广告 SDK 常见)也会改变 IMP:本探针只给证据,是否处置由
|
||||
@@ -28,7 +28,7 @@ typedef struct {
|
||||
} IGObjCWatch;
|
||||
|
||||
/*
|
||||
* 采集基线(重置并重建):对每个观察项记录当前 IMP 地址、提供方镜像路径与代码前缀 32 字节。
|
||||
* 重置观察项,按方法的声明类和选择子查询构建基线。指令比较最多 32 字节。
|
||||
* 支持实例方法(默认或 '-' 前缀)与类方法(自动回退或 '+' 前缀)。
|
||||
* 幂等,重复调用以最后一次为准;count==0 清空基线。返回当前累计成功登记的条数。
|
||||
*/
|
||||
@@ -45,7 +45,7 @@ size_t ig_objc_baseline_count(void);
|
||||
|
||||
/*
|
||||
* 扫描:IMP 地址变化,或 IMP 相同但代码前缀变化 → DETECTED(110);
|
||||
* 类不可解析 / IMP 为转发桩 / 代码页不可读 → UNAVAILABLE(110)。
|
||||
* 已登记方法无法解析、缺少可信构建记录或代码页不可读 → UNAVAILABLE(110)。
|
||||
* 未 capture 过时为 clear no-op(不惩罚未接入的调用方)。
|
||||
* 返回 DETECTED 条数;只采集证据不处置。
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user