1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

IP Blocker in CPanel

Discussion in 'Security' started by jrbiz, Jun 26, 2018.

  1. #1
    I have hosting space via Hostgator. Over the years, for one of my sites that seemed to attract every bot on the planet, I have added IP addresses for the cpanel IP Blocker to deny access to. There are now about 28,000 of them, by my rough count. It turns out, that I may be blocking a number of countries that I now would like to allow access to due to a new business development. I asked hostgator support if there was a way to delete all of the blocked IP's (to start fresh) and they came back and said that I would have to delete them one by one. This could take a loooong time, of course.

    Any suggestions as to how I can remove these IP addresses from the IP Blocker in cpanel?
     
    jrbiz, Jun 26, 2018 IP
  2. pavv

    pavv Well-Known Member

    Messages:
    275
    Likes Received:
    8
    Best Answers:
    3
    Trophy Points:
    120
    #2
    The IPs are in the /home/youraccountname/public_html/.htaccess file.
    Like:
    ===
    deny from 1.1.1.1
    deny from 1.2.2.2
    etc
    ===

    You can remove them from this file. Take care not to delete other rules. Also, make a backup of the file first.
     
    pavv, Jun 26, 2018 IP
    jrbiz likes this.
  3. jrbiz

    jrbiz Acclaimed Member

    Messages:
    6,035
    Likes Received:
    2,612
    Best Answers:
    2
    Trophy Points:
    570
    #3
    Wow, thanks for your help! I will check that out!
     
    jrbiz, Jun 28, 2018 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #4
    After you remove all those IPs, put this at the very top of your site's index page (provided your index page is index.php). It will bounce every bad bot before it has a chance to crawl your site:

    
    <?php
    // ---------------------------------------------------------------------------------------------------------------
    // Banned IP Addresses and Bots - Redirects banned visitors who make it past the .htaccess and or robots.txt files to an URL.
    // The $banned_ip_addresses array can contain both full and partial IP addresses, i.e. Full = 123.456.789.101, Partial = 123.456.789. or 123.456. or 123.
    // Use partial IP addresses to include all IP addresses that begin with a partial IP addresses. The partial IP addresses must end with a period.
    // The $banned_bots, $banned_unknown_bots, and $good_bots arrays should contain keyword strings found within the User Agent string.
    // The $banned_unknown_bots array is used to identify unknown robots (identified by 'bot' followed by a space or one of the following characters _+:,.;/\-).
    // The $good_bots array contains keyword strings used as exemptions when checking for $banned_unknown_bots. If you do not want to utilize the $good_bots array such as
    // $good_bots = array(), then you must remove the the keywords strings 'bot.','bot/','bot-' from the $banned_unknown_bots array or else the good bots will also be banned.
    $banned_ip_addresses = array('');
    $banned_bots = array('.ru', 'AhrefsBot', 'crawl', 'crawler', 'DotBot', 'linkdex', 'majestic', 'meanpath', 'PageAnalyzer', 'robot', 'rogerbot', 'semalt', 'SeznamBot', 'spider');
    $banned_unknown_bots = array('bot ', 'bot_', 'bot+', 'bot:', 'bot,', 'bot;', 'bot\\', 'bot.', 'bot/', 'bot-');
    $good_bots = array('Google', 'Googlebot', 'MSN', 'bing', 'bingbot', 'Slurp', 'Yahoo', 'DuckDuck', 'STBot', 'Mediapartners-Google', 'ia_archiver');
    $banned_redirect_url = 'https://google.com';
    // Visitor's IP address and Browser (User Agent)
    $ip_address = $_SERVER['REMOTE_ADDR'];
    $browser = $_SERVER['HTTP_USER_AGENT'];
    // Declared Temporary Variables
    $ipfound = $piece = $botfound = $gbotfound = $ubotfound = '';
    // Checks for Banned IP Addresses and Bots
    if ($banned_redirect_url != '') {
        // Checks for Banned IP Address
        if (!empty($banned_ip_addresses)) {
            if (in_array($ip_address, $banned_ip_addresses)) {
                $ipfound = 'found';
            }
            if ($ipfound != 'found') {
                $ip_pieces = explode('.', $ip_address);
                foreach ($ip_pieces as $value) {
                    $piece = $piece . $value . '.';
                    if (in_array($piece, $banned_ip_addresses)) {
                        $ipfound = 'found';
                        break;
                    }
                }
            }
            if ($ipfound == 'found') {
                header("location: $banned_redirect_url");
                exit();
            }
        }
        // Checks for Banned Bots
        if (!empty($banned_bots)) {
            foreach ($banned_bots as $bbvalue) {
                $pos1 = stripos($browser, $bbvalue);
                if ($pos1 !== false) {
                    $botfound = 'found';
                    break;
                }
            }
            if ($botfound == 'found') {
                header("location: $banned_redirect_url");
                exit();
            }
        }
        // Checks for Banned Unknown Bots
        if (!empty($good_bots)) {
            foreach ($good_bots as $gbvalue) {
                $pos2 = stripos($browser, $gbvalue);
                if ($pos2 !== false) {
                    $gbotfound = 'found';
                    break;
                }
            }
        }
        if ($gbotfound != 'found') {
            if (!empty($banned_unknown_bots)) {
                foreach ($banned_unknown_bots as $bubvalue) {
                    $pos3 = stripos($browser, $bubvalue);
                    if ($pos3 !== false) {
                        $ubotfound = 'found';
                        break;
                    }
                }
                if ($ubotfound == 'found') {
                    header("location: $banned_redirect_url");
                    exit();
                }
            }
        }
    }
    // ---------------------------------------------------------------------------------------------------------------
    ?>
    
    Code (markup):
     
    qwikad.com, Jun 29, 2018 IP
    jrbiz likes this.
  5. jrbiz

    jrbiz Acclaimed Member

    Messages:
    6,035
    Likes Received:
    2,612
    Best Answers:
    2
    Trophy Points:
    570
    #5
    Hi @qwikad.com and thanks. Sounds like some good code to have there!
     
    jrbiz, Jun 29, 2018 IP