方式一: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