Hacktricks-skills python-security-research
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.
git clone https://github.com/abelrguezr/hacktricks-skills
skills/generic-methodologies-and-resources/python/python/SKILL.MDPython 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
or__import__importlib - Built-in function restrictions may be bypassed through
manipulation__builtins__ - File system access can sometimes be gained through
oropen()exec() - Network access restrictions may be bypassed via
orsocketurllib
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
parameter to restrict importscustom_objects - 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:
module analysis toolspickle- 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