new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / debian / ossec-hids / usr / share / doc / ossec-hids / 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 use Regexp::IPv6 qw($IPv6_re);
18
19 # -----------------------
20 # DB access configuration
21 # -----------------------
22 my $db_name  = 'ossec_active_lists';
23 my $db_user  = 'suspicious';
24 my $db_pass  = 'xxxxxxxxxx';
25
26 my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
27 my $theTime  = sprintf("%d-%02d-%02d %02d:%02d:%02d", 
28         $yearOffset+1900, $month+1, $dayOfMonth, $hour, $minute, $second);
29
30 my $nArgs = $#ARGV + 1;
31 if ($nArgs != 5) {
32         print STDERR "Usage: active-list.pl <action> <username> <ip>\n";
33         exit 1;
34 }
35
36 my $action      = $ARGV[0];
37 my $ipAddr      = $ARGV[2];
38 my $alertId     = $ARGV[3];
39 my $ruleId      = $ARGV[4];
40
41 if ($action ne "add" && $action ne "delete") {
42         WriteLog("Invalid action: $action\n");
43         exit 1;
44 }
45
46 if ($ipAddr =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)/) {
47         if ($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255) {
48                 WriteLog("Invalid IP address: $ipAddr\n");
49                 exit 1;
50         }
51 }
52 else if ($ipAddr =~ m/^$IPv6_re/) {
53 }
54 else {
55         WriteLog("Invalid IP address: $ipAddr\n");
56 }
57
58 WriteLog("active-list.pl $action $ipAddr $alertId $ruleId\n");
59
60 my $dbh = DBI->connect('DBI:mysql:' .  $db_name, $db_user, $db_pass) || \
61         die "Could not connect to database: $DBI::errstr";
62
63 if ( $action eq "add" ) {
64         my $sth = $dbh->prepare('SELECT ip FROM ip_addresses WHERE ip = "' . $ipAddr . '"');
65         $sth->execute();
66         my $result = $sth->fetchrow_hashref();
67         if (!$result->{ip}) {
68                 $sth = $dbh->prepare('INSERT INTO ip_addresses VALUES ("' . $ipAddr . '","'. $theTime . '",' . $alertId . ',' . $ruleId . ',"Added by suspicious-ip Perl Script")');
69                 if (!$sth->execute) {
70                         WriteLog("Cannot insert new IP address: $DBI::errstr\n");
71                 }
72         }
73         else {
74                 $sth = $dbh->prepare('UPDATE ip_addresses SET timestamp = "' . $theTime . '", alertid = ' . $alertId . ', ruleid = ' . $ruleId . ' WHERE ip = "' . $ipAddr . '"');
75                 if (!$sth->execute) {
76                         WriteLog("Cannot update IP address: $DBI::errstr\n");
77                 }
78         }
79
80 else {
81         my $sth = $dbh->prepare('DELETE FROM ip_addresses WHERE ip = "' . $ipAddr . '"');
82         if (!$sth->execute) {
83                 WriteLog("Cannot remove IP address: $DBI::errstr\n");
84         }
85 }
86
87 $dbh->disconnect;
88 exit 0;
89
90 sub WriteLog
91 {
92         if ( $_[0] eq "" ) { return; }
93
94         my $pwd  = `pwd`;
95         chomp($pwd);
96         my $date = `date`;
97         chomp($date);
98
99         open(LOGH, ">>" . $pwd . "/../active-responses.log") || die "Cannot open log file.";
100         print LOGH $date . " " . $_[0];
101         close(LOGH);
102         return;
103 }