]>
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 "httpd" => \
&message_parser_httpd
,
13 "snort" => \
&message_parser_snort
,
14 "ssh" => \
&message_parser_ssh
,
18 ## The main parsing function.
20 ## It is used to determine which sub-parser has to be used to
21 ## parse the given message in the right way and to return if
22 ## any action should be performed.
25 my ($parser, @message) = @_;
27 # If no responsible message parser could be found, just return nothing.
28 unless (exists($logfile_parsers{$parser})) {
32 # Call responsible message parser.
33 my @actions = $logfile_parsers{$parser}->(@message);
35 # In case an action has been returned, return it too.
37 # Return which actions should be performed.
41 # Return undef, if no actions are required.
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
(@
) {
81 # Temporary array to store single alerts.
84 # The name of the parser module.
87 # Default returned message in case no one could be grabbed
88 # from the snort alert.
89 my $message = "An active snort rule has matched and gained an alert.";
91 # Snort uses a log buffer and a result of this, when detecting multiple
92 # events at once, multiple alerts will be written at one time to the alert
93 # file. They have to be seperated from each, to be able to parse them
95 foreach my $line (@message) {
96 # Remove any newlines.
99 # A single alert contains multiple lines, push all of them
103 # Each alert ends with an empty line, if one is found,
104 # all lines of the current processed alert have been found
105 # and pushed to the temporary array.
106 if($line =~ /^\s*$/) {
107 # Variable to store the grabbed IP-address.
110 # Loop through all lines of the current alert.
111 foreach my $line (@alert) {
112 # Check Priority Level and skip the alert if it is to low.
113 #if ($line =~ /.*\[Priority: (\d+)\].*/) {
114 # return unless($1 < $priority);
117 # Search for a line like xxx.xxx.xxx.xxx -> xxx.xxx.xxx.xxx
118 if ($line =~ /(\d+\.\d+\.\d+\.\d+)+ -\> (\d+\.\d+\.\d+\.\d+)+/) {
119 # Store the grabbed IP-address.
123 # Search for a line like xxx.xxx.xxx.xxx:xxx -> xxx.xxx.xxx.xxx:xxx
124 elsif ($line =~ /(\d+\.\d+\.\d+\.\d+):\d+ -\> (\d+\.\d+\.\d+\.\d+):\d+/) {
125 # Store the obtained IP-address.
129 # Obtain the reported reason from the headline of the alert.
130 if ($line =~ /.*\] (.*) \[\*\*\]/) {
131 # Store the extracted message.
135 # If the reason could not be determined, try to obtain it from a msg field.
136 elsif ($line =~ /.*msg:\"(.*)\".*/) {
137 # Store the extracted message.
142 # Check if at least the IP-address information has been extracted.
143 if (defined ($address)) {
144 # Add the extracted values and event message for the computed
145 # event to the actions array.
146 push(@actions, "count $address $name $message");
149 # The alert has been processed, clear the temporary array for storing
155 # If any actions are required, return the array.
160 # If we got here, the alert could not be parsed correctly, or did not match any filter.
161 # Therefore it can be skipped - return nothing.
166 ## The SSH message parser.
168 ## This subfunction is used for parsing and detecting different attacks
169 ## against the SSH service.
171 sub message_parser_ssh
(@
) {
175 # The name of the parser module.
178 # Variable to store the grabbed IP-address.
181 # Variable to store the parsed event.
184 # Loop through all lines, in case multiple one have
186 foreach my $line (@message) {
187 # Check for failed password attempts.
188 if ($line =~/.*sshd.*Failed password for (.*) from (.*) port.*/) {
189 # Store the grabbed IP-address.
193 $message = "Possible SSH-Bruteforce Attack for user: $1.";
196 # This should catch Bruteforce Attacks with enabled preauth
197 elsif ($line =~ /.*sshd.*Received disconnect from (.*):.*\[preauth\]/) {
198 # Store obtained IP-address.
202 $message = "Possible SSH-Bruteforce Attack - failed preauth.";
205 # Check if at least the IP-address information has been extracted.
206 if (defined ($address)) {
207 # Add the extracted values and event message for the computed
208 # event to the actions array.
209 push(@actions, "count $address $name $message");
213 # If any actions are required, return the array.
218 # If we got here, the provided message is not affected by any filter and
219 # therefore can be skipped. Return nothing (False) in this case.
224 ## The HTTPD message parser.
226 ## This subfunction is used for parsing and detecting different attacks
227 ## against a running HTTPD service.
229 sub message_parser_httpd
(@
) {
233 # The name of the parser module.
236 # Variable to store the grabbed IP-address.
239 # Variable to store the parsed event.
242 # Loop through all lines, in case multiple one have
244 foreach my $line (@message) {
245 # This will catch brute-force attacks against htaccess logins (username).
246 if ($line =~ /.*\[error\] \[client (.*)\] user(.*) not found:.*/) {
247 # Store the grabbed IP-address.
251 $message = "Possible WUI brute-force attack, wrong user: $2.";
254 # Detect htaccess password brute-forcing against a username.
255 elsif ($line =~ /.*\[error\] \[client (.*)\] user(.*): authentication failure for.*/) {
256 # Store the extracted IP-address.
260 $message = "Possible WUI brute-force attack, wrong password for user: $2.";
263 # Check if at least the IP-address information has been extracted.
264 if (defined ($address)) {
265 # Add the extracted values and event message to the actions array.
266 push(@actions, "count $address $name $message");
270 # If any actions are required, return the array.
275 # If we got here, the provided message is not affected by any filter and
276 # therefore can be skipped. Return nothing (False) in this case.