脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - shell - 一个简单的防CC攻击Shell脚本分享

一个简单的防CC攻击Shell脚本分享

2023-02-10 18:36shell教程网 shell

这篇文章主要介绍了一个简单的防CC攻击Shell脚本分享,主要原理是分析apache或者nginx的访问日志,对大量访问的IP加入iptables进行禁止访问,需要的朋友可以参考下

实现代码:

复制代码 代码如下:
#!/bin/sh
cd /var/log/httpd/
cat access_log|awk '{print $1}'|sort|uniq -c|sort -n -r|head -n 20 > a
cp /dev/null access_log
cp /dev/null error_log
cp /dev/null limit.sh
cp /dev/null c
#awk '{print $2}' a|awk -F. '{print $1"."$2"."$3}'|sort|uniq > b
cat a|while read num ip
do
if [ "$num" -gt "20" ]
then
echo $ip >> c
fi
done
cat c|awk -F. '{print $1"."$2"."$3}'|sort|uniq > b
#cat c|sort|uniq > b
for i in `cat b`
#cat b|sed 's/\./ /g'|while read i1 i2 i3 i4
do
if `cat ourip |grep $i > /dev/null 2>&1`
then
echo "`date` $i" >> test
else
echo "iptables -I INPUT -p tcp -dport 80 -s $i.0/24 -j DROP" >> limit.sh
fi
done

使用方法,运行命令:sh limit.sh,这样就会把访问量过高的IP加入防火墙,并禁止访问

延伸 · 阅读

精彩推荐