sandbox-utils/sandbox-run

GitHub: sandbox-utils/sandbox-run

基于 bubblewrap 的零依赖命令行沙箱工具,让开发者能够安全隔离地运行不可信程序和第三方依赖,有效防范供应链攻击和恶意代码窃取敏感数据。

Stars: 75 | Forks: 4

# sandbox-run:在安全的 OS 沙箱中运行命令 [![构建状态](https://img.shields.io/github/actions/workflow/status/sandbox-utils/sandbox-run/ci.yml?branch=master&style=for-the-badge)](https://github.com/sandbox-utils/sandbox-run/actions) [![语言: shell / Bash](https://img.shields.io/badge/lang-Shell-peachpuff?style=for-the-badge)](https://github.com/sandbox-utils/sandbox-run) [![源代码行数](https://img.shields.io/endpoint?url=https%3A%2F%2Fghloc.vercel.app%2Fapi%2Fsandbox-utils%2Fsandbox-run%2Fbadge?filter=sandbox-run%26format=human&style=for-the-badge&label=SLOC&color=skyblue)](https://ghloc.vercel.app/sandbox-utils/sandbox-run) [![脚本大小](https://img.shields.io/github/size/sandbox-utils/sandbox-run/sandbox-run?style=for-the-badge&color=skyblue)](https://github.com/sandbox-utils/sandbox-run) [![Issues](https://img.shields.io/github/issues/sandbox-utils/sandbox-run?style=for-the-badge)](https://github.com/sandbox-utils/sandbox-run/issues) [![赞助者](https://img.shields.io/github/sponsors/kernc?color=pink&style=for-the-badge)](https://github.com/sponsors/kernc) #### 问题陈述 运行他人的程序本质上是不安全的。 [恶意依赖](https://www.google.com/search?q=malicious+python+packages&tbm=nws)\* 🎯 或 [被黑掉的库代码](https://www.google.com/search?q=(hacked+OR+hijacked+OR+backdoored+OR+"supply+chain+attack")+(npm+OR+pypi)&tbm=nws&num=100) :pirate_flag: ([等等](https://slsa.dev/spec/draft/threats-overview) :warning:) **可能会造成严重破坏,包括访问您的所有私密部分** :bangbang:——试想 当前用户的所有凭据以及更多个人数据,例如: * `~/.ssh`, * `~/.pki/nssdb/`, * `~/.mozilla/firefox//key4.db`, * `~/.mozilla/firefox//formhistory.sqlite` ... ✱ 运行任何 [Electron 应用](https://www.electronjs.org/apps) 至少依赖于成百上千个依赖项、NodeJS 和 Chromium 的完美无缺! 😬 #### 解决方案 在独立的安全容器中运行可怕的软件: ``` podman run --rm -it -v "$PWD:$PWD" --net=host --workdir="$PWD" debian:stable-slim ./scary-binary ``` 或者你可以简单地 `sandbox-run scary-binary` (例如 `sandbox-run npx @google/gemini-cli`) 它使用 [**bubblewrap**](https://github.com/containers/bubblewrap)(因 [Flatpak](https://en.wikipedia.org/wiki/Flatpak) 而闻名)在底层生成你的原生 OS 容器, 并在下载了近 500 MB ❗ 的 JavaScript 源代码后, 匿名且安全地执行这个不受信任的第三方 Node/NPM 包, 其 CWD 位于 `$PWD`,新的 `$HOME` 位于 `$PWD/.sandbox-home`。 ## 安装 **除了带有[其标准实用程序集](https://en.wikipedia.org/wiki/List_of_POSIX_commands)的 POSIX shell 之外没有其他依赖**,**以及 `bubblewrap`**。 安装过程以及脚本运行时, 在所有相关的计算平台上行为应该类似, 包括 GNU/Linux 甚至 [Windos/WSL](https://learn.microsoft.com/en-us/windows/wsl/install)。 🤞 ``` # 安装少数不太可能缺失的依赖项,例如 sudo apt install coreutils binutils bubblewrap # 下载脚本并将其放在 PATH 中的某处 curl -vL 'https://bit.ly/sandbox-run' | sudo tee /usr/local/bin/sandbox-run sudo chmod +x /usr/local/bin/sandbox-run # Mark executable sandbox-run # 用法:sandbox-run ARG... sandbox-run ls / ``` ## 使用 每当你想运行一个可怕的 executable 时,只需运行: ``` sandbox-run scary-app args ``` 即可在安全的沙箱中运行 `scary-app`。 #### 额外的 Bubblewrap 参数 你也可以通过 **`$BWRAP_ARGS` 环境变量** 向单个进程调用传递额外的 bubblewrap 参数。例如: ``` BWRAP_ARGS='--bind /opt /opt' \ sandbox-run ./NVIDIA-Driver-Installer.run ``` 有关详细信息,请参阅 `bubblewrap --help` 或 [`man 1 bwrap`](https://manpages.debian.org/unstable/bwrap)。 请注意,**项目根目录下的 [`.env` 文件](https://stackoverflow.com/questions/68267862/what-is-an-env-or-dotenv-file-exactly)** 会被尊重,并为沙箱环境加载 (sourced)。 请参阅下面的更具体示例。 #### 文件系统挂载 **当前工作目录以读写权限挂载**, 而成功运行所需的所有其他内容(例如 /usr) 则以**只读**方式挂载。此外: * `"$PWD/.sandbox-home"` 被绑定挂载 (bind-mounted) 为 `"$HOME"`, 要挂载额外的端点,请使用带有 `--bind` 或 `--bind-ro` 开关的 `BWRAP_ARGS=`。 任何其他未通过额外 CLI 开关显式挂载的内容 **都会在容器终止时丢失**。 #### Linux Seccomp 参见 `bwrap` 开关 [`--seccomp FD` 和 `--add-seccomp-fd FD`](https://manpages.debian.org/unstable/bubblewrap/bwrap.1.en.html#:~:text=Lockdown%20options%3A-,--seccomp%20fd,-Load%20and%20use)。 #### 运行时监控 如果 **环境变量 `VERBOSE=`** 设置为非空值, 完整的 `bwrap` 命令行将在执行前输出到 stderr。 你可以使用 [命令 `lsns`](https://manpages.debian.org/unstable/lsns) 或以下 shell 函数列出 bubblewrap 的进程: ``` list_bwrap () { lsns -u -W | { IFS= read header; echo "$header"; grep bwrap; }; } list_bwrap # Function call ``` 你可以运行 `sandbox-run bash` 来生成 **沙箱内的交互式 shell**。 #### 环境变量 * `BWRAP_ARGS=` – 传递给 `bwrap` 进程的额外参数;以空格或换行符分隔(如果路径等参数本身包含空格)。 * `SANDBOX_RO_BIND=` – 要在沙箱内以只读方式挂载的额外路径 glob 表达式列表。 * `VERBOSE=` – 在执行前打印完整的 `exec bwrap` 命令行。 #### 调试 要查看失败原因,可以使用类似 `colorstrace -f -e '%file,%process' ...` 的命令运行沙箱。 ## 示例 要传递额外的环境变量(除了默认过滤掉的变量之外), 请使用 `bwrap --setenv`,例如: ``` BWRAP_ARGS='--setenv OPENAI_API_KEY c4f3b4b3' sandbox-run my-ai-prog # 或通过 .env (dotenv) 文件传递 ``` 要以 **超级用户** 身份运行沙箱进程 (同时仍保留容器沙箱的所有安全功能), 例如要开放特权端口,请使用参数: ``` BWRAP_ARGS='--uid 0 --cap-add cap_net_bind_service' sandbox-run python -m http.server 80 ``` 要运行 **GUI (X11) 应用**,之前已取得一些成功的做法例如: ``` BWRAP_ARGS='--bind /tmp/.X11-unix/X0 /tmp/.X11-unix/X8 --setenv DISPLAY :8' \ sandbox-run python -m tkinter ``` 参见 [ArchWiki 上的更多示例](https://wiki.archlinux.org/title/Bubblewrap#Using_X11)。 ## 贡献 发现错误——你来修复。谢谢! ## 替代方案 参见姊妹项目 [`sandbox-venv`](https://github.com/sandbox-utils/sandbox-venv/#Viable-alternatives) 中讨论的几个替代方案。
标签:Bash, Bing搜索, Cutter, LangChain, OS-Native, Shell脚本, 子域名枚举, 安全沙箱, 恶意代码防护, 权限控制, 沙箱, 系统安全, 轻量级, 进程隔离, 隔离执行, 零依赖