feat(defender): 可集成静态库 + readoor 接入方案(检测→分级→数据毒化闭环)

在 iOSGuard 侧产出可集成产物与接入文档,不修改 readoor 代码。

- defender/: anti_hook/anti_frida/anti_debug/anti_jailbreak 探针 + syscall 直发、
  policy 分级层、poison 数据依赖毒化、dispatch/resolve/call_bridge 等子模块
- poison: 三副本冗余、无分支掩码;健康恒等、毒化破坏 key(替代 if-exit 处置)
- policy: STRONG/MEDIUM/WEAK 分级,守误报底线,仅 DETECTED 参与、组合命中才毒化
- anti_jailbreak: IGSyscall 直发 svc 检测越狱文件(400)/沙盒逃逸(401),绕 libc hook
- tools/build_defender_lib.sh: 一键交叉编译打包 libIGDefender.a(arm64) + umbrella 头
- docs/18_READOOR_DEFENDER_INTEGRATION.md: 精确到文件/bridging/替换点/代码片段
- docs/05-17 + evidence + tests + macho_guard/static_sdk_redirect 工具链

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-11-12 22:16:47 +09:00
co-authored by Claude Opus 4.8
parent ff0aee7f47
commit 599e1da5ce
91 changed files with 8751 additions and 0 deletions
+76
View File
@@ -0,0 +1,76 @@
/*
* IGResolve.c — 加密名 + 带校验的符号解析实现。
*
* 复用 IGAntiHook 的 ig_verify_function 做「放行前校验」:dlsym 得到地址后,必须 provenance
* (提供方系统路径)+ 序言(非 inline-hook 蹦床)双过,否则返回 NULL——把「被挟持的实现」挡在调用前。
*/
#include "IGResolve.h"
#include "../anti_hook/IGAntiHook.h"
#include "../primitives/IGInline.h"
#include <dlfcn.h>
#include <crt_externs.h> /* _NSGetEnviron */
char *ig_decrypt(const unsigned char *enc, size_t len, char *out, size_t cap) {
if (!out || cap==0) return NULL;
out[0]='\0';
if (!enc || len==0 || len>=cap) return NULL;
for (size_t i = 0; i < len; ++i) {
out[i] = (char)(enc[i] ^ IG_ENC_KEY);
if (out[i]=='\0') { ig_wipe(out,cap); return NULL; }
}
out[len] = '\0';
return out;
}
void ig_wipe(void *p, size_t n) {
if (!p) return;
volatile unsigned char *v = (volatile unsigned char *)p;
while (n--) *v++ = 0; /* volatile:防编译器把清零 DCE 掉 */
}
IGResolveResult ig_resolve_verified(void *handle, const unsigned char *enc_name, size_t sym_len,
IGFindingSink sink, void *ctx) {
char name[256];
IGResolveResult result={IG_UNAVAILABLE,NULL};
if (!ig_decrypt(enc_name, sym_len, name, sizeof(name))) return result;
void *addr = dlsym(handle ? handle : RTLD_DEFAULT, name);
if (addr) {
/* 放行前校验:provenance + inline-hook。不过即判被挟持,返回 NULL。 */
result.status=ig_verify_function(addr,name,sink,ctx);
if (result.status==IG_CLEAR) result.address=addr;
} else if (sink) {
IGFinding f={0};
f.status=IG_UNAVAILABLE; f.reason=IG_REASON_SYMBOL_UNRESOLVED;
sink(&f,ctx);
}
ig_wipe(name, sizeof(name)); /* 明文符号名用后清零,不驻留栈 */
return result;
}
void *ig_dlsym_verified(void *handle, const unsigned char *enc_name, size_t sym_len) {
return ig_resolve_verified(handle,enc_name,sym_len,NULL,NULL).address;
}
void *ig_dlopen_hidden(const unsigned char *enc_path, size_t path_len, int mode) {
char path[512];
if (!ig_decrypt(enc_path, path_len, path, sizeof(path))) return NULL;
void *h = dlopen(path, mode);
ig_wipe(path, sizeof(path));
return h;
}
const char *ig_getenv(const char *name) {
char ***environment = _NSGetEnviron();
if (!environment || !*environment || !name || !name[0]) return NULL;
char *const *envp = *environment;
size_t nlen = ig_strlen(name);
for (; *envp; ++envp) {
const char *e = *envp;
/* 匹配 "name=" 前缀 */
if (ig_strncmp(e, name, nlen) == 0 && e[nlen] == '=') {
return e + nlen + 1;
}
}
return NULL;
}
+61
View File
@@ -0,0 +1,61 @@
/*
* IGResolve.h — 加密名 + 带校验的自定义符号解析(落地方案第二优先级)。
*
* 问题:核心逻辑直接 dlopen("…")/dlsym(h,"…") 会在 __cstring 留下明文库名/符号名,
* 既暴露意图(攻击者 grep 字符串即知你在找什么),__got 里的 dlopen/dlsym 本身也是 hook 靶点。
*
* 本模块:
* 1) 编译期 XOR 混淆字符串(IG_ENC 宏),运行期在**栈上**解密、用完即清零,明文不驻留常量区。
* 2) 解析后**校验再放行**:dladdr 确认提供方在系统路径白名单内 + 校验函数序言首字节不是
* inline-hook 蹦床(B / LDR-literal+BR x16/x17),命中则返回 NULL(视作被挟持,拒绝使用)。
* 3) ig_getenv 经 _NSGetEnviron 手工遍历,规避对 getenv 的 hook/特征。
*
* 边界:dlopen/dlsym 本身仍会被解析/调用(仅隐藏其**参数**与**对结果验真**);若需连 dlsym 调用
* 也抹除,改走 Export Trie 手工遍历(本头预留 ig_resolve_in_image,当前实现以 dlsym 为主路径)。
* 字符串混淆是“抬高静态分析成本”,非加密强度;密钥在二进制内,决心足够者可还原——与 svc 号混淆同理。
*/
#ifndef IG_RESOLVE_H
#define IG_RESOLVE_H
#include <stddef.h>
#include <stdint.h>
#include "../include/IGDefender.h"
#ifdef __cplusplus
extern "C" {
#endif
/* 单字节 XOR 密钥(与 IGSyscall 的掩码独立)。 */
#define IG_ENC_KEY 0x3Cu
/*
* 栈上解密:把 enc(已 XOR)解到调用方缓冲 out(容量 cap),返回 out。
* 用完务必 ig_wipe 清零,避免解密后的明文在栈/寄存器残留被 dump。
*/
char *ig_decrypt(const unsigned char *enc, size_t len, char *out, size_t cap);
void ig_wipe(void *p, size_t n);
/*
* 带校验的 dlsym:在 handle(NULL=RTLD_DEFAULT)中解析名为 enc_name(XOR 编码) 的符号,
* 成功后对结果做 provenance + 序言校验;任一不过返回 NULL。sym_len 为编码串长度(不含结尾)。
*/
void *ig_dlsym_verified(void *handle, const unsigned char *enc_name, size_t sym_len);
typedef struct { IGStatus status; void *address; } IGResolveResult;
IGResolveResult ig_resolve_verified(void *handle, const unsigned char *enc_name, size_t sym_len,
IGFindingSink sink, void *ctx);
/*
* 带隐藏名的 dlopen:以 enc_path(XOR 编码) 打开库,返回 handle 或 NULL。
* 仅隐藏库路径字符串;是否可信由后续 ig_dlsym_verified 的校验保证。
*/
void *ig_dlopen_hidden(const unsigned char *enc_path, size_t path_len, int mode);
/* 经 _NSGetEnviron 手工查环境变量,规避 getenv 符号。未命中返回 NULL。name 为明文(短、非敏感)。 */
const char *ig_getenv(const char *name);
#ifdef __cplusplus
}
#endif
#endif /* IG_RESOLVE_H */