test(defender): 处置链真机验证诊断进程 + 构建/执行器(step12)

补齐 docs/19 的真机 harness,交叉编译通过(iphoneos arm64),待设备执行。

- tests/fixtures/poison_device.m:真机诊断进程。D4–D7 环境无关不变量
  (apply恒等/mark坏key/构造期mask=0/策略分级)输出 IG_POISON_CASE 作套件门槛;
  D1–D3 环境相关(聚合扫描/越狱文件400/沙盒逃逸401)输出 OBS + IG_POISON_ENV
  环境判定,供 R/J 组对照判定。只被动探测,不自造越狱文件,每例 ig_poison_init 复位。
- tools/macho_guard/build_poison_device.py:照 hook 版,模块含 poison/policy/
  anti_jailbreak,专用 Bundle 前缀 poisonpilot、通配开发 profile 校验、源码快照哈希。
- tools/macho_guard/run_poison_device.py:单相位安装→启动→卸载→证据哈希;
  --expect clean|jailbroken 自动校验 R 组 jailbroken=no / J 组命中 400/401。
- docs/19 §2/§3/§6 更新为“已建、交叉编译通过、待真机执行”。

全套 63 项宿主/工具测试通过。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-11-12 22:43:03 +09:00
co-authored by Claude Opus 4.8
parent c4c9a6925a
commit 3503a24bd4
4 changed files with 446 additions and 8 deletions
+176
View File
@@ -0,0 +1,176 @@
/*
* 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触发)
*
* 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 <string.h>
/* ---------- 发现收集 ---------- */
#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();
}
/* ===== 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;
}