]> git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Parser.pm
65f770879014c893167314513ea32af66fcad2a7
[people/stevee/guardian.git] / modules / Parser.pm
1 package Guardian::Parser;
2 use strict;
3 use warnings;
4
5 use Exporter qw(import);
6
7 our @EXPORT_OK = qw(IsSupportedParser Parser);
8
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,
15 );
16
17 #
18 ## The main parsing function.
19 #
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.
23 #
24 sub Parser ($$) {
25 my ($parser, @message) = @_;
26
27 # If no responsible message parser could be found, just return nothing.
28 unless (exists($logfile_parsers{$parser})) {
29 return;
30 }
31
32 # Call responsible message parser.
33 my $action = $logfile_parsers{$parser}->(@message);
34
35 # In case an action has been returned, return it too.
36 if (defined($action)) {
37 # Return which action should be performed.
38 return "count $action";
39 }
40
41 # Return undef, no action required.
42 return undef;
43 }
44
45 #
46 ## IsSupportedParser function.
47 #
48 ## This very tiny function checks if a given parser name is available and
49 ## therefore a supported parser.
50 #
51 ## To perform these check, the function is going to lookup if a key in the
52 ## hash of supported parsers is available
53 #
54 sub IsSupportedParser ($) {
55 my $parser = $_[0];
56
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.
60 return 1;
61 }
62
63 # Return "False" if we got here, and therefore no parser
64 # is available.
65 return;
66 }
67
68 #
69 ## The Snort message parser.
70 #
71 ## This subfunction is responsible for parsing sort alerts and determine if
72 ## an action should be performed.
73 #
74 sub message_parser_snort($) {
75 my @message = @_;
76
77 # XXX
78 # Currently this parser just returns a simple message.
79 return "$message[0] SNORT A simple Snort Message";
80 }
81
82 #
83 ## The SSH message parser.
84 #
85 ## This subfunction is used for parsing and detecting different attacks
86 ## against the SSH service.
87 #
88 sub message_parser_ssh (@) {
89 my @message = @_;
90
91 # The name of the parser module.
92 my $name = "SSH";
93
94 # Variable to store the grabbed IP-address.
95 my $address;
96
97 # Variable to store the parsed event.
98 my $message;
99
100 # Loop through all lines, in case multiple one have
101 # been passed.
102 foreach my $line (@message) {
103 # Check for failed password attempts.
104 if ($line =~/.*sshd.*Failed password for (.*) from (.*) port.*/) {
105 # Store the grabbed IP-address.
106 $address = $2;
107
108 # Set event message.
109 $message = "Possible SSH-Bruteforce Attack for user: $1.";
110 }
111
112 # This should catch Bruteforce Attacks with enabled preauth
113 elsif ($line =~ /.*sshd.*Received disconnect from (.*):.*\[preauth\]/) {
114 # Store obtained IP-address.
115 $address = $1;
116
117 # Set event message.
118 $message = "Possible SSH-Bruteforce Attack - failed preauth.";
119 }
120 }
121
122 # Check if at least the IP-address information has been extracted.
123 if (defined ($address)) {
124 # Return the extracted values and event message.
125 return "$address $name $message";
126 }
127
128 # If we got here, the provided message is not affected by any filter and
129 # therefore can be skipped. Return nothing (False) in this case.
130 return;
131 }
132
133 #
134 ## The HTTPD message parser.
135 #
136 ## This subfunction is used for parsing and detecting different attacks
137 ## against a running HTTPD service.
138 #
139 sub message_parser_httpd (@) {
140 my @message = @_;
141
142 # The name of the parser module.
143 my $name = "HTTPD";
144
145 # Variable to store the grabbed IP-address.
146 my $address;
147
148 # Variable to store the parsed event.
149 my $message;
150
151 # Loop through all lines, in case multiple one have
152 # been passed.
153 foreach my $line (@message) {
154 # This will catch brute-force attacks against htaccess logins (username).
155 if ($line =~ /.*\[error\] \[client (.*)\] user(.*) not found:.*/) {
156 # Store the grabbed IP-address.
157 $address = $1;
158
159 # Set event message.
160 $message = "Possible WUI brute-force attack, wrong user: $2.";
161 }
162
163 # Detect htaccess password brute-forcing against a username.
164 elsif ($line =~ /.*\[error\] \[client (.*)\] user(.*): authentication failure for.*/) {
165 # Store the extracted IP-address.
166 $address = $1;
167
168 # Set event message.
169 $message = "Possible WUI brute-force attack, wrong password for user: $2.";
170 }
171 }
172
173 # Check if at least the IP-address information has been extracted.
174 if (defined ($address)) {
175 # Return the extracted values and event message.
176 return "$address $name $message";
177 }
178
179 # If we got here, the provided message is not affected by any filter and
180 # therefore can be skipped. Return nothing (False) in this case.
181 return;
182 }
183
184 1;