#! /usr/bin/env perl
use Sys::Hostname;
use LWP::UserAgent;
use Digest::MD5 qw(md5_hex);
use JSON qw(encode_json);
use strict;
use warnings;
my $hostname = hostname();
our $version = 'v0.1.0';
my $who = (defined($ENV{RUSER}) && $ENV{RUSER} =~ /^\w+$/) ? $ENV{RUSER} : 'support';
my $bldir = '/opt/mod_security/blacklists';

sub foundnone { exit 1 }

sub usage {
    print <<"USAGE";
usage: blmodsec <block|robot> <#h|#d> <REQUEST REGEX> <DOMAIN>
usage: blmodsec <blockall|robotall> <#h|#d> <REQUEST REGEX>
usage: blmodsec <blockdom|robotdom> <#h|#d> <DOMAIN>
usage: blmodsec <blockua|robotua> <#h|#d> <UA>

  --help      # display this text
  --version   # display the version

usage examples:

  blmodsec robot 2d 'index[.]php[?][0-9]+=[0-9]+' under-attack.com
    # For 2 days, require visitors to under-attack.com (exact match) to pass a
    # transparent anti-bot check if their request URI contains e.g.
    # index.php?123=123

  blmodsec robot 2d 'index[.]php[?][0-9]+=[0-9]+' .under-attack.com
    # Same, for this domain or any subdomain of it.

  blmodsec blockall 2h /wlwmanifest.xml
    # For 2 hours, block all request URIs containing /wlmanifest.xml ,
    # regardless of the domain they're for.

  blmodsec blockdom 6h .abused.com
    # For 6 hours, block all traffic to abused.com or any subdomain of it.
    # Without the leading . only abused.com would be blocked.

  blmodsec robotua 2d python-requests
    # for 2 days, require all traffic with a UA containing python-requests
    # to pass a transparent anti-abit check.

other commands:

  blmodsec list      # list any active blocks on this server
  blmodsec list <id> # list specific blocks on this server
  blmodsec stats     # report on effectiveness of any active blocks
  blmodsec expire    # remove any rules that are past their time

  blmodsec reset <#h|#d> <id>  # give rule a new expiry time

  blmodsec <id> <anything here>
                    # annotate blacklist rule with human-readable text
                    # ex. blmodsec 777003 EMCSI-1234 brute force
                    # to add "# EMCSI-1234 brute force" to 777003.conf

Rule files are created under $bldir .
Bearing in mind that ALL *.conf files in this directory will be loaded,
and that Apache can SUDDENLY GO DOWN if there is an invalid *.conf file
in this directory, you can manually inspect these files.

USAGE
    exit 1;
}

