前言
虛拟主機服務商在營運過程中可能會受到黑客攻擊,常見的攻擊方式有syn,ddos等。通過更換ip,查找被攻擊的站點可能避開攻擊,但是中斷服務的時間比較長。比較徹底的解決方法是添置硬體防火牆。不過,硬體防火牆價格比較昂貴。可以考慮利用linux 系統本身提供的防火牆功能來防禦。syn攻擊是利用tcp/ip協定3次握手的原理,發送大量的建立連接配接的網絡包,但不實際建立連接配接,最終導緻被攻擊伺服器的網絡隊列被占滿,無法被正常使用者通路。 linux核心提供了若幹syn相關的配置,加大syn隊列長度可以容納更多等待連接配接的網絡連接配接數,打開syn cookie功能可以阻止部分 syn攻擊,降低重試次數也有一定效果。而ddos則是通過使網絡過載來幹擾甚至阻斷正常的網絡通訊。通過向伺服器送出大量請求,使伺服器超負荷。阻斷某一使用者通路伺服器阻斷某服務與特定系統或個人的通訊。可以通過配置防火牆或者使用腳本工具來防範
通過sysctl和iptables來防範
對sysctl參數進行修改
$ sudo sysctl -a | grep ipv4 | grep syn
輸出類似下面:
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_synack_retries = 5
net.ipv4.tcp_syn_retries = 5
net.ipv4.tcp_syncookies:是否打開syn cookies的功能,“1”為打開,“2”關閉。
net.ipv4.tcp_max_syn_backlog:syn隊列的長度,加大隊列長度可以容納更多等待連接配接的網絡連接配接數。
net.ipv4.tcp_synack_retries和net.ipv4.tcp_syn_retries:定義syn重試次數。
把如下加入到/etc/sysctl.conf即可,之後執行“sysctl -p”!
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
提高tcp連接配接能力
net.ipv4.tcp_rmem = 32768
net.ipv4.tcp_wmem = 32768
net.ipv4.sack=0
使用iptables
指令:
# netstat -an | grep ":80" | grep established
來檢視哪些ip可疑~比如:221.238.196.83這個ip連接配接較多,并很可疑,并不希望它再次與221.238.196.81有連接配接。可使用指令:
iptables -a input -s 221.238.196.83 -p tcp -j drop
将來自221.238.196.83的包丢棄。
對于僞造源ip位址的syn flood攻擊。該方法無效
其他參考
防止同步包洪水(sync flood)
# iptables -a forward -p tcp --syn -m limit --limit 1/s -j accept
也有人寫作
# iptables -a input -p tcp --syn -m limit --limit 1/s -j accept
--limit 1/s 限制syn并發數每秒1次,可以根據自己的需要修改防止各種端口掃描
# iptables -a forward -p tcp --tcp-flags syn,ack,fin,rst rst -m limit --limit 1/s -j accept
ping洪水攻擊(ping of death)
# iptables -a forward -p icmp --icmp-type echo-request -m limit --limit 1/s -j accept
使用ddos deflate自動屏蔽攻擊ip
ddos deflate介紹
ddos deflate是一款免費的用來防禦和減輕ddos攻擊的腳本。它通過netstat監測跟蹤建立大量網絡連接配接的ip位址,
在檢測到某個結點超過預設的限 制時,該程式會通過apf或iptables禁止或阻擋這些ip.
ddos deflate官方網站:http://deflate.medialayer.com/
如何确認是否受到ddos攻擊?
執行:
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
以下是我自己用vps測試的結果:
li88-99:~# 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 vps偵探 http://www.vpser.net/
2311 67.215.242.196 #(這個看起來像攻擊)
每個ip幾個、十幾個或幾十個連接配接數都還算比較正常,如果像上面成百上千肯定就不正常了。
1、安裝ddos deflate
wget http://www.inetbase.com/scripts/ddos/install.sh //下載下傳ddos deflate
chmod 0700 install.sh //添權重限
./install.sh //執行
2、配置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秒,可根據情況調整
3、選項
/usr/local/ddos/ddos.sh -h //檢視選項
/usr/local/ddos/ddos.sh -k n //殺掉連接配接數大于n的連接配接。n預設為配置檔案的no_of_connections
/usr/local/ddos/ddos.sh -c //按照配置檔案建立一個執行計劃
4、解除安裝
wget http://www.inetbase.com/scripts/ddos/uninstall.ddos
chmod 0700 uninstall.ddos
./uninstall.ddos
使用以上方法可以緩解一些攻擊。如果伺服器是nginx的話可以按照
1、打開nginx通路日志
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for'; #設定日志格式
#access_log /dev/null;
access_log /usr/local/nginx/logs/access.log access;
2、觀察nginx日志
more /usr/local/nginx/logs/access.log
檢視哪些ip重複很嚴重。
(1)agent的特征
比如:msie 5.01
配置nginx
location /{
if ( $http_user_agent ~* "msie 5.01" ) {
#proxy_pass http://www.google.com;
return 500;
#access_log /home/logs/1.log main;
}
将ip加入iptable内
iptables -a input -s 202.195.62.113 -j drop
我發現的 攻擊時代理+ddos
搜尋ip均為代理位址。日志最後一部分為真實ip(重複很多)。
如何檢視user_agent
在位址欄輸入:
回車就會彈出目前使用的浏覽器的useragent.
ps:ie和firefox的user—agent
"mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)" ( ie6.0)
mozilla/4.0 (compatible; msie 8.0; ; trident/4.0; .net clr 2.0.50727; ciba) (ie8.0)
mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv:1.9.2.12) gecko/20101026 firefox/3.6.12
注意:都含有windows nt 5.1。
"mozilla/4.0 (compatible; msie 6.0; windows 5.1)"(這個應該就是攻擊者使用user_agent).
參考至:http://hi.baidu.com/niupeiyuan/blog/item/685ca931d1d526b85fdf0e5b.html
http://baike.baidu.com/view/23271.htm
http://wenku.baidu.com/view/8d605b1d650e52ea55189821.html
http://www.hackbase.com/tech/2011-07-12/64479.html
http://sudone.com/nginx/nginx_ddos.html
http://www.selfcai.com.cn/?cat=23
本文原創,轉載請注明出處、作者
如有錯誤,歡迎指正
作者:czmmiao 文章出處:http://czmmiao.iteye.com/blog/1616837