简单配置vps,防ddos攻击

防人之心不可无。 网上总有些无聊或者有意的人。很少说了。上干货,配置vps apf防小流量ddos攻击。前端

对于大流量的ddos攻击, 须要机房的硬件防火墙,vps内部可能也扛不住。linux

1. 安装 DDoS deflateweb

DDoS deflate的原理是经过netstat命令找出 发出过量链接的单个IP,并使用iptables防火墙将这些IP进行拒绝。因为iptables防火墙拒绝IP的链接远比从Apache层面上来得高效,所以iptables便成了运行在Apache前端的“过滤器”。一样的,DDoS deflate也能够设置采用APF(高级防火墙)进行IP阻止。ubuntu

 

wget http://www.inetbase.com/scripts/ddos/install.sh
chmod +x install.sh
./install.sh
主要功能与配置

一、能够设置IP白名单,在 /usr/local/ddos/ignore.ip.list 中设置便可;bash

二、主要配置文件位于 /usr/local/ddos/ddos.conf ,打开此文件,根据提示进行简单的编辑便可;服务器

三、DDoS deflate能够在阻止某一IP后,隔一段预置的时候自动对其解封;网络

四、能够在配置文件中设置多长时间检查一次网络链接状况;网站

五、当阻止IP后,能够设置Email提醒this

简单配置一下:调试

FREQ=1 #检测的频率为1分钟 
NO_OF_CONNECTIONS=100 #当单个IP超过100个链接请求时断定为DDOS

APF_BAN=1 #若是打算使用APF阻止IP,则设置为1(须要预先安装APF);若是使用iptables,则设置为0; 

KILL=1 #是否阻止 

EMAIL_TO="webmaster@firstVM.com" #接收邮件 

BAN_PERIOD=600 #阻止时长,10分钟

 

2. 安装配置apf。

APF(Advanced Policy Firewall)是 Rf-x Networks 出品的Linux环境下的软件防火墙,被大部分Linux服务器管理员所采用,使用iptables的规则,易于理解及使用。

适合对iptables不是很熟悉的人使用,由于它的安装配置比较简单,可是功能仍是很是强大的。

脚本安装:

root@linux:/home/zhangy# wget http://www.rfxnetworks.com/downloads/apf-current.tar.gz  
root@linux:/home/zhangy# tar -xvzf apf-current.tar.gz  
root@linux:/home/zhangy# cd apf-9.7-1  
root@linux:/home/zhangy/apf-9.7-1# ./install.sh  

ubuntu 能够快速安装:

sudo aptitude install apf-firewall

 

配置:

vi /etc/apf/conf.apf

 

日后翻页,找到:

# Configure inbound (ingress) accepted services. This is an optional
# feature; services and customized entries may be made directly to an ip's
# virtual net file located in the vnet/ directory. Format is comma separated
# and underscore separator for ranges.
#
# Example:
# IG_TCP_CPORTS="21,22,25,53,80,443,110,143,6000_7000"
# IG_UDP_CPORTS="20,21,53,123"
# IG_ICMP_TYPES="3,5,11,0,30,8"

# Common inbound (ingress) TCP ports
IG_TCP_CPORTS="22"

 

默认只有22端口开放。 咱们先无论。 访问如下80端口的网站试试。 发现居然能够访问。 为何规则没有起做用。

继续查看配置文件。 找啊找。

这一行引发了个人注意:

# Untrusted Network interface(s); all traffic on defined interface will be
# subject to all firewall rules. This should be your internet exposed
# interfaces. Only one interface is accepted for each value.
IFACE_IN="eth0"

忽然想到, 会不会是监听端口的问题。

咱们知道, 若是是真实服务器或者是 xen虚拟化的vps, 其网卡是 eth*。  例如:

ifconfig

image

可是, 我这台vps是openvz虚拟化的。 它的网卡通常是 vnet* 的。 好比:

 

image

 

因而改上面的配置文件:

# Untrusted Network interface(s); all traffic on defined interface will be
# subject to all firewall rules. This should be your internet exposed
# interfaces. Only one interface is accepted for each value.
IFACE_IN="venet0"

重启 apf:

apf -r

提示说找不到 ip_tables 模块。

ip_tables

apf(4677): {glob} unable to load iptables module (ip_tables), aborting.

image

 

因而搜索:  ubuntu  apf  找到这篇文章

http://davidwinter.me/articles/2011/06/05/install-apf-on-ubuntu-11-04/

 

大体意思是说, 在ubuntu中, iptables默认被编译进了内核, 而不是以模块方式运行的。apf默认是使用模块方式调用iptables。 因此要修改apf的配置:

SET_MONOKERN="1"

 

而后重启 apf

而后看到一长串的日志:

image

 

看样子是成功了。

试一下, 果真80端口不能访问了。

只有22还在。

 

回到配置文件, 咱们把端口开放:

vi /etc/apf/conf.apf

找到:

# Configure inbound (ingress) accepted services. This is an optional
# feature; services and customized entries may be made directly to an ip's
# virtual net file located in the vnet/ directory. Format is comma separated
# and underscore separator for ranges.
#
# Example:
# IG_TCP_CPORTS="21,22,25,53,80,443,110,143,6000_7000"
# IG_UDP_CPORTS="20,21,53,123"
# IG_ICMP_TYPES="3,5,11,0,30,8"

# Common inbound (ingress) TCP ports
IG_TCP_CPORTS="22,80,443"

 

保存,重启:  apf –r

 

再访问如下,成功了。

 

最后, 关闭apf的调试模式,正式上线:

找到:

# !!! Do not leave set to (1) !!!
# When set to enabled; 5 minute cronjob is set to stop the firewall. Set
# this off (0) when firewall is determined to be operating as desired.
DEVEL_MODE="1"

 

改为 0:

# !!! Do not leave set to (1) !!!
# When set to enabled; 5 minute cronjob is set to stop the firewall. Set
# this off (0) when firewall is determined to be operating as desired.
DEVEL_MODE="0"

 

欢迎你们访问个人我的独立博客: http://blog.byneil.com

相关文章
相关标签/搜索