方式一:http日志分析
通过检查web日志ip访问次数来判断是否异常,如果超过规定的次数即添加到防火墙,禁止访问,达到一定的阻止作用,弊端是可能会造成误封,导致正常用户无法访问;
#!/bin/bash
#
file_logs=/var/log/access.log
while true; do
awk '{print $1}' $file_logs |sort | uniq -c | sort -rnk 1 >/home/http_access.log
exec </home/http_access.log
while read line; do
ip=`echo $line|awk '{print $2}'`
count=`echo $line|awk '{print $1}'`
if [ $count -gt 3 ] && [ `firewall-cmd --list-all | grep -w "$ip"|wc -l` -lt 1 ]; then
firewall-cmd --permanent --zone=public --add-rich-rule="rule family=ipv4 source address=$ip drop"
sleep 1
firewall-cmd --reload
echo "$line is droped" >>/home/droplist.log
#firewall-cmd --permnent --zone=public --remove-rich-rule="rule family=ipv4 source address=$ip drop"
fi
done
sleep 4
done
备注:
1、在设置阀值的时候根据实际环境设置一个值,过大过小利弊取舍;
2、建议在这基础之上添加一个白名单判断,防止误封,导致正常用户无法访问;
方式二:网络连接分析
#分析保存数据到tmp.log
netstat -an|grep EST |awk -F '[ :]' '{print $6}'|sort | uniq -c | sort -rnk 1 >/home/http_access.log
转载于:https://blog.51cto.com/13941177/2364073