我们可以使用netstat命令查看当前系统连接的状态,是否有受到DDOS攻击

# netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
  • 1.

前面是IP地址的请求数

9 183.32.67.179
10 121.35.228.194
  • 1.
  • 2.

开始配置防御,版本:DDoS-Deflate version 0.6

第一步:安装命令:

# wget http://www.inetbase.com/scripts/ddos/install.sh
# chmod 700 install.sh
# ./install.sh
  • 1.
  • 2.
  • 3.

然后会自动进行安装,完成后会有一段版权提示与说明,按q键退出即可。

卸载命令:

# wget http://www.inetbase.com/scripts/ddos/uninstall.ddos
# chmod 700 uninstall.ddos
# ./uninstall.ddos
  • 1.
  • 2.
  • 3.

第二步:修改配置

进入目录/usr/local/ddos/ddos.conf,或者也可以通过命令更改 vi /usr/local/ddos/ddos.conf 编辑完成后:wq保存退出
下面介绍一下ddos.conf的基本配置#为注释部分不用理会关键配置项有:

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" #crond定时任务脚本
APF="/etc/apf/apf"          #APF执行文件
IPT="/sbin/iptables"        #iptables执行文件

FREQ=1 #间隔多久检查一次,默认1分钟

NO_OF_CONNECTIONS=150 #最大连接数设置,超过这个数字的IP就会被屏蔽

APF_BAN=0 #1:APF,0:iptables:1,CENTOS 6.X 推荐使用iptables

KILL=1    #是否屏蔽IP 1:屏蔽,0:不屏蔽

EMAIL_TO="root" #发送电子邮件报警的邮箱地址,换成自己使用的邮箱

BAN_PERIOD=600  #禁用IP时间,可根据情况调整,默认单位:秒
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

配置文件/usr/local/ddos/ddos.sh,修改117行

netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr > $BAD_IP_LIST
  • 1.

修改为以下代码即可!

netstat -ntu | awk '{print $5}' | cut -d: -f1 | sed -n '/[0-9]/p' | sort | uniq -c | sort -nr > $BAD_IP_LIST
  • 1.

第三步:启动防御

先添加定时crond执行任务:

# crontab /etc/cron.d/ddos.cron
  • 1.

然后启动服务:

# service iptables restart
# service crond restart
  • 1.
  • 2.

最后加入开机任务:

# chkconfig crond on
# chkconfig iptables on
  • 1.
  • 2.

收工。