文件包含靶场
文件包含漏洞靶场搭建
一、漏洞原理简介
文件包含漏洞(File Inclusion) 是由于服务器端 include()
、require()
等函数对用户输入的文件路径缺乏校验,导致攻击者可以包含本地或远程文件,从而实现:
- 本地文件包含(LFI):包含服务器上的敏感文件
- 远程文件包含(RFI):包含外部恶意文件,实现命令执行(仅当 PHP 允许远程)
二,搭建过程
项目目录结构
file-include-lab/
├── Dockerfile
└── src/
├── index.php
├── include/
│ └── flag.txt
├── home.php
├── about.php
├── contact.php
└── assets/
└── style.css
通过命令查看:
tree file-include-lab
操作命令流程
创建目录结构
mkdir -p file-include-lab/src/{include,assets} && cd file-include-lab
写入Dockerfile
cat > Dockerfile <<EOF
FROM php:7.4-apache
COPY src/ /var/www/html/
RUN chown -R www-data:www-data /var/www/html
EXPOSE 80
CMD ["apache2-foreground"]
EOF
# 写入PHP主文件
cat > src/index.php <<'EOF'
<!DOCTYPE html>
<html>
<head>
<title>文件内容查看器</title>
<link rel="stylesheet" href="assets/style.css">
</head>
<body>
<div class="container">
<h1>📁 文件内容查看系统</h1>
<form method="GET">
<input type="text" name="page" placeholder="输入文件路径(如:home.php)">
<button type="submit">查看</button>
</form>
<div class="content">
<?php
$allowed_pages = ['home.php', 'about.php', 'contact.php'];
$page = $_GET['page'] ?? 'home.php';
if (in_array($page, $allowed_pages)) {
include($page);
} else {
@include($_GET['page']);
}
?>
</div>
<div class="menu">
<a href="/?page=home.php">首页</a>
<a href="/?page=about.php">关于</a>
<a href="/?page=contact.php">联系我们</a>
</div>
</div>
</body>
</html>
EOF
写入CSS样式文件
mkdir -p src/assets && cat > src/assets/style.css <<EOF
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
background-color: #f0f2f5;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 {
color: #2c3e50;
border-bottom: 2px solid #3498db;
padding-bottom: 10px;
}
form {
margin: 20px 0;
}
input[type="text"] {
width: 300px;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
}
button {
padding: 8px 15px;
background: #3498db;
border: none;
color: white;
border-radius: 4px;
cursor: pointer;
}
.content {
min-height: 150px;
padding: 20px;
border: 1px solid #eee;
margin: 20px 0;
}
.menu a {
margin-right: 15px;
color: #3498db;
text-decoration: none;
}
EOF
写入flag文件
echo "flag{W3zYcT8rDxV5sQmK}" > src/include/flag.txt
创建辅助页面
for page in home about contact; do
cat > src/${page}.php <<EOF
<h2>${page^}</h2>
<p>This is ${page} page content.</p>
EOF
done
docker一键搭建
# 构建镜像
docker build -t file-include-lab .
# 启动容器
docker run -d -p 8001:80 --name include-lab file-include-lab