]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Fix HTML formated spaces.
[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 %rulessettings=();
38 my %rulesetsources = ();
39 my %cgiparams=();
40 my %checked=();
41 my %selected=();
42 my %ignored=();
43
44 # Read-in main settings, for language, theme and colors.
45 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
46 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
47
48 # Get the available network zones, based on the config type of the system and store
49 # the list of zones in an array.
50 my @network_zones = &IDS::get_available_network_zones();
51
52 my $errormessage;
53
54 # Create files if they does not exist yet.
55 &IDS::check_and_create_filelayout();
56
57 # Hash which contains the colour code of a network zone.
58 my %colourhash = (
59 'red' => $Header::colourred,
60 'green' => $Header::colourgreen,
61 'blue' => $Header::colourblue,
62 'orange' => $Header::colourorange
63 );
64
65 &Header::showhttpheaders();
66
67 #Get GUI values
68 &Header::getcgihash(\%cgiparams);
69
70 ## Add/edit an entry to the ignore file.
71 #
72 if (($cgiparams{'WHITELIST'} eq $Lang::tr{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr{'update'})) {
73
74 # Check if any input has been performed.
75 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
76
77 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
78 if ((!&General::validip($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General::validipandmask($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
79 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
80 }
81 } else {
82 $errormessage = "$Lang::tr{'guardian empty input'}";
83 }
84
85 # Go further if there was no error.
86 if ($errormessage eq '') {
87 my %ignored = ();
88 my $id;
89 my $status;
90
91 # Assign hash values.
92 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
93 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
94
95 # Read-in ignoredfile.
96 &General::readhasharray($IDS::ignored_file, \%ignored);
97
98 # Check if we should edit an existing entry and got an ID.
99 if (($cgiparams{'WHITELIST'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) {
100 # Assin the provided id.
101 $id = $cgiparams{'ID'};
102
103 # Undef the given ID.
104 undef($cgiparams{'ID'});
105
106 # Grab the configured status of the corresponding entry.
107 $status = $ignored{$id}[2];
108 } else {
109 # Each newly added entry automatically should be enabled.
110 $status = "enabled";
111
112 # Generate the ID for the new entry.
113 #
114 # Sort the keys by their ID and store them in an array.
115 my @keys = sort { $a <=> $b } keys %ignored;
116
117 # Reverse the key array.
118 my @reversed = reverse(@keys);
119
120 # Obtain the last used id.
121 my $last_id = @reversed[0];
122
123 # Increase the last id by one and use it as id for the new entry.
124 $id = ++$last_id;
125 }
126
127 # Add/Modify the entry to/in the ignored hash.
128 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
129
130 # Write the changed ignored hash to the ignored file.
131 &General::writehasharray($IDS::ignored_file, \%ignored);
132
133 # Regenerate the ignore file.
134 &IDS::generate_ignore_file();
135 }
136
137 # Check if the IDS is running.
138 if(&IDS::ids_is_running()) {
139 # Call suricatactrl to perform a reload.
140 &IDS::call_suricatactrl("reload");
141 }
142
143 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
144 #
145
146 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'toggle enable disable'}) {
147 my %ignored = ();
148
149 # Only go further, if an ID has been passed.
150 if ($cgiparams{'ID'}) {
151 # Assign the given ID.
152 my $id = $cgiparams{'ID'};
153
154 # Undef the given ID.
155 undef($cgiparams{'ID'});
156
157 # Read-in ignoredfile.
158 &General::readhasharray($IDS::ignored_file, \%ignored);
159
160 # Grab the configured status of the corresponding entry.
161 my $status = $ignored{$id}[2];
162
163 # Switch the status.
164 if ($status eq "disabled") {
165 $status = "enabled";
166 } else {
167 $status = "disabled";
168 }
169
170 # Modify the status of the existing entry.
171 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
172
173 # Write the changed ignored hash to the ignored file.
174 &General::writehasharray($IDS::ignored_file, \%ignored);
175
176 # Regenerate the ignore file.
177 &IDS::generate_ignore_file();
178
179 # Check if the IDS is running.
180 if(&IDS::ids_is_running()) {
181 # Call suricatactrl to perform a reload.
182 &IDS::call_suricatactrl("reload");
183 }
184 }
185
186 ## Remove entry from ignore list.
187 #
188 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'remove'}) {
189 my %ignored = ();
190
191 # Read-in ignoredfile.
192 &General::readhasharray($IDS::ignored_file, \%ignored);
193
194 # Drop entry from the hash.
195 delete($ignored{$cgiparams{'ID'}});
196
197 # Undef the given ID.
198 undef($cgiparams{'ID'});
199
200 # Write the changed ignored hash to the ignored file.
201 &General::writehasharray($IDS::ignored_file, \%ignored);
202
203 # Regenerate the ignore file.
204 &IDS::generate_ignore_file();
205
206 # Check if the IDS is running.
207 if(&IDS::ids_is_running()) {
208 # Call suricatactrl to perform a reload.
209 &IDS::call_suricatactrl("reload");
210 }
211 }
212
213 # Check if any error has been stored.
214 if (-e $IDS::storederrorfile) {
215 # Open file to read in the stored error message.
216 open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
217
218 # Read the stored error message.
219 $errormessage = <FILE>;
220
221 # Close file.
222 close (FILE);
223
224 # Delete the file, which is now not longer required.
225 unlink($IDS::storederrorfile);
226 }
227
228 ## Grab all available snort rules and store them in the idsrules hash.
229 #
230 # Open snort rules directory and do a directory listing.
231 opendir(DIR, $IDS::rulespath) or die $!;
232 # Loop through the direcory.
233 while (my $file = readdir(DIR)) {
234
235 # We only want files.
236 next unless (-f "$IDS::rulespath/$file");
237
238 # Ignore empty files.
239 next if (-z "$IDS::rulespath/$file");
240
241 # Use a regular expression to find files ending in .rules
242 next unless ($file =~ m/\.rules$/);
243
244 # Ignore files which are not read-able.
245 next unless (-R "$IDS::rulespath/$file");
246
247 # Skip whitelist rules file.
248 next if( $file eq "whitelist.rules");
249
250 # Call subfunction to read-in rulefile and add rules to
251 # the idsrules hash.
252 &readrulesfile("$file");
253 }
254
255 closedir(DIR);
256
257 # Gather used rulefiles.
258 #
259 # Check if the file for activated rulefiles is not empty.
260 if(-f $IDS::used_rulefiles_file) {
261 # Open the file for used rulefile and read-in content.
262 open(FILE, $IDS::used_rulefiles_file) or die "Could not open $IDS::used_rulefiles_file. $!\n";
263
264 # Read-in content.
265 my @lines = <FILE>;
266
267 # Close file.
268 close(FILE);
269
270 # Loop through the array.
271 foreach my $line (@lines) {
272 # Remove newlines.
273 chomp($line);
274
275 # Skip comments.
276 next if ($line =~ /\#/);
277
278 # Skip blank lines.
279 next if ($line =~ /^\s*$/);
280
281 # Gather rule sid and message from the ruleline.
282 if ($line =~ /.*- (.*)/) {
283 my $rulefile = $1;
284
285 # Check if the current rulefile exists in the %idsrules hash.
286 # If not, the file probably does not exist anymore or contains
287 # no rules.
288 if($idsrules{$rulefile}) {
289 # Add the rulefile state to the %idsrules hash.
290 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
291 }
292 }
293 }
294 }
295
296 # Save ruleset configuration.
297 if ($cgiparams{'RULESET'} eq $Lang::tr{'save'}) {
298 my %oldsettings;
299
300 # Read-in current (old) IDS settings.
301 &General::readhash("$IDS::rules_settings_file", \%oldsettings);
302
303 # Prevent form name from been stored in conf file.
304 delete $cgiparams{'RULESET'};
305
306 # Check if an oinkcode has been provided.
307 if ($cgiparams{'OINKCODE'}) {
308 # Check if the oinkcode contains unallowed chars.
309 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
310 $errormessage = $Lang::tr{'invalid input for oink code'};
311 }
312 }
313
314 # Go on if there are no error messages.
315 if (!$errormessage) {
316 # Store settings into settings file.
317 &General::writehash("$IDS::rules_settings_file", \%cgiparams);
318 }
319
320 # Check if the the automatic rule update hass been touched.
321 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldsettings{'AUTOUPDATE_INTERVAL'}) {
322 # Call suricatactrl to set the new interval.
323 &IDS::call_suricatactrl("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
324 }
325
326 # Check if a ruleset is present - if not or the source has been changed download it.
327 if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) {
328 # Check if the red device is active.
329 unless (-e "${General::swroot}/red/active") {
330 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
331 }
332
333 # Check if enought free disk space is availabe.
334 if(&IDS::checkdiskspace()) {
335 $errormessage = "$Lang::tr{'not enough disk space'}";
336 }
337
338 # Check if any errors happend.
339 unless ($errormessage) {
340 # Lock the webpage and print notice about downloading
341 # a new ruleset.
342 &working_notice("$Lang::tr{'snort working'}");
343
344 # Call subfunction to download the ruleset.
345 if(&IDS::downloadruleset()) {
346 $errormessage = $Lang::tr{'could not download latest updates'};
347
348 # Call function to store the errormessage.
349 &IDS::_store_error_message($errormessage);
350 } else {
351 # Call subfunction to launch oinkmaster.
352 &IDS::oinkmaster();
353 }
354
355 # Check if the IDS is running.
356 if(&IDS::ids_is_running()) {
357 # Call suricatactrl to stop the IDS - because of the changed
358 # ruleset - the use has to configure it before suricata can be
359 # used again.
360 &IDS::call_suricatactrl("stop");
361 }
362
363 # Perform a reload of the page.
364 &reload();
365 }
366 }
367
368 # Save ruleset.
369 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
370 # Arrays to store which rulefiles have been enabled and will be used.
371 my @enabled_rulefiles;
372
373 # Hash to store the user-enabled and disabled sids.
374 my %enabled_disabled_sids;
375
376 # Loop through the hash of idsrules.
377 foreach my $rulefile(keys %idsrules) {
378 # Check if the rulefile is enabled.
379 if ($cgiparams{$rulefile} eq "on") {
380 # Add rulefile to the array of enabled rulefiles.
381 push(@enabled_rulefiles, $rulefile);
382
383 # Drop item from cgiparams hash.
384 delete $cgiparams{$rulefile};
385 }
386 }
387
388 # Read-in the files for enabled/disabled sids.
389 # This will be done by calling the read_enabled_disabled_sids_file function two times
390 # and merge the returned hashes together into the enabled_disabled_sids hash.
391 %enabled_disabled_sids = (
392 &read_enabled_disabled_sids_file($IDS::disabled_sids_file),
393 &read_enabled_disabled_sids_file($IDS::enabled_sids_file));
394
395 # Loop through the hash of idsrules.
396 foreach my $rulefile (keys %idsrules) {
397 # Loop through the single rules of the rulefile.
398 foreach my $sid (keys %{$idsrules{$rulefile}}) {
399 # Skip the current sid if it is not numeric.
400 next unless ($sid =~ /\d+/ );
401
402 # Check if there exists a key in the cgiparams hash for this sid.
403 if (exists($cgiparams{$sid})) {
404 # Look if the rule is disabled.
405 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
406 # Check if the state has been set to 'on'.
407 if ($cgiparams{$sid} eq "on") {
408 # Add/Modify the sid to/in the enabled_disabled_sids hash.
409 $enabled_disabled_sids{$sid} = "enabled";
410
411 # Drop item from cgiparams hash.
412 delete $cgiparams{$rulefile}{$sid};
413 }
414 }
415 } else {
416 # Look if the rule is enabled.
417 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
418 # Check if the state is 'on' and should be disabled.
419 # In this case there is no entry
420 # for the sid in the cgiparams hash.
421 # Add/Modify it to/in the enabled_disabled_sids hash.
422 $enabled_disabled_sids{$sid} = "disabled";
423
424 # Drop item from cgiparams hash.
425 delete $cgiparams{$rulefile}{$sid};
426 }
427 }
428 }
429 }
430
431 # Open enabled sid's file for writing.
432 open(ENABLED_FILE, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
433
434 # Open disabled sid's file for writing.
435 open(DISABLED_FILE, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
436
437 # Write header to the files.
438 print ENABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
439 print DISABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
440
441 # Check if the hash for enabled/disabled files contains any entries.
442 if (%enabled_disabled_sids) {
443 # Loop through the hash.
444 foreach my $sid (keys %enabled_disabled_sids) {
445 # Check if the sid is enabled.
446 if ($enabled_disabled_sids{$sid} eq "enabled") {
447 # Print the sid to the enabled_sids file.
448 print ENABLED_FILE "enablesid $sid\n";
449 # Check if the sid is disabled.
450 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
451 # Print the sid to the disabled_sids file.
452 print DISABLED_FILE "disablesid $sid\n";
453 # Something strange happende - skip the current sid.
454 } else {
455 next;
456 }
457 }
458 }
459
460 # Close file for enabled_sids after writing.
461 close(ENABLED_FILE);
462
463 # Close file for disabled_sids after writing.
464 close(DISABLED_FILE);
465
466 # Call function to generate and write the used rulefiles file.
467 &IDS::write_used_rulefiles_file(@enabled_rulefiles);
468
469 # Lock the webpage and print message.
470 &working_notice("$Lang::tr{'snort working'}");
471
472 # Call oinkmaster to alter the ruleset.
473 &IDS::oinkmaster();
474
475 # Check if the IDS is running.
476 if(&IDS::ids_is_running()) {
477 # Call suricatactrl to perform a reload.
478 &IDS::call_suricatactrl("reload");
479 }
480
481 # Reload page.
482 &reload();
483
484 # Download new ruleset.
485 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update ruleset'}) {
486 # Check if the red device is active.
487 unless (-e "${General::swroot}/red/active") {
488 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
489 }
490
491 # Check if enought free disk space is availabe.
492 if(&IDS::checkdiskspace()) {
493 $errormessage = "$Lang::tr{'not enough disk space'}";
494 }
495
496 # Check if any errors happend.
497 unless ($errormessage) {
498 # Lock the webpage and print notice about downloading
499 # a new ruleset.
500 &working_notice("$Lang::tr{'snort working'}");
501
502 # Call subfunction to download the ruleset.
503 if(&IDS::downloadruleset()) {
504 $errormessage = $Lang::tr{'could not download latest updates'};
505
506 # Call function to store the errormessage.
507 &IDS::_store_error_message($errormessage);
508
509 # Preform a reload of the page.
510 &reload();
511 } else {
512 # Call subfunction to launch oinkmaster.
513 &IDS::oinkmaster();
514
515 # Check if the IDS is running.
516 if(&IDS::ids_is_running()) {
517 # Call suricatactrl to perform a reload.
518 &IDS::call_suricatactrl("reload");
519 }
520
521 # Perform a reload of the page.
522 &reload();
523 }
524 }
525 # Save snort settings.
526 } elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
527 my %oldidssettings;
528 my $reload_page;
529 my $monitored_zones = 0;
530
531 # Read-in current (old) IDS settings.
532 &General::readhash("$IDS::ids_settings_file", \%oldidssettings);
533
534 # Prevent form name from been stored in conf file.
535 delete $cgiparams{'IDS'};
536
537 # Check if the IDS should be enabled.
538 if ($cgiparams{'ENABLE_IDS'} eq "on") {
539 # Check if any ruleset is available. Otherwise abort and display an error.
540 unless(%idsrules) {
541 $errormessage = $Lang::tr{'ids no ruleset available'};
542 }
543
544 # Loop through the array of available interfaces.
545 foreach my $zone (@network_zones) {
546 # Convert interface name into upper case.
547 my $zone_upper = uc($zone);
548
549 # Check if the IDS is enabled for this interaces.
550 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
551 # Increase count.
552 $monitored_zones++;
553 }
554 }
555
556 # Check if at least one zone should be monitored, or show an error.
557 unless ($monitored_zones >= 1) {
558 $errormessage = $Lang::tr{'ids no network zone'};
559 }
560 }
561
562 # Go on if there are no error messages.
563 if (!$errormessage) {
564 # Store settings into settings file.
565 &General::writehash("$IDS::ids_settings_file", \%cgiparams);
566 }
567
568 # Generate file to store the home net.
569 &IDS::generate_home_net_file();
570
571 # Temporary variable to set the ruleaction.
572 # Default is "drop" to use suricata as IPS.
573 my $ruleaction="drop";
574
575 # Check if the traffic only should be monitored.
576 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} eq 'on') {
577 # Switch the ruleaction to "alert".
578 # Suricata acts as an IDS only.
579 $ruleaction="alert";
580 }
581
582 # Write the modify sid's file and pass the taken ruleaction.
583 &IDS::write_modify_sids_file($ruleaction);
584
585 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
586 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
587 # Check if a ruleset exists.
588 if (%idsrules) {
589 # Lock the webpage and print message.
590 &working_notice("$Lang::tr{'snort working'}");
591
592 # Call oinkmaster to alter the ruleset.
593 &IDS::oinkmaster();
594
595 # Set reload_page to "True".
596 $reload_page="True";
597 }
598 }
599
600 # Check if the IDS currently is running.
601 if(&IDS::ids_is_running()) {
602 # Check if ENABLE_IDS is set to on.
603 if($cgiparams{'ENABLE_IDS'} eq "on") {
604 # Call suricatactrl to perform a reload of suricata.
605 &IDS::call_suricatactrl("reload");
606 } else {
607 # Call suricatactrl to stop suricata.
608 &IDS::call_suricatactrl("stop");
609 }
610 } else {
611 # Call suricatactrl to start suricata.
612 &IDS::call_suricatactrl("start");
613 }
614
615 # Check if the page should be reloaded.
616 if ($reload_page) {
617 # Perform a reload of the page.
618 &reload();
619 }
620 }
621
622 # Read-in idssettings and rulesetsettings
623 &General::readhash("$IDS::ids_settings_file", \%idssettings);
624 &General::readhash("$IDS::rules_settings_file", \%rulessettings);
625
626 # If no autoupdate intervall has been configured yet, set default value.
627 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
628 # Set default to "weekly".
629 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
630 }
631
632 # Read-in ignored hosts.
633 &General::readhasharray("$IDS::settingsdir/ignored", \%ignored);
634
635 $checked{'ENABLE_IDS'}{'off'} = '';
636 $checked{'ENABLE_IDS'}{'on'} = '';
637 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
638 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
639 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
640 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
641 $selected{'RULES'}{'nothing'} = '';
642 $selected{'RULES'}{'community'} = '';
643 $selected{'RULES'}{'emerging'} = '';
644 $selected{'RULES'}{'registered'} = '';
645 $selected{'RULES'}{'subscripted'} = '';
646 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
647 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
648 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
649 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
650 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
651
652 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
653
654 ### Java Script ###
655 print"<script>\n";
656
657 # Java script variable declaration for show and hide.
658 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
659 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
660
661 print <<END
662 // JQuery function to show/hide the text input field for
663 // Oinkcode/Subscription code.
664 \$(function() {
665 \$('#RULES').change(function(){
666 if(\$('#RULES').val() == 'registered') {
667 \$('#code').show();
668 } else if(\$('#RULES').val() == 'subscripted') {
669 \$('#code').show();
670 } else if(\$('#RULES').val() == 'emerging_pro') {
671 \$('#code').show();
672 } else {
673 \$('#code').hide();
674 }
675 });
676 });
677
678 // Tiny java script function to show/hide the rules
679 // of a given category.
680 function showhide(tblname) {
681 \$("#" + tblname).toggle();
682
683 // Get current content of the span element.
684 var content = document.getElementById("span_" + tblname);
685
686 if (content.innerHTML === show) {
687 content.innerHTML = hide;
688 } else {
689 content.innerHTML = show;
690 }
691 }
692 </script>
693 END
694 ;
695
696 &Header::openbigbox('100%', 'left', '', $errormessage);
697
698 if ($errormessage) {
699 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
700 print "<class name='base'>$errormessage\n";
701 print "&nbsp;</class>\n";
702 &Header::closebox();
703 }
704
705 # Draw current state of the IDS
706 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
707
708 # Check if the IDS is running and obtain the process-id.
709 my $pid = &IDS::ids_is_running();
710
711 # Display some useful information, if suricata daemon is running.
712 if ($pid) {
713 # Gather used memory.
714 my $memory = &get_memory_usage($pid);
715
716 print <<END;
717 <table width='95%' cellspacing='0' class='tbl'>
718 <tr>
719 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
720 </tr>
721
722 <tr>
723 <td class='base'>$Lang::tr{'guardian daemon'}</td>
724 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
725 </tr>
726
727 <tr>
728 <td class='base'></td>
729 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
730 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
731 </tr>
732
733 <tr>
734 <td class='base'></td>
735 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
736 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
737 </tr>
738 </table>
739 END
740 } else {
741 # Otherwise display a hint that the service is not launched.
742 print <<END;
743 <table width='95%' cellspacing='0' class='tbl'>
744 <tr>
745 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
746 </tr>
747
748 <tr>
749 <td class='base'>$Lang::tr{'guardian daemon'}</td>
750 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
751 </tr>
752 </table>
753 END
754 }
755
756 my $rulesdate;
757
758 # Check if a ruleset allready has been downloaded.
759 if ( -f "$IDS::rulestarball"){
760 # Call stat on the filename to obtain detailed information.
761 my @Info = stat("$IDS::rulestarball");
762
763 # Grab details about the creation time.
764 $rulesdate = localtime($Info[9]);
765 }
766
767 # Only show this area, if a ruleset is present.
768 if (%idsrules) {
769
770 print <<END
771
772 <br><br><h2>$Lang::tr{'settings'}</h2>
773
774 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
775 <table width='100%' border='0'>
776 <tr>
777 <td class='base' colspan='2'>
778 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>&nbsp;$Lang::tr{'ids enable'}
779 </td>
780
781 <td class='base' colspan='2'>
782 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>&nbsp;$Lang::tr{'ids monitor traffic only'}
783 </td>
784 </tr>
785
786 <tr>
787 <td><br><br></td>
788 <td><br><br></td>
789 <td><br><br></td>
790 <td><br><br></td>
791 </tr>
792
793 <tr>
794 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
795 </tr>
796
797 <tr>
798 END
799 ;
800
801 # Loop through the array of available networks and print config options.
802 foreach my $zone (@network_zones) {
803 my $checked_input;
804 my $checked_forward;
805
806 # Convert current zone name to upper case.
807 my $zone_upper = uc($zone);
808
809 # Set zone name.
810 my $zone_name = $zone;
811
812 # Dirty hack to get the correct language string for the red zone.
813 if ($zone eq "red") {
814 $zone_name = "red1";
815 }
816
817 # Grab checkbox status from settings hash.
818 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
819 $checked_input = "checked = 'checked'";
820 }
821
822 print "<td class='base' width='25%'>\n";
823 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
824 print "&nbsp;$Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
825 print "</td>\n";
826 }
827
828 print <<END
829 </tr>
830 </table>
831
832 <br><br>
833
834 <table width='100%'>
835 <tr>
836 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
837 </tr>
838 </table>
839 </form>
840 END
841 ;
842
843 }
844
845 &Header::closebox();
846
847 # Draw elements for ruleset configuration.
848 &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'});
849
850 print <<END
851 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
852 <table width='100%' border='0'>
853 <tr>
854 <td><b>$Lang::tr{'ids rules update'}</b></td>
855 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
856 </tr>
857
858 <tr>
859 <td><select name='RULES' id='RULES'>
860 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
861 <option value='emerging_pro' $selected{'RULES'}{'emerging_pro'} >$Lang::tr{'emerging pro rules'}</option>
862 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
863 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
864 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
865 </select>
866 </td>
867
868 <td>
869 <select name='AUTOUPDATE_INTERVAL'>
870 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
871 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
872 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
873 </select>
874 </td>
875 </tr>
876
877 <tr>
878 <td colspan='2'><br><br></td>
879 </tr>
880
881 <tr style='display:none' id='code'>
882 <td colspan='2'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
883 </tr>
884
885 <tr>
886 <td>&nbsp;</td>
887
888 <td align='right'>
889 END
890 ;
891 # Check if a ruleset has been downloaded yet.
892 if (%idsrules) {
893 # Display button to update the ruleset.
894 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
895 }
896 print <<END;
897 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
898 </td>
899
900 </tr>
901 </table>
902 </form>
903 END
904 ;
905
906 &Header::closebox();
907
908 #
909 # Whitelist / Ignorelist
910 #
911 &Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'});
912
913 print <<END;
914 <table width='100%'>
915 <tr>
916 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
917 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
918 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
919 </tr>
920 END
921 # Check if some hosts have been added to be ignored.
922 if (keys (%ignored)) {
923 my $col = "";
924
925 # Loop through all entries of the hash.
926 while( (my $key) = each %ignored) {
927 # Assign data array positions to some nice variable names.
928 my $address = $ignored{$key}[0];
929 my $remark = $ignored{$key}[1];
930 my $status = $ignored{$key}[2];
931
932 # Check if the key (id) number is even or not.
933 if ($cgiparams{'ID'} eq $key) {
934 $col="bgcolor='${Header::colouryellow}'";
935 } elsif ($key % 2) {
936 $col="bgcolor='$color{'color22'}'";
937 } else {
938 $col="bgcolor='$color{'color20'}'";
939 }
940
941 # Choose icon for the checkbox.
942 my $gif;
943 my $gdesc;
944
945 # Check if the status is enabled and select the correct image and description.
946 if ($status eq 'enabled' ) {
947 $gif = 'on.gif';
948 $gdesc = $Lang::tr{'click to disable'};
949 } else {
950 $gif = 'off.gif';
951 $gdesc = $Lang::tr{'click to enable'};
952 }
953
954 print <<END;
955 <tr>
956 <td width='20%' class='base' $col>$address</td>
957 <td width='65%' class='base' $col>$remark</td>
958
959 <td align='center' $col>
960 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
961 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
962 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
963 <input type='hidden' name='ID' value='$key' />
964 </form>
965 </td>
966
967 <td align='center' $col>
968 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
969 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
970 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
971 <input type='hidden' name='ID' value='$key' />
972 </form>
973 </td>
974
975 <td align='center' $col>
976 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
977 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
978 <input type='hidden' name='ID' value='$key'>
979 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
980 </form>
981 </td>
982 </tr>
983 END
984 }
985 } else {
986 # Print notice that currently no hosts are ignored.
987 print "<tr>\n";
988 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
989 print "</tr>\n";
990 }
991
992 print "</table>\n";
993
994 # Section to add new elements or edit existing ones.
995 print <<END;
996 <br>
997 <hr>
998 <br>
999
1000 <div align='center'>
1001 <table width='100%'>
1002 END
1003
1004 # Assign correct headline and button text.
1005 my $buttontext;
1006 my $entry_address;
1007 my $entry_remark;
1008
1009 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1010 if ($cgiparams{'ID'} ne '') {
1011 $buttontext = $Lang::tr{'update'};
1012 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1013
1014 # Grab address and remark for the given key.
1015 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1016 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1017 } else {
1018 $buttontext = $Lang::tr{'add'};
1019 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1020 }
1021
1022 print <<END;
1023 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1024 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1025 <tr>
1026 <td width='30%'>$Lang::tr{'ip address'}: </td>
1027 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1028
1029 <td width='30%'>$Lang::tr{'remark'}: </td>
1030 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1031 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1032 </tr>
1033 </form>
1034 </table>
1035 </div>
1036 END
1037
1038 &Header::closebox();
1039
1040 # Only show the section for configuring the ruleset if one is present.
1041 if (%idsrules) {
1042 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
1043
1044 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1045
1046 # Output display table for rule files
1047 print "<table width='100%'>\n";
1048
1049 # Loop over each rule file
1050 foreach my $rulefile (sort keys(%idsrules)) {
1051 my $rulechecked = '';
1052
1053 # Check if rule file is enabled
1054 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1055 $rulechecked = 'CHECKED';
1056 }
1057
1058 # Convert rulefile name into category name.
1059 my $categoryname = &_rulefile_to_category($rulefile);
1060
1061 # Table and rows for the rule files.
1062 print"<tr>\n";
1063 print"<td class='base' width='5%'>\n";
1064 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1065 print"</td>\n";
1066 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1067 print"<td class='base' width='5%' align='right'>\n";
1068 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1069 print"</td>\n";
1070 print"</tr>\n";
1071
1072 # Rows which will be hidden per default and will contain the single rules.
1073 print"<tr style='display:none' id='$categoryname'>\n";
1074 print"<td colspan='3'>\n";
1075
1076 # Local vars
1077 my $lines;
1078 my $rows;
1079 my $col;
1080
1081 # New table for the single rules.
1082 print "<table width='100%'>\n";
1083
1084 # Loop over rule file rules
1085 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1086 # Local vars
1087 my $ruledefchecked = '';
1088
1089 # Skip rulefile itself.
1090 next if ($sid eq "Rulefile");
1091
1092 # If 2 rules have been displayed, start a new row
1093 if (($lines % 2) == 0) {
1094 print "</tr><tr>\n";
1095
1096 # Increase rows by once.
1097 $rows++;
1098 }
1099
1100 # Colour lines.
1101 if ($rows % 2) {
1102 $col="bgcolor='$color{'color20'}'";
1103 } else {
1104 $col="bgcolor='$color{'color22'}'";
1105 }
1106
1107 # Set rule state
1108 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1109 $ruledefchecked = 'CHECKED';
1110 }
1111
1112 # Create rule checkbox and display rule description
1113 print "<td class='base' width='5%' align='right' $col>\n";
1114 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1115 print "</td>\n";
1116 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1117
1118 # Increment rule count
1119 $lines++;
1120 }
1121
1122 # If do not have a second rule for row, create empty cell
1123 if (($lines % 2) != 0) {
1124 print "<td class='base'></td>";
1125 }
1126
1127 # Close display table
1128 print "</tr></table></td></tr>";
1129 }
1130
1131 # Close display table
1132 print "</table>";
1133
1134 print <<END
1135 <table width='100%'>
1136 <tr>
1137 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
1138 &nbsp; <!-- space for future online help link -->
1139 </td>
1140 </tr>
1141 </table>
1142 </form>
1143 END
1144 ;
1145 &Header::closebox();
1146 }
1147
1148 &Header::closebigbox();
1149 &Header::closepage();
1150
1151 #
1152 ## A function to display a notice, to lock the webpage and
1153 ## tell the user which action currently will be performed.
1154 #
1155 sub working_notice ($) {
1156 my ($message) = @_;
1157
1158 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
1159 &Header::openbigbox('100%', 'left', '', $errormessage);
1160 &Header::openbox( 'Waiting', 1,);
1161 print <<END;
1162 <table>
1163 <tr>
1164 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1165 <td>$message</td>
1166 </tr>
1167 </table>
1168 END
1169 &Header::closebox();
1170 &Header::closebigbox();
1171 &Header::closepage();
1172 }
1173
1174 #
1175 ## A tiny function to perform a reload of the webpage after one second.
1176 #
1177 sub reload () {
1178 print "<meta http-equiv='refresh' content='1'>\n";
1179
1180 # Stop the script.
1181 exit;
1182 }
1183
1184 #
1185 ## Private function to read-in and parse rules of a given rulefile.
1186 #
1187 ## The given file will be read, parsed and all valid rules will be stored by ID,
1188 ## message/description and it's state in the idsrules hash.
1189 #
1190 sub readrulesfile ($) {
1191 my $rulefile = shift;
1192
1193 # Open rule file and read in contents
1194 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1195
1196 # Store file content in an array.
1197 my @lines = <RULEFILE>;
1198
1199 # Close file.
1200 close(RULEFILE);
1201
1202 # Loop over rule file contents
1203 foreach my $line (@lines) {
1204 # Remove whitespaces.
1205 chomp $line;
1206
1207 # Skip blank lines.
1208 next if ($line =~ /^\s*$/);
1209
1210 # Local vars.
1211 my $sid;
1212 my $msg;
1213
1214 # Gather rule sid and message from the ruleline.
1215 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1216 $msg = $1;
1217 $sid = $2;
1218
1219 # Check if a rule has been found.
1220 if ($sid && $msg) {
1221 # Add rule to the idsrules hash.
1222 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1223
1224 # Grab status of the rule. Check if ruleline starts with a "dash".
1225 if ($line =~ /^\#/) {
1226 # If yes, the rule is disabled.
1227 $idsrules{$rulefile}{$sid}{'State'} = "off";
1228 } else {
1229 # Otherwise the rule is enabled.
1230 $idsrules{$rulefile}{$sid}{'State'} = "on";
1231 }
1232 }
1233 }
1234 }
1235 }
1236
1237 #
1238 ## Function to get the used memory of a given process-id.
1239 #
1240 sub get_memory_usage($) {
1241 my ($pid) = @_;
1242
1243 my $memory = 0;
1244
1245 # Try to open the status file for the given process-id on the pseudo
1246 # file system proc.
1247 if (open(FILE, "/proc/$pid/status")) {
1248 # Loop through the entire file.
1249 while (<FILE>) {
1250 # Splitt current line content and store them into variables.
1251 my ($key, $value) = split(":", $_, 2);
1252
1253 # Check if the current key is the one which contains the memory usage.
1254 # The wanted one is VmRSS which contains the Real-memory (resident set)
1255 # of the entire process.
1256 if ($key eq "VmRSS") {
1257 # Found the memory usage add it to the memory variable.
1258 $memory += $value;
1259
1260 # Break the loop.
1261 last;
1262 }
1263 }
1264
1265 # Close file handle.
1266 close(FILE);
1267
1268 # Return memory usage.
1269 return $memory;
1270 }
1271
1272 # If the file could not be open, return nothing.
1273 return;
1274 }
1275
1276 #
1277 ## Function to read-in the given enabled or disables sids file.
1278 #
1279 sub read_enabled_disabled_sids_file($) {
1280 my ($file) = @_;
1281
1282 # Temporary hash to store the sids and their state. It will be
1283 # returned at the end of this function.
1284 my %temphash;
1285
1286 # Open the given filename.
1287 open(FILE, "$file") or die "Could not open $file. $!\n";
1288
1289 # Loop through the file.
1290 while(<FILE>) {
1291 # Remove newlines.
1292 chomp $_;
1293
1294 # Skip blank lines.
1295 next if ($_ =~ /^\s*$/);
1296
1297 # Skip coments.
1298 next if ($_ =~ /^\#/);
1299
1300 # Splitt line into sid and state part.
1301 my ($state, $sid) = split(" ", $_);
1302
1303 # Skip line if the sid is not numeric.
1304 next unless ($sid =~ /\d+/ );
1305
1306 # Check if the sid was enabled.
1307 if ($state eq "enablesid") {
1308 # Add the sid and its state as enabled to the temporary hash.
1309 $temphash{$sid} = "enabled";
1310 # Check if the sid was disabled.
1311 } elsif ($state eq "disablesid") {
1312 # Add the sid and its state as disabled to the temporary hash.
1313 $temphash{$sid} = "disabled";
1314 # Invalid state - skip the current sid and state.
1315 } else {
1316 next;
1317 }
1318 }
1319
1320 # Close filehandle.
1321 close(FILE);
1322
1323 # Return the hash.
1324 return %temphash;
1325 }
1326
1327 #
1328 ## Private function to convert a given rulefile to a category name.
1329 ## ( No file extension anymore and if the name contained a dot, it
1330 ## would be replaced by a underline sign.)
1331 #
1332 sub _rulefile_to_category($) {
1333 my ($filename) = @_;
1334
1335 # Splitt the filename into single chunks and store them in a
1336 # temorary array.
1337 my @parts = split(/\./, $filename);
1338
1339 # Return / Remove last element of the temporary array.
1340 # This removes the file extension.
1341 pop @parts;
1342
1343 # Join together the single elements of the temporary array.
1344 # If these are more than one, use a "underline" for joining.
1345 my $category = join '_', @parts;
1346
1347 # Return the converted filename.
1348 return $category;
1349 }