#!/usr/bin/env python3 """安装并运行 step12 处置链真机验证诊断 App,收集/解析结果(见 docs/19)。 单相位启动(无需 DYLD 注入)。解析两类输出: - IG_POISON_CASE:环境无关不变量(D4–D7),构成套件 PASS/FAIL 门槛。 - IG_POISON_OBS / IG_POISON_ENV:环境相关观察(D1–D3 + 越狱判定),供人按 R(非越狱)/J(越狱)组对照 docs/19 §4 验收门槛判定,不参与套件 PASS/FAIL。 结束即卸载诊断 App;全部证据与哈希写入输出目录(必须不存在)。 退出码:套件不变量全过且环境判定与 --expect 一致 → 0;否则 1。 --expect clean|jailbroken|any(默认 any):声明本次所在组的期望环境,便于自动校验 “R 组必须 jailbroken=no、J 组必须 jailbroken=yes”。 """ import argparse import hashlib import json import re from pathlib import Path import subprocess CASE_RE = re.compile(r'IG_POISON_CASE (\S+) (PASS|FAIL)(?: detail=(.*))?$') OBS_RE = re.compile(r'IG_POISON_OBS (\S+) (.*)$') SUITE_RE = re.compile(r'IG_POISON_SUITE (PASS|FAIL) pass=(\d+) fail=(\d+)') ENV_RE = re.compile(r'IG_POISON_ENV jailbroken=(yes|no) reason400=(\d+) reason401=(\d+)') def run(args, log: Path, label: str, timeout: int = 240): r = subprocess.run(list(map(str, args)), capture_output=True, text=True, timeout=timeout) log.write_text(r.stdout + r.stderr) return r def parse_console(path: Path): cases, observations, suite, env = [], [], None, None for line in path.read_text(errors='replace').splitlines(): m = CASE_RE.search(line) if m: cases.append(dict(name=m.group(1), result=m.group(2), detail=m.group(3) or '')) continue m = OBS_RE.search(line) if m: observations.append(dict(name=m.group(1), detail=m.group(2))) continue m = SUITE_RE.search(line) if m: suite = dict(result=m.group(1), passed=int(m.group(2)), failed=int(m.group(3))) continue m = ENV_RE.search(line) if m: env = dict(jailbroken=(m.group(1) == 'yes'), reason400=int(m.group(2)), reason401=int(m.group(3))) return cases, observations, suite, env def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument('--device', required=True) p.add_argument('--app', type=Path, required=True) p.add_argument('--bundle-id', required=True) p.add_argument('--output', type=Path, required=True) p.add_argument('--expect', choices=['clean', 'jailbroken', 'any'], default='any', help='R 组传 clean,J 组传 jailbroken;默认 any 不校验环境') a = p.parse_args() out = a.output.resolve() if out.exists(): raise ValueError('输出目录必须不存在') out.mkdir(parents=True) base = ['xcrun', 'devicectl'] r = run(base + ['device', 'info', 'details', '--device', a.device], out / 'device-details.log', 'device info') (out / 'device-details.json').write_text(r.stdout) run(base + ['device', 'install', 'app', '--device', a.device, '--timeout', '60', '--json-output', out / 'install.json', a.app], out / 'install.log', 'install') run(base + ['device', 'process', 'launch', '--device', a.device, '--console', '--timeout', '90', '--json-output', out / 'launch.json', a.bundle_id], out / 'console.log', 'launch') cases, observations, suite, env = parse_console(out / 'console.log') run(base + ['device', 'uninstall', 'app', '--device', a.device, '--timeout', '60', '--json-output', out / 'uninstall.json', a.bundle_id], out / 'uninstall.log', 'uninstall') failed = [c for c in cases if c['result'] != 'PASS'] invariants_ok = bool(cases) and not failed and suite and suite['result'] == 'PASS' env_ok = True env_note = '未校验(--expect any)' if a.expect != 'any': if env is None: env_ok = False env_note = '未解析到 IG_POISON_ENV' else: want = (a.expect == 'jailbroken') env_ok = (env['jailbroken'] == want) env_note = (f"期望 {a.expect},实测 jailbroken=" f"{'yes' if env['jailbroken'] else 'no'}" f"(400={env['reason400']} 401={env['reason401']})") verdict = 'PASS' if (invariants_ok and env_ok) else 'FAIL' verification = dict( device=a.device, bundle_id=a.bundle_id, app=str(a.app), expect=a.expect, verdict=verdict, invariant_cases=cases, invariant_suite=suite, invariants_ok=invariants_ok, observations=observations, environment=env, env_check=dict(ok=env_ok, note=env_note), acceptance_ref='docs/19 §4', limitations=[ 'D1–D3 依赖真实环境:R 组应 jailbroken=no 且 baseline mask=0;J 组应命中 400/401', 'bypass(Shadow/Liberty) 可规避:J 组若装 bypass 未命中不代表探针失效,需分裸越狱/带 bypass 两档记录', '诊断为独立进程,业务密钥派生端到端效果需接入 readoor 后再验(docs/18 §4)', ]) (out / 'device-verification.json').write_text( json.dumps(verification, ensure_ascii=False, indent=2) + '\n') hashes = {} for f in sorted(out.rglob('*')): if f.is_file() and f.name != 'evidence-hashes.json': hashes[str(f.relative_to(out))] = hashlib.sha256(f.read_bytes()).hexdigest() (out / 'evidence-hashes.json').write_text( json.dumps(hashes, ensure_ascii=False, indent=2) + '\n') print(json.dumps(dict(verdict=verdict, invariants_ok=invariants_ok, suite=suite, environment=env, env_check=env_note), ensure_ascii=False)) raise SystemExit(0 if verdict == 'PASS' else 1) if __name__ == '__main__': main()