Imported Upstream version 2.7
[ossec-hids.git] / contrib / active-list.pl
1 #!/usr/bin/perl
2 #
3 # OSSEC active-response script to store a suspicious IP address in a MySQL table.
4 #
5 # Available actions are:
6 #       'add'    - Create a new record in the MySQL DB
7 #       'delete' - Remove a existing record 
8 #
9 # History
10 # -------
11 # 2010/10/24    xavir@rootshell.be      Created
12 #
13
14 use strict;
15 use warnings;
16 use DBI;
17
18 # -----------------------
19 # DB access configuration
20 # -----------------------
21 my $db_name  = 'ossec_active_lists';
22 my $db_user  = 'suspicious';
23 my $db_pass  = 'xxxxxxxxxx';
24
25 my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
26 my $theTime  = sprintf("%d-%02d-%02d %02d:%02d:%02d", 
27         $yearOffset+1900, $month+1, $dayOfMonth, $hour, $minute, $second);
28
29 my $nArgs = $#ARGV + 1;
30 if ($nArgs != 5) {
31         print STDERR "Usage: active-list.pl <action> <username> <ip>\n";
32         exit 1;
33 }
34
35 my $action      = $ARGV[0];
36 my $ipAddr      = $ARGV[2];
37 my $alertId     = $ARGV[3];
38 my $ruleId      = $ARGV[4];
39
40 if ($action ne "add" && $action ne "delete") {
41         WriteLog("Invalid action: $action\n");
42         exit 1;
43 }
44
45 if ($ipAddr =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)/) {
46         if ($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255) {
47                 WriteLog("Invalid IP address: $ipAddr\n");
48                 exit 1;
49         }
50 }
51 else {
52         WriteLog("Invalid IP address: $ipAddr\n");
53 }
54
55 WriteLog("active-list.pl $action $ipAddr $alertId $ruleId\n");
56
57 my $dbh = DBI->connect('DBI:mysql:' .  $db_name, $db_user, $db_pass) || \
58         die "Could not connect to database: $DBI::errstr";
59
60 if ( $action eq "add" ) {
61         my $sth = $dbh->prepare('SELECT ip FROM ip_addresses WHERE ip = "' . $ipAddr . '"');
62         $sth->execute();
63         my $result = $sth->fetchrow_hashref();
64         if (!$result->{ip}) {
65                 $sth = $dbh->prepare('INSERT INTO ip_addresses VALUES ("' . $ipAddr . '","'. $theTime . '",' . $alertId . ',' . $ruleId . ',"Added by suspicious-ip Perl Script")');
66                 if (!$sth->execute) {
67                         WriteLog("Cannot insert new IP address: $DBI::errstr\n");
68                 }
69         }
70         else {
71                 $sth = $dbh->prepare('UPDATE ip_addresses SET timestamp = "' . $theTime . '", alertid = ' . $alertId . ', ruleid = ' . $ruleId . ' WHERE ip = "' . $ipAddr . '"');
72                 if (!$sth->execute) {
73                         WriteLog("Cannot update IP address: $DBI::errstr\n");
74                 }
75         }
76
77 else {
78         my $sth = $dbh->prepare('DELETE FROM ip_addresses WHERE ip = "' . $ipAddr . '"');
79         if (!$sth->execute) {
80                 WriteLog("Cannot remove IP address: $DBI::errstr\n");
81         }
82 }
83
84 $dbh->disconnect;
85 exit 0;
86
87 sub WriteLog
88 {
89         if ( $_[0] eq "" ) { return; }
90
91         my $pwd  = `pwd`;
92         chomp($pwd);
93         my $date = `date`;
94         chomp($date);
95
96         open(LOGH, ">>" . $pwd . "/../active-responses.log") || die "Cannot open log file.";
97         print LOGH $date . " " . $_[0];
98         close(LOGH);
99         return;
100 }