Files
iOSGuard/defender/poison/IGPoison.c
T
shenleiandClaude Opus 4.8 b76d7cdc21 harden(defender): docs/18 §7 抗静态分析硬化(符号隐藏/串混淆/副本分散/基线派生)
针对「纯客户端抗静态分析」的最高性价比项落地,守在 tests/test_static_hardening.py(6 条,gate 于 iphoneos SDK):

- 符号隐藏 + strip(§7.0 新增):IGVisibility.h 定 IG_INTERNAL=hidden,全库
  -fvisibility=hidden;仅四入口 ig_defender_scan/ig_poison_init/ig_poison_apply/
  ig_policy_scan_and_mark 标 IG_PUBLIC 保持导出,其余 ig_* 变 private external,
  链接后 strip -x 彻底消失——攻击者 nm 不再能按名定位 ig_poison_mark/mask 去 NOP。
- §7.1 串混淆:gen_jb_paths.py(唯一真源)生成 IGJailbreakPaths.inc /
  IGInjectSignatures.inc(XOR keystream,两表不共享 key),运行期解码进栈、用完擦除;
  decode 内 volatile key 屏障阻止 -O2 把常量折叠回 __const 明文。strings 已捞不到
  /var/jb、Cydia、substrate、gum-js-loop 等。--check 守 .inc 与清单同步。
- §7.2 副本分散:g_h0/h1/h2 置于不相邻命名段 __DATA,__ig_p0/p1/p2,加 __ig_pd 诱饵
  (mask 不读);刻意不用 TLS(会破坏跨线程毒化一致性)。
- §7.4 基线派生:删硬编码 0xA5C3F10E7B2D4968,改 ig_poison_baseline() 由拆分种子经
  volatile 屏障派生;otool -tV 已无旧魔数立即数。
- §7.3 OLLVM/VMP:仅留 build 的 IG_OBFUSCATOR hook,stock clang 无能力,需另备工具链(未完成)。
- §7.5 越狱设备正向验证仍阻塞(无设备)。

构建/快照同步:build_defender_lib.sh 加 -fvisibility=hidden + gen --check + IG_OBFUSCATOR
hook;build_poison_device.py 快照纳入 *.inc。全量 69 测试通过;poison_device.m 全模块
iphoneos 交叉编译通过(arm64)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-13 10:47:35 +09:00

96 lines
4.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* IGPoison.c — 毒化状态机实现。见 IGPoison.h 的设计与边界声明。
*
* 实现要点:
* - 三副本冗余、写法各异、均 volatile(防编译器把 (copy ^ BASELINE) 常量折叠回 0)。
* - 掩码由三副本与基线异或后按位或得出:健康时每副本==基线 → 全 0;毒化时非 0。
* - 没有任何 `if (poisoned)` 控制流;处置是纯算术数据流。
*
* §7 硬化(本次已落地):
* - §7.4 基线不再明文驻留:IG_POISON_BASELINE 宏删除,改 ig_poison_baseline() 运行期
* 由分片种子派生,volatile 屏障阻止折叠回单一 64bit 魔数写入常量区。
* - §7.2 三副本分散:g_h0/g_h1/g_h2 各置于独立命名 section(__DATA 内不相邻),
* 并维护一路诱饵 g_hd(init/mark 同步写、mask 不读),抬高「哪几路真正喂掩码」的辨识成本。
*
* §7 硬化(仍 TODO,需额外工具链,见 docs/18 §7.3):
* - mark / mask / apply 三个函数用 OLLVM(Fla+BCF) + 常量加密盖住;本库预留
* IG_OBFUSCATOR 编译钩子,默认构建不启用。
*/
#include "IGPoison.h"
/*
* §7.4 基线运行期派生:具体 bit 无语义要求(健康态三副本==基线即可,mask=copy^基线),
* 只要全程一致且非零。volatile 种子强制运行期求值,阻止整条链折叠回单一常量写入 __const。
*/
static uint64_t ig_poison_baseline(void) {
volatile uint64_t seed = 0x243F6A88ULL; /* 分片种子,非最终值 */
uint64_t x = (uint64_t)seed;
x ^= 0x85A308D3ULL;
x *= 0xFF51AFD7ED558CCDULL;
x ^= x >> 33;
x += 0x2545F4914F6CDD1DULL;
x ^= x >> 29;
return x | 1ULL; /* 保证非零 */
}
/*
* §7.2 三副本分散:各自独立 section,避免三者在 BSS 相邻被一眼框定为「一组健康值」。
* 仍是进程级全局(非 TLS)——毒化态必须跨线程一致:检测线程 mark 后,解密线程读到的
* 掩码必须同为非零,TLS 会让每线程各看各的、破坏处置闭环,故**刻意不用 TLS**。
*/
static volatile uint64_t g_h0 __attribute__((section("__DATA,__ig_p0")));
static volatile uint64_t g_h1 __attribute__((section("__DATA,__ig_p1")));
static volatile uint64_t g_h2 __attribute__((section("__DATA,__ig_p2")));
/* 诱饵副本:与真副本同步维护、写法相同,但 mask 永不读它。抬高静态辨识成本。 */
static volatile uint64_t g_hd __attribute__((section("__DATA,__ig_pd")));
/* 把 entropy 扩散成三个互不相同、且必非 0 的偏移量(保证任一副本都会偏离基线)。 */
static uint64_t ig_spread(uint64_t e, unsigned lane) {
e ^= 0x9E3779B97F4A7C15ULL * (lane + 1u); /* 每路不同常量 */
e ^= e >> 30; e *= 0xBF58476D1CE4E5B9ULL;
e ^= e >> 27; e *= 0x94D049BB133111EBULL;
e ^= e >> 31;
return e | 1ULL; /* 置最低位,确保非 0 */
}
void ig_poison_init(void) {
uint64_t b = ig_poison_baseline();
g_h0 = b;
g_h1 = b;
g_h2 = b;
g_hd = b; /* 诱饵同步,看起来与真副本无异 */
}
/* 构造期兜底初始化(若宿主未显式调用 ig_poison_init)。 */
__attribute__((constructor))
static void ig_poison_ctor(void) { ig_poison_init(); }
void ig_poison_mark(uint64_t entropy) {
/* entropy 为 0 时仍强制非 0,避免“混入 0 = 不生效”。 */
uint64_t e = entropy ? entropy : 0xD1B54A32D192ED03ULL;
g_h0 ^= ig_spread(e, 0);
g_hd ^= ig_spread(e, 3); /* 诱饵也动,混淆“哪几路生效” */
g_h1 ^= ig_spread(e, 1);
g_h2 ^= ig_spread(e, 2);
}
uint64_t ig_poison_mask(void) {
/* 健康:每项 ^基线 == 0 → 或起来为 0。毒化:至少一项非 0。无分支。
* 只读三路真副本,诱饵 g_hd 不参与——即便攻击者把 g_hd 写回基线也不影响掩码。 */
uint64_t b = ig_poison_baseline();
uint64_t m = (g_h0 ^ b)
| (g_h1 ^ b)
| (g_h2 ^ b);
return m;
}
void ig_poison_apply(const uint8_t *in, uint8_t *out, size_t n) {
if (!in || !out) return;
uint64_t m = ig_poison_mask();
for (size_t i = 0; i < n; ++i) {
/* 健康 m==0 → 扰动字节恒 0 → out[i]==in[i]。毒化 → 每字节被 m 的对应字节异或。 */
uint8_t d = (uint8_t)((m >> ((i & 7u) * 8u)) & 0xFFu);
out[i] = (uint8_t)(in[i] ^ d);
}
}