Files
iOSGuard/tools/gen_jb_paths.py
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

138 lines
5.8 KiB
Python
Raw Permalink 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.
#!/usr/bin/env python3
"""gen_jb_paths.py — 生成越狱特征路径的 XOR 混淆表(docs/18 §7.1 IG_ENC)。
越狱路径常量(/var/jb、Cydia、substrate…)若明文入二进制,`strings` 一把捞出,
等于把检测目标白送。本脚本把每条路径按位置相关的 keystream 异或编码,产出
defender/anti_jailbreak/IGJailbreakPaths.inc;运行期由 IGAntiJailbreak.c 解码进
栈缓冲、用完擦除,二进制里不再驻留明文。
路径清单以本脚本为唯一真源;改清单后重跑本脚本重生成 .inc(可复现)。
python3 tools/gen_jb_paths.py # 写回 .inc
python3 tools/gen_jb_paths.py --check # 只校验 .inc 与当前清单一致(CI 用)
"""
import argparse
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[1]
INC = ROOT / 'defender/anti_jailbreak/IGJailbreakPaths.inc'
INC_INJ = ROOT / 'defender/anti_frida/IGInjectSignatures.inc'
# keystream: enc[j] = plain[j] ^ ((K0 + idx*K1 + j*K2) & 0xFF)。解码端须完全一致。
K0, K1, K2 = 0x5A, 0x2F, 0x13
# anti_frida 用一组不同常量(defense in depth,两表不共享 keystream)。
IK0, IK1, IK2 = 0x3C, 0x47, 0x1D
# 已知注入/调试/越狱增强框架镜像名子串(小写,ig_strstr 子串匹配)。
INJECT = [
"frida", "gadget", "cynject", "cycript", "libcycript",
"substrate", "substitute", "libsubstrate", "tweakinject",
"rocketbootstrap", "reveal", "flexloader", "libhooker",
"shadowhook", "dobby", "fishhook",
]
# 已知 frida 线程名(精确匹配)。
THREADS = [
"gum-js-loop", "gmain", "gdbus", "pool-frida", "pool-spawner", "linjector",
]
# 内置 iOS 越狱特征(强信号)。rootless 越狱重点在 /var/jb。
PATHS = [
"/var/jb",
"/Applications/Cydia.app",
"/Applications/Sileo.app",
"/Applications/Zebra.app",
"/usr/sbin/sshd",
"/usr/bin/ssh",
"/bin/bash",
"/bin/sh",
"/etc/apt",
"/private/var/lib/apt",
"/usr/lib/substrate",
"/Library/MobileSubstrate/MobileSubstrate.dylib",
"/usr/lib/TweakInject",
]
# 沙盒外探测路径(能创建即沙盒逃逸)。
PROBE = "/private/.ig_jb_probe"
def encode(s: str, idx: int, k0=K0, k1=K1, k2=K2) -> list:
return [(ord(c) ^ ((k0 + idx * k1 + j * k2) & 0xFF)) & 0xFF for j, c in enumerate(s)]
def arr(name: str, data: list) -> str:
body = ', '.join('0x%02x' % b for b in data)
return f'static const unsigned char {name}[] = {{ {body} }};'
def render() -> str:
out = []
out.append('/* IGJailbreakPaths.inc — 由 tools/gen_jb_paths.py 生成,请勿手改。 */')
out.append('/* 越狱路径 XOR 混淆表(§7.1):enc[j]=plain[j]^((K0+idx*K1+j*K2)&0xFF)。 */')
out.append(f'#define IG_JB_XOR_K0 0x{K0:02x}u')
out.append(f'#define IG_JB_XOR_K1 0x{K1:02x}u')
out.append(f'#define IG_JB_XOR_K2 0x{K2:02x}u')
out.append('')
for i, p in enumerate(PATHS):
out.append(arr(f'kIGJbEnc{i}', encode(p, i)) + f' /* len={len(p)} */')
out.append('')
rows = ', '.join(f'{{ kIGJbEnc{i}, {len(p)} }}' for i, p in enumerate(PATHS))
out.append('static const struct { const unsigned char *b; unsigned char n; } '
f'kIGJbEnc[] = {{ {rows} }};')
out.append(f'#define IG_JB_ENC_COUNT {len(PATHS)}')
out.append('')
out.append('/* 沙盒逃逸探测路径(idx 用一个与上表不冲突的独立下标)。 */')
probe_idx = 0xA5
out.append(arr('kIGJbProbeEnc', encode(PROBE, probe_idx)) + f' /* len={len(PROBE)} */')
out.append(f'#define IG_JB_PROBE_LEN {len(PROBE)}')
out.append(f'#define IG_JB_PROBE_IDX 0x{probe_idx:02x}')
out.append('')
return '\n'.join(out) + '\n'
def render_inject() -> str:
out = []
out.append('/* IGInjectSignatures.inc — 由 tools/gen_jb_paths.py 生成,请勿手改。 */')
out.append('/* 注入/Frida 特征串 XOR 混淆表(§7.1):enc[j]=plain[j]^((K0+idx*K1+j*K2)&0xFF)。 */')
out.append(f'#define IG_INJ_XOR_K0 0x{IK0:02x}u')
out.append(f'#define IG_INJ_XOR_K1 0x{IK1:02x}u')
out.append(f'#define IG_INJ_XOR_K2 0x{IK2:02x}u')
out.append('')
for i, s in enumerate(INJECT):
out.append(arr(f'kIGInjEnc{i}', encode(s, i, IK0, IK1, IK2)) + f' /* len={len(s)} */')
rows = ', '.join(f'{{ kIGInjEnc{i}, {len(s)} }}' for i, s in enumerate(INJECT))
out.append('static const struct { const unsigned char *b; unsigned char n; } '
f'kIGInjEnc[] = {{ {rows} }};')
out.append(f'#define IG_INJ_ENC_COUNT {len(INJECT)}')
out.append('')
for i, s in enumerate(THREADS):
out.append(arr(f'kIGThrEnc{i}', encode(s, i, IK0, IK1, IK2)) + f' /* len={len(s)} */')
rows = ', '.join(f'{{ kIGThrEnc{i}, {len(s)} }}' for i, s in enumerate(THREADS))
out.append('static const struct { const unsigned char *b; unsigned char n; } '
f'kIGThrEnc[] = {{ {rows} }};')
out.append(f'#define IG_THR_ENC_COUNT {len(THREADS)}')
out.append('')
return '\n'.join(out) + '\n'
def main():
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument('--check', action='store_true', help='只校验,不写回')
a = ap.parse_args()
outputs = [(INC, render(), f'{len(PATHS)} 条路径 + probe'),
(INC_INJ, render_inject(), f'{len(INJECT)} 注入特征 + {len(THREADS)} 线程名')]
if a.check:
for path, text, _ in outputs:
cur = path.read_text() if path.exists() else ''
if cur != text:
print(f'{path.name} 与当前清单不一致,请重跑 gen_jb_paths.py', file=sys.stderr)
sys.exit(1)
print('ok: 混淆表 .inc 与清单一致')
return
for path, text, note in outputs:
path.write_text(text)
print(f'已写 {path.relative_to(ROOT)}({note})')
if __name__ == '__main__':
main()