TL;DR
PHP safe mode is deprecated and removed in recent versions (5.6+). However, if you encounter a legacy system still using it, this guide outlines common bypass techniques. Warning: Bypassing security measures can be illegal or unethical without proper authorization. This information is for educational purposes only.
Bypassing PHP Safe Mode
Safe mode restricts PHP’s functionality to enhance security. These methods exploit vulnerabilities and misconfigurations. Always prioritize upgrading the PHP version instead of relying on bypasses.
- Check if Safe Mode is Enabled:
- Use
phpinfo()to determine if safe mode is active. Look for ‘safe_mode’ in the output. - Using `$_SERVER` Variables (Common Bypass):
- Attempt to read environment variables using
$_SERVER['PHP_ENV']or similar keys. The exact key depends on the server configuration. - Using `$_GET`, `$_POST` and `$_COOKIE` Variables:
- Attempt to pass file paths or commands through GET, POST, or COOKIE parameters and execute them using functions like
include()orsystem()(if allowed). - Using `register_globals` (If Enabled – Highly Deprecated):
- Pass variables directly through the URL to modify safe mode settings or execute commands.
- Using `escapeshellarg()` and `shell_exec()`/`system()` (If Allowed):
- Use
escapeshellarg()to sanitize the input before passing it toshell_exec()orsystem(). - Using `proc_open()` (If Allowed):
- Use
proc_open()to execute commands with specific arguments and environment variables. - Exploiting File Upload Vulnerabilities:
- Upload a PHP script containing code designed to execute commands or access restricted files. Ensure the uploaded file has executable permissions.
- Using `symlink()` (If Allowed):
- Create a symbolic link to a restricted file or directory.
Safe mode often restricts access to certain server variables directly. However, you can sometimes access them indirectly through $_SERVER.
Safe mode might not always properly sanitize input from these sources. This can be exploited to inject malicious code.
If register_globals is enabled (a very insecure configuration), variables passed in the URL are automatically registered as global variables. This allows direct manipulation of server settings.
If shell execution is allowed, you can attempt to bypass safe mode restrictions by properly escaping arguments passed to shell commands.
Similar to shell execution, proc_open() allows executing system commands. It provides more control but requires careful handling of input and output.
array("pipe", "r"), // stdin is a pipe that the child reads from
1 => array("pipe", "w"), // stdout is a pipe that the child writes to
2 => array("pipe", "w") // stderr is a pipe that the child writes to
);
$process = proc_open('ls ' . escapeshellarg($_GET['dir']), $descriptorspec, $pipes);
?>
If file upload functionality exists and is not properly secured, you might be able to upload a malicious PHP script that bypasses safe mode restrictions.
If symlink() is enabled, you might be able to create symbolic links to bypass file restrictions.
Important Considerations
- Upgrade PHP: The best solution is always to upgrade to the latest version of PHP, which no longer supports safe mode.
- Input Validation: Implement robust input validation and sanitization techniques to prevent code injection attacks.
- Least Privilege: Run PHP with the least necessary privileges to minimize potential damage from successful exploits.

