FALCONINTERNET

fail2ban jails that ban WordPress login and XML-RPC brute force

Tutorials
fail2ban jails that ban WordPress login and XML-RPC brute force

Any WordPress site on a public IP receives automated password guesses against wp-login.php and xmlrpc.php. Login-limit plugins stop the guesses inside PHP, but every attempt still costs a PHP-FPM worker and a database query. fail2ban moves the block to the firewall: it reads the web server access log, counts failed logins per IP, and drops that IP's packets before they reach PHP. This guide sets up two jails on Ubuntu 24.04 and AlmaLinux 9, for Apache or Nginx.

How failed WordPress logins look in the access log

When a POST to wp-login.php fails, WordPress renders the login form again with an error, so the server logs HTTP 200. A successful login ends in a redirect (302) to the dashboard or the redirect_to target. That difference is the whole detection method:

203.0.113.40 - - [02/Dec/2026:09:14:02 +0000] "POST /wp-login.php HTTP/1.1" 200 4417 "-" "Mozilla/5.0"
198.51.100.7 - - [02/Dec/2026:09:15:31 +0000] "POST /wp-login.php HTTP/1.1" 302 0 "-" "Mozilla/5.0"

The first line is a failure, the second a successful login. XML-RPC is different. The XML-RPC spec requires HTTP 200 for every response, so a bad password comes back as a 200 with a fault code of 403 inside the XML body. The access log cannot tell success from failure there, so the XML-RPC jail counts every POST and uses a higher threshold. WordPress core stops checking passwords after the first failure inside a single XML-RPC request, which limits each request to one guess, but nothing in core limits how many requests a bot sends.

Installing fail2ban on Ubuntu 24.04 and AlmaLinux 9

Upstream fail2ban is at 1.1.1 (August 15, 2026). The distributions ship older builds, and those are fine for this work.

Ubuntu 24.04:

sudo apt update
sudo apt install fail2ban
dpkg -l fail2ban | tail -1

You should see 1.0.2-3ubuntu0.1 from noble-updates. The original 1.0.2-3 package in the noble release pocket fails to start under Python 3.12 with No module named 'asynchat' (Launchpad bug 2055114). If a server has not pulled updates since 2024 and fail2ban will not start, that is the reason.

AlmaLinux 9 and other RHEL 9 rebuilds get fail2ban from EPEL:

sudo dnf install epel-release
sudo dnf install fail2ban fail2ban-firewalld
sudo systemctl enable --now fail2ban

EPEL 9 currently carries 1.1.0-6.el9. The fail2ban-firewalld subpackage drops /etc/fail2ban/jail.d/00-firewalld.conf, which routes bans through firewalld instead of raw iptables. Read that file once so you know which ban action is in effect.

Global settings in jail.local

Never edit /etc/fail2ban/jail.conf; package upgrades replace it. fail2ban reads jail.conf, then jail.d/*.conf, then jail.local, then jail.d/*.local, and later files win. Create /etc/fail2ban/jail.local:

[DEFAULT]
# Your office, VPN and monitoring addresses. Space separated.
ignoreip = 127.0.0.1/8 ::1 192.0.2.10

# Repeat offenders get longer bans, capped at one week.
bantime.increment = true
bantime.maxtime = 1w

[recidive]
enabled = true

The shipped defaults are bantime = 10m, findtime = 10m and maxretry = 5. With bantime.increment on, fail2ban looks up earlier bans for the same IP in its database and lengthens each new one. The recidive jail watches /var/log/fail2ban.log and bans any IP that gets banned five times in a day, on all ports, for a week.

Get ignoreip right before you enable anything. Banning your own office IP locks staff out of every site on the server, not only WordPress.

Filters for wp-login.php and xmlrpc.php

fail2ban does not ship a WordPress filter, so write two. /etc/fail2ban/filter.d/wordpress-login.conf:

[Definition]
failregex = ^<HOST> -[^"]*"POST /(?:[^ "]*/)?wp-login\.php HTTP/[0-9.]+" 200 \d+
ignoreregex =

/etc/fail2ban/filter.d/wordpress-xmlrpc.conf:

