防止ddos***软件DDoS-Deflate 的安装和使用


系统环境:Centos 6.5 X64

测试完成日期为2017.6.14 by evan.li



1.安装 DDoS deflate


[root@host ]# wget http://www.inetbase.com/scripts/ddos/install.sh

[root@host ]# chmod 0700 install.sh

[root@host ]# ./install.sh


//弹出版权信息后,在SSH下键入 q , 即可退出版权信息。


2.配置 DDoS deflate .


[root@host ]# vi /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” //白名单

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

//ddos.sh执行的频率,单位是分钟,搭配crontab来执行.

如果你更改了该设置,请执行 /usr/local/ddos/ddos.sh –cron

—————————————————————————-


##### How many connections define a bad IP? Indicate that below.

NO_OF_CONNECTIONS=150

//当哪个IP的连接数达到150就加入黑名单;查看连接数你可以执行

#netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n

—————————————————————————-


##### 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=0

//一般情况下你是使用iptables来做防火墙,所以这里你需要将 APF_BAN的值改为0.

—————————————————————————-


##### KILL=0 (Bad IPs are’nt banned, good for interactive execution of script)

##### KILL=1 (Recommended setting)

KILL=1

//将非法的的IP加入iptables的INPUT链中,DROP掉.

—————————————————————————-


##### An email is sent to the following address when an IP is banned.

##### Blank would suppress sending of mails

EMAIL_TO="evan"

//事件通知人的邮件地址,给evan发一封邮件,可以改成你的邮箱地址

—————————————————————————-

##### Number of seconds the banned ip should remain in blacklist.

BAN_PERIOD=600

//关押黑名单中的IP的时长,阻止被定义为ddos***者ip与本机通信时间,默认为600秒


更改白名单配置

[root@host ]# vi /usr/local/ddos/ddos.sh 

在117行,更改此段代码为:netstat -ntu | awk '{print $5}' | egrep -o "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" | sort | uniq -c | sort -nr > $BAD_IP_LIST


注释掉134行:echo $CURR_LINE_IP >> $IGNORE_IP_LIST


执行脚本 

[root@host ]# /usr/local/ddos/ddos.sh --cron


执行后,会生成一个排程文件,内容如下

[root@host ]# vi /etc/cron.d/ddos.cron

SHELL=/bin/sh

0-59/1 * * * * root /usr/local/ddos/ddos.sh >/dev/null 2>&1


安装WEB压力测试软件Siege

一款开源的压力测试工具,可以根据配置对一个WEB站点进行多用户的并发访问,记录每个用户所有请求过程的相应时间,并在一定数量的并发访问下重复进行。
官方:
http://www.joedog.org/
参考文章:  https://www.vpser.net/opt/webserver-test.html

[root@host ]#  wget http://download.joedog.org/siege/siege-4.0.2.tar.gz

[root@host ]#  tar zxf siege-4.0.2.tar.gz
[root@host ]#  cd siege-4.0.2
安装:
[root@host siege-4.0.2]#  ./configure
[root@host siege-4.0.2]#  make 
[root@host siege-4.0.2]#  make install

使用
[root@host siege-4.0.2]# siege -c 200 -r 10 -f example.url
-c是并发量,-r是重复次数。 url文件就是一个文本,每行都是一个url,它会从里面随机访问的。

example.url内容为:

http://www.baidu.com
http://www.163.com

运行5分钟后,按Ctrl+c 中断任务,显示出结果
运行结果说明

Lifting the server siege...

Transactions:                    8 hits       //完成8次处理

Availability:                  53.33 %         //53.33 % 成功率

Elapsed time:                1535.51 secs      //总共用时

Data transferred:              12.43 MB        //共数据传输12.43M

Response time:                 63.41 secs     //响应用时63.41秒:显示网络连接的速度

Transaction rate:               0.01 trans/sec  //均每秒完成 0.01 次处理:表示服务器后

Throughput:                     0.01 MB/sec   //平均每秒传送数据

Concurrency:                    0.33        //实际最高并发数

Successful transactions:         122          //成功处理次数

Failed transactions:               7         //失败处理次数

Longest transaction:           10.46        //每次传输所花最长时间

Shortest transaction:           0.00        //每次传输所花最短时间


Siege压力测试软件运行时,可用命令查看连接数

执行后,将会显示服务器上所有的每个IP多少个连接数

[root@host ]# netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr

    161 103.235.46.39

     10 14.215.177.38

      8 14.215.177.37

      8 115.239.210.27

      6 115.239.211.112

      5 62.145.211.15

      5 183.131.62.40

      1 servers)

      1 Address

      1 191.96.249.136


查看单个IP的连接数目可以通过如下命令查看,依次排列:

[root@host ]#  netstat -ntu | awk '{print $5}' | egrep -o "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" | sort | uniq -c | sort -nr 

    161 103.235.46.39

     10 14.215.177.38

      8 14.215.177.37

      8 115.239.210.27

      6 115.239.211.112

      5 62.145.211.15

      5 183.131.62.40

      1 121.11.81.164

      1 113.107.57.41

      1 106.11.68.13


如果屏蔽后,请用iptables -L -n 命令查看被屏蔽的IP

[root@host ]# iptables -L -n

Chain INPUT (policy ACCEPT)

target     prot opt source               destination         

DROP       all  --  103.235.46.39        0.0.0.0/0           

DROP       all  --  101.78.18.13         0.0.0.0/0           

DROP       all  --  101.78.18.4          0.0.0.0/0           

DROP       all  --  101.99.15.195        0.0.0.0/0  


如上,103.235.46.39此IP有161个连接, 被防火墙给DROP掉了。

邮箱会收到如下的信息

Banned the following ip addresses on Wed Jun 14 15:19:01 CST 2017

 103.235.46.39 with 161 connections


如果不想影响到web压力测试,需要停用排程服务,ddos脚本将不会被执行到

[root@host ]# /sbin/service crond stop


如何删除规则

首先我们要知道 这条规则的编号,每条规则都有一个编号

通过命令 iptables -L -n --line-number 可以显示规则和相对应的编号

num target     prot opt source               destination

1    DROP       all  --  101.78.18.13         0.0.0.0/0           

2    DROP       all  --  101.78.18.4          0.0.0.0/0           

3    DROP       all  --  101.99.15.195        0.0.0.0/0 

多了 num 这一列, 这样我们就可以 看到刚才的规则对应的是 编号2

那么我们就可以进行删除了

iptables -D INPUT 2

删除INPUT链编号为2的规则。这样就可以解除对该IP的封锁了。



参考文章:https://www.aliyun.com/zixun/content/2_6_518775.html


下面连接为DDoS-Deflate配置文件

http://down.51cto.com/data/2317563


以上测试完成日期为2017.6.14 by evan.li