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:
co-authored by
Claude Opus 4.8
parent
ff0aee7f47
commit
599e1da5ce
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* IGAntiInject.c — 代码注入与 Frida 运行时检测实现。
|
||||
*
|
||||
* 全部公开 API:<mach-o/dyld.h>(镜像枚举)、getenv(dyld 环境变量)、
|
||||
* task_threads + thread_info(THREAD_EXTENDED_INFO)(线程名)、BSD socket(端口弱探测)。
|
||||
*
|
||||
* 边界:
|
||||
* - 镜像特征按文件名子串匹配,越狱/注入框架会更名规避——命中是强证据,未命中不等于干净。
|
||||
* - 端口探测仅弱信号(docs/05:不以单一端口判定);默认不启用,需显式传 probe_port。
|
||||
* - 线程名依赖目标进程把 frida 线程命名为已知值,新版本可能改名——同为组合证据之一。
|
||||
*/
|
||||
#include "IGAntiInject.h"
|
||||
#include "IGInline.h" /* ig_strstr/ig_strcmp/ig_strncmp/ig_strlen:抹除探针自身对 libc 匹配函数的依赖 */
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <crt_externs.h> /* _NSGetEnviron:手工遍历环境表,替代可被 hook 的 getenv */
|
||||
#include <mach-o/dyld.h>
|
||||
#include <mach/mach.h>
|
||||
#include <mach/thread_act.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
/*
|
||||
* 经 _NSGetEnviron 手工遍历环境表取变量,规避对 getenv 的 hook/特征。
|
||||
* 关键:攻击者只需 hook getenv 让其对 "DYLD_INSERT_LIBRARIES" 返回 NULL,
|
||||
* 即可让下方第 3 步「插入库」检测彻底失明;本函数不经 libc getenv,关闭该盲点。
|
||||
* 仅用 ig_* 内联原语比较,不引入新的可 hook 导入符号。
|
||||
*/
|
||||
static const char *ig_local_getenv(const char *name) {
|
||||
char ***envp = _NSGetEnviron();
|
||||
if (!name || !envp || !*envp) return NULL;
|
||||
size_t nlen = ig_strlen(name);
|
||||
for (char **e = *envp; *e; ++e) {
|
||||
const char *entry = *e;
|
||||
if (ig_strncmp(entry, name, nlen) == 0 && entry[nlen] == '=') {
|
||||
return entry + nlen + 1;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* 已知注入 / 调试 / 越狱增强框架的镜像名子串(小写)。 */
|
||||
static const char *const kInjectSignatures[] = {
|
||||
"frida", "gadget", "cynject", "cycript", "libcycript",
|
||||
"substrate", "substitute", "libsubstrate", "tweakinject",
|
||||
"rocketbootstrap", "reveal", "flexloader", "libhooker",
|
||||
"shadowhook", "dobby", "fishhook",
|
||||
};
|
||||
|
||||
/* 已知 frida 线程名。 */
|
||||
static const char *const kFridaThreads[] = {
|
||||
"gum-js-loop", "gmain", "gdbus", "pool-frida", "pool-spawner", "linjector",
|
||||
};
|
||||
|
||||
static void ig_emit2(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));
|
||||
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 (st == IG_DETECTED) (*hits)++;
|
||||
if (sink && sink(&f, ctx) != 0) *stop = 1;
|
||||
}
|
||||
|
||||
/* 小写化到缓冲区,便于大小写无关子串匹配。 */
|
||||
static void ig_lower(const char *src, char *dst, size_t cap) {
|
||||
size_t i = 0;
|
||||
for (; src && src[i] && i + 1 < cap; ++i) {
|
||||
char c = src[i];
|
||||
dst[i] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c;
|
||||
}
|
||||
dst[i] = '\0';
|
||||
}
|
||||
|
||||
static int ig_is_system_path2(const char *path) {
|
||||
if (!path) return 0;
|
||||
return ig_strncmp(path, "/usr/lib/", 9) == 0 ||
|
||||
ig_strncmp(path, "/System/", 8) == 0 ||
|
||||
/* iOS 16+ 系统库经 Cryptex 挂载(/private/preboot/Cryptexes/OS/...),系统自身非注入。 */
|
||||
ig_strncmp(path, "/private/preboot/Cryptexes/", 27) == 0;
|
||||
}
|
||||
|
||||
int ig_anti_inject_check(const char *bundle_path, IGFindingSink sink, void *ctx) {
|
||||
int hits = 0, stop = 0;
|
||||
|
||||
/* 1+2. 镜像扫描。 */
|
||||
uint32_t imgc = _dyld_image_count();
|
||||
for (uint32_t i = 0; i < imgc && !stop; ++i) {
|
||||
const char *name = _dyld_get_image_name(i);
|
||||
if (!name) continue;
|
||||
/* Apple 系统镜像(/System、/usr/lib、Cryptex)只读且 Apple 签名,永不作为注入证据先行跳过:
|
||||
* 否则子串匹配会把 Apple 私有框架(如 RevealCore 命中 "reveal")误判为注入;
|
||||
* Xcode 调试还会加载 GPUToolsCapture/libViewDebuggerSupport 等系统库。
|
||||
* 真正的 frida/substrate/gadget 装在 Bundle 内或越狱路径(/Library、/var),不在此列。 */
|
||||
if (ig_is_system_path2(name)) continue;
|
||||
|
||||
char low[1024];
|
||||
ig_lower(name, low, sizeof(low));
|
||||
|
||||
int matched = 0;
|
||||
for (size_t s = 0; s < sizeof(kInjectSignatures)/sizeof(kInjectSignatures[0]); ++s) {
|
||||
if (ig_strstr(low, kInjectSignatures[s])) {
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev), "镜像=%s 命中特征=%s", name, kInjectSignatures[s]);
|
||||
ig_emit2(sink, ctx, &stop, &hits, IG_DETECTED, IG_REASON_INJECTED_IMAGE, name, ev);
|
||||
matched = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (matched) continue;
|
||||
|
||||
/* 非系统、非特征命中:若在 Bundle 外则作为弱信号上报。 */
|
||||
if (bundle_path && ig_strncmp(name, bundle_path, ig_strlen(bundle_path)) != 0) {
|
||||
ig_emit2(sink, ctx, &stop, &hits, IG_DETECTED, IG_REASON_FOREIGN_IMAGE, name,
|
||||
"Bundle 外非系统镜像(弱信号,需结合其它证据)");
|
||||
}
|
||||
}
|
||||
|
||||
/* 3. DYLD_INSERT_LIBRARIES 逐条解析。 */
|
||||
const char *dyld = ig_local_getenv("DYLD_INSERT_LIBRARIES");
|
||||
if (dyld && dyld[0] && !stop) {
|
||||
char buf[4096];
|
||||
strncpy(buf, dyld, sizeof(buf)-1); buf[sizeof(buf)-1] = '\0';
|
||||
char *save = NULL;
|
||||
for (char *tok = strtok_r(buf, ":", &save); tok && !stop;
|
||||
tok = strtok_r(NULL, ":", &save)) {
|
||||
int in_bundle = (bundle_path && ig_strncmp(tok, bundle_path, ig_strlen(bundle_path)) == 0);
|
||||
/* 排除 Apple 系统路径条目:Xcode 调试经 DYLD_INSERT_LIBRARIES 注入
|
||||
* /usr/lib/libViewDebuggerSupport.dylib、/System/.../GPUToolsCapture 等,非恶意注入。
|
||||
* 越狱 frida/substrate 的插入库位于 Bundle 内或非系统路径,仍会命中。 */
|
||||
if (!in_bundle && !ig_is_system_path2(tok)) {
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev), "DYLD_INSERT_LIBRARIES 条目=%s(Bundle 外)", tok);
|
||||
ig_emit2(sink, ctx, &stop, &hits, IG_DETECTED, IG_REASON_DYLD_INSERT, tok, ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
return hits;
|
||||
}
|
||||
|
||||
/* 遍历本进程线程,匹配 frida 线程名。 */
|
||||
static void ig_scan_threads(IGFindingSink sink, void *ctx, int *stop, int *hits) {
|
||||
thread_act_array_t threads;
|
||||
mach_msg_type_number_t count = 0;
|
||||
if (task_threads(mach_task_self(), &threads, &count) != KERN_SUCCESS) {
|
||||
ig_emit2(sink, ctx, stop, hits, IG_UNAVAILABLE, IG_REASON_FRIDA_THREAD,
|
||||
"task_threads", "无法枚举线程");
|
||||
return;
|
||||
}
|
||||
for (mach_msg_type_number_t i = 0; i < count; ++i) {
|
||||
thread_extended_info_data_t info;
|
||||
mach_msg_type_number_t icount = THREAD_EXTENDED_INFO_COUNT;
|
||||
if (thread_info(threads[i], THREAD_EXTENDED_INFO,
|
||||
(thread_info_t)&info, &icount) != KERN_SUCCESS) continue;
|
||||
if (info.pth_name[0] == '\0') continue;
|
||||
for (size_t t = 0; t < sizeof(kFridaThreads)/sizeof(kFridaThreads[0]); ++t) {
|
||||
if (ig_strcmp(info.pth_name, kFridaThreads[t]) == 0) {
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev), "线程名=%s 命中 frida 特征", info.pth_name);
|
||||
ig_emit2(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_FRIDA_THREAD,
|
||||
info.pth_name, ev);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (*stop) break;
|
||||
}
|
||||
/* 回收端口。 */
|
||||
for (mach_msg_type_number_t i = 0; i < count; ++i) {
|
||||
mach_port_deallocate(mach_task_self(), threads[i]);
|
||||
}
|
||||
vm_deallocate(mach_task_self(), (vm_address_t)threads, count * sizeof(thread_act_t));
|
||||
}
|
||||
|
||||
/* 非阻塞 connect 到 127.0.0.1:port,可连通即弱信号。 */
|
||||
static int ig_port_open(int port) {
|
||||
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0) return 0;
|
||||
fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||
struct sockaddr_in sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_port = htons((uint16_t)port);
|
||||
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
int rc = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
|
||||
int open = 0;
|
||||
if (rc == 0) {
|
||||
open = 1;
|
||||
} else if (errno == EINPROGRESS) {
|
||||
fd_set wset; FD_ZERO(&wset); FD_SET(fd, &wset);
|
||||
struct timeval tv = { .tv_sec = 0, .tv_usec = 200000 }; /* 200ms 预算 */
|
||||
if (select(fd + 1, NULL, &wset, NULL, &tv) > 0) {
|
||||
int err = 0; socklen_t len = sizeof(err);
|
||||
getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
|
||||
open = (err == 0);
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
return open;
|
||||
}
|
||||
|
||||
int ig_anti_frida_check(int probe_port, IGFindingSink sink, void *ctx) {
|
||||
int hits = 0, stop = 0;
|
||||
ig_scan_threads(sink, ctx, &stop, &hits);
|
||||
if (probe_port && !stop) {
|
||||
if (ig_port_open(probe_port)) {
|
||||
char ev[128];
|
||||
snprintf(ev, sizeof(ev), "127.0.0.1:%d 可连接(弱信号)", probe_port);
|
||||
char sub[32]; snprintf(sub, sizeof(sub), "port:%d", probe_port);
|
||||
ig_emit2(sink, ctx, &stop, &hits, IG_DETECTED, IG_REASON_FRIDA_PORT, sub, ev);
|
||||
}
|
||||
}
|
||||
return hits;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* IGAntiInject.h — 代码注入与 Frida 运行时检测探针。
|
||||
*
|
||||
* 覆盖(docs/05 §5.C「镜像/白名单」「已知工具线索」+ §4 P1 DYLD 逐项解析):
|
||||
* 1. 镜像扫描:已加载镜像匹配已知注入/调试框架特征(substrate/frida/cycript/reveal…)→ 强证据。
|
||||
* 2. Bundle 外非系统镜像:弱信号(可能是合法 dylib),作为组合证据上报。
|
||||
* 3. DYLD_INSERT_LIBRARIES:逐条解析(非整串前缀),任何条目指向 Bundle 外路径即上报。
|
||||
* 4. Frida 运行时:线程名特征(gum-js-loop/gmain/pool-frida…)+ 默认端口(弱信号)。
|
||||
*
|
||||
* 只采集证据,不处置。单一端口/名称命中不作独立判定,交上层组合。
|
||||
*/
|
||||
#ifndef IG_ANTI_INJECT_H
|
||||
#define IG_ANTI_INJECT_H
|
||||
|
||||
#include "IGDefender.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 注入检测:镜像特征扫描 + Bundle 外镜像 + DYLD_INSERT_LIBRARIES 逐项解析。
|
||||
* bundle_path:App 主 bundle 路径(.app 目录);用于判定镜像/插入库是否在 Bundle 内。
|
||||
* 传 NULL 则跳过「Bundle 外」判定,仅做特征匹配与 dyld env。
|
||||
* 返回 DETECTED 条数。
|
||||
*/
|
||||
int ig_anti_inject_check(const char *bundle_path, IGFindingSink sink, void *ctx);
|
||||
|
||||
/*
|
||||
* Frida 运行时检测:线程名特征 + 默认端口探测(弱信号)。
|
||||
* probe_port 非 0 时才做端口连接探测(默认 27042);0 表示只做线程名扫描。
|
||||
* 返回 DETECTED 条数。
|
||||
*/
|
||||
int ig_anti_frida_check(int probe_port, IGFindingSink sink, void *ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* IG_ANTI_INJECT_H */
|
||||
Reference in New Issue
Block a user