防人之心不可無。 網上總有些無聊或者有意的人。不多說了。上干貨,配置vps apf防小流量ddos攻擊。
對於大流量的ddos攻擊, 需要機房的硬件防火牆,vps內部可能也扛不住。
1. 安裝 DDoS deflate
DDoS deflate的原理是通過netstat命令找出 發出過量連接的單個IP,並使用iptables防火牆將這些IP進行拒絕。由於iptables防火牆拒絕IP的連接遠比從Apache層面上來得高效,因此iptables便成了運行在Apache前端的“過濾器”。同樣的,DDoS deflate也可以設置采用APF(高級防火牆)進行IP阻止。
wget http://www.inetbase.com/scripts/ddos/install.sh chmod +x install.sh ./install.sh
主要功能與配置
1、可以設置IP白名單,在 /usr/local/ddos/ignore.ip.list 中設置即可;
2、主要配置文件位於 /usr/local/ddos/ddos.conf ,打開此文件,根據提示進行簡單的編輯即可;
3、DDoS deflate可以在阻止某一IP后,隔一段預置的時候自動對其解封;
4、可以在配置文件中設置多長時間檢查一次網絡連接情況;
5、當阻止IP后,可以設置Email提醒
簡單配置一下:
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
但是, 我這台vps是openvz虛擬化的。 它的網卡一般是 vnet* 的。 比如:
於是改上面的配置文件:
# 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.
於是搜索: ubuntu apf 找到這篇文章
http://davidwinter.me/articles/2011/06/05/install-apf-on-ubuntu-11-04/
大致意思是說, 在ubuntu中, iptables默認被編譯進了內核, 而不是以模塊方式運行的。apf默認是使用模塊方式調用iptables。 所以要修改apf的配置:
SET_MONOKERN="1"
然后重啟 apf
然后看到一長串的日志:
看樣子是成功了。
試一下, 果然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