Raunaksplanet/Thick-Client-Pentesting-On-MacOS
GitHub: Raunaksplanet/Thick-Client-Pentesting-On-MacOS
一份整合CyberArk Labs研究和社区经验的macOS厚客户端渗透测试综合指南,填补了该领域系统化参考资料的空白。
Stars: 0 | Forks: 0
# macOS 应用程序渗透测试 — 综合指南
## 目录
- [概述](#overview)
- [涵盖范围](#coverage)
- [阶段](#phases)
- [App Bundle 结构](#app-bundle-structure)
- [安全控制](#security-controls)
- [静态分析](#static-analysis)
- [代码签名与 Entitlements](#code-signing--entitlements)
- [网络测试](#network-testing)
- [动态分析](#dynamic-analysis)
- [Dylib 劫持](#dylib-hijacking)
- [代码注入](#code-injection)
- [XPC 攻击](#xpc-attacks)
- [快速命令参考](#quick-command-reference)
- [工具](#tools)
- [来源](#sources)
## 概述
与 Windows/Linux 相比,macOS 厚客户端渗透测试缺乏整合的资源。本指南填补了这一空白 —— 涵盖了从初始侦察到漏洞利用的完整攻击链。
**范围:** 原生 `.app` bundle、特权辅助工具、XPC 服务、Mach-O 二进制文件。
**涵盖语言:** Swift、Objective-C、C/C++。
**测试环境:** macOS 10.15+ (Catalina 及更高版本)。
## 涵盖范围
| 区域 | 主题 |
|---|---|
| **结构** | App bundle 布局、Info.plist、Frameworks、XPCServices、LaunchServices |
| **安全控制** | SIP、Gatekeeper、Notarization、App Sandbox、TCC、AMFI、Quarantine |
| **静态分析** | 代码签名、Hardened Runtime 标志、Entitlements、Dylib 枚举、符号表 |
| **文件分析** | 日志、缓存、SQLite 数据库、plist/JSON 配置、字符串提取 |
| **网络** | Burp 代理设置 (macOS 特定)、Wireshark、SIP 交互 |
| **动态分析** | Frida hook、lldb 内存转储、DTrace 跟踪、进程/文件监控 |
| **漏洞利用** | Dylib 劫持、DYLD 注入、Mach Task Port 注入、XPC 攻击 |
## 阶段
```
Phase 1 — Recon Bundle structure, Info.plist, entitlements, SIP status, TCC perms
Phase 2 — Static Mach-O analysis, code signing flags, linked dylibs, symbols, strings
Phase 3 — Dynamic Frida hooks, lldb debug/dump, DTrace, process/file/network monitoring
Phase 4 — Exploitation Dylib hijacking, DYLD injection, task port injection, XPC abuse
```
## App Bundle 结构
```
MyApp.app/
└── Contents/
├── Info.plist ← Bundle ID, entitlements config, privacy keys
├── MacOS/ ← Main Mach-O executable
├── Resources/ ← Images, strings, UI files
├── Frameworks/ ← Embedded frameworks & dylibs ← hijack surface
├── Plugins/ ← App extensions
└── Library/
├── LaunchServices/ ← Privileged helper tools (run as root) ← priv-esc
├── SystemExtensions/ ← Network/endpoint extensions
└── XPCServices/ ← XPC service bundles ← IPC attack surface
```
**优先检查的关键文件:**
| 文件 | 检查内容 |
|---|---|
| `Info.plist` | Bundle ID、NSApp* 隐私键、URL Schemes |
| `MacOS/` | 架构、签名、Hardened Runtime 标志 |
| `Frameworks/*.dylib` | 存在 CVE 的第三方库;签名不匹配 |
| `Library/LaunchServices/` | 特权辅助工具 — 主要的权限提升攻击面 |
| `Library/XPCServices/` | XPC 服务 — 检查授权策略 |
## 安全控制
| 控制机制 | 功能 | 渗透测试影响 |
|---|---|---|
| **SIP** | 即使是 root 用户也限制对 `/System`、`/bin`、`/sbin`、`/usr` 的访问 | 必须禁用 (通过恢复模式) 或绕过才能进行完整测试 |
| **Gatekeeper** | 阻止非 Apple 签名/未公证的 App | 禁用或使用有效证书对测试二进制文件进行签名 |
| **App Sandbox** | 将资源访问限制为声明的 Entitlements 范围内 | 检查是否存在过度权限;通过 XPC 辅助程序逃逸沙箱 |
| **TCC** | 控制摄像头、麦克风、定位、完全磁盘访问权限 | 寻找通过特权辅助程序绕过 TCC 的途径 |
| **AMFI** | 在加载 dylib 时验证代码签名 | 阻止未签名的注入,除非存在 Runtime 异常 |
| **Quarantine** | 将下载的文件标记以供 Gatekeeper 检查 | 使用 `xattr -d com.apple.quarantine ` 移除 |
| **Hardened Runtime** | 防止代码注入、DLL 劫持、内存篡改 | 检查标志;在 Entitlements 中寻找 Runtime 异常 |
### SIP 命令
```
csrutil status # check status
csrutil disable # disable (Recovery Mode → Terminal)
csrutil enable --without debug # keep SIP but allow debugging
csrutil enable # re-enable after testing
```
## 静态分析
### 代码签名
```
# 完整签名详情
codesign -dvv /Applications/Target.app
# 验证 bundle 完整性
codesign --verify --deep --verbose /Applications/Target.app
# Gatekeeper 评估
spctl --assess --verbose /Applications/Target.app
# 获取 signing authority + TeamIdentifier
codesign -v -d /Applications/Target.app 2>&1 | grep -E "Authority|TeamIdentifier"
# 重签名 (adhoc,用于测试)
codesign -s - --force /path/to/binary
```
### Hardened Runtime 标志
标志显示在 `codesign -dvv` 输出的 `CodeDirectory` 行中:
```
0x0 → No hardened runtime, no restrictions — prime target
0x0002 → Adhoc signed
0x10000 → Hardened runtime active
0x10002 → Hardened runtime + adhoc (0x10000 | 0x0002)
0x2000 → Library validation required
0x0800 → Restrict dyld loading
```
## 代码签名与 Entitlements
```
# 导出 entitlements
codesign -d --entitlements :- /path/to/binary
# 使用 jtool2
jtool2 --ent /path/to/binary
```
**高价值 Entitlements:**
| Entitlements | 影响 |
|---|---|
| `cs.allow-dyld-environment-variables` | 允许 `DYLD_INSERT_LIBRARIES` → 代码注入 |
| `cs.disable-library-validation` | 无需签名检查即可加载任意 dylib |
| `security.get-task-allow` | 其他进程 (调试器) 可以附加 |
| `cs.debugger` | 可以通过 Task Port 附加到其他进程 |
| `cs.allow-unsigned-executable-memory` | 在可执行内存中存在未签名代码 |
| `files.downloads.read-write` | 对 `~/Downloads` 的完全读写权限 |
### 库枚举
```
otool -L /path/to/binary # all linked dylibs
otool -l /path/to/binary # all load commands (detailed)
nm /path/to/binary # symbol table — _objc = ObjC, _swift = Swift
strings /path/to/binary | grep -i "password\|token\|key\|secret"
```
## 网络测试
### Burp 代理设置 (macOS)
1. 设置 Burp 监听器:`Proxy → Options → 127.0.0.1:8080`
2. 导出 Burp CA 证书为 `.der` 格式
3. 在 Keychain 中安装证书 → 设置为 **Always Trust**
4. `System Preferences → Network → Advanced → Proxies` → HTTP: `127.0.0.1:8080`
5. 验证流量是否在 Burp 中显示
## 动态分析
### Frida
```
pip3 install frida-tools
# Hook open() — 拦截文件访问
frida -n "TargetApp" -l hook.js
# Spawn 并注入
frida -f /Applications/Target.app/Contents/MacOS/Target -l hook.js
```
```
// hook.js — intercept open() calls
Interceptor.attach(Module.findExportByName(null, "open"), {
onEnter: function(args) {
console.log("[open] " + Memory.readUtf8String(args[0]));
}
});
```
### lldb — 内存转储
```
lldb --attach-pid
(lldb) process save-core /tmp/target.dump
(lldb) memory read 0x1234abcd --count 64 --format x
(lldb) disassemble --name functionName
(lldb) image list
(lldb) process status
# Dump 之后
strings /tmp/target.dump | grep -i "password\|Bearer\|token"
```
### DTrace
```
# Trace malloc 调用
sudo dtrace -n 'pid$target::malloc:entry { trace(arg0); }' -p
```
### 敏感文件位置
| 路径 | 风险 |
|---|---|
| `~/Library/Logs//` | 调试日志 — token、会话 ID、堆栈跟踪 |
| `~/Library/Caches//` | 带有缓存响应的 SQLite 数据库 |
| `~/Library/Application Support//` | 配置、本地数据库、凭证 |
| `~/Library/Preferences/.plist` | 明文存储的 Auth Token、API 密钥 |
| `/tmp/` | TOCTOU、不安全的临时文件创建 |
## Dylib 劫持
类似于 Windows 的 DLL 劫持 —— 攻击者将恶意 dylib 放置在应用程序期望合法 dylib 所在的位置。
### 寻找候选目标
```
# Weak dylibs (可选 — 缺失 = 可安全丢弃恶意 dylib)
otool -l /path/to/binary | grep LC_LOAD_WEAK_DYLIB -A5
# 从 @rpath 加载的 Dylibs
otool -l /path/to/binary | grep LC_LOAD_DYLIB -A5 | grep rpath
# 所有 LC_RPATH 条目 (检查可写路径)
otool -l /path/to/binary | grep LC_RPATH -A3
```
或使用 Patrick Wardle 的 [Dylib Hijack Scanner](https://objective-see.org/products/dhs.html)。
### 利用条件
- App 没有 Hardened Runtime (`flags = 0x0`)
- App 具有 `cs.disable-library-validation` Entitlement
- `app/Contents/` 内的文件签名无效或缺失
- 弱 dylib 预期位于用户可写的路径中
- LC_RPATH 指向合法路径之前的可写目录
### 构建劫持器
```
// hijack.c
#include
#include
__attribute__((constructor))
static void customConstructor(int argc) {
syslog(LOG_ERR, "Dylib loaded!");
// payload here
}
```
```
# 使用 version compat + re-export symbols 进行编译以避免崩溃
gcc -dynamiclib \
-current_version 6.8.0 \
-compatibility_version 6.8.0 \
hijack.c -o custom.dylib \
-Wl,-reexport_library "/path/to/legitimate.dylib"
# 设置 install name 以匹配 app 预期
install_name_tool -id @rpath/custom.dylib custom.dylib
# 放入主 rpath 搜索路径
cp custom.dylib /Applications/Target.app/Contents/Frameworks/
```
## 代码注入
### 1. DYLD_INSERT_LIBRARIES (环境变量注入)
类似于 Linux 上的 `LD_PRELOAD`。仅适用于你启动的进程。
**要求** (当启用 Hardened Runtime 时):
- `com.apple.security.cs.allow-dyld-environment-variables = true`
- `com.apple.security.cs.disable-library-validation = true` (如果 dylib 不属于同一 Team ID)
```
gcc -dynamiclib inject.c -o inject.dylib
DYLD_INSERT_LIBRARIES=./inject.dylib /Applications/Target.app/Contents/MacOS/Target
```
### 2. Mach Task Port 注入
注入到**已经运行**的进程中。要求注入者具有 root 权限或 `com.apple.security.cs.debugger` Entitlement,并且目标进程具有 `com.apple.security.get-task-allow` Entitlement。
```
mach_port_t task;
kern_return_t kr = task_for_pid(mach_task_self(), target_pid, &task);
// use task port to read/write process memory
```
参考:[Jonathan Levin 的 inject.c](https://newosxbook.com/src.jl?tree=listings&file=inject.c) · [Scott Knight 的远程线程注入](https://gist.github.com/knightsc/45edfc4903a9d2fa9f5905f60b02ce5a)
## XPC 攻击
XPC 服务和特权辅助工具是 macOS 上主要的权限提升攻击面。
**关键位置:**
```
/Library/PrivilegedHelperTools/ ← runs as root
/Applications/Target.app/Contents/Library/XPCServices/
/Library/LaunchDaemons/ ← system-wide daemons
/Library/LaunchAgents/ ← per-user agents
```
**攻击向量:**
| 向量 | 描述 |
|---|---|
| 消息伪造 | 如果调用者身份未经验证,则伪造恶意 XPC 消息 |
| PID 重用 | PID 检查与服务使用之间的 TOCTOU |
| 缺失 Entitlement 验证 | 服务未验证调用者的 Entitlements/签名 |
| 授权绕过 | 配置不当的授权策略 |
| 特权辅助工具滥用 | Root 辅助工具暴露了危险的 XPC API |
**分析:**
```
find /Applications/Target.app -name "*.xpc"
codesign -d --entitlements :- Target.xpc/Contents/MacOS/Target
security authorizationdb read com.target.privilegedOp
```
延伸阅读:[theevilbit XPC 系列](https://theevilbit.github.io/tags/xpc/) · [OffSec: Teams macOS LPE](https://www.offsec.com/offsec/microsoft-teams-macos-local-privesc/)
## 快速命令参考
```
# === 侦察 ===
csrutil status
file /Applications/Target.app/Contents/MacOS/Target
lipo -info /Applications/Target.app/Contents/MacOS/Target
xattr -l /Applications/Target.app # check quarantine flag
# === 代码签名 ===
codesign -dvv /Applications/Target.app
codesign --verify --deep --verbose /Applications/Target.app
codesign -d --entitlements :- /path/to/binary
jtool2 --ent /path/to/binary
spctl --assess --verbose /Applications/Target.app
# === 二进制分析 ===
otool -L /path/to/binary # linked dylibs
otool -l /path/to/binary | grep LC_LOAD_WEAK_DYLIB -A5 # weak dylibs
otool -l /path/to/binary | grep LC_RPATH -A3 # rpath entries
nm /path/to/binary # symbols
strings /path/to/binary | grep -i "password\|token\|key" # sensitive strings
# === 动态分析 ===
ps aux | grep TargetApp
lldb --attach-pid
frida -n "TargetApp" -l hook.js
sudo dtrace -n 'pid$target::malloc:entry { trace(arg0); }' -p
# === DYLIB 劫持 ===
otool -l binary | grep LC_LOAD_WEAK_DYLIB -A5
otool -l binary | grep LC_LOAD_DYLIB -A5 | grep rpath
codesign --verify --deep --verbose /Applications/Target.app # find invalid sigs
# === 杂项 ===
xattr -d com.apple.quarantine # remove quarantine
install_name_tool -id @rpath/custom.dylib custom.dylib # set dylib install name
security authorizationdb read # check auth policy
```
## 工具
| 工具 | 类型 | 用途 |
|---|---|---|
| `codesign` | 静态 | 签名、Entitlements、验证 |
| `otool` | 静态 | 加载命令、dylib 依赖项、头文件 |
| `jtool2` | 静态 | 增强版 otool — Entitlements、codesign |
| `nm` | 静态 | 符号表、语言检测 |
| `strings` | 静态 | 从二进制文件中提取可读字符串 |
| `spctl` | 静态 | Gatekeeper 策略评估 |
| Ghidra | 静态 | 反汇编 + 反编译 (免费) |
| Hopper | 静态 | macOS 原生反汇编器,支持 ObjC |
| IDA Pro | 静态 | 业界标准的反编译器 |
| Dylib Hijack Scanner | 静态 | 自动化劫持候选查找器 |
| Frida | 动态 | Runtime API hooking + 插桩 |
| `lldb` | 动态 | 调试器 — 附加、断点、内存 |
| `dtrace` | 动态 | 内核跟踪、malloc/syscall 监控 |
| ProcessMonitor | 动态 | 进程创建/终止事件 |
| FileMonitor | 动态 | 文件系统事件监控 |
| TaskExplorer | 动态 | 运行中的任务、打开的文件、已加载的 dylib |
| Crescendo | 动态 | macOS 上的 GUI Procmon 替代品 |
| Wireshark | 网络 | 原始数据包捕获与协议分析 |
| BurpSuite | 网络 | HTTP/HTTPS 代理拦截 |
| Apple Instruments | 动态 | 内存泄漏、CPU、文件系统性能分析 |
## 来源
- [offs3cg33k — An Approach to macOS Thick Client Pen Test](https://offs3cg33k.medium.com/an-approach-to-mac-os-thick-client-pen-test-47da0acc7718) · Dhanishtha Awasthi
- [CyberArk — Deep Dive Part 1](https://www.cyberark.com/resources/threat-research-blog/a-deep-dive-into-penetration-testing-of-macos-applications-part-1) · Julia Minin & Daniel Rabinovich
- [CyberArk — Deep Dive Part 2](https://www.cyberark.com/resources/threat-research-blog/a-deep-dive-into-penetration-testing-of-macos-applications-part-2) · Julia Minin & Daniel Rabinovich
- [CyberArk — Deep Dive Part 3](https://www.cyberark.com/resources/threat-research-blog/a-deep-dive-into-penetration-testing-of-macos-applications-part-3) · Julia Minin & Daniel Rabinovich
- [Apple 开发者文档](https://developer.apple.com/documentation/)
- [HackTricks — macOS Hardening](https://book.hacktricks.xyz/macos-hardening/)
- [theevilbit — XPC Attack Series](https://theevilbit.github.io/tags/xpc/)
- [Patrick Wardle — Dylib Hijacking on OS X](https://www.virusbulletin.com/virusbulletin/2015/03/dylib-hijacking-os-x)
- [Objective-See 工具](https://objective-see.org/tools.html)
标签:C/C++, Cutter, CVE监控, CyberArk Labs, Docker支持, Dylib劫持, Frida, lldb, Mach-O, macOS安全, Objective-C, SIP, Swift, TCC, Thick Client Pentesting, UML, XPC攻击, 事务性I/O, 云安全监控, 云资产清单, 代码签名, 反取证, 后端开发, 安全评估, 搜索语句(dork), 数据包嗅探, 无线安全, 沙箱逃逸, 白皮书, 红队技术, 网络安全, 网络安全审计, 胖客户端测试, 自定义密码套件, 逆向工具, 逆向工程, 隐私保护, 静态分析