Hacktricks-skills php-bypass-techniques
Security research skill for understanding and documenting PHP function bypass techniques including disable_functions, safe_mode, and open_basedir restrictions. Use this skill when users ask about PHP security testing, penetration testing PHP applications, researching PHP vulnerabilities, documenting bypass methods for authorized security assessments, or analyzing PHP configuration weaknesses. Trigger on mentions of PHP security, disabled functions, safe_mode bypass, proc_open exploitation, LD_PRELOAD attacks, or PHP pentesting.
git clone https://github.com/abelrguezr/hacktricks-skills
skills/network-services-pentesting/pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedir-bypass/disable_functions-bypass-php-safe_mode-bypass-via-proc_open-and-custom-environment-exploit/SKILL.MDPHP Bypass Techniques Research Skill
Purpose
This skill helps security professionals understand, document, and analyze PHP bypass techniques for authorized penetration testing and security research only. It covers methods to bypass PHP restrictions like
disable_functions, safe_mode, and open_basedir.
⚠️ Legal and Ethical Requirements
Before using any technique from this skill:
- Written authorization - You must have explicit written permission from the system owner
- Scope definition - Know exactly which systems you're authorized to test
- Legal compliance - Ensure your testing complies with local laws (CFAA, Computer Misuse Act, etc.)
- Responsible disclosure - Report findings to the appropriate parties
- No production abuse - Never use these techniques on systems you don't own or have permission to test
Unauthorized use of these techniques is illegal and can result in criminal charges.
Core Technique: proc_open with LD_PRELOAD
Overview
When PHP has
disable_functions enabled (blocking functions like system(), exec(), shell_exec()), attackers may bypass restrictions using proc_open() with custom environment variables to load malicious shared libraries.
How It Works
- proc_open() is often not disabled even when other execution functions are
- LD_PRELOAD environment variable can force the dynamic linker to load a custom shared library before any other
- Custom .so file can contain code that executes arbitrary commands
- File descriptors redirect output to readable files
Technical Details
// Basic structure of the bypass $descriptorspec = array( 0 => array("pipe", "r"), // stdin 1 => array("file", "output.txt", "w"), // stdout 2 => array("file", "errors.txt", "a") // stderr ); $cwd = '.'; $env = array('LD_PRELOAD' => '/path/to/malicious.so'); $process = proc_open('id', $descriptorspec, $pipes, $cwd, $env);
Key Components
| Component | Purpose |
|---|---|
| Defines input/output streams for the process |
| Environment variable that loads custom shared library |
| Compiled shared object containing exploit code |
| PHP function that spawns the process with custom environment |
When This Technique Applies
Conditions Required
- proc_open() is NOT disabled - Check with
function_exists('proc_open') - Write access - You can write files to the server filesystem
- Compilation capability - You can compile a .so file (or upload pre-compiled)
- Linux system - LD_PRELOAD works on Linux (not Windows/macOS)
- Dynamic linking - PHP is compiled with dynamic linking support
Detection Methods
// Check if proc_open is available if (function_exists('proc_open')) { echo "proc_open is available"; } // Check disabled functions $disabled = ini_get('disable_functions'); echo "Disabled: " . $disabled; // Check safe_mode (deprecated in PHP 5.4, removed in 7.0) $safe_mode = ini_get('safe_mode'); echo "Safe mode: " . ($safe_mode ? 'enabled' : 'disabled');
Related Bypass Techniques
1. open_basedir Bypass
When
open_basedir restricts file access:
- Symlink attacks - Create symlinks to restricted directories
- Phar stream wrapper - Use
to read filesphar:// - Zip stream wrapper - Use
for archive accesszip:// - Filter wrappers - Use
for encoding/decodingphp://filter
2. Alternative Execution Methods
If
proc_open() is also disabled:
- popen() - May still be available
- pcntl_exec() - Process control extension
- curl_file_get_contents() - With custom protocols
- mail() - With crafted parameters
- fsockopen() - Network-based command execution
Documentation Template
When documenting findings in a security report:
## Finding: PHP Function Bypass via proc_open ### Severity: High ### Description PHP configuration allows command execution through proc_open() with LD_PRELOAD environment variable manipulation, bypassing disable_functions restrictions. ### Technical Details - Affected function: proc_open() - Bypass method: LD_PRELOAD shared library injection - Required conditions: Write access, Linux system, dynamic linking ### Proof of Concept [Include sanitized code example] ### Impact - Arbitrary command execution - Full system compromise possible - Data exfiltration capability ### Remediation 1. Disable proc_open() in disable_functions 2. Use PHP-FPM with proper user isolation 3. Implement chroot jails 4. Keep PHP updated (safe_mode removed in 7.0) 5. Use modern PHP security extensions ### References - [Original research](http://blog.safebuff.com/2016/05/06/disable-functions-bypass/) - PHP Security Checklist - OWASP PHP Security Cheat Sheet
Testing Workflow
Step 1: Reconnaissance
# Check PHP version php -v # Check disabled functions php -r "echo ini_get('disable_functions');" # Check open_basedir php -r "echo ini_get('open_basedir');"
Step 2: Capability Testing
<?php // Test proc_open availability if (function_exists('proc_open')) { echo "proc_open: AVAILABLE\n"; } else { echo "proc_open: DISABLED\n"; } // Test file write capability $test_file = '/tmp/test_write_' . uniqid(); if (file_put_contents($test_file, 'test')) { echo "File write: AVAILABLE\n"; unlink($test_file); } else { echo "File write: RESTRICTED\n"; } ?>
Step 3: Documentation
Document all findings with:
- System configuration
- Available bypass vectors
- Proof of concept (sanitized)
- Remediation recommendations
Security Recommendations
For System Administrators
- Disable dangerous functions - Include
,proc_open
,popen
,system
,execshell_exec - Use PHP-FPM - Run PHP with proper user isolation
- Implement chroot - Restrict filesystem access
- Keep updated - PHP 7.0+ removed safe_mode (use modern alternatives)
- Use security extensions - Consider ModSecurity, RIPS, or similar
For Developers
- Never trust user input - Validate and sanitize all inputs
- Use prepared statements - Prevent SQL injection
- Implement proper error handling - Don't expose stack traces
- Follow principle of least privilege - Run with minimal permissions
- Regular security audits - Use static analysis tools
References
- Original Safebuff Research
- PHP Security Checklist
- OWASP PHP Security Cheat Sheet
- HackTricks PHP Pentesting
Disclaimer
This skill is for educational and authorized security testing purposes only. The author and Anthropic are not responsible for misuse of this information. Always obtain proper authorization before testing any system.