如何使用黑白名单防范漏洞

以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

在这里插入图片描述

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值