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
+79
View File
@@ -0,0 +1,79 @@
/*
* IGDefender.h — iOSGuard 运行时防护公共结果模型。
*
* 设计遵循 docs/05 §5.C:探针(probe)与处置(policy)分离。
* 探针只采集证据并通过回调上报,绝不自行 exit/中断业务;是否限制功能由上层策略决定(观察模式优先)。
* 状态三态 clear/detected/unavailable——unavailable(平台/架构不支持、数据缺失)不得当作安全。
*
* 纯 C,无 Objective-C / Swift 依赖,可直接编入任意 iOS target(也可在 macOS 宿主上编译自测)。
*/
#ifndef IG_DEFENDER_H
#define IG_DEFENDER_H
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
IG_CLEAR = 0, /* 未发现异常证据 */
IG_DETECTED = 1, /* 命中威胁证据 */
IG_UNAVAILABLE = 2, /* 无法判定:架构/平台不支持或数据缺失——不得视为安全 */
} IGStatus;
/* 原因码按模块分段:1xx = anti_hook,2xx = 注入/Frida。 */
typedef enum {
IG_REASON_NONE = 0,
/* anti_hook */
IG_REASON_SYMBOL_PROVENANCE = 100, /* 符号解析到非系统提供方(dyld interpose / 注入覆盖) */
IG_REASON_INLINE_HOOK = 101, /* 关键函数序言被改写为跳转蹦床(Frida/Dobby inline hook) */
IG_REASON_GOT_REBIND = 102, /* 自身镜像 GOT/符号指针槽被重绑定到非系统地址(fishhook 类) */
IG_REASON_SYMBOL_UNRESOLVED = 103, /* dlsym 无法解析该符号(unavailable) */
IG_REASON_VERIFICATION_UNAVAILABLE = 104, /* 提供方/代码页无法确认 */
IG_REASON_DISPATCH_TAMPER = 105, /* 私有调度地址或初始化后的代码前缀变化 */
IG_REASON_DISPATCH_UNAVAILABLE = 106, /* 调度入口无法安全使用 */
/* 107 已分配:Swift 观察层 archive-dispatch 不可用(RDRuntimeDefender.swift),C 侧不占用 */
IG_REASON_OBJC_IMP_CHANGED = 110, /* ObjC 关键方法 IMP 相对基线变化(swizzle/替换/函数体改写) */
/* 注入 / Frida */
IG_REASON_INJECTED_IMAGE = 200, /* 加载了已知注入/调试框架镜像(substrate/frida/cycript…) */
IG_REASON_FOREIGN_IMAGE = 201, /* Bundle 外且非系统路径的镜像(弱信号,需结合其它证据) */
IG_REASON_DYLD_INSERT = 202, /* DYLD_INSERT_LIBRARIES 指向 Bundle 外路径 */
IG_REASON_FRIDA_THREAD = 203, /* 线程名命中 frida 运行时特征(gum-js-loop 等) */
IG_REASON_FRIDA_PORT = 204, /* 默认 frida 端口可连接(弱信号) */
/* anti_debug */
IG_REASON_DEBUGGER_ATTACHED = 300, /* 直发 sysctl 检出 P_TRACED:进程被调试器跟踪 */
/* anti_jailbreak(4xx)——均走 IGSyscall 直发 svc,绕过 libc 层对 stat/access 的 hook */
IG_REASON_JAILBREAK_FILE = 400, /* 越狱特征文件/目录可访问(Cydia/Sileo/apt/substrate/var-jb…) */
IG_REASON_JAILBREAK_SANDBOX = 401, /* 能在 App 沙盒外创建文件:沙盒逃逸=越狱强信号 */
} IGReason;
typedef struct {
IGStatus status; /* 本条发现的状态 */
int reason; /* IGReason */
char subject[128]; /* 受检对象:符号名 / 镜像路径 / 线程名 */
char evidence[256];/* 证据详情:地址、提供方路径、端口等人读文本 */
} IGFinding;
/*
* 发现回调。探针对每条 DETECTED/UNAVAILABLE 证据调用一次(CLEAR 不上报,保持安静)。
* sink 返回非 0 表示请求提前停止该探针的后续扫描;返回 0 继续。
* ctx 由调用方透传(可用于计数、聚合、打点)。
*/
typedef int (*IGFindingSink)(const IGFinding *finding, void *ctx);
/*
* 聚合入口:一次性运行 anti_hook(provenance/inline/GOT)+ 注入/Frida 全部探针。
* bundle_path:App 主 bundle(.app)路径,用于 Bundle 外镜像/插入库判定;NULL 则跳过该判定。
* probe_port:非 0 时才做 frida 端口弱探测(典型 27042);0 表示跳过。
* 返回 DETECTED 证据总条数。本函数只采集不处置,绝不 exit。
*/
int ig_defender_scan(const char *bundle_path, int probe_port,
IGFindingSink sink, void *ctx);
#ifdef __cplusplus
}
#endif
#endif /* IG_DEFENDER_H */
+20
View File
@@ -0,0 +1,20 @@
/*
* IGGuard.h — iOSGuard Defender 对外统一公共头(umbrella)。
*
* 接入方(如 readoor)只需在 bridging header 里 `#import "IGGuard.h"`,即可拿到:
* - 结果模型:IGStatus / IGReason / IGFinding / IGFindingSink(见 IGDefender.h)
* - 聚合扫描:ig_defender_scan(只采集证据,绝不处置)
* - 策略层:ig_policy_scan_and_mark(扫描→分级→命中强信号组合则毒化,守误报底线)
* - 毒化原语:ig_poison_init / ig_poison_apply(数据依赖处置,接到解密密钥派生)
*
* Header Search Paths 需同时包含 defender/ 与 defender/include/(IGGuard.h 内部按此相对路径引用)。
* 典型用法见 docs/18_READOOR_DEFENDER_INTEGRATION.md。
*/
#ifndef IG_GUARD_H
#define IG_GUARD_H
#include "IGDefender.h" /* IGStatus/IGReason/IGFinding/IGFindingSink + ig_defender_scan */
#include "poison/IGPoison.h" /* ig_poison_init / ig_poison_mark / ig_poison_apply */
#include "policy/IGPolicy.h" /* ig_policy_scan_and_mark / ig_policy_sink / IGPolicyState */
#endif /* IG_GUARD_H */
+75
View File
@@ -0,0 +1,75 @@
/*
* IGInline.h — 强制内联的自实现 str/mem 原语(落地方案第一优先级)。
*
* 目的:核心校验/鉴权/加解密逻辑里避免直接调用 strcmp/memcmp/memcpy/strstr/getenv 等
* 被攻击者重点监控的 libc 函数——这些调用会在 __la_symbol_ptr/__got 留下符号引用,
* 既是 Fishhook rebind 的靶点,也是静态分析的锚点。
*
* 本头提供 __attribute__((always_inline)) 版本:使用处直接展开为离散汇编指令,
* 不生成 bl 跳转、不产生导入表项,从而从导入表中抹除这些符号。
*
* 用法:在安全逻辑中用 ig_memcmp/ig_strcmp/... 替换 libc 同名函数。纯头文件,零依赖。
* 注意:这是“隐藏特征”,非“更安全的实现”——不做常量时间比较;需防时序侧信道时另行处理。
*/
#ifndef IG_INLINE_H
#define IG_INLINE_H
#include <stddef.h>
#include <stdint.h>
#define IG_AINLINE static inline __attribute__((always_inline))
IG_AINLINE size_t ig_strlen(const char *s) {
const char *p = s;
while (*p) ++p;
return (size_t)(p - s);
}
IG_AINLINE int ig_memcmp(const void *a, const void *b, size_t n) {
const unsigned char *x = (const unsigned char *)a;
const unsigned char *y = (const unsigned char *)b;
for (size_t i = 0; i < n; ++i) {
if (x[i] != y[i]) return (int)x[i] - (int)y[i];
}
return 0;
}
IG_AINLINE void *ig_memcpy(void *dst, const void *src, size_t n) {
unsigned char *d = (unsigned char *)dst;
const unsigned char *s = (const unsigned char *)src;
for (size_t i = 0; i < n; ++i) d[i] = s[i];
return dst;
}
IG_AINLINE void *ig_memset(void *dst, int c, size_t n) {
unsigned char *d = (unsigned char *)dst;
for (size_t i = 0; i < n; ++i) d[i] = (unsigned char)c;
return dst;
}
IG_AINLINE int ig_strcmp(const char *a, const char *b) {
while (*a && (*a == *b)) { ++a; ++b; }
return (int)(unsigned char)*a - (int)(unsigned char)*b;
}
IG_AINLINE int ig_strncmp(const char *a, const char *b, size_t n) {
for (size_t i = 0; i < n; ++i) {
unsigned char ca = (unsigned char)a[i], cb = (unsigned char)b[i];
if (ca != cb) return (int)ca - (int)cb;
if (ca == 0) break;
}
return 0;
}
/* 朴素子串匹配,避免引用 libc strstr。 */
IG_AINLINE const char *ig_strstr(const char *hay, const char *needle) {
if (!needle[0]) return hay;
for (; *hay; ++hay) {
const char *h = hay, *n = needle;
while (*h && *n && (*h == *n)) { ++h; ++n; }
if (!*n) return hay;
}
return (const char *)0;
}
#endif /* IG_INLINE_H */