2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2020 IPFire Team <info@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
24 # enable only the following on debugging purpose
26 #use CGI::Carp 'fatalsToBrowser';
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";
34 my %mainsettings = ();
38 my %rulesetsources = ();
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);
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
();
52 # Check if openvpn is started and add it to the array of network zones.
53 if ( -e
"/var/run/openvpn.pid") {
54 push(@network_zones, "ovpn");
59 # Create files if they does not exist yet.
60 &IDS
::check_and_create_filelayout
();
62 # Hash which contains the colour code of a network zone.
64 'red' => $Header::colourred
,
65 'green' => $Header::colourgreen
,
66 'blue' => $Header::colourblue
,
67 'orange' => $Header::colourorange
,
68 'ovpn' => $Header::colourovpn
71 &Header
::showhttpheaders
();
74 &Header
::getcgihash
(\
%cgiparams);
76 ## Add/edit an entry to the ignore file.
78 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'})) {
80 # Check if any input has been performed.
81 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
83 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
84 if ((!&General
::validip
($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
85 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
88 $errormessage = "$Lang::tr{'guardian empty input'}";
91 # Go further if there was no error.
92 if ($errormessage eq '') {
98 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
99 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
101 # Read-in ignoredfile.
102 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
104 # Check if we should edit an existing entry and got an ID.
105 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
106 # Assin the provided id.
107 $id = $cgiparams{'ID'};
109 # Undef the given ID.
110 undef($cgiparams{'ID'});
112 # Grab the configured status of the corresponding entry.
113 $status = $ignored{$id}[2];
115 # Each newly added entry automatically should be enabled.
118 # Generate the ID for the new entry.
120 # Sort the keys by their ID and store them in an array.
121 my @keys = sort { $a <=> $b } keys %ignored;
123 # Reverse the key array.
124 my @reversed = reverse(@keys);
126 # Obtain the last used id.
127 my $last_id = @reversed[0];
129 # Increase the last id by one and use it as id for the new entry.
133 # Add/Modify the entry to/in the ignored hash.
134 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
136 # Write the changed ignored hash to the ignored file.
137 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
139 # Regenerate the ignore file.
140 &IDS
::generate_ignore_file
();
143 # Check if the IDS is running.
144 if(&IDS
::ids_is_running
()) {
145 # Call suricatactrl to perform a reload.
146 &IDS
::call_suricatactrl
("reload");
149 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
152 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'toggle enable disable'}) {
155 # Only go further, if an ID has been passed.
156 if ($cgiparams{'ID'}) {
157 # Assign the given ID.
158 my $id = $cgiparams{'ID'};
160 # Undef the given ID.
161 undef($cgiparams{'ID'});
163 # Read-in ignoredfile.
164 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
166 # Grab the configured status of the corresponding entry.
167 my $status = $ignored{$id}[2];
170 if ($status eq "disabled") {
173 $status = "disabled";
176 # Modify the status of the existing entry.
177 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
179 # Write the changed ignored hash to the ignored file.
180 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
182 # Regenerate the ignore file.
183 &IDS
::generate_ignore_file
();
185 # Check if the IDS is running.
186 if(&IDS
::ids_is_running
()) {
187 # Call suricatactrl to perform a reload.
188 &IDS
::call_suricatactrl
("reload");
192 ## Remove entry from ignore list.
194 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'remove'}) {
197 # Read-in ignoredfile.
198 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
200 # Drop entry from the hash.
201 delete($ignored{$cgiparams{'ID'}});
203 # Undef the given ID.
204 undef($cgiparams{'ID'});
206 # Write the changed ignored hash to the ignored file.
207 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
209 # Regenerate the ignore file.
210 &IDS
::generate_ignore_file
();
212 # Check if the IDS is running.
213 if(&IDS
::ids_is_running
()) {
214 # Call suricatactrl to perform a reload.
215 &IDS
::call_suricatactrl
("reload");
219 # Check if the page is locked, in this case, the ids_page_lock_file exists.
220 if (-e
$IDS::ids_page_lock_file
) {
221 # Lock the webpage and print notice about autoupgrade of the ruleset
223 &working_notice
("$Lang::tr{'ids ruleset autoupdate in progress'}");
225 # Loop and check if the file still exists.
226 while(-e
$IDS::ids_page_lock_file
) {
227 # Sleep for a second and re-check.
231 # Page has been unlocked, perform a reload.
235 # Check if any error has been stored.
236 if (-e
$IDS::storederrorfile
) {
237 # Open file to read in the stored error message.
238 open(FILE
, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
240 # Read the stored error message.
241 $errormessage = <FILE
>;
246 # Delete the file, which is now not longer required.
247 unlink($IDS::storederrorfile
);
250 ## Grab all available rules and store them in the idsrules hash.
252 # Open rules directory and do a directory listing.
253 opendir(DIR
, $IDS::rulespath
) or die $!;
254 # Loop through the direcory.
255 while (my $file = readdir(DIR
)) {
257 # We only want files.
258 next unless (-f
"$IDS::rulespath/$file");
260 # Ignore empty files.
261 next if (-z
"$IDS::rulespath/$file");
263 # Use a regular expression to find files ending in .rules
264 next unless ($file =~ m/\.rules$/);
266 # Ignore files which are not read-able.
267 next unless (-R
"$IDS::rulespath/$file");
269 # Skip whitelist rules file.
270 next if( $file eq "whitelist.rules");
272 # Call subfunction to read-in rulefile and add rules to
274 &readrulesfile
("$file");
279 # Gather used rulefiles.
281 # Check if the file for activated rulefiles is not empty.
282 if(-f
$IDS::used_rulefiles_file
) {
283 # Open the file for used rulefile and read-in content.
284 open(FILE
, $IDS::used_rulefiles_file
) or die "Could not open $IDS::used_rulefiles_file. $!\n";
292 # Loop through the array.
293 foreach my $line (@lines) {
298 next if ($line =~ /\#/);
301 next if ($line =~ /^\s*$/);
303 # Gather rule sid and message from the ruleline.
304 if ($line =~ /.*- (.*)/) {
307 # Check if the current rulefile exists in the %idsrules hash.
308 # If not, the file probably does not exist anymore or contains
310 if($idsrules{$rulefile}) {
311 # Add the rulefile state to the %idsrules hash.
312 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
318 # Save ruleset configuration.
319 if ($cgiparams{'RULESET'} eq $Lang::tr
{'save'}) {
323 # Read-in current (old) IDS settings.
324 &General
::readhash
("$IDS::rules_settings_file", \
%oldsettings);
326 # Get all available ruleset locations.
327 &General
::readhash
("$IDS::rulesetsourcesfile", \
%rulesetsources);
329 # Prevent form name from been stored in conf file.
330 delete $cgiparams{'RULESET'};
332 # Grab the URL based on the choosen vendor.
333 my $url = $rulesetsources{$cgiparams{'RULES'}};
335 # Check if the choosen vendor (URL) requires an subscription/oinkcode.
336 if ($url =~ /\<oinkcode\>/ ) {
337 # Check if an subscription/oinkcode has been provided.
338 if ($cgiparams{'OINKCODE'}) {
339 # Check if the oinkcode contains unallowed chars.
340 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
341 $errormessage = $Lang::tr
{'invalid input for oink code'};
344 # Print an error message, that an subsription/oinkcode is required for this
346 $errormessage = $Lang::tr
{'ids oinkcode required'};
350 # Go on if there are no error messages.
351 if (!$errormessage) {
352 # Store settings into settings file.
353 &General
::writehash
("$IDS::rules_settings_file", \
%cgiparams);
355 # Check if the the automatic rule update hass been touched.
356 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldsettings{'AUTOUPDATE_INTERVAL'}) {
357 # Call suricatactrl to set the new interval.
358 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
361 # Check if a ruleset is present - if not or the source has been changed download it.
362 if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) {
363 # Check if the red device is active.
364 unless (-e
"${General::swroot}/red/active") {
365 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
368 # Check if enough free disk space is availabe.
369 if(&IDS
::checkdiskspace
()) {
370 $errormessage = "$Lang::tr{'not enough disk space'}";
373 # Check if any errors happend.
374 unless ($errormessage) {
375 # Lock the webpage and print notice about downloading
377 &working_notice
("$Lang::tr{'ids working'}");
379 # Write the modify sid's file and pass the taken ruleaction.
380 &IDS
::write_modify_sids_file
();
382 # Call subfunction to download the ruleset.
383 if(&IDS
::downloadruleset
()) {
384 $errormessage = $Lang::tr
{'could not download latest updates'};
386 # Call function to store the errormessage.
387 &IDS
::_store_error_message
($errormessage);
389 # Call subfunction to launch oinkmaster.
393 # Check if the IDS is running.
394 if(&IDS
::ids_is_running
()) {
395 # Call suricatactrl to stop the IDS - because of the changed
396 # ruleset - the use has to configure it before suricata can be
398 &IDS
::call_suricatactrl
("stop");
401 # Perform a reload of the page.
408 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'ids apply'}) {
409 # Arrays to store which rulefiles have been enabled and will be used.
410 my @enabled_rulefiles;
412 # Hash to store the user-enabled and disabled sids.
413 my %enabled_disabled_sids;
415 # Store if a restart of suricata is required.
416 my $suricata_restart_required;
418 # Loop through the hash of idsrules.
419 foreach my $rulefile(keys %idsrules) {
420 # Check if the state of the rulefile has been changed.
421 unless ($cgiparams{$rulefile} eq $idsrules{$rulefile}{'Rulefile'}{'State'}) {
422 # A restart of suricata is required to apply the changes of the used rulefiles.
423 $suricata_restart_required = 1;
426 # Check if the rulefile is enabled.
427 if ($cgiparams{$rulefile} eq "on") {
428 # Add rulefile to the array of enabled rulefiles.
429 push(@enabled_rulefiles, $rulefile);
431 # Drop item from cgiparams hash.
432 delete $cgiparams{$rulefile};
436 # Read-in the files for enabled/disabled sids.
437 # This will be done by calling the read_enabled_disabled_sids_file function two times
438 # and merge the returned hashes together into the enabled_disabled_sids hash.
439 %enabled_disabled_sids = (
440 &read_enabled_disabled_sids_file
($IDS::disabled_sids_file
),
441 &read_enabled_disabled_sids_file
($IDS::enabled_sids_file
));
443 # Loop through the hash of idsrules.
444 foreach my $rulefile (keys %idsrules) {
445 # Loop through the single rules of the rulefile.
446 foreach my $sid (keys %{$idsrules{$rulefile}}) {
447 # Skip the current sid if it is not numeric.
448 next unless ($sid =~ /\d+/ );
450 # Check if there exists a key in the cgiparams hash for this sid.
451 if (exists($cgiparams{$sid})) {
452 # Look if the rule is disabled.
453 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
454 # Check if the state has been set to 'on'.
455 if ($cgiparams{$sid} eq "on") {
456 # Add/Modify the sid to/in the enabled_disabled_sids hash.
457 $enabled_disabled_sids{$sid} = "enabled";
459 # Drop item from cgiparams hash.
460 delete $cgiparams{$rulefile}{$sid};
464 # Look if the rule is enabled.
465 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
466 # Check if the state is 'on' and should be disabled.
467 # In this case there is no entry
468 # for the sid in the cgiparams hash.
469 # Add/Modify it to/in the enabled_disabled_sids hash.
470 $enabled_disabled_sids{$sid} = "disabled";
472 # Drop item from cgiparams hash.
473 delete $cgiparams{$rulefile}{$sid};
479 # Open enabled sid's file for writing.
480 open(ENABLED_FILE
, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
482 # Open disabled sid's file for writing.
483 open(DISABLED_FILE
, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
485 # Write header to the files.
486 print ENABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
487 print DISABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
489 # Check if the hash for enabled/disabled files contains any entries.
490 if (%enabled_disabled_sids) {
491 # Loop through the hash.
492 foreach my $sid (keys %enabled_disabled_sids) {
493 # Check if the sid is enabled.
494 if ($enabled_disabled_sids{$sid} eq "enabled") {
495 # Print the sid to the enabled_sids file.
496 print ENABLED_FILE
"enablesid $sid\n";
497 # Check if the sid is disabled.
498 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
499 # Print the sid to the disabled_sids file.
500 print DISABLED_FILE
"disablesid $sid\n";
501 # Something strange happende - skip the current sid.
508 # Close file for enabled_sids after writing.
511 # Close file for disabled_sids after writing.
512 close(DISABLED_FILE
);
514 # Call function to generate and write the used rulefiles file.
515 &IDS
::write_used_rulefiles_file
(@enabled_rulefiles);
517 # Lock the webpage and print message.
518 &working_notice
("$Lang::tr{'ids apply ruleset changes'}");
520 # Call oinkmaster to alter the ruleset.
523 # Check if the IDS is running.
524 if(&IDS
::ids_is_running
()) {
525 # Check if a restart of suricata is required.
526 if ($suricata_restart_required) {
527 # Call suricatactrl to perform the restart.
528 &IDS
::call_suricatactrl
("restart");
530 # Call suricatactrl to perform a reload.
531 &IDS
::call_suricatactrl
("reload");
538 # Download new ruleset.
539 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'update ruleset'}) {
540 # Check if the red device is active.
541 unless (-e
"${General::swroot}/red/active") {
542 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
545 # Check if enought free disk space is availabe.
546 if(&IDS
::checkdiskspace
()) {
547 $errormessage = "$Lang::tr{'not enough disk space'}";
550 # Check if any errors happend.
551 unless ($errormessage) {
552 # Lock the webpage and print notice about downloading
554 &working_notice
("$Lang::tr{'ids download new ruleset'}");
556 # Call subfunction to download the ruleset.
557 if(&IDS
::downloadruleset
()) {
558 $errormessage = $Lang::tr
{'could not download latest updates'};
560 # Call function to store the errormessage.
561 &IDS
::_store_error_message
($errormessage);
563 # Preform a reload of the page.
566 # Call subfunction to launch oinkmaster.
569 # Check if the IDS is running.
570 if(&IDS
::ids_is_running
()) {
571 # Call suricatactrl to perform a reload.
572 &IDS
::call_suricatactrl
("reload");
575 # Perform a reload of the page.
580 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
583 my $monitored_zones = 0;
585 # Read-in current (old) IDS settings.
586 &General
::readhash
("$IDS::ids_settings_file", \
%oldidssettings);
588 # Prevent form name from been stored in conf file.
589 delete $cgiparams{'IDS'};
591 # Check if the IDS should be enabled.
592 if ($cgiparams{'ENABLE_IDS'} eq "on") {
593 # Check if any ruleset is available. Otherwise abort and display an error.
595 $errormessage = $Lang::tr
{'ids no ruleset available'};
598 # Loop through the array of available interfaces.
599 foreach my $zone (@network_zones) {
600 # Convert interface name into upper case.
601 my $zone_upper = uc($zone);
603 # Check if the IDS is enabled for this interaces.
604 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
610 # Check if at least one zone should be monitored, or show an error.
611 unless ($monitored_zones >= 1) {
612 $errormessage = $Lang::tr
{'ids no network zone'};
616 # Go on if there are no error messages.
617 if (!$errormessage) {
618 # Store settings into settings file.
619 &General
::writehash
("$IDS::ids_settings_file", \
%cgiparams);
622 # Generate file to store the home net.
623 &IDS
::generate_home_net_file
();
625 # Generate file to the store the DNS servers.
626 &IDS
::generate_dns_servers_file
();
628 # Generate file to store the HTTP ports.
629 &IDS
::generate_http_ports_file
();
631 # Write the modify sid's file and pass the taken ruleaction.
632 &IDS
::write_modify_sids_file
();
634 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
635 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
636 # Check if a ruleset exists.
638 # Lock the webpage and print message.
639 &working_notice
("$Lang::tr{'ids working'}");
641 # Call oinkmaster to alter the ruleset.
644 # Set reload_page to "True".
649 # Check if the IDS currently is running.
650 if(&IDS
::ids_is_running
()) {
651 # Check if ENABLE_IDS is set to on.
652 if($cgiparams{'ENABLE_IDS'} eq "on") {
653 # Call suricatactrl to perform a reload of suricata.
654 &IDS
::call_suricatactrl
("reload");
656 # Call suricatactrl to stop suricata.
657 &IDS
::call_suricatactrl
("stop");
660 # Call suricatactrl to start suricata.
661 &IDS
::call_suricatactrl
("start");
664 # Check if the page should be reloaded.
666 # Perform a reload of the page.
671 # Read-in idssettings and rulesetsettings
672 &General
::readhash
("$IDS::ids_settings_file", \
%idssettings);
673 &General
::readhash
("$IDS::rules_settings_file", \
%rulessettings);
675 # If no autoupdate intervall has been configured yet, set default value.
676 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
677 # Set default to "weekly".
678 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
681 # Read-in ignored hosts.
682 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
684 $checked{'ENABLE_IDS'}{'off'} = '';
685 $checked{'ENABLE_IDS'}{'on'} = '';
686 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
687 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
688 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
689 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
690 $selected{'RULES'}{'nothing'} = '';
691 $selected{'RULES'}{'community'} = '';
692 $selected{'RULES'}{'emerging'} = '';
693 $selected{'RULES'}{'registered'} = '';
694 $selected{'RULES'}{'subscripted'} = '';
695 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
696 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
697 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
698 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
699 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
701 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
706 # Java script variable declaration for show and hide.
707 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
708 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
711 // Java Script function to show/hide the text input field for
712 // Oinkcode/Subscription code.
713 var update_code = function() {
714 if(\$('#RULES').val() == 'registered') {
716 } else if(\$('#RULES').val() == 'subscripted') {
718 } else if(\$('#RULES').val() == 'emerging_pro') {
725 // JQuery function to call corresponding function when
726 // the ruleset is changed or the page is loaded for showing/hiding
728 \$(document).ready(function() {
729 \$('#RULES').change(update_code);
733 // Tiny java script function to show/hide the rules
734 // of a given category.
735 function showhide(tblname) {
736 \$("#" + tblname).toggle();
738 // Get current content of the span element.
739 var content = document.getElementById("span_" + tblname);
741 if (content.innerHTML === show) {
742 content.innerHTML = hide;
744 content.innerHTML = show;
751 &Header
::openbigbox
('100%', 'left', '', $errormessage);
754 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
755 print "<class name='base'>$errormessage\n";
756 print " </class>\n";
760 # Draw current state of the IDS
761 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
763 # Check if the IDS is running and obtain the process-id.
764 my $pid = &IDS
::ids_is_running
();
766 # Display some useful information, if suricata daemon is running.
768 # Gather used memory.
769 my $memory = &get_memory_usage
($pid);
772 <table width='95%' cellspacing='0' class='tbl'>
774 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
778 <td class='base'>$Lang::tr{'guardian daemon'}</td>
779 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
783 <td class='base'></td>
784 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
785 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
789 <td class='base'></td>
790 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
791 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
796 # Otherwise display a hint that the service is not launched.
798 <table width='95%' cellspacing='0' class='tbl'>
800 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
804 <td class='base'>$Lang::tr{'guardian daemon'}</td>
805 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
811 # Only show this area, if a ruleset is present.
816 <br><br><h2>$Lang::tr{'settings'}</h2>
818 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
819 <table width='100%' border='0'>
821 <td class='base' colspan='2'>
822 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}> $Lang::tr{'ids enable'}
825 <td class='base' colspan='2'>
826 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}> $Lang::tr{'ids monitor traffic only'}
838 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
845 # Loop through the array of available networks and print config options.
846 foreach my $zone (@network_zones) {
850 # Convert current zone name to upper case.
851 my $zone_upper = uc($zone);
854 my $zone_name = $zone;
856 # Dirty hack to get the correct language string for the red zone.
857 if ($zone eq "red") {
861 # Grab checkbox status from settings hash.
862 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
863 $checked_input = "checked = 'checked'";
866 print "<td class='base' width='20%'>\n";
867 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
868 print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
880 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
891 # Draw elements for ruleset configuration.
892 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
895 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
896 <table width='100%' border='0'>
898 <td><b>$Lang::tr{'ids rules update'}</b></td>
899 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
903 <td><select name='RULES' id='RULES'>
904 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
905 <option value='emerging_pro' $selected{'RULES'}{'emerging_pro'} >$Lang::tr{'emerging pro rules'}</option>
906 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
907 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
908 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
913 <select name='AUTOUPDATE_INTERVAL'>
914 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
915 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
916 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
922 <td colspan='2'><br><br></td>
925 <tr style='display:none' id='code'>
926 <td colspan='2'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
935 # Show the "Update Ruleset"-Button only if a ruleset has been downloaded yet and automatic updates are disabled.
936 if ((%idsrules) && ($rulessettings{'AUTOUPDATE_INTERVAL'} eq "off")) {
937 # Display button to update the ruleset.
938 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
941 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
953 # Whitelist / Ignorelist
955 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ignored hosts'});
960 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
961 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
962 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
965 # Check if some hosts have been added to be ignored.
966 if (keys (%ignored)) {
969 # Loop through all entries of the hash.
970 while( (my $key) = each %ignored) {
971 # Assign data array positions to some nice variable names.
972 my $address = $ignored{$key}[0];
973 my $remark = $ignored{$key}[1];
974 my $status = $ignored{$key}[2];
976 # Check if the key (id) number is even or not.
977 if ($cgiparams{'ID'} eq $key) {
978 $col="bgcolor='${Header::colouryellow}'";
980 $col="bgcolor='$color{'color22'}'";
982 $col="bgcolor='$color{'color20'}'";
985 # Choose icon for the checkbox.
989 # Check if the status is enabled and select the correct image and description.
990 if ($status eq 'enabled' ) {
992 $gdesc = $Lang::tr
{'click to disable'};
995 $gdesc = $Lang::tr
{'click to enable'};
1000 <td width='20%' class='base' $col>$address</td>
1001 <td width='65%' class='base' $col>$remark</td>
1003 <td align='center' $col>
1004 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1005 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
1006 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
1007 <input type='hidden' name='ID' value='$key' />
1011 <td align='center' $col>
1012 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1013 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
1014 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
1015 <input type='hidden' name='ID' value='$key' />
1019 <td align='center' $col>
1020 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
1021 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1022 <input type='hidden' name='ID' value='$key'>
1023 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1030 # Print notice that currently no hosts are ignored.
1032 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1038 # Section to add new elements or edit existing ones.
1044 <div align='center'>
1045 <table width='100%'>
1048 # Assign correct headline and button text.
1053 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1054 if ($cgiparams{'ID'} ne '') {
1055 $buttontext = $Lang::tr
{'update'};
1056 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1058 # Grab address and remark for the given key.
1059 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1060 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1062 $buttontext = $Lang::tr
{'add'};
1063 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1067 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1068 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1070 <td width='30%'>$Lang::tr{'ip address'}: </td>
1071 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1073 <td width='30%'>$Lang::tr{'remark'}: </td>
1074 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1075 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1082 &Header
::closebox
();
1084 # Only show the section for configuring the ruleset if one is present.
1086 # Load neccessary perl modules for file stat and to format the timestamp.
1088 use POSIX
qw( strftime );
1090 # Call stat on the rulestarball.
1091 my $stat = stat("$IDS::rulestarball");
1093 # Get timestamp the file creation.
1094 my $mtime = $stat->mtime;
1096 # Convert into human read-able format.
1097 my $rulesdate = strftime
('%Y-%m-%d %H:%M:%S', localtime($mtime));
1099 &Header
::openbox
('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'} ($rulesdate)" );
1101 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1103 # Output display table for rule files
1104 print "<table width='100%'>\n";
1106 # Loop over each rule file
1107 foreach my $rulefile (sort keys(%idsrules)) {
1108 my $rulechecked = '';
1110 # Check if rule file is enabled
1111 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1112 $rulechecked = 'CHECKED';
1115 # Convert rulefile name into category name.
1116 my $categoryname = &_rulefile_to_category
($rulefile);
1118 # Table and rows for the rule files.
1120 print"<td class='base' width='5%'>\n";
1121 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1123 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1124 print"<td class='base' width='5%' align='right'>\n";
1125 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1129 # Rows which will be hidden per default and will contain the single rules.
1130 print"<tr style='display:none' id='$categoryname'>\n";
1131 print"<td colspan='3'>\n";
1138 # New table for the single rules.
1139 print "<table width='100%'>\n";
1141 # Loop over rule file rules
1142 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1144 my $ruledefchecked = '';
1146 # Skip rulefile itself.
1147 next if ($sid eq "Rulefile");
1149 # If 2 rules have been displayed, start a new row
1150 if (($lines % 2) == 0) {
1151 print "</tr><tr>\n";
1153 # Increase rows by once.
1159 $col="bgcolor='$color{'color20'}'";
1161 $col="bgcolor='$color{'color22'}'";
1165 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1166 $ruledefchecked = 'CHECKED';
1169 # Create rule checkbox and display rule description
1170 print "<td class='base' width='5%' align='right' $col>\n";
1171 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1173 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1175 # Increment rule count
1179 # If do not have a second rule for row, create empty cell
1180 if (($lines % 2) != 0) {
1181 print "<td class='base'></td>";
1184 # Close display table
1185 print "</tr></table></td></tr>";
1188 # Close display table
1192 <table width='100%'>
1194 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'></td>
1200 &Header
::closebox
();
1203 &Header
::closebigbox
();
1204 &Header
::closepage
();
1207 ## A function to display a notice, to lock the webpage and
1208 ## tell the user which action currently will be performed.
1210 sub working_notice
($) {
1213 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1214 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1215 &Header
::openbox
( 'Waiting', 1,);
1219 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1224 &Header
::closebox
();
1225 &Header
::closebigbox
();
1226 &Header
::closepage
();
1230 ## A tiny function to perform a reload of the webpage after one second.
1233 print "<meta http-equiv='refresh' content='1'>\n";
1240 ## Private function to read-in and parse rules of a given rulefile.
1242 ## The given file will be read, parsed and all valid rules will be stored by ID,
1243 ## message/description and it's state in the idsrules hash.
1245 sub readrulesfile
($) {
1246 my $rulefile = shift;
1248 # Open rule file and read in contents
1249 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1251 # Store file content in an array.
1252 my @lines = <RULEFILE
>;
1257 # Loop over rule file contents
1258 foreach my $line (@lines) {
1259 # Remove whitespaces.
1263 next if ($line =~ /^\s*$/);
1269 # Gather rule sid and message from the ruleline.
1270 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1274 # Check if a rule has been found.
1276 # Add rule to the idsrules hash.
1277 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1279 # Grab status of the rule. Check if ruleline starts with a "dash".
1280 if ($line =~ /^\#/) {
1281 # If yes, the rule is disabled.
1282 $idsrules{$rulefile}{$sid}{'State'} = "off";
1284 # Otherwise the rule is enabled.
1285 $idsrules{$rulefile}{$sid}{'State'} = "on";
1293 ## Function to get the used memory of a given process-id.
1295 sub get_memory_usage
($) {
1300 # Try to open the status file for the given process-id on the pseudo
1302 if (open(FILE
, "/proc/$pid/status")) {
1303 # Loop through the entire file.
1305 # Splitt current line content and store them into variables.
1306 my ($key, $value) = split(":", $_, 2);
1308 # Check if the current key is the one which contains the memory usage.
1309 # The wanted one is VmRSS which contains the Real-memory (resident set)
1310 # of the entire process.
1311 if ($key eq "VmRSS") {
1312 # Found the memory usage add it to the memory variable.
1320 # Close file handle.
1323 # Return memory usage.
1327 # If the file could not be open, return nothing.
1332 ## Function to read-in the given enabled or disables sids file.
1334 sub read_enabled_disabled_sids_file
($) {
1337 # Temporary hash to store the sids and their state. It will be
1338 # returned at the end of this function.
1341 # Open the given filename.
1342 open(FILE
, "$file") or die "Could not open $file. $!\n";
1344 # Loop through the file.
1350 next if ($_ =~ /^\s*$/);
1353 next if ($_ =~ /^\#/);
1355 # Splitt line into sid and state part.
1356 my ($state, $sid) = split(" ", $_);
1358 # Skip line if the sid is not numeric.
1359 next unless ($sid =~ /\d+/ );
1361 # Check if the sid was enabled.
1362 if ($state eq "enablesid") {
1363 # Add the sid and its state as enabled to the temporary hash.
1364 $temphash{$sid} = "enabled";
1365 # Check if the sid was disabled.
1366 } elsif ($state eq "disablesid") {
1367 # Add the sid and its state as disabled to the temporary hash.
1368 $temphash{$sid} = "disabled";
1369 # Invalid state - skip the current sid and state.
1383 ## Private function to convert a given rulefile to a category name.
1384 ## ( No file extension anymore and if the name contained a dot, it
1385 ## would be replaced by a underline sign.)
1387 sub _rulefile_to_category
($) {
1388 my ($filename) = @_;
1390 # Splitt the filename into single chunks and store them in a
1392 my @parts = split(/\./, $filename);
1394 # Return / Remove last element of the temporary array.
1395 # This removes the file extension.
1398 # Join together the single elements of the temporary array.
1399 # If these are more than one, use a "underline" for joining.
1400 my $category = join '_', @parts;
1402 # Return the converted filename.