macOS 鲜为人知的命令行沙盒工具
macOS's Little-Known Command-Line Sandboxing Tool (2025)

原始链接: https://igorstechnoclub.com/sandbox-exec/

## sandbox-exec:macOS 安全工具摘要 `sandbox-exec` 是一个 macOS 命令行工具,用于在受限制的沙箱环境中运行应用程序,从而限制其对系统资源的访问并增强安全性。它对于测试不受信任的代码、限制漏洞造成的损害、控制隐私以及创建隔离的测试环境非常有用。 沙箱化是通过使用类似 Scheme 语法的“配置文件”实现的。这些配置文件定义允许或禁止的操作,针对文件、进程和网络访问。您可以采用“默认拒绝”(最安全)或“默认允许”的方法。 调试沙箱问题涉及使用 Console.app 或终端日志来识别被拒绝的操作。高级技术包括为频繁使用创建别名以及导入在 `/System/Library/Sandbox/Profiles` 中找到的现有系统配置文件。 虽然功能强大,但 `sandbox-exec` 被认为是一个较低级别的工具,其学习曲线比 Apple 的 App Sandbox 更陡峭。它需要迭代测试,并且可能受到 macOS 更新的影响。尽管有些被弃用,但它提供的对安全环境的精细控制是基于 GUI 的解决方案无法比拟的,使其对注重安全的用戶和开发者有用。对于那些寻求更深入了解的人,可以购买详细的手册。

## macOS 沙盒工具:摘要 最近的 Hacker News 讨论强调了 macOS 鲜为人知的 `sandbox-exec` 命令行工具,用于应用程序沙盒化。尽管该工具已被弃用近十年,但仍被 Claude、Firefox、Chrome、Bazel 和 Homebrew 等主流应用程序以及 Swift Package Manager 等工具广泛使用。 弃用的原因是苹果更希望开发者采用“App Sandbox”功能,该功能提供自动策略更新。然而,维护自定义 `sandbox-exec` 配置文件在操作系统更新时可能具有挑战性。虽然 `sandbox-exec` 尚未立即消失(因为它被第一方应用程序使用),但苹果不鼓励将其用于向第三方发布。 讨论还涉及了底层技术——基于 Scheme 的策略语言——以及 macOS 沙盒化与 Linux 容器相比的局限性。一些用户提倡更简单的替代方案,例如为更安全的开发环境使用专用用户帐户。尽管 `sandbox-exec` 已经过时并被弃用,但它仍然是许多需要对 macOS 上应用程序安全性进行细粒度控制的开发人员的重要工具。
相关文章

原文

What is sandbox-exec?

sandbox-exec is a built-in macOS command-line utility that enables users to execute applications within a sandboxed environment. In essence, it creates a secure, isolated space where applications can run with limited access to system resources – only accessing what you explicitly permit.

The concept behind sandboxing is fundamental to modern security: by restricting what an application can access, you minimize the potential damage from malicious code or unintended behavior. Think of it as putting an application in a secure room where it can only interact with specific objects you've placed there.

Benefits of Application Sandboxing

Before diving into usage, let's understand why sandboxing matters:

  1. Protection from malicious code: If you're testing an unfamiliar application or script, sandboxing can prevent it from accessing sensitive files or sending data across the network.

  2. Damage limitation: Even trusted applications can have vulnerabilities. Sandboxing limits the potential impact if an application is compromised.

  3. Privacy control: You can explicitly deny applications access to personal directories like Documents, Photos, or Contacts.

  4. Testing environment: Developers can test how applications function with limited permissions before implementing formal App Sandbox entitlements.

  5. Resource restriction: Beyond security, sandboxing can limit an application's resource consumption or network access.

Getting Started with sandbox-exec

Using sandbox-exec requires creating a sandbox profile (configuration file) that defines the rules for your secure environment. The basic syntax is:

sandbox-exec -f profile.sb command_to_run

Where profile.sb contains the rules defining what the sandboxed application can and cannot do, and command_to_run is the application you want to run within those constraints.

Understanding Sandbox Profiles

Sandbox profiles use a Scheme-like syntax (a LISP dialect) with parentheses grouping expressions. The basic structure includes:

  • A version declaration: (version 1)
  • Default policy: (deny default) or (allow default)
  • Specific rules allowing or denying operations

Rules can target specific resources using:

  • Literal paths: (literal "/path/to/file")
  • Regular expressions: (regex "^/System")
  • Glob patterns: (subpath "/Library")

