Get a Pentest and security assessment of your IT network.

Cyber Security

PHP Safe Mode Bypass

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.

  1. Check if Safe Mode is Enabled:
    • Use phpinfo() to determine if safe mode is active. Look for ‘safe_mode’ in the output.
  2. Using `$_SERVER` Variables (Common Bypass):
  3. Safe mode often restricts access to certain server variables directly. However, you can sometimes access them indirectly through $_SERVER.

    • Attempt to read environment variables using $_SERVER['PHP_ENV'] or similar keys. The exact key depends on the server configuration.
  4. Using `$_GET`, `$_POST` and `$_COOKIE` Variables:
  5. Safe mode might not always properly sanitize input from these sources. This can be exploited to inject malicious code.

    • Attempt to pass file paths or commands through GET, POST, or COOKIE parameters and execute them using functions like include() or system() (if allowed).
  6. Using `register_globals` (If Enabled – Highly Deprecated):
  7. 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.

    • Pass variables directly through the URL to modify safe mode settings or execute commands.
  8. Using `escapeshellarg()` and `shell_exec()`/`system()` (If Allowed):
  9. If shell execution is allowed, you can attempt to bypass safe mode restrictions by properly escaping arguments passed to shell commands.

    • Use escapeshellarg() to sanitize the input before passing it to shell_exec() or system().
  10. Using `proc_open()` (If Allowed):
  11. Similar to shell execution, proc_open() allows executing system commands. It provides more control but requires careful handling of input and output.

    • Use proc_open() to execute commands with specific arguments and environment variables.
     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);
    ?>
  12. Exploiting File Upload Vulnerabilities:
  13. 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.

    • Upload a PHP script containing code designed to execute commands or access restricted files. Ensure the uploaded file has executable permissions.
  14. Using `symlink()` (If Allowed):
  15. If symlink() is enabled, you might be able to create symbolic links to bypass file restrictions.

    • Create a symbolic link to a restricted file or directory.

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.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation