Plugin Directory

Changeset 1249476


Ignore:
Timestamp:
09/20/2015 05:10:09 AM (11 years ago)
Author:
achbed
Message:

Updated to v1.0.0

Location:
botnet-blocker/trunk
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • botnet-blocker/trunk/botnet-blocker.php

    r1249472 r1249476  
    55Description: Free botnet IP blocker according to public DNSBL bases. Based on public DNSBL class.
    66Author: Dennis Wallace
    7 Version: 0.2.0
     7Version: 1.0.0
    88License: GPLv2 or later
    99*/
     
    2525*/
    2626
    27 require_once( 'DNSBL.php' );    // see http://xbsoft.org/php/
    28 
    29 if ( ! class_exists( 'botnet_blocker' ) ) {
     27if ( ! class_exists( "DNSBL" ) ) {
     28    include_once( 'DNSBL.php' );    // see http://xbsoft.org/php/
     29}
     30
     31if ( ! class_exists( 'BNBlocker_Config' ) ) {
     32    include_once( 'class.bnblocker-config.php' );
     33}
     34
     35if ( ! class_exists( 'BNBlocker_Admin' ) ) {
     36    include_once( 'class.bnblocker-admin.php' );
     37}
     38
     39
     40if ( ! class_exists( 'Plugin_BNBlocker' ) ) {
    3041    /**
    3142     * Checks for a spammish IP at init time, and blocks/404s/handles it.
    3243     */
    33     class botnet_blocker {
    34         /**
    35          * The instance of a DNSBL object to use during lookups
    36          */
    37         private $dnsbl = null;
    38 
    39         /**
    40          * An array of addresses to skip checking.  Server address is included
    41          * automatically at object construction time.
    42          */
    43         public $skiplist = array(
    44             '127.0.0.1',
    45            
    46             // Sucuri incoming addresses
    47             '192.88.134.6',
    48             '192.88.135.6',
    49             '185.93.228.6',
    50             '185.93.229.6',
    51             '185.93.230.6',
    52             '192.88.134.0/23',
    53             '185.93.228.0/22',
    54             '192.124.249.0/24',
    55             '199.223.236.179',
    56             '146.148.117.213',
    57             '23.251.134.134',
    58             '178.33.238.180',
    59             '142.4.217.0/24',
    60             '167.114.0.0/24',
    61             '192.99.17.0/24',
    62             '5.196.79.0/24',
    63             '130.211.0.0/16',
    64             '104.155.0.0/16',
    65         );
    66        
    67         /**
    68          * An array of addresses that will be blocked if not on the whitelist.
    69          * Use with care.
    70          */
    71         public $blacklist = array(
    72         );
    73        
    74         /**
    75          * An array of addresses that will be granted access no matter what. 
    76          * Use with care - overrides blacklist and botnet membership checks.
    77          */
    78         public $whitelist = array(
    79         );
     44    class Plugin_BNBlocker {
     45        /**
     46         * Activate the plugin
     47         */
     48        public static function activate() {
     49        }
     50       
     51        /**
     52         * Deactivate the plugin
     53         */     
     54        public static function deactivate() {
     55        }
     56       
     57        /**
     58         * Uninstall the plugin
     59         */     
     60        public static function uninstall() {
     61        }
     62       
     63        /**
     64         * Things that run during the WP init action
     65         */
     66        public function handle_init() {
     67          load_plugin_textdomain( 'bnblocker', false, 'botnet-blocker/languages' );
     68        }
    8069       
    8170        /**
     
    8372         */
    8473        public function __construct() {
    85             $this->dnsbl = new DNSBL();
     74            $this->config = new BNBlocker_Config();
     75            if ( isset( $_GET['debug'] ) ) {
     76                header('X-BotnetBlocker-Debug-Mode: Debug Mode Enabled');
     77                $this->config->debug = 'on';
     78            }
     79           
    8680           
    8781            // Add the current server address to the skiplist
     
    8983                $this->skiplist[] = $_SERVER['SERVER_ADDR'];
    9084            }
     85           
     86            add_action( 'init', array( &$this, 'handle_init' ) );
    9187        }
    9288       
     
    9995            $this->timer_start();
    10096           
    101             $ips = array();
     97            $ips = '';
    10298            if ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
    103                 $ips = explode( ' ', str_replace(',', ' ', $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
    104             }
    105             $ips[] = $_SERVER['REMOTE_ADDR'];
     99                $ips = $_SERVER['HTTP_X_FORWARDED_FOR'];
     100            }
     101            $ips .= "\n".$_SERVER['REMOTE_ADDR'];
     102            $ips = $this->config->preplist( $ips );
    106103           
    107104            $result = false;
     
    109106            $blacklisted = false;
    110107
    111             if ( isset( $_GET['botnetwhite'] ) ) {
    112                 if ( empty( $_GET['botnetwhite'] ) ) {
    113                     $this->whitelist[] = $_SERVER['REMOTE_ADDR'];
    114                 } else {
    115                     $this->whitelist[] = $_GET['botnetwhite'];
    116                 }
    117             }
    118            
    119             if ( isset( $_GET['botnetblack'] ) ) {
    120                 if ( empty( $_GET['botnetblack'] ) ) {
    121                     $this->blacklist[] = $_SERVER['REMOTE_ADDR'];
    122                 } else {
    123                     $this->blacklist[] = $_GET['botnetblack'];
    124                 }
    125             }
    126            
    127             if ( isset( $_GET['debug'] ) ) {
     108            $skiplist = $this->config->skiplist();
     109            $blacklist = $this->config->blacklist();
     110            $whitelist = $this->config->whitelist();
     111           
     112            if ( $this->config->debug == 'on' ) {
    128113                header('X-BotnetBlocker-Debug-IPList: '.implode(';',$ips));
    129114                header('X-BotnetBlocker-Debug-CurrentIP: '.$_SERVER['REMOTE_ADDR']);
    130                 header('X-BotnetBlocker-Debug-SkipList: '.implode(';',$this->skiplist));
    131                 header('X-BotnetBlocker-Debug-Whitelist: '.implode(';',$this->whitelist));
    132                 header('X-BotnetBlocker-Debug-Blacklist: '.implode(';',$this->blacklist));
     115                header('X-BotnetBlocker-Debug-SkipList: '.implode(';',$skiplist));
     116                header('X-BotnetBlocker-Debug-Whitelist: '.implode(';',$whitelist));
     117                header('X-BotnetBlocker-Debug-Blacklist: '.implode(';',$blacklist));
    133118            }
    134119           
    135120            foreach ( $ips as $ip ) {
    136                 if ( ! empty( $ip ) ) {
    137                     if ( ! $this->netmatch_array( $ip, $this->skiplist ) ) {
    138                         if ( $this->netmatch_array( $ip, $this->whitelist ) ) {
    139                             $whitelisted = true;
    140                         }
     121                if ( empty( $ip ) ) {
     122                    continue;
     123                }
     124               
     125                if ( $this->netmatch_array( $ip, $skiplist ) ) {
     126                    continue;
     127                }
     128               
     129                if ( $this->netmatch_array( $ip, $whitelist ) ) {
     130                    $whitelisted = true;
     131                    continue;
     132                }
    141133                       
    142                         if ( $this->netmatch_array( $ip, $this->blacklist ) ) {
    143                             $blacklisted = true;
    144                         }
     134                if ( $this->netmatch_array( $ip, $blacklist ) ) {
     135                    $blacklisted = true;
     136                    continue;
     137                }
    145138                       
    146                         if ( ( $whitelisted || $blacklisted || $result ) == false) {
    147                             // Only check for botnet membership if we've not already decided
    148                             if ( $this->dnsbl->CheckSpamIP( $ip ) ) {
    149                                 $result = true;
    150                             }
    151                         }
     139                if ( ( $whitelisted || $blacklisted || $result ) == false) {
     140                    // Only check for botnet membership if we've not already decided
     141                    if ( $this->config->dnsbl->CheckSpamIP( $ip ) ) {
     142                        $result = true;
    152143                    }
    153144                }
     
    157148           
    158149            if ( $whitelisted ) {
    159                 if ( isset( $_GET['debug'] ) ) {
     150                if ( $this->config->debug == 'on' ) {
    160151                    header('X-BotnetBlocker-Debug-Result: Whitelist');
    161152                }
     
    164155           
    165156            if ( $blacklisted ) {
    166                 if ( isset( $_GET['debug'] ) ) {
     157                if ( $this->config->debug == 'on' ) {
    167158                    header('X-BotnetBlocker-Debug-Result: Blacklist');
    168159                }
     
    170161            }
    171162           
    172             if ( isset( $_GET['debug'] ) ) {
     163            if ( $this->config->debug == 'on' ) {
    173164                header('X-BotnetBlocker-Debug-Result: ' . ( $result ? 'Bot' : 'OK' ) );
    174165            }
     
    199190            list ( $net, $mask, $extra ) = explode ( '/', $cidr );
    200191            $mask = intval( $mask );
    201             return ( ip2long ($ip) & ~((1 << (32 - $mask)) - 1) ) == ip2long ($net);
     192            $net = ip2long ($net) & ~((1 << (32 - $mask)) - 1); // Enforce the mask on the network too (just in case)
     193            return ( ( ip2long ($ip) & ~((1 << (32 - $mask)) - 1) ) == $net );
    202194        }
    203195       
     
    239231}
    240232
    241 $BOTNETBLOCKER_OBJ = new botnet_blocker();
     233if ( class_exists( 'Plugin_BNBlocker' ) && class_exists( 'BNBlocker_Admin' ) && function_exists( 'register_activation_hook' ) ) {
     234    // Installation and uninstallation hooks
     235    register_activation_hook(__FILE__, array('Plugin_BNBlocker', 'activate'));
     236    register_deactivation_hook(__FILE__, array('Plugin_BNBlocker', 'deactivate'));
     237    register_uninstall_hook(__FILE__, array('Plugin_BNBlocker', 'uninstall'));
     238
     239    // instantiate the plugin class
     240    global $wp_plugin_bnblocker;
     241    $wp_plugin_bnblocker = new Plugin_BNBlocker();
     242    $wp_plugin_bnblocker_admin = new BNBlocker_Admin();
     243}
  • botnet-blocker/trunk/readme.txt

    r1249472 r1249476  
    44Requires at least: -.-.-
    55Tested up to: 4.3.1
    6 Stable tag: 0.2.0
     6Stable tag: 1.0.0
    77
    88Botnet IP checker using public DNSBL bases and a hardcoded whitelist/blacklist.
     
    2121```
    2222<?php
    23   global $BOTNETBLOCKER_OBJ;
    24     if ( method_exists( $BOTNETBLOCKER_OBJ, 'is_botnet' ) ) {
    25         if ( $BOTNETBLOCKER_OBJ->is_botnet() ) {
     23  global $wp_plugin_bnblocker;
     24    if ( method_exists( $wp_plugin_bnblocker, 'is_botnet' ) ) {
     25        if ( $wp_plugin_bnblocker->is_botnet() ) {
    2626      /* bot detected, do something */
    2727    }
     
    5555Added timing checks for debugging speed issues
    5656Added Sucuri network addresses to skip list
     57
     58= 1.0.0 =
     59Complete refactor of the Plugin
     60Now has an admin section for modifying white/black/skip lists within the UI
     61Allows choosing which RBL in the UI
     62Revised logic to improve speed
     63Now uses CIDR-formatted netmasks for better maintenance
     64BREAKING CHANGE: Global object has been renamed from $BOTNETBLOCKER_OBJ to $wp_plugin_bnblocker
Note: See TracChangeset for help on using the changeset viewer.