in

How to Block Bots using Cloudflare Firewall? An In-Depth Guide for Taking Control of Your Bot Traffic

Bots are taking over the internet – automated scripts and crawlers account for more than 40% of all website traffic. While some bots are helpful, many waste bandwidth and resources and pose threats to security and performance. Implementing bot management is crucial for any website.

In this comprehensive 2000+ word guide, we‘ll do a deep dive on precisely controlling bots with Cloudflare Firewall. I‘ll draw on my 5+ years of experience as an infrastructure engineer to provide tons of technical details, data, tips, and best practices to help you gain granular control over the bots hitting your site.

The Bot Landscape – A Primer

Let‘s kick things off with an overview of the different types of bots crawling the modern web:

  • Search engine bots like Googlebot and Bingbot responsibly crawl the web to index sites for search engines. You‘ll typically want to allow these beneficial bots.

  • RSS/ATOM feed fetchers read sites‘ feed URLs to access updated content. Some are well-behaved but others can aggressively over-crawl.

  • Scrapers aim to extract and copy content from sites. Scrapers can range from aggregators playing by the rules to rogue actors stealing copyrighted content.

  • Spambots spam comment sections, forums, and guestbooks with links and ads. 41% of comment spam comes from bots according to Cloudflare.

  • Performance bots run tests to grade site speed and uptime. Well-intentioned but they can drain resources.

  • Impersonators masquerade as legitimate browsers but are actually bots trying to scrape data.

  • Hackers look for vulnerabilities and weaknesses like DDoS, XSS, and SQLi to exploit.

To quantify the prevalence of bots, studies show:

  • 36.5% of web traffic comes from bots according to Imperva.

  • The top 100 most aggressive bots account for 24% of requests according to Distil Networks.

  • 61% of bots exhibit malicious behavior according to DATADOGME.

Blocking the bad bots is crucial for security, performance, and cost optimization. But bot management requires care to avoid blocking helpful bots that provide SEO value. Cloudflare Firewall provides sophisticated tools to achieve this balance.

Cloudflare Firewall 101

Cloudflare Firewall gives you application-layer filtering to precisely control incoming traffic at Cloudflare‘s network edge. That means you can block bad bots before requests ever hit your origin server infrastructure.

Here are some key capabilities:

Flexible filtering options – Block by IP, IP range, ASN, geography, request header fields like user-agent, cookies, and much more.

Custom expressions – Write sophisticated logic in the firewall language to combine filters for advanced bot blocking.

Global deployment – Rules apply across Cloudflare‘s 250+ data centers without changing your origin config.

Instant change propagation – Changes deploy within seconds, allowing quick response to new threats.

Integration with other Cloudflare products – Firewall works alongside WAF, Rate Limiting, Bot Management and more for layered defense.

I especially appreciate how customizable and fast-deploying Cloudflare Firewall is compared to changing web server configs. Let‘s look at how to leverage these tools to block bad bots.

Step-by-Step Guide to Blocking Bots with Cloudflare Firewall

You have two options for creating bot blocking firewall rules – using the GUI or coding custom expressions:

Using the Intuitive Cloudflare Firewall GUI

  1. Log in to the Cloudflare dashboard, and navigate to Firewall > Firewall Rules. Click Create Firewall Rule.

  2. Give your rule a name like "Block Scraping Bots". Descriptive names help manage your ruleset.

  3. Select a field to filter on, like User-Agent for user agent blocking.

  4. Choose the operator, typically equals or contains for user agent rules.

  5. Enter the full or partial user agent string to block. For example Scrapy or python-requests.

  6. Optional – Add more filtered values using OR to block multiple user agents in one rule.

  7. Set the action to Block to discard matching traffic.

  8. Click Deploy and the rule will instantly go live on the Cloudflare network.

Repeat these steps to create additional rules, for example blocking countries, IP ranges, request methods, and more.

Coding Custom Firewall Expressions for Advanced Bot Logic

For maximum control, you can write custom firewall expressions like:

(http.user_agent contains "Scrapy") or (ip.src in {‘192.0.2.1‘ ‘192.0.2.2‘})

Expressions allow crafting sophisticated bot blocking behavior tuned to your site. I suggest reviewing Cloudflare‘s Firewall Rules Language to understand the syntax options.

Now let‘s explore some specific examples of firewall rules you can implement.

