#!/usr/bin/perl

=head1 NAME

pfdetect - listen to snort alerts and create PF violations

=head1 SYNOPSIS

pfdetect -p <snortpipe> [options]

 Options:
   -d     Daemonize
   -h     Help

=cut

use strict;
use warnings;
use File::Basename qw(basename);
use Getopt::Std;
use Pod::Usage;
use POSIX qw(:signal_h);

BEGIN {
    # log4perl init
    use constant INSTALL_DIR => '/usr/local/pf';
    use lib INSTALL_DIR . "/lib";
    use pf::log(service => 'pfdetect');
}

use pf::action;
use pf::class;
use pf::config;
use pf::config::cached;
use pf::db;
use pf::iplog;
use pf::node;
use pf::person;
use pf::trigger;
use pf::util;
use pf::services::util;
use pf::violation;

# initialization
# --------------
# assign process name (see #1464)
our $PROGRAM_NAME = $0 = "pfdetect";

my $logger = Log::Log4perl->get_logger( $PROGRAM_NAME );

# init signal handlers
POSIX::sigaction(
    &POSIX::SIGHUP,
    POSIX::SigAction->new(
        'normal_sighandler', POSIX::SigSet->new(), &POSIX::SA_NODEFER
    )
) or $logger->logdie("pfdetect: could not set SIGHUP handler: $!");

POSIX::sigaction(
    &POSIX::SIGTERM,
    POSIX::SigAction->new(
        'normal_sighandler', POSIX::SigSet->new(), &POSIX::SA_NODEFER
    )
) or $logger->logdie("pfdetect: could not set SIGTERM handler: $!");

POSIX::sigaction(
    &POSIX::SIGINT,
    POSIX::SigAction->new(
        'normal_sighandler', POSIX::SigSet->new(), &POSIX::SA_NODEFER
    )
) or $logger->logdie("pfdetect: could not set SIGINT handler: $!");


my @ORIG_ARGV = @ARGV;
my %args;
getopts( 'dhp:', \%args );

my $daemonize = $args{d};
my $snortpipe = $args{p};
my $snortpipe_fh;
my $portscan_sid = 1200003;

pod2usage( -verbose => 1 ) if ( $args{h} || !$args{p} );

my ($line, $sid, $descr, $priority, $date, $srcmac, $srcip, $dstip);

daemonize($PROGRAM_NAME) if ($daemonize);

$logger->info("initialized");

if ( !open( $snortpipe_fh, '<', "$snortpipe" ) ) {
    $logger->logdie("unable to open snort pipe ($snortpipe): $!");
} else {
    $logger->info("listening on $snortpipe");
}

while (<$snortpipe_fh>) {

    $logger->info("alert received: $_");
    if ( $_
        =~ /^(.+?)\s+\[\*\*\]\s+\[\d+:(\d+):\d+\]\s+(.+?)\s+.+?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(:\d+){0,1}\s+\-\>\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(:\d+){0,1}/
        )
    {
        $date  = $1;
        $sid   = $2;
        $descr = $3;
        $srcip = $4;
        $dstip = $6;
    } elsif ( $_
        =~ /^(.+?)\s+\[\*\*\]\s+\[\d+:(\d+):\d+\]\s+Portscan\s+detected\s+from\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
        )
    {
        $date  = $1;
        $sid   = $portscan_sid;
        $srcip = $3;
        $descr = "PORTSCAN";
    } elsif ( $_
        =~ /^(.+?)\[\*\*\] \[\d+:(\d+):\d+\]\s+\(spp_portscan2\) Portscan detected from (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
        )
    {
        $date  = $1;
        $sid   = $portscan_sid;
        $srcip = $3;
        $descr = "PORTSCAN";
    } else {
        $logger->warn("unknown input: $_ ");
        next;
    }

    $srcmac = pf::iplog::ip2mac($srcip);

    if ($srcmac) {

        $logger->info("pfdetect: violation $sid [$descr]: $srcmac");
        violation_trigger($srcmac, $sid, "detect", ( ip => $srcip ));
    } else {
        $logger->warn(
            "pfdetect: $srcip MAC NOT FOUND for violation $sid [$descr]");
    }
    #reload all cached configs after each iteration
    pf::config::cached::ReloadConfigs();
}

END {
    deletepid();
    if (defined($snortpipe_fh)) {
        $logger->info("stopping pfdetect");
        close($snortpipe_fh);
    }
}

exit(0);

sub normal_sighandler {
    deletepid();
    $logger->logdie( "caught SIG" . $_[0] . " - terminating" );
}


=head1 AUTHOR

Inverse inc. <info@inverse.ca>

Minor parts of this file may have been contributed. See CREDITS.

=head1 COPYRIGHT

Copyright (C) 2005-2015 Inverse inc.

Copyright (C) 2005 Kevin Amorin

Copyright (C) 2005 David LaPorte

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.

=cut

