Cloudflare security rules

Total Time Required

15 minutes

Difficulty

Easy

Required Knowledge

Crowdsec, Cloudflare

Drawing

Prerequisites

circle-exclamation

This stack monitors...

  • Crowdsec blocklists

... And changes

  • Cloudflare security rules

Cloudflare

Generate your API key, get your details and create your security rules

Get your Account ID

  1. Next to your name, click on the 3 dots and select Copy Account ID

  2. Save to your notepad, CF_ACCOUNTID=

Get your Zone ID

  1. Click manage next to your domain

  2. Scroll down and locate "API" on the right

  3. Save your Zone ID to your notepad, CF_ZONE_ID=

Generate an API key

  1. Click on Create token > custom token

    1. Give your token a name and fill out the below permissions

      Account

      Account Filter Lists

      Edit

      Account

      Firewall Access Rules

      Edit

      Zone

      Zone

      Read

      Zone

      Firewall Services

      Edit

    2. Account resources

      Field
      Data

      Include

      All Accounts

    3. Zone resources

      Include

      Specific Zone

      Your Domain

    4. Click on continue to summary

  2. Save your API key to your notepad, CF_APITOKEN=

Set your Security rules

Configure some security rules to reduce the risk of malicious actors accessing your domain

  1. Select your domain

  2. On the left, click expand Security and select rules

  3. Click on create rule > custom rule

    1. Next to Expression Preview, click on 'edit expression' to get the free text field

  4. Create a rule for each of the below

Block bots

This policy will show a Captcha challenge to any IPs suspected of botting

Field
Data

Rule Name

Block Bots

Expression

(cf.client.bot)

Choose action

Managed Challenge

Place at

First

Challenge Threat Score

These IPs are potentially malicious. These addresses will be prompted for Captcha

Field
Data

Rule Name

Challenge Threat Score

Expression

(cf.threat_score gt 10)

Choose action

Managed Challenge

Place at

Custom - after 'Block Bots'

Block Threat Score

These IPs are very likely to be malicious. These addresses will be blocked

Field
Data

Rule Name

Challenge Threat Score

Expression

(cf.threat_score gt 50)

Choose action

Block

Place at

Custom - after 'Challenge Threat Score'

circle-info

An additional rule will be created by the Crowdsec CF Bouncer container after the Compose file is ran

Docker Compose

Fill out the below env using your notes

https://github.com/trentnbauer/HomelabPublic/blob/main/docker-compose/crowdsec-cloudflare.yml

Last updated