Imported Upstream version 2.7
[ossec-hids.git] / contrib / ossec_report_contrib.pl
1 #!/usr/bin/perl -w
2 # by Meir Michanie
3 # GPL licensed
4 # meirm@riunx.com
5 my $VERSION="0.1";
6 use strict;
7 &help() unless @ARGV;
8 if ($ARGV[0]=~ m/^-h$|^--help$/i){
9         &help();
10 }
11 &help unless $ARGV[0]=~ m/^-r$|^--report$|^-s$|^--summary$|^-t$|^--top$/;
12 my (@argv) = ();
13 while (@ARGV){
14         push @argv, shift @ARGV;
15 }
16
17 my $newrecord=0;
18 my %stats;
19 my ($timestamp,$sec,$mail,$date,$alerthost,$datasource,$rule,$level,$description,
20 $srcip,$user,$text);
21 while(<>){
22         if (m/^$/){
23                 $newrecord=1;
24                 $stats{$alerthost}{mail}{$mail}++;
25                 $stats{$alerthost}{alerthost}{$alerthost}++;
26                 $stats{$alerthost}{datasource}{$datasource}++;
27                 $stats{$alerthost}{rule}{$rule}++;
28                 $stats{$alerthost}{level}{$level}++;
29                 $stats{$alerthost}{description}{$description}++;
30                 if (defined $srcip) { $stats{$alerthost}{srcip}{$srcip}++; }
31                 if (defined $user)  { $stats{$alerthost}{user}{$user}++; }
32                 next ;
33         }
34         if (m/^\*\* Alert ([0-9]+).([0-9]+):(.*)$/){
35                 $timestamp=$1;
36                 $sec=$2;
37                 $mail=$3;
38         }elsif ( m/^([0-9]+\s\w+\s[0-9]+\s[0-9]+:[0-9]+:[0-9]+)\s(.*?)->(.*)$/){
39                 $date=$1;
40                 $alerthost=$2;
41                 $datasource=$3;
42         }elsif ( m/^([0-9]+\s\w+\s[0-9]+\s[0-9]+:[0-9]+:[0-9]+)\s(.*?)$/){
43                 $date=$1;
44                 $alerthost='none';
45                 $datasource=$2;
46         }elsif ( m/Rule: ([0-9]+) \(level ([0-9]+)\) -> (.*)$/ ){
47                 $rule=$1;
48                 $level=$2;
49                 $description= $3;
50         }elsif ( m/Src IP: (.*)$/){
51                 $srcip=$1;
52         }elsif ( m/User: (.*)$/){
53                 $user=$1;
54         }elsif( m/(.*)$/){
55                 $text=$1;
56         }
57                 
58
59 }
60 if ($argv[0]=~ m/^-r$|^--report$/i){
61         &report(\%stats);
62 }elsif ($argv[0]=~ m/^-s$|^--summary$/){
63         &summary(\%stats);
64 }elsif ($argv[0]=~ m/^-t$|^--top$/){
65         $argv[1]= $argv[1] ? $argv[1] : 'srcip' ;
66         &top(\%stats,$argv[1]);
67 }else{
68         &help();
69 }
70
71 sub printversion(){
72         print "OSSEC report tool $VERSION\n";
73         print "Licensed under GPL\n";
74         print "Contributor Meir Michanie\n";
75 }
76
77 sub help(){
78         &printversion();
79         print "$0 [-h|--help] # This text you read now\n";
80         print "$0 [-r|--report] # prints a report for each element\n";
81         print "$0 [-s|--summary] # prints a summary report\n";
82         print "$0 [-t|--top] <field> #prints the top list\n";
83         print "\nHow To:\n";
84         print   "=======\n";
85         print "$0\tOSSEC report tool $VERSION\n";
86         print "  $0 is a GNU style program.\nIt reads from STDIN and write to stdout. ";
87         print "This gives you the advantage to use it in pipes.\n";
88         print "i.e.\n";
89         print " cat ossec-alerts-05.log | $0 -r | mail root -s 'OSSEC detailed report'\n";
90         print " cat ossec-alerts-05.log | $0 -s | mail root -s 'OSSEC summary report'\n";
91         print " cat ossec-alerts-05.log | $0 -t srcip |  head -n 15 | mail root -s 'OSSEC top 15 offenders report'\n";
92         print " Crontab entry:\n";
93         print "58 23 * * * (cat ossec-alerts-05.log | $0 -s)\n";
94         exit 0;
95 }
96
97 sub report(){
98         my ($statref)=@_;
99 my ($stat,$key,$value);
100 format TOPREPORT =
101 =============================================================================
102 |               Summary report                                              |
103 =============================================================================
104 |Alerthost | Stat       | Key                                       | Count |
105 =============================================================================
106 .
107 $~='TOPREPORT';
108 write; 
109 format REPORT =
110 |@<<<<<<<<<<|@<<<<<<<<<<<|@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|@######|
111 $alerthost,$stat,$key,$value
112 .
113 $~='REPORT';
114 foreach(sort keys %{$statref}){
115         $alerthost=$_;
116         foreach(sort keys %{${$statref}{$alerthost}}){
117                 $stat=$_;
118                 foreach(sort keys %{${$statref}{$alerthost}{$stat}}){
119                         $key=$_;
120                         $value=${$statref}{$alerthost}{$stat}{$key};
121                         write;
122                 }
123         }
124 }
125 }
126
127 sub summary(){  
128 my ($statref)=@_;
129 my (%totals);
130 my ($stat,$key,$value);
131 foreach(sort keys %{$statref}){
132         $alerthost=$_;
133         foreach(sort keys %{${$statref}{$alerthost}}){
134                 $stat=$_;
135                 foreach(sort keys %{${$statref}{$alerthost}{$stat}}){
136                         $key=$_;
137                         $value=${$statref}{$alerthost}{$stat}{$key};
138                         $totals{$stat}{$key}+=$value;
139                 }
140         }
141 }
142 format TOPSUMREPORT =
143 =================================================================
144 |               Statistic report                                |
145 =================================================================
146 |Stat        | Key                                      | Count |
147 =================================================================
148 .
149 $~='TOPSUMREPORT';
150 write; 
151 format SUMREPORT =
152 |@<<<<<<<<<<<|@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|@######|
153 $stat,$key,$value
154 .
155 $~='SUMREPORT';
156 foreach(sort keys %totals){
157                         $stat=$_;
158                         foreach(sort keys %{$totals{$stat}}){
159                                 $key=$_;
160                                 $value=$totals{$stat}{$key};
161                         write;
162         }
163 }
164 }
165
166 sub top(){
167 my ($statref,$stat)=@_;
168 my (%totals,%mytest);
169 my ($key,$value);
170 foreach(keys %{$statref}){
171         $alerthost=$_;
172                 foreach( keys %{${$statref}{$alerthost}{$stat}}){
173                         $key=$_;
174                         $value=${$statref}{$alerthost}{$stat}{$key};
175                         $totals{$stat}{$key}+=$value;
176                 }
177 }
178 foreach (keys %{$totals{$stat}}){
179         $mytest{$totals{$stat}{$_}}=$_;
180 }; 
181 foreach (sort {$b <=> $a}  keys %mytest){
182         print "$mytest{$_} => $_\n";
183 }
184 }