Cookie Settings

Choose which cookies to allow.

Full list in our Privacy Policy.

Cloudflare WAF Block Country: How to Block a Country or IP Range Without Blocking Real Visitors

A custom rule on one country lets verified bots and partner addresses through and challenges the rest of that country's requests

A Cloudflare WAF block country rule is a custom rule that matches the visitor’s country, such as ip.src.country in {"KP"}, with the Block action, and custom rules work on every plan. It refuses every request whose IP address Cloudflare places in that country: customers, search engine crawlers, partners calling your API. We read the 10 third-party guides that came up next to Cloudflare’s docs when we searched for this on December 1, 2026: none looks at how many requests the country sends before blocking it, and two keep verified bots out of the rule. Ask Lelops, the chat in Lelops Advise, creates the rule with its exceptions, saves each change as a version and lists the zone’s latest security events, where the rule’s own carry its ID.

How the Cloudflare WAF Block Country Rule Works

The rule has an expression on the country field and an action. In the dashboard, open Security rules, select Create rule > Custom rules, set Field to Country and Operator to is in, pick the countries, choose the action, then select Deploy (Create a custom rule).

The field is ip.src.country, the two-letter ISO code of the visitor’s IP address; ip.geoip.country, still found in older examples, is deprecated but keeps working (ip.src.country). Tor traffic has its own code in the continent field: ip.src.continent eq "T1" (ip.src.continent). Blocking a country with an IP access rule needs the Enterprise plan, and an attack in progress calls for a rule on what its requests share rather than on their country.

How Much of That Country’s Traffic Is Real?

Cloudflare’s Analytics page counts the requests a country sends, page by page once you filter on a path, so read it before you write the rule. Of the 10 guides we read, two send you to the Threats by Country map, which shows only where threats came from, and none to the country’s requests.

On Pro, Business and Enterprise, on the zone’s Analytics > Traffic tab, select Add filter and pick Country (Zone Analytics). Read Requests, Page views and Visits for that country over the last 30 days, then add a Path filter for the pages your customers use, such as checkout or login. On Free, the Web Traffic Requests by Country map shows each country’s requests, with no filter for a path.

Requests from that country on those pages are where a block would refuse real visitors: when there are any, start with a challenge and the exceptions below.

Managed Challenge or Block?

The challenge solve rate decides the action: a high rate means real visitors match the rule, close to zero means Block

Start with Managed Challenge, then switch to Block once the challenge shows no real visitors. Cloudflare gives the same order: its Log action, which records matches without acting, is for Enterprise only, so on other plans it says to start with Managed Challenge, review the matched requests in Security Events, then switch to Block (Create a custom rule).

The share of challenges that visitors pass is the rule’s challenge solve rate (CSR), shown on the zone’s Security rules page. Most automated traffic abandons the challenge, and Cloudflare reads a high CSR as false positives, real visitors matching the rule (Challenge solve rate).

Then open Security Events: on Pro and above, Top events by source lists the countries, paths and user agents of the requests your rules acted on, and Free shows sampled logs only (Security Events). We recommend switching to Block once the CSR stays close to zero and the paths are ones no customer uses.

How to Keep Search Engines and Partners Out of the Block

Write the exceptions into the rule’s own expression, so the block never reaches them. Add and not cf.client.bot for verified bots: the field, Known Bots in the rule builder, is true for every bot Cloudflare has verified: search engine crawlers and monitoring services, but also verified AI crawlers and, since July 2026, signed AI agents, which the exception lets through too (cf.client.bot, verified bots). Cloudflare’s own example pairs it with a country list and Managed Challenge (Allow traffic from search engine bots); two of the 10 guides we read include it.

For partners, such as a payment provider’s webhook addresses or a contractor’s office, put their IP addresses in an IP list named partners and add and not ip.src in $partners (Use lists in expressions). Lists belong to the Cloudflare account, so rules on every zone can use the same one (Lists). The rule then reads (ip.src.country in {"KP"} and not cf.client.bot and not ip.src in $partners).

Cloudflare WAF Block IP Range: Use a Custom Rule, Not IP Access Rules

A /20 range takes 16 IP access rules of one /24 each, or one custom rule

Match the range with ip.src in a custom rule, because IP access rules accept two IPv4 range sizes only. We checked Cloudflare’s public API schema on December 1, 2026: an IP access rule range takes the /16 and /24 prefixes for IPv4 and nothing else, so a /20 needs 16 rules of one /24 each (parameters). A custom rule takes the range as written: ip.src in {198.18.0.0/20} (inline lists).

