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)文章
Linux下rsync(本地、遠(yuǎn)程)文件同步方式
這篇文章主要介紹了Linux下rsync(本地、遠(yuǎn)程)文件同步方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01在Linux中如何列出和刪除Iptables防火墻規(guī)則
當(dāng)涉及到網(wǎng)絡(luò)安全時(shí),防火墻是一個(gè)至關(guān)重要的組成部分,Iptables是一種常用的防火墻解決方案,可以幫助管理員保護(hù)他們的系統(tǒng)免受惡意攻擊,本文將詳細(xì)介紹如何列出和刪除Iptables防火墻規(guī)則,以幫助您更好地管理系統(tǒng)的安全性2023-06-06Virtualbox安裝Lubuntu 18.04 64位的圖文教程
這篇文章主要為大家詳細(xì)介紹了Virtualbox安裝Lubuntu 18.04 64位的圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07基于cobbler 實(shí)現(xiàn)自動(dòng)安裝linux系統(tǒng)
這篇文章主要介紹了cobbler 自動(dòng)安裝linux系統(tǒng)的教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11Linux下查詢外網(wǎng)IP的幾種方法總結(jié)
這篇文章主要介紹了Linux下查詢外網(wǎng)IP的幾種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03Ubuntu 18.04 安裝MySQL時(shí)未提示輸入密碼的問題及解決方法
這篇文章主要介紹了Ubuntu 18.04 安裝MySQL時(shí)未提示輸入密碼的問題及解決方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10