/* * 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 #include #include 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; }