Giter Club home page Giter Club logo

wp-fail2ban-redux's Introduction

WP Fail2Ban Redux

Contributors: thebrandonallen
Donate link: https://brandonallen.me/donate/
Tags: fail2ban, login, security, syslog
Requires at least: 5.5
Tested up to: 6.4
Requires PHP: 7.0
Stable tag: 0.9.1
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html

Records various WordPress events to your server's system log for integration with Fail2Ban.

Description

WP Fail2Ban Redux records various WordPress events to your server's system log for integration with Fail2Ban.

This plugin is (mostly) a drop-in replacement for WP fail2ban by Charles Lecklider.

While WP fail2ban is a great plugin, there are a number of improvements that could be made. In order to facilitate these improvements, a major refactoring of the codebase was necessary.

The core functionality between WP Fail2Ban Redux and WP fail2ban remains the same. WP Fail2Ban Redux is considered to be mostly a drop-in replacement, because all constants have been replaced with filters, and will, possibly, require some upgrade work. Don’t work it’s as simple as implementing the constants.

The following events are recorded by default:

  • Failed XML-RPC authentication attempts.
  • Successful authentication attempts.
  • Failed authentication attempts -- differentiated by a user's existence.
  • Pingback errors.

The following events can be enabled via filter:

  • Pingback requests.
  • Blocked user enumeration attempts.
  • Authentication attempts for blocked usernames.
  • Spammed comments.

Extra documentation is available on the WP Fail2Ban Redux GitHub Wiki.

Installation

  1. Upload the plugin to your plugins directory.
  2. Activate the plugin through the 'Plugins' menu in WordPress.
  3. Copy the config/filters/wordpress-hard.conf and config/filters/wordpress-soft.conf files to your Fail2Ban filters directory (generally /etc/fail2ban/filters.d).
  4. Copy the config/jail/wordpress.conf file to your Fail2Ban jail directory (generally /etc/fail2ban/jail.d), or append it's contents to your jail.local file. Make sure you read the notes in this file to aid successful setup.
  5. Reload or restart Fail2Ban.

Frequently Asked Questions

How do I upgrade from WP fail2ban?

If you haven't set any of the WP fail2ban constants, you don't need to do anything. If you have set some of the constants, view the upgrade instructions.

Will the wordpress-hard.conf and wordpress-soft.conf filters still work?

Yes! All of the improvements made in WP Fail2Ban Redux were done in a way that would allow existing functionality to work without changes to your filters. However, the demo filters included with the plugin do contain some recommended changes. There are also new features not found in WP fail2ban that will require changes to your filters to be effective. These changes are linked, by filter, below: wordpress-hard.conf wordpress-soft.conf

Be ye forewarned: Future changes to WP fail2ban may break backwards compatibility with WP Fail2Ban Redux filters. No attempts will be made to fix this. So, even though it's not required, it is probably a good idea to update the filters anyway.

Can I use this as a must-use plugin in the mu-plugins folder?

As of version 0.5.0, yes! Download the plugin, and unzip. Inside the plugin folder will be another folder named wp-fail2ban-redux and wp-fail2ban-redux.php. Upload this folder and file to the mu-plugins directory of your site.

How do you I use this plugin if my site is behind a proxy, like Cloudflare?

You need to add some code to your wp-config.php file. See the below links for guidance.

Changelog

0.9.1

  • Release date: 2023-10-17
  • Bumps "Tested up to" version to 6.4
  • Bumps minimum required PHP version to 7.0
  • Bumps minimum required WP version to 5.5
  • Update dependency package versions
  • No changes to jail or filters in the release.

0.8.3

  • Release date: 2023-10-17
  • Bumps "Tested up to" version to 5.9
  • No changes to jail or filters in the release.

0.8.2

  • Release date: 2021-08-08
  • Bumps "Tested up to" version to 5.8
  • No changes to jail or filters in the release.

0.8.1

  • Release date: 2021-06-01
  • Actually bumps "Tested up to" version to 5.7

