Linux+DDoS deflate 预防DDoS

使用DDoS脚本防止DDoS

 

使用DDoS脚本防止DDoS:

DDoS概述:

分布式拒绝服务(DDoS:Distributed Denial of Service),指借助于客户/服务器技术,将多个计算机联合起来作为attack平台,对一个或多个目标发动DDoS,从而成倍地提高拒绝服务的威力。

如何查看是否受到DDoS?

通过netstat  查看网络连接数。如果一个IP地址对服务器建立很多连接数(比如一分钟产生了100个连接),就认为发生了DDoS

[root@web ~]# vim  ddos_test.sh   #写入以下内容
#!/bin/bash
netstat -ntu | awk '{print $5}' | cut -d: -f1 | uniq -c | sort -n
查看tcp和udp连接情况|截取外网IP和端口|截取外网IP|去重|排序并统计

模拟DDOS

[root@web ~]# systemctl start httpd
# 使用ab命令模拟DDoS 访问一个页面 , 页面越大 ,消耗服务器带宽就越大
[root@DDoS ~]# ab -n 1000 -c 10 http://192.168.94.11/index.html  # -n 要产生的链接数总和   -c 同时打开的客户端数量
# 回到web服务器查看
[root@web ~]# netstat -ntu | awk '{print $5}' | cut -d: -f1 | uniq -c | sort -n      1 192.168.94.254
      1 Address      1 servers)   1000 192.168.94.111# 如果发现某个IP连接数据有上百的链接,说明就有DDOS

解决办法

使用DDoS deflate 解决服务器被DDOS的问题

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

下面开始安装DDos deflate

[root@web ~]# wget  
[root@web ~]# chmod +x install.sh 
[root@web ~]# ./install.sh #需要机器能上网
Installing DOS-Deflate 0.6Downloading source files.........doneCreating cron to run script every minute.....(Default setting).....doneInstallation has completed.
Config file is at /usr/local/ddos/ddos.conf
Please send in your comments and/or suggestions to [email protected] #下面内容省略 输入q退出
[root@web ~]# cd /usr/local/ddos/
[root@web ddos]# ls
ddos.conf  ddos.sh  ignore.ip.list  LICENSE    # 安装目录下面有配置文件、脚本和IP白名单

配置DDoS deflate配置文件

##### Paths of the script and other files                                             
 2 PROGDIR="/usr/local/ddos"
 3 PROG="/usr/local/ddos/ddos.sh"    #要执行的DDoS脚本  
 4 IGNORE_IP_LIST="/usr/local/ddos/ignore.ip.list"    # IP白名单 , 名单中的IP不受限制  
 5 CRON="/etc/cron.d/ddos.cron"    # 周期任务  
 6 APF="/etc/apf/apf"
 7 IPT="/sbin/iptables"
 8 
 9 ##### frequency in minutes for running the script 
 10 ##### Caution: Every time this setting is changed, run the script with --cron 
 11 #####          option so that the new frequency takes effect 
 12 FREQ=1    # 检查DDoS×××间隔 , 默认一分钟 
 13 
 14 ##### How many connections define a bad IP? Indicate that below. 
 15 NO_OF_CONNECTIONS=150    # 最大连接数,超过这个数IP就会被屏蔽,一般默认即可 
 16 
 17 ##### APF_BAN=1 (Make sure your APF version is atleast 0.96) 
 18 ##### APF_BAN=0 (Uses iptables for banning ips instead of APF) 
 19 APF_BAN=1    # 使用APF还是iptables。推荐使用iptables,将APF_BAN的值改为0即可 
 20 
 21 ##### KILL=0 (Bad IPs are'nt banned, good for interactive execution of script)
 22 ##### KILL=1 (Recommended setting) 
 23 KILL=1    #  是否屏蔽IP,默认即可 
 24 
 25 ##### An email is sent to the following address when an IP is banned. 
 26 ##### Blank would suppress sending of mails 
 27 EMAIL_TO="root"    # 当IP被屏蔽时给指定邮箱发送邮件报警,换成自己的邮箱即可 
 28 
 29 ##### Number of seconds the banned ip should remain in blacklist. 
 30 BAN_PERIOD=600    # 禁用IP时间,默认600秒,可根据情况调整

查看周期任务

[root@web ddos]# cat /etc/cron.d/ddos.cron # 系统级别的计划任务 
SHELL=/bin/sh0-59/1 * * * * root /usr/local/ddos/ddos.sh >/dev/null 2>&1# 每分钟查看一下,是不是有DDoS,如果发现就开始拒绝

测试

# 查看web服务器iptables策略
[root@web ddos]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        

#使用ab命令模拟DDoS
[root@DDoS ~]# ab -n 1000 -c 10 http://192.168.94.11/index.html # 等一分钟查看web服务器查看iptables策略
[root@web ddos]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       all  --  192.168.94.111       0.0.0.0/0           Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@web ddos]#

若果不想使用DDoS deflate怎么办呢?

很简单

一键卸载 :

[root@web ddos]# wget  
[root@web ddos]# chmod +x uninstall.ddos 
[root@web ddos]# ./uninstall.ddos 

Uninstalling DOS-Deflate


Deleting script files.........doneDeleting cron job.......doneUninstall Complete

虽然说DDoS deflate的计划任务是系统级别的

hacker一样有办法去篡改一个系统级别的计划任务

可能我们也看不出动了什么手脚 ,  定期检查的时候看着还是跟往常一样

忽然某一天被DDoS还不知道怎么回事

所以 , 我们还是先下手为强

[root@web ~]# find /etc/cron* -type f -exec md5sum {} \; > /usr/share/cronfile_md5
# 可以使用md5加密 , 因为md5加密算法是不可逆的 , 文件只要被改动过就能看得出来
[root@web share]# cat cronfile_md5 
1638f7fe39f7f52c412e1705a0bc52d1  /etc/cron.d/0hourly
367636170f3ac44df6a117e3cbf7e4ba  /etc/cron.d/raid-check
a51a35325c37a1cc0ed00f8638a06b96  /etc/cron.d/ddos.cron
6e10e35911b4ba4e2dff44613b56676f  /etc/cron.daily/logrotate
16e73be8fe46a83f7525b59f921e9bab  /etc/cron.daily/man-db.cron
d41d8cd98f00b204e9800998ecf8427e  /etc/cron.deny
8675eb4a3dba8e20bd6b82c626304556  /etc/cron.hourly/0anacron
c39252b11aad842fcb75e05c6a27eef8  /etc/crontab
[root@web share]# md5sum -c cronfile_md5     
# 校验文件信息/etc/cron.d/0hourly: 确定
/etc/cron.d/raid-check: 确定
/etc/cron.d/ddos.cron: 确定
/etc/cron.daily/logrotate: 确定
/etc/cron.daily/man-db.cron: 确定
/etc/cron.deny: 确定
/etc/cron.hourly/0anacron: 确定
/etc/crontab: 确定
[root@web share]# echo " " >> /etc/cron.d/ddos.cron     # 加一个空格进去
[root@web share]# md5sum -c cronfile_md5 
/etc/cron.d/0hourly: 确定
/etc/cron.d/raid-check: 确定
/etc/cron.d/ddos.cron: 失败    # 验证失败
/etc/cron.daily/logrotate: 确定
/etc/cron.daily/man-db.cron: 确定
/etc/cron.deny: 确定
/etc/cron.hourly/0anacron: 确定
/etc/crontab: 确定
md5sum: 警告:1 个校验和不匹配

这样定期检查的时候就可以发现系统级别的计划任务是否被篡改

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章