Hacktricks-skills macos-xpc-audit-token-spoofing
macOS XPC audit token spoofing vulnerability research and exploitation. Use this skill whenever the user mentions XPC, audit tokens, macOS privilege escalation, IPC vulnerabilities, Mach ports, xpc_connection_get_audit_token, or any macOS inter-process communication security issues. This skill helps identify, analyze, and understand XPC audit token race condition vulnerabilities including the two main variants: async audit token checks and reply forwarding attacks.
git clone https://github.com/abelrguezr/hacktricks-skills
skills/macos-hardening/macos-security-and-privilege-escalation/macos-proces-abuse/macos-ipc-inter-process-communication/macos-xpc/macos-xpc-connecting-process-check/macos-xpc_connection_get_audit_token-attack/SKILL.MDmacOS XPC Audit Token Spoofing
A skill for researching and understanding XPC audit token spoofing vulnerabilities on macOS. This covers the race condition class of bugs where an attacker can manipulate which process's audit token is used during authorization checks.
Core Concepts
Mach Messages
- Mach ports are single receiver, multiple sender communication channels
- Multiple processes can send messages to a mach port, but only one can read from it
- Ports are kernel-managed integers that processes use to communicate
XPC Connection Audit Tokens
- XPC's abstraction is one-to-one connection, but built on Mach's multiple sender technology
- An XPC connection's audit token is copied from the most recently received message
- Audit tokens are critical for security checks and authorization decisions
The Vulnerability
The core issue: XPC connections can have their audit token overwritten by messages from different senders under specific timing conditions.
Not vulnerable:
- Checks before accepting a connection (in
)-listener:shouldAcceptNewConnection: - Inside XPC event handlers (synchronous handling prevents overwrite)
Vulnerable patterns:
called outside the event handler (e.g., inxpc_connection_get_audit_token
)dispatch_async- Reply message parsing concurrent with event handler execution
Attack Variant 1: Async Audit Token Check
Requirements
- Two Mach services A and B that you can both connect to
- Service A has an authorization check for an action that B can pass but you cannot
- Service A obtains the audit token asynchronously (e.g., from
)dispatch_async
Exploitation Flow
- Connect to service A (e.g.,
)smd - Connect to service B (e.g.,
running as root)diagnosticd - Duplicate the send right of your A connection and use it as B's client port
- Messages from B are now delivered to A's connection
- Trigger B to send messages while you request the privileged action from A
- Race condition: B's audit token overwrites yours during A's authorization check
Real-World Example: smd + diagnosticd
- Service A:
(System Management Daemon)smd - Service B:
(runs as root, sends frequent monitoring messages)diagnosticd - Privileged action:
to install privileged helper tools as rootSMJobBless - Attack: Flood
with messages whilesmd
sends monitoring data, causingdiagnosticd
to usesmd
's root audit tokendiagnosticd
Attack Variant 2: Reply Forwarding
Requirements
- Two Mach services A and B
- Service A sends a message expecting a reply
- You can send a message to B that it will respond to
Exploitation Flow
- Wait for service A to send a message expecting a reply
- Hijack the reply port instead of replying directly to A
- Send the reply port to service B
- Dispatch your privileged action request to A
- Race condition: B's reply overwrites the audit token while your request is being parsed
Key Insight
: processed on designated queuexpc_connection_send_message_with_reply
: processed on current dispatch queuexpc_connection_send_message_with_reply_sync- Reply packets can be parsed concurrently with event handler execution
locks only the audit token, not the entire connection object_xpc_connection_set_creds
Discovery Techniques
Static Analysis
- Search for
callsxpc_connection_get_audit_token - Look for calls reachable from
/dispatch_async
blocksdispatch_after - Find authorization helpers mixing per-connection and per-message state:
(per-connection) +xpc_connection_get_pid
(per-connection)xpc_connection_get_audit_token
- In NSXPC code, verify checks use per-message audit tokens
Dynamic Analysis
Use the Frida hook script to detect vulnerable patterns:
# Run the Frida hook on a target process frida -U -f <process-name> -l scripts/xpc-audit-token-hook.js --no-pause
The script flags
xpc_connection_get_audit_token calls outside the event handler path.
Tooling
- gxpc: Open-source XPC sniffer for enumerating connections and observing traffic
gxpc -p <PID> --whitelist <service-name> - Frida: Dynamic instrumentation for hooking XPC functions
- IDA/Ghidra: Static analysis of Mach service binaries
- dyld interposing: Log call sites and stacks during black-box testing
Exploitation Primitives
Multi-Sender Setup (Variant 1)
Duplicate a SEND right to route B's messages through A's connection:
// Duplicate a SEND right you already hold mach_port_t dup; mach_port_insert_right(mach_task_self(), a_client, a_client, MACH_MSG_TYPE_MAKE_SEND); dup = a_client; // Use `dup` when crafting B's connect packet
Reply Hijack (Variant 2)
Capture the send-once right from A's pending request and use it for B's reply:
// Extract reply port from A's message mach_port_t reply_port = /* from A's message header */; // Send to B using A's reply port xpc_connection_send_message_with_reply(b_connection, message, reply_port);
The Fix
Apple's fix for CVE-2023-32405 in
smd:
- Replace
withxpc_connection_get_audit_tokenxpc_dictionary_get_audit_token - The dictionary function retrieves the audit token directly from the mach message
- This prevents race conditions since the token is tied to the specific message
Note:
xpc_dictionary_get_audit_token is not part of the public API.
Current Status (2024-2025)
- The issue persists in iOS 17 and macOS 14
- Apple has not implemented a broader fix (possibly due to legitimate audit token changes in
scenarios)setuid - Focus on authorization performed outside message event handlers or concurrently with reply processing
References
Quick Start
To begin researching XPC audit token vulnerabilities:
- Identify target services using
or by examining Mach portsgxpc - Run the Frida hook to detect vulnerable
callsxpc_connection_get_audit_token - Analyze the call stack to determine if the check is outside the event handler
- Map the authorization flow to understand what privileges are being checked
- Identify a second service (B) with higher privileges that can send messages to the target
- Craft the exploit using the appropriate variant based on the vulnerability pattern