0.8.0

  • Release date: 2021-05-31
  • Bumps "Tested up to" version to 5.7
  • Fix issue where logging out of WordPress could cause a blocked user log to be recorded

0.7.0

  • Release date: 2021-01-05
  • Bumps "Tested up to" version to 5.6
  • Move Composer dependencies to require-dev to reduce the number of packages installed when WP Fail2Ban Redux is installed via composer. See #17

0.6.0

  • Release date: 2020-06-07
  • Bumps the minimum required version to WordPress 4.9.
  • Bumps "Tested up to" version to 5.4.1

0.5.1

0.5.0

  • Release date: 2018-10-27
  • Add better support for use as a must-use plugin in the mu-plugins directory.

0.4.0

  • Release date: 2018-01-15
  • Bumped the minimum required WordPress version to 4.5.
  • Bumped the minimum required PHP version to 5.3. This is a soft bump, meaning, nothing changed that will break PHP 5.2 compatability. However, this could easily change in the future, and PHP 5.2 is no longer actively tested.
  • Renamed the wp_fail2ban_redux_openlog_indent filter to wp_fail2ban_redux_openlog_ident, because... it was misspelled.

0.3.1

  • Release date: 2017-05-18
  • Bump minimum required WordPress version to 4.4.
  • Performance improvements when not blocking user enumeration.
  • Use wp_die to exit, to allow for greater customization of exit messages.
  • Exit messages are now escaped using esc_html.
  • Refactored plugin loading.
  • You can now create your own, custom, logging class, in case you don't want to use the standard syslog() output.

0.3.0

  • Superseded by 0.3.1

