使用DDoS deflate簡單防攻擊
更新LNMP的NGINX到最新版
找到其中的#Port 22(第13行),去掉#,修改成Port 3333
使用如下指令,重新開機SSH服務,注:以後用新端口登陸。
service SSHd restart
二、禁止ROOT登陸
給vpsmm帳号設定密碼:
仍舊是修改/etc/ssh/sshd_config檔案,第39行:#PermitRootLogin yes,去掉前面的#,并把yes改成no,然後,重新開機SSH服務。以後,先使用vpsmm登陸,再su root即可得到ROOT管理權限。
login as: vpsmm
vpsmm@ip password:*****
Last login: Tue Nov 22 14:39:58 2010 from 1.2.3.4
su root
Password:*********** #注這裡輸入ROOT的密碼
使用netstat指令,檢視VPS目前連結确認是否受到攻擊:
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
IP前面的數字,即為連接配接數,如果說正常網站,幾十到一百都屬于正常連接配接,但出現幾百,或上千的就可以墾定這個IP與你的VPS之間可能存在可疑連接配接現象。
可以使用iptables直接BAN了這個IP的永久通路:
iptables -A INPUT -s 12.34.56.78 -j DROP
今天介紹給大家一種方法,是使用軟體DDos deflate來自動檢測并直接BAN掉的方法,首先我們要确認一下iptables服務狀态,預設CENTOS就安裝的,不看也行。
service iptables status
安裝DDos deflat:
wget http://www.inetbase.com/scripts/ddos/install.sh
chmod +x install.sh && ./install.sh
安裝後需要修改/usr/local/ddos/ddos.conf,主要是APF_BAN=1要設定成0,因為要使用iptables來封某些可疑連接配接,注意EMAIL_TO="root",這樣BAN哪個IP會有郵件提示:
##### 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秒,可根據情況調整
四、使用iftop檢視詳細網絡狀況
安裝IFTOP軟體:
yum -y install flex byacc libpcap ncurses ncurses-devel libpcap-devel
wget http://www.ex-parrot.com/pdw/iftop/download/iftop-0.17.tar.gz
tar zxvf iftop-0.17.tar.gz
cd iftop-0.17
./configure
make && make install
快捷鍵:h幫助,n切換顯示IP主機名,s是否顯示本機資訊,d是否顯示遠端資訊,N切換端口服務名稱,b切換是否時數流量圖形條。
執行個體
預設是監控第一塊網卡的流量: iftop
監控eth1: iftop -i eth1
直接顯示IP, 不進行DNS反解析 : iftop -n
直接顯示連接配接埠編号, 不顯示服務名稱: iftop -N
顯示某個網段進出封包流量: iftop -F 192.168.1.0/24 or 192.168.1.0/255.255.255.0
iftop界面含義
第一行:帶寬顯示
中間部分:外部連接配接清單,即記錄了哪些ip正在和本機的網絡連接配接
中間部分右邊:實時參數分别是該通路ip連接配接到本機2秒,10秒和40秒的平均流量
=>代表發送資料,<= 代表接收資料
底部三行:表示發送,接收和全部的流量
底部三行第二列:為你運作iftop到目前流量
底部三行第三列:為高峰值
底部三行第四列:為平均值
六、更新LNMP中的NGINX到最新版
現在最新版是0.8.53,如果以後出新版,隻要更新版本号就可以,在SSH裡運作:
wget http://www.nginx.org/download/nginx-0.8.53.tar.gz
tar zxvf nginx-0.8.53.tar.gz
cd nginx-0.8.53
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module
make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
cd objs/
cp nginx /usr/local/nginx/sbin/
/usr/local/nginx/sbin/nginx -t
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
/usr/local/nginx/sbin/nginx -v
cd ..
rm -rf nginx-0.8.53
rm -rf nginx-0.8.53.tar.gz
七、常用netstat指令:
1.檢視所有80端口的連接配接數
netstat -nat|grep -i "80"|wc -l
2.對連接配接的IP按連接配接數量進行排序
3.檢視TCP連接配接狀态
netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}'
netstat -n | awk '/^tcp/ {++state[$NF]}; END {for(key in state) print key,"\t",state[key]}'
netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"\t",arr[k]}'
netstat -n |awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn
netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c
4.檢視80端口連接配接數最多的20個IP
netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
netstat -ant |awk '/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A,i}' |sort -rn|head -n20
5.用tcpdump嗅探80端口的通路看看誰最高
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -20
6.查找較多time_wait連接配接
netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
7.找查較多的SYN連接配接
netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more
觀看生成的.log日志檔案,判斷來路,直接301百度:
觀看生成的.log日志檔案,判斷浏覽器特征碼,直接301百度:
根據特征碼,直接查找.log檔案,并iptables封死相應IP:
cat com.log | grep 'Mozilla/5.0' | awk '{print "iptables -I INPUT -p tcp --dport 80 -s ", $1, "-j DROP"}'| sort -n | uniq | sh
nginx處理靜态的能力是無與倫比的,如果隻是簡單的判斷分發,就可以解決掉很多攻擊。
本文轉自奔跑在路上部落格51CTO部落格,原文連結http://blog.51cto.com/qiangsh/1586178如需轉載請自行聯系原作者
qianghong000