簡單配置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

相關文章
相關標籤/搜索