Configure Fail2ban and Unattended Upgrades to Harden a Linux WordPress Server

Harden a Linux WordPress server with Fail2ban and unattended upgrades

A WordPress server on the internet is scanned all the time by bots looking for weak passwords, open admin endpoints and unpatched software. Two tools cover two different layers. Fail2ban reads the logs, finds repeated failures from one IP and blocks it in the firewall. Unattended-upgrades installs the security updates of the operating system automatically, so known vulnerabilities are closed within hours.

Problem: The server has no automatic protection from login brute force, and OS security updates are installed only when someone remembers.

Solution: A Fail2ban jail for WordPress logins based on the web server access log, the recidive jail for repeat offenders, and unattended-upgrades for security updates.

Install

sudo apt install fail2ban unattended-upgrades

A filter for WordPress logins

A failed login on wp-login.php returns the form again with status 200, a successful one redirects with 302. So the filter counts only POST requests with status 200. For xmlrpc.php every POST counts: a site that does not use XML-RPC should not receive them at all.

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

[Definition]
failregex = ^<HOST> .* "POST /wp-login\.php HTTP/[^"]*" 200
            ^<HOST> .* "POST /xmlrpc\.php HTTP/[^"]*"
ignoreregex =

The jails

/etc/fail2ban/jail.d/wordpress.conf:

[wordpress]
enabled  = true
port     = http,https
filter   = wordpress
logpath  = /var/log/nginx/access.log
maxretry = 5
findtime = 600
bantime  = 86400

[recidive]
enabled   = true
logpath   = /var/log/fail2ban.log
banaction = iptables-allports
findtime  = 86400
maxretry  = 3
bantime   = 604800

Five failures in ten minutes give a ban for a day; three bans in a day give a ban for a week on all ports. For Apache the log is usually /var/log/apache2/access.log.

Test the filter on the real log and apply the configuration:

sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/wordpress.conf
sudo fail2ban-client reload
sudo fail2ban-client status wordpress

Unattended upgrades

Enable the daily run:

sudo dpkg-reconfigure -plow unattended-upgrades

and check the main settings in /etc/apt/apt.conf.d/50unattended-upgrades:

Unattended-Upgrade::Allowed-Origins {
    "${distro_id}:${distro_codename}-security";
};
Unattended-Upgrade::AutoFixInterruptedDpkg "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "false";

Only the security repository is allowed, so normal package upgrades that might change behavior wait for you. A test run shows what would be installed:

sudo unattended-upgrade --dry-run --debug

With Automatic-Reboot "false" kernel updates take effect only after a reboot that you plan yourself; the file /var/run/reboot-required shows that one is due.

NOTE: If the site is behind Cloudflare or another proxy, the access log contains the proxy’s IPs, and Fail2ban would ban Cloudflare instead of the attacker. Configure the web server to log the real client IP first (set_real_ip_from and real_ip_header CF-Connecting-IP in Nginx, mod_remoteip in Apache). Fail2ban complements the login protection in WordPress itself, see rate limiting the WordPress login.