- 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
249 lines
10 KiB
Python
249 lines
10 KiB
Python
"""Unit tests for M8: IGFunctionOutline (CodeExtractor -> ig_sub_*) and IGOpaqueFieldAccess."""
|
|
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'
|
|
sys.path.insert(0, str(TOOLS_LLVM_OBF))
|
|
|
|
from ig_toolchain_resolver import detect_stock_tool, resolve_opt # 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 macos_sdk_path() -> str:
|
|
return subprocess.check_output(['xcrun', '--sdk', 'macosx', '--show-sdk-path'], text=True).strip()
|
|
|
|
|
|
def iphoneos_sdk_path() -> str:
|
|
return subprocess.check_output(['xcrun', '--sdk', 'iphoneos', '--show-sdk-path'], text=True).strip()
|
|
|
|
|
|
class TestOpaqueFieldAndOutlinePass(unittest.TestCase):
|
|
def test_c_outline_routed_via_icall_field_masking_and_runtime_exact(self):
|
|
opt_path = require_host_opt()
|
|
clang = detect_stock_tool('clang')
|
|
sdk_host = macos_sdk_path()
|
|
sdk_ios = iphoneos_sdk_path()
|
|
|
|
with tempfile.TemporaryDirectory(prefix='ig-m8-c-') as td:
|
|
p = Path(td)
|
|
policy_path = p / 'policy.json'
|
|
policy_path.write_text(json.dumps({
|
|
'exempt_path_substrings': [],
|
|
'exempt_function_prefixes': [],
|
|
'function_outline_max_per_fn': 2,
|
|
'function_outline_min_insts': 4,
|
|
'function_outline_targets': [{'path_contains': 'm8_field.c', 'function_contains': 'compute_box_digest'}],
|
|
'opaque_field_max_per_fn': 32,
|
|
'opaque_field_targets': [{'path_contains': 'm8_field.c', 'function_contains': 'compute_box_digest'}],
|
|
'indirect_call_targets': [{'path_contains': 'm8_field.c', 'function_contains': 'compute_box_digest'}],
|
|
'indirect_call_same_image_prefixes': ['ig_sub_'],
|
|
'flatten_targets': [{'path_contains': 'm8_field.c', 'function_contains': 'compute_box_digest'}],
|
|
}), encoding='utf-8')
|
|
|
|
src_c = p / 'm8_field.c'
|
|
src_c.write_text(
|
|
'''#include <stdint.h>
|
|
#include <stdio.h>
|
|
|
|
struct BoxState {
|
|
uint32_t header;
|
|
uint32_t salt;
|
|
uint64_t quota;
|
|
uint32_t flags;
|
|
uint32_t checksum;
|
|
};
|
|
|
|
uint32_t g_secret_multiplier = 0x9E3779B1u;
|
|
static uint64_t g_secret_offset = 0x123456789ABCDEF0ULL;
|
|
|
|
__attribute__((noinline))
|
|
uint64_t compute_box_digest(struct BoxState *st, uint32_t mode) {
|
|
uint64_t acc = st->quota ^ g_secret_offset;
|
|
if (mode > 0) {
|
|
uint32_t a = st->header * g_secret_multiplier;
|
|
uint32_t b = (st->salt ^ 0xA55AA55Au) + mode;
|
|
uint32_t c = (a ^ b) + st->flags;
|
|
uint64_t d = ((uint64_t)c << 17) ^ ((uint64_t)st->checksum * 33ULL);
|
|
acc += d + (uint64_t)a * 7ULL + (uint64_t)b;
|
|
} else {
|
|
acc ^= ((uint64_t)st->flags << 3) + st->salt + 19ULL;
|
|
}
|
|
if ((acc & 1ULL) != 0ULL) {
|
|
g_secret_multiplier ^= (uint32_t)(mode & 3u);
|
|
}
|
|
return acc ^ ((uint64_t)st->checksum + 0x55AAULL);
|
|
}
|
|
|
|
int main(void) {
|
|
struct BoxState s1 = {0x11223344u, 0x55667788u, 0xCAFEBABE00112233ULL, 7u, 0x99AABBCCu};
|
|
struct BoxState s2 = {0xDEADBEEFu, 0x10203040u, 0x0102030405060708ULL, 13u, 0x0A0B0C0Du};
|
|
uint64_t r1 = compute_box_digest(&s1, 0);
|
|
uint64_t r2 = compute_box_digest(&s1, 5);
|
|
uint64_t r3 = compute_box_digest(&s2, 12);
|
|
printf("%016llx:%016llx:%016llx\\n",
|
|
(unsigned long long)r1, (unsigned long long)r2, (unsigned long long)r3);
|
|
return 0;
|
|
}
|
|
''',
|
|
encoding='utf-8',
|
|
)
|
|
|
|
# Reference un-obfuscated output
|
|
ref_exe = p / 'ref_exe'
|
|
subprocess.run([clang, '-isysroot', sdk_host, '-O2', str(src_c), '-o', str(ref_exe)], check=True)
|
|
ref_out = subprocess.check_output([str(ref_exe)], text=True).strip()
|
|
|
|
# Obfuscated host build
|
|
bc_h = p / 'm8_h.bc'
|
|
obf_ll_h = p / 'm8_h.obf.ll'
|
|
aud_h = p / 'm8_h.audit.json'
|
|
obj_h = p / 'm8_h.o'
|
|
exe_h = p / 'm8_h_exe'
|
|
subprocess.run([clang, '-isysroot', sdk_host, '-O2', '-g', '-emit-llvm', '-c', str(src_c), '-o', str(bc_h)], check=True)
|
|
subprocess.run([
|
|
opt_path, '-S', '-passes=ig-obfuscate',
|
|
f'--ig-policy={policy_path}', '--ig-mode=full', '--ig-seed=m8_c_seed_1',
|
|
f'--ig-source-path={src_c}', f'--ig-audit-out={aud_h}',
|
|
str(bc_h), '-o', str(obf_ll_h),
|
|
], check=True)
|
|
ir_text = obf_ll_h.read_text(encoding='utf-8')
|
|
self.assertIn('define internal ', ir_text)
|
|
self.assertIn('ig_sub_', ir_text)
|
|
self.assertIn('__ig_fop_', ir_text)
|
|
self.assertIn('__ig_gba_', ir_text)
|
|
|
|
audit = json.loads(aud_h.read_text(encoding='utf-8'))
|
|
self.assertGreaterEqual(audit['functions_outlined_count'], 1)
|
|
self.assertGreaterEqual(audit['opaque_field_access_count'], 2)
|
|
self.assertGreaterEqual(audit['indirect_calls_count'], 1)
|
|
|
|
subprocess.run([
|
|
clang, '-isysroot', sdk_host, '-O2', '-Xclang', '-disable-llvm-passes',
|
|
'-fno-builtin', '-Wno-override-module', '-g', '-x', 'ir', '-c', str(obf_ll_h), '-o', str(obj_h),
|
|
], check=True)
|
|
subprocess.run([clang, '-isysroot', sdk_host, str(obj_h), '-o', str(exe_h)], check=True)
|
|
obf_out = subprocess.check_output([str(exe_h)], text=True).strip()
|
|
self.assertEqual(obf_out, ref_out)
|
|
|
|
# iOS arm64 bitcode lowering verification
|
|
bc_ios = p / 'm8_ios.bc'
|
|
obf_ios = p / 'm8_ios.obf.bc'
|
|
obj_ios = p / 'm8_ios.o'
|
|
aud_ios = p / 'm8_ios.audit.json'
|
|
subprocess.run([
|
|
clang, '-target', 'arm64-apple-ios15.0', '-isysroot', sdk_ios,
|
|
'-O2', '-g', '-emit-llvm', '-c', str(src_c), '-o', str(bc_ios),
|
|
], check=True)
|
|
subprocess.run([
|
|
opt_path, '-passes=ig-obfuscate',
|
|
f'--ig-policy={policy_path}', '--ig-mode=full', '--ig-seed=m8_ios_seed_1',
|
|
f'--ig-source-path={src_c}', f'--ig-audit-out={aud_ios}',
|
|
str(bc_ios), '-o', str(obf_ios),
|
|
], check=True)
|
|
subprocess.run([
|
|
clang, '-target', 'arm64-apple-ios15.0', '-isysroot', sdk_ios,
|
|
'-O2', '-Xclang', '-disable-llvm-passes', '-fno-builtin',
|
|
'-Wno-override-module', '-g', '-x', 'ir', '-c', str(obf_ios), '-o', str(obj_ios),
|
|
], check=True)
|
|
magic = struct.unpack_from('<I', obj_ios.read_bytes(), 0)[0]
|
|
self.assertEqual(magic, 0xFEEDFACF)
|
|
|
|
def test_swift_debug_info_outline_and_opaque_field_lowering(self):
|
|
opt_path = require_host_opt()
|
|
swiftc = detect_stock_tool('swiftc')
|
|
clang = detect_stock_tool('clang')
|
|
sdk_ios = iphoneos_sdk_path()
|
|
|
|
with tempfile.TemporaryDirectory(prefix='ig-m8-sw-') as td:
|
|
p = Path(td)
|
|
src_sw = p / 'm8_swift.swift'
|
|
src_sw.write_text(
|
|
'''import Foundation
|
|
|
|
public final class TrialRecord {
|
|
public var bookId: Int = 101
|
|
public var readablePages: Int = 5
|
|
public var bonusPages: Int = 2
|
|
public var watermarkSeed: UInt64 = 0x13572468ACE01122
|
|
public init() {}
|
|
}
|
|
|
|
private var gTrialCounter: Int = 9
|
|
|
|
@inline(never)
|
|
public func evaluateTrialRecord(_ rec: TrialRecord, _ page: Int) -> UInt64 {
|
|
var acc = rec.watermarkSeed ^ UInt64(gTrialCounter)
|
|
if page > rec.readablePages {
|
|
let a = UInt64(rec.bookId &* 131) ^ UInt64(rec.bonusPages &* 17)
|
|
let b = (acc &+ a) ^ 0x55AA33CC11223344
|
|
let c = b &* 3 &+ UInt64(page ^ 0x77)
|
|
acc = (c ^ a) &+ UInt64(rec.readablePages)
|
|
} else {
|
|
acc = acc &+ UInt64(rec.bookId + page)
|
|
}
|
|
if (acc & 1) == 0 {
|
|
gTrialCounter += 1
|
|
}
|
|
return acc
|
|
}
|
|
''',
|
|
encoding='utf-8',
|
|
)
|
|
policy_path = p / 'policy.json'
|
|
policy_path.write_text(json.dumps({
|
|
'exempt_path_substrings': [],
|
|
'exempt_function_prefixes': [],
|
|
'function_outline_max_per_fn': 2,
|
|
'function_outline_min_insts': 4,
|
|
'function_outline_targets': [{'path_contains': 'm8_swift.swift'}],
|
|
'opaque_field_max_per_fn': 32,
|
|
'opaque_field_targets': [{'path_contains': 'm8_swift.swift'}],
|
|
'indirect_call_targets': [{'path_contains': 'm8_swift.swift'}],
|
|
'indirect_call_same_image_prefixes': ['ig_sub_'],
|
|
'flatten_targets': [{'path_contains': 'm8_swift.swift'}],
|
|
}), encoding='utf-8')
|
|
|
|
bc_sw = p / 'm8_sw.bc'
|
|
obf_sw = p / 'm8_sw.obf.bc'
|
|
obj_sw = p / 'm8_sw.o'
|
|
aud_sw = p / 'm8_sw.audit.json'
|
|
subprocess.run([
|
|
swiftc, '-target', 'arm64-apple-ios15.0', '-sdk', sdk_ios,
|
|
'-O', '-lto=llvm-thin', '-g', '-module-name', 'M8Mod',
|
|
'-parse-as-library', '-c', str(src_sw), '-o', str(bc_sw),
|
|
], check=True)
|
|
subprocess.run([
|
|
opt_path, '-passes=default<O2>,ig-obfuscate',
|
|
f'--ig-policy={policy_path}', '--ig-mode=full', '--ig-seed=m8_sw_1',
|
|
f'--ig-source-path={src_sw}', '--ig-swift-module=M8Mod',
|
|
f'--ig-audit-out={aud_sw}', str(bc_sw), '-o', str(obf_sw),
|
|
], check=True)
|
|
subprocess.run([
|
|
clang, '-target', 'arm64-apple-ios15.0', '-isysroot', sdk_ios,
|
|
'-O2', '-Xclang', '-disable-llvm-passes', '-fno-builtin',
|
|
'-Wno-override-module', '-g', '-x', 'ir', '-c', str(obf_sw), '-o', str(obj_sw),
|
|
], check=True)
|
|
|
|
audit = json.loads(aud_sw.read_text(encoding='utf-8'))
|
|
self.assertGreaterEqual(audit['functions_outlined_count'], 1)
|
|
self.assertGreaterEqual(audit['opaque_field_access_count'], 1)
|
|
magic = struct.unpack_from('<I', obj_sw.read_bytes(), 0)[0]
|
|
self.assertEqual(magic, 0xFEEDFACF)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|