宝塔面板6.X-shell脚本,自动切换5秒盾脚本防CC攻击

大鸟前一篇文章讲了6.X-shell脚本,自动拉黑恶意IP到Cloudflare防火墙。如果我们的网站遭遇CC和DDoS攻击时,我们可以用这个方法来简单的防御。

不过,Cloudflare还有一个很经典的功能就是Cloudflare经典的5秒盾防攻击,我们可以和上一篇文章结合起来看,当我们自动拉黑恶意IP到Cloudflare防火墙的时候,可以根据系统的负载状态来实现自动开启5秒盾。但是我们要通过Cloudflare API启用这个功能。

这篇文章大鸟来说说,如何通过Cloudflare API自动开启Cloudflare经典的5秒盾。

一:脚本设置

源码地址:https://github.com/Machou/Cloudflare-Block

当你的服务器受到攻击时,系统负载就会爆增,利用脚本自动检测系统负载,当压力超过一定的值时就可以切换为” I’m Under Attack! “模式了。操作步骤如下:

#下载
cd /root && git clone https://github.com/Machou/Cloudflare-Block.git DDoS
#打开Cloudflare.sh,修改配置
API_KEY      You're Global API Key (https://dash.cloudflare.com/profile)
MAIL_ACCOUNT    Email of your Cloudflare account
DOMAIN      Zone ID (https://dash.cloudflare.com/_zone-id_/domain.com)
#设置定时任务
crontab -e
*/1 * * * * /root/DDoS/Cloudflare.sh 0 # check every 1 minute if protection is not enabled
*/20 * * * * /root/DDoS/Cloudflare.sh 1 # check every 20 minutes if protection is enabled

1.1完整源代码

脚本默认的是检测系统负载为10,启动” I’m Under Attack! “模式,你以根据需要来调整。脚本如下:

#!/bin/bash


# $1 = 1min, $2 = 5min, $3 = 15min
loadavg=$(cat /proc/loadavg|awk '{printf "%f", $1}')


# load is 10, you can modify this if you want load more than 10
maxload=10


# Configuration API Cloudflare
# You're Global API Key (https://dash.cloudflare.com/profile)
api_key=
# Email of your account Cloudflare
email=
# Zone ID (https://dash.cloudflare.com/_zone-id_/domain.com)
zone_id=     


# create file attacking if doesn't exist
if [ ! -e $attacking ]; then
	echo 0 > $attacking
fi

attacking=./attacking


hasattack=$(cat $attacking)


if [ $(echo "$loadavg > $maxload"|bc) -eq 1 ]; then

	if [[ $hasattack = 0 && $1 = 0 ]]; then

		# Active protection
		echo 1 > $attacking
		curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$zone_id/settings/security_level" \
						-H "X-Auth-Email: $email" \
						-H "X-Auth-Key: $api_key" \
						-H "Content-Type: application/json" \
						--data '{"value":"under_attack"}'
	fi

	else
		if [[ $hasattack = 1 && $1 = 1 ]]; then

		# Disable Protection
		echo 0 > $attacking
		curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$zone_id/settings/security_level" \
						-H "X-Auth-Email: $email" \
						-H "X-Auth-Key: $api_key" \
						-H "Content-Type: application/json" \
						--data '{"value":"high"}'
	fi
fi

exit 0

二:宝塔面板计划任务

我们如果想省点事就直接复制上面第一条中的脚本代码,放在计划任务的shell脚本之中,也可以是同样的效果。

宝塔面板6.X-shell脚本,自动切换5秒盾脚本防CC攻击

这里的设置很简单,大鸟也说过很多次了,就不多做赘述了。

三:一些问题解决

这个脚本配置好之后,如果我们测试会出现一些问题。

3.1,bc: command not found

竟然报  “bc: command not found ”  ,大鸟是第一次见到这个。

于是搜索了下,原来还真有这个命令,不过不知道编译内核需要这个做什么。这个东西估计用的人也很少,不过如果在命令行下要计算数也是个不错的工具吧。

yum -y install bc 一下就可以解决了。如图:

宝塔面板6.X-shell脚本,自动切换5秒盾脚本防CC攻击

3.2./attacking文件不存在

./attacking文件不存在这个问题,如果遇到可以把把脚本代码中26行的attacking=./attacking剪切到20行,应该可行,不过大鸟没遇到这问题。

四:总结

这个也就是个思路,如果动手能力强的同学,可以自己写个脚本来调用,大鸟这里是借用别人的方法来实现这个思路的,可以说,Cloudflare是一个非常好用的防御DDos和CC攻击的工具,免费版本的Cloudflare结合API可以实现更加灵活的功能,对于普通的防御足够自己使用了。

 


【AD】DMIT美国洛杉矶Pro VPS套餐,$14.9/月起,1G-10Gbps带宽/CN2 GIA线路/有高防GIA可选
【AD】美国洛杉矶CN2 VPS/香港CN2 VPS/日本CN2 VPS推荐,延迟低、稳定性高、免费备份_搬瓦工vps