]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/ids.cgi
ids-functions.pl: Also log errors to syslog
[ipfire-2.x.git] / html / cgi-bin / ids.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
b6f571fa 5# Copyright (C) 2007-2015 IPFire Team <info@ipfire.org> #
70df8302
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
ac1cfefa
MT
22use strict;
23
24# enable only the following on debugging purpose
90c2e164
CS
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
ac1cfefa 27
986e08d9 28require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
8dcebe53 31require "${General::swroot}/ids-functions.pl";
ac1cfefa 32
f2fdd0c1
CS
33my %color = ();
34my %mainsettings = ();
0b89daee 35my %netsettings = ();
a232b58c 36my %snortrules = ();
ac1cfefa 37my %snortsettings=();
43263ea6 38my %rulesetsources = ();
298723b9 39my %cgiparams=();
ac1cfefa 40my %checked=();
5a3e0dca 41my %selected=();
0b89daee
SS
42
43# Read-in main settings, for language, theme and colors.
44&General::readhash("${General::swroot}/main/settings", \%mainsettings);
45&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
46
47# Get netsettings.
ac1cfefa
MT
48&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
49
43263ea6
SS
50my $snortrulepath = "/etc/snort/rules";
51my $snortusedrulefilesfile = "${General::swroot}/snort/snort-used-rulefiles.conf";
52my $errormessage;
53
ac1cfefa
MT
54&Header::showhttpheaders();
55
0b89daee 56# Default settings for snort.
ac1cfefa
MT
57$snortsettings{'ENABLE_SNORT'} = 'off';
58$snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
59$snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
60$snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
5a3e0dca 61$snortsettings{'RULES'} = '';
ac1cfefa 62$snortsettings{'OINKCODE'} = '';
ac1cfefa 63
298723b9
SS
64#Get GUI values
65&Header::getcgihash(\%cgiparams);
ac1cfefa 66
500c5c55
SS
67# Try to determine if oinkmaster is running.
68my $oinkmaster_pid = `pidof oinkmaster.pl -x`;
69
70# If oinkmaster is running display output.
71if ($oinkmaster_pid) {
72 &working("$Lang::tr{'snort working'}");
73}
74
3983aebd
SS
75# Check if any error has been stored.
76if (-e $IDS::storederrorfile) {
77 # Open file to read in the stored error message.
78 open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
79
80 # Read the stored error message.
81 $errormessage = <FILE>;
82
83 # Close file.
84 close (FILE);
85
86 # Delete the file, which is now not longer required.
87 unlink($IDS::storederrorfile);
88}
89
90
3da6e01b
SS
91## Grab all available snort rules and store them in the snortrules hash.
92#
422204ff
SS
93# Open snort rules directory and do a directory listing.
94opendir(DIR, $snortrulepath) or die $!;
95 # Loop through the direcory.
96 while (my $file = readdir(DIR)) {
97
98 # We only want files.
99 next unless (-f "$snortrulepath/$file");
100
101 # Ignore empty files.
102 next if (-z "$snortrulepath/$file");
103
3da6e01b 104 # Use a regular expression to find files ending in .rules
422204ff
SS
105 next unless ($file =~ m/\.rules$/);
106
3da6e01b
SS
107 # Ignore files which are not read-able.
108 next unless (-R "$snortrulepath/$file");
395e3b90 109
3da6e01b
SS
110 # Call subfunction to read-in rulefile and add rules to
111 # the snortrules hash.
112 &readrulesfile("$file");
395e3b90 113 }
395e3b90 114
3da6e01b 115closedir(DIR);
395e3b90 116
e5738079
SS
117# Gather used rulefiles.
118#
119# Check if the file for activated rulefiles is not empty.
120if(-f $snortusedrulefilesfile) {
121 # Open the file for used rulefile and read-in content.
122 open(FILE, $snortusedrulefilesfile) or die "Could not open $snortusedrulefilesfile. $!\n";
123
124 # Read-in content.
125 my @lines = <FILE>;
126
127 # Close file.
128 close(FILE);
129
130 # Loop through the array.
131 foreach my $line (@lines) {
132 # Remove newlines.
133 chomp($line);
134
135 # Skip comments.
136 next if ($line =~ /\#/);
137
138 # Skip blank lines.
139 next if ($line =~ /^\s*$/);
140
141 # Gather rule sid and message from the ruleline.
142 if ($line =~ /.*include \$RULE_PATH\/(.*)/) {
143 my $rulefile = $1;
144
145 # Add the rulefile to the %snortrules hash.
146 $snortrules{$rulefile}{'Rulefile'}{'State'} = "on";
147 }
148 }
149}
150
298723b9
SS
151# Save ruleset.
152if ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
153 my $enabled_sids_file = "${General::swroot}/snort/oinkmaster-enabled-sids.conf";
154 my $disabled_sids_file = "${General::swroot}/snort/oinkmaster-disabled-sids.conf";
155
156 # Arrays to store sid which should be added to the corresponding files.
157 my @enabled_sids;
158 my @disabled_sids;
e5738079 159 my @enabled_rulefiles;
298723b9
SS
160
161 # Loop through the hash of snortrules.
162 foreach my $rulefile(keys %snortrules) {
e5738079
SS
163 # Check if the rulefile is enabled.
164 if ($cgiparams{$rulefile} eq "on") {
165 # Add rulefile to the array of enabled rulefiles.
166 push(@enabled_rulefiles, $rulefile);
b65b5ef3
SS
167
168 # Drop item from cgiparams hash.
169 delete $cgiparams{$rulefile};
e5738079 170 }
466c6779 171 }
e5738079 172
466c6779
SS
173 # Loop through the hash of snortrules.
174 foreach my $rulefile (keys %snortrules) {
298723b9
SS
175 # Loop through the single rules of the rulefile.
176 foreach my $sid (keys %{$snortrules{$rulefile}}) {
c51a044a
SS
177 # Skip the current sid if it is not numeric.
178 next unless ($sid =~ /\d+/ );
179
298723b9
SS
180 # Check if there exists a key in the cgiparams hash for this sid.
181 if (exists($cgiparams{$sid})) {
182 # Look if the rule is disabled.
183 if ($snortrules{$rulefile}{$sid}{'State'} eq "off") {
184 # Check if the state has been set to 'on'.
185 if ($cgiparams{$sid} eq "on") {
186 # Add the sid to the enabled_sids array.
187 push(@enabled_sids, $sid);
188
189 # Drop item from cgiparams hash.
60333473 190 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
191 }
192 }
193 } else {
194 # Look if the rule is enabled.
195 if ($snortrules{$rulefile}{$sid}{'State'} eq "on") {
196 # Check if the state is 'on' and should be disabled.
197 # In this case there is no entry
198 # for the sid in the cgiparams hash.
199 # Add it to the disabled_sids array.
200 push(@disabled_sids, $sid);
201
202 # Drop item from cgiparams hash.
60333473 203 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
204 }
205 }
206 }
207 }
208
37659505
SS
209 # Open enabled sid's file for writing.
210 open(FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
298723b9 211
37659505
SS
212 # Write header to file.
213 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
298723b9 214
37659505
SS
215 # Check if the enabled_sids array contains any sid's.
216 if (@enabled_sids) {
298723b9
SS
217 # Loop through the array of enabled sids and write them to the file.
218 foreach my $sid (@enabled_sids) {
37659505 219 print FILE "enablesid $sid\n";
298723b9 220 }
298723b9
SS
221 }
222
37659505
SS
223 # Close file after writing.
224 close(FILE);
225
226 # Open disabled sid's file for writing.
227 open(FILE, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
298723b9 228
37659505
SS
229 # Write header to file.
230 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
298723b9 231
37659505
SS
232 # Check if the enabled_sids array contains any sid's.
233 if (@disabled_sids) {
234 # Loop through the array of disabled sids and write them to the file.
235 foreach my $sid (@disabled_sids) {
236 print FILE "disablesid $sid\n";
237 }
238 }
298723b9 239
37659505
SS
240 # Close file after writing.
241 close(FILE);
e5738079
SS
242
243 # Open file for used rulefiles.
37659505 244 open (FILE, ">$snortusedrulefilesfile") or die "Could not write to $snortusedrulefilesfile. $!\n";
e5738079
SS
245
246 # Write header to file.
247 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
248
249 # Check if the enabled_rulefiles array contains any entries.
250 if (@enabled_rulefiles) {
251 # Loop through the array of rulefiles which should be loaded and write the to the file.
252 foreach my $file (@enabled_rulefiles) {
253 print FILE "include \$RULE_PATH/$file\n";
254 }
255 }
256
257 # Close file after writing.
258 close(FILE);
52599865
SS
259
260 # Call oinkmaster to alter the ruleset.
261 &oinkmaster();
262
263# Download new ruleset.
264} elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
43263ea6
SS
265 # Check if the red device is active.
266 unless (-e "${General::swroot}/red/active") {
267 $errormessage = $Lang::tr{'could not download latest updates'};
268 }
52599865 269
3983aebd 270 # Check if enought free disk space is availabe.
59052432 271 $errormessage = &IDS::checkdiskspace();
52599865 272
43263ea6
SS
273 # Check if any errors happend.
274 unless ($errormessage) {
3983aebd
SS
275 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
276 &Header::openbigbox('100%', 'left', '', $errormessage);
277 &Header::openbox( 'Waiting', 1,);
278 print <<END;
279 <table>
280 <tr>
281 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
282 <td>$Lang::tr{'snort working'}</td>
283 </tr>
284 </table>
285END
286 &Header::closebox();
287 &Header::closebigbox();
288 &Header::closepage();
289
43263ea6 290 # Call subfunction to download the ruleset.
eea2670b 291 $errormessage = &IDS::downloadruleset();
8f22237b 292
3983aebd
SS
293 # Check if the downloader returned an error.
294 if ($errormessage) {
295 # Call function to store the errormessage.
296 &IDS::log_error($errormessage);
52599865 297
3983aebd
SS
298 # Preform a reload of the page.
299 &reload();
300 } else {
301 # Call subfunction to launch oinkmaster.
302 &IDS::oinkmaster();
43263ea6 303
3983aebd
SS
304 # Perform a reload of the page.
305 &reload();
306 }
52599865 307 }
a232b58c
SS
308# Save snort settings.
309} elsif ($cgiparams{'SNORT'} eq $Lang::tr{'save'}) {
310 # Prevent form name from been stored in conf file.
311 delete $cgiparams{'SNORT'};
312
313 # Check if an oinkcode has been provided.
314 if ($cgiparams{'OINKCODE'}) {
315 # Check if the oinkcode contains unallowed chars.
316 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
317 $errormessage = $Lang::tr{'invalid input for oink code'};
318 }
f9c2147d 319 }
ac1cfefa 320
a232b58c
SS
321 # Go on if there are no error messages.
322 if (!$errormessage) {
323 # Store settings into settings file.
324 &General::writehash("${General::swroot}/snort/settings", \%cgiparams);
325
a232b58c 326 # Call snortctrl to restart snort
a9a91e5f
MT
327 system('/usr/local/bin/snortctrl restart >/dev/null');
328 }
ac1cfefa
MT
329}
330
52599865
SS
331# Read-in snortsettings
332&General::readhash("${General::swroot}/snort/settings", \%snortsettings);
333
ac1cfefa
MT
334$checked{'ENABLE_SNORT'}{'off'} = '';
335$checked{'ENABLE_SNORT'}{'on'} = '';
336$checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
337$checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
338$checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
339$checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
340$checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
341$checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
342$checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
343$checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
344$checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
345$checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
5a3e0dca 346$selected{'RULES'}{'nothing'} = '';
5a3e0dca 347$selected{'RULES'}{'community'} = '';
a0fa489f 348$selected{'RULES'}{'emerging'} = '';
5a3e0dca
MT
349$selected{'RULES'}{'registered'} = '';
350$selected{'RULES'}{'subscripted'} = '';
351$selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
ac1cfefa
MT
352
353&Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
354
17726644
SS
355### Java Script ###
356print <<END
357<script>
358 // Tiny java script function to show/hide the rules
359 // of a given category.
360 function showhide(tblname) {
361 \$("#" + tblname).toggle();
362 }
363</script>
364END
365;
366
ac1cfefa
MT
367&Header::openbigbox('100%', 'left', '', $errormessage);
368
369if ($errormessage) {
370 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
371 print "<class name='base'>$errormessage\n";
372 print "&nbsp;</class>\n";
373 &Header::closebox();
374}
375
7cc8a0e5 376&Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
1504a375
SS
377
378my $rulesdate;
379
380# Check if a ruleset allready has been downloaded.
a69b96d2 381if ( -f "$IDS::rulestarball"){
1504a375 382 # Call stat on the filename to obtain detailed information.
a69b96d2 383 my @Info = stat("$IDS::rulestarball");
1504a375
SS
384
385 # Grab details about the creation time.
386 $rulesdate = localtime($Info[9]);
387}
388
ac1cfefa 389print <<END
1504a375
SS
390<form method='post' action='$ENV{'SCRIPT_NAME'}'>
391 <table width='100%' border='0'>
392 <tr>
393 <td class='base' width='25%'>
394 <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'}>RED Snort
395 </td>
396
397 <td class='base' width='25%'>
398 <input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'}>GREEN Snort
399 </td>
400
401 <td class='base' width='25%'>
ac1cfefa
MT
402END
403;
1504a375
SS
404
405# Check if a blue device is configured.
406if ($netsettings{'BLUE_DEV'}) {
407 print "<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} />BLUE Snort\n";
ac1cfefa 408}
1504a375
SS
409
410print "</td>\n";
411
412print "<td width='25%'>\n";
413
414# Check if an orange device is configured.
415if ($netsettings{'ORANGE_DEV'}) {
416 print "<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} />ORANGE Snort\n";
ac1cfefa 417}
1b73b07e 418
ac1cfefa 419print <<END
1504a375
SS
420 </td>
421 </tr>
422
423 <tr>
424 <td colspan='4'><br><br></td>
425 </tr>
426
427 <tr>
428 <td colspan='4'><b>$Lang::tr{'ids rules update'}</b></td>
429 </tr>
430
431 <tr>
432 <td colspan='4'><select name='RULES'>
5a3e0dca 433 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
a0fa489f 434 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
5a3e0dca
MT
435 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
436 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
437 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
438 </select>
1504a375
SS
439 </td>
440 </tr>
441
442 <tr>
443 <td colspan='4'>
444 <br>$Lang::tr{'ids rules license'} <a href='https://www.snort.org/subscribe' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}</br>
445 <br>$Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}</br>
446 </td>
447 </tr>
448
449 <tr>
450 <td colspan='4' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}'></td>
451 </tr>
452
453 <tr>
454 <td colspan='4' align='left'><br>
455 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>&nbsp;$Lang::tr{'updates installed'}: $rulesdate
456 </td>
457
458 </tr>
459 </table>
460
461 <br><br>
462
463 <table width='100%'>
464 <tr>
465 <td align='right'><input type='submit' name='SNORT' value='$Lang::tr{'save'}' /></td>
466 </tr>
467 </table>
ac1cfefa
MT
468</form>
469END
470;
471
ac1cfefa 472&Header::closebox();
fbfdb241 473
f7fcd1c0 474&Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
298723b9
SS
475 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
476
f7fcd1c0 477 # Output display table for rule files
17726644 478 print "<table width='100%'>\n";
ce0e83b3 479
17726644
SS
480 # Local variable required for java script to show/hide
481 # rules of a rulefile.
482 my $rulesetcount = 1;
f7fcd1c0
SS
483
484 # Loop over each rule file
485 foreach my $rulefile (sort keys(%snortrules)) {
486 my $rulechecked = '';
487
f7fcd1c0 488 # Check if rule file is enabled
e5738079 489 if ($snortrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
f7fcd1c0
SS
490 $rulechecked = 'CHECKED';
491 }
3ffee04b 492
17726644
SS
493 # Table and rows for the rule files.
494 print"<tr>\n";
495 print"<td class='base' width='5%'>\n";
e5738079 496 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
17726644
SS
497 print"</td>\n";
498 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
499 print"<td class='base' width='5%' align='right'>\n";
500 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
501 print"</td>\n";
502 print"</tr>\n";
503
504 # Rows which will be hidden per default and will contain the single rules.
505 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
506 print"<td colspan='3'>\n";
507
508 # Local vars
509 my $lines;
510 my $rows;
511 my $col;
512
513 # New table for the single rules.
514 print "<table width='100%'>\n";
515
516 # Loop over rule file rules
517 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
f7fcd1c0 518 # Local vars
17726644 519 my $ruledefchecked = '';
f9c2147d 520
e5738079
SS
521 # Skip rulefile itself.
522 next if ($sid eq "Rulefile");
523
17726644
SS
524 # If 2 rules have been displayed, start a new row
525 if (($lines % 2) == 0) {
526 print "</tr><tr>\n";
3ffee04b 527
17726644
SS
528 # Increase rows by once.
529 $rows++;
530 }
f7fcd1c0 531
17726644
SS
532 # Colour lines.
533 if ($rows % 2) {
534 $col="bgcolor='$color{'color20'}'";
535 } else {
536 $col="bgcolor='$color{'color22'}'";
f7fcd1c0
SS
537 }
538
17726644 539 # Set rule state
298723b9 540 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
17726644 541 $ruledefchecked = 'CHECKED';
f7fcd1c0 542 }
3ffee04b 543
17726644
SS
544 # Create rule checkbox and display rule description
545 print "<td class='base' width='5%' align='right' $col>\n";
546 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
547 print "</td>\n";
548 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
549
550 # Increment rule count
551 $lines++;
552 }
553
554 # If do not have a second rule for row, create empty cell
555 if (($lines % 2) != 0) {
556 print "<td class='base'></td>";
f7fcd1c0
SS
557 }
558
559 # Close display table
17726644 560 print "</tr></table></td></tr>";
3ffee04b 561
17726644
SS
562 # Finished whith the rule file, increase count.
563 $rulesetcount++;
f7fcd1c0 564 }
17726644
SS
565
566 # Close display table
567 print "</table>";
568
f7fcd1c0 569print <<END
2999f1d2
CS
570<table width='100%'>
571<tr>
298723b9 572 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
3ffee04b
CS
573 &nbsp; <!-- space for future online help link -->
574 </td>
2999f1d2
CS
575</tr>
576</table>
298723b9 577</form>
3ffee04b
CS
578END
579;
f7fcd1c0 580&Header::closebox();
ac1cfefa
MT
581&Header::closebigbox();
582&Header::closepage();
583
3983aebd
SS
584#
585## A tiny function to perform a reload of the webpage after one second.
586#
587sub reload () {
588 print "<meta http-equiv='refresh' content='1'>\n";
589
590 # Stop the script.
591 exit;
a70d269a
SS
592}
593
25f5cb0d
SS
594#
595## Private function to read-in and parse rules of a given rulefile.
596#
597## The given file will be read, parsed and all valid rules will be stored by ID,
598## message/description and it's state in the snortrules hash.
599#
3da6e01b
SS
600sub readrulesfile ($) {
601 my $rulefile = shift;
602
603 # Open rule file and read in contents
604 open(RULEFILE, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
605
606 # Store file content in an array.
607 my @lines = <RULEFILE>;
608
609 # Close file.
610 close(RULEFILE);
611
612 # Loop over rule file contents
613 foreach my $line (@lines) {
614 # Remove whitespaces.
615 chomp $line;
616
617 # Skip blank lines.
618 next if ($line =~ /^\s*$/);
619
620 # Local vars.
621 my $sid;
622 my $msg;
623
624 # Gather rule sid and message from the ruleline.
625 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
626 $msg = $1;
627 $sid = $2;
628
629 # Check if a rule has been found.
630 if ($sid && $msg) {
631 # Add rule to the snortrules hash.
632 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
633
634 # Grab status of the rule. Check if ruleline starts with a "dash".
635 if ($line =~ /^\#/) {
636 # If yes, the rule is disabled.
298723b9 637 $snortrules{$rulefile}{$sid}{'State'} = "off";
3da6e01b
SS
638 } else {
639 # Otherwise the rule is enabled.
298723b9 640 $snortrules{$rulefile}{$sid}{'State'} = "on";
3da6e01b
SS
641 }
642 }
643 }
644 }
645}