无越狱设备下 J 组真实越狱文件命中无法证;但 R 组 D2 的 400=0 单看无法区分 「无越狱文件」与「svc 直发在 iOS 静默失败恒返 0」的致命假阴性。 - poison_device.m 加 D8 svc_access_live:自身沙盒建真实可访问文件喂 ig_anti_jb_check_paths,命中即证 svc #0x80 access(2) 在真机 iOS arm64 能工作。 - iPhone 16 Plus(非越狱)实测:verdict PASS,不变量 5/5,D8 accessible_file_hits=1。 坐实 D2 的 0 命中是真阴性(无越狱文件)而非探针坏;聚合扫描 mask=0x0 零毒化。 - docs/19 §2 补 D8 用例与价值说明,§6 记 R 组达标、J 组环境天花板与残留缺口收窄。 至此无越狱设备下的最大可得验证完成:机制真机可命中 + 非越狱零误报。 残留仅「真越狱环境端到端正向(400真文件/401沙盒逃逸)」,补越狱机即闭合。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
201 lines
9.2 KiB
Objective-C
201 lines
9.2 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 <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();
|
||
}
|
||
|
||
/* ===== 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();
|
||
}
|
||
|
||
/* ===== 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;
|
||
}
|