以pikachu靶场的ssrf漏洞为例:
点击“累了吧,来读一首诗吧”
发现url敏感参数,发现可以传入路径
尝试传入本地文件:
?url=file://c:\windows\system32\drivers\etc\hosts
黑名单防御
防御之前效果
?url=http://127.0.0.1/test/hello.html 正常访问
?url=http://127.0.0.1/test/hello.php正常访问
?url=http://127.0.0.1/test/hello.php3正常访问
?url=http://127.0.0.1/test/hello.txt正常访问
过滤不允许使用访问后缀名为 php3、txt的文件
// 黑名单
// Set blacklist
$substitutions = array(
'php3' => '',
'txt' => '',
);
// Remove any of the charactars in the array (blacklist).
$URL = str_replace( array_keys( $substitutions ), $substitutions, $URL);
防御之后效果
?url=http://127.0.0.1/test/hello.html 正常访问
?url=http://127.0.0.1/test/hello.php 正常访问
?url=http://127.0.0.1/test/hello.php3 无法访问
?url=http://127.0.0.1/test/hello.txt 无法访问
白名单防御
防御之前效果
访问php文件,可以正常访问
?url=http://127.0.0.1/pikachu/vul/ssrf/ssrf_info/info1.php
访问非php文件,可以正常访问
?url=file://c:\windows\system32\drivers\etc\hosts
根据url,考虑通过使用切割函数explode以符号“.”切割成两部分
判断文件是否为php文件,如果不是php文件,则输出“黑客!!!”
由于ip中有三个. 我们将127.0.0.1 改成localhost
?url=http://localhost/pikachu/vul/ssrf/ssrf_info/info1.php
// 白名单
echo $URL."<br \>";
$octet = explode('.',$URL);
echo $octet[1]."<br \>";
if($octet[1] != 'php'){
die('黑客!!!');
}
$URL = $octet[0].".".$octet[1];
防御之后效果
访问php文件,可以正常访问
?url=http://localhost/pikachu/vul/ssrf/ssrf_info/info1.php
访问非php文件,提示“黑客!!!!”
?url=file://c:\windows\system32\drivers\etc\hosts