How to Use Cloudflare Firewall Rules to Protect Your Web Application

Cloudflare-firewall-rules-banner

What is Cloudflare Firewall Rules?

Cloudflare Firewall Rules is another firewall tools offered by Cloudflare which announced in October 2018. It is a powerful and flexible security tool to filter web application traffic.

Cloudflare Firewall Rules is available for all Cloudflare plans. The free plan can have up to 5 active Firewall rules.

A Firewall Rule consists of two parts: Matching and Action.

  • Matching: A defined filter that runs and match your traffic for a string or pattern
  • Action: the action perform on the matched traffic (block, challenge, captcha, allow)

You can also order the firewall rules to override the default sequence which is based on the rule’s action.

Firewall Rules: Matching

You can match a traffic to the HTTP request, including country, hostname, IP address, URI, referrer, known bots, threat score, and more.

Known bots (cf.client.bot) is Cloudflare defined list of “known good bots”, which includes bots from Google, Apple, Bing, Linkedin, Pingdom, Yahoo… You are recommended to add cf.client.bot in an Allowed rule to avoid blocking good crawlers which could affect your SEO and monitoring.

Cloudflare has an internal algorithm to calculate an IP’s reputation and assigns a threat score (types of threats) that range from 0 to 100. Threat score is used for the Security Level setting under Firewall:

  • High – for scores greater than 0
  • Medium – for scores greater than 14
  • Low – for scores greater than 24
  • Essentially Off – for scores greater than 49

Regular Expression matching is supported for Cloudflare Business and Enterprise plans.

Firewall Rules: Action

You can set to perform an action to a filter matched traffic

  • Block: the traffic is block to reach your web application
  • JS Challenge: JavaScript challenge. Visitors do not have JavaScript support (mostly bots) will be blocked
  • Challenge (Captcha): Visitor is required to pass a captcha challenge to allow access
  • Allow: Traffic is allowed to reach your web application

Access Cloudflare Firewall Rules

In this article, we will show you how to setup Cloudflare Firewall Rules through dashboard. You can also configure Firewall Rules through API and Terraform.

  1. Login your Cloudflare dashboard
  2. Select the domain name you want to configure Firewall Rules
  3. Click Firewall from the tools at the top
  4. Click Firewall Rules
Cloudflare Firewall Rules panel
Cloudflare Firewall Rules panel

 

From there, you can:

  • Create a new Firewall Rule
  • Search and filter the list of existing rules
  • See a list of existing rules (active and paused)
  • Activate or pause rules (turn on or off)
  • Edit a rule
  • Delete a rule

Cloudflare Firewall Rules Examples

Example 1 — Block all countries except Malaysia

Cloudflare Firewall Rules: Block countries except Malaysia
Cloudflare Firewall Rules: Block countries except Malaysia

 

Expression Editor:

(ip.geoip.country ne "MY")

You can easily do country blocking using Cloudflare Firewall Rules.

Tip: Change the operator to equal to block only the chosen country in Value field. You can add more countries by clicking “OR” button.

Example 2 — WordPress Security

Cloudflare Firewall Rules: WordPress Security
Cloudflare Firewall Rules: WordPress Security

 

Expression Editor:

((http.request.uri.path contains "/xmlrpc.php") or (http.request.uri.path contains "/wp-login.php") or (http.request.uri.path contains "/wp-admin/" and not http.request.uri.path contains "/wp-admin/admin-ajax.php" and not http.request.uri.path contains " /wp-admin/theme-editor.php")) and ip.geoip.country ne "MY"

This Firewall Rule will challenge any non-Malaysia visitor that try to access WordPress xmlrpc.php, wp-login.php, and /wp-admin (except admin-ajax.php and theme-editor.php). A simple rule like this could block most hack attempts to your WordPress website.

You notice that no visual builder for this rule. It is because I use a complex (nested brackets) expression.

Example 3 — Block bad bots (use Expression Editor)

Animation: Create Cloudflare Firewall Rule using Expression Editor
Animation: Create Cloudflare Firewall Rule using Expression Editor

 

Expression Editor:

(http.user_agent contains "Yandex") or (http.user_agent contains "muckrack") or (http.user_agent contains "Qwantify") or (http.user_agent contains "Sogou") or (http.user_agent contains "BUbiNG") or (http.user_agent contains "knowledge") or (http.user_agent contains "CFNetwork") or (http.user_agent contains "Scrapy") or (http.user_agent contains "SemrushBot") or (http.user_agent contains "AhrefsBot") or (http.user_agent contains "Baiduspider") or (http.user_agent contains "python-requests") or (http.user_agent contains "crawl" and not cf.client.bot) or (http.user_agent contains "Crawl" and not cf.client.bot) or (http.user_agent contains "bot" and not http.user_agent contains "Google" and not http.user_agent contains "Twitter" and not cf.client.bot) or (http.user_agent contains "Bot" and not http.user_agent contains "Google" and not cf.client.bot) or (http.user_agent contains "Spider" and not cf.client.bot) or (http.user_agent contains "spider" and not cf.client.bot)

Okay… This is a long bots blocking rule. It blocks any non-known-good-bots traffic with user agent that contain strings “crawl”, “bot”, “spider”, plus a few custom user agents.

Here we add the Firewall rule using the Expression Editor (shown in animated GIF above):

  1. Click “Create a Firewall Rule
  2. Give a Rule Name
  3. Click “Edit expression
  4. Copy & Paste the expression into the text area
  5. Select “Block” action
  6. Click “Deploy” to activate the Firewall rule

You can use Visual Builder to update a Firewall rule that is created using Expression Editor, provided you do not use nested brackets in the expression.

The same expression can be rewrite using nested bracket, grouping rule logics in single ():

(http.user_agent contains "Yandex") 
or (http.user_agent contains "muckrack") 
or (http.user_agent contains "Qwantify") 
or (http.user_agent contains "Sogou") 
or (http.user_agent contains "BUbiNG") 
or (http.user_agent contains "knowledge") 
or (http.user_agent contains "CFNetwork") 
or (http.user_agent contains "Scrapy") 
or (http.user_agent contains "SemrushBot") 
or (http.user_agent contains "AhrefsBot") 
or (http.user_agent contains "Baiduspider") 
or (http.user_agent contains "python-requests") 
or ((http.user_agent contains "crawl") 
or (http.user_agent contains "Crawl") 
or (http.user_agent contains "bot" and not http.user_agent contains "Google" and not http.user_agent contains "Twitter") 
or (http.user_agent contains "Bot" and not http.user_agent contains "Google") 
or (http.user_agent contains "Spider") 
or (http.user_agent contains "spider") 
and not cf.client.bot)

Does Your Cloudflare Firewall Rules work?

You have configure and active your Firewall rules. Do they work as expected? You can check the Firewall Event Log (Firewall > Events) for the list of firewall events (allow, challenge, block) and their details.

Take note on the challenged and blocked events. You do not want to mistakenly blocking good traffic because of a wrongly configured Firewall rule.

Firewall Event Log
Cloudflare Firewall event log

 

In this example, Firewall Rule blocks “magpie-crawler” bot with IP address “185.25.35.15” (United Kingdom) when it tries to access a post’s feed.

Summary

In this tutorial, you have learned what is Cloudflare Firewall Rules and how to configure it to filter traffic and protect your web application. You also learned about Expression Editor for writing complex firewall rule.

I hope you find this tutorial helpful. If you have any question, please add in the comment below.

How do you use Cloudflare Firewall Rules?

Sources:

Share This On
Share on facebook
Share on twitter
Share on linkedin
Share on reddit

Leave a Comment

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

Article You Might Interested In