Set Cloudflare WAF rules with Fail2Ban actions

Cloudflare offers API access to it’s WAF IP access rules, which can be a great tool to ban bots on a “edge” level, reducing load on each instance. So once a server bans an IP for repeatedly failing auth, it will be blocked on the CF level, unable to bother other servers.

My old script has stopped working, apparently Cloudflare have changed their APIs. Luckily there’s an official fail2ban action now, grab it directly from their Github: https://github.com/fail2ban/fail2ban/blob/master/config/action.d/cloudflare.conf

Add it to the actions.d folder, configure the cftoken und cfuser variables with your own global Cloudflare API token (My Profile -> API Tokens -> Global API Key)

Now you just need to add the action to your jail configuration like this:

I mainly use it for my repeatoffeners jail, which is set to monitor fail2ban logs and ban those who have been banned multiple times within the last 30 days, this can look like this:

Now let’s test the new setup, first a manual fail2ban ban:

Check the Tools tab in Cloudflare’s WAF setting to make sure it worked:

now test the unban command:

IP address should now disappear from Cloudflare IP Access Rules.

Leave a comment

Your email address will not be published. Required fields are marked *