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
+143
View File
@@ -0,0 +1,143 @@
/*
* IGSyscall.c — ARM64 混淆直发系统调用实现。
*
* ABI(本会话真机/宿主实测):调用号入 x16(arm64 BSD 调用**不加** 0x2000000 掩码,那是 x86_64 的
* class 位);参数 x0..x5;`svc #0x80`;返回值在 x0,出错时 carry 置位、x0=errno。
* 本封装对出错统一返回 -errno,便于调用方判负。
*/
#include "IGSyscall.h"
/* 运行期密钥,volatile 防止编译器把 enc^key 折叠回明文立即数。构造函数置值,提供默认兜底。 */
volatile long ig_sc_key = IG_SC_CMASK;
__attribute__((constructor))
static void ig_sc_init(void) {
/* 显式再置一次:即使该 TU 的 constructor 被裁剪,静态初值也保证正确。 */
ig_sc_key = IG_SC_CMASK;
}
#if defined(__aarch64__)
/*
* 直发核心。真实调用号 = enc_num ^ ig_sc_key,在运行期于寄存器内还原后载入 x16,
* 二进制里既无 `movz x16,#<真实号>` 立即数,也无该号的明文常量。
* carry 置位(出错)时返回 -x0(errno),否则返回 x0。
*/
long ig_syscall6(long enc_num, long a0, long a1, long a2, long a3, long a4, long a5) {
register long x16 __asm__("x16") = enc_num ^ ig_sc_key;
register long x0 __asm__("x0") = a0;
register long x1 __asm__("x1") = a1;
register long x2 __asm__("x2") = a2;
register long x3 __asm__("x3") = a3;
register long x4 __asm__("x4") = a4;
register long x5 __asm__("x5") = a5;
register long cc __asm__("x8"); /* 承接 carry 标志 */
__asm__ __volatile__(
"svc #0x80\n"
"cset %[cc], cs\n" /* carry set → 出错 */
: "+r"(x0), [cc] "=r"(cc)
: "r"(x16), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5)
: "cc", "memory");
return cc ? -x0 : x0;
}
#else
/* 非 arm64(宿主自测可能是 x86_64):退化为 unavailable 语义,返回 -1。 */
long ig_syscall6(long enc_num, long a0, long a1, long a2, long a3, long a4, long a5) {
(void)enc_num; (void)a0; (void)a1; (void)a2; (void)a3; (void)a4; (void)a5;
return -1;
}
#endif
/* BSD 调用号(arm64,无 class 掩码)。本会话实测:getpid=20、write=4 已验证。 */
#define IG_NR_EXIT 1
#define IG_NR_READ 3
#define IG_NR_WRITE 4
#define IG_NR_OPEN 5
#define IG_NR_CLOSE 6
#define IG_NR_UNLINK 10
#define IG_NR_GETPID 20
#define IG_NR_ACCESS 33
#define IG_NR_SYSCTL 202
int ig_sys_getpid(void) {
return (int)ig_syscall6(IG_SC(IG_NR_GETPID), 0, 0, 0, 0, 0, 0);
}
long ig_sys_write(int fd, const void *buf, size_t n) {
return ig_syscall6(IG_SC(IG_NR_WRITE), (long)fd, (long)buf, (long)n, 0, 0, 0);
}
long ig_sys_read(int fd, void *buf, size_t n) {
return ig_syscall6(IG_SC(IG_NR_READ), (long)fd, (long)buf, (long)n, 0, 0, 0);
}
int ig_sys_open(const char *path, int flags, int mode) {
return (int)ig_syscall6(IG_SC(IG_NR_OPEN), (long)path, (long)flags, (long)mode, 0, 0, 0);
}
int ig_sys_close(int fd) {
return (int)ig_syscall6(IG_SC(IG_NR_CLOSE), (long)fd, 0, 0, 0, 0, 0);
}
int ig_sys_access(const char *path, int amode) {
return (int)ig_syscall6(IG_SC(IG_NR_ACCESS), (long)path, (long)amode, 0, 0, 0, 0);
}
int ig_sys_unlink(const char *path) {
return (int)ig_syscall6(IG_SC(IG_NR_UNLINK), (long)path, 0, 0, 0, 0, 0);
}
void ig_sys_exit(int code) {
ig_syscall6(IG_SC(IG_NR_EXIT), (long)code, 0, 0, 0, 0, 0);
for (;;) { } /* 不返回 */
}
/*
* 调试器检测:sysctl(mib={CTL_KERN,KERN_PROC,KERN_PROC_PID,pid}) 读 kinfo_proc.kp_proc.p_flag,
* P_TRACED(0x800) 置位即被调试。直发 svc,不经 libc 的 sysctl(避开对其的 hook)。
* 为避免依赖 <sys/sysctl.h>/<sys/proc.h> 的完整布局,这里用常量与偏移手工构造。
*/
#define IG_CTL_KERN 1
#define IG_KERN_PROC 14
#define IG_KERN_PROC_PID 1
#define IG_P_TRACED 0x00000800
int ig_sys_is_debugged(void) {
#if defined(__aarch64__)
/*
* struct kinfo_proc 在 arm64 iOS/macOS 上,kp_proc.p_flag 为结构体起始处的第一个 int(偏移 0)。
* 该布局自 BSD 以来稳定:extern_proc 以 p_un(union,8B) 起,随后 p_flag。为稳妥,取足够大的缓冲,
* 并从 p_flag 的已知偏移读取。extern_proc:前 8B 为 union 指针/ru,接着 struct timeval(16B? ) —
* 不同平台有差异,故用保守做法:定位 p_flag 偏移为 extern_proc 内 0x10。
*/
unsigned char buf[648]; /* sizeof(struct kinfo_proc)==648(本机 offsetof 实测) */
for (size_t i = 0; i < sizeof(buf); ++i) buf[i] = 0;
int mib[4];
mib[0] = IG_CTL_KERN;
mib[1] = IG_KERN_PROC;
mib[2] = IG_KERN_PROC_PID;
mib[3] = ig_sys_getpid();
size_t len = sizeof(buf);
/* sysctl(name, namelen, oldp, oldlenp, newp, newlen) */
long r = ig_syscall6(IG_SC(IG_NR_SYSCTL), (long)mib, 4, (long)buf, (long)&len, 0, 0);
if (r < 0) return -1; /* unavailable */
/*
* p_flag 在 kinfo_proc 内的字节偏移:kp_proc 位于 kinfo_proc 偏移 0,extern_proc.p_flag 偏移 32——
* 两者均由本机 `offsetof(struct kinfo_proc, kp_proc)==0` + `offsetof(struct extern_proc, p_flag)==32`
* 实测得到(见 scratchpad/off.c)。故 p_flag 偏移 = 0x20。LP64 ABI 稳定。
*/
int p_flag;
const unsigned char *pf = buf + 0x20;
p_flag = (int)((unsigned)pf[0] | ((unsigned)pf[1] << 8) |
((unsigned)pf[2] << 16) | ((unsigned)pf[3] << 24));
return (p_flag & IG_P_TRACED) ? 1 : 0;
#else
return -1;
#endif
}
+61
View File
@@ -0,0 +1,61 @@
/*
* IGSyscall.h — ARM64 混淆直发系统调用(落地方案第一优先级)。
*
* 文件/进程/安全类系统调用若经 libsystem_kernel.dylib 走,libSystem 的函数入口可被 inline hook、
* __got 槽可被 fishhook 重绑定。本模块用内联汇编 `svc #0x80` 直接进内核,绕过 C 库封装层——
* 那条可被 hook 的 libSystem 入口在这条路径上根本不存在。
*
* 反静态特征:IDA 等常搜索 `svc #0x80` 并读 x16 的立即数识别调用号。本模块不把真实调用号写成
* `movz x16,#n` 立即数,而是调用方传入 XOR 编码值(IG_SC 宏),运行期与 ig_sc_key 异或还原后
* 由寄存器载入 x16,使静态扫描读不到真实号。
*
* 边界(如实声明,与 docs/05 §2 一致):
* - 仅 arm64。直发系统调用绕过 libSystem,跨 iOS 版本的调用号/ABI 稳定性由调用方负责;
* Apple 审核对直发 svc 可能关注,非 App Store 主线,用于确需防 hook 的关键路径。
* - 错误语义:返回值 <0 不自动转 errno;错误时内核置 carry、x0=errno,本封装统一返回 -errno。
* - 变参/取消点/EINTR 等 libc 语义不代为处理,调用方按需补。
*/
#ifndef IG_SYSCALL_H
#define IG_SYSCALL_H
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/* 编译期 XOR 编码掩码:调用方用 IG_SC(真实号) 传参,真实号不以明文/立即数进入二进制。 */
#define IG_SC_CMASK 0x5Al
#define IG_SC(n) ((long)((long)(n) ^ IG_SC_CMASK))
/* 运行期密钥(构造函数置为 IG_SC_CMASK,经 volatile 存取避免被常量折叠回立即数)。 */
extern volatile long ig_sc_key;
/*
* 通用直发。enc_num:IG_SC(真实调用号)。a0..a5:参数(不足补 0)。
* 成功返回内核结果(x0);失败返回 -errno。
*/
long ig_syscall6(long enc_num, long a0, long a1, long a2, long a3, long a4, long a5);
/* 常用封装(均走 ig_syscall6,调用号经 IG_SC 编码)。 */
int ig_sys_getpid(void);
long ig_sys_write(int fd, const void *buf, size_t n);
long ig_sys_read(int fd, void *buf, size_t n);
int ig_sys_open(const char *path, int flags, int mode);
int ig_sys_close(int fd);
int ig_sys_access(const char *path, int amode); /* 直发 access(2);0=可访问,<0=-errno */
int ig_sys_unlink(const char *path); /* 直发 unlink(2);0=成功,<0=-errno */
void ig_sys_exit(int code) __attribute__((noreturn));
/*
* 调试器检测:直发 sysctl(KERN_PROC/KERN_PROC_PID) 读 kinfo_proc.p_flag & P_TRACED。
* 返回 1=被跟踪,0=未跟踪,-1=unavailable(sysctl 失败)。不经 libc,规避对 sysctl 的 hook。
*/
int ig_sys_is_debugged(void);
#ifdef __cplusplus
}
#endif
#endif /* IG_SYSCALL_H */