說明:還是老話題,不可能完全杜絕,只能減輕。
查看攻擊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添加執行權限。
參考:
http://www.centoscn.com/CentosSecurity/CentosSafe/2013/1017/1867.html(以上內容轉自此篇文章)