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,27 @@
|
||||
/*
|
||||
* IGDefender.c — 运行时防护聚合入口。
|
||||
*
|
||||
* 一次性运行 anti_hook + 注入/Frida 全部探针,证据统一经 sink 上报;不做任何处置。
|
||||
* 典型用法(观察模式):App 启动 / 回前台 / 关键操作前调用 ig_defender_scan,
|
||||
* 由上层策略根据 reason 组合决定是否限制功能——本库绝不 exit。
|
||||
*/
|
||||
#include "IGDefender.h"
|
||||
#include "anti_hook/IGAntiHook.h"
|
||||
#include "anti_hook/IGObjCBaseline.h"
|
||||
#include "anti_frida/IGAntiInject.h"
|
||||
#include "anti_debug/IGAntiDebug.h"
|
||||
#include "anti_jailbreak/IGAntiJailbreak.h"
|
||||
#include "dispatch/IGDispatch.h"
|
||||
|
||||
int ig_defender_scan(const char *bundle_path, int probe_port,
|
||||
IGFindingSink sink, void *ctx) {
|
||||
int hits = 0;
|
||||
hits += ig_dispatch_check(sink, ctx);
|
||||
hits += ig_anti_hook_check_default(sink, ctx);
|
||||
hits += ig_objc_baseline_check(sink, ctx);
|
||||
hits += ig_anti_inject_check(bundle_path, sink, ctx);
|
||||
hits += ig_anti_frida_check(probe_port, sink, ctx);
|
||||
hits += ig_anti_debug_check(sink, ctx);
|
||||
hits += ig_anti_jb_check(sink, ctx);
|
||||
return hits;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* IGAntiDebug.c — 调试器跟踪探针实现。
|
||||
* 全部逻辑在 syscall 模块(ig_sys_is_debugged,直发 svc + P_TRACED),
|
||||
* 这里只做三态到 IGFinding 的映射与上报。
|
||||
*/
|
||||
#include "IGAntiDebug.h"
|
||||
#include "syscall/IGSyscall.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int ig_anti_debug_check(IGFindingSink sink, void *ctx) {
|
||||
int traced = ig_sys_is_debugged();
|
||||
if (traced == 1) {
|
||||
IGFinding f;
|
||||
__builtin_memset(&f, 0, sizeof(f));
|
||||
f.status = IG_DETECTED;
|
||||
f.reason = IG_REASON_DEBUGGER_ATTACHED;
|
||||
snprintf(f.subject, sizeof(f.subject), "kinfo_proc.p_flag&P_TRACED");
|
||||
snprintf(f.evidence, sizeof(f.evidence), "直发 sysctl 检出进程被调试器跟踪");
|
||||
if (sink) sink(&f, ctx);
|
||||
return 1;
|
||||
}
|
||||
if (traced < 0) {
|
||||
IGFinding f;
|
||||
__builtin_memset(&f, 0, sizeof(f));
|
||||
f.status = IG_UNAVAILABLE;
|
||||
f.reason = IG_REASON_DEBUGGER_ATTACHED;
|
||||
snprintf(f.subject, sizeof(f.subject), "kinfo_proc.p_flag&P_TRACED");
|
||||
snprintf(f.evidence, sizeof(f.evidence), "sysctl 查询失败,无法判定");
|
||||
if (sink) sink(&f, ctx);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* IGAntiDebug.h — 调试器跟踪探针(接入聚合扫描)。
|
||||
*
|
||||
* 复用 syscall 模块的 ig_sys_is_debugged():直发 sysctl 读 kinfo_proc 的
|
||||
* P_TRACED 位(公开 API 语义,docs/12 已在 lldb 下实测命中),此前该能力
|
||||
* 已实现但未进入 ig_defender_scan 聚合,策略层无从消费——本模块补上接线。
|
||||
* 三态映射:1 → DETECTED(300),0 → clear,-1 → UNAVAILABLE(300)。
|
||||
*/
|
||||
#ifndef IG_ANTI_DEBUG_H
|
||||
#define IG_ANTI_DEBUG_H
|
||||
|
||||
#include "IGDefender.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 调试器跟踪检测:只采集证据不处置(绝不自行 ptrace/exit)。返回 DETECTED 条数。 */
|
||||
int ig_anti_debug_check(IGFindingSink sink, void *ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* IG_ANTI_DEBUG_H */
|
||||
@@ -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 */
|
||||
@@ -0,0 +1,269 @@
|
||||
/*
|
||||
* IGAntiHook.c — 关键函数 HOOK 完整性探针实现。
|
||||
*
|
||||
* 全部基于公开 API:<dlfcn.h>(dlsym/dladdr)、<mach-o/dyld.h>(镜像枚举)、
|
||||
* <mach-o/loader.h>/<nlist.h>(运行时解析自身镜像的 GOT 槽,fishhook 同款走法,用于「校验」而非改写)。
|
||||
*
|
||||
* 边界(如实声明,unavailable 不等于安全):
|
||||
* - 仅支持 arm64 / x86_64(LP64)。非 64 位镜像跳过。
|
||||
* - arm64e:__auth_got 槽为带签名指针,比较前用 ptrauth_strip 去签名;若工具链无 ptrauth,
|
||||
* 对认证槽的判定标记为 unavailable,不强行比较。
|
||||
* - chained fixups:运行时 dyld 绑定完成后,__got 槽持最终(slide 后)地址,本探针读到的即该值;
|
||||
* 未绑定/惰性未触发的槽会与 trusted 不等——仅对非惰性 __got(S_NON_LAZY_SYMBOL_POINTERS)判 rebind,
|
||||
* 惰性槽(__la_symbol_ptr)首次调用前可能仍指向 helper,故不判为重绑定,只在 provenance/inline 层兜底。
|
||||
*/
|
||||
#include "IGAntiHook.h"
|
||||
#include "IGInline.h" /* ig_strcmp/ig_strncmp/ig_strlen:抹除探针自身对 libc 匹配函数的依赖 */
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <dlfcn.h>
|
||||
#include <mach-o/dyld.h>
|
||||
#include <mach-o/loader.h>
|
||||
#include <mach-o/nlist.h>
|
||||
#include <mach/mach.h>
|
||||
|
||||
#if defined(__has_feature)
|
||||
# if __has_feature(ptrauth_calls)
|
||||
# include <ptrauth.h>
|
||||
# define IG_HAS_PTRAUTH 1
|
||||
# endif
|
||||
#endif
|
||||
#ifndef IG_HAS_PTRAUTH
|
||||
# define IG_HAS_PTRAUTH 0
|
||||
#endif
|
||||
|
||||
/* 报告命中的 12 个关键符号(不带下划线)。 */
|
||||
static const char *const kIGDefaultSymbols[] = {
|
||||
"fopen", "dlopen", "dlsym", "write", "read", "open",
|
||||
"close", "exit", "fork", "syscall", "unlink", "getpid",
|
||||
};
|
||||
|
||||
/* ---- 工具:系统提供方路径白名单 ---- */
|
||||
|
||||
/* dli_fname 是否落在系统镜像路径(含 dyld 共享缓存里的系统 dylib)。 */
|
||||
static int ig_is_system_path(const char *path) {
|
||||
if (!path) return 0;
|
||||
static const char *const prefixes[] = {
|
||||
"/usr/lib/", "/System/", "/usr/lib/system/",
|
||||
/* iOS 16+ 系统库经 Cryptex 挂载,dladdr 报此前缀(非越狱,系统自身)。 */
|
||||
"/private/preboot/Cryptexes/",
|
||||
};
|
||||
for (size_t i = 0; i < sizeof(prefixes)/sizeof(prefixes[0]); ++i) {
|
||||
if (ig_strncmp(path, prefixes[i], ig_strlen(prefixes[i])) == 0) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void ig_emit(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;
|
||||
}
|
||||
|
||||
/* 去 PAC(arm64e 认证指针);无 ptrauth 时原样返回。 */
|
||||
static const void *ig_strip(const void *p) {
|
||||
#if IG_HAS_PTRAUTH
|
||||
return ptrauth_strip(p, ptrauth_key_asia);
|
||||
#else
|
||||
return p;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ---- 1. 符号提供方 ---- */
|
||||
/* trusted = dlsym(RTLD_DEFAULT,name);其宿主镜像若非系统路径,说明被 interpose/注入抢占。 */
|
||||
static void *ig_check_provenance(const char *name, IGFindingSink sink, void *ctx,
|
||||
int *stop, int *hits) {
|
||||
void *addr = dlsym(RTLD_DEFAULT, name);
|
||||
if (!addr) {
|
||||
ig_emit(sink, ctx, stop, hits, IG_UNAVAILABLE, IG_REASON_SYMBOL_UNRESOLVED,
|
||||
name, "dlsym(RTLD_DEFAULT) 返回 NULL");
|
||||
return NULL;
|
||||
}
|
||||
Dl_info info;
|
||||
if (!dladdr(addr, &info) || !info.dli_fname) {
|
||||
ig_emit(sink, ctx, stop, hits, IG_UNAVAILABLE, IG_REASON_VERIFICATION_UNAVAILABLE,
|
||||
name, "无法确认符号提供方");
|
||||
return NULL;
|
||||
}
|
||||
if (!ig_is_system_path(info.dli_fname)) {
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev), "解析地址 %p 提供方=%s(非系统路径)",
|
||||
addr, info.dli_fname ? info.dli_fname : "?");
|
||||
ig_emit(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_SYMBOL_PROVENANCE, name, ev);
|
||||
}
|
||||
return addr;
|
||||
}
|
||||
|
||||
/* ---- 2. inline hook:序言首指令 ---- */
|
||||
int ig_arm64_suspicious_prologue(uint32_t first, uint32_t second) {
|
||||
if ((first & 0xfc000000u)==0x14000000u) return 1;
|
||||
uint32_t rt=first & 31u, rn=(second>>5)&31u;
|
||||
return (first & 0xff000000u)==0x58000000u && (rt==16 || rt==17)
|
||||
&& (second & 0xfffffc1fu)==0xd61f0000u && rn==rt;
|
||||
}
|
||||
/* arm64:首指令为无条件 B,或 (LDR-literal → x16/x17) + (BR x16/x17) 蹦床,判为 inline hook。 */
|
||||
static IGStatus ig_check_prologue(const char *name, void *addr, IGFindingSink sink, void *ctx,
|
||||
int *stop, int *hits) {
|
||||
#if defined(__arm64__) || defined(__aarch64__)
|
||||
uint32_t code[2];
|
||||
vm_size_t copied=0;
|
||||
if (vm_read_overwrite(mach_task_self(), (vm_address_t)ig_strip(addr),
|
||||
sizeof(code), (vm_address_t)code, &copied) != KERN_SUCCESS || copied!=sizeof(code)) {
|
||||
ig_emit(sink, ctx, stop, hits, IG_UNAVAILABLE, IG_REASON_VERIFICATION_UNAVAILABLE,
|
||||
name, "无法读取函数代码");
|
||||
return IG_UNAVAILABLE;
|
||||
}
|
||||
const uint32_t *ins = code;
|
||||
uint32_t w0 = ins[0];
|
||||
uint32_t w1 = ins[1];
|
||||
int hook = ig_arm64_suspicious_prologue(w0,w1);
|
||||
const char *kind = "首指令 B 或 LDR literal + 同寄存器 BR 蹦床";
|
||||
if (hook) {
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev), "%s @%p 指令[%08x %08x]", kind, addr, w0, w1);
|
||||
ig_emit(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_INLINE_HOOK, name, ev);
|
||||
return IG_DETECTED;
|
||||
}
|
||||
#else
|
||||
(void)name; (void)addr; (void)sink; (void)ctx; (void)stop; (void)hits;
|
||||
#endif
|
||||
return IG_CLEAR;
|
||||
}
|
||||
|
||||
/* ---- 3. GOT 重绑定:扫本进程非系统镜像的非惰性符号指针槽 ---- */
|
||||
/* trusted_map:name→trusted addr 的平行数组。对每个镜像的 __got 槽,若其绑定符号在集合内
|
||||
* 且当前值去 PAC 后既不等于 trusted、宿主又非系统路径,则判 GOT 重绑定。 */
|
||||
static void ig_check_got_one_image(uint32_t img_index,
|
||||
const char *const *names, void *const *trusted, size_t count,
|
||||
IGFindingSink sink, void *ctx, int *stop, int *hits) {
|
||||
const struct mach_header_64 *mh =
|
||||
(const struct mach_header_64 *)_dyld_get_image_header(img_index);
|
||||
if (!mh || mh->magic != MH_MAGIC_64) return;
|
||||
const char *img_path = _dyld_get_image_name(img_index);
|
||||
if (ig_is_system_path(img_path)) return; /* 只查自有镜像(app/嵌入 framework) */
|
||||
intptr_t slide = _dyld_get_image_vmaddr_slide(img_index);
|
||||
|
||||
/* 先定位 __LINKEDIT、LC_SYMTAB、LC_DYSYMTAB。 */
|
||||
const struct segment_command_64 *linkedit = NULL;
|
||||
const struct symtab_command *symtab = NULL;
|
||||
const struct dysymtab_command *dysymtab = NULL;
|
||||
const uint8_t *cmd = (const uint8_t *)(mh + 1);
|
||||
for (uint32_t i = 0; i < mh->ncmds; ++i) {
|
||||
const struct load_command *lc = (const struct load_command *)cmd;
|
||||
if (lc->cmd == LC_SEGMENT_64) {
|
||||
const struct segment_command_64 *seg = (const struct segment_command_64 *)lc;
|
||||
if (ig_strcmp(seg->segname, SEG_LINKEDIT) == 0) linkedit = seg;
|
||||
} else if (lc->cmd == LC_SYMTAB) {
|
||||
symtab = (const struct symtab_command *)lc;
|
||||
} else if (lc->cmd == LC_DYSYMTAB) {
|
||||
dysymtab = (const struct dysymtab_command *)lc;
|
||||
}
|
||||
cmd += lc->cmdsize;
|
||||
}
|
||||
if (!linkedit || !symtab || !dysymtab) return;
|
||||
|
||||
uintptr_t linkedit_base = (uintptr_t)slide + linkedit->vmaddr - linkedit->fileoff;
|
||||
const struct nlist_64 *symtab_list = (const struct nlist_64 *)(linkedit_base + symtab->symoff);
|
||||
const char *strtab = (const char *)(linkedit_base + symtab->stroff);
|
||||
const uint32_t *indirect = (const uint32_t *)(linkedit_base + dysymtab->indirectsymoff);
|
||||
|
||||
/* 遍历各 section,挑非惰性符号指针段(__got / __auth_got)。 */
|
||||
cmd = (const uint8_t *)(mh + 1);
|
||||
for (uint32_t i = 0; i < mh->ncmds; ++i) {
|
||||
const struct load_command *lc = (const struct load_command *)cmd;
|
||||
if (lc->cmd == LC_SEGMENT_64) {
|
||||
const struct segment_command_64 *seg = (const struct segment_command_64 *)lc;
|
||||
const struct section_64 *sect = (const struct section_64 *)(seg + 1);
|
||||
for (uint32_t s = 0; s < seg->nsects; ++s) {
|
||||
uint32_t type = sect[s].flags & SECTION_TYPE;
|
||||
if (type != S_NON_LAZY_SYMBOL_POINTERS) continue;
|
||||
size_t n = sect[s].size / sizeof(void *);
|
||||
const uint32_t *idxs = indirect + sect[s].reserved1;
|
||||
void **slots = (void **)((uintptr_t)slide + sect[s].addr);
|
||||
for (size_t k = 0; k < n; ++k) {
|
||||
uint32_t symi = idxs[k];
|
||||
if (symi == INDIRECT_SYMBOL_ABS || symi == INDIRECT_SYMBOL_LOCAL) continue;
|
||||
const char *sym = strtab + symtab_list[symi].n_un.n_strx;
|
||||
if (sym[0] != '_') continue;
|
||||
const char *bare = sym + 1;
|
||||
for (size_t m = 0; m < count; ++m) {
|
||||
if (ig_strcmp(bare, names[m]) != 0) continue;
|
||||
const void *cur = ig_strip(slots[k]);
|
||||
if (!trusted[m] || cur == trusted[m]) break; /* 一致=正常转发 */
|
||||
Dl_info di;
|
||||
const char *prov = (dladdr((void*)cur, &di) ? di.dli_fname : NULL);
|
||||
if (prov && ig_is_system_path(prov)) break; /* 仍指向系统实现,放行 */
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev),
|
||||
"镜像=%s 槽@%p 当前=%p(提供方=%s) 期望=%p",
|
||||
img_path ? img_path : "?", (void*)&slots[k], (void*)cur,
|
||||
prov ? prov : "?", trusted[m]);
|
||||
ig_emit(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_GOT_REBIND, sym, ev);
|
||||
break;
|
||||
}
|
||||
if (*stop) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
cmd += lc->cmdsize;
|
||||
}
|
||||
}
|
||||
|
||||
int ig_anti_hook_check(const char *const *names, size_t count,
|
||||
IGFindingSink sink, void *ctx) {
|
||||
if (!names || count == 0) return 0;
|
||||
int hits = 0, stop = 0;
|
||||
|
||||
/* 逐符号:provenance + inline-hook,并收集 trusted 地址表供 GOT 检查复用。 */
|
||||
void *trusted[64];
|
||||
size_t n = count < 64 ? count : 64;
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
void *addr = ig_check_provenance(names[i], sink, ctx, &stop, &hits);
|
||||
trusted[i] = addr;
|
||||
if (addr) ig_check_prologue(names[i], addr, sink, ctx, &stop, &hits);
|
||||
if (stop) return hits;
|
||||
}
|
||||
|
||||
/* GOT 重绑定:遍历全部镜像,内部只查非系统镜像。 */
|
||||
uint32_t imgc = _dyld_image_count();
|
||||
for (uint32_t i = 0; i < imgc && !stop; ++i) {
|
||||
ig_check_got_one_image(i, names, trusted, n, sink, ctx, &stop, &hits);
|
||||
}
|
||||
return hits;
|
||||
}
|
||||
|
||||
IGStatus ig_verify_function(void *addr, const char *name, IGFindingSink sink, void *ctx) {
|
||||
if (!addr) return IG_UNAVAILABLE;
|
||||
int hits = 0, stop = 0;
|
||||
/* provenance:地址提供方须在系统路径白名单内。 */
|
||||
Dl_info info;
|
||||
if (!dladdr(addr, &info) || !info.dli_fname) {
|
||||
ig_emit(sink, ctx, &stop, &hits, IG_UNAVAILABLE, IG_REASON_VERIFICATION_UNAVAILABLE,
|
||||
name ? name : "?", "无法确认函数提供方");
|
||||
return IG_UNAVAILABLE;
|
||||
}
|
||||
if (!ig_is_system_path(info.dli_fname)) {
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev), "解析地址 %p 提供方=%s(非系统路径)",
|
||||
addr, info.dli_fname ? info.dli_fname : "?");
|
||||
ig_emit(sink, ctx, &stop, &hits, IG_DETECTED, IG_REASON_SYMBOL_PROVENANCE,
|
||||
name ? name : "?", ev);
|
||||
return IG_DETECTED;
|
||||
}
|
||||
/* inline-hook 序言。 */
|
||||
return ig_check_prologue(name ? name : "?", addr, sink, ctx, &stop, &hits);
|
||||
}
|
||||
|
||||
int ig_anti_hook_check_default(IGFindingSink sink, void *ctx) {
|
||||
return ig_anti_hook_check(kIGDefaultSymbols,
|
||||
sizeof(kIGDefaultSymbols)/sizeof(kIGDefaultSymbols[0]),
|
||||
sink, ctx);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* IGAntiHook.h — 关键函数 HOOK 完整性探针。
|
||||
*
|
||||
* 覆盖三类 HOOK 形态(docs/05 §5.C「GOT/符号地址」「关键自有代码完整性」):
|
||||
* 1. 符号提供方异常:dlsym 解析到非系统镜像(dyld interpose / 注入 dylib 抢占定义)。
|
||||
* 2. inline hook:关键函数序言首指令被改写为无条件跳转或 LDR+BR x16 蹦床。
|
||||
* 3. GOT 重绑定:本进程镜像的 __got/__la_symbol_ptr 槽被 fishhook 类手法改指到非系统地址。
|
||||
*
|
||||
* 只采集证据,不处置。PAC/chained-fixups/arm64e 的边界见 .c 注释。
|
||||
*/
|
||||
#ifndef IG_ANTI_HOOK_H
|
||||
#define IG_ANTI_HOOK_H
|
||||
|
||||
#include "IGDefender.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 校验一批关键 libc/系统函数的运行时完整性。
|
||||
* names: 函数名数组(不带前导下划线,如 "write"/"read"/"dlsym")。
|
||||
* count: 数组长度。
|
||||
* 对每个符号依次做 provenance / inline-hook / GOT-rebind 检查,命中即经 sink 上报。
|
||||
* 返回 DETECTED 条数(0 表示本轮全部 clear 或仅 unavailable)。
|
||||
*/
|
||||
int ig_anti_hook_check(const char *const *names, size_t count,
|
||||
IGFindingSink sink, void *ctx);
|
||||
|
||||
/* 便捷入口:用内置关键符号集(报告命中的 12 个)执行 ig_anti_hook_check。 */
|
||||
int ig_anti_hook_check_default(IGFindingSink sink, void *ctx);
|
||||
|
||||
/*
|
||||
* 单点「调用前校验」:对一个**已解析**的函数地址做 provenance(dladdr 提供方须系统路径)
|
||||
* + inline-hook 序言检查。供 IGResolve 在放行解析结果前复用。
|
||||
* 命中经 sink 上报(可传 NULL 只取返回值)。
|
||||
* 返回 IG_CLEAR=干净可用 / IG_DETECTED=疑被挟持应拒用 / IG_UNAVAILABLE=addr 为空无法判定。
|
||||
*/
|
||||
IGStatus ig_verify_function(void *addr, const char *name, IGFindingSink sink, void *ctx);
|
||||
|
||||
/* ARM64 序言形态分类,可独立测试;命中只是一条证据,不覆盖所有 Hook。 */
|
||||
int ig_arm64_suspicious_prologue(uint32_t first, uint32_t second);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* IG_ANTI_HOOK_H */
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* IGObjCBaseline.c — ObjC 关键方法 IMP 基线探针实现。
|
||||
*
|
||||
* 全部公开 API:objc_getClass / sel_registerName / class_getMethodImplementation
|
||||
* (objc/runtime.h,纯 C)、dladdr(提供方)、vm_read_overwrite(代码前缀)。
|
||||
* method_getImplementation 返回"当前会被实际调用的实现"(已解析 swizzle),
|
||||
* 与基线不一致即说明方法被交换/替换——这正是要抓的形态。
|
||||
*
|
||||
* 线程模型:capture 由策略层在确定时点(如首帧后)调用一次;check 在扫描时点调用。
|
||||
* 两者并发属策略层误用,本模块不加锁(保持零依赖、可嵌入任意 target)。
|
||||
*/
|
||||
#include "IGObjCBaseline.h"
|
||||
#include "IGInline.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <dlfcn.h>
|
||||
#include <mach/mach.h>
|
||||
#include <objc/objc.h>
|
||||
#include <objc/runtime.h>
|
||||
|
||||
|
||||
#define IG_OBJC_MAX_WATCH 64
|
||||
#define IG_PREFIX_BYTES 16
|
||||
|
||||
typedef struct {
|
||||
const char *cls; /* 登记时的类名 */
|
||||
const char *sel; /* 登记时的选择子 */
|
||||
void *imp; /* 基线 IMP(去 PAC 后) */
|
||||
char prov[192]; /* 基线 IMP 提供方镜像路径 */
|
||||
unsigned char prefix[IG_PREFIX_BYTES];
|
||||
int have_prefix;
|
||||
} IGObjCSlot;
|
||||
|
||||
static IGObjCSlot g_slots[IG_OBJC_MAX_WATCH];
|
||||
static size_t g_nslots;
|
||||
|
||||
static const void *ig_strip(const void *p) {
|
||||
#if defined(__has_feature)
|
||||
# if __has_feature(ptrauth_calls)
|
||||
return ptrauth_strip(p, ptrauth_key_asia);
|
||||
# else
|
||||
return p;
|
||||
# endif
|
||||
#else
|
||||
return p;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void ig_emit_objc(IGFindingSink sink, void *ctx, int *stop, int *hits,
|
||||
IGStatus st, const char *subject, const char *evidence) {
|
||||
if (*stop) return;
|
||||
IGFinding f;
|
||||
memset(&f, 0, sizeof(f));
|
||||
f.status = st;
|
||||
f.reason = IG_REASON_OBJC_IMP_CHANGED;
|
||||
strncpy(f.subject, subject, sizeof(f.subject) - 1);
|
||||
strncpy(f.evidence, evidence, sizeof(f.evidence) - 1);
|
||||
if (st == IG_DETECTED) (*hits)++;
|
||||
if (sink && sink(&f, ctx) != 0) *stop = 1;
|
||||
}
|
||||
|
||||
/* 读取函数前 N 字节;失败返回 0(调用方按 unavailable 语义处理)。 */
|
||||
static int ig_read_prefix(const void *addr, unsigned char *out) {
|
||||
vm_size_t copied = 0;
|
||||
if (vm_read_overwrite(mach_task_self(), (vm_address_t)ig_strip(addr),
|
||||
IG_PREFIX_BYTES, (vm_address_t)out, &copied) != KERN_SUCCESS
|
||||
|| copied != IG_PREFIX_BYTES)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t ig_objc_baseline_capture(const IGObjCWatch *watches, size_t count) {
|
||||
g_nslots = 0;
|
||||
if (!watches) return 0;
|
||||
size_t ok = 0;
|
||||
for (size_t i = 0; i < count && g_nslots < IG_OBJC_MAX_WATCH; ++i) {
|
||||
if (!watches[i].class_name || !watches[i].selector) continue;
|
||||
Class c = objc_getClass(watches[i].class_name);
|
||||
if (!c) continue;
|
||||
Method m = class_getInstanceMethod(c, sel_registerName(watches[i].selector));
|
||||
if (!m) continue; /* 类存在但方法不可解析:不登记(check 语义与登记时一致) */
|
||||
IMP imp = method_getImplementation(m);
|
||||
if (!imp) continue;
|
||||
IGObjCSlot *slot = &g_slots[g_nslots++];
|
||||
slot->cls = watches[i].class_name;
|
||||
slot->sel = watches[i].selector;
|
||||
slot->imp = (void *)imp;
|
||||
slot->prov[0] = '\0';
|
||||
Dl_info info;
|
||||
if (dladdr((void *)imp, &info) && info.dli_fname)
|
||||
strncpy(slot->prov, info.dli_fname, sizeof(slot->prov) - 1);
|
||||
slot->have_prefix = ig_read_prefix((void *)imp, slot->prefix);
|
||||
++ok;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
int ig_objc_baseline_check(IGFindingSink sink, void *ctx) {
|
||||
if (g_nslots == 0) return 0; /* 未接入:clear no-op */
|
||||
int hits = 0, stop = 0;
|
||||
char subject[192], ev[256];
|
||||
for (size_t i = 0; i < g_nslots && !stop; ++i) {
|
||||
const IGObjCSlot *b = &g_slots[i];
|
||||
snprintf(subject, sizeof(subject), "-[%s %s]", b->cls, b->sel);
|
||||
Class c = objc_getClass(b->cls);
|
||||
if (!c) {
|
||||
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject,
|
||||
"基线登记后类不可解析(被卸载或运行时异常)");
|
||||
continue;
|
||||
}
|
||||
Method m = class_getInstanceMethod(c, sel_registerName(b->sel));
|
||||
if (!m || !method_getImplementation(m)) {
|
||||
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject,
|
||||
"方法实现不可解析(转发桩或已移除)");
|
||||
continue;
|
||||
}
|
||||
IMP imp = method_getImplementation(m);
|
||||
const void *cur = ig_strip(imp);
|
||||
if (cur != b->imp) {
|
||||
/* IMP 换人:典型 swizzle / class_replaceMethod / exchange。 */
|
||||
char cur_prov[192] = "?";
|
||||
Dl_info info;
|
||||
if (dladdr((void *)cur, &info) && info.dli_fname)
|
||||
strncpy(cur_prov, info.dli_fname, sizeof(cur_prov) - 1);
|
||||
snprintf(ev, sizeof(ev), "IMP %p(%s) → %p(%s)(实现被替换)",
|
||||
b->imp, b->prov[0] ? b->prov : "?", cur, cur_prov);
|
||||
ig_emit_objc(sink, ctx, &stop, &hits, IG_DETECTED, subject, ev);
|
||||
continue;
|
||||
}
|
||||
/* IMP 未变但代码前缀变:实现函数体被 inline patch。 */
|
||||
if (b->have_prefix) {
|
||||
unsigned char now[IG_PREFIX_BYTES];
|
||||
if (!ig_read_prefix((void *)cur, now)) {
|
||||
ig_emit_objc(sink, ctx, &stop, &hits, IG_UNAVAILABLE, subject,
|
||||
"代码前缀不可读");
|
||||
continue;
|
||||
}
|
||||
if (memcmp(now, b->prefix, IG_PREFIX_BYTES) != 0) {
|
||||
snprintf(ev, sizeof(ev), "IMP %p 不变但代码前缀变化(函数体被改写)", cur);
|
||||
ig_emit_objc(sink, ctx, &stop, &hits, IG_DETECTED, subject, ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
return hits;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* IGObjCBaseline.h — ObjC 关键方法 IMP 基线探针(swizzle / 方法替换检测)。
|
||||
*
|
||||
* 背景(docs/05 §4 P1):IGAntiHook 只覆盖 C 符号层(序言/GOT/提供方),对 ObjC 层
|
||||
* method_exchangeImplementations / class_replaceMethod / method_setImplementation
|
||||
* 等劫持手段此前零覆盖。本模块由策略层指定关键方法清单,启动后采集 IMP 基线
|
||||
* (实现地址 + 提供方镜像 + 代码前缀),扫描时比对——IMP 换人或函数体被改即上报。
|
||||
*
|
||||
* 边界(如实声明):
|
||||
* - 合法 swizzle(统计/广告 SDK 常见)也会改变 IMP:本探针只给证据,是否处置由
|
||||
* 策略层决定;基线采集时机本身是策略选择(早采集会把业务自身的合法交换计入)。
|
||||
* - 只比对策略层显式登记的方法,不做全类扫(误报面不可控)。
|
||||
* - 纯 C 调 objc runtime API,不引入 ObjC 编译依赖;未 capture 过时 check 为 no-op。
|
||||
*/
|
||||
#ifndef IG_OBJC_BASELINE_H
|
||||
#define IG_OBJC_BASELINE_H
|
||||
|
||||
#include "IGDefender.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 观察项:类名 + 选择子(如 { "RDOrderViewController", "payOrder" })。 */
|
||||
typedef struct {
|
||||
const char *class_name;
|
||||
const char *selector;
|
||||
} IGObjCWatch;
|
||||
|
||||
/*
|
||||
* 采集基线:对每个观察项记录当前 IMP 地址、提供方镜像路径与代码前缀 16 字节。
|
||||
* 幂等,重复调用以最后一次为准;count==0 清空基线。返回成功登记的条数
|
||||
* (类/选择子当时不可解析的条目不登记,check 时按 unavailable 上报)。
|
||||
*/
|
||||
size_t ig_objc_baseline_capture(const IGObjCWatch *watches, size_t count);
|
||||
|
||||
/*
|
||||
* 扫描:IMP 地址变化,或 IMP 相同但代码前缀变化 → DETECTED(110);
|
||||
* 类不可解析 / IMP 为转发桩 / 代码页不可读 → UNAVAILABLE(110)。
|
||||
* 未 capture 过时为 clear no-op(不惩罚未接入的调用方)。
|
||||
* 返回 DETECTED 条数;只采集证据不处置。
|
||||
*/
|
||||
int ig_objc_baseline_check(IGFindingSink sink, void *ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* IG_OBJC_BASELINE_H */
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* IGAntiJailbreak.c — 越狱检测探针实现。见 .h 的定位与边界。
|
||||
*
|
||||
* 全部文件探测经 IGSyscall 直发(access/open/close/unlink),不经 libc——规避 Shadow/Liberty
|
||||
* 等对 stat/access 的 hook。证据经 sink 上报,不做处置。
|
||||
*/
|
||||
#include "IGAntiJailbreak.h"
|
||||
#include "syscall/IGSyscall.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/* access 的 amode:F_OK=0(仅测存在/可访问)。open 标志(BSD/Darwin 值)。 */
|
||||
#define IG_F_OK 0
|
||||
#define IG_O_RDWR 0x0002
|
||||
#define IG_O_CREAT 0x0200
|
||||
#define IG_O_EXCL 0x0800
|
||||
|
||||
/*
|
||||
* 内置 iOS 越狱特征(强信号)。沙盒内正常 App 对这些路径 access 应得 -ENOENT/-EPERM;
|
||||
* 能访问即越狱。rootless 越狱重点在 /var/jb。TODO:改为 IG_ENC XOR 混淆,勿明文驻留。
|
||||
*/
|
||||
static const char *const kIGJailbreakPaths[] = {
|
||||
"/var/jb", /* rootless 越狱根 */
|
||||
"/Applications/Cydia.app",
|
||||
"/Applications/Sileo.app",
|
||||
"/Applications/Zebra.app",
|
||||
"/usr/sbin/sshd",
|
||||
"/usr/bin/ssh",
|
||||
"/bin/bash",
|
||||
"/bin/sh",
|
||||
"/etc/apt",
|
||||
"/private/var/lib/apt",
|
||||
"/usr/lib/substrate",
|
||||
"/Library/MobileSubstrate/MobileSubstrate.dylib",
|
||||
"/usr/lib/TweakInject",
|
||||
};
|
||||
|
||||
static void ig_jb_emit(IGFindingSink sink, void *ctx, int *hits,
|
||||
IGStatus st, int reason, const char *subject, const char *evidence) {
|
||||
IGFinding f;
|
||||
__builtin_memset(&f, 0, sizeof(f));
|
||||
f.status = st;
|
||||
f.reason = reason;
|
||||
if (subject) snprintf(f.subject, sizeof(f.subject), "%s", subject);
|
||||
if (evidence) snprintf(f.evidence, sizeof(f.evidence), "%s", evidence);
|
||||
if (st == IG_DETECTED) (*hits)++;
|
||||
if (sink) sink(&f, ctx);
|
||||
}
|
||||
|
||||
int ig_anti_jb_check_paths(const char *const *paths, size_t count,
|
||||
const char *sandbox_probe_path,
|
||||
IGFindingSink sink, void *ctx) {
|
||||
int hits = 0;
|
||||
|
||||
/* 1) 越狱特征文件:直发 access(path, F_OK)==0 即可访问。 */
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
if (!paths[i]) continue;
|
||||
if (ig_sys_access(paths[i], IG_F_OK) == 0) {
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev), "直发 access 可访问越狱特征:%s", paths[i]);
|
||||
ig_jb_emit(sink, ctx, &hits, IG_DETECTED, IG_REASON_JAILBREAK_FILE, paths[i], ev);
|
||||
}
|
||||
}
|
||||
|
||||
/* 2) 沙盒逃逸:在沙盒外路径直发 open(O_CREAT|O_EXCL) 创建文件,成功=越狱强信号。 */
|
||||
if (sandbox_probe_path) {
|
||||
ig_sys_unlink(sandbox_probe_path); /* 清理上次残留(忽略错误) */
|
||||
int fd = ig_sys_open(sandbox_probe_path, IG_O_RDWR | IG_O_CREAT | IG_O_EXCL, 0600);
|
||||
if (fd >= 0) {
|
||||
ig_sys_close(fd);
|
||||
ig_sys_unlink(sandbox_probe_path); /* 立即清理,不留痕 */
|
||||
char ev[256];
|
||||
snprintf(ev, sizeof(ev), "能在沙盒外创建文件:%s", sandbox_probe_path);
|
||||
ig_jb_emit(sink, ctx, &hits, IG_DETECTED, IG_REASON_JAILBREAK_SANDBOX,
|
||||
sandbox_probe_path, ev);
|
||||
}
|
||||
}
|
||||
|
||||
return hits;
|
||||
}
|
||||
|
||||
int ig_anti_jb_check(IGFindingSink sink, void *ctx) {
|
||||
return ig_anti_jb_check_paths(
|
||||
kIGJailbreakPaths,
|
||||
sizeof(kIGJailbreakPaths) / sizeof(kIGJailbreakPaths[0]),
|
||||
"/private/.ig_jb_probe", /* 沙盒外路径:正常 App 不可写 */
|
||||
sink, ctx);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* IGAntiJailbreak.h — 越狱检测探针(走 IGSyscall 直发 svc,绕过 libc 层 hook)。
|
||||
*
|
||||
* 定位:Shadow / Liberty / A-Bypass 这类反越狱检测插件主要 hook 在 libc/ObjC 层
|
||||
* (stat/access/fopen/NSFileManager)。本探针用 IGSyscall 的 svc #0x80 直发
|
||||
* access(2)/open(2),那条可被 hook 的 libSystem 入口在此路径上不存在——检测点下沉到
|
||||
* bypass 够不到的层。这是纯客户端对抗里"抬高绕过成本"的关键一环(非根治)。
|
||||
*
|
||||
* 只采集证据、不处置:命中经 sink 上报,是否毒化/降级由策略层([[iosguard-disposition-strategy]]
|
||||
* 的 ig_poison_mark)按强信号组合决定。本探针绝不自行 exit。
|
||||
*
|
||||
* 边界:
|
||||
* - 路径特征可被 rootless 越狱改位;未命中不等于干净(组合判定原则不变)。
|
||||
* - 当前骨架路径字符串为明文,静态可见、bypass 可针对——TODO 用 IGResolve 的 XOR 混淆
|
||||
* (IG_ENC),与 svc 号混淆同理。svc 直发本身已是主要价值,字符串混淆是第二层。
|
||||
* - 仅 arm64 直发;非 arm64(宿主 x86_64)syscall 退化 unavailable。
|
||||
*/
|
||||
#ifndef IG_ANTI_JAILBREAK_H
|
||||
#define IG_ANTI_JAILBREAK_H
|
||||
|
||||
#include "IGDefender.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 默认检测:内置 iOS 越狱特征表 + 沙盒逃逸探测。返回 DETECTED 条数。 */
|
||||
int ig_anti_jb_check(IGFindingSink sink, void *ctx);
|
||||
|
||||
/*
|
||||
* 可注入版本(供自测/定制)。
|
||||
* paths/count:要探测可访问性的路径表(access F_OK);命中记 reason=400。
|
||||
* sandbox_probe_path:非 NULL 时尝试在该(应在沙盒外的)路径创建文件,成功=沙盒逃逸,记 401
|
||||
* 并立即清理;NULL 跳过逃逸探测。
|
||||
*/
|
||||
int ig_anti_jb_check_paths(const char *const *paths, size_t count,
|
||||
const char *sandbox_probe_path,
|
||||
IGFindingSink sink, void *ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* IG_ANTI_JAILBREAK_H */
|
||||
@@ -0,0 +1,52 @@
|
||||
#include "IGFileBridge.h"
|
||||
#include <unistd.h>
|
||||
|
||||
#if !defined(__clang__)
|
||||
#error "此验证实现需要 Clang 的 optnone 属性"
|
||||
#endif
|
||||
|
||||
// 仅 SDK 的未定义引用重定向到此函数;本文件不能参与符号重定向。
|
||||
// 无日志、检测或全局可变状态,直接返回系统 FILE * 并保留 errno。
|
||||
// 间接调用不代表 GOT/Inline Hook 防护,仍须对最终 Release 单独验收。
|
||||
__attribute__((optnone, noinline))
|
||||
FILE *IG_fopen(const char *path, const char *mode) {
|
||||
FILE *(*function)(const char *, const char *) = fopen;
|
||||
return function(path, mode);
|
||||
}
|
||||
|
||||
// 各入口单独保持真实类型;不进行自动重试或短读短写补齐。
|
||||
__attribute__((optnone, noinline))
|
||||
int IG_fclose(FILE *stream) {
|
||||
int (*function)(FILE *) = fclose;
|
||||
return function(stream);
|
||||
}
|
||||
|
||||
__attribute__((optnone, noinline))
|
||||
size_t IG_fread(void *buffer, size_t size, size_t count, FILE *stream) {
|
||||
size_t (*function)(void *, size_t, size_t, FILE *) = fread;
|
||||
return function(buffer, size, count, stream);
|
||||
}
|
||||
|
||||
__attribute__((optnone, noinline))
|
||||
size_t IG_fwrite(const void *buffer, size_t size, size_t count, FILE *stream) {
|
||||
size_t (*function)(const void *, size_t, size_t, FILE *) = fwrite;
|
||||
return function(buffer, size, count, stream);
|
||||
}
|
||||
|
||||
__attribute__((optnone, noinline))
|
||||
ssize_t IG_read(int descriptor, void *buffer, size_t count) {
|
||||
ssize_t (*function)(int, void *, size_t) = read;
|
||||
return function(descriptor, buffer, count);
|
||||
}
|
||||
|
||||
__attribute__((optnone, noinline))
|
||||
ssize_t IG_write(int descriptor, const void *buffer, size_t count) {
|
||||
ssize_t (*function)(int, const void *, size_t) = write;
|
||||
return function(descriptor, buffer, count);
|
||||
}
|
||||
|
||||
__attribute__((optnone, noinline))
|
||||
int IG_close(int descriptor) {
|
||||
int (*function)(int) = close;
|
||||
return function(descriptor);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef IG_FILE_BRIDGE_H
|
||||
#define IG_FILE_BRIDGE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
FILE *IG_fopen(const char *path, const char *mode);
|
||||
int IG_fclose(FILE *stream);
|
||||
size_t IG_fread(void *buffer, size_t size, size_t count, FILE *stream);
|
||||
size_t IG_fwrite(const void *buffer, size_t size, size_t count, FILE *stream);
|
||||
ssize_t IG_read(int descriptor, void *buffer, size_t count);
|
||||
ssize_t IG_write(int descriptor, const void *buffer, size_t count);
|
||||
int IG_close(int descriptor);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,226 @@
|
||||
#include "IGArchiveGuard.h"
|
||||
#include "../resolve/IGResolve.h"
|
||||
#include "../anti_hook/IGAntiHook.h"
|
||||
#include "../primitives/IGInline.h"
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
#include <string.h>
|
||||
#include <mach/mach.h>
|
||||
#include <mach-o/dyld.h>
|
||||
#include <mach-o/loader.h>
|
||||
#include <mach-o/nlist.h>
|
||||
|
||||
/* 仅混淆常量,不作为秘密。SDK GOT 由 dyld 在独立镜像滑移后绑定。 */
|
||||
static const unsigned char names[4][6] = {
|
||||
{0x5a,0x53,0x4c,0x59,0x52}, {0x5a,0x4e,0x59,0x5d,0x58},
|
||||
{0x5a,0x4b,0x4e,0x55,0x48,0x59}, {0x5a,0x5f,0x50,0x53,0x4f,0x59}
|
||||
};
|
||||
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
||||
static _Thread_local int resolving;
|
||||
static _Atomic(uintptr_t) targets[4];
|
||||
static uintptr_t expected[4], bindingSlots[4];
|
||||
static unsigned char prefixes[4][16];
|
||||
static IGStatus initial[4];
|
||||
static int bindingReady;
|
||||
#ifdef IG_OBSERVATION
|
||||
static _Atomic(uint64_t) counters[12];
|
||||
#define COUNT(i, k) atomic_fetch_add_explicit(&counters[(i)*3+(k)], 1, memory_order_relaxed)
|
||||
#else
|
||||
#define COUNT(i, k) ((void)0)
|
||||
#endif
|
||||
|
||||
static int read_bytes(uintptr_t address, void *out, size_t size) {
|
||||
vm_size_t copied = 0;
|
||||
return vm_read_overwrite(mach_task_self(), address, size, (vm_address_t)out, &copied) == KERN_SUCCESS
|
||||
&& copied == size;
|
||||
}
|
||||
|
||||
/* 仅支持打包工具已审核的非认证 arm64 GOT;缺槽/重复槽均标为不可验证。 */
|
||||
static int locate_bindings(void) {
|
||||
unsigned found = 0, images = 0;
|
||||
for (uint32_t index = 0; index < _dyld_image_count(); index++) {
|
||||
const char *path = _dyld_get_image_name(index);
|
||||
const char *suffix = "/SSZipArchive.framework/SSZipArchive";
|
||||
size_t length = path ? ig_strlen(path) : 0, tail = ig_strlen(suffix);
|
||||
if (length < tail || ig_strcmp(path + length - tail, suffix)) continue;
|
||||
if (++images != 1) return 0;
|
||||
const struct mach_header_64 *header = (const void *)_dyld_get_image_header(index);
|
||||
if (!header || header->magic != MH_MAGIC_64 || header->cputype != CPU_TYPE_ARM64
|
||||
|| (header->cpusubtype & 0xffffff) != 0) return 0;
|
||||
uintptr_t slide = _dyld_get_image_vmaddr_slide(index);
|
||||
const struct segment_command_64 *linkedit = NULL;
|
||||
const struct symtab_command *symtab = NULL;
|
||||
const struct dysymtab_command *dysymtab = NULL;
|
||||
const uint8_t *command = (const void *)(header + 1);
|
||||
const uint8_t *end = command + header->sizeofcmds;
|
||||
for (uint32_t i = 0; i < header->ncmds; i++) {
|
||||
if (command + sizeof(struct load_command) > end) return 0;
|
||||
const struct load_command *lc = (const void *)command;
|
||||
if (lc->cmdsize < 8 || lc->cmdsize % 8 || lc->cmdsize > (size_t)(end-command)) return 0;
|
||||
if (lc->cmd == LC_SEGMENT_64) {
|
||||
if (lc->cmdsize < sizeof(struct segment_command_64)) return 0;
|
||||
const struct segment_command_64 *seg = (const void *)lc;
|
||||
if (sizeof(*seg) + (uint64_t)seg->nsects * sizeof(struct section_64) != lc->cmdsize) return 0;
|
||||
if (!ig_strcmp(seg->segname, SEG_LINKEDIT)) linkedit = seg;
|
||||
} else if (lc->cmd == LC_SYMTAB && lc->cmdsize == sizeof(*symtab)) symtab = (const void *)lc;
|
||||
else if (lc->cmd == LC_DYSYMTAB && lc->cmdsize == sizeof(*dysymtab)) dysymtab = (const void *)lc;
|
||||
command += lc->cmdsize;
|
||||
}
|
||||
if (!linkedit || !symtab || !dysymtab || command != end) return 0;
|
||||
uint64_t lo = linkedit->fileoff, hi = lo + linkedit->filesize;
|
||||
if (hi < lo || symtab->symoff < lo || (uint64_t)symtab->symoff + (uint64_t)symtab->nsyms*16 > hi
|
||||
|| symtab->stroff < lo || (uint64_t)symtab->stroff + symtab->strsize > hi
|
||||
|| dysymtab->indirectsymoff < lo
|
||||
|| (uint64_t)dysymtab->indirectsymoff + (uint64_t)dysymtab->nindirectsyms*4 > hi) return 0;
|
||||
uintptr_t base = slide + linkedit->vmaddr - lo;
|
||||
const struct nlist_64 *symbols = (const void *)(base + symtab->symoff);
|
||||
const char *strings = (const void *)(base + symtab->stroff);
|
||||
const uint32_t *indirect = (const void *)(base + dysymtab->indirectsymoff);
|
||||
command = (const void *)(header + 1);
|
||||
for (uint32_t i = 0; i < header->ncmds; i++) {
|
||||
const struct load_command *lc = (const void *)command;
|
||||
if (lc->cmd == LC_SEGMENT_64) {
|
||||
const struct segment_command_64 *seg = (const void *)lc;
|
||||
const struct section_64 *sections = (const void *)(seg + 1);
|
||||
for (uint32_t j = 0; j < seg->nsects; j++) {
|
||||
const struct section_64 *s = sections + j;
|
||||
if ((s->flags & SECTION_TYPE) != S_NON_LAZY_SYMBOL_POINTERS) continue;
|
||||
if (s->size % 8 || (uint64_t)s->reserved1 + s->size/8 > dysymtab->nindirectsyms) return 0;
|
||||
for (uint64_t k = 0; k < s->size/8; k++) {
|
||||
uint32_t symbol = indirect[s->reserved1 + k];
|
||||
if (symbol & (INDIRECT_SYMBOL_LOCAL | INDIRECT_SYMBOL_ABS)) continue;
|
||||
if (symbol >= symtab->nsyms) return 0;
|
||||
uint32_t strx = symbols[symbol].n_un.n_strx;
|
||||
if (strx >= symtab->strsize) return 0;
|
||||
const char *name = strings + strx;
|
||||
if (!memchr(name, 0, symtab->strsize-strx)) return 0;
|
||||
for (unsigned n = 0; n < 4; n++) {
|
||||
char expectedName[] = "_ig_a0";
|
||||
expectedName[5] += n;
|
||||
if (ig_strcmp(name, expectedName)) continue;
|
||||
if (found & (1u << n)) return 0;
|
||||
bindingSlots[n] = slide + s->addr + k*8;
|
||||
found |= 1u << n;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
command += lc->cmdsize;
|
||||
}
|
||||
}
|
||||
return found == 15 && images == 1;
|
||||
}
|
||||
|
||||
static void initialize(void) {
|
||||
bindingReady = locate_bindings();
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
IGResolveResult result = ig_resolve_verified(NULL, names[i], i < 2 ? 5 : 6, NULL, NULL);
|
||||
initial[i] = result.status;
|
||||
if (result.status != IG_CLEAR || !result.address) continue;
|
||||
if (!read_bytes((uintptr_t)result.address, prefixes[i], 16)) { initial[i] = IG_UNAVAILABLE; continue; }
|
||||
expected[i] = (uintptr_t)result.address;
|
||||
atomic_store_explicit(&targets[i], expected[i], memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
static int prepare(void) {
|
||||
if (resolving) return 0;
|
||||
resolving = 1;
|
||||
int result = pthread_once(&once, initialize);
|
||||
resolving = 0;
|
||||
return result == 0;
|
||||
}
|
||||
|
||||
static IGStatus validate(unsigned index, uintptr_t *address) {
|
||||
if (initial[index] != IG_CLEAR) return initial[index];
|
||||
uintptr_t current = atomic_load_explicit(&targets[index], memory_order_acquire);
|
||||
if (!current || current != expected[index]) return IG_DETECTED;
|
||||
IGStatus status = ig_verify_function((void *)current, "archive-target", NULL, NULL);
|
||||
if (status != IG_CLEAR) return status;
|
||||
unsigned char bytes[16];
|
||||
if (!read_bytes(current, bytes, 16)) return IG_UNAVAILABLE;
|
||||
if (ig_memcmp(bytes, prefixes[index], 16)) return IG_DETECTED;
|
||||
*address = current;
|
||||
return IG_CLEAR;
|
||||
}
|
||||
|
||||
static IGStatus check_bindings(void) {
|
||||
#ifdef IG_ARCHIVE_TESTING
|
||||
/* 宿主 ABI fixture 不装载 SSZipArchive;此豁免不能编入业务包。 */
|
||||
return IG_CLEAR;
|
||||
#else
|
||||
if (!bindingReady) return IG_UNAVAILABLE;
|
||||
uintptr_t correct[4] = {(uintptr_t)&ig_a0, (uintptr_t)&ig_a1, (uintptr_t)&ig_a2, (uintptr_t)&ig_a3};
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
uintptr_t current;
|
||||
if (!read_bytes(bindingSlots[i], ¤t, sizeof(current))) return IG_UNAVAILABLE;
|
||||
if (current != correct[i]) return IG_DETECTED;
|
||||
}
|
||||
return IG_CLEAR;
|
||||
#endif
|
||||
}
|
||||
|
||||
static uintptr_t checked(unsigned index) {
|
||||
COUNT(index, 0);
|
||||
if (!prepare()) { COUNT(index, 2); errno = EDEADLK; return 0; }
|
||||
uintptr_t address = 0;
|
||||
IGStatus status = check_bindings();
|
||||
if (status == IG_CLEAR) status = validate(index, &address);
|
||||
if (status != IG_CLEAR) { COUNT(index, 2); errno = status == IG_DETECTED ? EACCES : ENOSYS; return 0; }
|
||||
COUNT(index, 1);
|
||||
return address;
|
||||
}
|
||||
|
||||
__attribute__((noinline)) FILE *ig_a0(const char *path, const char *mode) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(0);
|
||||
if (!address) return NULL;
|
||||
errno = incoming;
|
||||
return ((FILE *(*)(const char *, const char *))address)(path, mode);
|
||||
}
|
||||
__attribute__((noinline)) size_t ig_a1(void *ptr, size_t size, size_t count, FILE *stream) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(1);
|
||||
if (!address) return 0;
|
||||
errno = incoming;
|
||||
return ((size_t (*)(void *, size_t, size_t, FILE *))address)(ptr, size, count, stream);
|
||||
}
|
||||
__attribute__((noinline)) size_t ig_a2(const void *ptr, size_t size, size_t count, FILE *stream) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(2);
|
||||
if (!address) return 0;
|
||||
errno = incoming;
|
||||
return ((size_t (*)(const void *, size_t, size_t, FILE *))address)(ptr, size, count, stream);
|
||||
}
|
||||
__attribute__((noinline)) int ig_a3(FILE *stream) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(3);
|
||||
if (!address) return EOF;
|
||||
errno = incoming;
|
||||
return ((int (*)(FILE *))address)(stream);
|
||||
}
|
||||
|
||||
int ig_archive_check(void) {
|
||||
int incoming = errno;
|
||||
if (!prepare()) { errno = incoming; return -1; }
|
||||
IGStatus status = check_bindings();
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
uintptr_t address;
|
||||
IGStatus current = validate(i, &address);
|
||||
if (current == IG_DETECTED || (current == IG_UNAVAILABLE && status == IG_CLEAR)) status = current;
|
||||
}
|
||||
errno = incoming;
|
||||
return status == IG_CLEAR ? 0 : status == IG_DETECTED ? 1 : -1;
|
||||
}
|
||||
#ifdef IG_OBSERVATION
|
||||
uint64_t ig_archive_counter(unsigned index) {
|
||||
return index < 12 ? atomic_load_explicit(&counters[index], memory_order_relaxed) : 0;
|
||||
}
|
||||
#endif
|
||||
#ifdef IG_ARCHIVE_TESTING
|
||||
void ig_archive_test_override(unsigned index, void *address) {
|
||||
if (index < 4 && prepare()) atomic_store_explicit(&targets[index], (uintptr_t)address, memory_order_release);
|
||||
}
|
||||
void ig_archive_test_reentry(int enabled) { resolving = enabled; }
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef IG_ARCHIVE_GUARD_H
|
||||
#define IG_ARCHIVE_GUARD_H
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#define IG_ARCHIVE_EXPORT __attribute__((visibility("default")))
|
||||
IG_ARCHIVE_EXPORT FILE *ig_a0(const char *path, const char *mode);
|
||||
IG_ARCHIVE_EXPORT size_t ig_a1(void *ptr, size_t size, size_t count, FILE *stream);
|
||||
IG_ARCHIVE_EXPORT size_t ig_a2(const void *ptr, size_t size, size_t count, FILE *stream);
|
||||
IG_ARCHIVE_EXPORT int ig_a3(FILE *stream);
|
||||
/* 0=正常,1=检测异常,-1=不可验证;保留 errno。 */
|
||||
IG_ARCHIVE_EXPORT int ig_archive_check(void);
|
||||
#ifdef IG_OBSERVATION
|
||||
/* index/3 为函数,index%3 为 attempted/dispatched/rejected。 */
|
||||
IG_ARCHIVE_EXPORT uint64_t ig_archive_counter(unsigned index);
|
||||
#endif
|
||||
#ifdef IG_ARCHIVE_TESTING
|
||||
void ig_archive_test_override(unsigned index, void *address);
|
||||
void ig_archive_test_reentry(int enabled);
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,149 @@
|
||||
#include "IGDispatch.h"
|
||||
#include "../resolve/IGResolve.h"
|
||||
#include "../anti_hook/IGAntiHook.h"
|
||||
#include "../primitives/IGInline.h"
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <mach/mach.h>
|
||||
|
||||
/* 编码只减少常量区明文,不作为秘密或强加密。缓存与基线仍可被同进程攻击者修改。 */
|
||||
static const unsigned char kNames[2][5] = {{0x4b,0x4e,0x55,0x48,0x59},
|
||||
{0x5f,0x50,0x53,0x4f,0x59}};
|
||||
static pthread_once_t gOnce=PTHREAD_ONCE_INIT;
|
||||
static _Atomic(uintptr_t) gSlots[2];
|
||||
static uintptr_t gExpected[2];
|
||||
static unsigned char gPrefixes[2][16];
|
||||
static IGStatus gInitialStatus[2]={IG_UNAVAILABLE,IG_UNAVAILABLE};
|
||||
static _Thread_local int gResolving;
|
||||
#ifdef IG_OBSERVATION
|
||||
static _Atomic(uint64_t) gAttempted[2], gDispatched[2];
|
||||
#define IG_COUNT(slot,index) atomic_fetch_add_explicit(&(slot)[index],1,memory_order_relaxed)
|
||||
#else
|
||||
#define IG_COUNT(slot,index) ((void)0)
|
||||
#endif
|
||||
|
||||
static int read_prefix(uintptr_t address, unsigned char out[16]) {
|
||||
vm_size_t copied=0;
|
||||
return vm_read_overwrite(mach_task_self(),address,16,(vm_address_t)out,&copied)==KERN_SUCCESS
|
||||
&& copied==16;
|
||||
}
|
||||
|
||||
static void initialize(void) {
|
||||
for (unsigned i=0;i<2;i++) {
|
||||
IGResolveResult r=ig_resolve_verified(NULL,kNames[i],5,NULL,NULL);
|
||||
gInitialStatus[i]=r.status;
|
||||
if (r.status!=IG_CLEAR || !r.address) continue;
|
||||
uintptr_t address=(uintptr_t)r.address;
|
||||
if (!read_prefix(address,gPrefixes[i])) { gInitialStatus[i]=IG_UNAVAILABLE; continue; }
|
||||
gExpected[i]=address;
|
||||
atomic_store_explicit(&gSlots[i],address,memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
static int prepare(void) {
|
||||
/* dlsym/装载初始化过程中若重入受保护入口,拒绝而不是等待自身 once 锁。 */
|
||||
if (gResolving) return 0;
|
||||
gResolving=1;
|
||||
int rc=pthread_once(&gOnce,initialize);
|
||||
gResolving=0;
|
||||
return rc==0;
|
||||
}
|
||||
|
||||
static IGStatus validate(unsigned i, uintptr_t *address, int *reason) {
|
||||
if (gInitialStatus[i]!=IG_CLEAR) {
|
||||
*reason=IG_REASON_DISPATCH_UNAVAILABLE;
|
||||
return gInitialStatus[i];
|
||||
}
|
||||
uintptr_t current=atomic_load_explicit(&gSlots[i],memory_order_acquire);
|
||||
if (!current || current!=gExpected[i]) {
|
||||
*reason=IG_REASON_DISPATCH_TAMPER;
|
||||
return IG_DETECTED;
|
||||
}
|
||||
IGStatus status=ig_verify_function((void *)current,"dispatch-target",NULL,NULL);
|
||||
if (status!=IG_CLEAR) {
|
||||
*reason=status==IG_DETECTED ? IG_REASON_INLINE_HOOK : IG_REASON_VERIFICATION_UNAVAILABLE;
|
||||
return status;
|
||||
}
|
||||
unsigned char bytes[16];
|
||||
if (!read_prefix(current,bytes)) {
|
||||
*reason=IG_REASON_VERIFICATION_UNAVAILABLE;
|
||||
return IG_UNAVAILABLE;
|
||||
}
|
||||
if (ig_memcmp(bytes,gPrefixes[i],16)!=0) {
|
||||
*reason=IG_REASON_DISPATCH_TAMPER;
|
||||
return IG_DETECTED;
|
||||
}
|
||||
*address=current;
|
||||
return IG_CLEAR;
|
||||
}
|
||||
|
||||
static uintptr_t checked_address(unsigned i) {
|
||||
if (!prepare()) { errno=EDEADLK; return 0; }
|
||||
uintptr_t address=0; int reason=0;
|
||||
IGStatus status=validate(i,&address,&reason);
|
||||
if (status!=IG_CLEAR) { errno=status==IG_DETECTED ? EACCES : ENOSYS; return 0; }
|
||||
return address;
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
ssize_t IG_dispatch_write(int fd, const void *buffer, size_t count) {
|
||||
int incoming=errno;
|
||||
IG_COUNT(gAttempted,0);
|
||||
uintptr_t address=checked_address(0);
|
||||
if (!address) return -1;
|
||||
IG_COUNT(gDispatched,0);
|
||||
errno=incoming;
|
||||
ssize_t (*function)(int,const void *,size_t)=(void *)address;
|
||||
return function(fd,buffer,count);
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
int IG_dispatch_close(int fd) {
|
||||
int incoming=errno;
|
||||
IG_COUNT(gAttempted,1);
|
||||
uintptr_t address=checked_address(1);
|
||||
if (!address) return -1;
|
||||
IG_COUNT(gDispatched,1);
|
||||
errno=incoming;
|
||||
int (*function)(int)=(void *)address;
|
||||
return function(fd);
|
||||
}
|
||||
|
||||
int ig_dispatch_check(IGFindingSink sink, void *ctx) {
|
||||
int saved=errno, hits=0;
|
||||
int ready=prepare();
|
||||
for (unsigned i=0;i<2;i++) {
|
||||
uintptr_t address=0; int reason=IG_REASON_DISPATCH_UNAVAILABLE;
|
||||
IGStatus status=ready ? validate(i,&address,&reason) : IG_UNAVAILABLE;
|
||||
if (status==IG_CLEAR) continue;
|
||||
IGFinding f={0}; f.status=status; f.reason=reason;
|
||||
const char *label=i==0 ? "dispatch-write" : "dispatch-close";
|
||||
strncpy(f.subject,label,sizeof(f.subject)-1);
|
||||
strncpy(f.evidence,"调度地址、提供方或代码前缀校验未通过",sizeof(f.evidence)-1);
|
||||
if (status==IG_DETECTED) hits++;
|
||||
if (sink && sink(&f,ctx)!=0) break;
|
||||
}
|
||||
errno=saved;
|
||||
return hits;
|
||||
}
|
||||
|
||||
#ifdef IG_OBSERVATION
|
||||
IGDispatchObservation ig_dispatch_observation(void) {
|
||||
IGDispatchObservation value={
|
||||
atomic_load_explicit(&gAttempted[0],memory_order_relaxed),
|
||||
atomic_load_explicit(&gDispatched[0],memory_order_relaxed),
|
||||
atomic_load_explicit(&gAttempted[1],memory_order_relaxed),
|
||||
atomic_load_explicit(&gDispatched[1],memory_order_relaxed)
|
||||
};
|
||||
return value;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef IG_DISPATCH_TESTING
|
||||
void ig_dispatch_test_override(unsigned i, void *address) {
|
||||
if (i<2 && prepare()) atomic_store_explicit(&gSlots[i],(uintptr_t)address,memory_order_release);
|
||||
}
|
||||
void ig_dispatch_test_reentry(int enabled) { gResolving=enabled; }
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef IG_DISPATCH_H
|
||||
#define IG_DISPATCH_H
|
||||
#include <sys/types.h>
|
||||
#include "../include/IGDefender.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 固定签名试点:首次按线程安全方式解析;异常时不调用未经校验的地址。 */
|
||||
ssize_t IG_dispatch_write(int fd, const void *buffer, size_t count);
|
||||
int IG_dispatch_close(int fd);
|
||||
int ig_dispatch_check(IGFindingSink sink, void *ctx);
|
||||
|
||||
#ifdef IG_OBSERVATION
|
||||
/* 仅观察构建导出只读计数,不提供地址覆盖或关闭校验的接口。 */
|
||||
typedef struct {
|
||||
uint64_t write_attempted, write_dispatched;
|
||||
uint64_t close_attempted, close_dispatched;
|
||||
} IGDispatchObservation;
|
||||
IGDispatchObservation ig_dispatch_observation(void);
|
||||
#endif
|
||||
|
||||
#ifdef IG_DISPATCH_TESTING
|
||||
/* 只编入宿主测试,不允许在 App 发布包定义此宏。 */
|
||||
void ig_dispatch_test_override(unsigned index, void *address);
|
||||
void ig_dispatch_test_reentry(int enabled);
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* IGPoison.c — 毒化状态机实现。见 IGPoison.h 的设计与边界声明。
|
||||
*
|
||||
* 实现要点:
|
||||
* - 三副本冗余、写法各异、均 volatile(防编译器把 (copy ^ BASELINE) 常量折叠回 0)。
|
||||
* - 掩码由三副本与基线异或后按位或得出:健康时每副本==BASELINE → 全 0;毒化时非 0。
|
||||
* - 没有任何 `if (poisoned)` 控制流;处置是纯算术数据流。
|
||||
*
|
||||
* 后续硬化(本骨架未做,留 TODO):
|
||||
* - 把三副本分散到不同存储域(TLS / 堆上结构 / 编码进业务数据的冗余位),而非都在 BSS。
|
||||
* - mark / mask / apply 三个函数用 OLLVM(Fla+BCF) + 字符串/常量加密盖住(README 规划)。
|
||||
* - 基线魔数不以明文入二进制:改为运行期由多来源派生,避免被静态定位后直接写回。
|
||||
*/
|
||||
#include "IGPoison.h"
|
||||
|
||||
/* 基线魔数:健康态三副本都等于它。TODO:改为运行期派生,勿明文驻留常量区。 */
|
||||
#define IG_POISON_BASELINE 0xA5C3F10E7B2D4968ULL
|
||||
|
||||
/* 三副本,volatile 防折叠。写法刻意不一致以抬高静态模式匹配成本。 */
|
||||
static volatile uint64_t g_h0;
|
||||
static volatile uint64_t g_h1;
|
||||
static volatile uint64_t g_h2;
|
||||
|
||||
/* 把 entropy 扩散成三个互不相同、且必非 0 的偏移量(保证任一副本都会偏离基线)。 */
|
||||
static uint64_t ig_spread(uint64_t e, unsigned lane) {
|
||||
e ^= 0x9E3779B97F4A7C15ULL * (lane + 1u); /* 每路不同常量 */
|
||||
e ^= e >> 30; e *= 0xBF58476D1CE4E5B9ULL;
|
||||
e ^= e >> 27; e *= 0x94D049BB133111EBULL;
|
||||
e ^= e >> 31;
|
||||
return e | 1ULL; /* 置最低位,确保非 0 */
|
||||
}
|
||||
|
||||
void ig_poison_init(void) {
|
||||
g_h0 = IG_POISON_BASELINE;
|
||||
g_h1 = IG_POISON_BASELINE;
|
||||
g_h2 = IG_POISON_BASELINE;
|
||||
}
|
||||
|
||||
/* 构造期兜底初始化(若宿主未显式调用 ig_poison_init)。 */
|
||||
__attribute__((constructor))
|
||||
static void ig_poison_ctor(void) { ig_poison_init(); }
|
||||
|
||||
void ig_poison_mark(uint64_t entropy) {
|
||||
/* entropy 为 0 时仍强制非 0,避免“混入 0 = 不生效”。 */
|
||||
uint64_t e = entropy ? entropy : 0xD1B54A32D192ED03ULL;
|
||||
g_h0 ^= ig_spread(e, 0);
|
||||
g_h1 ^= ig_spread(e, 1);
|
||||
g_h2 ^= ig_spread(e, 2);
|
||||
}
|
||||
|
||||
uint64_t ig_poison_mask(void) {
|
||||
/* 健康:每项 ^BASELINE == 0 → 或起来为 0。毒化:至少一项非 0。无分支。 */
|
||||
uint64_t m = (g_h0 ^ IG_POISON_BASELINE)
|
||||
| (g_h1 ^ IG_POISON_BASELINE)
|
||||
| (g_h2 ^ IG_POISON_BASELINE);
|
||||
return m;
|
||||
}
|
||||
|
||||
void ig_poison_apply(const uint8_t *in, uint8_t *out, size_t n) {
|
||||
if (!in || !out) return;
|
||||
uint64_t m = ig_poison_mask();
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
/* 健康 m==0 → 扰动字节恒 0 → out[i]==in[i]。毒化 → 每字节被 m 的对应字节异或。 */
|
||||
uint8_t d = (uint8_t)((m >> ((i & 7u) * 8u)) & 0xFFu);
|
||||
out[i] = (uint8_t)(in[i] ^ d);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* IGPoison.h — 毒化状态机(数据依赖处置,纯 C,可编入业务 target)。
|
||||
*
|
||||
* 设计核心(对应纯客户端反 Hook 的处置哲学):
|
||||
* 不写 `if (越狱) exit()`。检测到强信号组合时,只把一段熵混进一个多副本冗余的
|
||||
* “健康值”,把它破坏掉;之后由业务关键数据的**密钥派生**消费这个健康值——
|
||||
* 健康 → 掩码为 0 → 密钥不变 → 解密正常;毒化 → 掩码非 0 → 密钥被扰动 → 解出乱码。
|
||||
* 全程**没有可被 patch 的控制流分支**,攻击者 nop 不掉“判断”,因为判断不存在,
|
||||
* 只有数据流污染。要绕过得逆向整条密钥推导 + 搞清健康值怎么混入,成本量级抬高。
|
||||
*
|
||||
* 天花板(如实声明):纯客户端无法根治。多副本 + 混淆 + VMP 只是把“找到并写回健康值”
|
||||
* 从几分钟抬到几天;有耐心有能力的定向攻击者最终仍可破。安全锚点不在这。
|
||||
*
|
||||
* 处置形态(App Store 上架约束):只做数据污染 / 功能静默降级,不触发随机崩溃。
|
||||
*
|
||||
* 误报底线(极重要):处置是隐蔽且体验不可逆的,误伤一个非越狱正常用户=差评卸载且难排查。
|
||||
* 因此 ig_poison_mark 只允许由**高置信强信号组合**触发(见 IGPoison.c 注释),
|
||||
* 弱信号(FOREIGN_IMAGE / FRIDA_PORT 等)绝不参与。上线前必须在多机型验证非越狱零毒化。
|
||||
*/
|
||||
#ifndef IG_POISON_H
|
||||
#define IG_POISON_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 把多副本健康值置为基线。应在尽早的构造期调用一次(也有构造函数兜底)。 */
|
||||
void ig_poison_init(void);
|
||||
|
||||
/*
|
||||
* 命中强信号组合时调用:把 entropy(必须非 0)混进所有健康值副本,使其偏离基线。
|
||||
* 幂等累积——多次调用只会持续偏离,不会“洗白”。此函数只写状态,不产生任何可见后果。
|
||||
*/
|
||||
void ig_poison_mark(uint64_t entropy);
|
||||
|
||||
/*
|
||||
* 返回处置掩码:健康=0,毒化=非 0(由多副本交叉推导,无分支)。
|
||||
* 业务侧不直接判它,而是把它揉进密钥/偏移等数据,见 ig_poison_apply。
|
||||
*/
|
||||
uint64_t ig_poison_mask(void);
|
||||
|
||||
/*
|
||||
* 数据依赖处置原语:把 in[0..n) 揉以当前掩码写入 out[0..n)(允许 in==out 原地)。
|
||||
* 健康时 out 与 in 逐字节相等(掩码 0,异或 0);毒化时每字节被掩码扰动。
|
||||
* 业务用法:对解密 key 的字节调用本函数后再传给解密器——健康照常、毒化解出乱码。
|
||||
*/
|
||||
void ig_poison_apply(const uint8_t *in, uint8_t *out, size_t n);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* IG_POISON_H */
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* IGPolicy.c — 策略层实现。见 .h 的分级与误报底线。
|
||||
*/
|
||||
#include "IGPolicy.h"
|
||||
#include "poison/IGPoison.h"
|
||||
|
||||
typedef enum { IG_TIER_IGNORE = 0, IG_TIER_WEAK, IG_TIER_MEDIUM, IG_TIER_STRONG } IGTier;
|
||||
|
||||
/* reason → 档位。未列出的 DETECTED 按 WEAK 处理(保守,不触发毒化)。 */
|
||||
static IGTier ig_reason_tier(int reason) {
|
||||
switch (reason) {
|
||||
/* STRONG:单条即可判定(误报概率极低) */
|
||||
case IG_REASON_JAILBREAK_SANDBOX: /* 401 沙盒逃逸 */
|
||||
case IG_REASON_JAILBREAK_FILE: /* 400 越狱特征文件 */
|
||||
case IG_REASON_GOT_REBIND: /* 102 */
|
||||
case IG_REASON_INLINE_HOOK: /* 101 */
|
||||
case IG_REASON_INJECTED_IMAGE: /* 200 */
|
||||
case IG_REASON_DEBUGGER_ATTACHED: /* 300 */
|
||||
return IG_TIER_STRONG;
|
||||
/* MEDIUM:需组合(含合法 swizzling 会误报的 ObjC IMP) */
|
||||
case IG_REASON_SYMBOL_PROVENANCE: /* 100 */
|
||||
case IG_REASON_OBJC_IMP_CHANGED: /* 110 */
|
||||
case IG_REASON_FRIDA_THREAD: /* 203 */
|
||||
return IG_TIER_MEDIUM;
|
||||
/* WEAK:仅观察,永不触发毒化 */
|
||||
case IG_REASON_FOREIGN_IMAGE: /* 201 */
|
||||
case IG_REASON_FRIDA_PORT: /* 204 */
|
||||
return IG_TIER_WEAK;
|
||||
default:
|
||||
return IG_TIER_WEAK;
|
||||
}
|
||||
}
|
||||
|
||||
void ig_policy_reset(IGPolicyState *st, IGFindingSink observer, void *observer_ctx) {
|
||||
if (!st) return;
|
||||
st->strong_count = 0;
|
||||
st->medium_bitmap = 0;
|
||||
st->medium_distinct = 0;
|
||||
st->entropy = 0;
|
||||
st->detected_total = 0;
|
||||
st->observer = observer;
|
||||
st->observer_ctx = observer_ctx;
|
||||
}
|
||||
|
||||
/* 把 reason 映射到 medium_bitmap 的一个位(三个 MEDIUM reason 分别占位)。 */
|
||||
static uint32_t ig_medium_bit(int reason) {
|
||||
switch (reason) {
|
||||
case IG_REASON_SYMBOL_PROVENANCE: return 1u << 0;
|
||||
case IG_REASON_OBJC_IMP_CHANGED: return 1u << 1;
|
||||
case IG_REASON_FRIDA_THREAD: return 1u << 2;
|
||||
default: return 0u;
|
||||
}
|
||||
}
|
||||
|
||||
int ig_policy_sink(const IGFinding *f, void *ctx) {
|
||||
IGPolicyState *st = (IGPolicyState *)ctx;
|
||||
if (!st || !f) return 0;
|
||||
|
||||
/* 先转发观察打点(无论档位),便于量化误报面。 */
|
||||
if (st->observer) st->observer(f, st->observer_ctx);
|
||||
|
||||
/* 误报底线:只有 DETECTED 参与判定;UNAVAILABLE/CLEAR 忽略。 */
|
||||
if (f->status != IG_DETECTED) return 0;
|
||||
st->detected_total++;
|
||||
|
||||
IGTier tier = ig_reason_tier(f->reason);
|
||||
if (tier == IG_TIER_STRONG) {
|
||||
st->strong_count++;
|
||||
st->entropy ^= 0x100000001b3ULL * (uint64_t)(f->reason + 1);
|
||||
} else if (tier == IG_TIER_MEDIUM) {
|
||||
uint32_t bit = ig_medium_bit(f->reason);
|
||||
if (bit && !(st->medium_bitmap & bit)) {
|
||||
st->medium_bitmap |= bit;
|
||||
st->medium_distinct++;
|
||||
}
|
||||
st->entropy ^= 0x100000001b3ULL * (uint64_t)(f->reason + 1);
|
||||
}
|
||||
/* WEAK/IGNORE:不计入判定,也不进熵。 */
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ig_policy_evaluate_and_mark(IGPolicyState *st) {
|
||||
if (!st) return 0;
|
||||
int trigger = (st->strong_count >= 1) || (st->medium_distinct >= 2);
|
||||
if (trigger) {
|
||||
uint64_t e = st->entropy ? st->entropy : 0xA24BAED4963EE407ULL;
|
||||
ig_poison_mark(e);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ig_policy_scan_and_mark(const char *bundle_path, int probe_port,
|
||||
IGFindingSink observer, void *observer_ctx) {
|
||||
IGPolicyState st;
|
||||
ig_policy_reset(&st, observer, observer_ctx);
|
||||
ig_defender_scan(bundle_path, probe_port, ig_policy_sink, &st);
|
||||
return ig_policy_evaluate_and_mark(&st);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* IGPolicy.h — 策略层:把探针证据按强弱分级,组合判定后决定是否毒化(ig_poison_mark)。
|
||||
*
|
||||
* 连接 [[iosguard-disposition-strategy]]:ig_defender_scan 流式上报 IGFinding,本层做 sink,
|
||||
* 累积一次扫描的全部 DETECTED 证据;扫描结束后 evaluate 按组合规则决定是否触发毒化。
|
||||
*
|
||||
* 误报底线(最重要):毒化隐蔽且不可逆,误伤非越狱正常用户=差评卸载难排查。故:
|
||||
* - 只有 DETECTED 参与;UNAVAILABLE(架构/平台不支持)与 CLEAR 一律忽略,绝不当攻击。
|
||||
* - 信号分三档:
|
||||
* STRONG(单条即判定):越狱文件/沙盒逃逸、GOT 重绑、inline hook、注入镜像、调试器跟踪。
|
||||
* MEDIUM(需 ≥2 个不同 MEDIUM,或与任一 STRONG 同现才判定):符号提供方、ObjC IMP 变化、
|
||||
* frida 线程——其中 ObjC IMP 合法 swizzling 会误报,故不单条定罪。
|
||||
* WEAK(永不触发毒化,仅观察):Bundle 外镜像、frida 端口等弱证据。
|
||||
*
|
||||
* 本层不自行 exit;唯一处置是数据依赖毒化。可选把 finding 转发给观察打点(observer)。
|
||||
*/
|
||||
#ifndef IG_POLICY_H
|
||||
#define IG_POLICY_H
|
||||
|
||||
#include "IGDefender.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
int strong_count; /* STRONG 档命中条数 */
|
||||
uint32_t medium_bitmap; /* MEDIUM 档已命中的不同 reason(去重用) */
|
||||
int medium_distinct; /* MEDIUM 档不同 reason 数 */
|
||||
uint64_t entropy; /* 命中 reason 聚合出的毒化熵(非 0) */
|
||||
int detected_total; /* 累计 DETECTED 条数(观察用) */
|
||||
IGFindingSink observer; /* 可选:每条 finding 透传给它做打点;NULL 跳过 */
|
||||
void *observer_ctx; /* observer 的上下文 */
|
||||
} IGPolicyState;
|
||||
|
||||
/* 重置状态;observer/observer_ctx 可传 NULL。 */
|
||||
void ig_policy_reset(IGPolicyState *st, IGFindingSink observer, void *observer_ctx);
|
||||
|
||||
/* 作为 ig_defender_scan 的 sink:ctx 必须是 IGPolicyState*。只累积、不处置。 */
|
||||
int ig_policy_sink(const IGFinding *finding, void *ctx);
|
||||
|
||||
/*
|
||||
* 扫描结束后调用:按组合规则判定。满足即 ig_poison_mark(entropy) 并返回 1;否则返回 0。
|
||||
* 规则:strong_count>=1 || medium_distinct>=2。
|
||||
*/
|
||||
int ig_policy_evaluate_and_mark(IGPolicyState *st);
|
||||
|
||||
/*
|
||||
* 便捷封装:reset → ig_defender_scan → evaluate_and_mark 一次完成。
|
||||
* 返回 1 表示本次触发了毒化。observer/observer_ctx 可 NULL。
|
||||
*/
|
||||
int ig_policy_scan_and_mark(const char *bundle_path, int probe_port,
|
||||
IGFindingSink observer, void *observer_ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* IG_POLICY_H */
|
||||
@@ -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 */
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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 */
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 */
|
||||
Reference in New Issue
Block a user