Linux服務(wù)器 屏蔽國(guó)外IP訪問(wèn)及簡(jiǎn)單的防CC攻擊攔截屏蔽國(guó)外IP訪問(wèn) 通過(guò)ssh遠(yuǎn)程登錄服務(wù)器內(nèi),運(yùn)行如下命令語(yǔ)句獲取國(guó)內(nèi)IP網(wǎng)段,會(huì)保存為/root/china_ssr.txt wget -q --timeout=60 -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > /root/china_ssr.txt將下面腳本保存為/root/allcn.sh ,并設(shè)置可執(zhí)行權(quán)限( 命令: chmod +x allcn.sh) mmode=$1
CNIP="/root/china_ssr.txt"
gen_iplist() {
cat <<-EOF
$(cat ${CNIP:=/dev/null} 2>/dev/null)
EOF
}
flush_r() {
iptables -F ALLCNRULE 2>/dev/null
iptables -D INPUT -p tcp -j ALLCNRULE 2>/dev/null
iptables -X ALLCNRULE 2>/dev/null
ipset -X allcn 2>/dev/null
}
mstart() {
ipset create allcn hash:net 2>/dev/null
ipset -! -R <<-EOF
$(gen_iplist | sed -e "s/^/add allcn /")
EOF
iptables -N ALLCNRULE
iptables -I INPUT -p tcp -j ALLCNRULE
iptables -A ALLCNRULE -s 127.0.0.0/8 -j RETURN
iptables -A ALLCNRULE -s 169.254.0.0/16 -j RETURN
iptables -A ALLCNRULE -s 224.0.0.0/4 -j RETURN
iptables -A ALLCNRULE -s 255.255.255.255 -j RETURN
iptables -A ALLCNRULE -m set --match-set allcn src -j RETURN
iptables -A ALLCNRULE -p tcp -j DROP
}
if [ "$mmode" == "stop" ] ;then
flush_r
exit 0
fi
flush_r
sleep 1
mstart執(zhí)行如下命令將開(kāi)始攔截 /root/allcn.sh 執(zhí)行如下命令即可停止攔截 /root/allcn.sh stop CC攻擊攔截 方式1:通過(guò)netstat -an命令統(tǒng)計(jì)出當(dāng)前請(qǐng)求并發(fā)大于100的IP,然后將不在白名單的IP自動(dòng)加入DROP規(guī)則 首先運(yùn)行 vi deny_1.sh 添加以下命令語(yǔ)句 #!/bin/bash
if [[ -z $1 ]];then
num=100
else
num=$1
fi
cd $(cd $(dirname $BASH_SOURCE) && pwd)
iplist=`netstat -an |grep ^tcp.*:80|egrep -v 'LISTEN|127.0.0.1'|awk -F"[ ]+|[:]" '{print $6}'|sort|uniq -c|sort -rn|awk -v str=$num '{if ($1>str){print $2} fi}'`
if [[ ! -z $iplist ]];
then
for black_ip in $iplist
do
ip_section=`echo $black_ip | awk -F"." '{print $1"."$2"."$3}'`
grep -q $ip_section ./white_ip.txt
if [[ $? -eq 0 ]];then
echo $black_ip >>./recheck_ip.txt
else
iptables -nL | grep $black_ip || iptables -I INPUT -s $black_ip -j DROP
echo $black_ip >>./black_ip.txt
fi
done
fi保存后執(zhí)行以下語(yǔ)句: chmod +x deny_1.sh sh deny_1.sh 攔截的IP會(huì)記錄到black_ip.txt中,如果有要排除的白名單IP,可將這些IP加入到white_ip.txt,一行一個(gè)。 方式2:通過(guò)web網(wǎng)站日志中攻擊者訪問(wèn)特征,根據(jù)這些特征過(guò)濾出攻擊的ip,利用iptables來(lái)阻止(排除本機(jī)IP:127.0.0.1)。 首先運(yùn)行 vi deny_2.sh 添加以下命令語(yǔ)句: #!/bin/bash
OLD_IFS=$IFS
IFS=$'\n'
for status in `cat 網(wǎng)站訪問(wèn)日志路徑 | grep '特征字符' | grep -v '127.0.0.1' | awk '{print $1}' |sort -n | uniq -c | sort -n -r | head -20`
do
IFS=$OLD_IFS
NUM=`echo $status | awk '{print $1}'`
IP=`echo $status | awk '{print $2}'`
if [ -z "`iptables -nvL | grep "dpt:80" | awk '{print $8}' | grep "$IP"`" ];then
if [ $NUM -gt 250 ];then
/sbin/iptables -I INPUT -p tcp -s $IP --dport 80 -j DROP
fi
fi
done保存后執(zhí)行以下語(yǔ)句: chmod +x deny_2.sh sh deny_2.sh 最后使用crontab -e 添加到任務(wù)計(jì)劃,每20分鐘執(zhí)行一次: */20 * * * * /root/deny_ip1.sh >dev/null 2>&1 注意:
|
|||||
|
若文檔內(nèi)容對(duì)您沒(méi)有幫助,不能解決問(wèn)題?
您還可以
咨詢?cè)诰客服
或 提交工單
或 搜索常見(jiàn)問(wèn)題
,我們將竭誠(chéng)為您服務(wù)。
|
|||||
| >> 相關(guān)文章 | |||||
|
|
|||||
400-028-5800
028-62778877
您好,非正常上班時(shí)間若有緊急技術(shù)問(wèn)題,請(qǐng)撥總機(jī)后按7號(hào)鍵, 其他問(wèn)題請(qǐng)?zhí)峤还位蛟谏习鄷r(shí)間聯(lián)系,謝謝支持!