#!/usr/bin/perl

# Portsentry Abuse Mailer v 0.3
# queries a set of whois servers and gleans email
# address(es) to contact for each IP
# E-mail is then sent to a moderators address
#
# usage: whois.pl history_file [history_file ..]
# This script is in testing phase...Greg Wright greg@ausit.com
# Idea was mine but coding is care of 
# Charles Galpin cgalpin@lighthouse-software.com
# The script is free for anyone to use, but it is asked
# that a copy of modifications be emailed to me & Charles, if any.

# Requirements: Currently Portsentry any version must be installed
# Requirements: Feedback welcomed on other mail systems
# Requirements: Tested on Sendmail, repoted ok on Postfix

# the whois servers to query
# you can change to suit your prefered whois server
my @servers = ( 'arin.net', 'whois.apnic.net', 'whois.ripe.net');

# info for mailing
# edit email addresses to suit
my $to = "Abuse Watch";
my $toEmail = "abuse\@domain.com";
my $from = "Abuse Watcher";
my $fromEmail = "abuse\@domain.com";
my $reviewSubject = "Daily whois lookup review";
my $abuseSubject = "ATTACK: System scan and abuse report";
my $logFile = "/var/log/portsentry/portsentry.history";
my $logFileTotal = "/var/log/portsentry/portsentry.history.total";
my $abuseTemplate = "abuse.template";

########################
# Do not edit below here
########################

my $message  = "$subject\n\n";
my %matches;
if ( open ( HISTORY, "$logFile" ) ) {
    if ( open ( TOTAL, ">>$logFileTotal" ) ) {
	while (<HISTORY>) {
	    print TOTAL;
	    if ( /Host:\s+.*?\/(.*?)\s+Port:.*?Blocked$/ ) {
		$ip = $1;
		print "\n$ip\n";
		processIp($ip );
	    } # if history match
	} # while (<HISTORY>)
	close (TOTAL);
	close (HISTORY);
    } else {
	print "Failed to open file $logFileTotal $!\n";
	$message .= "\n\nFailed to open file $logFileTotal $!\n\n";
    } # open TOTAL
} else {
    print "Failed to open file $logFile $!\n";
    $message .= "\n\nFailed to open file $logFile $!\n\n";
} # open HISTORY

if ( sendMail( $to, $toEmail, $from, $fromEmail, $reviewSubject, $message)
) {
    print "review emailed successfuly to $to <$toEmail>\n";
} else {
    print "review email failed\n";
}

# reset without clobbering inode
# this may still not work properly (may need to start/stop
# portsentry. untested
`cat /dev/null > $logFile`;

# end main


sub processIp() {
    my $ip = shift;

    %matches = ();
    foreach $server ( @servers ) {
        print "  [$server]\n";
        if ( open ( SERVER, "whois $ip\@$server|" ) ) {
            while (<SERVER>) {
                #print;
                # arin.net format
                if ( /\s*Coordinator:/ ) {
                    $_ = <SERVER>;
                    #print;
                    if ( /\s+(.+?)\s+?\(.*?\)\s+(.*?\@.*?\..*)/ ) {
                        print "\tFound coordinator $1 - $2\n";
                        $matches{$2} = $1;                      
                    }
                    last;

                    
                    # whois.apnic.net and whois.ripe.net format
                } elsif ( /^notify:\s*(.*)/ ) { 
                    print "\tFound notifier - $1\n";
                    $matches{$1} = "Abuse Contact";
                }elsif ( /^(?:person|role):\s*(.*)/ ) { 
                    my $person = $1;
                    while (<SERVER>) {
                        #print;
                        if ( /^e-mail:\s*(.*)/ ) { 
                            print "\tFound person/role $person - $1\n";
                            $matches{$1} = $person;
                            last;
                        }
                    }
                }
            } # while (<SERVER>) outer loop
        } else {
            print "whois failed to access $server\n";
        }
    } # foreach $server
    
    # map thse matches to this IP.
    $message .= "$ip\n";
    foreach $email ( sort keys %matches ) {
        $message .= "  " . $matches{$email} . "\t<$email>\n";
        &submitAbuseReport( $ip, $matches{$email}, $email );
    }
    $message .= "\n\n";
}

sub submitAbuseReport() {
    my ($ip, $name, $email) = @_;
    my $message = "";
    my $log = "";

    open (TEMPL, "$abuseTemplate") or die "cannot open
$abuseTemplate: $!\n";
    while (<TEMPL>) {
        next if /^\s*\#/; # skip comment lines
        s/__IP__/$ip/g;
        s/__NAME__/$name/g;
        s/__EMAIL__/$email/g;
        if ( /__LOG__/ ) {
            $line = $_;
            # global var $logFile
            open ( LOG, "$logFile") or die "cannot open $logFile$!\n";
            while (<LOG>) {
                #print "log: $_\n";
                $log .= $_;
            }
            close (LOG);
            $_ = $line;
            s/__LOG__/$log/g;
        }
        $message .= $_;
    }
    close( TEMPL );

    if ( sendMail( $to, $toEmail, $from, $fromEmail, $abuseSubject,
$message) ) {
        print "abuse emailed successfuly to $to <$toEmail>\n";
    } else {
        print "abuse email failed\n";
    }   
}


# to, toEmail, from, fromEmail, subject, message
sub sendMail {
    my ($to, $toEmail, $from, $fromEmail, $subject, $message) = @_;

    my $sendmail = "/usr/sbin/sendmail -i ";

    if ( open( MAIL,"|$sendmail -t -f $fromEmail" ) )
    {
        print MAIL "Reply-to: $fromEmail\n";
        print MAIL "Subject: $subject\n";
        print MAIL "From: $from <$fromEmail>\n";
        print MAIL "To: $to <$toEmail>\n";
        print MAIL "\n";
        print MAIL "$message\n";
        close (MAIL);

        return 1;
    }
    else
    {
        return 0;
    }

}