#! /usr/bin/env perl
use strict;
use warnings;
our $version = 'v0.1.0';
my $who = (defined($ENV{RUSER}) && $ENV{RUSER} =~ /^\w+$/) ? $ENV{RUSER} : 'support';
my $bldir = '/opt/mod_security/blacklists';

sub foundnone { die "No blmodsec blacklists were found on this server.\n" }

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

  --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.

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 <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 == 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] =~ /^(777[0-9][0-9])$/) {
    annotate($1, join(' ', @ARGV[1..$#ARGV]));
} else { usage() }
exit;

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);
        }
    }

    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";
    } else {
        die "Rule rejected! No configuration files were harmed.\n";
    }
}

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";
    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()
            }
        }
    }
}

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);
        $ids{$id} = {
            author => $author,
            blocktype => $blocktype,
            expires => $future,
            request => $request,
            domain => $domain,
            annotations => \@annotations,
        } if defined $author;
    }
    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";
}

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 )$/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 =~ /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+) (blockall|robotall) ([0-9]+) (.*)$/) {
        return $1, $2, $3, $4
    } elsif ($header =~ /^# (\w+) (block|robot) ([0-9]+) (\S+) (.*)$/) {
        return $1, $2, $3, $5, $4
    }
    return
}
