In order to enable automatic mitigation of random prefix attacks:
-
Set up DNS Firewall.
-
Enable attack mitigation on your DNS Firewall cluster.
-
In the Cloudflare dashboard, go to the DNS Firewall Clusters page.
Go to Clusters ↗ -
Select the cluster you want to update, then select Edit.
-
Turn on Attack mitigation and choose whether Cloudflare should only mitigate attacks when the upstream is unhealthy.
-
Select Save.
Send a
PATCHrequest to update your DNS Firewall cluster:
At least one of the following token permissions is required:Required API token permissions
DNS Firewall Write
Update DNS Firewall Clusterbash curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/dns_firewall/$DNS_FIREWALL_ID" \ --request PATCH \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ "attack_mitigation": { "enabled": true, "only_when_upstream_unhealthy": true } }' -
Once you turn on attack mitigation, Cloudflare returns a REFUSED response to queries that are part of a random prefix attack.
The only_when_upstream_unhealthy setting controls when Cloudflare starts blocking queries identified as part of a random prefix attack.
| Mode | only_when_upstream_unhealthy |
When to use |
|---|---|---|
| Conditional (default) | true |
Steady-state protection. Cloudflare blocks attack traffic only when your upstream nameservers are unresponsive. Reduces the risk of false positives — legitimate queries are less likely to be refused. Recommended for most deployments. |
| Always on | false |
Active attack response. Cloudflare blocks identified attack traffic at all times, regardless of upstream health. Use this when your upstream is being overwhelmed. Some legitimate queries for subdomains not seen before may receive a REFUSED response. |
To check your current cluster configuration before making changes, send a GET request:
Required API token permissions
At least one of the following token permissions is required:DNS Firewall WriteDNS Firewall Read
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/dns_firewall/$DNS_FIREWALL_ID" \
--request GET \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"DNS Firewall provides different levels of protection depending on the type of attack. Before applying mitigations, identify which pattern you are dealing with by inspecting a sample of query names hitting your upstream nameservers.
| Attack pattern | Description | DNS Firewall protection |
|---|---|---|
| Simple DNS flood | The same query name, or a small set of names, repeated at high volume. | ✅ Strong — cached responses absorb the traffic. Increase cache TTL values. |
| Random prefix attack | Random-looking subdomains all ending with the same suffix (for example, abc123.example.com, xyz456.example.com). |
Best-effort — use attack_mitigation. Refer to Respond to an active random prefix attack. |
| Random-query attack | Random subdomains across many different suffixes, or the targeted suffix is on the Public Suffix List ↗︎ (for example, co.uk). |
Partial — DNS Firewall can mitigate attacks targeting specific domain apexes. Attacks targeting an entire public suffix (such as co.uk) cannot be fully mitigated. |
If the same query names are being repeated at high volume, Cloudflare's cache is your primary defense. Apply the following settings via API or dashboard to maximise cache absorption.
Raising the cache TTL lets Cloudflare serve more responses from cache and reduces queries forwarded to your upstream nameservers:
Required API token permissions
At least one of the following token permissions is required:DNS Firewall Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/dns_firewall/$DNS_FIREWALL_ID" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"minimum_cache_ttl": 3600,
"maximum_cache_ttl": 21600
}'If the attack includes queries for names that do not exist, enable negative response caching to prevent repeated lookups for NXDOMAIN, REFUSED, and SERVFAIL responses from reaching your upstream nameservers:
Required API token permissions
At least one of the following token permissions is required:DNS Firewall Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/dns_firewall/$DNS_FIREWALL_ID" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"negative_cache_ttl": 900
}'ANY queries are frequently used in DNS amplification attacks. If not already disabled, turn them off:
Required API token permissions
At least one of the following token permissions is required:DNS Firewall Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/dns_firewall/$DNS_FIREWALL_ID" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"deprecate_any_requests": true
}'If your upstream nameservers are being overwhelmed by a random prefix attack, apply the following steps in order. All changes can be made via API. Rate limit configuration is not available in the dashboard.
Set only_when_upstream_unhealthy to false so that Cloudflare blocks identified attack traffic immediately, without waiting for your upstream to become unresponsive:
Required API token permissions
At least one of the following token permissions is required:DNS Firewall Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/dns_firewall/$DNS_FIREWALL_ID" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"attack_mitigation": {
"enabled": true,
"only_when_upstream_unhealthy": false
}
}'Raising the cache TTL lets Cloudflare serve more responses from cache, reducing the number of queries forwarded to your upstream nameservers:
Required API token permissions
At least one of the following token permissions is required:DNS Firewall Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/dns_firewall/$DNS_FIREWALL_ID" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"minimum_cache_ttl": 3600,
"maximum_cache_ttl": 21600
}'Rate limiting caps the number of uncached queries per second that Cloudflare forwards to your upstream nameservers, applied per data center. Queries that exceed the limit receive a REFUSED response. Rate limiting is disabled by default — set a value based on what your nameservers can sustain under normal load:
Required API token permissions
At least one of the following token permissions is required:DNS Firewall Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/dns_firewall/$DNS_FIREWALL_ID" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"ratelimit": 1000
}'Once the attack subsides, revert only_when_upstream_unhealthy to true and restore your normal cache TTL values to reduce the risk of false positives.