0.2.1

  • Release date: 2017-02-15
  • Fix a stupid syntax error in the comment spam filter :( Thanks to @ichtarzan for reporting!

0.2.0

  • Release date: 2016-09-27
  • Fixed PHP notices where WP_Fail2Ban_Redux::comment_spam expects two parameters. Decided it was probably a good idea to oblige.
  • User enumeration blocking now checks for both the author and author_name parameters. The author_name parameter could be used to validate the existence of a particular username, so blocking on this parameter as well will further reduce the attack surface.
  • Fixes an issue where user enumeration blocking was overzealous and would prevent actions in the admin area. Props pjv. #2
  • WP_Fail2Ban_Redux::redirect_canonical is now deprecated. If you were doing anything with this function, or the hook that initialized it, you should look at WP_Fail2Ban_Redux::user_enumeration instead.
  • Added a note to wordpress.conf about the logpath parameter, and common auth log locations. There is no need to changed existing configurations. This is merely to aid setup for future users.

0.1.1

  • Release date: 2016-07-23
  • In PHP < 7.0, exit isn't allowed as a method name. WP_Fail2Ban_Redux_Log::exit is now WP_Fail2Ban_Redux_Log::_exit.

0.1.0

  • Release date: 2016-07-13
  • Initial release.

wp-fail2ban-redux's People

Contributors

dependabot[bot] avatar thebrandonallen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wp-fail2ban-redux's Issues

the fail2ban part itself seems to not work in a wp-mu setup

I have the wp-failtoban-redux plugin installed in a multisite wordpress. The logging part works just fine, but fail2ban is not picking up anything...

the only changes i've made on the fail2ban side is to reduce the number of failed attempts to 1 respective 3, and here's a bit from /var/log/messages (yes I have changed the wordpress.conf file to point at /var/log/messages):

2020-12-24T15:08:07.102643+01:00 nextcloud wp(hanaya.eregion.de)[30520]: Authentication failure for admin from 102.186.99.203
2020-12-24T20:24:36.130203+01:00 nextcloud wp(eregion.de)[32200]: XML-RPC authentication failure from 178.128.68.121

wordpress-hard.conf regex

I noticed fail2ban was not banning entries like:

XML-RPC multicall authentication failure from 1.2.3.4

When I looked at the regex in wordpress-hard.conf, I see:

^%(__prefix_line)sXML-RPC multicall authentication failure <HOST>$

What appears to work for me is:

^%(__prefix_line)sXML-RPC multicall authentication failure from <HOST>$

settings

would be nice to have better WP integration with a settings panel etc

php exec() an call iptables and ban a specific IP address and stuffing the bans into the database seems like a good idea

user enumeration

i'm not (yet) using this plugin, but stumbled onto your repo in researching an issue i just posted for the original wp-fail2ban plugin. quickly looking at your code, i think it should have the same issue, so you might want to take a look at: https://wordpress.org/support/topic/false-positive-on-user-enumeration?replies=1

i'm not sure this is the best solution, but off the top of my head, i think that in the function starting around line 140 in /classes/class-wp-fail2ban-redux.php if you check for logged in users and exempted them from the block it would be an improvement.

PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback

WP Fail2Ban Redux 0.4.0
WordPress 4.9.8

With WP Fail2Ban Redux enabled the following warning is logged on every request:
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function '_return_true' not found or invalid function name in www/wordpress/wp-includes/class-wp-hook.php on line 286

Plugin does not integrate cleanly with systemd-enabled systems

Systemd (long may it and its developer burn in hell) has come along and replaced all the sane and logical system-logging solutions we all used to love and adore. With that now being a fait accompli, we are all suffering as we try and learn how to use the incomprehensible behemoth that is systemd.
It would be nice/convenient/useful to have two options available for the provided wordpress.jail configuration file, one for wise systems that still use syslog-ng/rsyslog/etc, and one for the now more brain-dead systems with systemd. (Yes, I loath systemd with a fiery burning passion)

So far I've been able to adjust the jail config file to recognise the systemd backend, but have not been able to write and test the journalmatch entry to catch the php-generated auth messages from this plugin.

This seems to be a mostly-functional method of re-discovering /var/log/auth.log :

journalctl -q SYSLOG_FACILITY=10 SYSLOG_FACILITY=4

and from my brief poking at it with grep, it looks like we only need facility=4.
I've managed to get this far:
/etc/fail2ban/jail.d/wordpress.conf
backend = systemd
journalmatch = _SYSLOG_FACILITY=4

The provided filters (soft and hard) do catch authentication failures - yay! but I've not yet found a way to wrap this information into something I can properly test with fail2ban-regex. I could just wait and see if these users turn up in the jails, but I'd like a more concrete way of validating the config. The doco for 'fail2ban-regex systemd-journal' is again, abyss-like in its absence.
I'm seeing singular authentication attempts from IP addresses, but not multiple attempts, so they've possibly not met the jailing requirements yet.

I'm hoping that you've been able to install and configure this module within a systemd-disabled system, and thus can share your working configuration files?

is this supposed to work with systemd?

what are we supposed to use for systemd systems that dont have/use
logpath = /var/log/auth.log ??

I have tried the following, but it doesnt seem to work..

[wordpress-hard]
enabled = true
filter = wordpress-hard
logpath  = systemd[journalflags=1]
maxretry = 2
port = http,https

[wordpress-soft]
enabled = true
filter = wordpress-soft
logpath  = systemd[journalflags=1]
maxretry = 5
port = http,https

-db

Enabling filters generates PHP warnings

I created a custom plugin with the following add_filter entries:

add_filter( 'wp_fail2ban_redux_block_user_enumeration', '_return_true' );
add_filter( 'wp_fail2ban_redux_log_pingbacks', '_return_true' );
add_filter( 'wp_fail2ban_redux_log_spam_comments', '_return_true' );

And now I'm seeing hundreds of these entries in my debug.log:

[27-Sep-2017 06:07:07 UTC] PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function '_return_true' not found or invalid function name in /var/www/mysite.com/wp-includes/class-wp-hook.php on line 298

no logging

/var/log/auth.log is empty, ideas? Had the same issue with the original plugin and now your version.

Changelog update for 0.6.0

The changelog entry for 0.6.0 is missing. There is no way to tell what the changes are from the WordPress update methods. The previous version required server changes so it would be good to know if this version needs server adjustments or not.

wp_login_failed firing on wp-login.php page load

For some reason just hitting the wp-login.php page is triggering the wp_login_failed action. So visiting the page and then having 1 failed login attempt can trigger a jail.

Oct 13 12:24:56 usr wp(wpsite.com)[12345]: Authentication attempt for unknown user  from 1.2.3.4
Oct 13 12:24:56 usr wp(wpsite.com)[12345]: Authentication attempt for unknown user [email protected] from 1.2.3.4
Oct 13 12:24:56 usr wp(wpsite.com)[12345]:  INFO [wordpress-hard] Found 1.2.3.4
Oct 13 12:24:56 usr wp(wpsite.com)[12345]:  NOTICE [wordpress-hard] Ban 1.2.3.4

In cases where wp_login_failed is triggered, but no username exists ( caused when triggered on page load, no form data ) should we let it pass?

public function wp_login_failed( $username ) {
    ...
    if ( empty( $username ) )
        return;
    ...
}

Thoughts?

Shouldn't $indent be $ident?

Great plugin. I was trying to figure out the best way to integrate this into a wordpress site run in docker with fail2ban on the docker host, and while doing so have been looking at your code as well as the documentation. I came across this and I am not sure if I just don't understand php enough (totally possible) or it is a misspelling.

You reference $indent and I think it should be $ident as stated in the reference URL you provide https://secure.php.net/manual/en/function.openlog.php - note that if changed, the wiki documentation should be updated as well.

		/**
		 * Filters the $indent parameter, which will be the `[DAEMON]`
		 * portion of the example in the class PHPDoc, and will be passed to
		 * `openlog()`.
		 *
		 * @see https://secure.php.net/manual/function.openlog.php
		 *
		 * @since 0.1.0
		 *
		 * @param string $indent The syslog tag.
		 * @param string $action The logging action.
		 */
		$indent = apply_filters( 'wp_fail2ban_redux_openlog_indent', "wp({$_SERVER['HTTP_HOST']})", $action );

Nginx Reverse Proxy fail2ban shows the offender as coming from the reverse proxy, definitely dont want to ban that ip.

Nginx Reverse Proxy fail2ban shows the offender as coming from the reverse proxy, definitely don't want to ban your reverse proxy and cut off ALL traffic to your site.

I have all my headers in place on the reverse proxy:

# example HTTPS
server {
    listen 443 ssl http2;
    server_name www.example.com example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    root /var/www/example/;
    index index.html;
    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host:$server_port;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_pass http://10.18.20.2:44777;
    }
}

