Cloudflare Firewall Rules for Securing WordPress

Updated on November 9, 2018
carny profile image

I use Cloudflare for all of my Wordpress websites - not just to make them faster, but more secure as well.

How to Use Cloudflare to Secure Your WordPress Site

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.

Cloudflare's Web Application Firewall (WAF) provides an additional layer of security for your websites.
Cloudflare's Web Application Firewall (WAF) provides an additional layer of security for your websites. | Source

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 scrolling down to Access Rules, entering your IP*, and choosing Whitelist in the drop-down menu.

*You have several choices here, in the order of decreasing security:

  • 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.

1. 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.

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

Save the rule, and it should be enabled automatically. Cloudflare will now start blocking every attempt to connect to wp-login.php, except for the IP you whitelisted.

These brute force attempts will vanish from your server logs, but you'll be able to track them on Cloudflare's Firewall Events section should you wish to verify that the protection is working.

2. Block xmlrpc.php 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 need it, however, 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")

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.

Second is /wp-admin/theme-editor.php, which runs an error check every time you edit your theme through the built-in editor in WordPress. If you don't add this exception, the check will fail every time, 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-Referer Requests to Plugins

Most WordPress sites get hacked through insecure plugins. The best thing, of course, is not to install them, 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 will be allowed automatically. You may also want to allow Google's crawler through just in case it tries to index something in your plugins folder (optional).

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: User Agent
  • Operator: does not contain
  • Value: Googlebot

[Action: Block]

Or, just paste this expression in directly (remember to replace yoursite.com with the actual address):

(http.request.uri.path contains "/plugins/" and not http.referer contains "yoursite.com" and not http.user_agent contains "Googlebot")

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: Referer
  • Operator: does not contain
  • Value: yoursite.com (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 "yoursite.com")

Your Website Is Now Slightly More Secure!

These five rules should cover the most common WordPress attacks, but feel free to adjust them according to your needs.

Most attacks you see in your logs come from automated bots just dumbly scanning websites for vulnerabilities, but should a person from outside your whitelisted IPs try to access your admin area, they will be greeted with an "Error 1020" page like this (you can confirm this by accessing it via a proxy or VPN):

This is the page a potential attacker will see after triggering one of your firewall rules.
This is the page a potential attacker will see after triggering one of your firewall rules.

Further Reading

Should you run into issues or want to learn more, I suggest you visit Cloudflare's knowledge base:

https://developers.cloudflare.com/firewall/cf-firewall-rules/

Questions & Answers

    © 2018 carny

    Comments

      0 of 8192 characters used
      Post Comment

      No comments yet.

      working

      This website uses cookies

      As a user in the EEA, your approval is needed on a few things. To provide a better website experience, turbofuture.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

      For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://turbofuture.com/privacy-policy#gdpr

      Show Details
      Necessary
      HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
      LoginThis is necessary to sign in to the HubPages Service.
      Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
      AkismetThis is used to detect comment spam. (Privacy Policy)
      HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
      HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
      Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
      CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
      Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
      Features
      Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
      Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
      Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
      Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
      Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
      VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
      PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
      Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
      MavenThis supports the Maven widget and search functionality. (Privacy Policy)
      Marketing
      Google AdSenseThis is an ad network. (Privacy Policy)
      Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
      Index ExchangeThis is an ad network. (Privacy Policy)
      SovrnThis is an ad network. (Privacy Policy)
      Facebook AdsThis is an ad network. (Privacy Policy)
      Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
      AppNexusThis is an ad network. (Privacy Policy)
      OpenxThis is an ad network. (Privacy Policy)
      Rubicon ProjectThis is an ad network. (Privacy Policy)
      TripleLiftThis is an ad network. (Privacy Policy)
      Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
      Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
      Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
      Statistics
      Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
      ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
      Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)