#!/usr/bin/perl -w

# Program: devilbanner
# Purpose: Block offensive IPs temporarily
# $Id: userbox_devilbanner.erb,v 1.4 2017/05/22 16:17:28 apayne Exp $

use strict;
use Getopt::Long;
use Fcntl qw( O_RDWR O_CREAT LOCK_EX);
use Sys::Syslog qw(:standard :macros);
use Net::IP qw( ip_is_ipv4 );
use Net::IP::Match::Regexp qw( create_iprange_regexp match_ip );

my $banfile = "/var/cpanel/devilips";
my $expireafter = 7200;
my $release = undef;
my $releaseall = undef;
my $quiet = undef;
my $single = undef;
my $stdin = undef;
my $help = undef;
my $iptables = "/sbin/iptables";
my $iproute2 = "/sbin/ip";
my $conntrack = "/usr/sbin/conntrack";

GetOptions
  "banfile=s" => \$banfile,
  "expire=i" => \$expireafter,
  "release=s" => \$release,
  "release-all" => \$releaseall,
  "singleip" => \$single,
  "stdin" => \$stdin,
  "quiet" => \$quiet,
  "help" => \$help,
  or &help();
&help() if $help;

sub help {
  print "\n$0 [--quiet] [--banfile $banfile ] [ --expire $expireafter ] [--release IP ] [ --release-all ] [--singleip] { --stdin | IPs ... }\n\n";
  exit;
}

# Fire up the logger
openlog('devilbanner', 'cons,pid', 'notice');

if ($release) {
  $release =~ s/\.\d+$/\.0/ unless $single;
  unban($release);
} elsif ($releaseall) {
  my $banned = loadbanned();
  stripbanned();
} else {
  if ($stdin) {
    output("Reading IPs from STDIN.  Hit CTRL-C to abort:");
    while (<STDIN>) {
      chomp;
      my ($banip) = split;
      push @ARGV, $banip;
    }
  }
  if (@ARGV) {
    my $banned = loadbanned();
    foreach my $banip (@ARGV) {
      my $realbanip = $banip;
      $banip =~ s/\.\d+$/\.0/ unless $single;
      if ($banned->{$banip}) {
        output("$banip was already banned.");
      } else {
        $banned->{$banip} = time;
        ban($banip, $realbanip);
      }
    }
    stripbanned();
  } else {
    &help();
  }
}

closelog();

sub loadbanned {
  sysopen (DB, $banfile, O_RDWR | O_CREAT ) or die "$banfile: open: $!\n";
  flock(DB, LOCK_EX) or die "$banfile: flock: $!\n";
  my %banned = ();
  while (<DB>) {
    my ($time,$banip) = split;
    $banned{$banip} = $time;
  }
  return \%banned;
}

sub ban {
  my $ip = shift;
  my $realip = shift;
  unless (ip_is_ipv4($ip)) {
    output("IP [$ip] TOO UGLY TO BAN?");
    return;
  }
  my @localips = `$iproute2 -4 addr show | awk '/inet/ {gsub(/\\/.*/,"",\$2); if (\$2 != "127.0.0.1") print \$2}'`;
  chomp @localips;
  my $dgate = `ip route | awk '/default/ {match(\$0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/); ip = substr(\$0,RSTART,RLENGTH); print ip}'`;
  chomp $dgate;
  unless (ip_is_ipv4($dgate)) {
    output("Could not find default gateway");
    return;
  }
  my $friendly_ip_regex = &gen_friendly_regex();
  if (match_ip($ip, $friendly_ip_regex)) {
    output("IP [$ip] TOO FRIENDLY TO BLOCK!");
    return;
  } elsif (grep(/$ip/, @localips)){
    output("IP [$ip] is local");
    return;
  } elsif ( $ip =~ m/^$dgate$/){
    output("IP [$ip] is default gateway");
    return;
  } elsif ($realip) {
    my ($sip) = $realip =~ /(\d+\.\d+\.\d+\.)/;
    if ($sip and (grep(/$sip/, @localips) or $sip =~ m/^$dgate/)){
      output("IP [$realip] shares a subnet with a local IP. Can only ban with --singleip");
      return;
    }
  }
  print DB time(), " $ip\n";
  my $prefix = ($ip =~ /\.0$/) ? 24 : 32;
  #my @cmd = ($iptables => -I => INPUT => "--src" => "$ip/$prefix" => -j => "DROP");
  my @cmd = ($iproute2 => route => add => blackhole => "$ip/$prefix");
  output("RUNNING: ", join(" ", @cmd));
  system @cmd;
  @cmd = ( $conntrack, "-D", "-s", "$realip");
  output("RUNNING: ", join(" ", @cmd));
  system @cmd;
}

