Giter Club home page Giter Club logo

waf4wordpress's Introduction

WAF for WordPress

License PHP Version Packagist stats Latest version PHPStan enabled

Stop real-life attacks on your WordPress website and trigger Fail2Ban.

๐Ÿ’ก Before using this WAF you have to clean your website, get rid of even tiny errors. See your access and error logs daily and run this WAF without Fail2ban for a week.

This WAF does not give proper HTTP responses to unusual requests. It blocks the attacking IP address instantly, the purpose of this are the following.

  1. Prevent website compromise in further requests
  2. Prevent DoS attacks

Shared hosting has no server-wide banning (because of trust issues) but you can still install this software without Fail2Ban to stop attacks by using one of the Miniban methods.

Support my work

Please consider sponsoring me monthly if you use my packages in an agency.

Sponsor

Theory

Your WordPress - really general HTTP - security consists of the followings.

  1. Use HTTPS
  2. Have daily backups
  3. Block known hostile networks
  4. Have Fail2Ban installed (controls the firewall)
  5. Maintain your website and use strict Fail2Ban filters which ban on the first suspicious request instantly
  6. Deny direct access to core WordPress files, themes and plugins
  7. Install WAF for WordPress (this project)
  8. Use Leanmail for filtering Fail2Ban notification emails

See the Block WordPress attack vectors note in my other repository for an overview of the topic.

Composer installation

Technically this is not a WordPress plugin nor an MU plugin. WAF for WordPress is distributed and autoloaded as a Composer package.

  1. Issue composer require szepeviktor/waf4wordpress command
  2. Load vendor/autoload.php from your wp-config
  3. Instantiate SzepeViktor\WordPress\Waf\HttpAnalyzer class early in wp-config
    require dirname(__DIR__) . '/vendor/autoload.php';
    new SzepeViktor\WordPress\Waf\HttpAnalyzer();
  4. Create an MU plugin in wp-content/mu-plugins/waf4wordpress.php
    <?php
    /*
     * Plugin Name: WAF for WordPress (MU)
     */
    if (! function_exists('add_filter')) {
        exit;
    }
    new SzepeViktor\WordPress\Waf\CoreEvents();

Manual installation

๐Ÿ’ก Please see Composer-managed WordPress for managing WordPress with Composer.

Technically this is not a WordPress plugin nor an MU plugin.

  1. First download WAF for WordPress then extract files to a directory, e.g. waf/
  2. Instantiate SzepeViktor\WordPress\Waf\HttpAnalyzer class early in wp-config
    require_once __DIR__ . '/waf/src/HttpAnalyzer.php';
    require_once __DIR__ . '/waf/src/CoreEvents.php';
    new SzepeViktor\WordPress\Waf\HttpAnalyzer();
  3. Create an MU plugin in wp-content/mu-plugins/waf4wordpress.php
    <?php
    /*
     * Plugin Name: WAF for WordPress (MU)
     */
    if (! function_exists('add_filter')) {
        exit;
    }
    new SzepeViktor\WordPress\Waf\CoreEvents();

Configuration

WAF for WordPress is configured in source code before class instantiation. in wp-config.

Create custom filters for Fail2Ban catching these PHP messages.

  • Likely malicious requests: Malicious traffic detected: may be banned after 6 attempts per 10 minutes
  • Surely break-in attempts: Break-in attempt detected: may be banned instantly

How to support PayPal IPN, Braintree and custom entry points in poorly written plugins

Copy this into your in wp-config.php.

// Enable PayPal IPN in WooCommerce
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
    if ( '/wc-api/WC_Gateway_Paypal/' === parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ) ) {
        // PayPal IPN does not send Accept: and User-Agent: headers
        $_SERVER['HTTP_ACCEPT'] = '*/*';
        $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 PayPal/IPN';
    }
}

// Enable Braintree Webhooks
new \Waf4WordPress\Braintree_Fix( '/braintree/webhook' );

