macOS Sandbox Debug & Bypass

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.

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.

macOS Office Sandbox Bypasses

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 Start

Abusing 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 Abuse

Static 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 Hooking

Interpost _libsecinit_initializer to prevent the sandbox

Interpost __mac_syscall to prevent the Sandbox

Debug & bypass Sandbox with lldb

Let's compile an application that should be sandboxed:

Then compile the app:

Let's debug the application to see when is the Sandbox loaded:

References

Last updated