feat: 集成 LLVM IR 混淆体系、Route 2 全量合规与系统调用调度加固
- 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
This commit is contained in:
+337
-65
@@ -1,27 +1,33 @@
|
||||
/*
|
||||
* IGAntiHook.c — 关键函数 HOOK 完整性探针实现。
|
||||
*
|
||||
* 全部基于公开 API:<dlfcn.h>(dlsym/dladdr)、<mach-o/dyld.h>(镜像枚举)、
|
||||
* <mach-o/loader.h>/<nlist.h>(运行时解析自身镜像的 GOT 槽,fishhook 同款走法,用于「校验」而非改写)。
|
||||
* 全部基于公开 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 去签名;若工具链无 ptrauth,
|
||||
* 对认证槽的判定标记为 unavailable,不强行比较。
|
||||
* - chained fixups:运行时 dyld 绑定完成后,__got 槽持最终(slide 后)地址,本探针读到的即该值;
|
||||
* 未绑定/惰性未触发的槽会与 trusted 不等——仅对非惰性 __got(S_NON_LAZY_SYMBOL_POINTERS)判 rebind,
|
||||
* 惰性槽(__la_symbol_ptr)首次调用前可能仍指向 helper,故不判为重绑定,只在 provenance/inline 层兜底。
|
||||
* - arm64e:__auth_got 槽为带签名指针,比较前用 ptrauth_strip 去签名。
|
||||
*/
|
||||
#include "IGAntiHook.h"
|
||||
#include "IGInline.h" /* ig_strcmp/ig_strncmp/ig_strlen:抹除探针自身对 libc 匹配函数的依赖 */
|
||||
#include "IGInline.h"
|
||||
#include "../resolve/IGResolve.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.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)
|
||||
@@ -33,42 +39,168 @@
|
||||
# define IG_HAS_PTRAUTH 0
|
||||
#endif
|
||||
|
||||
/* 报告命中的 12 个关键符号(不带下划线)。 */
|
||||
static const char *const kIGDefaultSymbols[] = {
|
||||
"fopen", "dlopen", "dlsym", "write", "read", "open",
|
||||
"close", "exit", "fork", "syscall", "unlink", "getpid",
|
||||
#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';
|
||||
}
|
||||
|
||||
/* ---- 工具:系统提供方路径白名单 ---- */
|
||||
|
||||
/* dli_fname 是否落在系统镜像路径(含 dyld 共享缓存里的系统 dylib)。 */
|
||||
static int ig_is_system_path(const char *path) {
|
||||
if (!path) return 0;
|
||||
static const char *const prefixes[] = {
|
||||
"/usr/lib/", "/System/", "/usr/lib/system/",
|
||||
/* iOS 16+ 系统库经 Cryptex 挂载,dladdr 报此前缀(非越狱,系统自身)。 */
|
||||
"/private/preboot/Cryptexes/",
|
||||
};
|
||||
for (size_t i = 0; i < sizeof(prefixes)/sizeof(prefixes[0]); ++i) {
|
||||
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;
|
||||
memset(&f, 0, sizeof(f));
|
||||
ig_memset(&f, 0, sizeof(f));
|
||||
f.status = st;
|
||||
f.reason = reason;
|
||||
if (subject) { strncpy(f.subject, subject, sizeof(f.subject)-1); }
|
||||
if (evidence) { strncpy(f.evidence, evidence, sizeof(f.evidence)-1); }
|
||||
#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;
|
||||
}
|
||||
|
||||
/* 去 PAC(arm64e 认证指针);无 ptrauth 时原样返回。 */
|
||||
static const void *ig_strip(const void *p) {
|
||||
#if IG_HAS_PTRAUTH
|
||||
return ptrauth_strip(p, ptrauth_key_asia);
|
||||
@@ -77,59 +209,177 @@ static const void *ig_strip(const void *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. 符号提供方 ---- */
|
||||
/* trusted = dlsym(RTLD_DEFAULT,name);其宿主镜像若非系统路径,说明被 interpose/注入抢占。 */
|
||||
|
||||
static void *ig_check_provenance(const char *name, IGFindingSink sink, void *ctx,
|
||||
int *stop, int *hits) {
|
||||
void *addr = dlsym(RTLD_DEFAULT, name);
|
||||
void *addr = ig_resolve_export_default(name);
|
||||
if (!addr) {
|
||||
ig_emit(sink, ctx, stop, hits, IG_UNAVAILABLE, IG_REASON_SYMBOL_UNRESOLVED,
|
||||
name, "dlsym(RTLD_DEFAULT) 返回 NULL");
|
||||
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, "无法确认符号提供方");
|
||||
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;
|
||||
}
|
||||
|
||||
/* ---- 2. inline hook:序言首指令 ---- */
|
||||
int ig_arm64_suspicious_prologue(uint32_t first, uint32_t second) {
|
||||
if ((first & 0xfc000000u)==0x14000000u) return 1;
|
||||
uint32_t rt=first & 31u, rn=(second>>5)&31u;
|
||||
return (first & 0xff000000u)==0x58000000u && (rt==16 || rt==17)
|
||||
&& (second & 0xfffffc1fu)==0xd61f0000u && rn==rt;
|
||||
}
|
||||
/* arm64:首指令为无条件 B,或 (LDR-literal → x16/x17) + (BR x16/x17) 蹦床,判为 inline hook。 */
|
||||
static IGStatus ig_check_prologue(const char *name, void *addr, IGFindingSink sink, void *ctx,
|
||||
int *stop, int *hits) {
|
||||
int *stop, int *hits) {
|
||||
#if defined(__arm64__) || defined(__aarch64__)
|
||||
uint32_t code[2];
|
||||
vm_size_t copied=0;
|
||||
if (vm_read_overwrite(mach_task_self(), (vm_address_t)ig_strip(addr),
|
||||
sizeof(code), (vm_address_t)code, &copied) != KERN_SUCCESS || copied!=sizeof(code)) {
|
||||
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, "无法读取函数代码");
|
||||
name, IG_DIAG_STR("无法读取函数代码"));
|
||||
return IG_UNAVAILABLE;
|
||||
}
|
||||
const uint32_t *ins = code;
|
||||
uint32_t w0 = ins[0];
|
||||
uint32_t w1 = ins[1];
|
||||
int hook = ig_arm64_suspicious_prologue(w0,w1);
|
||||
const char *kind = "首指令 B 或 LDR literal + 同寄存器 BR 蹦床";
|
||||
if (hook) {
|
||||
if (ig_arm64_suspicious_window(code, nwords)) {
|
||||
#if IG_DIAGNOSTIC_STRINGS
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev), "%s @%p 指令[%08x %08x]", kind, addr, w0, w1);
|
||||
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
|
||||
@@ -139,8 +389,7 @@ static IGStatus ig_check_prologue(const char *name, void *addr, IGFindingSink si
|
||||
}
|
||||
|
||||
/* ---- 3. GOT 重绑定:扫本进程非系统镜像的非惰性符号指针槽 ---- */
|
||||
/* trusted_map:name→trusted addr 的平行数组。对每个镜像的 __got 槽,若其绑定符号在集合内
|
||||
* 且当前值去 PAC 后既不等于 trusted、宿主又非系统路径,则判 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) {
|
||||
@@ -148,10 +397,9 @@ static void ig_check_got_one_image(uint32_t img_index,
|
||||
(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; /* 只查自有镜像(app/嵌入 framework) */
|
||||
if (ig_is_system_path(img_path)) return;
|
||||
intptr_t slide = _dyld_get_image_vmaddr_slide(img_index);
|
||||
|
||||
/* 先定位 __LINKEDIT、LC_SYMTAB、LC_DYSYMTAB。 */
|
||||
const struct segment_command_64 *linkedit = NULL;
|
||||
const struct symtab_command *symtab = NULL;
|
||||
const struct dysymtab_command *dysymtab = NULL;
|
||||
@@ -175,7 +423,6 @@ static void ig_check_got_one_image(uint32_t img_index,
|
||||
const char *strtab = (const char *)(linkedit_base + symtab->stroff);
|
||||
const uint32_t *indirect = (const uint32_t *)(linkedit_base + dysymtab->indirectsymoff);
|
||||
|
||||
/* 遍历各 section,挑非惰性符号指针段(__got / __auth_got)。 */
|
||||
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;
|
||||
@@ -197,16 +444,20 @@ static void ig_check_got_one_image(uint32_t img_index,
|
||||
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; /* 一致=正常转发 */
|
||||
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; /* 仍指向系统实现,放行 */
|
||||
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,
|
||||
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;
|
||||
@@ -220,11 +471,11 @@ static void ig_check_got_one_image(uint32_t img_index,
|
||||
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;
|
||||
|
||||
/* 逐符号:provenance + inline-hook,并收集 trusted 地址表供 GOT 检查复用。 */
|
||||
void *trusted[64];
|
||||
size_t n = count < 64 ? count : 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;
|
||||
@@ -232,7 +483,6 @@ int ig_anti_hook_check(const char *const *names, size_t count,
|
||||
if (stop) return hits;
|
||||
}
|
||||
|
||||
/* GOT 重绑定:遍历全部镜像,内部只查非系统镜像。 */
|
||||
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);
|
||||
@@ -241,29 +491,51 @@ int ig_anti_hook_check(const char *const *names, size_t count,
|
||||
}
|
||||
|
||||
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;
|
||||
/* provenance:地址提供方须在系统路径白名单内。 */
|
||||
Dl_info info;
|
||||
if (!dladdr(addr, &info) || !info.dli_fname) {
|
||||
ig_emit(sink, ctx, &stop, &hits, IG_UNAVAILABLE, IG_REASON_VERIFICATION_UNAVAILABLE,
|
||||
name ? name : "?", "无法确认函数提供方");
|
||||
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;
|
||||
}
|
||||
/* inline-hook 序言。 */
|
||||
return ig_check_prologue(name ? name : "?", addr, sink, ctx, &stop, &hits);
|
||||
return ig_check_prologue(IG_DIAG_STR(name ? name : "?"), addr, sink, ctx, &stop, &hits);
|
||||
}
|
||||
|
||||
int ig_anti_hook_check_default(IGFindingSink sink, void *ctx) {
|
||||
return ig_anti_hook_check(kIGDefaultSymbols,
|
||||
sizeof(kIGDefaultSymbols)/sizeof(kIGDefaultSymbols[0]),
|
||||
sink, 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user