补 anti_hook 只盯固定函数之外的空洞,正面反制 App Store 标准破解链 (脱壳解密→patch→重签): - FairPlay cryptid(reason=500):cryptid==0 或 LC_ENCRYPTION_INFO 被剥除=脱壳/重打包 - 代码签名 csops CS_OPS_STATUS(reason=501):CS_VALID 失效/ad-hoc 重签/分发包带 get-task-allow 误报底线:两条均按 expect_encrypted/expect_distribution 构建上下文开关, dev/诊断/TestFlight 传 0→UNAVAILABLE,绝不在非分发构建误报。作为独立探针 由 Swift 策略层/observer 带构建上下文单独调用,不并入 ig_defender_scan(不破坏现有签名)。 回归:tests/test_anti_tamper.py 合成 Mach-O 头跑 cryptid 核心五例(加密正常/脱壳/ 剥除/非分发跳过/NULL);模块接入 poison_host 与 device 链接列表。全量 73 过, iphoneos arm64 交叉编译通过。csops+真实主镜像验证待设备。docs/18 §7.7。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
129 lines
5.5 KiB
C
129 lines
5.5 KiB
C
/*
|
|
* IGAntiTamper.c — 二进制完整性 / 防重打包探针实现。见 .h 的定位与误报底线。
|
|
*
|
|
* 只采集证据经 sink 上报,不处置。两条检查均按 expect_* 构建上下文开关,dev/诊断构建传 0
|
|
* → 返回 UNAVAILABLE,绝不在非分发构建上报 DETECTED。
|
|
*/
|
|
#include "IGAntiTamper.h"
|
|
|
|
#include <mach-o/dyld.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
|
|
/* 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 (subject) snprintf(f.subject, sizeof(f.subject), "%s", subject);
|
|
if (evidence) snprintf(f.evidence, sizeof(f.evidence), "%s", evidence);
|
|
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,
|
|
"cryptid", "非分发构建(expect_encrypted=0),跳过加密校验");
|
|
return hits;
|
|
}
|
|
if (!mh || mh->magic != MH_MAGIC_64) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_UNAVAILABLE, IG_REASON_BINARY_DECRYPTED,
|
|
"cryptid", "主镜像 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,
|
|
"cryptid", "应加密分发包无 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,
|
|
"cryptid", "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,
|
|
"codesign", "非分发构建(expect_distribution=0),跳过签名校验");
|
|
return hits;
|
|
}
|
|
|
|
uint32_t status = 0;
|
|
if (csops(getpid(), IG_CS_OPS_STATUS, &status, sizeof(status)) != 0) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_UNAVAILABLE, IG_REASON_CODESIGN_INVALID,
|
|
"codesign", "csops(CS_OPS_STATUS) 调用失败");
|
|
return hits;
|
|
}
|
|
|
|
if (!(status & IG_CS_VALID)) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_DETECTED, IG_REASON_CODESIGN_INVALID,
|
|
"codesign", "CS_VALID 失效:代码页签名校验未通过(内存/磁盘被 patch)");
|
|
}
|
|
if (status & IG_CS_ADHOC) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_DETECTED, IG_REASON_CODESIGN_INVALID,
|
|
"codesign", "ad-hoc 签名:无开发者证书的重签(典型破解重打包)");
|
|
}
|
|
if (status & IG_CS_GET_TASK_ALLOW) {
|
|
ig_tamper_emit(sink, ctx, &hits, IG_DETECTED, IG_REASON_CODESIGN_INVALID,
|
|
"codesign", "分发包却带 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;
|
|
}
|