Skip to main content

Cloudflare Firewall Rules for Securing WordPress

Cloudflare Firewall Rules for Securing WordPress

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.

now you can pass anyone if you are using it somehow 

Block wp-login.php Attacks

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]

Expression Preview 

(http.request.uri.path contains "/wp-login.php")

Block xmlrpc.php/wp-json Attacks:

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]

Note: you can do the same for "wp-json/" if you do not use it

Note: you can do the same for "author/"  this will help "Configure your web server to block requests to /?author=<number>"

Expression Preview 

(http.request.uri.path contains "/xmlrpc.php")

Good Example : Expression Preview 

(http.request.uri.path contains "/xmlrpc.php" and not http.referer contains "{yourwebsite}.com") or (http.request.full_uri contains "/wp-json" and not http.referer contains "{yourwebsite}.com")

Example 2

(http.request.uri.path contains "/xmlrpc.php" and not http.referer contains "example.com") or (http.request.full_uri contains "/wp-json" and not http.referer contains "example.com") or (http.request.uri.path contains "/author") or (http.request.uri.path contains "?author")

 

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]

Please note that you should except your IP from this rule

Expression Preview 

(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")

 

Block No-Referer 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 "http://yoursite.com/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: Referer
  • Operator: does not contain
  • Value: yoursite.com (replace with your real domain)

[AND]

  • Field: Known Bots
  • Operator: equals
  • Value: Off

[Action: Block]

Expression Preview 

(http.request.uri.path contains "/wp-content/plugins/" and not http.referer contains "{yoursite}.com" and not cf.client.bot)

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: Referer
  • Operator: does not contain
  • Value: yoursite.com (replace with your real domain)

[Action: Block]

Expression Preview 

(http.request.uri.path eq "/wp-comments-post.php" and http.request.method eq "POST" and not http.referer contains "{yoursite}.com")

 

whole example expresion  (replace {example.com} with your website)

(http.request.uri.path contains "/xmlrpc.php" and not http.referer contains "{example.com}")
or (http.request.full_uri contains "/wp-json" and not http.referer contains "{example.com}") 
or (http.request.uri.path contains "/author")|
or (http.request.uri.path contains "?author")
or (http.request.uri.path contains "/wp-content/plugins/" and not http.referer contains "{example.com}" and not cf.client.bot)
or (http.request.uri.path eq "/wp-comments-post.php" and http.request.method eq "POST" and not http.referer contains "{example.com}") 
or (http.request.uri.path contains "wp-content/themes" and not http.referer contains "{example.com}")
 

 

 

 

 

 

Referene : Cloudflare Firewall Rules for Securing WordPress - TurboFuture - Technology