centos redhat系列對(duì)抗ddos之居家必備利器 banip.txt
更新時(shí)間:2010年11月28日 18:51:35 作者:
本文可以用于redhat centos 系列 linux 系統(tǒng)的 屏蔽多連接ip,具有抗ddos功能的代碼。
復(fù)制代碼 代碼如下:
##############################################
#version="20100718"
#author="phpsir"
#author_email="phpsir@phpsir.com"
##############################################
maxnum=50
runmin_max=300
#runmin_max is clear time
ipfile="/tmp/80link.txt"
banip_data_file="/root/banip_data.txt"
ipopenfile="/root/openip.txt"
nsfile="/tmp/netstat80.txt"
myip=`/sbin/ifconfig eth0 | grep inet | awk '{print $2}' | sed 's/addr://' | grep .`
if [ ! -f $ipopenfile ]
then
echo "init $ipopenfile"
touch $ipopenfile
fi
if [ -f $banip_data_file ]
then
source $banip_data_file
echo "last runtime=$runtime"
else
echo "init $banip_data_file"
echo 'runtime='`date "+%s"` > $banip_data_file
source $banip_data_file
runmin_max=-1
fi
echo "start shell" `date "+%Y-%m-%d %H:%M:%S"`
runmin=$((`date "+%s"`-$runtime))
if [ $runmin -gt $runmin_max ]
then
echo $runmin "is bigger than " $runmin_max
echo "clear ips"
/sbin/iptables -F
echo 'runtime='`date "+%s"` > $banip_data_file
else
echo $runmin "is lowwer than " $runmin_max
fi
netstat -an | grep "$myip:80" > $nsfile
echo "Total Links = " `cat $nsfile | wc -l `
echo "Total Links ESTABLISHED = " `cat $nsfile | grep ESTABLISHED | wc -l `
echo "Total Links SYNC = " `cat $nsfile | grep SYN | wc -l `
cat $nsfile | awk '{print $5}' | awk -F: '{print $1}' | sort|uniq -c|sort -rn | head -n 10 > $ipfile
cat $ipfile | while read oneline
do
ip=`echo $oneline | cut -d " " -f 2`
num=`echo $oneline | cut -d " " -f 1`
str="$ip has linked $num "
banme="yes"
for allowip in `cat $ipopenfile`
do
echo $ip | grep $allowip > /dev/null
if [ $? -eq 0 ]
then
banme="no"
echo $allowip "banme = " $banme
/sbin/iptables -D INPUT -p tcp -s $ip -d $myip --dport 80 -j REJECT > /dev/null 2>&1
continue
fi
done
if [ $banme = "yes" ]
then
if [ $num -gt "$maxnum" ]
then
/sbin/iptables -L -n | grep "$ip" >/dev/null
status=`echo $?`
if [ $status -eq 1 ]
then
echo "deny $ip ,because $str "
/sbin/iptables -A INPUT -p tcp -s $ip -d $myip --dport 80 -j REJECT
echo "BAN " $ip " OK "
#/sbin/iptables -L -n | grep "$ip"
else
echo > /dev/null
#echo "$str alread reject"
fi
else
echo > /dev/null
#echo "$str $ip ok ,less $maxnum "
fi
fi
done
echo "stop shell" `date "+%Y-%m-%d %H:%M:%S"`
相關(guān)文章
CentOS上搭建Nginx+Mono運(yùn)行asp.net環(huán)境的配置方法
這篇文章主要介紹了CentOS上搭建Nginx+Mono運(yùn)行asp.net環(huán)境的配置方法,需要的朋友可以參考下2017-03-03
關(guān)于bash函數(shù)你可能不知道的一些事情(譯)
這篇文章主要給大家介紹了關(guān)于bash函數(shù)你可能不知道的一些事情,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Linux服務(wù)器配置ip白名單防止遠(yuǎn)程登錄以及端口暴露的問題
今天小編就為大家分享一篇Linux服務(wù)器配置ip白名單防止遠(yuǎn)程登錄以及端口暴露的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07
CentOS7 LNMP+phpmyadmin環(huán)境搭建 第三篇phpmyadmin安裝
這篇文章主要介紹了CentOS7 LNMP+phpmyadmin環(huán)境搭建,第三篇phpmyadmin安裝,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

