- defender: 扩展 IGDispatch/IGArchiveGuard/IGAntiHook/IGObjCBaseline/IGResolve,支持 Route 2 静态库桥接 (IGThreadID.c) 与防重签名检测 - llvm_passes: 新增 IGObfuscator LLVM IR 插件源码(控制流平坦化、字符串加密、间接跳转/调用、不透明常量/字段、MBA、函数外提、基本块拆分、反 Hook 序言) - tools & config: 新增 Xcode 归档编译/链接包装器、LLVM 自愈工具链 (ig_toolchain_resolver)、IPA 审计门禁、崩溃符号还原及最新 ig_llvm_policy.json(文件级匹配 + 3.0 全业务 Handler 平坦化) - tests & docs: 补齐 M0~M3、Pass 单测及系统调用调度/Route 2/LLVM 方案文档;.gitignore 忽略 *.bc 与 .obfuscate_extraction_cache.json
137 lines
6.0 KiB
C
137 lines
6.0 KiB
C
/*
|
|
* IGAntiTamper.c — 二进制完整性 / 防重打包探针实现。见 .h 的定位与误报底线。
|
|
*
|
|
* 只采集证据经 sink 上报,不处置。两条检查均按 expect_* 构建上下文开关,dev/诊断构建传 0
|
|
* → 返回 UNAVAILABLE,绝不在非分发构建上报 DETECTED。
|
|
*/
|
|
#include "IGAntiTamper.h"
|
|
|
|
#include <mach-o/dyld.h>
|
|
#include <unistd.h>
|
|
#if IG_DIAGNOSTIC_STRINGS
|
|
# include <stdio.h>
|
|
#endif
|
|
|
|
/* csops(2):读本进程代码签名状态。手工声明(<sys/codesign.h> 非 SDK 公共头)。 */
|
|
#define IG_CS_OPS_STATUS 0
|
|
#define IG_CS_VALID 0x00000001u /* 动态有效(代码页签名校验通过) */
|
|
#define IG_CS_ADHOC 0x00000002u /* ad-hoc 签名(无开发者证书) */
|
|
#define IG_CS_GET_TASK_ALLOW 0x00000004u /* 允许被 task_for_pid/调试器附着 */
|
|
extern int csops(pid_t pid, unsigned int ops, void *useraddr, size_t usersize);
|
|
|
|
static void ig_tamper_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 IG_DIAGNOSTIC_STRINGS
|
|
if (subject) snprintf(f.subject, sizeof(f.subject), "%s", subject);
|
|
if (evidence) snprintf(f.evidence, sizeof(f.evidence), "%s", evidence);
|
|
#else
|
|
(void)subject;
|
|
(void)evidence;
|
|
#endif
|
|
if (st == IG_DETECTED && hits) (*hits)++;
|
|
if (sink) sink(&f, ctx);
|
|
}
|
|
|
|
int ig_tamper_scan_cryptid(const struct mach_header_64 *mh, int expect_encrypted,
|
|
IGFindingSink sink, void *ctx) {
|
|
int hits = 0;
|
|
|
|
/* 非分发构建:未 FairPlay 加密是正常的,无法据此断言篡改。 */
|
|
if (!expect_encrypted) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_UNAVAILABLE, IG_REASON_BINARY_DECRYPTED,
|
|
IG_DIAG_STR("cryptid"), IG_DIAG_STR("非分发构建(expect_encrypted=0),跳过加密校验"));
|
|
return hits;
|
|
}
|
|
if (!mh || mh->magic != MH_MAGIC_64) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_UNAVAILABLE, IG_REASON_BINARY_DECRYPTED,
|
|
IG_DIAG_STR("cryptid"), IG_DIAG_STR("主镜像 Mach-O 头不可用"));
|
|
return hits;
|
|
}
|
|
|
|
/* 遍历 load command 找 LC_ENCRYPTION_INFO_64。 */
|
|
const struct load_command *lc = (const struct load_command *)(mh + 1);
|
|
int found = 0;
|
|
uint32_t cryptid = 0;
|
|
for (uint32_t i = 0; i < mh->ncmds; ++i) {
|
|
if (lc->cmdsize == 0) break; /* 防御畸形头导致的死循环 */
|
|
if (lc->cmd == LC_ENCRYPTION_INFO_64) {
|
|
const struct encryption_info_command_64 *e =
|
|
(const struct encryption_info_command_64 *)lc;
|
|
found = 1;
|
|
cryptid = e->cryptid;
|
|
break;
|
|
}
|
|
lc = (const struct load_command *)((const uint8_t *)lc + lc->cmdsize);
|
|
}
|
|
|
|
if (!found) {
|
|
/* 应加密的分发包却整条没有加密命令:被剥除/重打包的强信号。 */
|
|
ig_tamper_emit(sink, ctx, &hits, IG_DETECTED, IG_REASON_BINARY_DECRYPTED,
|
|
IG_DIAG_STR("cryptid"), IG_DIAG_STR("应加密分发包无 LC_ENCRYPTION_INFO(加密命令被剥除/重打包)"));
|
|
} else if (cryptid == 0) {
|
|
/* 标准脱壳(Clutch/frida-ios-dump)会把 cryptid 改 0。 */
|
|
ig_tamper_emit(sink, ctx, &hits, IG_DETECTED, IG_REASON_BINARY_DECRYPTED,
|
|
IG_DIAG_STR("cryptid"), IG_DIAG_STR("cryptid==0:二进制已脱壳解密(dump/重打包)"));
|
|
}
|
|
/* cryptid != 0 → CLEAR,保持安静不上报。 */
|
|
return hits;
|
|
}
|
|
|
|
static int ig_tamper_scan_codesign(int expect_distribution,
|
|
IGFindingSink sink, void *ctx) {
|
|
int hits = 0;
|
|
|
|
/* 非分发构建:dev/诊断签名本就带 get-task-allow、可能 ad-hoc,不据此断言。 */
|
|
if (!expect_distribution) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_UNAVAILABLE, IG_REASON_CODESIGN_INVALID,
|
|
IG_DIAG_STR("codesign"), IG_DIAG_STR("非分发构建(expect_distribution=0),跳过签名校验"));
|
|
return hits;
|
|
}
|
|
|
|
uint32_t status = 0;
|
|
/* pid=0 在 XNU 内核直接指向当前进程,避免引入对 _getpid 的显式调用。 */
|
|
if (csops(0, IG_CS_OPS_STATUS, &status, sizeof(status)) != 0) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_UNAVAILABLE, IG_REASON_CODESIGN_INVALID,
|
|
IG_DIAG_STR("codesign"), IG_DIAG_STR("csops(CS_OPS_STATUS) 调用失败"));
|
|
return hits;
|
|
}
|
|
|
|
if (!(status & IG_CS_VALID)) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_DETECTED, IG_REASON_CODESIGN_INVALID,
|
|
IG_DIAG_STR("codesign"), IG_DIAG_STR("CS_VALID 失效:代码页签名校验未通过(内存/磁盘被 patch)"));
|
|
}
|
|
if (status & IG_CS_ADHOC) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_DETECTED, IG_REASON_CODESIGN_INVALID,
|
|
IG_DIAG_STR("codesign"), IG_DIAG_STR("ad-hoc 签名:无开发者证书的重签(典型破解重打包)"));
|
|
}
|
|
if (status & IG_CS_GET_TASK_ALLOW) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_DETECTED, IG_REASON_CODESIGN_INVALID,
|
|
IG_DIAG_STR("codesign"), IG_DIAG_STR("分发包却带 get-task-allow:被重签成可调试"));
|
|
}
|
|
return hits;
|
|
}
|
|
|
|
static const struct mach_header_64 *ig_main_image_header(void) {
|
|
uint32_t n = _dyld_image_count();
|
|
for (uint32_t i = 0; i < n; ++i) {
|
|
const struct mach_header *h = _dyld_get_image_header(i);
|
|
if (h && h->magic == MH_MAGIC_64 &&
|
|
((const struct mach_header_64 *)h)->filetype == MH_EXECUTE) {
|
|
return (const struct mach_header_64 *)h;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int ig_anti_tamper_check(int expect_encrypted, int expect_distribution,
|
|
IGFindingSink sink, void *ctx) {
|
|
int hits = 0;
|
|
hits += ig_tamper_scan_cryptid(ig_main_image_header(), expect_encrypted, sink, ctx);
|
|
hits += ig_tamper_scan_codesign(expect_distribution, sink, ctx);
|
|
return hits;
|
|
}
|