Skip to content Skip to main navigation Skip to footer

Linux:Iptables DDOS/CC 自动屏蔽脚本

最近不停地被 CC (DDOS的一种)频繁干扰,分享一个 iptables 屏蔽 DDOS 的脚本。 

让 crond 每分钟运行一次。

############### KILL DDOS ##############
iptables_log="/data/logs/iptables_conf.log"
### Iptables 配置导出的路径,可任意修改 ###
########################################
status=`netstat -na|awk '$5 ~ /[0-9]+:[0-9]+/ {print $5}'|awk -F ":" -- '{print $1}' |sort -n|uniq -c |sort -n|grep -v 127.0.0.1|tail -n 1`
NUM=`echo $status|awk '{print $1}'`
IP=`echo $status|awk '{print $2}'`
result=`echo "$NUM > 200" | bc`
### 如果同时连接数大于 200 则干掉!###
if [[ $result = 1 ]]
then
echo IP:$IP is over $NUM, BAN IT!
/sbin/iptables -A INPUT -s $IP -j DROP
fi
########################################
iptables-save > ${iptables_log}
### 输出当前的 iptable 配置作为日志 ###
########################################

有朋友指出,“ 這個腳本裏應該把tail -n 1和grep -v 127.0.0.1倒一倒,否則有些情況下會完全不起作用的”,所以对原脚本有修改。

via http://www.linuxde.net/2013/05/13538.html

0 Comments

There are no comments yet

Leave a comment

Your email address will not be published.