sub unban {
  my $ip = shift;
  my $banned = loadbanned();
  if ($banned->{$ip}) {
    output("Releasing $ip ...");
    stripbanned($ip);
  } else {
    output("$ip was already not banned.");
  }
}


sub stripbanned {
  my $release = shift;
  my $wipe_before = time - $expireafter;
  seek (DB, 0, 0);
  my $CONTENT = "";
  while (<DB>) {
    my ($when, $ip) = split;
    if ($when > $wipe_before and
        (!$release or
         $release ne $ip)) {
      $CONTENT .= $_;
    } else {
      my $prefix = ($ip =~ /\.0$/) ? 24 : 32;
      #my @cmd = ($iptables => -D => INPUT => "--src" => "$ip/$prefix" => -j => "DROP");
      my @cmd = ($iproute2 => route => del => blackhole => "$ip/$prefix");
      output("RUNNING: ",join(" ",@cmd));
      system @cmd;
    }
  }
  seek (DB, 0, 0);
  print DB $CONTENT;
  truncate(DB, length $CONTENT);
  close(DB);
}

sub output {
  my @sure = @_ or die "You have to output something $!";
  my $text = join(' ', @sure);
  syslog(LOG_ERR, $text);
  print STDERR "$text\n" unless $quiet;
  return 1;
}

