DDoS-Deflate是一款非常小巧的防御和減輕DDoS攻擊的工具猾瘸,它可以通過監(jiān)測(cè)netstat來跟蹤來創(chuàng)建大量互聯(lián)網(wǎng)連接的IP地址信息,通過APF或IPTABLES禁止或阻檔這些非常IP地址颁股。
工作過程描述:
同一個(gè)IP鏈接到服務(wù)器的連接數(shù)到達(dá)設(shè)置的伐值后星著,所有超過伐值的IP將被屏蔽特笋,同時(shí)把屏蔽的IP寫入ignore.ip.list文件中黔姜,與此同時(shí)會(huì)在tmp中生成一個(gè)腳本文件拢切,這個(gè)腳本文件馬上被執(zhí)行,但是一運(yùn)行就遇到sleep 預(yù)設(shè)的秒地淀,當(dāng)睡眠了這么多的時(shí)間后失球,解除被屏蔽的IP,同時(shí)把之前寫入ignore.ip.list文件中的這個(gè)被封鎖的IP刪除帮毁,然后刪除臨時(shí)生成的文件。
一個(gè)事實(shí):如果被屏蔽的IP手工解屏蔽豺撑,那么如果這個(gè)IP繼續(xù)產(chǎn)生攻擊烈疚,那么腳本將不會(huì)再次屏蔽它(因?yàn)榧尤氲搅薸gnore.ip.list),直到在預(yù)設(shè)的時(shí)間之后才能起作用聪轿,加入到了ignore.ip.list中的IP是檢測(cè)的時(shí)候忽略的IP爷肝。可以把IP寫入到這個(gè)文件以避免這些IP被堵塞陆错,已經(jīng)堵塞了的IP也會(huì)加入到ignore.ip.list中灯抛,但堵塞了預(yù)定時(shí)間后會(huì)從它之中刪除。
在tmp文件中生成的解除屏蔽IP的腳本文件內(nèi)容如下:
cat? /tmp/unban.XXXXXXXX#!/bin/shsleep 600/sbin/iptables -D INPUT -s 10.0.10.55 -j DROPgrep -v --file=/tmp/unban.3RIsCVkG /usr/local/ddos/ignore.ip.list > /tmp/unban.VPYmNEnbmv /tmp/unban.VPYmNEnb /usr/local/ddos/ignore.ip.listrm -f /tmp/unban.uqPU5tGKrm -f /tmp/unban.3RIsCVkGrm -f /tmp/unban.VPYmNEnb
另外值得一提的是音瓷,使用iptables進(jìn)行屏蔽時(shí)对嚼,使用的是-I?參數(shù),規(guī)則會(huì)被放置在規(guī)則列表的最前面(這一點(diǎn)很重要)绳慎。
-----------------------------------------------------------------------------
我們可以使用netstat命令查看當(dāng)前系統(tǒng)連接數(shù)據(jù)的統(tǒng)計(jì)纵竖,是否有受到DDOS攻擊
#?netstat -ntu | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
#?netstat -ntu | awk '{print $5}' | cut -d: -f1 |sed -n '/[0-9]/p'| sort | uniq -c | sort -nr
# ?watch -n 1 'echo "show table http-in" | socat unix:/var/run/haproxy.stats - |grep "157.55.39" '
1、安裝DDoS deflate
wget http://www.inetbase.com/scripts/ddos/install.sh
chmod 0700 install.sh
./install.sh
自動(dòng)生成定時(shí)任務(wù)杏愤,默認(rèn)每分鐘執(zhí)行一次
# ?cat /etc/cron.d/ddos.cron?
SHELL=/bin/sh
0-59/1 * * * * root /usr/local/ddos/ddos.sh >/dev/null 2>&1
這個(gè)cron任務(wù)的執(zhí)行頻率是依賴ddos.conf文件中的FREQ變量產(chǎn)生的靡砌,如果修改了此值,可以通過運(yùn)行如下命令更新(實(shí)際也是在安裝時(shí)運(yùn)行了如下命令):
/usr/local/ddos/ddos.sh -c ?或 ?/usr/local/ddos/ddos.sh –cron
2珊楼、配置DDoS deflate
下面是DDoS deflate的默認(rèn)配置位于/usr/local/ddos/ddos.conf 通殃,內(nèi)容如下:
##### 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” ? ?#相當(dāng)于IP地址白名單和當(dāng)前時(shí)段被檢測(cè)出訪問異常的IP地址的合集
CRON=“/etc/cron.d/ddos.cron” ? #定時(shí)執(zhí)行程序
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 ?#檢查時(shí)間間隔厕宗,默認(rèn)1分鐘
##### How many connections define a bad IP画舌? Indicate that below.
NO_OF_CONNECTIONS=150 ? #最大連接數(shù),超過這個(gè)數(shù)IP就會(huì)被屏蔽媳瞪,一般默認(rèn)即可
##### 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=0 ??#使用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,默認(rèn)即可
##### An email is sent to the following address when an IP is banned.
##### Blank would suppress sending of mails
EMAIL_TO=“root” ? #當(dāng)IP被屏蔽時(shí)給指定郵箱發(fā)送郵件,推薦使用乍丈,換成自己的郵箱即可(如果不希望發(fā)送郵件剂碴,設(shè)置為空)
##### Number of seconds the banned ip should remain in blacklist.
BAN_PERIOD=600 ?#禁用IP時(shí)間,默認(rèn)600秒轻专,可根據(jù)情況調(diào)整
用戶可根據(jù)給默認(rèn)配置文件加上的注釋提示內(nèi)容忆矛,修改配置文件。
?修改/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
ddos.sh --help顯示幫助催训,比如如果要即時(shí)干掉當(dāng)前超過N個(gè)連接的IP,使用sh ddos.sh -k 150, sh ddos.sh -c?創(chuàng)建cron job
用戶也可以用Web壓力測(cè)試軟件ab測(cè)試一下效果宗收,相信DDoS deflate還是能給你的VPS或服務(wù)器抵御一部分DDOS攻擊漫拭,給你的網(wǎng)站更多的保護(hù)。
ab命令安裝:#yum install httpd-tools ?-y
3混稽、卸載DDoS deflate
wget http://www.inetbase.com/scripts/ddos/uninstall.ddos
chmod 0700 uninstall.ddos
./uninstall.ddos
參考資料:http://www.myhack58.com/Article/48/66/2013/41214.htm
-----------------------------------------------------------------------------------------------------------
參考補(bǔ)充:
防范DDOS攻擊腳本:
#防止SYN攻擊 輕量級(jí)預(yù)防
iptables -N syn-flood
iptables -A INPUT -p tcp --syn -j syn-flood
iptables -I syn-flood -p tcp -m limit --limit3/s --limit-burst6-j RETURN
iptables -A syn-flood -j REJECT
#防止DOS太多連接進(jìn)來,可以允許外網(wǎng)網(wǎng)卡每個(gè)IP最多15個(gè)初始連接,超過的丟棄
iptables -A INPUT -i eth0 -p tcp --syn -m connlimit --connlimit-above15-j DROP
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
#用Iptables抵御DDOS (參數(shù)與上相同)
iptables -A INPUT? -p tcp --syn -m limit --limit12/s --limit-burst24-j ACCEPT
iptables -A FORWARD -p tcp --syn -m limit --limit1/s -j ACCEPT
iptables防DDOS攻擊腳本:
#!/bin/sh## define some vars
MAX_TOTAL_SYN_RECV="1000"MAX_PER_IP_SYN_RECV="20"MARK="SYN_RECV"PORT="80"LOGFILE="/var/log/netstat_$MARK-$PORT"LOGFILE_IP="/var/log/netstat_connect_ip.log"DROP_IP_LOG="/var/log/netstat_syn_drop_ip.log"## iptables default rules: accept normailly packages and drop baleful SYN* packages
iptables -F -t filter
iptables -A INPUT -p TCP ! --syn -m state --state NEW -j DROP
iptables -A INPUT -p ALL -m state --state INVALID -j DROP
iptables -A INPUT -p ALL -m state --state ESTABLISHED,RELATED -j ACCEPT
## initializeif[ -z $MARK ];thenMARK="LISTEN"fiif[ -z $PORT ];thenSPORT="tcp"elseSPORT=":$PORT"fi######################## end
## save the results of command netstat to specifal filenetstat -atun|grep$MARK|grep$SPORT2>/dev/null>$LOGFILE
REPEAT_CONNECT_IP=`less$LOGFILE|awk'{print $5}'|cut-f1 -d':'|sort|uniq-d |tee> $LOGFILE_IP`if[ -f $DROP_IP_LOG ];thenforiin`less$DROP_IP_LOG`;doiptables -A INPUT -p ALL -s $i -j DROPdonefiforiin`less$LOGFILE_IP`;doREPEAT_CONNECT_NUM=`grep$i $LOGFILE|wc-l`
## count repeat connections ,ifthe accout is large than default number,then drop packagesif[ $REPEAT_CONNECT_NUM -gt $MAX_PER_IP_SYN_RECV ];thenecho"$i####$REPEAT_CONNECT_NUM">> $DROP_IP_LOG
iptables -A INPUT -p ALL -s $i -j DROPfidoneALL_CONNECT=`uniq-u $LOGFILE|wc-l`
#echo $ALL_CONNECT
## count repeat connections ,ifthe accout is large than default number,then drop packagesif[ $ALL_CONNECT -gt $MAX_TOTAL_SYN_RECV ];then#echo $ALL_CONNECT
exitfi
原文:https://blog.csdn.net/weixin_33895695/article/details/85800117
? ? 有服務(wù)器需求請(qǐng)加QQ1911624872咨詢