CentOS下防護或減輕DDoS攻擊方法(轉)

說明:仍是老話題,不可能徹底杜絕,只能減輕。html

查看攻擊IPcentos

首先使用如下代碼,找出攻擊者IPbash

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+iptablestcp

DDoS deflate是一款免費的用來防護和減輕DDoS攻擊的腳本。它經過netstat監測跟蹤建立大量網絡鏈接的IP地址,在檢測到某個結點超過預設的限制時,該程序會經過APF或IPTABLES禁止或阻擋這些IP.post

安裝DDoS deflatethis

wget http://www.inetbase.com/scripts/ddos/install.sh #下載DDoS deflate
chmod 0700 install.sh #添加權限
./install.sh #執行

配置DDoS deflatespa

下面是DDoS deflate的默認配置位於/usr/local/ddos/ddos.conf ,默認以下:rest

##### 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(以上內容轉自此篇文章)

相關文章
相關標籤/搜索