Python Security Research & Analysis
A skill for understanding and researching Python-based security vulnerabilities, sandbox escape techniques, and deserialization attacks.
When to Use This Skill
Use this skill when:
- Researching Python sandbox escape techniques for security assessments
- Analyzing deserialization vulnerabilities in Python applications
- Investigating Pyscript security issues in web applications
- Understanding Keras model deserialization RCE risks
- Learning Python web request patterns for security testing
- Conducting defensive security analysis of Python codebases
- Preparing for security certifications or training
Core Concepts
Python Sandbox Escapes
Python sandboxes are often used to restrict code execution, but they can be bypassed through various techniques:
Common bypass vectors:
- Import restrictions can be circumvented via
__import__ or importlib
- Built-in function restrictions may be bypassed through
__builtins__ manipulation
- File system access can sometimes be gained through
open() or exec()
- Network access restrictions may be bypassed via
socket or urllib
Research approach:
- Identify the sandbox implementation (restricted python, custom sandbox, etc.)
- Enumerate available builtins and modules
- Test for common bypass techniques
- Document findings for remediation
Deserialization Vulnerabilities
Python's pickle module and similar serialization mechanisms are inherently unsafe:
Key risks:
- Arbitrary code execution through crafted pickle payloads
- Gadget chains in standard library and third-party packages
- Keras model files can contain malicious deserialization code
- YAML, JSON, and other formats may have similar issues
Research methodology:
- Identify serialization points in the application
- Determine the serialization format (pickle, YAML, etc.)
- Research known gadget chains for the Python version
- Test with controlled payloads in isolated environments
- Document remediation strategies
Pyscript Security
Pyscript allows Python execution in browsers, introducing unique attack vectors:
Security considerations:
- Client-side code execution risks
- Cross-origin resource sharing implications
- Data exfiltration through browser APIs
- Integration with web application security
Analysis approach:
- Review Pyscript configuration and restrictions
- Identify data flow between Python and JavaScript
- Test for privilege escalation vectors
- Assess impact of compromised Pyscript execution
Keras Model Deserialization
Keras model files (.h5, .keras) can contain malicious code:
Attack surface:
- Custom objects in model definitions
- Lambda functions with arbitrary code
- Callbacks that execute on load
- Layer configurations with code execution
Defensive measures:
- Validate model sources before loading
- Use
custom_objects parameter to restrict imports
- Implement model signing and verification
- Run model loading in isolated environments
Research Workflow
Phase 1: Reconnaissance
- Identify the target: What Python application or system are you analyzing?
- Determine the context: Is this a sandbox, web application, data pipeline, or ML system?
- Gather information: Python version, installed packages, configuration files
- Map the attack surface: Entry points, data flows, trust boundaries
Phase 2: Analysis
- Review code: Look for deserialization, exec/eval, import statements
- Test restrictions: If sandboxed, enumerate what's available
- Research vulnerabilities: Check CVE databases, security advisories
- Document findings: Create a structured report of potential issues
Phase 3: Validation
- Create test cases: Develop controlled test scenarios
- Execute safely: Use isolated environments (containers, VMs)
- Verify impact: Confirm the vulnerability exists and understand scope
- Document remediation: Provide actionable fixes
Phase 4: Reporting
- Summarize findings: Clear description of each vulnerability
- Assess severity: Use CVSS or similar framework
- Provide remediation: Specific code changes and configurations
- Include references: Link to relevant security resources
Safety Guidelines
Always follow these principles:
- Authorization: Only test systems you have explicit permission to assess
- Isolation: Run all testing in isolated environments (containers, VMs)
- Documentation: Keep detailed records of all testing activities
- Disclosure: Report vulnerabilities responsibly to affected parties
- Legal compliance: Understand and follow applicable laws and regulations
Common Tools & Resources
For research and testing:
pickle module analysis tools
- Python sandbox testing frameworks
- Static analysis tools (Bandit, Pylint)
- Dynamic analysis (strace, ltrace)
- Network analysis (Wireshark, tcpdump)
For learning:
- OWASP Python Security Cheat Sheet
- Python Security Best Practices
- CVE databases for Python-related vulnerabilities
- Security research blogs and conferences
Output Format
When providing security research results, use this structure:
## Vulnerability Analysis
### Finding: [Brief description]
**Severity**: [Critical/High/Medium/Low]
**Location**: [File/Function/Component]
**Description**: [Detailed explanation]
**Proof of Concept**: [Safe, controlled example if applicable]
**Impact**: [What an attacker could achieve]
**Remediation**: [Specific fixes]
**References**: [Links to relevant resources]
Example Scenarios
Scenario 1: Analyzing a Python Sandbox
Input: "I need to understand how to test this Python sandbox for escape vectors"
Approach:
- Identify the sandbox implementation
- Enumerate available builtins and modules
- Test common bypass techniques
- Document findings and remediation
Scenario 2: Deserialization Risk Assessment
Input: "This application loads pickle files from user uploads"
Approach:
- Identify all deserialization points
- Assess input validation and sanitization
- Research known gadget chains
- Recommend safer alternatives (JSON, msgpack)
Scenario 3: Pyscript Security Review
Input: "We're using Pyscript in our web app, what should I check?"
Approach:
- Review Pyscript configuration
- Analyze data flow between Python and JavaScript
- Test for privilege escalation
- Recommend security hardening
Next Steps
After initial research:
- Deep dive: Focus on specific vulnerabilities found
- Tool development: Create custom testing tools if needed
- Team training: Share findings with development teams
- Continuous monitoring: Set up alerts for new vulnerabilities
Important Notes
- This skill is for educational and defensive purposes
- Always obtain proper authorization before testing
- Document all findings for remediation
- Stay updated on new Python security research
- Consider the broader security context, not just Python-specific issues
1---2name: python-security-research3description: How to research and understand Python security vulnerabilities including sandbox escapes, deserialization attacks, and Pyscript exploitation. Use this skill whenever the user mentions Python security, sandbox bypass, deserialization vulnerabilities, Pyscript hacking, Keras model attacks, or needs to understand Python-based attack vectors for security research, penetration testing, or defensive analysis.4---56# Python Security Research & Analysis78A skill for understanding and researching Python-based security vulnerabilities, sandbox escape techniques, and deserialization attacks.910## When to Use This Skill1112Use this skill when:13- Researching Python sandbox escape techniques for security assessments14- Analyzing deserialization vulnerabilities in Python applications15- Investigating Pyscript security issues in web applications16- Understanding Keras model deserialization RCE risks17- Learning Python web request patterns for security testing18- Conducting defensive security analysis of Python codebases19- Preparing for security certifications or training2021## Core Concepts2223### Python Sandbox Escapes2425Python sandboxes are often used to restrict code execution, but they can be bypassed through various techniques:2627**Common bypass vectors:**28- Import restrictions can be circumvented via `__import__` or `importlib`29- Built-in function restrictions may be bypassed through `__builtins__` manipulation30- File system access can sometimes be gained through `open()` or `exec()`31- Network access restrictions may be bypassed via `socket` or `urllib`3233**Research approach:**341. Identify the sandbox implementation (restricted python, custom sandbox, etc.)352. Enumerate available builtins and modules363. Test for common bypass techniques374. Document findings for remediation3839### Deserialization Vulnerabilities4041Python's pickle module and similar serialization mechanisms are inherently unsafe:4243**Key risks:**44- Arbitrary code execution through crafted pickle payloads45- Gadget chains in standard library and third-party packages46- Keras model files can contain malicious deserialization code47- YAML, JSON, and other formats may have similar issues4849**Research methodology:**501. Identify serialization points in the application512. Determine the serialization format (pickle, YAML, etc.)523. Research known gadget chains for the Python version534. Test with controlled payloads in isolated environments545. Document remediation strategies5556### Pyscript Security5758Pyscript allows Python execution in browsers, introducing unique attack vectors:5960**Security considerations:**61- Client-side code execution risks62- Cross-origin resource sharing implications63- Data exfiltration through browser APIs64- Integration with web application security6566**Analysis approach:**671. Review Pyscript configuration and restrictions682. Identify data flow between Python and JavaScript693. Test for privilege escalation vectors704. Assess impact of compromised Pyscript execution7172### Keras Model Deserialization7374Keras model files (.h5, .keras) can contain malicious code:7576**Attack surface:**77- Custom objects in model definitions78- Lambda functions with arbitrary code79- Callbacks that execute on load80- Layer configurations with code execution8182**Defensive measures:**83- Validate model sources before loading84- Use `custom_objects` parameter to restrict imports85- Implement model signing and verification86- Run model loading in isolated environments8788## Research Workflow8990### Phase 1: Reconnaissance91921. **Identify the target**: What Python application or system are you analyzing?932. **Determine the context**: Is this a sandbox, web application, data pipeline, or ML system?943. **Gather information**: Python version, installed packages, configuration files954. **Map the attack surface**: Entry points, data flows, trust boundaries9697### Phase 2: Analysis98991. **Review code**: Look for deserialization, exec/eval, import statements1002. **Test restrictions**: If sandboxed, enumerate what's available1013. **Research vulnerabilities**: Check CVE databases, security advisories1024. **Document findings**: Create a structured report of potential issues103104### Phase 3: Validation1051061. **Create test cases**: Develop controlled test scenarios1072. **Execute safely**: Use isolated environments (containers, VMs)1083. **Verify impact**: Confirm the vulnerability exists and understand scope1094. **Document remediation**: Provide actionable fixes110111### Phase 4: Reporting1121131. **Summarize findings**: Clear description of each vulnerability1142. **Assess severity**: Use CVSS or similar framework1153. **Provide remediation**: Specific code changes and configurations1164. **Include references**: Link to relevant security resources117118## Safety Guidelines119120**Always follow these principles:**1211221. **Authorization**: Only test systems you have explicit permission to assess1232. **Isolation**: Run all testing in isolated environments (containers, VMs)1243. **Documentation**: Keep detailed records of all testing activities1254. **Disclosure**: Report vulnerabilities responsibly to affected parties1265. **Legal compliance**: Understand and follow applicable laws and regulations127128## Common Tools & Resources129130**For research and testing:**131- `pickle` module analysis tools132- Python sandbox testing frameworks133- Static analysis tools (Bandit, Pylint)134- Dynamic analysis (strace, ltrace)135- Network analysis (Wireshark, tcpdump)136137**For learning:**138- OWASP Python Security Cheat Sheet139- Python Security Best Practices140- CVE databases for Python-related vulnerabilities141- Security research blogs and conferences142143## Output Format144145When providing security research results, use this structure:146147```148## Vulnerability Analysis149150### Finding: [Brief description]151152**Severity**: [Critical/High/Medium/Low]153154**Location**: [File/Function/Component]155156**Description**: [Detailed explanation]157158**Proof of Concept**: [Safe, controlled example if applicable]159160**Impact**: [What an attacker could achieve]161162**Remediation**: [Specific fixes]163164**References**: [Links to relevant resources]165```166167## Example Scenarios168169### Scenario 1: Analyzing a Python Sandbox170171**Input**: "I need to understand how to test this Python sandbox for escape vectors"172173**Approach**:1741. Identify the sandbox implementation1752. Enumerate available builtins and modules1763. Test common bypass techniques1774. Document findings and remediation178179### Scenario 2: Deserialization Risk Assessment180181**Input**: "This application loads pickle files from user uploads"182183**Approach**:1841. Identify all deserialization points1852. Assess input validation and sanitization1863. Research known gadget chains1874. Recommend safer alternatives (JSON, msgpack)188189### Scenario 3: Pyscript Security Review190191**Input**: "We're using Pyscript in our web app, what should I check?"192193**Approach**:1941. Review Pyscript configuration1952. Analyze data flow between Python and JavaScript1963. Test for privilege escalation1974. Recommend security hardening198199## Next Steps200201After initial research:2021. **Deep dive**: Focus on specific vulnerabilities found2032. **Tool development**: Create custom testing tools if needed2043. **Team training**: Share findings with development teams2054. **Continuous monitoring**: Set up alerts for new vulnerabilities206207## Important Notes208209- This skill is for **educational and defensive purposes**210- Always obtain proper authorization before testing211- Document all findings for remediation212- Stay updated on new Python security research213- Consider the broader security context, not just Python-specific issues