在 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>
144 lines
5.4 KiB
C
144 lines
5.4 KiB
C
/*
|
||
* 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
|
||
}
|