The solution that the other fail2ban package uses is working for me. I did originally try yours first.

This one allows you to define the IP of your proxy, and if defined it will use the X-Forwarded-For header:
https://docs.wp-fail2ban.com/en/4.2/defines/constants/WP_FAIL2BAN_PROXIES.html#wp-fail2ban-proxies

I was able to see in your code, that you say I just need to configure wp-config.php, I am wondering which values you think would actually solve this issue and be able to use your plugin behind a reverse proxy.

In my wp-config.php I have added these lines:

$_SERVER['HTTPS'] = 'on';
$_SERVER['HTTP_HOST'] = 'www.example.com';
define('WP_HOME','https://www.example.com');
define('WP_SITEURL','https://www.example.com');
define('WP_FAIL2BAN_PROXIES','10.18.10.1');

section of your code:

/**
 * Returns the remote IP address of the current visitor.
 *
 * We use `REMOTE_ADDR` here directly. If you are behind a proxy, you
 * should ensure that it is properly set, such as in wp-config.php, for
 * your environment.
 *
 * @see https://core.trac.wordpress.org/ticket/9235
 *
 * @since 0.1.0
 *
 * @return string The remote IP address.
 */
private static function get_remote_ip() {
  if ( empty( self::$ip ) ) {
    self::$ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
  }
  return self::$ip;
}

Not Banning Failed Attempts With Real Username

I’m seeing that I get bans on failed attempts to login with a username that doesn’t exist on a site. However, when failed attempts are made with a real username of the site, I’m not seeing any bans happen, which leads to people/bots to try to login as many times as they want. I’m using the latest version with wordpress-hard.conf. Any ideas? Thanks!

Running Fail2Ban version 0.11.1 on Ubuntu 20.04

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.