#!/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()