针对「纯客户端抗静态分析」的最高性价比项落地,守在 tests/test_static_hardening.py(6 条,gate 于 iphoneos SDK): - 符号隐藏 + strip(§7.0 新增):IGVisibility.h 定 IG_INTERNAL=hidden,全库 -fvisibility=hidden;仅四入口 ig_defender_scan/ig_poison_init/ig_poison_apply/ ig_policy_scan_and_mark 标 IG_PUBLIC 保持导出,其余 ig_* 变 private external, 链接后 strip -x 彻底消失——攻击者 nm 不再能按名定位 ig_poison_mark/mask 去 NOP。 - §7.1 串混淆:gen_jb_paths.py(唯一真源)生成 IGJailbreakPaths.inc / IGInjectSignatures.inc(XOR keystream,两表不共享 key),运行期解码进栈、用完擦除; decode 内 volatile key 屏障阻止 -O2 把常量折叠回 __const 明文。strings 已捞不到 /var/jb、Cydia、substrate、gum-js-loop 等。--check 守 .inc 与清单同步。 - §7.2 副本分散:g_h0/h1/h2 置于不相邻命名段 __DATA,__ig_p0/p1/p2,加 __ig_pd 诱饵 (mask 不读);刻意不用 TLS(会破坏跨线程毒化一致性)。 - §7.4 基线派生:删硬编码 0xA5C3F10E7B2D4968,改 ig_poison_baseline() 由拆分种子经 volatile 屏障派生;otool -tV 已无旧魔数立即数。 - §7.3 OLLVM/VMP:仅留 build 的 IG_OBFUSCATOR hook,stock clang 无能力,需另备工具链(未完成)。 - §7.5 越狱设备正向验证仍阻塞(无设备)。 构建/快照同步:build_defender_lib.sh 加 -fvisibility=hidden + gen --check + IG_OBFUSCATOR hook;build_poison_device.py 快照纳入 *.inc。全量 69 测试通过;poison_device.m 全模块 iphoneos 交叉编译通过(arm64)。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
245 lines
10 KiB
C
245 lines
10 KiB
C
/*
|
||
* 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;
|
||
}
|
||
|
||
/*
|
||
* §7.1 IG_ENC:注入特征串与 frida 线程名不再明文驻留(避免 `strings` 捞出 frida/substrate/
|
||
* gum-js-loop 等,白送检测目标)。混淆表由 tools/gen_jb_paths.py 生成,运行期解码进栈。
|
||
*/
|
||
#include "IGInjectSignatures.inc"
|
||
|
||
/* 解码单条混淆串到 out(容量 cap,含结尾 0)。返回长度,失败返回 0。
|
||
* k0 取 volatile:阻止 -O2 把「常量 enc + 常量 key」整条折叠回明文存入 __const
|
||
* (否则 strings 又能捞出 frida/gum-js-loop)。强制运行期解码。 */
|
||
static size_t ig_inj_decode(const unsigned char *enc, size_t n, int idx,
|
||
char *out, size_t cap) {
|
||
volatile unsigned k0 = IG_INJ_XOR_K0;
|
||
if (n + 1 > cap) return 0;
|
||
for (size_t j = 0; j < n; ++j) {
|
||
unsigned ks = ((unsigned)k0 + (unsigned)idx * IG_INJ_XOR_K1 + j * IG_INJ_XOR_K2) & 0xFFu;
|
||
out[j] = (char)(enc[j] ^ ks);
|
||
}
|
||
out[n] = '\0';
|
||
return n;
|
||
}
|
||
|
||
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;
|
||
|
||
/* §7.1:特征串运行期解码进栈,循环内用解码副本;函数退出前擦除。 */
|
||
char injsig[IG_INJ_ENC_COUNT][24];
|
||
for (int s = 0; s < IG_INJ_ENC_COUNT; ++s)
|
||
ig_inj_decode(kIGInjEnc[s].b, kIGInjEnc[s].n, s, injsig[s], sizeof(injsig[s]));
|
||
|
||
/* 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 (int s = 0; s < IG_INJ_ENC_COUNT; ++s) {
|
||
if (ig_strstr(low, injsig[s])) {
|
||
char ev[256];
|
||
snprintf(ev, sizeof(ev), "镜像=%s 命中特征=%s", name, injsig[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);
|
||
}
|
||
}
|
||
}
|
||
__builtin_memset(injsig, 0, sizeof(injsig));
|
||
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;
|
||
}
|
||
/* §7.1:frida 线程名运行期解码进栈,比对后擦除。 */
|
||
char thr[IG_THR_ENC_COUNT][24];
|
||
for (int t = 0; t < IG_THR_ENC_COUNT; ++t)
|
||
ig_inj_decode(kIGThrEnc[t].b, kIGThrEnc[t].n, t, thr[t], sizeof(thr[t]));
|
||
|
||
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 (int t = 0; t < IG_THR_ENC_COUNT; ++t) {
|
||
if (ig_strcmp(info.pth_name, thr[t]) == 0) {
|
||
char ev[256];
|
||
snprintf(ev, sizeof(ev), "线程名=%s 命中注入运行时特征", info.pth_name);
|
||
ig_emit2(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_FRIDA_THREAD,
|
||
info.pth_name, ev);
|
||
break;
|
||
}
|
||
}
|
||
if (*stop) break;
|
||
}
|
||
__builtin_memset(thr, 0, sizeof(thr));
|
||
/* 回收端口。 */
|
||
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;
|
||
}
|