脚本文件
vim ddosdefense.sh #!/bin/bash #定义脚本按照目录 AccordingToTheCatalogue=/sh/ddos #加载环境变量 source $AccordingToTheCatalogue/conf/ddosdefense.conf #判断防火墙状态 JudgeFirewall=`systemctl status firewalld.service | grep 'active (running)'` if [ -z "$JudgeFirewall" ]; then echo "您当前机器firewalld防火墙未开启本程序无法运行" > $AccordingToTheCatalogue/log/TheListOfAttackers`date +"%Y-%m-%d"`.log exit 1 fi for AttackInformation in `netstat -ntu | awk '{print $4 " " $5}' | awk -F':' '{print $1":"$2}' | awk '{print "被攻击目标["$1"]" "---" "攻击人["$2"]"}' | awk '{a[$1]++} END {for(b in a) print b "---" "攻击次数["a[b]"]"}'` do #获取攻击次数 NumberOfAttacks=`echo "$AttackInformation" | awk -F'---' '{print $NF}' | awk -F'[' '{print $NF}' | awk -F']' '{print $NR}'` if [ $NumberOfAttacks -ge $NumberOfAttacksIsBlack ]; then #获取攻击人ip GetAttackerIP=`echo $AttackInformation | awk -F'---' '{print $2}' | awk -F'[' '{print $NF}' | awk -F']' '{print $NR}'` #判断攻击人ip是否在白名单列表 JudgeGetAttackerIP=`cat $AccordingToTheCatalogue/conf/ddosdefense.conf | grep WhiteList | grep $GetAttackerIP` if [ -z $JudgeGetAttackerIP ]; then #把攻击人员信息写入日志中 if [ -d $AccordingToTheCatalogue/log/TheListOfAttackers`date +"%Y-%m-%d"`.log ]; then echo $AttackInformation >> $AccordingToTheCatalogue/log/TheListOfAttackers`date +"%Y-%m-%d"`.log else touch $AccordingToTheCatalogue/log/TheListOfAttackers`date +"%Y-%m-%d"`.log echo $AttackInformation >> $AccordingToTheCatalogue/log/TheListOfAttackers`date +"%Y-%m-%d"`.log fi #拉黑攻击人员 /usr/bin/firewall-cmd --permanent --zone=drop --add-source=$GetAttackerIP /usr/bin/firewall-cmd --reload fi fi done #清除30天前的日志 find $AccordingToTheCatalogue/log/ -type f -name '*.log' -mtime +30 -exec rm -rf {
} \;
配置文件
vim ddosdefense.conf #攻击多少次的ip拉入黑名单 NumberOfAttacksIsBlack=1000 #白名单列表 WhiteList1=192.168.66.240
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/217842.html原文链接:https://javaforall.net
