- 构建期生成随包签名的 IGMethods.bin(仅主程序),运行时按镜像 UUID、 入口偏移与函数边界内指令摘要校验;导出审计复核清单与最终镜像一致 - 系统函数、调度与解包保护的代码前缀按 LC_FUNCTION_STARTS 限定在函数边界内 - 边界内识别 B / LDR+BR / ADRP+ADD+BR / MOVK+BR 蹦床并确认目标归属,避免系统尾调用误报 - 登记前 IMP 已落入匿名内存判为改写;清单读取改走 IG_dispatch_open - 函数边界长度按入口缓存;镜像未重新链接时复用基线;Debug 附加调试器时不按入口摘要判定改写 - 补充宿主测试;删除已过时的 README 与 docs 历史文档,保留 THIRD_PARTY_NOTICES
112 lines
5.1 KiB
Python
112 lines
5.1 KiB
Python
"""用真实的 arm64 Mach-O 对照构建器输出和 Objective-C 运行时。"""
|
|
import hashlib
|
|
from pathlib import Path
|
|
import platform
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'tools/xcode_archive'))
|
|
from objc_method_baseline import generate, image_uuid, NAME, ROW
|
|
|
|
FIXTURE_SWIFT = (
|
|
'import Foundation\n'
|
|
'class SwiftProbe: NSObject {\n'
|
|
' @objc func swiftValue() {}\n'
|
|
' class Nested: NSObject { @objc func nestedValue() {} }\n'
|
|
'}\n'
|
|
'private class PrivateProbe: NSObject { @objc func privateValue() {} }\n'
|
|
'@_cdecl("ig_load_swift_fixture") public func loadFixture() { _ = SwiftProbe.self }\n'
|
|
'@_cdecl("ig_swift_nested_name") public func nestedName() -> UnsafePointer<CChar> { class_getName(SwiftProbe.Nested.self) }\n'
|
|
'@_cdecl("ig_swift_private_name") public func privateName() -> UnsafePointer<CChar> { class_getName(PrivateProbe.self) }\n'
|
|
)
|
|
FRAMEWORK_OBJC = (
|
|
'#import <Foundation/Foundation.h>\n'
|
|
'@interface IGFrameworkOnly : NSObject\n- (int)value;\n@end\n'
|
|
'@implementation IGFrameworkOnly\n- (int)value { return 1; }\n@end\n'
|
|
)
|
|
|
|
|
|
@unittest.skipUnless(sys.platform == 'darwin' and platform.machine() == 'arm64', '需要 arm64 macOS')
|
|
class MethodBaselineTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.tmp = tempfile.TemporaryDirectory()
|
|
app = cls.app = Path(cls.tmp.name)
|
|
frameworks = app / 'Frameworks'
|
|
frameworks.mkdir()
|
|
# Framework 中的方法不进入基线。
|
|
other = app / 'Other.m'
|
|
other.write_text(FRAMEWORK_OBJC)
|
|
subprocess.run(['xcrun', 'clang', '-dynamiclib', str(other), '-framework', 'Foundation',
|
|
'-o', str(frameworks / 'Other.dylib')], check=True)
|
|
# 受监控的 ObjC 与 Swift 类都链接进主程序,与真实应用一致。
|
|
defender = ROOT / 'defender'
|
|
sources = [ROOT / 'tests/fixtures/objc_manifest_host.m', defender / 'anti_hook/IGObjCBaseline.c',
|
|
defender / 'anti_hook/IGAntiHook.c', defender / 'resolve/IGResolve.c',
|
|
defender / 'dispatch/IGDispatch.c']
|
|
objects = []
|
|
for i, source in enumerate(sources):
|
|
obj = app / f'{i}.o'
|
|
# DEBUG=1 覆盖 Debug 构建中的调试器分支。
|
|
subprocess.run(['xcrun', 'clang', '-O2', '-DDEBUG=1', '-I', str(defender), '-I', str(defender / 'include'),
|
|
'-c', str(source), '-o', str(obj)], check=True)
|
|
objects.append(str(obj))
|
|
swift = app / 'Fixture.swift'
|
|
swift.write_text(FIXTURE_SWIFT)
|
|
cls.exe = app / 'probe'
|
|
subprocess.run(['xcrun', 'swiftc', '-parse-as-library', '-module-name', 'Fixture', str(swift), *objects,
|
|
'-framework', 'Foundation', '-o', str(cls.exe)], check=True)
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.tmp.cleanup()
|
|
|
|
def run_mode(self, mode):
|
|
subprocess.run([str(self.exe), mode], check=True)
|
|
|
|
def test_signed_resource_contract(self):
|
|
manifest = self.app / NAME
|
|
generate(self.app, self.exe, manifest)
|
|
blob = manifest.read_bytes()
|
|
self.assertEqual(blob[8:24], image_uuid(self.exe))
|
|
rows = {row[0]: row for row in ROW.iter_unpack(blob[24:])}
|
|
self.assertNotIn(hashlib.sha256(b'-IGFrameworkOnly value').digest(), rows)
|
|
tail = rows[hashlib.sha256(b'-IGManifestParent value').digest()]
|
|
self.assertEqual(tail[3], 4)
|
|
entry = self.exe.read_bytes()[tail[2]:tail[2] + 4]
|
|
self.assertEqual(int.from_bytes(entry, 'little') >> 26, 0b000101)
|
|
self.assertEqual(tail[4], hashlib.sha256(entry).digest())
|
|
for mode in ('clean', 'before', 'before-block', 'before-anon', 'after'):
|
|
self.run_mode(mode)
|
|
# 只更改测试基线中的指令摘要,验证地址不变时代码差异也可被发现。
|
|
altered = bytearray(blob)
|
|
altered[blob.index(tail[0], 24) + 60] ^= 1
|
|
manifest.write_bytes(altered)
|
|
self.run_mode('different-bytes')
|
|
# 调试器附加时(Debug),软断点导致的摘要差异不判定为改写。
|
|
self.run_mode('different-bytes-traced')
|
|
manifest.write_bytes(blob[:8] + bytes(16) + blob[24:])
|
|
self.run_mode('unavailable')
|
|
manifest.unlink()
|
|
self.run_mode('unavailable')
|
|
|
|
def test_phase_reuses_unchanged_baseline(self):
|
|
import ig_guard_frameworks_phase as phase
|
|
audit = self.app / 'audit'
|
|
manifest = self.app / NAME
|
|
manifest.unlink(missing_ok=True)
|
|
phase.write_method_baseline(self.app, self.exe, audit)
|
|
original = manifest.read_bytes()
|
|
stamp = audit / 'objc-method-baseline.stamp'
|
|
self.assertTrue(stamp.is_file())
|
|
mtime = manifest.stat().st_mtime_ns
|
|
phase.write_method_baseline(self.app, self.exe, audit)
|
|
self.assertEqual(manifest.stat().st_mtime_ns, mtime)
|
|
# 资源被改动时不复用,重新生成。
|
|
manifest.write_bytes(b'tampered')
|
|
phase.write_method_baseline(self.app, self.exe, audit)
|
|
self.assertEqual(manifest.read_bytes(), original)
|