[Definition]
failregex = ^<HOST> -[^"]*"POST /(?:[^ "]*/)?xmlrpc\.php HTTP/[0-9.]+"
ignoreregex =

<HOST> is fail2ban's placeholder for the client address. The optional (?:[^ "]*/)? group matches installs in a subdirectory such as /blog/wp-login.php. fail2ban finds and strips the timestamp in the standard Apache and Nginx combined log formats on its own, so no datepattern line is needed. If you use a custom log format, the client IP must still be the first field.

Jail definitions for Apache and Nginx logs

Create /etc/fail2ban/jail.d/wordpress.local. This example is for Nginx on Ubuntu:

[wordpress-login]
enabled  = true
port     = http,https
filter   = wordpress-login
logpath  = /var/log/nginx/*access.log
maxretry = 5
findtime = 10m
bantime  = 1h

[wordpress-xmlrpc]
enabled  = true
port     = http,https
filter   = wordpress-xmlrpc
logpath  = /var/log/nginx/*access.log
maxretry = 20
findtime = 5m
bantime  = 1h

Change logpath to match your server. Globs work, and each vhost log needs to be covered:

  • Apache on Ubuntu: /var/log/apache2/*access.log
  • Apache on AlmaLinux: /var/log/httpd/*access_log
  • Control panels usually keep per-domain logs elsewhere; check the vhost's CustomLog or access_log directive.

If no file matches the glob when fail2ban starts, the jail fails to load, so check the path with ls first.

Testing the filters with fail2ban-regex

Run each filter against a real log before you reload:

sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/wordpress-login.conf
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/wordpress-xmlrpc.conf

The summary shows how many lines matched. Zero matches on a public WordPress server usually means the log format differs from the regex, or the log you chose belongs to another vhost. Then check the full configuration and load it:

sudo fail2ban-client -t
sudo fail2ban-client reload
sudo fail2ban-client status wordpress-login

To confirm a ban end to end, send six bad logins from a machine that is not in ignoreip, such as a phone hotspot or a spare VPS:

for i in 1 2 3 4 5 6; do
  curl -s -o /dev/null -w '%{http_code}\n' \
    -d 'log=nobody&pwd=wrong' https://example.com/wp-login.php
done

Expect five or six 200 responses, then timeouts once the ban lands. fail2ban-client status wordpress-login should list the test IP under Banned IP list. Remove it with:

sudo fail2ban-client set wordpress-login unbanip 203.0.113.40

Sites behind Cloudflare or a load balancer

If traffic arrives through Cloudflare, a reverse proxy or a load balancer, the access log records the proxy's address. fail2ban will then ban the proxy and take the site offline for everyone. Fix the logged address first:

  • Nginx: set_real_ip_from for each proxy range plus real_ip_header CF-Connecting-IP; (or X-Forwarded-For for your own load balancer).
  • Apache: mod_remoteip with RemoteIPHeader CF-Connecting-IP and RemoteIPTrustedProxy for each proxy range.

With the real client IP in the log, the filters work, but a local firewall ban still does nothing because packets come from Cloudflare's addresses. Use fail2ban's bundled cloudflare action instead, which creates a block rule through the Cloudflare API and deletes it on unban. It needs an API token in the jail config, so set jail.local to mode 640.

Pitfalls with Jetpack, two-factor plugins and WooCommerce

  • Jetpack needs xmlrpc.php to talk to WordPress.com. The 20-in-5-minutes threshold leaves room for it, but watch the XML-RPC jail's ban list for a week after enabling it. If no site on the server uses Jetpack or the mobile app, deny xmlrpc.php in the web server config instead and skip that jail.
  • Two-factor plugins often answer a correct password with a 200 page that asks for the code. Each real login then counts as one failure. A maxretry of 5 absorbs this for normal use, but do not lower it to 2 or 3 on those sites.
  • WooCommerce customer logins post to the My Account page, not wp-login.php, so this filter does not see them.
  • On a shared office IP, one staff member with a stale saved password in a browser can get the whole office banned. That is what ignoreip is for.

If you want fail2ban to act on confirmed authentication failures instead of HTTP status codes, the WP fail2ban plugin (version 5.5.1) writes WordPress login failures to syslog and ships its own wordpress-hard.conf and wordpress-soft.conf filters. Because it logs the authentication result and not an HTTP status, XML-RPC's always-200 responses and two-factor prompts do not skew its counts. The cost is one more plugin to keep patched.

On the servers we manage, our 24x7x365 NOC monitoring addresses are in every ignoreip list so uptime checks never trip a ban. Check sudo fail2ban-client status a day after enabling the jails; a WordPress server facing the public internet should already show banned addresses in both.

Need this handled instead of explained?

We do this for a living — talk to an engineer about your setup.