Skip to content

Custom Logging Actions

Brandon Allen edited this page May 31, 2017 · 4 revisions

WP Fail2Ban Redux includes a log class whose methods can be to create custom logging rules.

Using the WP_Fail2Ban_Redux_Log class to create custom rules

The below example rule is based on Simple Honeypot by Kailey Lampert.

<?php
/**
 * Plugin Name:     Simple Honeypot for WP Fail2Ban Redux
 * Plugin URI:      https://gist.github.com/trepmal/9928ab0e023087da8a11#file-simple-honeypot-php
 * Description:     Basic honeypot for comment form with fail2ban integration. Requires: https://wordpress.org/plugins/wp-fail2ban-redux/
 * Author:          Brandon Allen
 * Author URI:      https://github.com/trepmal
 * Text Domain:     simple-honeypot-for-wp-fail2ban-redux
 * Domain Path:     /languages
 */

// Only add our actions if WP Fail2Ban Redux is loaded.
function sh_setup_actions() {
	add_action( 'comment_form_after_fields', 'sh_comment_form_after_fields' );
	add_action( 'pre_comment_on_post', 'sh_pre_comment_on_post' );
}
add_action( 'wp_fail2ban_redux_loaded', 'sh_setup_actions' );

function sh_comment_form_after_fields() {
	$field = '<span style="display: none;"><input type="text" name="sh" /></span>';
	echo apply_filters( 'honeypot_field', $field, $name );
}

function sh_pre_comment_on_post() {
	if ( isset( $_POST['sh'] ) && ! empty( $_POST['sh'] ) ) {
		$wpf2br = WP_Fail2Ban_Redux::get_instance();
		$wpf2br->openlog( 'simple_honeypot' );
		$wpf2br->syslog( 'Bot in the Honeypot' );
		$wpf2br->_exit( 'simple_honeypot' );
	}
}

That's it! One would, of course, need to make sure that either the wordpress-hard.conf or wordpress-soft.conf filters are updated with the new log message.

Clone this wiki locally