]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
0dce9e994d44ad814d8984493cb4d59b9b7473a5
[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-2018 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 %idsrules = ();
36 my %idssettings=();
37 my %rulesetsources = ();
38 my %cgiparams=();
39 my %checked=();
40 my %selected=();
41
42 # Read-in main settings, for language, theme and colors.
43 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
44 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
45
46 # Get the available network zones, based on the config type of the system and store
47 # the list of zones in an array.
48 my @network_zones = &IDS::get_available_network_zones();
49
50 my $idsusedrulefilesfile = "$IDS::settingsdir/suricata-used-rulefiles.yaml";
51 my $errormessage;
52
53 &Header::showhttpheaders();
54
55 #Get GUI values
56 &Header::getcgihash(\%cgiparams);
57
58 # Check if any error has been stored.
59 if (-e $IDS::storederrorfile) {
60 # Open file to read in the stored error message.
61 open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
62
63 # Read the stored error message.
64 $errormessage = <FILE>;
65
66 # Close file.
67 close (FILE);
68
69 # Delete the file, which is now not longer required.
70 unlink($IDS::storederrorfile);
71 }
72
73
74 ## Grab all available snort rules and store them in the idsrules hash.
75 #
76 # Open snort rules directory and do a directory listing.
77 opendir(DIR, $IDS::rulespath) or die $!;
78 # Loop through the direcory.
79 while (my $file = readdir(DIR)) {
80
81 # We only want files.
82 next unless (-f "$IDS::rulespath/$file");
83
84 # Ignore empty files.
85 next if (-z "$IDS::rulespath/$file");
86
87 # Use a regular expression to find files ending in .rules
88 next unless ($file =~ m/\.rules$/);
89
90 # Ignore files which are not read-able.
91 next unless (-R "$IDS::rulespath/$file");
92
93 # Call subfunction to read-in rulefile and add rules to
94 # the idsrules hash.
95 &readrulesfile("$file");
96 }
97
98 closedir(DIR);
99
100 # Gather used rulefiles.
101 #
102 # Check if the file for activated rulefiles is not empty.
103 if(-f $idsusedrulefilesfile) {
104 # Open the file for used rulefile and read-in content.
105 open(FILE, $idsusedrulefilesfile) or die "Could not open $idsusedrulefilesfile. $!\n";
106
107 # Read-in content.
108 my @lines = <FILE>;
109
110 # Close file.
111 close(FILE);
112
113 # Loop through the array.
114 foreach my $line (@lines) {
115 # Remove newlines.
116 chomp($line);
117
118 # Skip comments.
119 next if ($line =~ /\#/);
120
121 # Skip blank lines.
122 next if ($line =~ /^\s*$/);
123
124 # Gather rule sid and message from the ruleline.
125 if ($line =~ /.*- (.*)/) {
126 my $rulefile = $1;
127
128 # Add the rulefile to the %idsrules hash.
129 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
130 }
131 }
132 }
133
134 # Save ruleset.
135 if ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
136 my $enabled_sids_file = "$IDS::settingsdir/oinkmaster-enabled-sids.conf";
137 my $disabled_sids_file = "$IDS::settingsdir/oinkmaster-disabled-sids.conf";
138
139 # Arrays to store sid which should be added to the corresponding files.
140 my @enabled_sids;
141 my @disabled_sids;
142 my @enabled_rulefiles;
143
144 # Loop through the hash of idsrules.
145 foreach my $rulefile(keys %idsrules) {
146 # Check if the rulefile is enabled.
147 if ($cgiparams{$rulefile} eq "on") {
148 # Add rulefile to the array of enabled rulefiles.
149 push(@enabled_rulefiles, $rulefile);
150
151 # Drop item from cgiparams hash.
152 delete $cgiparams{$rulefile};
153 }
154 }
155
156 # Loop through the hash of idsrules.
157 foreach my $rulefile (keys %idsrules) {
158 # Loop through the single rules of the rulefile.
159 foreach my $sid (keys %{$idsrules{$rulefile}}) {
160 # Skip the current sid if it is not numeric.
161 next unless ($sid =~ /\d+/ );
162
163 # Check if there exists a key in the cgiparams hash for this sid.
164 if (exists($cgiparams{$sid})) {
165 # Look if the rule is disabled.
166 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
167 # Check if the state has been set to 'on'.
168 if ($cgiparams{$sid} eq "on") {
169 # Add the sid to the enabled_sids array.
170 push(@enabled_sids, $sid);
171
172 # Drop item from cgiparams hash.
173 delete $cgiparams{$rulefile}{$sid};
174 }
175 }
176 } else {
177 # Look if the rule is enabled.
178 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
179 # Check if the state is 'on' and should be disabled.
180 # In this case there is no entry
181 # for the sid in the cgiparams hash.
182 # Add it to the disabled_sids array.
183 push(@disabled_sids, $sid);
184
185 # Drop item from cgiparams hash.
186 delete $cgiparams{$rulefile}{$sid};
187 }
188 }
189 }
190 }
191
192 # Open enabled sid's file for writing.
193 open(FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
194
195 # Write header to file.
196 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
197
198 # Check if the enabled_sids array contains any sid's.
199 if (@enabled_sids) {
200 # Loop through the array of enabled sids and write them to the file.
201 foreach my $sid (@enabled_sids) {
202 print FILE "enablesid $sid\n";
203 }
204 }
205
206 # Close file after writing.
207 close(FILE);
208
209 # Open disabled sid's file for writing.
210 open(FILE, ">$disabled_sids_file") or die "Could not write to $disabled_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 (@disabled_sids) {
217 # Loop through the array of disabled sids and write them to the file.
218 foreach my $sid (@disabled_sids) {
219 print FILE "disablesid $sid\n";
220 }
221 }
222
223 # Close file after writing.
224 close(FILE);
225
226 # Open file for used rulefiles.
227 open (FILE, ">$idsusedrulefilesfile") or die "Could not write to $idsusedrulefilesfile. $!\n";
228
229 # Write yaml header to the file.
230 print FILE "%YAML 1.1\n";
231 print FILE "---\n\n";
232
233 # Write header to file.
234 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
235
236 # Check if the enabled_rulefiles array contains any entries.
237 if (@enabled_rulefiles) {
238 # Loop through the array of rulefiles which should be loaded and write the to the file.
239 foreach my $file (@enabled_rulefiles) {
240 print FILE " - $file\n";
241 }
242 }
243
244 # Close file after writing.
245 close(FILE);
246
247 # Lock the webpage and print message.
248 &working_notice("$Lang::tr{'snort working'}");
249
250 # Call oinkmaster to alter the ruleset.
251 &IDS::oinkmaster();
252
253 # Reload page.
254 &reload();
255
256 # Download new ruleset.
257 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
258 # Check if the red device is active.
259 unless (-e "${General::swroot}/red/active") {
260 $errormessage = $Lang::tr{'could not download latest updates'};
261 }
262
263 # Check if enought free disk space is availabe.
264 if(&IDS::checkdiskspace()) {
265 $errormessage = "$Lang::tr{'not enough disk space'}";
266 }
267
268 # Check if any errors happend.
269 unless ($errormessage) {
270 # Lock the webpage and print notice about downloading
271 # a new ruleset.
272 &working_notice("$Lang::tr{'snort working'}");
273
274 # Call subfunction to download the ruleset.
275 if(&IDS::downloadruleset()) {
276 $errormessage = $Lang::tr{'could not download latest updates'};
277
278 # Call function to store the errormessage.
279 &IDS::_store_error_message($errormessage);
280
281 # Preform a reload of the page.
282 &reload();
283 } else {
284 # Call subfunction to launch oinkmaster.
285 &IDS::oinkmaster();
286
287 # Perform a reload of the page.
288 &reload();
289 }
290 }
291 # Save snort settings.
292 } elsif ($cgiparams{'SNORT'} eq $Lang::tr{'save'}) {
293 # Prevent form name from been stored in conf file.
294 delete $cgiparams{'SNORT'};
295
296 # Check if an oinkcode has been provided.
297 if ($cgiparams{'OINKCODE'}) {
298 # Check if the oinkcode contains unallowed chars.
299 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
300 $errormessage = $Lang::tr{'invalid input for oink code'};
301 }
302 }
303
304 # Go on if there are no error messages.
305 if (!$errormessage) {
306 # Store settings into settings file.
307 &General::writehash("$IDS::settingsdir/settings", \%cgiparams);
308
309 # Call suricatactrl to restart the IDS
310 system('/usr/local/bin/suricatactrl restart >/dev/null');
311 }
312 }
313
314 # Read-in idssettings
315 &General::readhash("$IDS::settingsdir/settings", \%idssettings);
316
317 $checked{'ENABLE_IDS'}{'off'} = '';
318 $checked{'ENABLE_IDS'}{'on'} = '';
319 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
320 $selected{'RULES'}{'nothing'} = '';
321 $selected{'RULES'}{'community'} = '';
322 $selected{'RULES'}{'emerging'} = '';
323 $selected{'RULES'}{'registered'} = '';
324 $selected{'RULES'}{'subscripted'} = '';
325 $selected{'RULES'}{$idssettings{'RULES'}} = "selected='selected'";
326
327 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
328
329 ### Java Script ###
330 print <<END
331 <script>
332 // Tiny java script function to show/hide the rules
333 // of a given category.
334 function showhide(tblname) {
335 \$("#" + tblname).toggle();
336 }
337 </script>
338 END
339 ;
340
341 &Header::openbigbox('100%', 'left', '', $errormessage);
342
343 if ($errormessage) {
344 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
345 print "<class name='base'>$errormessage\n";
346 print "&nbsp;</class>\n";
347 &Header::closebox();
348 }
349
350 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
351
352 my $rulesdate;
353
354 # Check if a ruleset allready has been downloaded.
355 if ( -f "$IDS::rulestarball"){
356 # Call stat on the filename to obtain detailed information.
357 my @Info = stat("$IDS::rulestarball");
358
359 # Grab details about the creation time.
360 $rulesdate = localtime($Info[9]);
361 }
362
363 print <<END
364 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
365 <table width='100%' border='0'>
366 <tr>
367 <td class='base' width='25%'>
368 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
369 </td>
370
371 <td class='base' width='25%'>
372 &nbsp
373 </td>
374 </tr>
375
376 <tr>
377 <td colspan='2'><br><br>
378 </tr>
379
380 <tr>
381 <td class='base' width='25%'>
382 <b>$Lang::tr{'ids analyze incomming traffic'}</b>
383 </td>
384
385 <td class='base' width='25%'>
386 <b>$Lang::tr{'ids analyze routing traffic'}</b>
387 </td>
388 </tr>
389 END
390 ;
391
392 # Loop through the array of available networks and print config options.
393 foreach my $zone (@network_zones) {
394 my $checked_input;
395 my $checked_forward;
396
397 # Convert current zone name to upper case.
398 my $zone_upper = uc($zone);
399
400 # Grab checkbox status from settings hash.
401 if ($idssettings{"ENABLE_IDS_INPUT_$zone_upper"} eq "on") {
402 $checked_input = "checked = 'checked'";
403 }
404
405 # Do the same for the forward setting.
406 if ($idssettings{"ENABLE_IDS_FORWARD_$zone_upper"} eq "on") {
407 $checked_forward = "checked = 'checked'";
408 }
409
410 print "<tr>\n";
411 print "<td class='base' width='25%'>\n";
412 print "<input type='checkbox' name='ENABLE_IDS_INPUT_$zone_upper' $checked_input>$Lang::tr{'ids active on'} $Lang::tr{$zone}\n";
413 print "</td>\n";
414
415 print "<td class='base' width='25%'>\n";
416 print "<input type='checkbox' name='ENABLE_IDS_FORWARD_$zone_upper' $checked_forward>$Lang::tr{'ids active on'} $Lang::tr{$zone}\n";
417 print "</td>\n";
418 print "</tr>\n";
419 }
420
421 print <<END
422 </tr>
423
424 <tr>
425 <td colspan='4'><br><br></td>
426 </tr>
427
428 <tr>
429 <td colspan='4'><b>$Lang::tr{'ids rules update'}</b></td>
430 </tr>
431
432 <tr>
433 <td colspan='4'><select name='RULES'>
434 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
435 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
436 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
437 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
438 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
439 </select>
440 </td>
441 </tr>
442
443 <tr>
444 <td colspan='4'>
445 <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>
446 <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>
447 </td>
448 </tr>
449
450 <tr>
451 <td colspan='4' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$idssettings{'OINKCODE'}'></td>
452 </tr>
453
454 <tr>
455 <td colspan='4' align='left'><br>
456 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>&nbsp;$Lang::tr{'updates installed'}: $rulesdate
457 </td>
458
459 </tr>
460 </table>
461
462 <br><br>
463
464 <table width='100%'>
465 <tr>
466 <td align='right'><input type='submit' name='SNORT' value='$Lang::tr{'save'}' /></td>
467 </tr>
468 </table>
469 </form>
470 END
471 ;
472
473 &Header::closebox();
474
475 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
476 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
477
478 # Output display table for rule files
479 print "<table width='100%'>\n";
480
481 # Local variable required for java script to show/hide
482 # rules of a rulefile.
483 my $rulesetcount = 1;
484
485 # Loop over each rule file
486 foreach my $rulefile (sort keys(%idsrules)) {
487 my $rulechecked = '';
488
489 # Check if rule file is enabled
490 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
491 $rulechecked = 'CHECKED';
492 }
493
494 # Table and rows for the rule files.
495 print"<tr>\n";
496 print"<td class='base' width='5%'>\n";
497 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
498 print"</td>\n";
499 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
500 print"<td class='base' width='5%' align='right'>\n";
501 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
502 print"</td>\n";
503 print"</tr>\n";
504
505 # Rows which will be hidden per default and will contain the single rules.
506 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
507 print"<td colspan='3'>\n";
508
509 # Local vars
510 my $lines;
511 my $rows;
512 my $col;
513
514 # New table for the single rules.
515 print "<table width='100%'>\n";
516
517 # Loop over rule file rules
518 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
519 # Local vars
520 my $ruledefchecked = '';
521
522 # Skip rulefile itself.
523 next if ($sid eq "Rulefile");
524
525 # If 2 rules have been displayed, start a new row
526 if (($lines % 2) == 0) {
527 print "</tr><tr>\n";
528
529 # Increase rows by once.
530 $rows++;
531 }
532
533 # Colour lines.
534 if ($rows % 2) {
535 $col="bgcolor='$color{'color20'}'";
536 } else {
537 $col="bgcolor='$color{'color22'}'";
538 }
539
540 # Set rule state
541 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
542 $ruledefchecked = 'CHECKED';
543 }
544
545 # Create rule checkbox and display rule description
546 print "<td class='base' width='5%' align='right' $col>\n";
547 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
548 print "</td>\n";
549 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
550
551 # Increment rule count
552 $lines++;
553 }
554
555 # If do not have a second rule for row, create empty cell
556 if (($lines % 2) != 0) {
557 print "<td class='base'></td>";
558 }
559
560 # Close display table
561 print "</tr></table></td></tr>";
562
563 # Finished whith the rule file, increase count.
564 $rulesetcount++;
565 }
566
567 # Close display table
568 print "</table>";
569
570 print <<END
571 <table width='100%'>
572 <tr>
573 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
574 &nbsp; <!-- space for future online help link -->
575 </td>
576 </tr>
577 </table>
578 </form>
579 END
580 ;
581 &Header::closebox();
582 &Header::closebigbox();
583 &Header::closepage();
584
585 #
586 ## A function to display a notice, to lock the webpage and
587 ## tell the user which action currently will be performed.
588 #
589 sub working_notice ($) {
590 my ($message) = @_;
591
592 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
593 &Header::openbigbox('100%', 'left', '', $errormessage);
594 &Header::openbox( 'Waiting', 1,);
595 print <<END;
596 <table>
597 <tr>
598 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
599 <td>$message</td>
600 </tr>
601 </table>
602 END
603 &Header::closebox();
604 &Header::closebigbox();
605 &Header::closepage();
606 }
607
608 #
609 ## A tiny function to perform a reload of the webpage after one second.
610 #
611 sub reload () {
612 print "<meta http-equiv='refresh' content='1'>\n";
613
614 # Stop the script.
615 exit;
616 }
617
618 #
619 ## Private function to read-in and parse rules of a given rulefile.
620 #
621 ## The given file will be read, parsed and all valid rules will be stored by ID,
622 ## message/description and it's state in the idsrules hash.
623 #
624 sub readrulesfile ($) {
625 my $rulefile = shift;
626
627 # Open rule file and read in contents
628 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
629
630 # Store file content in an array.
631 my @lines = <RULEFILE>;
632
633 # Close file.
634 close(RULEFILE);
635
636 # Loop over rule file contents
637 foreach my $line (@lines) {
638 # Remove whitespaces.
639 chomp $line;
640
641 # Skip blank lines.
642 next if ($line =~ /^\s*$/);
643
644 # Local vars.
645 my $sid;
646 my $msg;
647
648 # Gather rule sid and message from the ruleline.
649 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
650 $msg = $1;
651 $sid = $2;
652
653 # Check if a rule has been found.
654 if ($sid && $msg) {
655 # Add rule to the idsrules hash.
656 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
657
658 # Grab status of the rule. Check if ruleline starts with a "dash".
659 if ($line =~ /^\#/) {
660 # If yes, the rule is disabled.
661 $idsrules{$rulefile}{$sid}{'State'} = "off";
662 } else {
663 # Otherwise the rule is enabled.
664 $idsrules{$rulefile}{$sid}{'State'} = "on";
665 }
666 }
667 }
668 }
669 }