poison_device.m 加 D9a–d 四条环境无关不变量 + csops_status/501 两条观察, 复用 build/run_poison_device harness。实测(verdict PASS,不变量 9/9): - 误报底线:expect_*=0 → 零 DETECTED,对应 finding UNAVAILABLE - cryptid 真读主镜像:expect=1 → reason500=1(dev 包 cryptid==0),mh=0x104390000 - 整入口贯通:ig_anti_tamper_check(1,1) → 500 + 501 - csops 真机活性:rc=0 status=0x22003305 valid=1 adhoc=0 get_task_allow=1 (坐实 csops 在 iOS arm64 活着,排除 501 假阴性) 残留缺口(只能在真 App Store/TestFlight 包验):cryptid==1 正向 CLEAR + 分发签名零误触——dev profile 无法复现 FairPlay 加密/分发签名。docs/18 §7.7、docs/19 §6。 证据 test-results/step12-device-poison/20261113-antitamper-R/(gitignore)。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
290 lines
14 KiB
Objective-C
290 lines
14 KiB
Objective-C
/*
|
||
* poison_device.m — 处置链真机验证诊断进程(step12,见 docs/19)。
|
||
*
|
||
* 在真机自身进程内运行真实的「检测→分级→数据毒化」链,分两类用例:
|
||
*
|
||
* A. 环境无关不变量(两组设备都必须 PASS,构成套件门槛):
|
||
* D4 poison_identity 健康态 apply 恒等(out==key)——证不误伤解密
|
||
* D5 poison_mark_corrupt mark 后 apply 坏 key(out!=key)——证毒化真的生效
|
||
* D6 ctor_clean_start 进程启动未 mark 时 mask==0——证构造期初始化干净、无启动即毒化
|
||
* D7 policy_grades 合成 finding 复核分级(弱/单MEDIUM不触发;双MEDIUM/单STRONG触发)
|
||
* D8 svc_access_live 在自身沙盒建真实可访问文件,证 svc 直发 access(2) 在真机能命中
|
||
* reason=400——坐实 D2 的 0 命中是「无越狱文件」而非「探针静默失败」,
|
||
* 无需越狱设备即可验证检测机制在 iOS arm64 上是活的。
|
||
*
|
||
* B. 环境相关观察(不写死 PASS/FAIL,输出命中供人按 R/J 组判定,见 docs/19 §4):
|
||
* D1 baseline_scan ig_policy_scan_and_mark(bundle) 的返回/mask
|
||
* D2 jb_file 内置越狱特征表 access 直发命中数(reason=400)
|
||
* D3 jb_sandbox 默认沙盒逃逸探测(reason=401)
|
||
* 并据 D2/D3 是否命中输出环境判定 IG_POISON_ENV jailbroken=yes|no。
|
||
*
|
||
* 边界:攻击/构造代码只存在于本诊断源码,绝不编入业务库;越狱特征文件的“存在性”
|
||
* 在 J 组由设备本身提供,诊断进程只做被动 access/open 探测,不自己造越狱文件。
|
||
* 每例结束前调 ig_poison_init() 复位,避免跨例污染。
|
||
*/
|
||
#import <Foundation/Foundation.h>
|
||
#include "IGDefender.h"
|
||
#include "poison/IGPoison.h"
|
||
#include "policy/IGPolicy.h"
|
||
#include "anti_jailbreak/IGAntiJailbreak.h"
|
||
#include "anti_tamper/IGAntiTamper.h"
|
||
|
||
#include <string.h>
|
||
#include <unistd.h>
|
||
#include <mach-o/dyld.h>
|
||
#include <mach-o/loader.h>
|
||
|
||
/* 原始 csops(与 IGAntiTamper 同,供 .m 直读状态字作真机观察,坐实 csops 活性) */
|
||
#ifndef CS_OPS_STATUS
|
||
#define CS_OPS_STATUS 0
|
||
#endif
|
||
#define IG_CS_VALID 0x00000001u
|
||
#define IG_CS_ADHOC 0x00000002u
|
||
#define IG_CS_GET_TASK_ALLOW 0x00000004u
|
||
extern int csops(pid_t pid, unsigned int ops, void *useraddr, size_t usersize);
|
||
|
||
/* 进程主可执行镜像头(与 IGAntiTamper 内部同逻辑,便于直接喂给可测核心) */
|
||
static const struct mach_header_64 *dev_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;
|
||
}
|
||
|
||
/* ---------- 发现收集 ---------- */
|
||
#define IG_MAXF 64
|
||
static IGFinding g_f[IG_MAXF];
|
||
static int g_nf;
|
||
static int g_detected;
|
||
|
||
static int collect(const IGFinding *f, void *ctx) {
|
||
(void)ctx;
|
||
if (f->status == IG_DETECTED) {
|
||
if (g_nf < IG_MAXF) g_f[g_nf++] = *f;
|
||
g_detected++;
|
||
}
|
||
NSLog(@"IG_POISON_FINDING status=%d reason=%d subject=%s evidence=%s",
|
||
f->status, f->reason, f->subject, f->evidence);
|
||
return 0;
|
||
}
|
||
static void reset_findings(void) { g_nf = 0; g_detected = 0; }
|
||
static int count_reason(int reason) {
|
||
int n = 0;
|
||
for (int i = 0; i < g_nf; ++i) if (g_f[i].reason == reason) n++;
|
||
return n;
|
||
}
|
||
|
||
/* ---------- PASS/FAIL 汇总 ---------- */
|
||
static int g_pass = 0, g_fail = 0;
|
||
static void mark_case(const char *name, int pass, const char *detail) {
|
||
if (pass) g_pass++; else g_fail++;
|
||
NSLog(@"IG_POISON_CASE %s %s detail=%s", name, pass ? "PASS" : "FAIL", detail ? detail : "");
|
||
}
|
||
static void observe(const char *name, const char *detail) {
|
||
NSLog(@"IG_POISON_OBS %s %s", name, detail ? detail : "");
|
||
}
|
||
|
||
/* ---------- policy 合成输入 ---------- */
|
||
static void feed(IGPolicyState *st, IGStatus status, int reason) {
|
||
IGFinding f; memset(&f, 0, sizeof(f));
|
||
f.status = status; f.reason = reason;
|
||
ig_policy_sink(&f, st);
|
||
}
|
||
static int run_policy_weak(void) {
|
||
ig_poison_init();
|
||
IGPolicyState st; ig_policy_reset(&st, NULL, NULL);
|
||
feed(&st, IG_DETECTED, IG_REASON_FOREIGN_IMAGE);
|
||
feed(&st, IG_DETECTED, IG_REASON_FRIDA_PORT);
|
||
return ig_policy_evaluate_and_mark(&st);
|
||
}
|
||
static int run_policy_single_medium(void) {
|
||
ig_poison_init();
|
||
IGPolicyState st; ig_policy_reset(&st, NULL, NULL);
|
||
feed(&st, IG_DETECTED, IG_REASON_OBJC_IMP_CHANGED);
|
||
return ig_policy_evaluate_and_mark(&st);
|
||
}
|
||
static int run_policy_two_medium(void) {
|
||
ig_poison_init();
|
||
IGPolicyState st; ig_policy_reset(&st, NULL, NULL);
|
||
feed(&st, IG_DETECTED, IG_REASON_OBJC_IMP_CHANGED);
|
||
feed(&st, IG_DETECTED, IG_REASON_SYMBOL_PROVENANCE);
|
||
return ig_policy_evaluate_and_mark(&st);
|
||
}
|
||
static int run_policy_single_strong(void) {
|
||
ig_poison_init();
|
||
IGPolicyState st; ig_policy_reset(&st, NULL, NULL);
|
||
feed(&st, IG_DETECTED, IG_REASON_JAILBREAK_SANDBOX);
|
||
return ig_policy_evaluate_and_mark(&st);
|
||
}
|
||
|
||
int main(int argc, char **argv) {
|
||
(void)argc; (void)argv;
|
||
@autoreleasepool {
|
||
const char *bundle = [[[NSBundle mainBundle] bundlePath] fileSystemRepresentation];
|
||
|
||
/* ===== D6 ctor_clean_start:进程启动早期、任何 mark 之前,mask 必须为 0 =====
|
||
* 放最前,尽量贴近启动态(构造器已跑,但本进程尚未触发任何检测/mark)。 */
|
||
{
|
||
uint64_t m = ig_poison_mask();
|
||
char d[64]; snprintf(d, sizeof(d), "mask=0x%llx", (unsigned long long)m);
|
||
mark_case("ctor_clean_start", m == 0, d);
|
||
}
|
||
|
||
/* ===== D4 poison_identity:健康态 apply 恒等 ===== */
|
||
{
|
||
ig_poison_init();
|
||
const uint8_t key[] = { 's','t','a','r','t','i','a','s','o','f','t',0x11,0x22 };
|
||
size_t n = sizeof(key); uint8_t out[sizeof(key)];
|
||
memset(out, 0xAB, n);
|
||
ig_poison_apply(key, out, n);
|
||
mark_case("poison_identity", memcmp(out, key, n) == 0, "healthy apply must be identity");
|
||
}
|
||
|
||
/* ===== D5 poison_mark_corrupt:mark 后 apply 坏 key ===== */
|
||
{
|
||
ig_poison_init();
|
||
const uint8_t key[] = { 's','t','a','r','t','i','a','s','o','f','t',0x11,0x22 };
|
||
size_t n = sizeof(key); uint8_t out[sizeof(key)];
|
||
ig_poison_mark(0xC0FFEE);
|
||
int nonzero = (ig_poison_mask() != 0);
|
||
ig_poison_apply(key, out, n);
|
||
int corrupted = (memcmp(out, key, n) != 0);
|
||
mark_case("poison_mark_corrupt", nonzero && corrupted, "marked apply must differ");
|
||
ig_poison_init();
|
||
}
|
||
|
||
/* ===== D7 policy_grades:真机复核分级 ===== */
|
||
{
|
||
int weak = run_policy_weak(); /* 期望 0 */
|
||
int med1 = run_policy_single_medium(); /* 期望 0 */
|
||
int med2 = run_policy_two_medium(); /* 期望 1 */
|
||
int strong = run_policy_single_strong(); /* 期望 1 */
|
||
int ok = (weak == 0 && med1 == 0 && med2 == 1 && strong == 1);
|
||
char d[96];
|
||
snprintf(d, sizeof(d), "weak=%d single_medium=%d two_medium=%d single_strong=%d",
|
||
weak, med1, med2, strong);
|
||
mark_case("policy_grades", ok, d);
|
||
ig_poison_init();
|
||
}
|
||
|
||
/* ===== D8 svc_access_live:合成正向,不依赖越狱 =====
|
||
* 非越狱设备上内置越狱表必然 0 命中——但那可能是「svc 直发在 iOS 上静默失败、
|
||
* 永远返回 0」的假阴性。此例在自身沙盒 Documents 建一个真实可访问文件,喂给
|
||
* ig_anti_jb_check_paths,确认 svc #0x80 access(2) 在真机 iOS arm64 上能真正命中
|
||
* (reason=400)。坐实 D2 的 0 命中是「无越狱文件」而非「探针坏了」。环境无关不变量。 */
|
||
{
|
||
reset_findings();
|
||
NSString *docs = NSSearchPathForDirectoriesInDomains(
|
||
NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
|
||
NSString *probe = [docs stringByAppendingPathComponent:@".ig_svc_live_probe"];
|
||
[@"x" writeToFile:probe atomically:YES encoding:NSUTF8StringEncoding error:nil];
|
||
const char *cpath = [probe fileSystemRepresentation];
|
||
const char *paths[] = { cpath };
|
||
ig_anti_jb_check_paths(paths, 1, NULL, collect, NULL);
|
||
int live = count_reason(IG_REASON_JAILBREAK_FILE);
|
||
[[NSFileManager defaultManager] removeItemAtPath:probe error:nil];
|
||
char d[96]; snprintf(d, sizeof(d), "accessible_file_hits=%d svc_access_fires_on_device", live);
|
||
mark_case("svc_access_live", live >= 1, d);
|
||
reset_findings();
|
||
}
|
||
|
||
/* ===== D9 anti_tamper:二进制完整性探针真机验证 =====
|
||
* dev 签名包是环境无关不变量:本地编译=无 FairPlay 加密(无 LC_ENCRYPTION_INFO
|
||
* 或 cryptid=0)、且带 get-task-allow。据此双向验证 IGAntiTamper:
|
||
* expect=0(dev 正确配置)→ 全 UNAVAILABLE、零 DETECTED(误报底线);
|
||
* expect=1(假装分发)→ cryptid 命中 500、csops 读到 get-task-allow 命中 501
|
||
* (坐实探针真读到真实镜像头/签名状态,逻辑正确)。 */
|
||
const struct mach_header_64 *mh = dev_main_image_header();
|
||
|
||
/* D9a cryptid_expect0_clean:非分发构建跳过,零 DETECTED(误报底线) */
|
||
{
|
||
reset_findings();
|
||
int hits = ig_tamper_scan_cryptid(mh, /*expect_encrypted=*/0, collect, NULL);
|
||
mark_case("at_cryptid_expect0_clean", hits == 0 && g_detected == 0,
|
||
"dev build, expect_encrypted=0 must not flag");
|
||
reset_findings();
|
||
}
|
||
|
||
/* D9b cryptid_expect1_detects:dev 包无 FairPlay 加密 → 命中 500(探针真读主镜像头) */
|
||
{
|
||
reset_findings();
|
||
int hits = ig_tamper_scan_cryptid(mh, /*expect_encrypted=*/1, collect, NULL);
|
||
int r500 = count_reason(IG_REASON_BINARY_DECRYPTED);
|
||
char d[96]; snprintf(d, sizeof(d), "hits=%d reason500=%d mh=%p", hits, r500, (void *)mh);
|
||
mark_case("at_cryptid_expect1_detects", hits >= 1 && r500 >= 1, d);
|
||
reset_findings();
|
||
}
|
||
|
||
/* D9c tamper_gated_off:两检查都 expect=0 → 零 DETECTED(误报底线整入口) */
|
||
{
|
||
reset_findings();
|
||
int hits = ig_anti_tamper_check(/*expect_encrypted=*/0, /*expect_distribution=*/0,
|
||
collect, NULL);
|
||
mark_case("at_tamper_gated_off", hits == 0 && g_detected == 0,
|
||
"dev build, expect_*=0 whole-entry must not flag");
|
||
reset_findings();
|
||
}
|
||
|
||
/* D9d tamper_on_detects:expect_*=1 → 至少 cryptid 500 命中(整入口贯通真机) */
|
||
{
|
||
reset_findings();
|
||
int hits = ig_anti_tamper_check(/*expect_encrypted=*/1, /*expect_distribution=*/1,
|
||
collect, NULL);
|
||
int r500 = count_reason(IG_REASON_BINARY_DECRYPTED);
|
||
int r501 = count_reason(IG_REASON_CODESIGN_INVALID);
|
||
char d[112]; snprintf(d, sizeof(d), "hits=%d reason500=%d reason501=%d", hits, r500, r501);
|
||
mark_case("at_tamper_on_detects", hits >= 1 && r500 >= 1, d);
|
||
/* 501(csops 代码签名)作观察:dev 包带 get-task-allow 应命中,但 csops 语义真机坐实为准 */
|
||
char o[64]; snprintf(o, sizeof(o), "reason501_codesign_hits=%d", r501);
|
||
observe("at_codesign_501", o);
|
||
reset_findings();
|
||
}
|
||
|
||
/* D9e csops_status(观察):直读 csops 状态字,坐实 csops 在真机 iOS arm64 活着 */
|
||
{
|
||
uint32_t st = 0;
|
||
int rc = csops(getpid(), CS_OPS_STATUS, &st, sizeof(st));
|
||
char o[128];
|
||
snprintf(o, sizeof(o), "rc=%d status=0x%x valid=%d adhoc=%d get_task_allow=%d",
|
||
rc, st, (st & IG_CS_VALID) != 0, (st & IG_CS_ADHOC) != 0,
|
||
(st & IG_CS_GET_TASK_ALLOW) != 0);
|
||
observe("csops_status", o);
|
||
}
|
||
|
||
/* ===== D2 jb_file(观察):内置越狱特征表直发 access ===== */
|
||
reset_findings();
|
||
ig_anti_jb_check(collect, NULL);
|
||
int hit400 = count_reason(IG_REASON_JAILBREAK_FILE);
|
||
int hit401_default = count_reason(IG_REASON_JAILBREAK_SANDBOX);
|
||
{
|
||
char d[96]; snprintf(d, sizeof(d), "reason400=%d reason401=%d", hit400, hit401_default);
|
||
observe("jb_file", d);
|
||
}
|
||
|
||
/* ===== D1 baseline_scan(观察):聚合策略扫描的返回与 mask ===== */
|
||
{
|
||
ig_poison_init();
|
||
int triggered = ig_policy_scan_and_mark(bundle, 0, NULL, NULL);
|
||
uint64_t m = ig_poison_mask();
|
||
char d[96]; snprintf(d, sizeof(d), "triggered=%d mask=0x%llx",
|
||
triggered, (unsigned long long)m);
|
||
observe("baseline_scan", d);
|
||
ig_poison_init();
|
||
}
|
||
|
||
/* ===== 环境判定:D2/D3 任一命中即视作越狱环境(J 组)===== */
|
||
int jailbroken = (hit400 > 0) || (hit401_default > 0);
|
||
NSLog(@"IG_POISON_ENV jailbroken=%s reason400=%d reason401=%d",
|
||
jailbroken ? "yes" : "no", hit400, hit401_default);
|
||
|
||
/* 套件判定只看环境无关不变量(D4–D7)。 */
|
||
NSLog(@"IG_POISON_SUITE %s pass=%d fail=%d", g_fail ? "FAIL" : "PASS", g_pass, g_fail);
|
||
}
|
||
return 0;
|
||
}
|