华岁呀Huasui

华岁呀Huasui

Cloudflare自动开关5秒盾

介绍#

一般我的站点都是采用国内线路+境外线路解析,默认线路为国内 CDN,境外则 cname 解析到 cloudflare 线路
CNAME 解析到 cloud flare 上可以参考我的文章 使用 CNAME 接入 CloudFlare

这样以来可以阻挡很多来自境外的攻击(前提是没有暴露源站 IP)不过华岁最近刚了解到可以 “穿盾” 什么的,貌似是把 CF 盾打穿再攻击?,实在令人恶心啊

CLoudflare 最有效的 DDoS 防御模式就是 “Under Attack 模式”,这个就是今天要说的 5 秒盾
Under Attack
开启之后在访问网站的时候会先跳转的 cloudflare 的检测页面上,用 5 秒时间来验证是否真实用户访问,可以有效缓解攻击带来的压力
如果一直开启的话会影响访问,所以今天就分享自动开关脚本。

开源的 sh 脚本#

开源的 sh 脚本 Cloudflare-Block
可以实现在 Linux 服务器端根据系统负载情况自动开启 CloudFlare 的五秒盾进行被攻击后的防御。

API 准备#

注册 / 登录 cloud flare 查看
API
1
APIkey
zoneid 区域 ID
点进接入在 CLOUDFLARE 的域名,在右侧下滑即可看到
zoneid
保存好上面两个 API,脚本需要用到。

脚本使用#

只需要复制下面的代码,保存为 Cloudflare.sh 文件再添加到 crontab 里定时执行脚本即可。(当然也可以使用宝塔定时任务)

#!/bin/bash

# $1 = 1min, $2 = 5min, $3 = 15min,这里选择使用哪个负载值为阈值。
loadavg=$(awk '{printf "%f", $1}' < /proc/loadavg)

# Linux 系统负载(load average)达到 10 即开启五秒盾,您可以根据情况来修改这个数值。
maxload=10

# 配置 Cloudflare 的 API
# 您 CloudFlare 的 Global API Key (https://dash.cloudflare.com/profile)
api_key=
# 您 CloudFlare 账号邮箱
email=
# 您 CloudFlare 账号的**区域 ID** (https://dash.cloudflare.com/_zone-id_/domain.com)
zone_id=
# 没有攻击时 CloudFlare 的默认安全级别
default_security_level=high
# 是否将调试消息写入脚本目录下的 debug.log 文件
debug=0

basedir=$(dirname "$0")

attacked_file=$basedir/attacked

[ "$debug" -eq 1 ] && exec > "${logfile:-$basedir/debug.log}"


# 您可以就地放置上述配置值,也可以将其放置在脚本目录中名为“config”的文件中。
config_file=$basedir/config
[ -e "$config_file" ] && source "$config_file"

api_set_mode() {
    local mode
    mode=$1
    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\":\"$mode\"}" \
    || echo "Error: failed to set security level to $mode"
}

# 如果不存在则创建文件“attacked”
if [ ! -e "$attacked_file" ]; then
    echo 0 > "$attacked_file"
fi

was_under_attack=$(cat "$attacked_file")
under_attack=$(echo "$loadavg > $maxload" | bc)

if [[ "$1" != [01] ]]; then
    echo "Incorrect usage! Please pass either 0 or 1 as an argument"
    exit 1
fi

if [ $debug -eq 1 ]; then
    echo "Mode: $1; was under attack: $was_under_attack; now under attack: $under_attack"
    echo "Load average: $loadavg"
fi

if [ "$1" -eq 0 ] && [ "$was_under_attack" -eq 0 ] && [ "$under_attack" -eq 1 ]; then
    # attack just started and we want to enable under-attack mode

    # Activate protection
    [ "$debug" -eq 1 ] && echo "Activating under-attack mode!"
    echo 1 > "$attacked_file"
    api_set_mode under_attack

elif [ "$1" -eq 1 ] && [ "$was_under_attack" -eq 1 ] && [ "$under_attack" -eq 0 ]; then
    # attack just finished (and up to 20 minutes passed since) 
    # and we want to disable under-attack mode

    # Disable Protection
    [ "$debug" -eq 1 ] && echo "Leaving under-attack mode!"
    echo 0 > "$attacked_file"
    api_set_mode "$default_security_level"

fi

exit 0

添加定时任务#

#添加定时任务
crontab -e
# 如果未启用五秒盾保护,则每 1 分钟检查一次
*/1 * * * * /root/DDoS/Cloudflare.sh 0
 # 每 20 分钟检查一次五秒盾保护是否已启用
*/20 * * * * /root/DDoS/Cloudflare.sh 1 
#重载 crontab
service crond reload

总结#

开启自动 5s 盾后,可以在受到攻击时自动开启,不用自己去手动开启,还是很方便的,懒人必备啊!

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。