- defender: 扩展 IGDispatch/IGArchiveGuard/IGAntiHook/IGObjCBaseline/IGResolve,支持 Route 2 静态库桥接 (IGThreadID.c) 与防重签名检测 - llvm_passes: 新增 IGObfuscator LLVM IR 插件源码(控制流平坦化、字符串加密、间接跳转/调用、不透明常量/字段、MBA、函数外提、基本块拆分、反 Hook 序言) - tools & config: 新增 Xcode 归档编译/链接包装器、LLVM 自愈工具链 (ig_toolchain_resolver)、IPA 审计门禁、崩溃符号还原及最新 ig_llvm_policy.json(文件级匹配 + 3.0 全业务 Handler 平坦化) - tests & docs: 补齐 M0~M3、Pass 单测及系统调用调度/Route 2/LLVM 方案文档;.gitignore 忽略 *.bc 与 .obfuscate_extraction_cache.json
206 lines
8.7 KiB
Python
206 lines
8.7 KiB
Python
"""Unit tests for M7: static framework ThinLTO bitcode extraction, obfuscation & repacking."""
|
|
import json
|
|
from pathlib import Path
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
TOOLS_LLVM_OBF = ROOT / 'tools' / 'llvm_obf'
|
|
TOOLS_XCODE_ARCHIVE = ROOT / 'tools' / 'xcode_archive'
|
|
sys.path.insert(0, str(TOOLS_LLVM_OBF))
|
|
sys.path.insert(0, str(TOOLS_XCODE_ARCHIVE))
|
|
|
|
from ig_toolchain_resolver import detect_stock_tool, resolve_opt # noqa: E402
|
|
import ig_ld_wrapper # noqa: E402
|
|
|
|
|
|
def require_host_opt() -> str:
|
|
tc = resolve_opt(require_obf=False)
|
|
opt_path = tc.get('opt_path')
|
|
if not opt_path:
|
|
raise unittest.SkipTest(f"Standalone opt not built yet: {tc.get('warning')}")
|
|
return opt_path
|
|
|
|
|
|
def iphoneos_sdk_path() -> str:
|
|
return subprocess.check_output(['xcrun', '--sdk', 'iphoneos', '--show-sdk-path'], text=True).strip()
|
|
|
|
|
|
class TestStaticFrameworkObfuscation(unittest.TestCase):
|
|
def test_convert_static_framework_bitcode_repacks_arm64_macho_and_audits(self):
|
|
require_host_opt()
|
|
swiftc = detect_stock_tool('swiftc')
|
|
clang = detect_stock_tool('clang')
|
|
sdk_ios = iphoneos_sdk_path()
|
|
|
|
with tempfile.TemporaryDirectory(prefix='ig-m7-staticfw-') as td:
|
|
root = Path(td)
|
|
repo = root / 'repo'
|
|
trial_dir = repo / 'Vendor/RDPDFReaderView/Sources'
|
|
trial_dir.mkdir(parents=True)
|
|
trial_swift = trial_dir / 'RDPDFReaderTrial.swift'
|
|
trial_swift.write_text(
|
|
'''import Foundation
|
|
|
|
public struct RDPDFReaderTrialPolicy {
|
|
public var readablePageCount: Int
|
|
public init(readablePageCount: Int) {
|
|
self.readablePageCount = readablePageCount
|
|
}
|
|
}
|
|
|
|
@inline(never)
|
|
public func evaluateTrialQuota(_ page: Int, _ policy: RDPDFReaderTrialPolicy) -> Int {
|
|
let secretTag = "RD_PDF_TRIAL_WALL_SECRET_GATE_12345"
|
|
var res = page ^ 0x1357
|
|
if policy.readablePageCount > 0 && page >= policy.readablePageCount {
|
|
res = (res + secretTag.count) & 0x7fff
|
|
} else if page < 0 {
|
|
res = 0x2468
|
|
}
|
|
if res > 100 {
|
|
return res ^ 0x55
|
|
}
|
|
return res + 17
|
|
}
|
|
''',
|
|
encoding='utf-8',
|
|
)
|
|
|
|
dummy_c = root / 'RDPDFReaderView-dummy.c'
|
|
dummy_c.write_text('int rd_pdf_dummy_marker(void) { return 42; }\n', encoding='utf-8')
|
|
|
|
obj_dir = root / 'objs'
|
|
obj_dir.mkdir()
|
|
bc_o = obj_dir / 'RDPDFReaderTrial.o'
|
|
dummy_o = obj_dir / 'RDPDFReaderView-dummy.o'
|
|
|
|
subprocess.run([
|
|
swiftc,
|
|
'-target', 'arm64-apple-ios15.0',
|
|
'-sdk', sdk_ios,
|
|
'-O', '-lto=llvm-thin',
|
|
'-g', '-file-prefix-map', f'{repo}=.',
|
|
'-module-name', 'RDPDFReaderView',
|
|
'-parse-as-library', '-c',
|
|
str(trial_swift), '-o', str(bc_o),
|
|
], check=True)
|
|
subprocess.run([
|
|
clang,
|
|
'-target', 'arm64-apple-ios15.0',
|
|
'-isysroot', sdk_ios,
|
|
'-O2', '-c', str(dummy_c), '-o', str(dummy_o),
|
|
], check=True)
|
|
|
|
self.assertTrue(ig_ld_wrapper.is_bitcode_wrapper(bc_o), 'Expected Swift -lto=llvm-thin to emit bitcode wrapper')
|
|
|
|
fw_dir = root / 'frameworks/RDPDFReaderView.framework'
|
|
fw_dir.mkdir(parents=True)
|
|
arch_bin = fw_dir / 'RDPDFReaderView'
|
|
subprocess.run(['xcrun', 'libtool', '-static', '-o', str(arch_bin), str(bc_o), str(dummy_o)], check=True)
|
|
|
|
policy_path = root / 'policy.json'
|
|
policy = {
|
|
'exempt_path_substrings': ['/Pods/'],
|
|
'exempt_function_prefixes': [],
|
|
'obfuscate_static_frameworks': ['RDPDFReaderView'],
|
|
'string_encrypt_targets': [{'id': 'pdf_str', 'path_contains': 'Vendor/RDPDFReaderView/Sources/RDPDFReaderTrial.swift'}],
|
|
'string_encrypt_swift_targets': [{'id': 'pdf_swstr', 'path_contains': 'Vendor/RDPDFReaderView/Sources/RDPDFReaderTrial.swift'}],
|
|
'flatten_targets': [{'id': 'pdf_fla', 'path_contains': 'Vendor/RDPDFReaderView/Sources/RDPDFReaderTrial.swift'}],
|
|
'opaque_const_targets': [{'id': 'pdf_const', 'path_contains': 'Vendor/RDPDFReaderView/Sources/RDPDFReaderTrial.swift'}],
|
|
'arithmetic_targets': [{'id': 'pdf_arith', 'path_contains': 'Vendor/RDPDFReaderView/Sources/RDPDFReaderTrial.swift'}],
|
|
'indirect_branch_targets': [{'id': 'pdf_ibr', 'path_contains': 'Vendor/RDPDFReaderView/Sources/RDPDFReaderTrial.swift'}],
|
|
'required_targets': [
|
|
{
|
|
'id': 'vendor_pdf_trial',
|
|
'path_contains': 'Vendor/RDPDFReaderView/Sources/RDPDFReaderTrial.swift',
|
|
'min_flattened': 1,
|
|
'min_strings_encrypted': 0,
|
|
}
|
|
],
|
|
}
|
|
policy_path.write_text(json.dumps(policy), encoding='utf-8')
|
|
|
|
out_bin = root / 'build/readoor31OtherPay.app/readoor31OtherPay'
|
|
out_bin.parent.mkdir(parents=True)
|
|
linkmap = root / 'build/LinkMap.txt'
|
|
ld_args = [
|
|
'-arch', 'arm64',
|
|
'-platform_version', 'ios', '15.0', '18.0',
|
|
'-syslibroot', sdk_ios,
|
|
'-F', str(fw_dir.parent),
|
|
'-framework', 'RDPDFReaderView',
|
|
'-flto=thin',
|
|
'-object_path_lto', str(root / 'build/lto.o'),
|
|
'-o', str(out_bin),
|
|
]
|
|
ctl = {
|
|
'mode': 'full',
|
|
'filter': 'all',
|
|
'route2': 'on',
|
|
'all_off': '0',
|
|
'seed': 'm7_fw_seed_1',
|
|
'stamp': 'test_stamp',
|
|
'policy_profile': 'default',
|
|
}
|
|
conv_report = {
|
|
'expected_swift_count': 0,
|
|
'converted_swift_count': 0,
|
|
'skipped_swift_count': 0,
|
|
'skipped_swift': [],
|
|
'swift_audits': [],
|
|
'objc_audits': [],
|
|
'replaced_entries': [],
|
|
'static_frameworks_obfuscated': [],
|
|
}
|
|
|
|
new_args, updated_report = ig_ld_wrapper.convert_static_framework_bitcode(
|
|
ld_args, out_bin, str(linkmap), ctl, str(policy_path), policy, repo, conv_report
|
|
)
|
|
if not ig_ld_wrapper.args_contain_remaining_bitcode(new_args, updated_report['replaced_entries']):
|
|
new_args = ig_ld_wrapper.strip_lto_link_flags(new_args)
|
|
|
|
self.assertNotIn('-framework', new_args)
|
|
self.assertNotIn('-flto=thin', new_args)
|
|
self.assertNotIn('-object_path_lto', new_args)
|
|
|
|
fw_recs = updated_report['static_frameworks_obfuscated']
|
|
self.assertEqual(len(fw_recs), 1)
|
|
self.assertEqual(fw_recs[0]['framework'], 'RDPDFReaderView')
|
|
self.assertEqual(fw_recs[0]['bitcode_members'], 1)
|
|
self.assertEqual(fw_recs[0]['passthrough_members'], 1)
|
|
|
|
obf_archive = Path(fw_recs[0]['output_archive'])
|
|
self.assertTrue(obf_archive.is_file())
|
|
self.assertIn(str(obf_archive), new_args)
|
|
|
|
# Inspect repacked archive members: zero bitcode remains, all Mach-O arm64 objects.
|
|
repacked_members = ig_ld_wrapper._parse_bsd_ar_members(obf_archive)
|
|
self.assertIsNotNone(repacked_members)
|
|
self.assertEqual(len(repacked_members), 2)
|
|
for m_name, payload in repacked_members:
|
|
self.assertNotIn(payload[:4], ig_ld_wrapper.BITCODE_MAGICS, f'Member {m_name} still bitcode!')
|
|
magic = struct.unpack_from('<I', payload, 0)[0]
|
|
self.assertEqual(magic, 0xFEEDFACF, f'Member {m_name} is not 64-bit Mach-O MH_OBJECT')
|
|
|
|
audit_summary = ig_ld_wrapper.evaluate_and_write_obf_audit(
|
|
out_bin, str(linkmap), ctl, str(policy_path), policy, updated_report
|
|
)
|
|
self.assertGreaterEqual(audit_summary['total_flattened'], 1)
|
|
self.assertGreaterEqual(audit_summary['total_opaque_constants'], 1)
|
|
self.assertGreaterEqual(audit_summary['total_arithmetic_rewrites'], 1)
|
|
self.assertEqual(len(audit_summary['required_targets']), 1)
|
|
self.assertEqual(audit_summary['required_targets'][0]['id'], 'vendor_pdf_trial')
|
|
self.assertGreaterEqual(audit_summary['required_targets'][0]['flattened_hits'], 1)
|
|
self.assertTrue(
|
|
any('Vendor/RDPDFReaderView/Sources/RDPDFReaderTrial.swift' in (a.get('source_path') or '')
|
|
for a in updated_report['swift_audits'])
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|