This guide is aimed at security-minded webmasters who run a WordPress site or blog on a Cloudflare-enabled domain. On the free plan, Cloudflare grants five firewall rules that are empty by default.
By adding WordPress-specific rules I describe on this page, you can secure your site and block attacks before they even reach your web host’s server.
Disclaimer: I’m not affiliated with Cloudflare in any way, but I’ve been a satisfied user of their services for several years.
0. Whitelist Your IP Address
Before you implement any firewall rules, you should first whitelist your own IP. This way you won’t be affected should you decide to block your WordPress admin area from outsiders (which I will explain in a minute).
This can be done by accessing your Cloudflare dashboard and clicking Firewall, then Tools, entering your IP*, and choosing Whitelist in the drop-down menu.
Whitelist your exact IP address. Optimal choice if your ISP grants you a static IP. Note that if your IP changes, you will need to reenter it lest you get locked out of your WordPress admin area.
Whitelist your ISP’s entire IP range. Good choice if you have a dynamic IP.
Whitelist your country. This won’t protect you from attacks coming from inside your own country, but it can be a convenient option if you travel frequently and use Wi-Fi to connect to your WordPress site.
An IP address or a country whitelisted in this manner will be exempt from all firewall rules, so you won’t need to add exceptions for each individual rule.
I. Fuck PK + TR + AZ + Tor
(ip.geoip.country eq "PK") or (ip.geoip.country eq "TR") or (ip.geoip.country eq "AZ") or (ip.geoip.country eq "T1")
II. Xmlrpc
After wp-login.php, xmlrpc.php is the second most common attack target. XML-RPC has legitimate uses, such as blogging from a smartphone or posting content to multiple WordPress sites at once. If you don’t do that, then it can be safely blocked. Follow the same procedure as previously and create the rule:
- Field: URI Path
- Operator: contains
- Value: /xmlrpc.php
[Action: Block]
You should see the following in the Expression Preview section:
(http.request.uri.path contains "/xmlrpc.php")
III. Login Protection
If you peek at your server logs, you’ll probably find numerous IPs from all over the world trying to access your wp-login.php file. This is by far the most common attack on WordPress installations. These are usually automated scans which do not pose a big threat, but you can still block them off for your peace of mind.
This of course assumes that you (the admin) are the only user on your site. If you have multiple users or use a membership plugin, you’ll probably want to skip this rule.
In your Cloudflare dashboard, click Firewall once again, then press the blue Create a Firewall rule button. Name it whatever you like and enter the following:
- Field: URI Path
- Operator: contains
- Value: /wp-login.php
[Action: Block]
If you did it right, you should see the following in the Expression Preview section:
(http.request.uri.path contains "/wp-login.php" or (http.request.uri.path contains "/login/") and ip.geoip.country ne "AM")
Save the rule, and it should be enabled automatically. Cloudflare will now block every attempt to connect to wp-login.php except from the IP you whitelisted.
These brute force attempts will vanish from your server logs, but you’ll be able to track them on Cloudflare Firewall Events section should you wish to verify that the protection is working.
*You have several choices here in the order of decreasing security:
IV. Content Protection
(http.request.uri.query contains "author_name=") or
(http.request.uri.query contains "author=" and not http.request.uri.path contains "/wp-admin/export.php") or
(http.request.full_uri contains "wp-config.") or
(http.request.uri.path contains "/wp-json/") or
(http.request.uri.path contains "/wp-content/" and http.request.uri.path contains ".php") or
(http.request.uri.path contains "phpmyadmin") or
(http.request.uri.path contains "/phpunit") or
(http.request.full_uri contains "<?php") or
(http.cookie contains "<?php") or
(http.request.full_uri contains "../") or (http.request.full_uri contains "..%2F") or
(http.request.full_uri contains "passwd") or
(http.request.uri contains "/dfs/") or
(http.request.uri contains "/autodiscover") or
(http.request.uri contains "/wpad.") or
(http.request.uri contains "/wallet.dat") or
(http.request.full_uri contains "webconfig.txt") or
(http.request.full_uri contains "vuln.") or
(http.request.uri.query contains "base64") or
(http.request.uri.query contains "<script") or (http.request.uri.query contains "%3Cscript") or
(http.cookie contains "<script") or (http.referer contains "<script") or
(upper(http.request.uri.query) contains " UNION ALL ") or (upper(http.request.uri.query)contains " SELECT ") or
(http.request.uri.query contains "$_GLOBALS[") or (http.request.uri.query contains "$_REQUEST[") or (http.request.uri.query contains "$_POST[")
(http.request.uri.path contains "/wp-content/"
and http.request.uri.path contains ".php")
or (http.request.uri.path contains "/wp-config.php")
or (http.request.full_uri eq "<?php")
or (http.cookie contains "<?php")
or (http.request.uri.query contains "<script")
or (http.cookie contains "<script")
or (http.request.uri.query contains "%3Cscript")
or (http.cookie contains "%3Cscript")
V. User Agents
(http.user_agent contains "AhrefsBot/") or (http.user_agent contains "BaiDuSpider") or (http.user_agent contains "baidu.com") or (http.user_agent contains "/bin/bash") or (http.user_agent contains "[email protected]") or (http.user_agent contains "DnyzBot/") or (http.user_agent contains "DotBot/") or (http.user_agent contains "eval(") or (http.user_agent contains "Go-http-client/") or (http.user_agent contains "Nikto") or (http.user_agent contains "Nimbostratus") or (http.user_agent contains "python-requests") or (http.user_agent contains "Scrapy/") or (http.user_agent contains "SeznamBot/") or (http.user_agent contains "Sogou web spider/") or (http.user_agent contains "spbot/") or (http.user_agent contains "Uptimebot/") or (http.user_agent contains "WebDAV-MiniRedir") or (http.user_agent contains "WinHttp.WinHttpRequest") or (http.user_agent contains "ZmEu")
VI. Cf threat score (Badbots & actors protection)
(cf.threat_score gt 20)
Choose an action: Challenge (Captcha)
3. Protect the wp-admin Area
Now let’s make it so you and only you can access your admin area. This rule is slightly more complex because you need to make two exceptions.
First is /wp-admin/admin-ajax.php, which is used by certain plugins to display dynamic content on your website. As such, despite being located inside the /wp-admin/ folder, it needs to be accessible from the outside.
Second is /wp-admin/theme-editor.php, which runs an error check every time you edit your theme through the built-in editor by creating a loopback request to your homepage. If you don’t add this exception, the check will fail with a message “Unable to communicate back with site to check for fatal errors” and your modifications won’t be saved.
Go ahead and create the following rule:
- Field: URI Path
- Operator: contains
- Value: /wp-admin/
[AND]
- Field: URI Path
- Operator: does not contain
- Value: /wp-admin/admin-ajax.php
[AND]
- Field: URI Path
- Operator: does not contain
- Value: /wp-admin/theme-editor.php
[Action: Block]
Or, just click Edit expression and paste in the following:
(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")
4. Block No-Referrer Requests to Plugins
Most WordPress sites get hacked through insecure plugins. The best approach, of course, is not to install them in the first place, but you can also create a firewall rule blocking direct access to /wp-content/plugins/.
Legitimate requests which come through your website have something along the lines of “https://site.am/page” as the HTTP referer and should be allowed. You may also want to allow known good bots (such as the Google crawler) just in case they try to index something – such as an image – inside your plugins folder.
Create the following rule:
- Field: URI Path
- Operator: contains
- Value: /wp-content/plugins/
[AND]
- Field: Referrer
- Operator: does not contain
- Value: yoursite.com (replace with your real domain)
[AND]
- Field: Known Bots
- Operator: equals
- Value: Off
[Action: Block]
Or, just paste this expression in directly (remember to replace site.am with the actual address):
(http.request.uri.path contains "/wp-content/plugins/" and not http.referer contains "site.am" and not cf.client.bot)
5. Reduce Spam by Blocking Direct Requests to wp-comments-post.php
I’ll be honest: the effect of this rule will be minimal as spam bots these days are sophisticated enough to spoof the referrer. This will only block bots hammering the wp-comments-post.php file directly. Still, the same tip is described in WordPress Codex (except they use a .htaccess rule rather than Cloudflare), so if it’s good enough for them, it’s good enough for me.
The rule is as follows:
- Field: URI Path
- Operator: equals
- Value: /wp-comments-post.php
[AND]
- Field: Request Method
- Operator: equals
- POST
[AND]
- Field: Referrer
- Operator: does not contain
- Value: site.am (replace with your real domain)
[Action: Block]
And here’s the expression to save you the time:
(http.request.uri.path eq "/wp-comments-post.php" and http.request.method eq "POST" and not http.referer contains "site.am")
Conclusion and recommendations
With these settings, you can make optimal use of Cloudflare free plan. This way you have a rule for 3 actions:
- JS Challenge
- Challenge (Captcha)
- Block
By the way the advantage is that expressions can be combined so that you can use only 3 of the 5 free firewall rules. The actions and expressions can be customized to your liking. This way you can use a more aggressive security or less aggressive.
It came to our attention that a JS Challenge can trigger a Captcha Challenge in some cases. So we will chose this less aggressive JS Challenge for the login protection, it is just uncomfortable for legitimate users and if their session expires they need to do a captcha again if you choosed Challenge instead of JS Challenge.
CF Threat Score
The field cf.threat_score is Cloudflare AI score in numbers.
cf.threat_score number
Represents a Cloudflare threat score from 0–100, where 0 indicates low risk. Values above 10 may represent spammers or bots, and values above 40 identify bad actors on the internet. It is rare to see values above 60. A common recommendation is to challenge requests with a score above 10 and to block those above 50.
In this article we used the average number of 20 in our example, but 15 will do fine also. Please feel free to modify the examples given.