// Enable email opens in Newsletter plugin
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
    $newsletter_path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
    if ( '/wp-content/plugins/newsletter/statistics/open.php' === $newsletter_path
        || '/wp-content/plugins/newsletter/statistics/link.php' === $newsletter_path
    ) {
        // UA hack for old email clients
        $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 ' . $_SERVER['HTTP_USER_AGENT'];
    }
    unset( $newsletter_path );
}

// Enable email open tracking in ALO EasyMail Newsletter plugin
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
    $alo_path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
    if ( '/wp-content/plugins/alo-easymail/tr.php' === $alo_path ) {
        // UA hack for old email clients
        $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 ' . $_SERVER['HTTP_USER_AGENT'];
    }
    unset( $alo_path );
}

Bug reports and feature requests

Open a new issue

Where script kiddies look for WordPress

  • /backup/
  • /blog/
  • /cms/
  • /demo/
  • /dev/
  • /home/
  • /main/
  • /new/
  • /old/
  • /portal/
  • /site/
  • /test/
  • /tmp/
  • /web/
  • /wordpress/
  • /wp/

Best not to create these directories to avoid lengthy log excerpts.

waf4wordpress's People

Contributors

leonstafford avatar szepeviktor 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

waf4wordpress's Issues

Welcome to wpf2b

Hello @lkraav !

I've just realized you starred this repo.

Could I help you to set it up (for free) ?

Plans for Http_Analyzer

  • How to restrict AJAX content type?
  • Block CDN attacks by what method?
  • order of headers to identify attackers
  • check POST: no more, no less variables
    a:5:{s:11:"redirect_to";s:28:"http://domain.com/wp-admin/";s:10:"testcookie"; s:1:"1";s:3:"log";s:5:"admin";s:3:"pwd";s:6:"123456";s:9:"wp-submit";s:6:"Log In";}
  • wp-login POST: login, postpass, resetpass, lostpassword, register
  • wp-login GET: logout, rp, lostpassword
  • non-login wp-login POST-s: comment, trackback, pingback, XML-RPC, WP-API, plugin POST-s

Plans for Core_Events

  • robots & errors in /wp-comments-post.php (as in block-bad-requests.inc)
  • non-attack 404:
    logsearch.sh -e wpf2b_404|sed -ne 's|.*wpf2b_404 (s:[0-9]\+:"\([^"]*\)";).*|\1|p'|grep -vx "/[a-z/-]\+/\|.*\.jpg"|sort
    • non-ascii post slugs
  • fake Googlebot, Referer: http://www.google.com
    grep -hi ' "[^"]*Googlebot[^"]*"$' /var/log/apache2/*access.log|grep -v "^66\.249\."
  • core: No filter for successful XMLRPC login in wp_authenticate()
  • write test.sh
  • robot requests not through /index.php (exclude: xmlrpc, trackback, see: wp_403())
  • append: http://plugins.svn.wordpress.org/block-bad-queries/trunk/block-bad-queries.php
  • option to immediately ban on non-WP scripts (.php$ .aspx?$)
  • new: invalid user during lost password
  • new: invalid "lost password" token
  • log xmlrpc? add_action( 'xmlrpc_call', function( $call ) { if ( 'pingback.ping' == $call ) {} } );
  • log proxy IP: HTTP_X_FORWARDED_FOR, HTTP_INCAP_CLIENT_IP, HTTP_CF_CONNECTING_IP backed by mod_remoteip
  • registration errors: the dirty way
    add_filter( 'login_errors', function ( $em ) { error_log( 'em:' . $em ); return $em; }, 0 );

You are free to try this WAF

Hello @Kladizkov
In your wpcli video you are using iTheme's Security.
I was contributing to it when it was called Better WordPress Security. Then I've realized that available WordPress security plugins are liberal, fits-all solutions.

This waf blocks 50 attack vectors. It triggers Fail2ban (a linux daemon) but you may use the included Miniban class although you are required to install it manually.

It consists of two php files:

  • an HTTP analyzer - load it from your wp-config
  • an MU plugin - copy it to wp-content/mu-plugins/

Feel free to ask for support.

All the best to you!

Object oriented rewrite

Hello @Jakiboy!

Thank you for your star โญ

Would you like to help to design the API for configuring this WAF?
Currently a legacy technology is in use: global constants.

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.