服务器被DDOS攻击的解决方案

一、现象

一天,收到阿里云发来的报警邮件,说明服务器已经受到DDOS攻击,超过基础防护黑洞阈值,这个时候,服务器的公网被断开,内网是可以用的。网站也不能打开。本次攻击时间短暂,未做处理,几分钟后,攻击暂停,网站恢复访问。

服务器被DDOS攻击的解决方案_第1张图片

第二次攻击开始,攻击持续1小时36分,用户不能访问网站,遭遇投诉。

服务器被DDOS攻击的解决方案_第2张图片

攻击暂停,网站恢复。需要想办法处理DDOS攻击,阿里云提供的方式,购买DDOS原生防护企业版,1年价格要几十万,太贵了。

服务器被DDOS攻击的解决方案_第3张图片

  

二、解决方案

网上寻找解决方案,并在本地测试

确认是否受到ddos攻击

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

过滤IP地址127.0和10.10段

netstat -ntu | awk '{print $5}' | cut -d: -f1 | grep -v -E '10.10|127.0' |sort | uniq -c | sort -n

1、安装DDos deflate

wget http://www.inetbase.com/scripts/ddos/install.sh   //下载DDoS deflate

chmod 0700 install.sh      //添加权限

./install.sh         //执行

2、配置DDos deflate

vim /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=150     #最大连接数,超过这个数IP就会被屏蔽,一般默认即可

##### 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。推荐使用iptables,将APF_BAN的值改为0即可。

##### 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秒,可根据情况调整

ddos.sh需要调整

netstat -ntu | awk '{print $5}' | cut -d: -f1 | grep -v -E '10.10|127.0' |sort | uniq -c | sort -nr > $BAD_IP_LIST

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    //按照配置文件创建一个执行计划

需要注意的是:
DDos default安装完成后在/usr/local/ddos目录下产生了ddos.conf、ddos.sh、ignore.ip.list和LICENSE这四个文件,其中:
ddos.conf是配置文件,ddos.sh是一个Shell文件,ignore.ip.list是存放忽略IP的文件,LICENSE是版权声明文件,安装完成后还在/etc/cron.d/下生产了ddos.cron文件,内容如下:

[root@localhost ddos]# cat /etc/cron.d/ddos.cron

SHELL=/bin/sh

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

4、卸载

http://www.inetbase.com/scripts/ddos/uninstall.ddos

chmod 0700 uninstall.ddos

./uninstall.ddos

5、本地压力测试

5.1压力测试准备

192.168.1.61 服务器安装nginx(安装步骤省)

5.2压力测试命令安装

yum -y install httpd-tools

ab -n 1000 -c 10 http://192.168.1.61/index.html

三、总结

安装DDoS deflate后,通过几天观察,DDOS攻击的现象暂时没有发生了。

注:遇到攻击先不要慌,积极想办法解决问题。

你可能感兴趣的:(服务器安全,服务器,ddos,运维)