See Appendix for more complete list of available rules

Two Fundamental Approaches to Sandboxing

There are two primary philosophies when creating sandbox profiles:

1. Deny by Default (Most Secure)

This approach starts by denying everything and explicitly allowing only required operations:

(version 1)
(deny default)
(allow file-read-data (regex "^/usr/lib"))
(allow process-exec (literal "/usr/bin/python3"))

This is the most secure approach, ideal for running untrusted code, but requires careful configuration to make applications functional.

2. Allow by Default (More Permissive)

Alternatively, you can allow everything except specific operations:

(version 1)
(allow default)
(deny network*)
(deny file-write* (regex "^/Users"))

This approach is easier to implement but less secure, as you must anticipate every potential risky operation.

Practical Examples of sandbox-exec in Action

Let's explore some real-world examples to demonstrate the power of custom sandboxing.

Example: Sandboxed Terminal Session

Create a sandboxed terminal session that can't access the network:

# Create terminal-sandbox.sb:
(version 1)
(allow default)
(deny network*)
(deny file-read-data (regex "^/Users/[^/]+/(Documents|Pictures|Desktop)"))

# Run a sandboxed terminal
sandbox-exec -f terminal-sandbox.sb zsh

This creates a terminal session that functions normally but cannot access the network or read from your personal directories.

Example: Using Pre-built System Profiles

macOS includes several pre-built sandbox profiles in /System/Library/Sandbox/Profiles:

# Run a command with the system's no-network profile
sandbox-exec -f /System/Library/Sandbox/Profiles/weatherd.sb command

These system profiles provide configurations for common restriction scenarios and applications. Some of them have quite good comments so you can use it as basis for your future profiles.

Debugging Sandbox Issues

When applications fail in a sandbox, determining the cause can be challenging. Here are effective debugging techniques:

Using the Console App

  1. Open Console.app (Applications → Utilities → Console)
  2. Search for "sandbox" and your application name
  3. Look for lines containing "deny" to identify blocked operations

Using Terminal for Real-time Logs

For real-time monitoring of sandbox violations:

log stream --style compact --predicate 'sender=="Sandbox"'

To filter for a specific application:

log stream --style compact --predicate 'sender=="Sandbox" and eventMessage contains "python"'

These logs show exactly which operations are being denied, helping you refine your sandbox profile.

Advanced Sandbox Techniques

Creating a Sandbox Alias

For frequent sandboxing, add an alias to your shell configuration:

# Add to ~/.zshrc or ~/.bash_profile
alias sandbox-no-network='sandbox-exec -p "(version 1)(allow default)(deny network*)"'

# Then use it as:
sandbox-no-network curl -v https://google.com

but when I did the same for UI applications it didn't work for some reason (I can still open Google.com):

sandbox-no-network /Applications/Firefox.app/Contents/MacOS/firefox

Importing Existing Profiles

You can import and extend existing profiles:

(version 1)
(import "/System/Library/Sandbox/Profiles/bsd.sb")
(deny network*)  # Add additional restrictions

Limitations and Considerations

Despite its power, sandbox-exec has some limitations to consider:

  1. Deprecation status: While functional, Apple discourages its direct use in favor of App Sandbox for developers.

  2. Complex applications: Modern applications often have complex requirements that make comprehensive sandboxing challenging without extensive testing.

  3. Trial and error: Creating effective sandbox profiles often requires iterative testing to identify all necessary permissions.

  4. No GUI: Unlike App Sandbox in Xcode, sandbox-exec has no graphical interface for configuration.

  5. System updates: Major macOS updates might change how sandbox-exec works or what rules are effective.

While Apple has moved toward more user-friendly security models, sandbox-exec remains a powerful tool for those willing to invest time in learning its intricacies. It offers a level of control and customization that GUI-based solutions simply cannot match.

For security-conscious users, developers testing applications, or anyone working with potentially untrusted code, sandbox-exec provides a native macOS solution for creating finely-tuned security environments. Though it requires knowledge of all it's possibility, despite lack of documentation, the security benefits make it well worth the effort.

The most powerful aspect of sandbox-exec is its flexibility – you can create custom security profiles tailored to specific applications and use cases, going far beyond the one-size-fits-all approach of most security tools.

What's Next

If you're interested in learning more about macOS security tools and techniques, check out Apple's official documentation on App Sandbox or explore the pre-built sandbox profiles in /System/Library/Sandbox/Profiles to see how Apple implements sandboxing for system services

联系我们 contact @ memedata.com