Useful Firewall Recipes for Blocking Bots

Cloudflare Firewall gives you countless options for bot management. Here are some recipes to apply for common use cases:

Block Scraping Bots by User Agent

Scraping bots like Scrapy and python-requests aggressively crawl sites to copy content. Block them by user agent:

(http.user_agent eq "Scrapy") or (http.user_agent contains "python-requests")

This is very effective against bots using common crawler libraries.

Block Spambots by IP Reputation

Many spambots have poor IP reputations. Use Cloudflare‘s Threat Score filter to block risky IPs:

(cf.threat_score > 0) and (ip.geoip.country eq "CN")

This stops traffic from China with non-zero threat scores. Adjust the country and threat score as needed.

Block Vulnerability Scanning Traffic

Many bots scan sites for weaknesses and misconfigurations. Block them via user agent:

(http.user_agent contains "NMAP") or (http.user_agent contains "sqlmap")

This prevents reconnaisance and probing by blocking popular scanning tools.

Block Unused HTTP Methods

Don‘t allow unused HTTP methods like PUT that only bots exploit:

http.request.method eq "PUT"

This is an easy hardening step to disable unneeded methods broadly.

Contain Overactive Feed Fetchers

Aggressive RSS/ATOM feed scrapers can over-crawl. Rate limit them:

http.user_agent contains "FeedFetcher" and rate_limit(10, 60)

This limits FeedFetcher to 10 requests per minute. Adjust as appropriate.

Block By Country

If certain countries are clearly abusive, block them:

ip.geoip.country eq "RU"

But be cautious about blanket country blocking which can impact real users.

There are Many More Options!

Nearly any attribute can be used for bot blocking rules:

  • ASN
  • IP address
  • Cookie values
  • Request rate
  • Headers like referer and origin
  • Request query patterns

Get creative in combining filters to block sneaky or evasive bots!

Bot Blocking Best Practices

When implementing firewall rules, keep these best practices in mind:

  • Test in simulate mode – This ensures you aren‘t accidentally blocking good bots before enforcing a rule.

  • Analyze bot signatures – Verify a bot is truly bad before blocking. Some user agents can represent multiple bot types.

  • Prefer specific rules – Block small IP ranges over huge ones when possible to limit collateral damage.

  • Check your firewall log – Review to confirm rules are blocking as expected without impacting legitimate traffic.

  • Spread rules across zones – Target bot blocking rules only to vulnerable hosts instead of network-wide.

  • Combine tools – Use Firewall in conjunction with WAF, Rate Limiting, Bot Management for layered defense.

Additional Strategies for Advanced Bot Protection

While firewall rules effectively block known bad bots, other techniques can further enhance protection:

  • Bot Management – Cloudflare‘s Bot Management utilizes machine learning to detect and mitigate highly sophisticated bots automagically.

  • CAPTCHAs – Challenge users with CAPTCHAs to filter bots without setting strict blocking rules.

  • IP Reputation – Change default Threat Score settings to filter traffic from risky IP ranges.

  • Rate Limiting – Throttle excessive requests instead of outright blocking. Helps limit scrapers.

  • Device Fingerprinting – Browser integrity checks help detect bots spoofing real visitors.

  • Monitor activity – Watch for patterns like repeated paths, failed logins, etc. that flag bot behavior.

  • Honeypots – Seed traps like fake login pages to catch spambots and scrapers.

Combining firewall rules with these other tactics provides layered bot defense tuned to your site‘s needs.

Closing Thoughts on Controlling Your Bot Traffic

Bots are a necessary evil of operating a site on the modern internet. While some provide value, many negatively impact security, performance, and costs if left unchecked.

Cloudflare Firewall provides a powerful set of tools to selectively filter and block bad bots crawling your site. Features like user agent blocking, IP reputation filters, and flexible custom expressions allow crafting granular rules tuned to your specific needs.

I suggest methodically analyzing bot access patterns, selectively blocking abusive bots via firewall policies, and combining rules with other mitigation strategies for defense in depth.

The bot landscape will only continue evolving, but with Cloudflare‘s Firewall capabilities you can stay in control. Here‘s to keeping your website running smoothly by filtering away the bots you don‘t want!

What bot blocking challenges have you faced? I‘d love to hear your experiences and feedback on securing your resources against bot abuse.

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.