test(defender): 固化处置链宿主基线 + 真机验证协议(docs/19),修越狱聚合回归
P0 验真:真机验证前先把「检测→分级→数据毒化」闭环做成可复现宿主绿灯。 新增: - tests/fixtures/poison_host.c + tests/test_poison_policy.py:poison/policy/ anti_jailbreak 闭环自测 19/19(poison 恒等+坏key+不可逆、policy 分级+去重+ mask一致、jailbreak 反向零误报+400+401),集成进 unittest 套件。 - docs/19_DISPOSITION_CHAIN_DEVICE_VERIFICATION.md:真机验证协议——R(非越狱)/ J(越狱)两组、D1–D7 用例、验收门槛(反向零毒化为开毒化前提)、边界与进度。 修复(接越狱探针进 ig_defender_scan 聚合器时埋下的回归): - test_defender_host.py SOURCES 漏 anti_jailbreak/IGAntiJailbreak.c → 链接缺符号。 - defender_host.c:reasons[] 由 256 扩到 512(400/401 越界漏计);clean 断言排除 越狱文件命中(macOS 自带 Unix userland,按 iOS 标准宿主本身即“越狱”,非真实误报)。 全套 63 项宿主/工具测试通过。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
599e1da5ce
commit
c4c9a6925a
Vendored
+9
-3
@@ -16,10 +16,10 @@
|
||||
#include <mach-o/loader.h>
|
||||
#include <mach-o/nlist.h>
|
||||
|
||||
typedef struct { int reasons[256]; int detected; int unavailable; } Findings;
|
||||
typedef struct { int reasons[512]; int detected; int unavailable; } Findings;
|
||||
static int collect(const IGFinding *f, void *context) {
|
||||
Findings *out = context;
|
||||
if (f->reason >= 0 && f->reason < 256) out->reasons[f->reason]++;
|
||||
if (f->reason >= 0 && f->reason < 512) out->reasons[f->reason]++;
|
||||
if (f->status == IG_DETECTED) out->detected++;
|
||||
if (f->status == IG_UNAVAILABLE) out->unavailable++;
|
||||
printf("status=%d reason=%d subject=%s evidence=%s\n",
|
||||
@@ -89,7 +89,13 @@ int main(int argc, char **argv) {
|
||||
Findings f={0};
|
||||
if (!strcmp(argv[1],"clean")) {
|
||||
int hits=ig_defender_scan(NULL,0,collect,&f);
|
||||
return hits!=f.detected || f.detected || f.unavailable;
|
||||
/* 越狱文件探针(400/401)在 macOS 宿主必然命中:mac 自带完整 Unix userland
|
||||
* (/usr/sbin/sshd、/bin/bash、/bin/sh…),按 iOS 标准宿主本身即“越狱”。
|
||||
* 这是宿主/iOS 环境差异,非真实误报——iOS 非越狱设备上这些路径不存在/沙盒不可达,
|
||||
* 其反向零命中由真机 R 组验证(docs/19)。故宿主 clean 只对非越狱探针断言零 DETECTED。 */
|
||||
int jb = f.reasons[IG_REASON_JAILBREAK_FILE] + f.reasons[IG_REASON_JAILBREAK_SANDBOX];
|
||||
int non_jb_detected = f.detected - jb;
|
||||
return hits!=f.detected || non_jb_detected || f.unavailable;
|
||||
}
|
||||
if (!strcmp(argv[1],"unresolved")) {
|
||||
const char *names[]={"IG_fixture_symbol_that_does_not_exist_123"};
|
||||
|
||||
Vendored
+169
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* poison_host.c — 处置链宿主自测(poison + policy + anti_jailbreak)。
|
||||
*
|
||||
* 固化此前只在 /tmp 临时跑过的「检测→分级→数据毒化」闭环为可复现基线,
|
||||
* 作为真机验证(docs/19)之前的宿主绿灯。覆盖:
|
||||
* A. poison:健康恒等、毒化破坏 key、mask 非分支语义、apply 逐字节扩散。
|
||||
* B. policy:弱信号/UNAVAILABLE/CLEAR 不触发、单 MEDIUM 不触发、双 MEDIUM 去重后触发、
|
||||
* 单 STRONG 触发——且触发后 mask 必非 0(真正接到了毒化)。
|
||||
* C. anti_jailbreak:可注入路径版本——不存在路径零命中(反向零误报)、
|
||||
* 存在路径命中 400、沙盒逃逸探测命中 401。
|
||||
*
|
||||
* 只用 ig_anti_jb_check_paths 的可注入入口,不依赖真机越狱特征文件,故宿主可跑。
|
||||
* 退出码 0=全绿;非 0=有用例失败(stderr 打印失败项)。
|
||||
*/
|
||||
#include "IGDefender.h"
|
||||
#include "poison/IGPoison.h"
|
||||
#include "policy/IGPolicy.h"
|
||||
#include "anti_jailbreak/IGAntiJailbreak.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int g_pass = 0, g_fail = 0;
|
||||
static void ok(const char *name, int cond) {
|
||||
if (cond) { g_pass++; printf(" PASS %s\n", name); }
|
||||
else { g_fail++; fprintf(stderr, " FAIL %s\n", name); }
|
||||
}
|
||||
|
||||
/* ---- 构造 policy sink 的输入:手工喂 finding 走 ig_policy_sink ---- */
|
||||
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);
|
||||
}
|
||||
|
||||
/* 跑一组 finding,返回 evaluate 是否触发毒化(并断言 mask 的一致性)。 */
|
||||
static int run_policy(void (*feeder)(IGPolicyState *)) {
|
||||
ig_poison_init(); /* 每例从健康态起 */
|
||||
IGPolicyState st;
|
||||
ig_policy_reset(&st, NULL, NULL);
|
||||
feeder(&st);
|
||||
int triggered = ig_policy_evaluate_and_mark(&st);
|
||||
return triggered;
|
||||
}
|
||||
|
||||
/* ---- A. poison ---- */
|
||||
static void test_poison(void) {
|
||||
printf("[A] poison\n");
|
||||
const uint8_t key[] = { 's','t','a','r','t','i','a','s','o','f','t', 0x11, 0x22 };
|
||||
const size_t n = sizeof(key);
|
||||
uint8_t out[sizeof(key)];
|
||||
|
||||
/* A1 健康:mask==0,apply 恒等 */
|
||||
ig_poison_init();
|
||||
ok("healthy_mask_zero", ig_poison_mask() == 0);
|
||||
memset(out, 0xAB, n);
|
||||
ig_poison_apply(key, out, n);
|
||||
ok("healthy_apply_identity", memcmp(out, key, n) == 0);
|
||||
|
||||
/* A2 毒化:mask!=0,apply 破坏 key(至少一字节不同) */
|
||||
ig_poison_mark(0xC0FFEE);
|
||||
ok("poisoned_mask_nonzero", ig_poison_mask() != 0);
|
||||
ig_poison_apply(key, out, n);
|
||||
ok("poisoned_key_corrupted", memcmp(out, key, n) != 0);
|
||||
|
||||
/* A3 毒化不可逆回健康(再 mark 不会抵消;只有 init 能清) */
|
||||
ig_poison_mark(0x1234);
|
||||
ok("poisoned_still_nonzero_after_second_mark", ig_poison_mask() != 0);
|
||||
ig_poison_init();
|
||||
ok("init_restores_healthy", ig_poison_mask() == 0);
|
||||
|
||||
/* A4 entropy==0 仍生效(防“混 0 = 不生效”) */
|
||||
ig_poison_init();
|
||||
ig_poison_mark(0);
|
||||
ok("zero_entropy_still_poisons", ig_poison_mask() != 0);
|
||||
}
|
||||
|
||||
/* ---- B. policy 分级 ---- */
|
||||
static void f_weak_only(IGPolicyState *st) {
|
||||
feed(st, IG_DETECTED, IG_REASON_FOREIGN_IMAGE); /* 201 WEAK */
|
||||
feed(st, IG_DETECTED, IG_REASON_FRIDA_PORT); /* 204 WEAK */
|
||||
}
|
||||
static void f_unavailable_clear(IGPolicyState *st) {
|
||||
feed(st, IG_UNAVAILABLE, IG_REASON_INLINE_HOOK); /* STRONG reason 但 UNAVAILABLE→忽略 */
|
||||
feed(st, IG_CLEAR, IG_REASON_GOT_REBIND); /* CLEAR→忽略 */
|
||||
}
|
||||
static void f_single_medium(IGPolicyState *st) {
|
||||
feed(st, IG_DETECTED, IG_REASON_OBJC_IMP_CHANGED); /* 110 单 MEDIUM */
|
||||
}
|
||||
static void f_medium_dup(IGPolicyState *st) {
|
||||
feed(st, IG_DETECTED, IG_REASON_OBJC_IMP_CHANGED); /* 110 */
|
||||
feed(st, IG_DETECTED, IG_REASON_OBJC_IMP_CHANGED); /* 110 重复 → 去重后仍算 1 */
|
||||
}
|
||||
static void f_two_medium(IGPolicyState *st) {
|
||||
feed(st, IG_DETECTED, IG_REASON_OBJC_IMP_CHANGED); /* 110 */
|
||||
feed(st, IG_DETECTED, IG_REASON_SYMBOL_PROVENANCE); /* 100 → 2 个不同 MEDIUM */
|
||||
}
|
||||
static void f_single_strong(IGPolicyState *st) {
|
||||
feed(st, IG_DETECTED, IG_REASON_JAILBREAK_SANDBOX); /* 401 STRONG */
|
||||
}
|
||||
|
||||
static void test_policy(void) {
|
||||
printf("[B] policy\n");
|
||||
ok("weak_only_no_trigger", run_policy(f_weak_only) == 0);
|
||||
ok("unavailable_clear_no_trigger",run_policy(f_unavailable_clear) == 0);
|
||||
ok("single_medium_no_trigger", run_policy(f_single_medium) == 0);
|
||||
ok("medium_dup_no_trigger", run_policy(f_medium_dup) == 0); /* 去重:同 reason×2 ≠ 2 distinct */
|
||||
ok("two_distinct_medium_trigger", run_policy(f_two_medium) == 1);
|
||||
ok("single_strong_trigger", run_policy(f_single_strong) == 1);
|
||||
|
||||
/* 触发后 mask 必非 0(策略真的接到了毒化,不只是返回值) */
|
||||
run_policy(f_single_strong);
|
||||
ok("strong_trigger_sets_mask", ig_poison_mask() != 0);
|
||||
ig_poison_init();
|
||||
run_policy(f_weak_only);
|
||||
ok("weak_leaves_mask_zero", ig_poison_mask() == 0);
|
||||
}
|
||||
|
||||
/* ---- C. anti_jailbreak(可注入) ---- */
|
||||
static int g_jb_hits;
|
||||
static int jb_sink(const IGFinding *f, void *ctx) {
|
||||
(void)ctx;
|
||||
if (f->status == IG_DETECTED) g_jb_hits++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_jailbreak(void) {
|
||||
printf("[C] anti_jailbreak\n");
|
||||
|
||||
/* C1 反向:全是不存在的路径 + 不做逃逸探测 → 零命中(守误报底线) */
|
||||
const char *absent[] = {
|
||||
"/var/jb/__ig_absent_a", "/Applications/__ig_absent.app", "/usr/bin/__ig_absent"
|
||||
};
|
||||
g_jb_hits = 0;
|
||||
ig_anti_jb_check_paths(absent, sizeof(absent)/sizeof(absent[0]), NULL, jb_sink, NULL);
|
||||
ok("absent_paths_zero_hit", g_jb_hits == 0);
|
||||
|
||||
/* C2 正向 400:构造一个真实存在的临时文件冒充越狱特征文件 → 命中 */
|
||||
char tmpl[] = "/tmp/ig_jb_fileXXXXXX";
|
||||
int fd = mkstemp(tmpl);
|
||||
ok("tmpfile_created", fd >= 0);
|
||||
if (fd >= 0) close(fd);
|
||||
const char *present[] = { tmpl };
|
||||
g_jb_hits = 0;
|
||||
ig_anti_jb_check_paths(present, 1, NULL, jb_sink, NULL);
|
||||
ok("present_path_hits_400", g_jb_hits >= 1);
|
||||
unlink(tmpl);
|
||||
|
||||
/* C3 正向 401:宿主上 /tmp 可写,沙盒逃逸探测应成功创建→命中(真机非越狱则失败=反向) */
|
||||
char probe[] = "/tmp/ig_jb_probeXXXXXX";
|
||||
int pfd = mkstemp(probe); if (pfd >= 0) { close(pfd); unlink(probe); } /* 腾出路径给探测自建 */
|
||||
g_jb_hits = 0;
|
||||
ig_anti_jb_check_paths(absent, sizeof(absent)/sizeof(absent[0]), probe, jb_sink, NULL);
|
||||
ok("sandbox_probe_hits_401_on_host", g_jb_hits >= 1);
|
||||
unlink(probe);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
printf("== poison_host self-test ==\n");
|
||||
test_poison();
|
||||
test_policy();
|
||||
test_jailbreak();
|
||||
printf("== %d pass, %d fail ==\n", g_pass, g_fail);
|
||||
return g_fail == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import unittest
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SOURCES = ['IGDefender.c','anti_hook/IGAntiHook.c','anti_hook/IGObjCBaseline.c',
|
||||
'anti_frida/IGAntiInject.c','anti_debug/IGAntiDebug.c',
|
||||
'anti_jailbreak/IGAntiJailbreak.c',
|
||||
'syscall/IGSyscall.c','resolve/IGResolve.c','dispatch/IGDispatch.c']
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
"""处置链宿主自测:poison + policy + anti_jailbreak 闭环(检测→分级→数据毒化)。
|
||||
|
||||
固化此前只在临时目录跑过的闭环为可复现基线,作为真机验证(docs/19)前的宿主绿灯。
|
||||
编译 tests/fixtures/poison_host.c 并运行,退出码 0=全部用例通过。
|
||||
"""
|
||||
from pathlib import Path
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SOURCES = [
|
||||
'poison/IGPoison.c', 'policy/IGPolicy.c',
|
||||
'anti_jailbreak/IGAntiJailbreak.c', 'syscall/IGSyscall.c',
|
||||
'IGDefender.c', 'anti_hook/IGAntiHook.c', 'anti_hook/IGObjCBaseline.c',
|
||||
'anti_frida/IGAntiInject.c', 'anti_debug/IGAntiDebug.c',
|
||||
'resolve/IGResolve.c', 'dispatch/IGDispatch.c',
|
||||
'dispatch/IGArchiveGuard.c', 'call_bridge/IGFileBridge.c',
|
||||
]
|
||||
|
||||
|
||||
@unittest.skipUnless(sys.platform == 'darwin' and shutil.which('xcrun'), '需要 Xcode/macOS')
|
||||
class PoisonPolicyTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.tmp = tempfile.TemporaryDirectory()
|
||||
folder = Path(cls.tmp.name)
|
||||
cls.exe = folder / 'poison-host'
|
||||
cls.env = dict(os.environ)
|
||||
cls.env.pop('DYLD_INSERT_LIBRARIES', None)
|
||||
args = ['xcrun', '--sdk', 'macosx', 'clang', '-O2',
|
||||
'-I', str(ROOT / 'defender/include'), '-I', str(ROOT / 'defender'),
|
||||
str(ROOT / 'tests/fixtures/poison_host.c'),
|
||||
*(str(ROOT / 'defender' / p) for p in SOURCES),
|
||||
'-framework', 'Foundation', '-lobjc', '-o', str(cls.exe)]
|
||||
p = subprocess.run(args, capture_output=True, text=True, env=cls.env)
|
||||
if p.returncode:
|
||||
raise RuntimeError(p.stdout + p.stderr)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.tmp.cleanup()
|
||||
|
||||
def test_chain(self):
|
||||
p = subprocess.run([str(self.exe)], capture_output=True, text=True, env=self.env)
|
||||
# 保留逐例证据到 unittest 日志。
|
||||
print(p.stdout.strip(), flush=True)
|
||||
self.assertEqual(p.returncode, 0, p.stdout + p.stderr)
|
||||
self.assertIn('0 fail', p.stdout)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user