]>
git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Parser.pm
1 package Guardian
::Parser
;
5 use Exporter
qw(import);
7 our @EXPORT_OK = qw(IsSupportedParser Parser);
9 # This hash contains all supported parsers and which function
10 # has to be called to parse messages in the right way.
11 my %logfile_parsers = (
12 "snort" => \
&message_parser_snort
,
16 ## The main parsing function.
18 ## It is used to determine which sub-parser has to be used to
19 ## parse the given message in the right way and to return if
20 ## any action should be performed.
23 my ($parser, @message) = @_;
25 # If no responsible message parser could be found, just return nothing.
26 unless (exists($logfile_parsers{$parser})) {
30 # Call responsible message parser.
31 my $action = $logfile_parsers{$parser}->(@message);
33 # If the parser successfully parsed the message, an action
36 # Return which action should be performed.
37 return "count $action";
40 # If the parser was not able to parse the the given message
41 # in the right way, return Nothing.
46 ## IsSupportedParser function.
48 ## This very tiny function checks if a given parser name is available and
49 ## therefore a supported parser.
51 ## To perform these check, the function is going to lookup if a key in the
52 ## hash of supported parsers is available
54 sub IsSupportedParser
($) {
57 # Check if a key for the given parser exists in the hash of logfile_parsers.
58 if(exists($logfile_parsers{$parser})) {
59 # Found a valid parser, so return nothing.
63 # Return "False" if we got here, and therefore no parser
69 ## The Snort message parser.
71 ## This subfunction is responsible for parsing sort alerts and determine if
72 ## an action should be performed.
74 ## XXX Currently the parser only supports IPv4. Add support for IPv6 at a
77 sub message_parser_snort
(@
) {
80 # The name of the parser module.
83 # Variable to store the grabbed IP-address.
86 # Default returned message in case no one could be grabbed
87 # from the snort alert.
88 my $message = "An active snort rule has matched and gained an alert.";
90 # A snort alert contains multiple lines, loop through all lines
91 # to parse the complete alert.
92 foreach my $line (@message) {
93 # Check Priority Level and skip the alert if it is to low.
94 #if ($line =~ /.*\[Priority: (\d+)\].*/) {
95 # return unless($1 < $priority);
98 # Search for a line like xxx.xxx.xxx.xxx -> xxx.xxx.xxx.xxx
99 if ($line =~ /(\d+\.\d+\.\d+\.\d+)+ -\> (\d+\.\d+\.\d+\.\d+)+/) {
100 # Store the grabbed IP-address.
104 # Search for a line like xxx.xxx.xxx.xxx:xxx -> xxx.xxx.xxx.xxx:xxx
105 elsif ($line =~ /(\d+\.\d+\.\d+\.\d+):\d+ -\> (\d+\.\d+\.\d+\.\d+):\d+/) {
106 # Store the obtained IP-address.
110 # Obtain the reported reason.
111 if ($line =~ /.*msg:\"(.*)\".*/) {
112 # Store the extracted message.
117 # Check if at least the IP-address information are obtained from the
120 # Return the extracted values.
121 return "$address $name $message";
124 # If we got here, the alert could not be parsed correctly, return nothing.