]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Reimplement function to lock page and show working notice
[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
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31 require "${General::swroot}/ids-functions.pl";
32
33 my %color = ();
34 my %mainsettings = ();
35 my %netsettings = ();
36 my %snortrules = ();
37 my %snortsettings=();
38 my %rulesetsources = ();
39 my %cgiparams=();
40 my %checked=();
41 my %selected=();
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.
48 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
49
50 my $snortrulepath = "/etc/snort/rules";
51 my $snortusedrulefilesfile = "${General::swroot}/snort/snort-used-rulefiles.conf";
52 my $errormessage;
53
54 &Header::showhttpheaders();
55
56 # Default settings for snort.
57 $snortsettings{'ENABLE_SNORT'} = 'off';
58 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
59 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
60 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
61 $snortsettings{'RULES'} = '';
62 $snortsettings{'OINKCODE'} = '';
63
64 #Get GUI values
65 &Header::getcgihash(\%cgiparams);
66
67 # Try to determine if oinkmaster is running.
68 my $oinkmaster_pid = `pidof oinkmaster.pl -x`;
69
70 # If oinkmaster is running display output.
71 if ($oinkmaster_pid) {
72 &working("$Lang::tr{'snort working'}");
73 }
74
75 # Check if any error has been stored.
76 if (-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
91 ## Grab all available snort rules and store them in the snortrules hash.
92 #
93 # Open snort rules directory and do a directory listing.
94 opendir(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
104 # Use a regular expression to find files ending in .rules
105 next unless ($file =~ m/\.rules$/);
106
107 # Ignore files which are not read-able.
108 next unless (-R "$snortrulepath/$file");
109
110 # Call subfunction to read-in rulefile and add rules to
111 # the snortrules hash.
112 &readrulesfile("$file");
113 }
114
115 closedir(DIR);
116
117 # Gather used rulefiles.
118 #
119 # Check if the file for activated rulefiles is not empty.
120 if(-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
151 # Save ruleset.
152 if ($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;
159 my @enabled_rulefiles;
160
161 # Loop through the hash of snortrules.
162 foreach my $rulefile(keys %snortrules) {
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);
167
168 # Drop item from cgiparams hash.
169 delete $cgiparams{$rulefile};
170 }
171 }
172
173 # Loop through the hash of snortrules.
174 foreach my $rulefile (keys %snortrules) {
175 # Loop through the single rules of the rulefile.
176 foreach my $sid (keys %{$snortrules{$rulefile}}) {
177 # Skip the current sid if it is not numeric.
178 next unless ($sid =~ /\d+/ );
179
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.
190 delete $cgiparams{$rulefile}{$sid};
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.
203 delete $cgiparams{$rulefile}{$sid};
204 }
205 }
206 }
207 }
208
209 # Open enabled sid's file for writing.
210 open(FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
211
212 # Write header to file.
213 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
214
215 # Check if the enabled_sids array contains any sid's.
216 if (@enabled_sids) {
217 # Loop through the array of enabled sids and write them to the file.
218 foreach my $sid (@enabled_sids) {
219 print FILE "enablesid $sid\n";
220 }
221 }
222
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";
228
229 # Write header to file.
230 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
231
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 }
239
240 # Close file after writing.
241 close(FILE);
242
243 # Open file for used rulefiles.
244 open (FILE, ">$snortusedrulefilesfile") or die "Could not write to $snortusedrulefilesfile. $!\n";
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);
259
260 # Lock the webpage and print message.
261 &working_notice("$Lang::tr{'snort working'}");
262
263 # Call oinkmaster to alter the ruleset.
264 &IDS::oinkmaster();
265
266 # Reload page.
267 &reload();
268
269 # Download new ruleset.
270 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
271 # Check if the red device is active.
272 unless (-e "${General::swroot}/red/active") {
273 $errormessage = $Lang::tr{'could not download latest updates'};
274 }
275
276 # Check if enought free disk space is availabe.
277 $errormessage = &IDS::checkdiskspace();
278
279 # Check if any errors happend.
280 unless ($errormessage) {
281 # Lock the webpage and print notice about downloading
282 # a new ruleset.
283 &working_notice("$Lang::tr{'snort working'}");
284
285 # Call subfunction to download the ruleset.
286 $errormessage = &IDS::downloadruleset();
287
288 # Check if the downloader returned an error.
289 if ($errormessage) {
290 # Call function to store the errormessage.
291 &IDS::log_error($errormessage);
292
293 # Preform a reload of the page.
294 &reload();
295 } else {
296 # Call subfunction to launch oinkmaster.
297 &IDS::oinkmaster();
298
299 # Perform a reload of the page.
300 &reload();
301 }
302 }
303 # Save snort settings.
304 } elsif ($cgiparams{'SNORT'} eq $Lang::tr{'save'}) {
305 # Prevent form name from been stored in conf file.
306 delete $cgiparams{'SNORT'};
307
308 # Check if an oinkcode has been provided.
309 if ($cgiparams{'OINKCODE'}) {
310 # Check if the oinkcode contains unallowed chars.
311 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
312 $errormessage = $Lang::tr{'invalid input for oink code'};
313 }
314 }
315
316 # Go on if there are no error messages.
317 if (!$errormessage) {
318 # Store settings into settings file.
319 &General::writehash("${General::swroot}/snort/settings", \%cgiparams);
320
321 # Call snortctrl to restart snort
322 system('/usr/local/bin/snortctrl restart >/dev/null');
323 }
324 }
325
326 # Read-in snortsettings
327 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
328
329 $checked{'ENABLE_SNORT'}{'off'} = '';
330 $checked{'ENABLE_SNORT'}{'on'} = '';
331 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
332 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
333 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
334 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
335 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
336 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
337 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
338 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
339 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
340 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
341 $selected{'RULES'}{'nothing'} = '';
342 $selected{'RULES'}{'community'} = '';
343 $selected{'RULES'}{'emerging'} = '';
344 $selected{'RULES'}{'registered'} = '';
345 $selected{'RULES'}{'subscripted'} = '';
346 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
347
348 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
349
350 ### Java Script ###
351 print <<END
352 <script>
353 // Tiny java script function to show/hide the rules
354 // of a given category.
355 function showhide(tblname) {
356 \$("#" + tblname).toggle();
357 }
358 </script>
359 END
360 ;
361
362 &Header::openbigbox('100%', 'left', '', $errormessage);
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 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
372
373 my $rulesdate;
374
375 # Check if a ruleset allready has been downloaded.
376 if ( -f "$IDS::rulestarball"){
377 # Call stat on the filename to obtain detailed information.
378 my @Info = stat("$IDS::rulestarball");
379
380 # Grab details about the creation time.
381 $rulesdate = localtime($Info[9]);
382 }
383
384 print <<END
385 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
386 <table width='100%' border='0'>
387 <tr>
388 <td class='base' width='25%'>
389 <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'}>RED Snort
390 </td>
391
392 <td class='base' width='25%'>
393 <input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'}>GREEN Snort
394 </td>
395
396 <td class='base' width='25%'>
397 END
398 ;
399
400 # Check if a blue device is configured.
401 if ($netsettings{'BLUE_DEV'}) {
402 print "<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} />BLUE Snort\n";
403 }
404
405 print "</td>\n";
406
407 print "<td width='25%'>\n";
408
409 # Check if an orange device is configured.
410 if ($netsettings{'ORANGE_DEV'}) {
411 print "<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} />ORANGE Snort\n";
412 }
413
414 print <<END
415 </td>
416 </tr>
417
418 <tr>
419 <td colspan='4'><br><br></td>
420 </tr>
421
422 <tr>
423 <td colspan='4'><b>$Lang::tr{'ids rules update'}</b></td>
424 </tr>
425
426 <tr>
427 <td colspan='4'><select name='RULES'>
428 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
429 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
430 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
431 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
432 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
433 </select>
434 </td>
435 </tr>
436
437 <tr>
438 <td colspan='4'>
439 <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>
440 <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>
441 </td>
442 </tr>
443
444 <tr>
445 <td colspan='4' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}'></td>
446 </tr>
447
448 <tr>
449 <td colspan='4' align='left'><br>
450 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>&nbsp;$Lang::tr{'updates installed'}: $rulesdate
451 </td>
452
453 </tr>
454 </table>
455
456 <br><br>
457
458 <table width='100%'>
459 <tr>
460 <td align='right'><input type='submit' name='SNORT' value='$Lang::tr{'save'}' /></td>
461 </tr>
462 </table>
463 </form>
464 END
465 ;
466
467 &Header::closebox();
468
469 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
470 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
471
472 # Output display table for rule files
473 print "<table width='100%'>\n";
474
475 # Local variable required for java script to show/hide
476 # rules of a rulefile.
477 my $rulesetcount = 1;
478
479 # Loop over each rule file
480 foreach my $rulefile (sort keys(%snortrules)) {
481 my $rulechecked = '';
482
483 # Check if rule file is enabled
484 if ($snortrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
485 $rulechecked = 'CHECKED';
486 }
487
488 # Table and rows for the rule files.
489 print"<tr>\n";
490 print"<td class='base' width='5%'>\n";
491 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
492 print"</td>\n";
493 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
494 print"<td class='base' width='5%' align='right'>\n";
495 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
496 print"</td>\n";
497 print"</tr>\n";
498
499 # Rows which will be hidden per default and will contain the single rules.
500 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
501 print"<td colspan='3'>\n";
502
503 # Local vars
504 my $lines;
505 my $rows;
506 my $col;
507
508 # New table for the single rules.
509 print "<table width='100%'>\n";
510
511 # Loop over rule file rules
512 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
513 # Local vars
514 my $ruledefchecked = '';
515
516 # Skip rulefile itself.
517 next if ($sid eq "Rulefile");
518
519 # If 2 rules have been displayed, start a new row
520 if (($lines % 2) == 0) {
521 print "</tr><tr>\n";
522
523 # Increase rows by once.
524 $rows++;
525 }
526
527 # Colour lines.
528 if ($rows % 2) {
529 $col="bgcolor='$color{'color20'}'";
530 } else {
531 $col="bgcolor='$color{'color22'}'";
532 }
533
534 # Set rule state
535 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
536 $ruledefchecked = 'CHECKED';
537 }
538
539 # Create rule checkbox and display rule description
540 print "<td class='base' width='5%' align='right' $col>\n";
541 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
542 print "</td>\n";
543 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
544
545 # Increment rule count
546 $lines++;
547 }
548
549 # If do not have a second rule for row, create empty cell
550 if (($lines % 2) != 0) {
551 print "<td class='base'></td>";
552 }
553
554 # Close display table
555 print "</tr></table></td></tr>";
556
557 # Finished whith the rule file, increase count.
558 $rulesetcount++;
559 }
560
561 # Close display table
562 print "</table>";
563
564 print <<END
565 <table width='100%'>
566 <tr>
567 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
568 &nbsp; <!-- space for future online help link -->
569 </td>
570 </tr>
571 </table>
572 </form>
573 END
574 ;
575 &Header::closebox();
576 &Header::closebigbox();
577 &Header::closepage();
578
579 #
580 ## A function to display a notice, to lock the webpage and
581 ## tell the user which action currently will be performed.
582 #
583 sub working_notice ($) {
584 my ($message) = @_;
585
586 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
587 &Header::openbigbox('100%', 'left', '', $errormessage);
588 &Header::openbox( 'Waiting', 1,);
589 print <<END;
590 <table>
591 <tr>
592 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
593 <td>$message</td>
594 </tr>
595 </table>
596 END
597 &Header::closebox();
598 &Header::closebigbox();
599 &Header::closepage();
600 }
601
602 #
603 ## A tiny function to perform a reload of the webpage after one second.
604 #
605 sub reload () {
606 print "<meta http-equiv='refresh' content='1'>\n";
607
608 # Stop the script.
609 exit;
610 }
611
612 #
613 ## Private function to read-in and parse rules of a given rulefile.
614 #
615 ## The given file will be read, parsed and all valid rules will be stored by ID,
616 ## message/description and it's state in the snortrules hash.
617 #
618 sub readrulesfile ($) {
619 my $rulefile = shift;
620
621 # Open rule file and read in contents
622 open(RULEFILE, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
623
624 # Store file content in an array.
625 my @lines = <RULEFILE>;
626
627 # Close file.
628 close(RULEFILE);
629
630 # Loop over rule file contents
631 foreach my $line (@lines) {
632 # Remove whitespaces.
633 chomp $line;
634
635 # Skip blank lines.
636 next if ($line =~ /^\s*$/);
637
638 # Local vars.
639 my $sid;
640 my $msg;
641
642 # Gather rule sid and message from the ruleline.
643 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
644 $msg = $1;
645 $sid = $2;
646
647 # Check if a rule has been found.
648 if ($sid && $msg) {
649 # Add rule to the snortrules hash.
650 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
651
652 # Grab status of the rule. Check if ruleline starts with a "dash".
653 if ($line =~ /^\#/) {
654 # If yes, the rule is disabled.
655 $snortrules{$rulefile}{$sid}{'State'} = "off";
656 } else {
657 # Otherwise the rule is enabled.
658 $snortrules{$rulefile}{$sid}{'State'} = "on";
659 }
660 }
661 }
662 }
663 }