# net ipblock_list select cidr
sub gen_friendly_regex {
  my @net_ip_range = (
    '0.0.0.0/8',
    '10.0.0.0/8',
    '103.11.65.66/32',
    '103.16.130.114/32',
    '103.200.6.77/32',
    '103.200.7.12/32',
    '103.21.58.0/23',
    '103.224.23.0/24',
    '103.236.162.175/32',
    '103.47.154.109/32',
    '104.140.107.17/32',
    '104.140.20.131/32',
    '104.140.20.235/32',
    '104.140.246.165/32',
    '104.140.247.140/32',
    '104.156.247.194/32',
    '104.206.223.21/32',
    '104.207.130.121/32',
    '104.207.142.102/32',
    '104.207.150.184/32',
    '104.219.169.152/32',
    '104.219.169.160/32',
    '104.219.169.168/32',
    '104.219.169.17/32',
    '104.236.114.81/32',
    '104.236.141.173/32',
    '104.236.141.173/32',
    '104.236.16.22/32',
    '104.236.53.140/32',
    '104.238.181.138/32',
    '104.238.181.78/32',
    '104.238.186.171/32',
    '104.244.209.20/32',
    '104.244.211.30/32',
    '104.245.35.232/32',
    '104.36.18.45/32',
    '107.172.55.204/32',
    '108.167.128.0/22',
    '108.167.132.0/24',
    '108.167.133.0/26',
    '108.167.133.128/25',
    '108.167.134.0/23',
    '108.167.136.0/23',
    '108.167.138.0/23',
    '108.167.140.0/22',
    '108.167.144.0/20',
    '108.167.160.0/24',
    '108.167.161.0/24',
    '108.167.163.0/24',
    '108.167.164.0/22',
    '108.167.168.0/22',
    '108.167.172.0/22',
    '108.167.176.0/22',
    '108.167.180.0/22',
    '108.167.184.0/22',
    '108.167.188.0/22',
    '108.179.192.0/22',
    '108.179.196.0/22',
    '108.179.200.0/22',
    '108.179.204.0/23',
    '108.179.206.0/23',
    '108.179.208.0/20',
    '108.179.224.0/22',
    '108.179.228.0/22',
    '108.179.232.0/22',
    '108.179.236.0/22',
    '108.179.240.0/21',
    '108.179.248.0/22',
    '108.179.252.0/23',
    '108.179.254.0/24',
    '108.179.255.0/26',
    '108.179.255.128/26',
    '108.179.255.192/26',
    '108.179.255.64/26',
    '108.61.203.16/32',
    '108.61.209.12/32',
    '108.61.217.92/32',
    '111.67.17.197/32',
    '112.213.32.133/32',
    '114.55.3.152/32',
    '116.93.97.116/32',
    '119.254.34.70/32',
    '119.81.237.98/32',
    '119.81.237.99/32',
    '119.82.28.190/32',
    '120.138.27.125/32',
    '120.138.30.84/32',
    '120.27.24.218/32',
    '120.55.164.6/32',
    '121.244.91.46/32',
    '122.15.156.174/32',
    '122.152.205.52/32',
    '123.207.67.246/32',
    '127.0.0.0/8',
    '130.193.12.189/32',
    '131.255.5.230/32',
    '134.19.176.31/32',
    '134.19.176.80/32',
    '134.19.176.81/32',
    '134.19.176.82/32',
    '138.68.2.184/32',
    '138.68.233.195/32',
    '139.162.14.50/32',
    '139.162.187.173/32',
    '139.162.59.38/32',
    '139.196.199.219/32',
    '139.196.202.229/32',
    '139.59.12.64/32',
    '142.234.160.204/32',
    '142.4.0.0/19',
    '148.163.220.12/32',
    '148.163.220.163/32',
    '149.154.152.93/32',
    '154.16.245.251/32',
    '154.66.197.14/32',
    '154.66.198.59/32',
    '158.58.170.74/32',
    '159.203.1.147/32',
    '159.203.200.222/32',
    '159.203.37.151/32',
    '162.144.0.0/16',
    '162.210.173.201/32',
    '162.213.36.11/32',
    '162.213.37.53/32',
    '162.214.0.0/15',
    '162.218.229.234/32',
    '162.240.0.0/15',
    '162.243.55.64/32',
    '162.243.75.49/32',
    '162.253.54.200/32',
    '162.254.116.12/32',
    '165.254.168.111/32',
    '169.57.14.116/32',
    '169.57.14.125/32',
    '169.57.14.126/32',
    '169.57.166.128/27',
    '169.57.166.232/29',
    '169.61.69.29/32',
    '172.106.33.118/32',
    '172.16.0.0/12',
    '172.26.23.94/32',
    '173.193.201.67/32',
    '173.212.227.39/32',
    '173.213.96.152/32',
    '173.232.228.20/32',
    '173.254.0.0/17',
    '173.255.252.4/32',
    '174.129.225.90/32',
    '175.41.141.147/32',
    '176.28.13.209/32',
    '176.58.98.218/32',
    '176.9.204.27/32',
    '177.54.146.183/32',
    '177.71.177.249/32',
    '178.209.40.103/32',
    '178.62.238.39/32',
    '179.43.191.26/32',
    '179.50.12.213/32',
    '180.149.241.212/32',
    '182.160.154.27/32',
    '182.54.235.47/32',
    '182.92.238.235/32',
    '184.73.189.166/32',
    '184.95.56.8/32',
    '185.117.118.126/32',
    '185.134.29.181/32',
    '185.153.248.168/32',
    '185.167.161.11/32',
    '185.20.209.150/32',
    '185.3.95.86/32',
    '185.56.90.140/32',
    '185.72.156.40/32',
    '186.226.56.241/32',
    '186.226.57.44/32',
    '186.226.60.108/32',
    '188.166.3.204/32',
    '191.96.102.6/32',
    '192.140.56.150/32',
    '192.163.192.0/19',
    '192.163.224.0/19',
    '192.168.0.0/16',
    '192.185.0.0/16',
    '192.232.192.0/18',
    '192.241.175.194/32',
    '192.254.128.0/17',
    '192.3.120.85/32',
    '192.34.60.107/32',
    '192.34.85.134/32',
    '192.73.242.23/32',
    '192.81.129.136/32',
    '194.68.44.228/32',
    '198.1.64.0/18',
    '198.154.192.0/20',
    '198.154.208.0/20',
    '198.154.224.0/19',
    '198.20.224.0/20',
    '198.20.240.0/21',
    '198.20.248.0/22',
    '198.20.252.0/22',
    '198.204.227.50/32',
    '198.204.248.186/32',
    '198.211.100.169/32',
    '198.57.128.0/17',
    '198.7.63.217/32',
    '198.74.56.175/32',
    '198.74.62.39/32',
    '199.19.214.79/32',
    '199.201.88.82/32',
    '199.58.196.0/22',
    '204.141.32.226/32',
    '204.141.42.0/32',
    '207.174.212.0/24',
    '207.244.103.237/32',
    '207.244.104.199/32',
    '207.244.104.233/32',
    '207.244.104.49/32',
    '207.244.105.239/32',
    '207.244.112.213/32',
    '207.244.97.125/32',
    '209.50.49.76/32',
    '209.50.50.39/32',
    '212.7.217.23/32',
    '213.219.39.144/32',
    '213.32.55.210/32',
    '216.12.194.43/32',
    '216.172.160.0/24',
    '216.172.161.0/24',
    '216.172.162.0/23',
    '216.172.164.0/22',
    '216.172.168.0/22',
    '216.172.172.0/23',
    '216.172.174.0/23',
    '216.172.176.0/22',
    '216.172.180.0/22',
    '216.172.184.0/21',
    '216.189.157.216/32',
    '216.230.231.142/32',
    '216.52.72.226/32',
    '217.112.154.222/32',
    '217.170.207.93/32',
    '217.78.0.115/32',
    '217.78.6.89/32',
    '223.165.76.47/32',
    '223.27.24.156/32',
    '23.111.69.169/32',
    '23.111.69.170/32',
    '23.111.74.105/32',
    '31.186.226.150/32',
    '31.193.180.147/32',
    '31.193.181.14/32',
    '37.235.48.47/32',
    '37.235.50.96/32',
    '37.235.56.210/32',
    '39.108.195.216/32',
    '43.230.27.46/32',
    '45.114.116.186/32',
    '45.32.144.240/32',
    '45.32.225.170/32',
    '45.32.229.88/32',
    '45.32.254.120/32',
    '45.33.0.252/32',
    '45.33.110.8/32',
    '45.33.117.219/32',
    '45.33.33.28/32',
    '45.33.65.221/32',
    '45.58.36.104/32',
    '45.63.10.251/32',
    '45.63.106.106/32',
    '45.63.84.131/32',
    '45.63.84.88/32',
    '45.63.87.133/32',
    '45.63.88.36/32',
    '45.63.93.3/32',
    '45.76.0.57/32',
    '45.79.102.189/32',
    '45.79.199.86/32',
    '45.79.2.230/32',
    '45.79.213.84/32',
    '45.79.67.12/32',
    '45.79.71.211/32',
    '45.79.98.201/32',
    '46.151.215.109/32',
    '46.151.215.91/32',
    '46.183.117.164/32',
    '46.183.118.235/32',
    '46.235.10.119/32',
    '47.91.105.8/32',
    '5.134.117.149/32',
    '5.189.131.209/32',
    '5.189.150.43/32',
    '5.189.164.222/32',
    '5.226.169.82/32',
    '5.77.35.99/32',
    '5.77.40.196/32',
    '50.116.104.0/22',
    '50.116.108.0/22',
    '50.116.112.0/22',
    '50.116.116.0/22',
    '50.116.120.0/22',
    '50.116.124.0/22',
    '50.116.38.61/32',
    '50.116.64.0/21',
    '50.116.72.0/22',
    '50.116.76.0/22',
    '50.116.80.0/20',
    '50.116.96.0/21',
    '50.2.81.144/32',
    '50.3.30.119/32',
    '50.87.0.0/16',
    '51.254.124.182/32',
    '52.66.168.144/32',
    '52.78.86.95/32',
    '54.197.228.117/32',
    '54.232.116.163/32',
    '54.248.99.81/32',
    '54.252.99.178/32',
    '54.253.123.89/32',
    '65.154.166.0/32',
    '66.147.240.0/20',
    '66.187.76.125/32',
    '67.20.64.0/18',
    '67.222.32.0/19',
    '67.231.28.122/32',
    '67.231.28.126/32',
    '69.195.64.0/18',
    '69.87.223.220/32',
    '69.89.16.0/20',
    '70.40.192.0/19',
    '72.44.88.41/32',
    '72.5.230.111/32',
    '72.5.230.65/32',
    '74.207.251.164/32',
    '74.220.192.0/19',
    '75.101.151.107/32',
    '76.8.60.8/32',
    '77.73.69.144/32',
    '77.81.104.105/32',
    '77.81.107.72/32',
    '78.108.38.106/32',
    '79.124.7.245/32',
    '79.125.119.192/32',
    '79.99.0.185/32',
    '79.99.1.106/32',
    '8.40.222.226/32',
    '8.45.169.0/32',
    '80.87.196.114/32',
    '82.103.140.78/32',
    '82.113.131.197/32',
    '82.113.131.198/32',
    '82.196.8.112/32',
    '83.136.248.38/32',
    '83.136.252.90/32',
    '87.252.215.28/32',
    '91.219.236.236/32',
    '91.228.126.149/32',
    '94.237.81.16/32',
    '94.242.246.57/32',
    '94.46.160.215/32',
    '95.141.37.20/32',
    '96.125.160.0/20',
    '96.126.102.94/32',
  );
  my $net_regex = create_iprange_regexp(@net_ip_range);
  return $net_regex;
}
