feat(anti_tamper): IGAntiTamper——二进制完整性/防重打包探针(cryptid+csops)
补 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>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
2e43ff6da2
commit
cb51fdc6f0
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* IGAntiTamper.h — 二进制完整性 / 防重打包探针。
|
||||
*
|
||||
* 定位:anti_hook 只校验固定一批关键函数的序言/GOT——攻击者 patch 这批之外的任意指令(含
|
||||
* NOP 掉某条检测/毒化调用边、改业务层比较)它看不见。本模块从「镜像整体」角度补这个空洞,
|
||||
* 正面反制 App Store 应用的标准破解链:**脱壳解密(Clutch/frida-ios-dump)→ patch → 重签**。
|
||||
*
|
||||
* 两条检查(均只采集证据、不处置,经 sink 上报;判级交 policy):
|
||||
* 1. FairPlay cryptid:App Store 分发包由 Apple 加密(LC_ENCRYPTION_INFO_64.cryptid=1)。
|
||||
* 脱壳 dump 会把 cryptid 改 0;重打包可能整条剥除加密命令。→ IG_REASON_BINARY_DECRYPTED。
|
||||
* 2. 代码签名状态(csops CS_OPS_STATUS):ad-hoc 重签、分发包却带 get-task-allow(被重签成
|
||||
* 可调试)、CS_VALID 失效。→ IG_REASON_CODESIGN_INVALID。
|
||||
*
|
||||
* 【误报底线】这两条都**依赖构建上下文**:dev/TestFlight/诊断构建本就未 FairPlay 加密、且带
|
||||
* get-task-allow——那是正常的,不是篡改。故由调用方按构建类型传 expect_* 开关:只有
|
||||
* 「本应是 App Store 分发包」时才判 DETECTED;否则返回 UNAVAILABLE(无法断言,不上报 DETECTED)。
|
||||
* expect 开关应由 Release/App Store 构建配置注入(见 docs/18),绝不在 dev 构建置 1。
|
||||
*
|
||||
* 【边界】csops 经 libc 调用,可被 hook 伪造(若攻击者已能 hook csops,说明环境已失陷,
|
||||
* 届时 anti_hook/anti_inject 会先命中);cryptid 读的是进程内 Mach-O 头,patch 头部也能伪装,
|
||||
* 但那要求攻击者已定位该检查——与整体「抬成本、非不可破」哲学一致,不作为唯一安全锚点。
|
||||
*/
|
||||
#ifndef IG_ANTI_TAMPER_H
|
||||
#define IG_ANTI_TAMPER_H
|
||||
|
||||
#include <mach-o/loader.h>
|
||||
|
||||
#include "IGDefender.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 真机入口:对当前进程主可执行镜像做 cryptid + 代码签名检查。
|
||||
* expect_encrypted:本构建是否「本应 FairPlay 加密」(App Store 分发=1,dev/诊断=0)。
|
||||
* expect_distribution:本构建是否「本应分发签名」(不含 get-task-allow;App Store=1,dev=0)。
|
||||
* 两者为 0 时对应检查返回 UNAVAILABLE(不误报)。返回 DETECTED 证据条数。
|
||||
*/
|
||||
int ig_anti_tamper_check(int expect_encrypted, int expect_distribution,
|
||||
IGFindingSink sink, void *ctx);
|
||||
|
||||
/*
|
||||
* 可测核心:对给定 Mach-O 头做 cryptid 检查(与真机入口共用逻辑,便于用合成头单测)。
|
||||
* mh:指向 mach_header_64。expect_encrypted:同上。
|
||||
* 返回 DETECTED 条数(0=CLEAR 或 UNAVAILABLE)。
|
||||
*/
|
||||
int ig_tamper_scan_cryptid(const struct mach_header_64 *mh, int expect_encrypted,
|
||||
IGFindingSink sink, void *ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* IG_ANTI_TAMPER_H */
|
||||
@@ -49,6 +49,10 @@ typedef enum {
|
||||
/* 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 沙盒外创建文件:沙盒逃逸=越狱强信号 */
|
||||
/* anti_tamper(5xx)——二进制完整性/防重打包。正面反制「脱壳解密+patch+重签」破解链,
|
||||
* 补 anti_hook 只盯固定函数之外的空洞。按构建上下文开关,dev/诊断构建不参与(UNAVAILABLE)。 */
|
||||
IG_REASON_BINARY_DECRYPTED = 500, /* 应加密的 App Store 包 cryptid==0 或 LC_ENCRYPTION_INFO 被剥除:脱壳/重打包强信号 */
|
||||
IG_REASON_CODESIGN_INVALID = 501, /* 代码签名异常:ad-hoc 重签 / 分发包却带 get-task-allow / CS_VALID 失效 */
|
||||
} IGReason;
|
||||
|
||||
typedef struct {
|
||||
|
||||
Reference in New Issue
Block a user