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
Vendored
+111
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* anti_tamper_host.c — IGAntiTamper cryptid 核心的宿主自测。
|
||||
*
|
||||
* 用合成 Mach-O 头(header + 可选 LC_ENCRYPTION_INFO_64)驱动 ig_tamper_scan_cryptid,
|
||||
* 覆盖四种情形:加密正常(cryptid=1)、脱壳(cryptid=0)、加密命令被剥除、非分发构建跳过。
|
||||
* 退出码 0 = 全部用例通过。不依赖真机(csops/真实镜像那部分由设备测试覆盖)。
|
||||
*/
|
||||
#include "anti_tamper/IGAntiTamper.h"
|
||||
|
||||
#include <mach-o/loader.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
int detected; /* DETECTED 条数 */
|
||||
int unavailable; /* UNAVAILABLE 条数 */
|
||||
int last_reason; /* 最后一条 DETECTED 的 reason */
|
||||
} Counter;
|
||||
|
||||
static int sink(const IGFinding *f, void *ctx) {
|
||||
Counter *c = (Counter *)ctx;
|
||||
if (f->status == IG_DETECTED) { c->detected++; c->last_reason = f->reason; }
|
||||
if (f->status == IG_UNAVAILABLE) { c->unavailable++; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 构造一个带 ncmds 的最小 mach_header_64;commands 由调用方填好拼在后面。 */
|
||||
typedef struct {
|
||||
struct mach_header_64 mh;
|
||||
union {
|
||||
struct encryption_info_command_64 enc;
|
||||
unsigned char raw[256];
|
||||
} cmds;
|
||||
} FakeImage;
|
||||
|
||||
static void init_header(FakeImage *img, uint32_t ncmds, uint32_t sizeofcmds) {
|
||||
memset(img, 0, sizeof(*img));
|
||||
img->mh.magic = MH_MAGIC_64;
|
||||
img->mh.cputype = CPU_TYPE_ARM64;
|
||||
img->mh.filetype = MH_EXECUTE;
|
||||
img->mh.ncmds = ncmds;
|
||||
img->mh.sizeofcmds = sizeofcmds;
|
||||
}
|
||||
|
||||
static int failures = 0;
|
||||
|
||||
static void check(const char *name, int cond) {
|
||||
if (!cond) { printf("FAIL: %s\n", name); failures++; }
|
||||
else { printf("ok: %s\n", name); }
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
/* 1) cryptid=1:正常加密分发包 → CLEAR,不上报 DETECTED。 */
|
||||
{
|
||||
FakeImage img;
|
||||
init_header(&img, 1, sizeof(struct encryption_info_command_64));
|
||||
img.cmds.enc.cmd = LC_ENCRYPTION_INFO_64;
|
||||
img.cmds.enc.cmdsize = sizeof(struct encryption_info_command_64);
|
||||
img.cmds.enc.cryptid = 1;
|
||||
Counter c = {0, 0, 0};
|
||||
int hits = ig_tamper_scan_cryptid(&img.mh, /*expect_encrypted=*/1, sink, &c);
|
||||
check("cryptid=1 → 0 detected", hits == 0 && c.detected == 0);
|
||||
}
|
||||
|
||||
/* 2) cryptid=0:已脱壳 dump → DETECTED reason=500。 */
|
||||
{
|
||||
FakeImage img;
|
||||
init_header(&img, 1, sizeof(struct encryption_info_command_64));
|
||||
img.cmds.enc.cmd = LC_ENCRYPTION_INFO_64;
|
||||
img.cmds.enc.cmdsize = sizeof(struct encryption_info_command_64);
|
||||
img.cmds.enc.cryptid = 0;
|
||||
Counter c = {0, 0, 0};
|
||||
int hits = ig_tamper_scan_cryptid(&img.mh, 1, sink, &c);
|
||||
check("cryptid=0 → detected 500",
|
||||
hits == 1 && c.detected == 1 && c.last_reason == IG_REASON_BINARY_DECRYPTED);
|
||||
}
|
||||
|
||||
/* 3) 无 LC_ENCRYPTION_INFO(加密命令被剥除)→ DETECTED reason=500。 */
|
||||
{
|
||||
FakeImage img;
|
||||
init_header(&img, 0, 0); /* 零条 load command */
|
||||
Counter c = {0, 0, 0};
|
||||
int hits = ig_tamper_scan_cryptid(&img.mh, 1, sink, &c);
|
||||
check("no-enc-cmd → detected 500",
|
||||
hits == 1 && c.detected == 1 && c.last_reason == IG_REASON_BINARY_DECRYPTED);
|
||||
}
|
||||
|
||||
/* 4) expect_encrypted=0:dev/诊断构建跳过 → UNAVAILABLE,无 DETECTED。 */
|
||||
{
|
||||
FakeImage img;
|
||||
init_header(&img, 1, sizeof(struct encryption_info_command_64));
|
||||
img.cmds.enc.cmd = LC_ENCRYPTION_INFO_64;
|
||||
img.cmds.enc.cmdsize = sizeof(struct encryption_info_command_64);
|
||||
img.cmds.enc.cryptid = 0; /* 即便 0,非分发构建也不判篡改 */
|
||||
Counter c = {0, 0, 0};
|
||||
int hits = ig_tamper_scan_cryptid(&img.mh, /*expect_encrypted=*/0, sink, &c);
|
||||
check("expect_encrypted=0 → unavailable, 0 detected",
|
||||
hits == 0 && c.detected == 0 && c.unavailable == 1);
|
||||
}
|
||||
|
||||
/* 5) NULL 头 + expect=1 → UNAVAILABLE(不崩、不误报)。 */
|
||||
{
|
||||
Counter c = {0, 0, 0};
|
||||
int hits = ig_tamper_scan_cryptid(NULL, 1, sink, &c);
|
||||
check("null header → unavailable, 0 detected",
|
||||
hits == 0 && c.detected == 0 && c.unavailable == 1);
|
||||
}
|
||||
|
||||
printf("%s (%d failures)\n", failures ? "FAILED" : "PASSED", failures);
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user