一体化macOS二进制分析:Mach-O解析、ARM64反汇编、代码签名和调试。
一个原生的 macOS 二进制分析工具,提供 Mach-O 解析、ARM64 反汇编和进程调试功能。完全使用 Swift 构建,零外部依赖。
| 功能 | 描述 |
|---|---|
| Mach-O 解析 | 头部、段、节、符号、动态库、字符串 |
| 代码签名 | Entitlements、CDHash、签名信息、Team ID |
| ARM64 反汇编 | 完整指令解码器,带 PAC 注释 |
| 进程调试 | 附加、断点、内存、寄存器 |
| Swift 库 | 可嵌入到您自己的项目中 |
| JSON 输出 | 适合脚本处理的输出格式 |
# 构建
swift build
# 解析一个二进制文件
swift run machscope parse /bin/ls
# 解析一个 macOS 应用
swift run machscope parse /Applications/Calculator.app/Contents/MacOS/Calculator
# 查看 Entitlements
swift run machscope parse /Applications/Safari.app/Contents/MacOS/Safari --entitlements
# JSON 输出
swift run machscope parse /bin/ls --json
brew install sadopc/tap/machscope
git clone https://github.com/sadopc/machscope.git
cd MachScope
swift build -c release
sudo cp .build/release/machscope /usr/local/bin/
分析 Mach-O 二进制结构:
# 基本分析
machscope parse /bin/ls
# 完整分析
machscope parse /bin/ls --all
# 特定节
machscope parse /path/to/binary --symbols
machscope parse /path/to/binary --dylibs
machscope parse /path/to/binary --strings
machscope parse /path/to/binary --signatures
machscope parse /path/to/binary --entitlements
# 用于脚本的 JSON 输出
machscope parse /bin/ls --json --all > analysis.json
反汇编 ARM64 代码:
# 列出函数
machscope disasm /bin/ls --list-functions
# 从指定地址反汇编
machscope disasm /bin/ls --address 0x100003f40 --length 50
# 显示指令字节
machscope disasm /bin/ls --show-bytes
查看哪些功能可用:
machscope check-permissions
输出:
Feature Status Notes
------------------------------------------------------------
Static Analysis ✓ Ready No special permissions needed
Disassembly ✓ Ready No special permissions needed
Debugger ✗ Denied Missing debugger entitlement
附加到正在运行的进程(需要签名):
# 首先,使用调试器 entitlement 进行签名
codesign --force --sign - --entitlements Resources/MachScope.entitlements .build/debug/machscope
# 在系统设置 > 隐私与安全性中启用开发者工具
# 附加到进程
machscope debug <pid>
将 MachScope 添加到您的 Package.swift:
dependencies: [
.package(url: "https://github.com/sadopc/machscope.git", from: "1.0.0")
]
然后在代码中使用:
import MachOKit
import Disassembler
// 解析一个二进制文件
let binary = try MachOBinary(path: "/bin/ls")
print("CPU: \(binary.header.cpuType)")
print("Segments: \(binary.segments.count)")
// 检查 Entitlements
if let signature = try binary.parseCodeSignature(),
let entitlements = signature.entitlements {
for key in entitlements.keys {
print("\(key): \(entitlements[key] ?? "nil")")
}
}
// 反汇编
let disasm = ARM64Disassembler(binary: binary)
let result = try disasm.disassembleFunction("_main", from: binary)
for instruction in result.instructions {
print(disasm.format(instruction))
}
MachScope/
├── Sources/
│ ├── MachOKit/ # 核心 Mach-O 解析库
│ ├── Disassembler/ # ARM64 指令解码器
│ ├── DebuggerCore/ # 进程调试
│ └── MachScope/ # 命令行应用
├── Tests/ # 测试套件(319+ 个测试)
├── Resources/ # 用于代码签名的 Entitlements
└── docs/ # 文档
MachScope 的主要优势:原生 Swift 库,可嵌入到您自己的工具中。
MIT 许可证 — 详见 LICENSE。
欢迎贡献!请先阅读 贡献指南。
# 提交前运行测试
swift test
# 格式化代码
xcrun swift-format -i -r Sources/ Tests/
用 ❤️ 在 Swift 中构建
| 工具 | 语言 | 库? | ARM64 PAC | 调试器 |
|---|
| MachScope | Swift | ✅ 是 | ✅ 是 | ✅ 是 |
| otool | C | ❌ 否 | ❌ 否 | ❌ 否 |
| objdump | C | ❌ 否 | ❌ 否 | ❌ 否 |
| jtool2 | C | ❌ 否 | ✅ 是 | ❌ 否 |
| Hopper | — | ❌ 否 | ✅ 是 | ❌ 否 |