| 1 | package ossec::log::parse; | ||
| 2 | # ABSTRACT: Perl interface for parsing Ossec alert files | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use autodie; | ||
| 7 | use Carp; | ||
| 8 | use Scalar::Util qw/openhandle/; | ||
| 9 | |||
| 10 | our $VERSION = '0.1.0'; | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | my @accessors = qw/fh file/; | ||
| 14 | for my $accessor ( @accessors ) { | ||
| 15 | no strict 'refs'; | ||
| 16 | *$accessor = sub { | ||
| 17 | my $self = shift; | ||
| 18 | return $self->{$accessor}; | ||
| 19 | } | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my $class = shift; | ||
| 25 | my $arg = shift; | ||
| 26 | |||
| 27 | my $self = {}; | ||
| 28 | |||
| 29 | if ( !defined($arg) ) { | ||
| 30 | $self->{diamond} = 1; | ||
| 31 | } elsif ( ref($arg) eq 'HASH' ) { | ||
| 32 | $self = $arg; | ||
| 33 | } elsif ( defined(openhandle($arg)) ) { | ||
| 34 | $self->{fh} = $arg; | ||
| 35 | } else { | ||
| 36 | $self->{file} = $arg; | ||
| 37 | } | ||
| 38 | |||
| 39 | bless $self, $class; | ||
| 40 | |||
| 41 | if ( defined($self->{file}) && !(defined($self->{fh})) ) { | ||
| 42 | unless ( -f $self->{file} ) { | ||
| 43 | carp("Could not open ".$self->{file}); | ||
| 44 | return 0; | ||
| 45 | } | ||
| 46 | open( my $fh, "<", $self->{file} ) or croak("Cannot open ".$self->{file}); | ||
| 47 | $self->{fh} = $fh; | ||
| 48 | } | ||
| 49 | |||
| 50 | if ( !defined($self->{fh}) && ( !defined($self->{diamond}) || !$self->{diamond} ) ) { | ||
| 51 | carp("No filename given in constructor. Aborting"); | ||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | return $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub getAlert { | ||
| 59 | |||
| 60 | my $self = shift; | ||
| 61 | my $fh = $self->{fh}; | ||
| 62 | |||
| 63 | my %alert; | ||
| 64 | my $position = 0; | ||
| 65 | |||
| 66 | while ( my $line = defined($fh) ? <$fh> : <> ) { | ||
| 67 | |||
| 68 | chomp($line); | ||
| 69 | |||
| 70 | if ($line =~ m/^\*\* Alert (\d+\.\d+):(.*)-(.*)/) { | ||
| 71 | $alert{'ts'} = $1; | ||
| 72 | $alert{'type'} = $2; | ||
| 73 | $alert{'group'} = $3; | ||
| 74 | # clean up variables | ||
| 75 | $alert{'type'} =~ s/^\s+|\s+$//g; # strip leading/trailing whitespace | ||
| 76 | $alert{'group'} =~ s/^\s+|\s+$//g; | ||
| 77 | $alert{'group'} =~ s/,$//g; # strip trailing comma | ||
| 78 | $position = 1; | ||
| 79 | next; | ||
| 80 | } | ||
| 81 | if ($position > 0) { | ||
| 82 | $position++; | ||
| 83 | if ($position == 2) { | ||
| 84 | if ($line =~ m!(\d+ \w+ \d+ \d+:\d+:\d+) \((.*)\) (\S+)->(.*)!) { # alert from remote agent | ||
| 85 | $alert{'ts.human'} = $1; | ||
| 86 | $alert{'agent.name'} = $2; | ||
| 87 | $alert{'agent.ip'} = $3; | ||
| 88 | $alert{'location'} = $4; | ||
| 89 | } | ||
| 90 | elsif ($line =~ m!(\d+ \w+ \d+ \d+:\d+:\d+) (\S+)->(.*)!) { # alert from local agent | ||
| 91 | $alert{'ts.human'} = $1; | ||
| 92 | $alert{'agent.name'} = $2; | ||
| 93 | $alert{'agent.ip'} = '-'; | ||
| 94 | $alert{'location'} = $3; | ||
| 95 | } | ||
| 96 | $alert{'2'} = $line; | ||
| 97 | next; | ||
| 98 | } | ||
| 99 | elsif ($position == 3) { | ||
| 100 | if ($line =~ /^Rule: (\d+) \(level (\d+)\) -> '(.*)'$/) { | ||
| 101 | $alert{'rule.id'} = $1; | ||
| 102 | $alert{'rule.level'} = $2; | ||
| 103 | $alert{'rule.comment'} = $3; | ||
| 104 | # clean up variable | ||
| 105 | $alert{'rule.comment'} =~ s/\.$//; # remove any trailing period | ||
| 106 | } | ||
| 107 | $alert{'3'} = $line; | ||
| 108 | next; | ||
| 109 | } | ||
| 110 | elsif ($line !~ m/^$/ ) { | ||
| 111 | if ($alert{'full_log'} ) { | ||
| 112 | $alert{'full_log'} = "$alert{'full_log'}\n$line"; | ||
| 113 | } | ||
| 114 | else { | ||
| 115 | $alert{'full_log'} = $line; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | $position = 0; | ||
| 120 | return \%alert; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||
| 127 |