- 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
542 lines
22 KiB
C
542 lines
22 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. inline hook(32 字节指令窗 + 启动期基线快照):首指令 B、全通用寄存器 x0~x30 的
|
||
* LDR Xt/BR Xt、ADRP+ADD+BR、MOVK+BR 蹦床,以及相对 constructor 基线前 32 字节变化。
|
||
* 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>
|
||
|
||
#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) {
|
||
if ((first & 0xfc000000u) == 0x14000000u) return 1;
|
||
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 ((ins[0] & 0xfc000000u) == 0x14000000u) return 1;
|
||
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;
|
||
}
|
||
|
||
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;
|
||
}
|
||
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;
|
||
}
|
||
return 0u;
|
||
}
|
||
|
||
/* ---- 启动期 32 字节基线快照 ---- */
|
||
|
||
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 nwords = ig_read_code_window(addr, words);
|
||
if (nwords < 2u || ig_arm64_suspicious_window(words, nwords)) continue;
|
||
IGHookBaselineSlot *slot = &g_hook_baselines[g_hook_baseline_count++];
|
||
slot->addr = ig_strip(addr);
|
||
slot->len = nwords * sizeof(uint32_t);
|
||
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];
|
||
size_t nwords = ig_read_code_window(addr, code);
|
||
if (nwords < 2u) {
|
||
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_DIAGNOSTIC_STRINGS
|
||
char ev[256];
|
||
snprintf(ev, sizeof(ev), "指令窗命中跳转蹦床 @%p [%08x %08x]", addr, code[0], code[1]);
|
||
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 IG_DIAGNOSTIC_STRINGS
|
||
char ev[256];
|
||
snprintf(ev, sizeof(ev), "前%zu字节偏离启动基线 @%p [%08x %08x]",
|
||
base->len, addr, code[0], code[1]);
|
||
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;
|
||
}
|