针对「纯客户端抗静态分析」的最高性价比项落地,守在 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>
131 lines
6.0 KiB
Python
131 lines
6.0 KiB
Python
"""静态硬化回归(docs/18 §7):守住「抗静态分析」几条不被后续改动悄悄打回。
|
||
|
||
验收(对 iphoneos 产物):
|
||
1. §7.1 strings 捞不到越狱路径 / 注入特征串 / frida 线程名等明文常量。
|
||
2. 符号可见性:只有 IG_PUBLIC 四入口导出(T);内部 ig_* 不以外部符号导出,
|
||
且链接进可执行文件 strip 后彻底消失。
|
||
3. §7.4 旧基线魔数 0xA5C3F10E7B2D4968 不以明文/立即数出现在反汇编。
|
||
4. 混淆表 .inc 与 gen_jb_paths.py 清单一致(防手改 .inc 或漏重生成)。
|
||
|
||
需 Xcode + iphoneos SDK;缺则跳过。
|
||
"""
|
||
from pathlib import Path
|
||
import shutil
|
||
import subprocess
|
||
import sys
|
||
import tempfile
|
||
import unittest
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
|
||
# strings 里绝不应出现的明文特征(越狱路径 + 注入/frida 特征 + 线程名)。
|
||
FORBIDDEN = [
|
||
'cydia', '/var/jb', '/bin/bash', 'tweakinject', 'sileo', 'mobilesubstrate',
|
||
'ig_jb_probe', 'gum-js-loop', 'pool-frida', 'pool-spawner', 'gmain', 'gdbus',
|
||
'linjector', 'cynject', 'cycript', 'libhooker', 'rocketbootstrap', 'flexloader',
|
||
'shadowhook', 'substitute', 'gadget', 'dobby', 'fishhook',
|
||
]
|
||
PUBLIC = {'_ig_defender_scan', '_ig_poison_init', '_ig_poison_apply', '_ig_policy_scan_and_mark'}
|
||
INTERNAL = ['_ig_poison_mark', '_ig_poison_mask', '_ig_policy_sink',
|
||
'_ig_policy_evaluate_and_mark', '_ig_anti_jb_check', '_ig_anti_inject_check']
|
||
OLD_MAGIC = ['a5c3f10e', '7b2d4968']
|
||
|
||
|
||
def have_sdk():
|
||
if sys.platform != 'darwin' or not shutil.which('xcrun'):
|
||
return False
|
||
r = subprocess.run(['xcrun', '--sdk', 'iphoneos', '--show-sdk-path'],
|
||
capture_output=True, text=True)
|
||
return r.returncode == 0 and r.stdout.strip() != ''
|
||
|
||
|
||
@unittest.skipUnless(have_sdk(), '需要 Xcode + iphoneos SDK')
|
||
class StaticHardeningTests(unittest.TestCase):
|
||
@classmethod
|
||
def setUpClass(cls):
|
||
cls.tmp = tempfile.TemporaryDirectory()
|
||
cls.dist = Path(cls.tmp.name) / 'dist'
|
||
r = subprocess.run(['sh', str(ROOT / 'tools/build_defender_lib.sh'), str(cls.dist)],
|
||
capture_output=True, text=True)
|
||
if r.returncode:
|
||
raise RuntimeError(r.stdout + r.stderr)
|
||
cls.lib = cls.dist / 'lib/libIGDefender.a'
|
||
cls.inc = cls.dist / 'include'
|
||
|
||
@classmethod
|
||
def tearDownClass(cls):
|
||
cls.tmp.cleanup()
|
||
|
||
def _nm(self, path, *extra):
|
||
return subprocess.run(['xcrun', '--sdk', 'iphoneos', 'nm', *extra, str(path)],
|
||
capture_output=True, text=True).stdout
|
||
|
||
def test_no_plaintext_needles(self):
|
||
"""§7.1:strings 不含任何越狱/注入/frida 明文特征。"""
|
||
out = subprocess.run(['strings', '-a', str(self.lib)],
|
||
capture_output=True, text=True).stdout.lower()
|
||
hit = [w for w in FORBIDDEN if w in out]
|
||
self.assertEqual(hit, [], f'明文特征泄漏:{hit}')
|
||
|
||
def test_gen_table_in_sync(self):
|
||
"""混淆表 .inc 与清单一致。"""
|
||
r = subprocess.run([sys.executable, str(ROOT / 'tools/gen_jb_paths.py'), '--check'],
|
||
capture_output=True, text=True)
|
||
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
|
||
|
||
def _defined_class(self):
|
||
"""解析 nm -m,返回 {symbol: 'private external'|'external'}(只取已定义符号)。
|
||
.a 阶段 nm -g 无法区分 private-extern(仍显示 T),须用 nm -m 看 N_PEXT 位。"""
|
||
cls = {}
|
||
for line in self._nm(self.lib, '-m').splitlines():
|
||
if '(undefined)' in line or ') ' not in line:
|
||
continue
|
||
sym = line.rsplit(' ', 1)[-1]
|
||
if 'private external' in line:
|
||
cls[sym] = 'private external'
|
||
elif 'external' in line:
|
||
cls[sym] = 'external'
|
||
return cls
|
||
|
||
def test_internal_symbols_private(self):
|
||
"""内部符号为 private external(-fvisibility=hidden 生效),最终 strip 即消失。"""
|
||
cls = self._defined_class()
|
||
for sym in INTERNAL:
|
||
if sym in cls: # 已定义者必须是 private external,不能是普通 external
|
||
self.assertEqual(cls[sym], 'private external', f'{sym} 未私有化:{cls[sym]}')
|
||
|
||
def test_public_entries_exported(self):
|
||
"""四个 IG_PUBLIC 入口为普通 external(保持导出,供接入方引用)。"""
|
||
cls = self._defined_class()
|
||
for sym in PUBLIC:
|
||
self.assertEqual(cls.get(sym), 'external', f'{sym} 非导出 external:{cls.get(sym)}')
|
||
|
||
def test_strip_removes_internal(self):
|
||
"""链接进可执行文件 + strip -x 后,内部 ig_* 名彻底消失。"""
|
||
w = Path(self.tmp.name)
|
||
src = w / 'main.c'
|
||
src.write_text('#include "IGGuard.h"\n'
|
||
'int main(){ ig_poison_init(); uint8_t k[4]={1,2,3,4},o[4];\n'
|
||
' ig_policy_scan_and_mark("/x",0,0,0); ig_poison_apply(k,o,4); return o[0]; }\n')
|
||
app = w / 'app'
|
||
r = subprocess.run(['xcrun', '--sdk', 'iphoneos', 'clang', '-arch', 'arm64',
|
||
'-miphoneos-version-min=15.0', '-O2', '-I', str(self.inc),
|
||
str(src), str(self.lib), '-lobjc', '-framework', 'Foundation',
|
||
'-o', str(app)], capture_output=True, text=True)
|
||
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
|
||
subprocess.run(['xcrun', '--sdk', 'iphoneos', 'strip', '-x', str(app)], check=True)
|
||
out = self._nm(app)
|
||
for sym in INTERNAL:
|
||
self.assertNotIn(sym, out, f'strip 后仍残留 {sym}')
|
||
|
||
def test_baseline_magic_absent(self):
|
||
"""§7.4:旧基线魔数不以明文/立即数出现在反汇编。"""
|
||
dis = subprocess.run(['xcrun', '--sdk', 'iphoneos', 'otool', '-tV', str(self.lib)],
|
||
capture_output=True, text=True).stdout.lower()
|
||
hit = [m for m in OLD_MAGIC if m in dis]
|
||
self.assertEqual(hit, [], f'旧基线魔数残留:{hit}')
|
||
|
||
|
||
if __name__ == '__main__':
|
||
unittest.main()
|