- 构建期生成随包签名的 IGMethods.bin(仅主程序),运行时按镜像 UUID、 入口偏移与函数边界内指令摘要校验;导出审计复核清单与最终镜像一致 - 系统函数、调度与解包保护的代码前缀按 LC_FUNCTION_STARTS 限定在函数边界内 - 边界内识别 B / LDR+BR / ADRP+ADD+BR / MOVK+BR 蹦床并确认目标归属,避免系统尾调用误报 - 登记前 IMP 已落入匿名内存判为改写;清单读取改走 IG_dispatch_open - 函数边界长度按入口缓存;镜像未重新链接时复用基线;Debug 附加调试器时不按入口摘要判定改写 - 补充宿主测试;删除已过时的 README 与 docs 历史文档,保留 THIRD_PARTY_NOTICES
669 lines
28 KiB
C
669 lines
28 KiB
C
/*
|
||
* IGAntiHook.c — 关键函数 HOOK 完整性探针实现。
|
||
*
|
||
* 全部基于公开 API:<dlfcn.h>(dladdr)、<mach-o/dyld.h>(镜像枚举)、
|
||
* <mach-o/loader.h>/<nlist.h>(运行时解析自身镜像的 GOT 槽)、
|
||
* <mach/mach.h>(vm_read_overwrite 读自身进程只读代码页)。
|
||
*
|
||
* 覆盖:
|
||
* 1. 符号提供方归属(provenance):Export Trie 解析地址须落入系统镜像路径。
|
||
* 2. 在函数边界内对比系统导出的启动快照,并识别边界内的跳转蹦床(B / LDR+BR / ADRP+ADD+BR / MOVK+BR,
|
||
* 对可解码目标确认非系统镜像归属,避免合法系统尾调用误报)。
|
||
* 3. GOT 重绑定:扫本进程非系统镜像的非惰性 __got/__auth_got 符号指针槽。
|
||
*
|
||
* 边界(如实声明,unavailable 不等于安全):
|
||
* - 仅支持 arm64 / x86_64(LP64)。非 64 位镜像跳过。
|
||
* - arm64e:__auth_got 槽为带签名指针,比较前用 ptrauth_strip 去签名。
|
||
*/
|
||
#include "IGAntiHook.h"
|
||
#include "IGInline.h"
|
||
#include "../resolve/IGResolve.h"
|
||
|
||
#if IG_DIAGNOSTIC_STRINGS
|
||
# include <stdio.h>
|
||
#endif
|
||
#include <dlfcn.h>
|
||
#include <mach-o/dyld.h>
|
||
#include <mach-o/loader.h>
|
||
#include <mach-o/nlist.h>
|
||
#include <mach/mach.h>
|
||
#include <pthread.h>
|
||
#include <stdatomic.h>
|
||
|
||
#if defined(__has_feature)
|
||
# if __has_feature(ptrauth_calls)
|
||
# include <ptrauth.h>
|
||
# define IG_HAS_PTRAUTH 1
|
||
# endif
|
||
#endif
|
||
#ifndef IG_HAS_PTRAUTH
|
||
# define IG_HAS_PTRAUTH 0
|
||
#endif
|
||
|
||
#define IG_HOOK_NAME_KEY 0x3Au
|
||
#define IG_HOOK_BASELINE_MAX 48u
|
||
#define IG_HOOK_PREFIX_WORDS 8u
|
||
#define IG_HOOK_PREFIX_BYTES (IG_HOOK_PREFIX_WORDS * sizeof(uint32_t))
|
||
|
||
enum {
|
||
IG_GROUP_CORE = 0u,
|
||
IG_GROUP_OBJC = 1u,
|
||
IG_GROUP_SECURITY = 2u,
|
||
};
|
||
|
||
typedef struct {
|
||
uint8_t group;
|
||
uint8_t len;
|
||
uint8_t bytes[32];
|
||
} IGHookSpec;
|
||
|
||
/*
|
||
* XOR(0x3A) 编码的受监控符号列表:
|
||
* - CORE (15): fopen, dlopen, dlsym, write, read, open, close, exit, fork, syscall,
|
||
* unlink, getpid, CCCrypt, CCHmac, CCKeyDerivationPBKDF
|
||
* - OBJC (4): objc_msgSend, method_exchangeImplementations, class_replaceMethod,
|
||
* method_setImplementation (仅在进程已加载 libobjc 时扫描)
|
||
* - SEC (7): SecKeyCreateWithData, SecKeyCreateSignature, SecKeyRawSign,
|
||
* SecItemCopyMatching, SecItemAdd, SecItemUpdate, SecItemDelete
|
||
* (仅在进程已加载 Security.framework 时扫描,避免无 Security CLI 宿主误报 unavailable)
|
||
*/
|
||
static const IGHookSpec kIGDefaultSpecs[] = {
|
||
{ IG_GROUP_CORE, 5u, { 0x5cu, 0x55u, 0x4au, 0x5fu, 0x54u } },
|
||
{ IG_GROUP_CORE, 6u, { 0x5eu, 0x56u, 0x55u, 0x4au, 0x5fu, 0x54u } },
|
||
{ IG_GROUP_CORE, 5u, { 0x5eu, 0x56u, 0x49u, 0x43u, 0x57u } },
|
||
{ IG_GROUP_CORE, 5u, { 0x4du, 0x48u, 0x53u, 0x4eu, 0x5fu } },
|
||
{ IG_GROUP_CORE, 4u, { 0x48u, 0x5fu, 0x5bu, 0x5eu } },
|
||
{ IG_GROUP_CORE, 4u, { 0x55u, 0x4au, 0x5fu, 0x54u } },
|
||
{ IG_GROUP_CORE, 5u, { 0x59u, 0x56u, 0x55u, 0x49u, 0x5fu } },
|
||
{ IG_GROUP_CORE, 4u, { 0x5fu, 0x42u, 0x53u, 0x4eu } },
|
||
{ IG_GROUP_CORE, 4u, { 0x5cu, 0x55u, 0x48u, 0x51u } },
|
||
{ IG_GROUP_CORE, 7u, { 0x49u, 0x43u, 0x49u, 0x59u, 0x5bu, 0x56u, 0x56u } },
|
||
{ IG_GROUP_CORE, 6u, { 0x4fu, 0x54u, 0x56u, 0x53u, 0x54u, 0x51u } },
|
||
{ IG_GROUP_CORE, 6u, { 0x5du, 0x5fu, 0x4eu, 0x4au, 0x53u, 0x5eu } },
|
||
{ IG_GROUP_CORE, 7u, { 0x79u, 0x79u, 0x79u, 0x48u, 0x43u, 0x4au, 0x4eu } },
|
||
{ IG_GROUP_CORE, 6u, { 0x79u, 0x79u, 0x72u, 0x57u, 0x5bu, 0x59u } },
|
||
{ IG_GROUP_CORE, 20u, { 0x79u, 0x79u, 0x71u, 0x5fu, 0x43u, 0x7eu, 0x5fu, 0x48u, 0x53u, 0x4cu,
|
||
0x5bu, 0x4eu, 0x53u, 0x55u, 0x54u, 0x6au, 0x78u, 0x71u, 0x7eu, 0x7cu } },
|
||
|
||
{ IG_GROUP_OBJC, 12u, { 0x55u, 0x58u, 0x50u, 0x59u, 0x65u, 0x57u, 0x49u, 0x5du, 0x69u, 0x5fu, 0x54u, 0x5eu } },
|
||
{ IG_GROUP_OBJC, 30u, { 0x57u, 0x5fu, 0x4eu, 0x52u, 0x55u, 0x5eu, 0x65u, 0x5fu, 0x42u, 0x59u,
|
||
0x52u, 0x5bu, 0x54u, 0x5du, 0x5fu, 0x73u, 0x57u, 0x4au, 0x56u, 0x5fu,
|
||
0x57u, 0x5fu, 0x54u, 0x4eu, 0x5bu, 0x4eu, 0x53u, 0x55u, 0x54u, 0x49u } },
|
||
{ IG_GROUP_OBJC, 19u, { 0x59u, 0x56u, 0x5bu, 0x49u, 0x49u, 0x65u, 0x48u, 0x5fu, 0x4au, 0x56u,
|
||
0x5bu, 0x59u, 0x5fu, 0x77u, 0x5fu, 0x4eu, 0x52u, 0x55u, 0x5eu } },
|
||
{ IG_GROUP_OBJC, 24u, { 0x57u, 0x5fu, 0x4eu, 0x52u, 0x55u, 0x5eu, 0x65u, 0x49u, 0x5fu, 0x4eu,
|
||
0x73u, 0x57u, 0x4au, 0x56u, 0x5fu, 0x57u, 0x5fu, 0x54u, 0x4eu, 0x5bu,
|
||
0x4eu, 0x53u, 0x55u, 0x54u } },
|
||
|
||
{ IG_GROUP_SECURITY, 20u, { 0x69u, 0x5fu, 0x59u, 0x71u, 0x5fu, 0x43u, 0x79u, 0x48u, 0x5fu, 0x5bu,
|
||
0x4eu, 0x5fu, 0x6du, 0x53u, 0x4eu, 0x52u, 0x7eu, 0x5bu, 0x4eu, 0x5bu } },
|
||
{ IG_GROUP_SECURITY, 21u, { 0x69u, 0x5fu, 0x59u, 0x71u, 0x5fu, 0x43u, 0x79u, 0x48u, 0x5fu, 0x5bu,
|
||
0x4eu, 0x5fu, 0x69u, 0x53u, 0x5du, 0x54u, 0x5bu, 0x4eu, 0x4fu, 0x48u, 0x5fu } },
|
||
{ IG_GROUP_SECURITY, 13u, { 0x69u, 0x5fu, 0x59u, 0x71u, 0x5fu, 0x43u, 0x68u, 0x5bu, 0x4du, 0x69u,
|
||
0x53u, 0x5du, 0x54u } },
|
||
{ IG_GROUP_SECURITY, 19u, { 0x69u, 0x5fu, 0x59u, 0x73u, 0x4eu, 0x5fu, 0x57u, 0x79u, 0x55u, 0x4au,
|
||
0x43u, 0x77u, 0x5bu, 0x4eu, 0x59u, 0x52u, 0x53u, 0x54u, 0x5du } },
|
||
{ IG_GROUP_SECURITY, 10u, { 0x69u, 0x5fu, 0x59u, 0x73u, 0x4eu, 0x5fu, 0x57u, 0x7bu, 0x5eu, 0x5eu } },
|
||
{ IG_GROUP_SECURITY, 13u, { 0x69u, 0x5fu, 0x59u, 0x73u, 0x4eu, 0x5fu, 0x57u, 0x6fu, 0x4au, 0x5eu,
|
||
0x5bu, 0x4eu, 0x5fu } },
|
||
{ IG_GROUP_SECURITY, 13u, { 0x69u, 0x5fu, 0x59u, 0x73u, 0x4eu, 0x5fu, 0x57u, 0x7eu, 0x5fu, 0x56u,
|
||
0x5fu, 0x4eu, 0x5fu } },
|
||
};
|
||
|
||
/* "/libobjc.A.dylib" ^ 0x3A 与 "/Security.framework/" ^ 0x3A,避免明文进常量区。 */
|
||
static const uint8_t kIGObjcNeedEnc[16] = {
|
||
0x15u, 0x56u, 0x53u, 0x58u, 0x55u, 0x58u, 0x50u, 0x59u,
|
||
0x14u, 0x7bu, 0x14u, 0x5eu, 0x43u, 0x56u, 0x53u, 0x58u
|
||
};
|
||
static const uint8_t kIGSecNeedEnc[20] = {
|
||
0x15u, 0x69u, 0x5fu, 0x59u, 0x4fu, 0x48u, 0x53u, 0x4eu, 0x43u, 0x14u,
|
||
0x5cu, 0x48u, 0x5bu, 0x57u, 0x5fu, 0x4du, 0x55u, 0x48u, 0x51u, 0x15u
|
||
};
|
||
|
||
typedef struct {
|
||
const void *addr;
|
||
size_t len;
|
||
uint8_t prefix[IG_HOOK_PREFIX_BYTES];
|
||
} IGHookBaselineSlot;
|
||
|
||
static IGHookBaselineSlot g_hook_baselines[IG_HOOK_BASELINE_MAX];
|
||
static size_t g_hook_baseline_count = 0;
|
||
static pthread_once_t g_hook_baseline_once = PTHREAD_ONCE_INIT;
|
||
|
||
static void ig_hook_wipe(void *p, size_t n) {
|
||
if (!p) return;
|
||
volatile unsigned char *v = (volatile unsigned char *)p;
|
||
while (n--) *v++ = 0;
|
||
}
|
||
|
||
static void ig_decode_bytes(const uint8_t *enc, size_t n, char *out, size_t cap) {
|
||
volatile uint8_t key = IG_HOOK_NAME_KEY;
|
||
if (!out || cap == 0u) return;
|
||
size_t limit = (n + 1u < cap) ? n : (cap - 1u);
|
||
for (size_t i = 0; i < limit; ++i) {
|
||
out[i] = (char)(enc[i] ^ key);
|
||
}
|
||
out[limit] = '\0';
|
||
}
|
||
|
||
/* ---- 工具:系统提供方路径白名单 ---- */
|
||
|
||
static int ig_is_system_path(const char *path) {
|
||
if (!path) return 0;
|
||
static const char *const prefixes[] = {
|
||
"/usr/lib/", "/System/", "/usr/lib/system/",
|
||
"/private/preboot/Cryptexes/",
|
||
};
|
||
for (size_t i = 0; i < sizeof(prefixes) / sizeof(prefixes[0]); ++i) {
|
||
if (ig_strncmp(path, prefixes[i], ig_strlen(prefixes[i])) == 0) return 1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
static int ig_has_loaded_image_substr(const uint8_t *enc, size_t len) {
|
||
char needle[32];
|
||
ig_decode_bytes(enc, len, needle, sizeof(needle));
|
||
uint32_t imgc = _dyld_image_count();
|
||
int found = 0;
|
||
for (uint32_t i = 0; i < imgc; ++i) {
|
||
const char *p = _dyld_get_image_name(i);
|
||
if (p && ig_strstr(p, needle) != NULL) {
|
||
found = 1;
|
||
break;
|
||
}
|
||
}
|
||
ig_hook_wipe(needle, sizeof(needle));
|
||
return found;
|
||
}
|
||
|
||
static void ig_emit(IGFindingSink sink, void *ctx, int *stop, int *hits,
|
||
IGStatus st, int reason, const char *subject, const char *evidence) {
|
||
if (*stop) return;
|
||
IGFinding f;
|
||
ig_memset(&f, 0, sizeof(f));
|
||
f.status = st;
|
||
f.reason = reason;
|
||
#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 const void *ig_strip(const void *p) {
|
||
#if IG_HAS_PTRAUTH
|
||
return ptrauth_strip(p, ptrauth_key_asia);
|
||
#else
|
||
return p;
|
||
#endif
|
||
}
|
||
|
||
/* ---- 2. inline hook:通用寄存器蹦床 + 32 字节指令窗检测 ---- */
|
||
|
||
static int ig_is_br_reg(uint32_t insn, uint32_t reg) {
|
||
return reg < 31u &&
|
||
(insn & 0xfffffc1fu) == 0xd61f0000u &&
|
||
((insn >> 5) & 31u) == reg;
|
||
}
|
||
|
||
int ig_arm64_suspicious_prologue(uint32_t first, uint32_t second) {
|
||
uint32_t rt = first & 31u;
|
||
return rt < 31u &&
|
||
(first & 0xff000000u) == 0x58000000u &&
|
||
ig_is_br_reg(second, rt);
|
||
}
|
||
|
||
int ig_arm64_suspicious_window(const uint32_t *ins, size_t count) {
|
||
if (!ins || count == 0u) return 0;
|
||
if (count < 2u) return 0;
|
||
if (ig_arm64_suspicious_prologue(ins[0], ins[1])) return 1;
|
||
|
||
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, #literal; BR Xt */
|
||
if ((w0 & 0xff000000u) == 0x58000000u && ig_is_br_reg(w1, rt)) {
|
||
return 1;
|
||
}
|
||
/* MOVK Xt, #imm, lsl #s; BR Xt */
|
||
if ((w0 & 0xff800000u) == 0xf2800000u && ig_is_br_reg(w1, rt)) {
|
||
return 1;
|
||
}
|
||
/* ADRP Xt, #page; ADD Xt, Xt, #imm12; 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)) {
|
||
return 1;
|
||
}
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
/* 只在登记时解析已加载镜像的 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;
|
||
}
|
||
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 first;
|
||
}
|
||
|
||
/* 函数边界在进程内不变。热路径(如解包保护的 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__)
|
||
int has_objc = ig_has_loaded_image_substr(kIGObjcNeedEnc, sizeof(kIGObjcNeedEnc));
|
||
int has_sec = ig_has_loaded_image_substr(kIGSecNeedEnc, sizeof(kIGSecNeedEnc));
|
||
size_t n_specs = sizeof(kIGDefaultSpecs) / sizeof(kIGDefaultSpecs[0]);
|
||
for (size_t i = 0; i < n_specs && g_hook_baseline_count < IG_HOOK_BASELINE_MAX; ++i) {
|
||
if (kIGDefaultSpecs[i].group == IG_GROUP_OBJC && !has_objc) continue;
|
||
if (kIGDefaultSpecs[i].group == IG_GROUP_SECURITY && !has_sec) continue;
|
||
char sym[40];
|
||
ig_decode_bytes(kIGDefaultSpecs[i].bytes, kIGDefaultSpecs[i].len, sym, sizeof(sym));
|
||
void *addr = ig_resolve_export_default(sym);
|
||
ig_hook_wipe(sym, sizeof(sym));
|
||
if (!addr) continue;
|
||
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 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 = len;
|
||
ig_memcpy(slot->prefix, words, slot->len);
|
||
}
|
||
#endif
|
||
}
|
||
|
||
static void ig_ensure_baselines(void) {
|
||
(void)pthread_once(&g_hook_baseline_once, ig_capture_baselines_once);
|
||
}
|
||
|
||
__attribute__((constructor))
|
||
static void ig_anti_hook_warmup(void) {
|
||
ig_ensure_baselines();
|
||
}
|
||
|
||
static const IGHookBaselineSlot *ig_find_baseline(const void *addr) {
|
||
const void *stripped = ig_strip(addr);
|
||
for (size_t i = 0; i < g_hook_baseline_count; ++i) {
|
||
if (g_hook_baselines[i].addr == stripped) return &g_hook_baselines[i];
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
/* ---- 1. 符号提供方 ---- */
|
||
|
||
static void *ig_check_provenance(const char *name, IGFindingSink sink, void *ctx,
|
||
int *stop, int *hits) {
|
||
void *addr = ig_resolve_export_default(name);
|
||
if (!addr) {
|
||
ig_emit(sink, ctx, stop, hits, IG_UNAVAILABLE, IG_REASON_SYMBOL_UNRESOLVED,
|
||
name, IG_DIAG_STR("导出表无法解析该符号"));
|
||
return NULL;
|
||
}
|
||
Dl_info info;
|
||
if (!dladdr(addr, &info) || !info.dli_fname) {
|
||
ig_emit(sink, ctx, stop, hits, IG_UNAVAILABLE, IG_REASON_VERIFICATION_UNAVAILABLE,
|
||
name, IG_DIAG_STR("无法确认符号提供方"));
|
||
return NULL;
|
||
}
|
||
if (!ig_is_system_path(info.dli_fname)) {
|
||
#if IG_DIAGNOSTIC_STRINGS
|
||
char ev[256];
|
||
snprintf(ev, sizeof(ev), "解析地址 %p 提供方=%s(非系统路径)",
|
||
addr, info.dli_fname ? info.dli_fname : "?");
|
||
ig_emit(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_SYMBOL_PROVENANCE, name, ev);
|
||
#else
|
||
ig_emit(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_SYMBOL_PROVENANCE, name, NULL);
|
||
#endif
|
||
}
|
||
return addr;
|
||
}
|
||
|
||
static IGStatus ig_check_prologue(const char *name, void *addr, IGFindingSink sink, void *ctx,
|
||
int *stop, int *hits) {
|
||
#if defined(__arm64__) || defined(__aarch64__)
|
||
uint32_t code[IG_HOOK_PREFIX_WORDS];
|
||
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_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], 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;
|
||
}
|
||
if (base && ig_memcmp(code, base->prefix, len) != 0) {
|
||
#if IG_DIAGNOSTIC_STRINGS
|
||
char ev[256];
|
||
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);
|
||
#endif
|
||
return IG_DETECTED;
|
||
}
|
||
#else
|
||
(void)name; (void)addr; (void)sink; (void)ctx; (void)stop; (void)hits;
|
||
#endif
|
||
return IG_CLEAR;
|
||
}
|
||
|
||
/* ---- 3. GOT 重绑定:扫本进程非系统镜像的非惰性符号指针槽 ---- */
|
||
|
||
static void ig_check_got_one_image(uint32_t img_index,
|
||
const char *const *names, void *const *trusted, size_t count,
|
||
IGFindingSink sink, void *ctx, int *stop, int *hits) {
|
||
const struct mach_header_64 *mh =
|
||
(const struct mach_header_64 *)_dyld_get_image_header(img_index);
|
||
if (!mh || mh->magic != MH_MAGIC_64) return;
|
||
const char *img_path = _dyld_get_image_name(img_index);
|
||
if (ig_is_system_path(img_path)) return;
|
||
intptr_t slide = _dyld_get_image_vmaddr_slide(img_index);
|
||
|
||
const struct segment_command_64 *linkedit = NULL;
|
||
const struct symtab_command *symtab = NULL;
|
||
const struct dysymtab_command *dysymtab = NULL;
|
||
const uint8_t *cmd = (const uint8_t *)(mh + 1);
|
||
for (uint32_t i = 0; i < mh->ncmds; ++i) {
|
||
const struct load_command *lc = (const struct load_command *)cmd;
|
||
if (lc->cmd == LC_SEGMENT_64) {
|
||
const struct segment_command_64 *seg = (const struct segment_command_64 *)lc;
|
||
if (ig_strcmp(seg->segname, SEG_LINKEDIT) == 0) linkedit = seg;
|
||
} else if (lc->cmd == LC_SYMTAB) {
|
||
symtab = (const struct symtab_command *)lc;
|
||
} else if (lc->cmd == LC_DYSYMTAB) {
|
||
dysymtab = (const struct dysymtab_command *)lc;
|
||
}
|
||
cmd += lc->cmdsize;
|
||
}
|
||
if (!linkedit || !symtab || !dysymtab) return;
|
||
|
||
uintptr_t linkedit_base = (uintptr_t)slide + linkedit->vmaddr - linkedit->fileoff;
|
||
const struct nlist_64 *symtab_list = (const struct nlist_64 *)(linkedit_base + symtab->symoff);
|
||
const char *strtab = (const char *)(linkedit_base + symtab->stroff);
|
||
const uint32_t *indirect = (const uint32_t *)(linkedit_base + dysymtab->indirectsymoff);
|
||
|
||
cmd = (const uint8_t *)(mh + 1);
|
||
for (uint32_t i = 0; i < mh->ncmds; ++i) {
|
||
const struct load_command *lc = (const struct load_command *)cmd;
|
||
if (lc->cmd == LC_SEGMENT_64) {
|
||
const struct segment_command_64 *seg = (const struct segment_command_64 *)lc;
|
||
const struct section_64 *sect = (const struct section_64 *)(seg + 1);
|
||
for (uint32_t s = 0; s < seg->nsects; ++s) {
|
||
uint32_t type = sect[s].flags & SECTION_TYPE;
|
||
if (type != S_NON_LAZY_SYMBOL_POINTERS) continue;
|
||
size_t n = sect[s].size / sizeof(void *);
|
||
const uint32_t *idxs = indirect + sect[s].reserved1;
|
||
void **slots = (void **)((uintptr_t)slide + sect[s].addr);
|
||
for (size_t k = 0; k < n; ++k) {
|
||
uint32_t symi = idxs[k];
|
||
if (symi == INDIRECT_SYMBOL_ABS || symi == INDIRECT_SYMBOL_LOCAL) continue;
|
||
const char *sym = strtab + symtab_list[symi].n_un.n_strx;
|
||
if (sym[0] != '_') continue;
|
||
const char *bare = sym + 1;
|
||
for (size_t m = 0; m < count; ++m) {
|
||
if (ig_strcmp(bare, names[m]) != 0) continue;
|
||
const void *cur = ig_strip(slots[k]);
|
||
if (!trusted[m] || cur == trusted[m]) break;
|
||
Dl_info di;
|
||
const char *prov = (dladdr((void *)cur, &di) ? di.dli_fname : NULL);
|
||
if (prov && ig_is_system_path(prov)) break;
|
||
#if IG_DIAGNOSTIC_STRINGS
|
||
char ev[256];
|
||
snprintf(ev, sizeof(ev),
|
||
"镜像=%s 槽@%p 当前=%p(提供方=%s) 期望=%p",
|
||
img_path ? img_path : "?", (void *)&slots[k], (void *)cur,
|
||
prov ? prov : "?", trusted[m]);
|
||
ig_emit(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_GOT_REBIND, sym, ev);
|
||
#else
|
||
ig_emit(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_GOT_REBIND, sym, NULL);
|
||
#endif
|
||
break;
|
||
}
|
||
if (*stop) return;
|
||
}
|
||
}
|
||
}
|
||
cmd += lc->cmdsize;
|
||
}
|
||
}
|
||
|
||
int ig_anti_hook_check(const char *const *names, size_t count,
|
||
IGFindingSink sink, void *ctx) {
|
||
if (!names || count == 0) return 0;
|
||
ig_ensure_baselines();
|
||
int hits = 0, stop = 0;
|
||
|
||
void *trusted[64];
|
||
size_t n = count < 64u ? count : 64u;
|
||
for (size_t i = 0; i < n; ++i) {
|
||
void *addr = ig_check_provenance(names[i], sink, ctx, &stop, &hits);
|
||
trusted[i] = addr;
|
||
if (addr) ig_check_prologue(names[i], addr, sink, ctx, &stop, &hits);
|
||
if (stop) return hits;
|
||
}
|
||
|
||
uint32_t imgc = _dyld_image_count();
|
||
for (uint32_t i = 0; i < imgc && !stop; ++i) {
|
||
ig_check_got_one_image(i, names, trusted, n, sink, ctx, &stop, &hits);
|
||
}
|
||
return hits;
|
||
}
|
||
|
||
IGStatus ig_verify_function(void *addr, const char *name, IGFindingSink sink, void *ctx) {
|
||
#if !IG_DIAGNOSTIC_STRINGS
|
||
(void)name;
|
||
#endif
|
||
if (!addr) return IG_UNAVAILABLE;
|
||
ig_ensure_baselines();
|
||
int hits = 0, stop = 0;
|
||
Dl_info info;
|
||
if (!dladdr(addr, &info) || !info.dli_fname) {
|
||
ig_emit(sink, ctx, &stop, &hits, IG_UNAVAILABLE, IG_REASON_VERIFICATION_UNAVAILABLE,
|
||
IG_DIAG_STR(name ? name : "?"), IG_DIAG_STR("无法确认函数提供方"));
|
||
return IG_UNAVAILABLE;
|
||
}
|
||
if (!ig_is_system_path(info.dli_fname)) {
|
||
#if IG_DIAGNOSTIC_STRINGS
|
||
char ev[256];
|
||
snprintf(ev, sizeof(ev), "解析地址 %p 提供方=%s(非系统路径)",
|
||
addr, info.dli_fname ? info.dli_fname : "?");
|
||
ig_emit(sink, ctx, &stop, &hits, IG_DETECTED, IG_REASON_SYMBOL_PROVENANCE,
|
||
name ? name : "?", ev);
|
||
#else
|
||
ig_emit(sink, ctx, &stop, &hits, IG_DETECTED, IG_REASON_SYMBOL_PROVENANCE,
|
||
NULL, NULL);
|
||
#endif
|
||
return IG_DETECTED;
|
||
}
|
||
return ig_check_prologue(IG_DIAG_STR(name ? name : "?"), addr, sink, ctx, &stop, &hits);
|
||
}
|
||
|
||
int ig_anti_hook_check_default(IGFindingSink sink, void *ctx) {
|
||
int has_objc = ig_has_loaded_image_substr(kIGObjcNeedEnc, sizeof(kIGObjcNeedEnc));
|
||
int has_sec = ig_has_loaded_image_substr(kIGSecNeedEnc, sizeof(kIGSecNeedEnc));
|
||
|
||
char buf[IG_HOOK_BASELINE_MAX][40];
|
||
const char *ptrs[IG_HOOK_BASELINE_MAX];
|
||
size_t n = 0;
|
||
size_t total = sizeof(kIGDefaultSpecs) / sizeof(kIGDefaultSpecs[0]);
|
||
for (size_t i = 0; i < total && n < IG_HOOK_BASELINE_MAX; ++i) {
|
||
if (kIGDefaultSpecs[i].group == IG_GROUP_OBJC && !has_objc) continue;
|
||
if (kIGDefaultSpecs[i].group == IG_GROUP_SECURITY && !has_sec) continue;
|
||
ig_decode_bytes(kIGDefaultSpecs[i].bytes, kIGDefaultSpecs[i].len, buf[n], sizeof(buf[n]));
|
||
ptrs[n] = buf[n];
|
||
++n;
|
||
}
|
||
|
||
int hits = ig_anti_hook_check(ptrs, n, sink, ctx);
|
||
ig_hook_wipe(buf, sizeof(buf));
|
||
return hits;
|
||
}
|