macOS Sandbox Debug & Bypass
Learn & practice AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Learn & practice GCP Hacking:
HackTricks Training GCP Red Team Expert (GRTE)
Sandbox loading process

In the previous image it's possible to observe how the sandbox will be loaded when an application with the entitlement com.apple.security.app-sandbox is run.
The compiler will link /usr/lib/libSystem.B.dylib to the binary.
Then, libSystem.B will be calling other several functions until the xpc_pipe_routine sends the entitlements of the app to securityd. Securityd checks if the process should be quarantine inside the Sandbox, and if so, it will be quarentine.
Finally, the sandbox will be activated will a call to __sandbox_ms which will call __mac_syscall.
Possible Bypasses
Bypassing quarantine attribute
Files created by sandboxed processes are appended the quarantine attribute to prevent sandbox escaped. However, if you manage to create an .app folder without the quarantine attribute within a sandboxed application, you could make the app bundle binary point to /bin/bash and add some env variables in the plist to abuse open to launch the new app unsandboxed.
This is what was done in CVE-2023-32364.
Therefore, at the moment, if you are just capable of creating a folder with a name ending in .app without a quarantine attribute, you can scape the sandbox because macOS only checks the quarantine attribute in the .app folder and in the main executable (and we will point the main executable to /bin/bash).
Note that if an .app bundle has already been authorized to run (it has a quarantine xttr with the authorized to run flag on), you could also abuse it... except that now you cannot write inside .app bundles unless you have some privileged TCC perms (which you won't have inside a sandbox high).
Abusing Open functionality
In the last examples of Word sandbox bypass can be appreciated how the open cli functionality could be abused to bypass the sandbox.
Launch Agents/Daemons
Even if an application is meant to be sandboxed (com.apple.security.app-sandbox), it's possible to make bypass the sandbox if it's executed from a LaunchAgent (~/Library/LaunchAgents) for example.
As explained in this post, if you want to gain persistence with an application that is sandboxed you could make be automatically executed as a LaunchAgent and maybe inject malicious code via DyLib environment variables.
Abusing Auto Start Locations
If a sandboxed process can write in a place where later an unsandboxed application is going to run the binary, it will be able to escape just by placing there the binary. A good example of this kind of locations are ~/Library/LaunchAgents or /System/Library/LaunchDaemons.
For this you might even need 2 steps: To make a process with a more permissive sandbox (file-read*, file-write*) execute your code which will actually write in a place where it will be executed unsandboxed.
Check this page about Auto Start locations:
macOS Auto StartAbusing other processes
If from then sandbox process you are able to compromise other processes running in less restrictive sandboxes (or none), you will be able to escape to their sandboxes:
macOS Process AbuseStatic Compiling & Dynamically linking
This research discovered 2 ways to bypass the Sandbox. Because the sandbox is applied from userland when the libSystem library is loaded. If a binary could avoid loading it, it would never get sandboxed:
If the binary was completely statically compiled, it could avoid loading that library.
If the binary wouldn't need to load any libraries (because the linker is also in libSystem), it won't need to load libSystem.
Shellcodes
Note that even shellcodes in ARM64 needs to be linked in libSystem.dylib:
Entitlements
Note that even if some actions might be allowed by at he sandbox if an application has an specific entitlement, like in:
Interposting Bypass
For more information about Interposting check:
macOS Function HookingInterpost _libsecinit_initializer to prevent the sandbox
_libsecinit_initializer to prevent the sandboxInterpost __mac_syscall to prevent the Sandbox
__mac_syscall to prevent the SandboxDebug & bypass Sandbox with lldb
Let's compile an application that should be sandboxed:
Then compile the app:
The app will try to read the file ~/Desktop/del.txt, which the Sandbox won't allow.
Create a file in there as once the Sandbox is bypassed, it will be able to read it:
Let's debug the application to see when is the Sandbox loaded:
Even with the Sandbox bypassed TCC will ask the user if he wants to allow the process to read files from desktop
References
Learn & practice AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Learn & practice GCP Hacking:
HackTricks Training GCP Red Team Expert (GRTE)
Last updated