Linux下防御或减轻DDOS攻击方法,linux防御ddos攻击在网上找了教程,成功


今天上午服务器被ddos攻击,还好对方只是使用了一台电脑,没有搞成千上万个木马来,要不然那服务器直接就垮了。在网上找了教程,成功解决。于是把本次防御ddos的方法记录一下了。


查看攻击IP

首先使用以下代码,找出攻击者IP

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

将会得出类似如下的结果:

1 114.226.9.132


1 174.129.237.157


1 58.60.118.142


1 Address


1 servers)


2 118.26.131.78


3 123.125.1.202


3 220.248.43.119


4 117.36.231.253


4 119.162.46.124


6 219.140.232.128


8 220.181.61.31


2311 67.215.242.196

前面的数字表示IP连接的次数,可见最后一个IP 67.215.242.196连接服务器2311次,每个IP几个、十几个或几十个连接数都还算比较正常,如果像上面成百上千肯定就不正常了。

解决方法,使用DDoS deflate+iptables

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

安装DDoS deflate


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


chmod 0700 install.sh //添加权限


./install.sh //执行

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

用户可根据给默认配置文件加上的注释提示内容,修改配置文件。

查看/usr/local/ddos/ddos.sh文件的第117行

netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -nr > $BAD_IP_LIST

修改为以下代码即可!

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

iptables防火墙

iptables是Linux上常用的防火墙软件,下面vps侦探给大家说一下iptables的安装、清除iptables规则、iptables只开放指定端口、iptables屏蔽指定ip、ip段及解封、删除已添加的iptables规则等iptables的基本应用。

安装iptables防火墙

如果没有安装iptables需要先安装,CentOS执行:

yum install iptables

清除已有iptables规则

iptables -F


iptables -X


iptables -Z

开放指定的端口

#允许本地回环接口(即运行本机访问本机)


iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT


# 允许已建立的或相关连的通行


iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT


#允许所有本机向外的访问


iptables -A OUTPUT -j ACCEPT


# 允许访问22端口


iptables -A INPUT -p tcp --dport 22 -j ACCEPT


#允许访问80端口


iptables -A INPUT -p tcp --dport 80 -j ACCEPT


#允许FTP服务的21和20端口


iptables -A INPUT -p tcp --dport 21 -j ACCEPT


iptables -A INPUT -p tcp --dport 20 -j ACCEPT


#如果有其他端口的话,规则也类似,稍微修改上述语句就行


#禁止其他未允许的规则访问


iptables -A INPUT -j REJECT (注意:如果22端口未加入允许规则,SSH链接会直接断开。)


iptables -A FORWARD -j REJECT

屏蔽IP

#如果只是想屏蔽IP的话“3、开放指定的端口”可以直接跳过。


#屏蔽单个IP的命令是


iptables -I INPUT -s 123.45.6.7 -j DROP


#封整个段即从123.0.0.1到123.255.255.254的命令


iptables -I INPUT -s 123.0.0.0/8 -j DROP


#封IP段即从123.45.0.1到123.45.255.254的命令


iptables -I INPUT -s 124.45.0.0/16 -j DROP


#封IP段即从123.45.6.1到123.45.6.254的命令是


iptables -I INPUT -s 123.45.6.0/24 -j DROP

查看已添加的iptables规则

iptables -L -n


v:显示详细信息,包括每条规则的匹配包数量和匹配字节数


x:在 v 的基础上,禁止自动单位换算(K、M)


n:只显示IP地址和端口号,不将ip解析为域名

删除已添加的iptables规则

将所有iptables以序号标记显示,执行:

iptables -L -n --line-numbers

比如要删除INPUT里序号为8的规则,执行:

iptables -D INPUT 8

iptables的开机启动及规则保存

CentOS上可能会存在安装好iptables后,iptables并不开机自启动,可以执行一下:

chkconfig --level 345 iptables on

将其加入开机启动。

CentOS上可以执行:service iptables save保存规则。

另外更需要注意的是Debian/Ubuntu上iptables是不会保存规则的。

需要按如下步骤进行,让网卡关闭是保存iptables规则,启动时加载iptables规则:

创建/etc/network/if-post-down.d/iptables 文件,添加如下内容:

#!/bin/bash


iptables-save > /etc/iptables.rules

执行:chmod +x /etc/network/if-post-down.d/iptables 添加执行权限。

创建/etc/network/if-pre-up.d/iptables 文件,添加如下内容:

#!/bin/bash


iptables-restore < /etc/iptables.rules

执行:chmod +x /etc/network/if-pre-up.d/iptables 添加执行权限。

关于更多的iptables的使用方法可以执行:iptables --help或网上搜索一下iptables参数的说明。


相关内容

    暂无相关文章