For a long set of ranges, or ranges several zones block, create an IP list: in your account’s Settings, open Lists, select Create new list, add the ranges, then match ip.src in $blocked_ranges (Create a list). A list item can be any IPv4 range from /8 to /32 (Custom lists). Free accounts get one custom list, Pro and Business ten, with 10,000 items across all of them (Lists), so on Free the partners list and the blocked ranges compete for one slot: keep the shorter set inline.

How to Set Up a Cloudflare WAF Block Country Rule with Lelops Advise

Lelops Advise answers questions about your Cloudflare setup and changes it through Ask Lelops, a chat in the Lelops app, for teams that run Cloudflare without a dedicated expert.

  1. Open Ask Lelops from the right edge of any page in the Lelops app. The Cloudflare API token you connected must allow editing WAF rules, and editing your account’s lists for step 2. Before each call to Cloudflare, reads included, Ask Lelops shows the call and waits for your OK.
  2. Create the partners list: “Create an IP list named partners in my Cloudflare account with 192.0.2.10 and 192.0.2.11.” Ask Lelops creates IP lists on an account and adds, replaces or deletes their addresses and ranges, the same way for a blocked_ranges list. The list tools work on an account, not a zone, so name the account if Lelops reaches more than one. A list change saves no version, and a version of a zone keeps the names of the account’s lists, not the addresses in them.
  3. Ask for the rule as a challenge: “Add a custom rule on example.com with Managed Challenge for requests from North Korea, except verified bots and the addresses in the list partners.” The call Ask Lelops shows holds the expression and the action: check that not cf.client.bot and not ip.src in $partners are in it, then confirm. Ask Lelops creates the WAF custom rule and saves the change as a new version of example.com. Lelops also checks that your role in the organization allows the change.
  4. Read what the rule stops: “Show the last security events on example.com.” Ask Lelops lists the 50 most recent security events, each with its action, the rule’s ID, the IP address, the path, the user agent and the country. Add a time range, such as “in the last hour”, to see only the events since the rule went live. Events with your rule’s ID on checkout or login paths, from browser user agents, point to real visitors in the challenge.
  5. Follow its volume: open Sentry > Monitoring, set Zones to example.com and pick the time range. On the Security tab, Top 10 WAF rules triggered lists the rules that acted most, with their request volume (Lelops Sentry).
  6. Switch to Block: once the rule’s CSR in Cloudflare stays close to zero, ask “Change the North Korea rule on example.com to Block.” Ask Lelops edits the rule after your OK and saves another version.
  7. Roll back if real visitors get blocked: open Guard > Versioning, expand the version before the change, select Rollback, then Restore and deploy. Lelops deploys that version of example.com again, its WAF custom rules included, as a new version (Lelops Guard). Rollback needs the Administrator role.

Lelops Advise turns a country or range block into a custom rule with its exceptions, shows what it stops and keeps a version of every rule change: lelops.ai/product/advise.

FAQ

Can Cloudflare block a country without the Enterprise plan?

Yes. A WAF custom rule that matches the Country field blocks a country on every Cloudflare plan, and one rule can list several countries. Only the other way of doing it, an IP access rule with a country as its value, needs the Enterprise plan.

Does blocking a country in Cloudflare block search engine crawlers?

It does when they crawl from that country, because the rule matches the country of the IP address and nothing else. Adding not cf.client.bot to the expression keeps Cloudflare's verified bots out of the rule: search engine crawlers and monitoring services, but also verified AI crawlers and agents. Cloudflare's own example combines a country list with that condition and the Managed Challenge action.

Should a country rule use Block or Managed Challenge?

Teams that are not sure who sends the traffic start with Managed Challenge, since a browser can pass it and most scripts give up. The rule's challenge solve rate then shows how many challenged visitors passed, and Cloudflare reads a high rate as real visitors matching the rule. Block fits once that rate stays close to zero, and the Log action, which only records matches, is limited to Enterprise.

How do you block Tor traffic with a Cloudflare custom rule?

Cloudflare gives the Tor network its own code, T1, in the continent field, so the expression ip.src.continent eq "T1" matches it in a custom rule, on any plan. IP access rules accept T1 as a country value instead, which makes it a country block and limits it to the Enterprise plan.

Can Cloudflare IP access rules block an IP range?

Yes, but for IPv4 an IP access rule accepts only a /16 or a /24 range, so other sizes have to be split into several rules. A custom rule takes any range in its expression, such as ip.src in {198.18.0.0/20}, and an IP list accepts IPv4 ranges from /8 to /32. Cloudflare recommends custom rules with IP lists over IP access rules for IP-based blocking.

Get started

Connect Cloudflare. See where your edge stands today.

Your first configuration health report, with the fixes to make first, in minutes.

  • No credit card required