for (@ARGV) {
    if ($_ eq '-h' or $_ eq '--help') { usage() }
    elsif ($_ eq '--version') {
        print "blmodsec $version\n";
        exit 1;
    }
}
if (@ARGV == 4 && $ARGV[0] =~ /^(?:block|robot)$/) { block(@ARGV) }
elsif (@ARGV == 3 && $ARGV[0] =~ /^(?:block|robot)all$/) { block(@ARGV) }
elsif (@ARGV == 3 && $ARGV[0] =~ /^(?:block|robot)dom$/) { blockdom(@ARGV) }
elsif (@ARGV == 3 && $ARGV[0] =~ /^(?:block|robot)ua$/) { blockua(@ARGV) }
elsif (@ARGV == 1 && $ARGV[0] eq 'list') { list() }
elsif (@ARGV == 2 && $ARGV[0] eq 'list') { show($ARGV[1]) }
elsif (@ARGV == 1 && $ARGV[0] eq 'stats') { stats() }
elsif (@ARGV == 1 && $ARGV[0] eq 'expire') { expire() }
elsif (@ARGV == 1 && $ARGV[0] eq 'report') { report(rules()) }
elsif (@ARGV == 3 && $ARGV[0] eq 'reset') { bump($ARGV[1], $ARGV[2]) }
elsif (@ARGV > 1 && $ARGV[0] =~ /^(777[0-9][0-9])$/) {
    annotate($1, join(' ', @ARGV[1..$#ARGV]));
} else { usage() }
exit;

sub bump {
    my ($duration, $id) = @_;
    my $future = duration_to_future($duration);
    my $rulefile = "$bldir/$id.conf";
    die "Rule id:$id not found at $rulefile\n" unless -e $rulefile;
    open my $fi, '<', $rulefile or die "Unable to open for reading : $rulefile : $!";
    my $header = <$fi>; # first line
    my @rule = <$fi>;   # other lines
    close $fi;
    my @header = parse_header($rulefile, $header);
    shift @header; # drop prior RUSER entry (gen_header will add current RUSER)
    $header[1] = $future; # replace expiry timestamp
    open my $fo, '>', $rulefile or die "Unable to open for writing : $rulefile : $!";
    print {$fo} gen_header(@header), "\n";
    print {$fo} @rule;
    close $fo;
    print "Expire time for rule $id updated to ".localtime($future)."\n";
    report(rules());
}

sub block {
    my ($blocktype, $duration, $request, $domain) = @_;
    check_blocktype($blocktype);
    check_domain($domain) if defined $domain;
    check_request($request);
    my $future = duration_to_future($duration);
    my $header = gen_header($blocktype, $future, $request, $domain);
    my $hostrule = domain_to_hostrule($domain);
    my $id = get_id();

    my $rule = $header . "\n";
    if ($blocktype eq "robot" || $blocktype eq "robotall") {
        $rule .= qq(SecRule REQUEST_URI "\@rx $request");
        $rule .= qq( "phase:1,deny,status:409,chain,log,id:$id,msg:'blmodsec blacklist by $who'"\n);
        $rule .= qq(SecRule &REQUEST_COOKIES:humans_21909 "!\@eq 1");
        if ($blocktype eq "robotall") {
            $rule .= "\n";
        } else {
            $rule .= qq( "chain"\n);
            $rule .= qq(SecRule REQUEST_HEADERS:Host "$hostrule"\n);
        }
    } else {
        if ($blocktype eq "blockall") {
            $rule .= qq(SecRule REQUEST_URI "\@rx $request" "phase:1,deny,status:406,log,id:$id,msg:'blmodsec blacklist by $who'"\n);
        } else { # blockall
            $rule .= qq(SecRule REQUEST_URI "\@rx $request" "phase:1,deny,status:406,chain,log,id:$id,msg:'blmodsec blacklist by $who'"\n);
            $rule .= qq(SecRule REQUEST_HEADERS:Host "$hostrule"\n);
        }
    }

    finish_rule($id, $rule);
}

sub finish_rule {
    my ($id, $rule) = @_;
    if (confirm_rule($rule)) {
        my $rulefile = "$bldir/$id.conf";
        open my $f, '>', $rulefile or die "Unable to open $rulefile for writing : $!";
        print {$f} $rule;
        close $f;
        print $rule;
        print "\nRule id:$id added to $rulefile\n";
        print "Apache has *NOT* been gracefulled. Configuration has *NOT* been checked.\n";

        report(rules());
    } else {
        die "Rule rejected! No configuration files were harmed.\n";
    }
}

sub blockua {
    my ($blocktype, $duration, $ua) = @_;
    check_blocktype($blocktype);
    check_request($ua);
    my $future = duration_to_future($duration);
    my $header = gen_header($blocktype, $future, $ua);
    my $id = get_id();

    my $rule = $header . "\n";
    if ($blocktype eq "robotua") {
        $rule .= qq(SecRule REQUEST_HEADERS:User-Agent "\@rx $ua");
        $rule .= qq( "phase:1,deny,status:409,chain,log,id:$id,msg:'blmodsec blacklist by $who'"\n);
        $rule .= qq(SecRule &REQUEST_COOKIES:humans_21909 "!\@eq 1\n");
    } else {
        $rule .= qq(SecRule REQUEST_HEADERS:User-Agent "\@rx $ua");
        $rule .= qq( "phase:1,deny,status:406,log,id:$id,msg:'blmodsec blacklist by $who'"\n);
    }

    finish_rule($id, $rule);
}

sub blockdom {
    my ($blocktype, $duration, $domain) = @_;
    check_blocktype($blocktype);
    check_domain($domain);
    my $hostrule = domain_to_hostrule($domain);
    my $future = duration_to_future($duration);
    my $header = gen_header($blocktype, $future, $domain);
    my $id = get_id();

    my $rule = $header . "\n";
    if ($blocktype eq "robotdom") {
        $rule .= qq(SecRule REQUEST_HEADERS:Host "$hostrule");
        $rule .= qq( "phase:1,deny,status:409,chain,log,id:$id,msg:'blmodsec blacklist by $who'"\n);
        $rule .= qq(SecRule &REQUEST_COOKIES:humans_21909 "!\@eq 1\n");
    } else {
        $rule .= qq(SecRule REQUEST_HEADERS:Host "$hostrule");
        $rule .= qq( "phase:1,deny,status:406,log,id:$id,msg:'blmodsec blacklist by $who'"\n);
    }

    finish_rule($id, $rule);
}

sub show {
    my ($id, $ids) = @_;
    $ids = rules() unless defined $ids;
    die "No such rule: $id\n" unless exists $ids->{$id};

    printf "%-8s %s\n", FILE => "$bldir/$id.conf";
    if ($ids->{$id}{blocktype} =~ /ua$/) {
        printf "%-8s %s\n", UA => $ids->{$id}{ua};
    } elsif ($ids->{$id}{blocktype} =~ /dom$/) {
        printf "%-8s %s\n", DOMAIN => $ids->{$id}{domain};
    } else {
        printf "%-8s %s\n", uc($ids->{$id}{blocktype}), $ids->{$id}{request};
        if ($ids->{$id}{blocktype} !~ /all$/) {
            printf "%-8s %s\n", DOMAIN => $ids->{$id}{domain};
        }
    }
    printf "%-8s %s\n", ID => $id;
    printf "%-8s %s\n", EXPIRES => scalar(localtime($ids->{$id}{expires}));
    printf "%-8s %s\n", BY => $ids->{$id}{author};
    printf "%-8s %s\n", REMARK => $_ for @{$ids->{$id}{annotations}};
    print "\n";
}

sub list {
    my %ids = %{rules()};
    my @ids = sort { $ids{$a}{expires} <=> $ids{$b}{expires} } keys %ids;
    for my $id (@ids) {
        show($id, \%ids);
    }
    if (!@ids && -t \*STDIN) {
        foundnone()
    }
}

sub stats {
    my %ids = %{rules()};
    foundnone() unless keys(%ids);
    my ($last, $count) = ('', 0);
    my @results;
    open my $f, '<', '/usr/local/apache/logs/error_log'
        or die "Unable to open /usr/local/apache/logs/error_log for reading : $!";
    while (defined(my $line = <$f>)) {
        next unless $line =~ /ModSecurity: Access denied/;
        next unless $line =~ /\[id "([0-9]+)"\]/;
        next unless exists $ids{$1};
        next unless $line =~ /^\[\S+ (\S+ \d+ \d+:)/;
        my $time = $1;
        if ($time eq $last) {
            $count++
        } else {
            push @results, [$count, $last] if $count;
            $last = $time;
            $count = 1;
        }
    }
    close $f;
    push @results, [$count, $last] if $count;
    my $width = 2;
    for (@results) {
        my $w = length($_->[0]);
        $width = $w if $w > $width;
    }
    $width++;
    for (@results) {
        printf "%${width}dx|%s00\n", @$_
    }
    unless (@results) {
        print "No modsec hits found for current modsec blacklists.\n";
    }
}

sub expire {
    my %ids = %{rules()};
    my $time = time();
    my $removed = 0;
    for my $id (keys %ids) {
        if ($ids{$id}{expires} < $time) {
            print "Expiring blmodsec blacklist id:$id\n" if -t \*STDIN;
            if (unlink "$bldir/$id.conf") {
                $removed++;
            } else {
                warn "Failed to remove $bldir/$id.conf : $!" if -t \*STDIN;
            }
        }
    }
    if (-t \*STDIN) {
        if ($removed) {
            print "Expired $removed blacklists.\n";
            print "Apache has *NOT* been gracefulled. Rules may remain in effect.\n";
        } else {
            if (keys(%ids)) {
                my $next = (sort { $ids{$a}{expires} <=> $ids{$b}{expires} } keys %ids)[0];
                printf "No blmodsec blacklists are ready to expire. id:$next expires in %.1fh hours.\n",
                    ($ids{$next}{expires} - $time)/3600;
            } else {
                foundnone()
            }
        }
    }
    report(\%ids) if $removed > 0 || rand(10) > 9; 
}

sub rules {
    my %ids;
    opendir my $dir, $bldir
        or die "Unable to open directory $bldir/ : $!";
    while (defined(my $conf = readdir($dir))) {
        next unless $conf =~ /^([0-9]+)\.conf$/;
        my $id = $1;
        my @annotations;
        open my $f, '<', "$bldir/$id.conf" or next;
        my $header = <$f>;
        while (defined(my $line = <$f>)) {
            chomp $line;
            next unless $line =~ /^# (.*)/;
            push @annotations, $1;
        }
        close $f;
        my ($author, $blocktype, $future, $request, $domain)
            = parse_header("$bldir/$id.conf", $header);
        if (defined $author) {
            if ($blocktype =~ /ua$/) {
                $ids{$id} = {
                    author => $author,
                    blocktype => $blocktype,
                    expires => $future,
                    ua => $request,
                    annotations => \@annotations,
                };
            } elsif ($blocktype =~ /dom$/) {
                $ids{$id} = {
                    author => $author,
                    blocktype => $blocktype,
                    expires => $future,
                    domain => $request,
                    annotations => \@annotations,
                };
            } else {
                $ids{$id} = {
                    author => $author,
                    blocktype => $blocktype,
                    expires => $future,
                    request => $request,
                    domain => $domain,
                    annotations => \@annotations,
                };
            }
        }
    }
    closedir $dir;
    return \%ids;
}

sub annotate {
    my ($id, $msg) = @_;
    die "Invalid annotation``$msg'': can't contain control characters\n"
        if $msg =~ /[\x00-\x1f]/;
    my $rulefile = "$bldir/$id.conf";
    die "Rule id:$id not found at $rulefile\n" unless -e $rulefile;
    open my $f, '>>', $rulefile or die "Unable to open $rulefile for appending : $!";
    print {$f} "# $msg\n";
    close $f;
    print "# $msg\n";
    report(rules());
}

sub duration_to_future {
    my ($duration) = @_;
    if ($duration =~ /^(\d+)h$/) {
        return time() + $1 * 3600 if $1 > 0 && $1 <= 7*24;
        die "Invalid duration ``$duration'' (must be more than 0 hours and no more than seven days)\n";
    }
    elsif ($duration =~ /^(\d+)d$/) {
        return time() + $1 * 24 * 3600 if $1 > 0 && $1 <= 7;
        die "Invalid duration ``$duration'' (must be more than 0 days and no more than seven days)\n";
    }
    else {
        die "Invalid duration ``$duration'' (must look like ``6h'', ``3h'', ``1d'', etc.)\n";
    }
}

# The linear search makes conflicts likely if two admins are running
# this command at the same time, but this would be a problem anyway.
sub get_id {
    my $id = 77700;
    $id++ while -e "$bldir/$id.conf";
    return $id
}

# HTTP requests, especially malicious requests, can have complete
# nonsense in the Host header, so this function only cares that the
# provided domain won't blatantly break modsec syntax.
sub check_domain {
    my ($domain) = @_;
    die "Invalid domain name ``$domain'': can't contain double quotes\n"
        if $domain =~ /"/;
    die "Invalid domain name ``$domain'': can't contain regex metachars\n"
        if $domain =~ /[\\*+(){}\[\]^\$]/;
    die "Invalid domain name ``$domain'': can't contain control characters\n"
        if $domain =~ /[\x00-\x1f]/;
}

sub check_blocktype {
    my ($blocktype) = @_;
    return if $blocktype =~ /^(?: block | blockall | robot | robotall | blockua | robotua | blockdom | robotdom )$/x;
    die "Invalid block type ``$blocktype'' (must be one of: block blockall robot robotall)\n";
}

sub check_request {
    my ($request) = @_;
    eval { "" =~ /$request/ };
    die "Invalid request regex ``$request'': $@\n" if $@;
    if ("" =~ /$request/) { die "Invalid request ``$request'' (shouldn't match the empty string)" }
    if ("x" =~ /$request/) { die "Invalid request ``$request'' (shouldn't match a single letter string)" }
}

sub confirm_rule {
    my ($rule) = @_;
    while (1) {
        print "Proposed rule: \e[1m\n$rule\e[0m\nIs this what you want to add? (y/n) ";
        my $answer = <STDIN>;
        die "Nevermind then.\n" unless defined $answer;
        chomp $answer;
        return 1 if $answer =~ /^y(?:es)?$/i;
        return 0 if $answer =~ /^n(?:o)?$/i;
        print "Sorry, I didn't understand ``$answer'' as either a 'y' or 'n'.\n\n";
        sleep 1;
    }
}

sub domain_to_hostrule {
    my ($domain) = @_;
    return unless defined $domain;
    if ($domain=~ /^\./) {
        $domain =~ s/^\.//;
        $domain =~ s/\./\\./g;
        return '@rx (^|\\.)'.$domain.'$'
    } else {
        return '@streq '.$domain
    }
}

sub gen_header {
    my ($blocktype, $future, $request, $domain) = @_;
    if ($blocktype =~ /(?:dom|ua|all)$/) {
        die "Error: domain/user provided but block type is $blocktype\n" if defined $domain;
        return "# $who $blocktype $future $request";
    } else {
        return "# $who $blocktype $future $domain $request";
    }
}

sub parse_header {
    my ($file, $header) = @_;
    if ($header =~ /^# (\w+) ((?:block|robot)(?:all|ua|dom)) ([0-9]+) (.*)$/) {
        return $1, $2, $3, $4
    } elsif ($header =~ /^# (\w+) (block|robot) ([0-9]+) (\S+) (.*)$/) {
        return $1, $2, $3, $5, $4
    }
    return
}

# caller already has %ids and has already confirmed that keys(%ids) > 0
#
# undocumented 'blmodsec report' can call this even with no rules, which
# will still be reported. this helps with clearing up stale reports if a rule
# was manually removed.
sub report {
    my ($idref) = @_;
    my %ids = %$idref;
    fork && return;
    # now in child:
    close STDOUT;
    close STDERR;
    close STDIN;

    my $key = md5_hex("eN5vK4N8e3zMhlUq3AQwaN4oaU4u:$hostname");
    my $ua = LWP::UserAgent->new;
    $ua->agent('blmodsec-reporter');
    $ua->ssl_opts(verify_hostname => 0);
    $ua->timeout(3);
    my $req = HTTP::Request->new(POST => "https://reports.soc.newfold.com/report/blmodsec?host=$hostname&key=$key");
    $req->content_type('application/json');
    $req->content(encode_json(\%ids));
    $ua->request($req);
}
