/* * IGAntiInject.c — 代码注入与 Frida 运行时检测实现。 * * 全部公开 API:(镜像枚举)、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 "../dispatch/IGDispatch.h" #include #if IG_DIAGNOSTIC_STRINGS # include #endif #include #include #include #include #include #include /* _NSGetEnviron:手工遍历环境表,替代可被 hook 的 getenv */ #include #include #include #include #include #include #include /* * 经 _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; } /* "DYLD_INSERT_LIBRARIES" ^ 0x5A(避免明文驻留 __cstring)。 */ static const unsigned char kIGDyldEnvEnc[21] = { 0x1eu, 0x03u, 0x16u, 0x1eu, 0x05u, 0x13u, 0x14u, 0x09u, 0x1fu, 0x08u, 0x0eu, 0x05u, 0x16u, 0x13u, 0x18u, 0x08u, 0x1bu, 0x08u, 0x13u, 0x1fu, 0x09u }; 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 IG_DIAGNOSTIC_STRINGS if (subject) strncpy(f.subject, subject, sizeof(f.subject)-1); if (evidence) strncpy(f.evidence, evidence, sizeof(f.evidence)-1); #else (void)subject; (void)evidence; #endif 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])) { #if IG_DIAGNOSTIC_STRINGS 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); #else ig_emit2(sink, ctx, &stop, &hits, IG_DETECTED, IG_REASON_INJECTED_IMAGE, NULL, NULL); #endif 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, IG_DIAG_STR("Bundle 外非系统镜像(弱信号,需结合其它证据)")); } } /* 3. DYLD_INSERT_LIBRARIES 逐条解析(运行期解码环境变量名)。 */ char env_name[24]; { volatile unsigned char emask = 0x5Au; for (size_t ei = 0; ei < sizeof(kIGDyldEnvEnc); ++ei) { env_name[ei] = (char)(kIGDyldEnvEnc[ei] ^ emask); } env_name[sizeof(kIGDyldEnvEnc)] = '\0'; } const char *dyld = ig_local_getenv(env_name); __builtin_memset(env_name, 0, sizeof(env_name)); 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)) { #if IG_DIAGNOSTIC_STRINGS 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); #else ig_emit2(sink, ctx, &stop, &hits, IG_DETECTED, IG_REASON_DYLD_INSERT, NULL, NULL); #endif } } } __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, IG_DIAG_STR("task_threads"), IG_DIAG_STR("无法枚举线程")); 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) { #if IG_DIAGNOSTIC_STRINGS 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); #else ig_emit2(sink, ctx, stop, hits, IG_DETECTED, IG_REASON_FRIDA_THREAD, NULL, NULL); #endif 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); } } IG_dispatch_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)) { #if IG_DIAGNOSTIC_STRINGS 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); #else ig_emit2(sink, ctx, &stop, &hits, IG_DETECTED, IG_REASON_FRIDA_PORT, NULL, NULL); #endif } } return hits; }