Cloudflare security rules
Total Time Required
15 minutes
Difficulty
Easy
Required Knowledge
Crowdsec, Cloudflare
Prerequisites
Crowdsec account
I recommend setting up a unique stack per domain you manage. This reduces the impact if the API key is leaked.
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
Browse to https://dash.cloudflare.com/
Next to your name, click on the 3 dots and select Copy Account ID
Save to your notepad,
CF_ACCOUNTID=
Get your Zone ID
Click manage next to your domain
Scroll down and locate "API" on the right
Save your Zone ID to your notepad,
CF_ZONE_ID=
Generate an API key
Navigate here https://dash.cloudflare.com/profile/api-tokens
Click on Create token > custom token
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
Account resources
FieldDataInclude
All Accounts
Zone resources
Include
Specific Zone
Your Domain
Click on continue to summary
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
Navigate to https://dash.cloudflare.com/
Select your domain
On the left, click expand Security and select rules
Click on create rule > custom rule
Next to Expression Preview, click on 'edit expression' to get the free text field
Create a rule for each of the below
Block bots
This policy will show a Captcha challenge to any IPs suspected of botting
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
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
Rule Name
Challenge Threat Score
Expression
(cf.threat_score gt 50)
Choose action
Block
Place at
Custom - after 'Challenge Threat Score'
Docker Compose
Fill out the below env using your notes
Last updated