=====API=====
**Get zones**
curl -s -X GET -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" https://api.cloudflare.com/client/v4/zones
**Purge Cache**
curl -s -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" https://api.cloudflare.com/client/v4/zones/$ZONE/purge_cache --data '{"purge_everything":true}'
=====Scripts=====
// CloudFront > Functions > Create function
// CloudFront > Functions > FunctionName > Publish
// CloudFront > Functions > FunctionName > Add association (Viewer request)
//
// echo -n 'user:password' | base64
// dXNlcjpwYXNzd29yZA==
function handler(event) {
var authHeaders = event.request.headers.authorization;
var expected = "Basic dXNlcjpwYXNzd29yZA==";
if (authHeaders && authHeaders.value === expected) {
return event.request;
}
var response = {
statusCode: 401,
statusDescription: "Unauthorized",
headers: {
"www-authenticate": {
value: 'Basic realm="Enter credentials for this super secure site"',
},
},
};
return response;
}
=====Fail2Ban Integration=====
**Filter**
[Definition]
failregex = ^ \- \S+ \[\] "POST \/api\/login HTTP/\d+\.\d+" 200 .+$
ignoreregex =
**Action**
[Definition]
actionstart =
actionstop =
actioncheck =
actionban = curl -s \
-H "X-Auth-Email: " \
-H "X-Auth-Key: " \
-H "Content-Type: application/json" \
-X POST "https://api.cloudflare.com/client/v4/zones/<cf_zoneid>/firewall/access_rules/rules" \
--data '{"mode":"block","configuration":{"target":"ip","value":""},"notes":"Fail2ban"}'
actionunban = curl -s \
-H "X-Auth-Email: " \
-H "X-Auth-Key: " \
-H "Content-Type: application/json" \
-X DELETE "https://api.cloudflare.com/client/v4/zones/<cf_zoneid>/firewall/access_rules/rules/"$( \
curl -s -H "X-Auth-Email: " \
-H "X-Auth-Key: " \
-H "Content-Type: application/json" \
-X GET "https://api.cloudflare.com/client/v4/zones/<cf_zoneid>/firewall/access_rules/rules?mode=block&configuration_target=ip&configuration_value=<ip>&page=1&per_page=1&match=all" \
|jq --raw-output '.result[] .id')
[Init]
**Jail**
[nginx-custom]
enabled = true
filter = custom-nginx-login
action = cloudflare[cf_email="EMAIL", cf_token="TOKEN", cf_zoneid="ZONEID"]
logpath = /var/log/nginx/access.log
findtime = 600
bantime = 3600
maxretry = 50