Linux 使用DDoS deflate结合APF阻止DDOS攻击【大量】

使用DDoS deflate自动屏蔽攻击ip

DDoS deflate 工作原理:

DDoS deflate是一款免费的用来防御和减轻DDoS攻击的脚本。它通过netstat监测跟踪创建大量网络连接的IP地址,在检测到某个IP连接数量超过预设的限制时,该程序会通过APF或IPTABLES禁止或阻挡这些IP.

这个工具对大量模拟的假IP地址效果不是很明显。

DDoS deflate官方网站:http://deflate.medialayer.com/

如何确认是否受到DDOS攻击?

执行:
netstat -tn|awk '{print $5}'|cut -d: -f4|sort|uniq -c|sort -n

# netstat -tn|awk '{print $5}'|cut -d: -f4|sort|uniq -c|sort -n


      3 119.130.3.204
      3 119.189.96.40
      3 120.32.96.36
      3 121.205.42.169
      3 124.14.224.166
      3 125.84.141.163
      3 140.206.88.198
      3 163.142.231.10
      3 183.10.101.226
      3 183.38.8.70
      3 218.16.73.96
      3 220.161.162.16
      3 221.0.181.242
      3 222.209.44.255
      3 27.156.103.252
      3 27.159.120.238
      3 42.236.200.152
      3 49.80.201.233
      3 58.218.180.150
      3 58.23.123.105
      3 58.245.181.18
      3 58.246.7.134
      3 58.63.28.184
      3 60.173.244.103
      4 116.22.197.31
      4 122.96.35.118
      4 123.166.242.11
      4 175.170.7.174
      4 211.138.16.225
      4 61.52.247.79
      5 110.96.188.245
      5 111.197.85.252
      6 123.139.88.43
      6 180.117.227.32
      6 221.13.1.125
      6 42.54.79.14
     10 172.22.40.3

每个IP几个、十几个或几十个连接数都还算比较正常,如果某个IP连接数有几百上千就是攻击了。

1、安装DDoS deflate

wget http://www.inetbase.com/scripts/ddos/install.sh   //下载DDoS deflate
chmod 0700 install.sh      //添加权限
./install.sh         //执行

2、配置DDoS deflate下面是DDoS deflate的默认配置位于/usr/local/ddos/ddos.conf ,内容如下:

##### Paths of the script and other files

PROGDIR="/usr/local/ddos"
PROG="/usr/local/ddos/ddos.sh"
IGNORE_IP_LIST="/usr/local/ddos/ignore.ip.list"  //IP地址白名单
CRON="/etc/cron.d/ddos.cron"    //定时执行程序
APF="/etc/apf/apf"
IPT="/sbin/iptables"

##### frequency in minutes for running the script
##### Caution: Every time this setting is changed, run the script with --cron
#####            option so that the new frequency takes effect
FREQ=1   //检查时间间隔,默认1分钟
##### How many connections define a bad IP? Indicate that below.
NO_OF_CONNECTIONS=4     //最大连接数,超过这个数IP就会被屏蔽,默认150,这里我改成了4,因为根据我们系统实际情况来看,我们确定大量的TIME_WAIT状态的连接都是来着不同伪造的IP,有的IP连接数是4个,那么我们就以4做为阀值,后面还得修改ddos.sh源代码,把原来的netstat命令改成我们需要的,后面有介绍,这样做才有效果。
##### APF_BAN=1 (Make sure your APF version is atleast 0.96)
##### APF_BAN=0 (Uses iptables for banning ips instead of APF)
APF_BAN=1        //使用APF还是iptables,推荐使用apf。如果使用iptables,将APF_BAN的值改为0即可。 可以yum install apf 来安装apf(apf就是Linux系统下控制iptables的防火墙工具)
##### KILL=0 (Bad IPs are'nt banned, good for interactive execution of script)
##### KILL=1 (Recommended setting)
KILL=1   //是否屏蔽IP,默认即可
##### An email is sent to the following address when an IP is banned.
##### Blank would suppress sending of mails
EMAIL_TO="root"   //当IP被屏蔽时给指定邮箱发送邮件,推荐使用,换成自己的邮箱即可
##### Number of seconds the banned ip should remain in blacklist.

BAN_PERIOD=600    //禁用IP时间,默认600秒,可根据情况调整

3、命令选项

/usr/local/ddos/ddos.sh -h    //查看选项
/usr/local/ddos/ddos.sh -k n  //杀掉连接数大于n的连接。n默认为配置文件的NO_OF_CONNECTIONS

/usr/local/ddos/ddos.sh -c    //按照配置文件创建一个执行计划

4、修改ddos.sh脚本

这个软件的关键程序就是这个ddos.sh脚本,原理是利用cron定时每秒执行一次ddos.sh脚本,而ddos.sh通过执行netstat命令获取到异常的IP列表,利用apf把这些IP添加到iptables中去。

定时执行的情况查看下面的文件

# cat /etc/cron.d/ddos.cron
SHELL=/bin/sh
0-59/1 * * * * root /usr/local/ddos/ddos.sh >/dev/null 2>&1

我个人觉得这个脚本在工作中有些问题,源代码中的netstat命令不太符合我的需要,因为我的系统中大量不同IP攻击状态都是TIME_WAIT,我判定这些状态的IP都是伪造的,所以我想针对这些状态的IP进行阻止,那么我修改ddos.sh源代码

BAD_IP_LIST=`$TMP_FILE`
#netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr > $BAD_IP_LIST
 netstat -tn|grep TIME_WAIT|awk '{print $5}'|cut -d: -f4|sort|uniq -c > $BAD_IP_LIST
cat $BAD_IP_LIST

保存,注释掉原来的代码,改成我们需要的。不需要重启,因为每秒钟会重新执行ddos.sh程序。

注意:ddos.sh程序每次还会删除不再连接的IP,这样就不会永久drop掉某个IP造成误阻挡的问题。

5、卸载

wget http://www.inetbase.com/scripts/ddos/uninstall.ddos
chmod 0700 uninstall.ddos

./uninstall.ddos

使用以上方法可以缓解一些攻击。

你可能感兴趣的:(Linux 使用DDoS deflate结合APF阻止DDOS攻击【大量】)