]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Re-add code for enable/disable rulefiles
[ipfire-2.x.git] / html / cgi-bin / ids.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2015 IPFire Team <info@ipfire.org> #
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
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27 use File::Copy;
28
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32
33 my %color = ();
34 my %mainsettings = ();
35 my %netsettings = ();
36 my %snortsettings=();
37 my %cgiparams=();
38 my %checked=();
39 my %selected=();
40
41 # Read-in main settings, for language, theme and colors.
42 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
43 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
44
45 # Get netsettings.
46 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
47
48 &Header::showhttpheaders();
49
50 # Default settings for snort.
51 $snortsettings{'ENABLE_SNORT'} = 'off';
52 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
53 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
54 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
55 $snortsettings{'ACTION'} = '';
56 $snortsettings{'RULES'} = '';
57 $snortsettings{'OINKCODE'} = '';
58 $snortsettings{'INSTALLDATE'} = '';
59
60 #Get GUI values
61 &Header::getcgihash(\%cgiparams);
62
63 my $snortrulepath = "/etc/snort/rules";
64 my $snortusedrulefilesfile = "${General::swroot}/snort/snort-used-rulefiles.conf";
65 my $restartsnortrequired = 0;
66 my %snortrules;
67 my $errormessage;
68 my $url;
69
70 ## Grab all available snort rules and store them in the snortrules hash.
71 #
72 # Open snort rules directory and do a directory listing.
73 opendir(DIR, $snortrulepath) or die $!;
74 # Loop through the direcory.
75 while (my $file = readdir(DIR)) {
76
77 # We only want files.
78 next unless (-f "$snortrulepath/$file");
79
80 # Ignore empty files.
81 next if (-z "$snortrulepath/$file");
82
83 # Use a regular expression to find files ending in .rules
84 next unless ($file =~ m/\.rules$/);
85
86 # Ignore files which are not read-able.
87 next unless (-R "$snortrulepath/$file");
88
89 # Call subfunction to read-in rulefile and add rules to
90 # the snortrules hash.
91 &readrulesfile("$file");
92 }
93
94 closedir(DIR);
95
96 # Gather used rulefiles.
97 #
98 # Check if the file for activated rulefiles is not empty.
99 if(-f $snortusedrulefilesfile) {
100 # Open the file for used rulefile and read-in content.
101 open(FILE, $snortusedrulefilesfile) or die "Could not open $snortusedrulefilesfile. $!\n";
102
103 # Read-in content.
104 my @lines = <FILE>;
105
106 # Close file.
107 close(FILE);
108
109 # Loop through the array.
110 foreach my $line (@lines) {
111 # Remove newlines.
112 chomp($line);
113
114 # Skip comments.
115 next if ($line =~ /\#/);
116
117 # Skip blank lines.
118 next if ($line =~ /^\s*$/);
119
120 # Gather rule sid and message from the ruleline.
121 if ($line =~ /.*include \$RULE_PATH\/(.*)/) {
122 my $rulefile = $1;
123
124 # Add the rulefile to the %snortrules hash.
125 $snortrules{$rulefile}{'Rulefile'}{'State'} = "on";
126 }
127 }
128 }
129
130 # Save ruleset.
131 if ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
132 my $enabled_sids_file = "${General::swroot}/snort/oinkmaster-enabled-sids.conf";
133 my $disabled_sids_file = "${General::swroot}/snort/oinkmaster-disabled-sids.conf";
134
135 # Arrays to store sid which should be added to the corresponding files.
136 my @enabled_sids;
137 my @disabled_sids;
138 my @enabled_rulefiles;
139
140 # Loop through the hash of snortrules.
141 foreach my $rulefile(keys %snortrules) {
142 # Check if the rulefile is enabled.
143 if ($cgiparams{$rulefile} eq "on") {
144 # Add rulefile to the array of enabled rulefiles.
145 push(@enabled_rulefiles, $rulefile);
146 }
147
148 # Loop through the single rules of the rulefile.
149 foreach my $sid (keys %{$snortrules{$rulefile}}) {
150 # Check if there exists a key in the cgiparams hash for this sid.
151 if (exists($cgiparams{$sid})) {
152 # Look if the rule is disabled.
153 if ($snortrules{$rulefile}{$sid}{'State'} eq "off") {
154 # Check if the state has been set to 'on'.
155 if ($cgiparams{$sid} eq "on") {
156 # Add the sid to the enabled_sids array.
157 push(@enabled_sids, $sid);
158
159 # Drop item from cgiparams hash.
160 delete $cgiparams{$sid};
161 }
162 }
163 } else {
164 # Look if the rule is enabled.
165 if ($snortrules{$rulefile}{$sid}{'State'} eq "on") {
166 # Check if the state is 'on' and should be disabled.
167 # In this case there is no entry
168 # for the sid in the cgiparams hash.
169 # Add it to the disabled_sids array.
170 push(@disabled_sids, $sid);
171
172 # Drop item from cgiparams hash.
173 delete $cgiparams{$sid};
174 }
175 }
176 }
177 }
178
179 # Check if the enabled_sids array contains any sid's.
180 if (@enabled_sids) {
181 # Open enabled sid's file for writing.
182 open(FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
183
184 # Write header to file.
185 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
186
187 # Loop through the array of enabled sids and write them to the file.
188 foreach my $sid (@enabled_sids) {
189 print FILE "enable_sid $sid\n";
190 }
191
192 # Close file after writing.
193 close(FILE);
194 }
195
196 # Check if the enabled_sids array contains any sid's.
197 if (@disabled_sids) {
198 # Open disabled sid's file for writing.
199 open(FILE, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
200
201 # Write header to file.
202 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
203
204 # Loop through the array of disabled sids and write them to the file.
205 foreach my $sid (@disabled_sids) {
206 print FILE "disable_sid $sid\n";
207 }
208
209 # Close file after writing.
210 close(FILE);
211 }
212
213 # Open file for used rulefiles.
214 open (FILE, ">$snortusedrulefilesfile") or die "Could not wirte to $snortusedrulefilesfile. $!\n";
215
216 # Write header to file.
217 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
218
219 # Check if the enabled_rulefiles array contains any entries.
220 if (@enabled_rulefiles) {
221 # Loop through the array of rulefiles which should be loaded and write the to the file.
222 foreach my $file (@enabled_rulefiles) {
223 print FILE "include \$RULE_PATH/$file\n";
224 }
225 }
226
227 # Close file after writing.
228 close(FILE);
229 }
230
231 if ($snortsettings{'OINKCODE'} ne "") {
232 $errormessage = $Lang::tr{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
233 }
234
235 if (!$errormessage) {
236 if ($snortsettings{'RULES'} eq 'subscripted') {
237 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
238 } elsif ($snortsettings{'RULES'} eq 'registered') {
239 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
240 } elsif ($snortsettings{'RULES'} eq 'community') {
241 $url=" https://www.snort.org/rules/community";
242 } else {
243 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
244 }
245
246 if ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
247 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
248 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
249 {
250 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
251 } else {
252 unlink "${General::swroot}/snort/enable";
253 }
254 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
255 {
256 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
257 } else {
258 unlink "${General::swroot}/snort/enable_green";
259 }
260 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
261 {
262 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
263 } else {
264 unlink "${General::swroot}/snort/enable_blue";
265 }
266 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
267 {
268 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
269 } else {
270 unlink "${General::swroot}/snort/enable_orange";
271 }
272 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
273 {
274 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
275 } else {
276 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
277 }
278
279 system('/usr/local/bin/snortctrl restart >/dev/null');
280 }
281
282 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
283 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
284
285 if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
286 my @df = `/bin/df -B M /var`;
287 foreach my $line (@df) {
288 next if $line =~ m/^Filesystem/;
289 my $return;
290
291 if ($line =~ m/dev/ ) {
292 $line =~ m/^.* (\d+)M.*$/;
293 my @temp = split(/ +/,$line);
294 if ($1<300) {
295 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
296 } else {
297 if ( $snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
298 &downloadrulesfile();
299 sleep(3);
300 $return = `cat /var/tmp/log 2>/dev/null`;
301
302 }
303
304 if ($return =~ "ERROR") {
305 $errormessage = "<br /><pre>".$return."</pre>";
306 } else {
307 system("/usr/local/bin/oinkmaster.pl -v -s -u file:///var/tmp/snortrules.tar.gz -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules >>/var/tmp/log 2>&1 &");
308 sleep(2);
309 }
310 }
311 }
312 }
313 }
314 }
315
316 $checked{'ENABLE_SNORT'}{'off'} = '';
317 $checked{'ENABLE_SNORT'}{'on'} = '';
318 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
319 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
320 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
321 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
322 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
323 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
324 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
325 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
326 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
327 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
328 $selected{'RULES'}{'nothing'} = '';
329 $selected{'RULES'}{'community'} = '';
330 $selected{'RULES'}{'emerging'} = '';
331 $selected{'RULES'}{'registered'} = '';
332 $selected{'RULES'}{'subscripted'} = '';
333 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
334
335 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
336
337 ### Java Script ###
338 print <<END
339 <script>
340 // Tiny java script function to show/hide the rules
341 // of a given category.
342 function showhide(tblname) {
343 \$("#" + tblname).toggle();
344 }
345 </script>
346 END
347 ;
348
349 &Header::openbigbox('100%', 'left', '', $errormessage);
350
351 ###############
352 # DEBUG DEBUG
353 # &Header::openbox('100%', 'left', 'DEBUG');
354 # my $debugCount = 0;
355 # foreach my $line (sort keys %snortsettings) {
356 # print "$line = $snortsettings{$line}<br />\n";
357 # $debugCount++;
358 # }
359 # print "&nbsp;Count: $debugCount\n";
360 # &Header::closebox();
361 # DEBUG DEBUG
362 ###############
363
364 if ($errormessage) {
365 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
366 print "<class name='base'>$errormessage\n";
367 print "&nbsp;</class>\n";
368 &Header::closebox();
369 }
370
371 my $return = `pidof oinkmaster.pl -x`;
372 chomp($return);
373 if ($return) {
374 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
375 print <<END;
376 <table>
377 <tr><td>
378 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />&nbsp;
379 <td>
380 $Lang::tr{'snort working'}
381 <tr><td colspan='2' align='center'>
382 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
383 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
384 </form>
385 <tr><td colspan='2' align='left'><pre>
386 END
387 my @output = `tail -20 /var/tmp/log`;
388 foreach (@output) {
389 print "$_";
390 }
391 print <<END;
392 </pre>
393 </table>
394 END
395 &Header::closebox();
396 &Header::closebigbox();
397 &Header::closepage();
398 exit;
399 refreshpage();
400 }
401
402 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
403 print <<END
404 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
405 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
406 END
407 ;
408 if ($netsettings{'BLUE_DEV'} ne '') {
409 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
410 }
411 if ($netsettings{'ORANGE_DEV'} ne '') {
412 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
413 }
414 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
415
416 print <<END
417 </td></tr>
418 <tr>
419 <td><br><br></td>
420 </tr>
421 <tr>
422 <td><b>$Lang::tr{'ids rules update'}</b></td>
423 </tr>
424 <tr>
425 <td><select name='RULES'>
426 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
427 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
428 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
429 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
430 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
431 </select>
432 </td>
433 </tr>
434 <tr>
435 <td><br />
436 $Lang::tr{'ids rules license'} <a href='https://www.snort.org/subscribe' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}<br /><br />
437 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
438 </td>
439 </tr>
440 <tr>
441 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
442 </tr>
443 <tr>
444 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
445 END
446 ;
447 if ( -e "/var/tmp/snortrules.tar.gz"){
448 my @Info = stat("/var/tmp/snortrules.tar.gz");
449 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
450 }
451 print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
452
453 print <<END
454 </tr>
455 </table>
456 <br><br>
457 <table width='100%'>
458 <tr>
459 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
460 </tr>
461 </table>
462 </form>
463 END
464 ;
465
466 &Header::closebox();
467
468 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
469 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
470
471 # Output display table for rule files
472 print "<table width='100%'>\n";
473
474 # Local variable required for java script to show/hide
475 # rules of a rulefile.
476 my $rulesetcount = 1;
477
478 # Loop over each rule file
479 foreach my $rulefile (sort keys(%snortrules)) {
480 my $rulechecked = '';
481
482 # Check if rule file is enabled
483 if ($snortrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
484 $rulechecked = 'CHECKED';
485 }
486
487 # Table and rows for the rule files.
488 print"<tr>\n";
489 print"<td class='base' width='5%'>\n";
490 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
491 print"</td>\n";
492 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
493 print"<td class='base' width='5%' align='right'>\n";
494 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
495 print"</td>\n";
496 print"</tr>\n";
497
498 # Rows which will be hidden per default and will contain the single rules.
499 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
500 print"<td colspan='3'>\n";
501
502 # Local vars
503 my $lines;
504 my $rows;
505 my $col;
506
507 # New table for the single rules.
508 print "<table width='100%'>\n";
509
510 # Loop over rule file rules
511 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
512 # Local vars
513 my $ruledefchecked = '';
514
515 # Skip rulefile itself.
516 next if ($sid eq "Rulefile");
517
518 # If 2 rules have been displayed, start a new row
519 if (($lines % 2) == 0) {
520 print "</tr><tr>\n";
521
522 # Increase rows by once.
523 $rows++;
524 }
525
526 # Colour lines.
527 if ($rows % 2) {
528 $col="bgcolor='$color{'color20'}'";
529 } else {
530 $col="bgcolor='$color{'color22'}'";
531 }
532
533 # Set rule state
534 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
535 $ruledefchecked = 'CHECKED';
536 }
537
538 # Create rule checkbox and display rule description
539 print "<td class='base' width='5%' align='right' $col>\n";
540 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
541 print "</td>\n";
542 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
543
544 # Increment rule count
545 $lines++;
546 }
547
548 # If do not have a second rule for row, create empty cell
549 if (($lines % 2) != 0) {
550 print "<td class='base'></td>";
551 }
552
553 # Close display table
554 print "</tr></table></td></tr>";
555
556 # Finished whith the rule file, increase count.
557 $rulesetcount++;
558 }
559
560 # Close display table
561 print "</table>";
562
563 print <<END
564 <table width='100%'>
565 <tr>
566 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
567 &nbsp; <!-- space for future online help link -->
568 </td>
569 </tr>
570 </table>
571 </form>
572 END
573 ;
574 &Header::closebox();
575
576 &Header::closebigbox();
577 &Header::closepage();
578
579 sub refreshpage {
580 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );
581 print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";
582 &Header::closebox();
583 }
584
585 sub downloadrulesfile {
586 my $peer;
587 my $peerport;
588
589 unlink("/var/tmp/log");
590
591 unless (-e "${General::swroot}/red/active") {
592 $errormessage = $Lang::tr{'could not download latest updates'};
593 return undef;
594 }
595
596 my %proxysettings=();
597 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
598
599 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
600 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
601 }
602
603 if ($peer) {
604 system("wget -r --proxy=on --proxy-user=$proxysettings{'UPSTREAM_USER'} --proxy-passwd=$proxysettings{'UPSTREAM_PASSWORD'} -e http_proxy=http://$peer:$peerport/ -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
605 } else {
606 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
607 }
608 }
609
610 sub readrulesfile ($) {
611 my $rulefile = shift;
612
613 # Open rule file and read in contents
614 open(RULEFILE, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
615
616 # Store file content in an array.
617 my @lines = <RULEFILE>;
618
619 # Close file.
620 close(RULEFILE);
621
622 # Loop over rule file contents
623 foreach my $line (@lines) {
624 # Remove whitespaces.
625 chomp $line;
626
627 # Skip blank lines.
628 next if ($line =~ /^\s*$/);
629
630 # Local vars.
631 my $sid;
632 my $msg;
633
634 # Gather rule sid and message from the ruleline.
635 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
636 $msg = $1;
637 $sid = $2;
638
639 # Check if a rule has been found.
640 if ($sid && $msg) {
641 # Add rule to the snortrules hash.
642 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
643
644 # Grab status of the rule. Check if ruleline starts with a "dash".
645 if ($line =~ /^\#/) {
646 # If yes, the rule is disabled.
647 $snortrules{$rulefile}{$sid}{'State'} = "off";
648 } else {
649 # Otherwise the rule is enabled.
650 $snortrules{$rulefile}{$sid}{'State'} = "on";
651 }
652 }
653 }
654 }
655 }