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";
32 require "${General::swroot}/network-functions.pl";
35 my %mainsettings = ();
39 my %rulesetsources = ();
45 # Read-in main settings, for language, theme and colors.
46 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
47 &General
::readhash
("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \
%color);
49 # Get the available network zones, based on the config type of the system and store
50 # the list of zones in an array.
51 my @network_zones = &Network
::get_available_network_zones
();
53 # Check if openvpn is started and add it to the array of network zones.
54 if ( -e
"/var/run/openvpn.pid") {
55 push(@network_zones, "ovpn");
60 # Create files if they does not exist yet.
61 &IDS
::check_and_create_filelayout
();
63 # Hash which contains the colour code of a network zone.
65 'red' => $Header::colourred
,
66 'green' => $Header::colourgreen
,
67 'blue' => $Header::colourblue
,
68 'orange' => $Header::colourorange
,
69 'ovpn' => $Header::colourovpn
72 &Header
::showhttpheaders
();
75 &Header
::getcgihash
(\
%cgiparams);
77 ## Add/edit an entry to the ignore file.
79 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'})) {
81 # Check if any input has been performed.
82 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
84 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
85 if ((!&General
::validip
($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
86 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
89 $errormessage = "$Lang::tr{'guardian empty input'}";
92 # Go further if there was no error.
93 if ($errormessage eq '') {
99 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
100 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
102 # Read-in ignoredfile.
103 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
105 # Check if we should edit an existing entry and got an ID.
106 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
107 # Assin the provided id.
108 $id = $cgiparams{'ID'};
110 # Undef the given ID.
111 undef($cgiparams{'ID'});
113 # Grab the configured status of the corresponding entry.
114 $status = $ignored{$id}[2];
116 # Each newly added entry automatically should be enabled.
119 # Generate the ID for the new entry.
121 # Sort the keys by their ID and store them in an array.
122 my @keys = sort { $a <=> $b } keys %ignored;
124 # Reverse the key array.
125 my @reversed = reverse(@keys);
127 # Obtain the last used id.
128 my $last_id = @reversed[0];
130 # Increase the last id by one and use it as id for the new entry.
134 # Add/Modify the entry to/in the ignored hash.
135 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
137 # Write the changed ignored hash to the ignored file.
138 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
140 # Regenerate the ignore file.
141 &IDS
::generate_ignore_file
();
144 # Check if the IDS is running.
145 if(&IDS
::ids_is_running
()) {
146 # Call suricatactrl to perform a reload.
147 &IDS
::call_suricatactrl
("reload");
150 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
153 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'toggle enable disable'}) {
156 # Only go further, if an ID has been passed.
157 if ($cgiparams{'ID'}) {
158 # Assign the given ID.
159 my $id = $cgiparams{'ID'};
161 # Undef the given ID.
162 undef($cgiparams{'ID'});
164 # Read-in ignoredfile.
165 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
167 # Grab the configured status of the corresponding entry.
168 my $status = $ignored{$id}[2];
171 if ($status eq "disabled") {
174 $status = "disabled";
177 # Modify the status of the existing entry.
178 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
180 # Write the changed ignored hash to the ignored file.
181 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
183 # Regenerate the ignore file.
184 &IDS
::generate_ignore_file
();
186 # Check if the IDS is running.
187 if(&IDS
::ids_is_running
()) {
188 # Call suricatactrl to perform a reload.
189 &IDS
::call_suricatactrl
("reload");
193 ## Remove entry from ignore list.
195 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'remove'}) {
198 # Read-in ignoredfile.
199 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
201 # Drop entry from the hash.
202 delete($ignored{$cgiparams{'ID'}});
204 # Undef the given ID.
205 undef($cgiparams{'ID'});
207 # Write the changed ignored hash to the ignored file.
208 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
210 # Regenerate the ignore file.
211 &IDS
::generate_ignore_file
();
213 # Check if the IDS is running.
214 if(&IDS
::ids_is_running
()) {
215 # Call suricatactrl to perform a reload.
216 &IDS
::call_suricatactrl
("reload");
220 # Check if the page is locked, in this case, the ids_page_lock_file exists.
221 if (-e
$IDS::ids_page_lock_file
) {
222 # Lock the webpage and print notice about autoupgrade of the ruleset
224 &working_notice
("$Lang::tr{'ids ruleset autoupdate in progress'}");
226 # Loop and check if the file still exists.
227 while(-e
$IDS::ids_page_lock_file
) {
228 # Sleep for a second and re-check.
232 # Page has been unlocked, perform a reload.
236 # Check if any error has been stored.
237 if (-e
$IDS::storederrorfile
) {
238 # Open file to read in the stored error message.
239 open(FILE
, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
241 # Read the stored error message.
242 $errormessage = <FILE
>;
247 # Delete the file, which is now not longer required.
248 unlink($IDS::storederrorfile
);
251 ## Grab all available rules and store them in the idsrules hash.
253 # Open rules directory and do a directory listing.
254 opendir(DIR
, $IDS::rulespath
) or die $!;
255 # Loop through the direcory.
256 while (my $file = readdir(DIR
)) {
258 # We only want files.
259 next unless (-f
"$IDS::rulespath/$file");
261 # Ignore empty files.
262 next if (-z
"$IDS::rulespath/$file");
264 # Use a regular expression to find files ending in .rules
265 next unless ($file =~ m/\.rules$/);
267 # Ignore files which are not read-able.
268 next unless (-R
"$IDS::rulespath/$file");
270 # Skip whitelist rules file.
271 next if( $file eq "whitelist.rules");
273 # Call subfunction to read-in rulefile and add rules to
275 &readrulesfile
("$file");
280 # Gather used rulefiles.
282 # Check if the file for activated rulefiles is not empty.
283 if(-f
$IDS::used_rulefiles_file
) {
284 # Open the file for used rulefile and read-in content.
285 open(FILE
, $IDS::used_rulefiles_file
) or die "Could not open $IDS::used_rulefiles_file. $!\n";
293 # Loop through the array.
294 foreach my $line (@lines) {
299 next if ($line =~ /\#/);
302 next if ($line =~ /^\s*$/);
304 # Gather rule sid and message from the ruleline.
305 if ($line =~ /.*- (.*)/) {
308 # Check if the current rulefile exists in the %idsrules hash.
309 # If not, the file probably does not exist anymore or contains
311 if($idsrules{$rulefile}) {
312 # Add the rulefile state to the %idsrules hash.
313 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
319 # Save ruleset configuration.
320 if ($cgiparams{'RULESET'} eq $Lang::tr
{'save'}) {
324 # Read-in current (old) IDS settings.
325 &General
::readhash
("$IDS::rules_settings_file", \
%oldsettings);
327 # Get all available ruleset locations.
328 &General
::readhash
("$IDS::rulesetsourcesfile", \
%rulesetsources);
330 # Prevent form name from been stored in conf file.
331 delete $cgiparams{'RULESET'};
333 # Grab the URL based on the choosen vendor.
334 my $url = $rulesetsources{$cgiparams{'RULES'}};
336 # Check if the choosen vendor (URL) requires an subscription/oinkcode.
337 if ($url =~ /\<oinkcode\>/ ) {
338 # Check if an subscription/oinkcode has been provided.
339 if ($cgiparams{'OINKCODE'}) {
340 # Check if the oinkcode contains unallowed chars.
341 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
342 $errormessage = $Lang::tr
{'invalid input for oink code'};
345 # Print an error message, that an subsription/oinkcode is required for this
347 $errormessage = $Lang::tr
{'ids oinkcode required'};
351 # Go on if there are no error messages.
352 if (!$errormessage) {
353 # Store settings into settings file.
354 &General
::writehash
("$IDS::rules_settings_file", \
%cgiparams);
356 # Check if the the automatic rule update hass been touched.
357 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldsettings{'AUTOUPDATE_INTERVAL'}) {
358 # Call suricatactrl to set the new interval.
359 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
362 # Check if a ruleset is present - if not or the source has been changed download it.
363 if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) {
364 # Check if the red device is active.
365 unless (-e
"${General::swroot}/red/active") {
366 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
369 # Check if enough free disk space is availabe.
370 if(&IDS
::checkdiskspace
()) {
371 $errormessage = "$Lang::tr{'not enough disk space'}";
374 # Check if any errors happend.
375 unless ($errormessage) {
376 # Lock the webpage and print notice about downloading
378 &working_notice
("$Lang::tr{'ids working'}");
380 # Write the modify sid's file and pass the taken ruleaction.
381 &IDS
::write_modify_sids_file
();
383 # Call subfunction to download the ruleset.
384 if(&IDS
::downloadruleset
()) {
385 $errormessage = $Lang::tr
{'could not download latest updates'};
387 # Call function to store the errormessage.
388 &IDS
::_store_error_message
($errormessage);
390 # Call subfunction to launch oinkmaster.
394 # Check if the IDS is running.
395 if(&IDS
::ids_is_running
()) {
396 # Call suricatactrl to stop the IDS - because of the changed
397 # ruleset - the use has to configure it before suricata can be
399 &IDS
::call_suricatactrl
("stop");
402 # Perform a reload of the page.
409 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'ids apply'}) {
410 # Arrays to store which rulefiles have been enabled and will be used.
411 my @enabled_rulefiles;
413 # Hash to store the user-enabled and disabled sids.
414 my %enabled_disabled_sids;
416 # Store if a restart of suricata is required.
417 my $suricata_restart_required;
419 # Loop through the hash of idsrules.
420 foreach my $rulefile(keys %idsrules) {
421 # Check if the state of the rulefile has been changed.
422 unless ($cgiparams{$rulefile} eq $idsrules{$rulefile}{'Rulefile'}{'State'}) {
423 # A restart of suricata is required to apply the changes of the used rulefiles.
424 $suricata_restart_required = 1;
427 # Check if the rulefile is enabled.
428 if ($cgiparams{$rulefile} eq "on") {
429 # Add rulefile to the array of enabled rulefiles.
430 push(@enabled_rulefiles, $rulefile);
432 # Drop item from cgiparams hash.
433 delete $cgiparams{$rulefile};
437 # Read-in the files for enabled/disabled sids.
438 # This will be done by calling the read_enabled_disabled_sids_file function two times
439 # and merge the returned hashes together into the enabled_disabled_sids hash.
440 %enabled_disabled_sids = (
441 &read_enabled_disabled_sids_file
($IDS::disabled_sids_file
),
442 &read_enabled_disabled_sids_file
($IDS::enabled_sids_file
));
444 # Loop through the hash of idsrules.
445 foreach my $rulefile (keys %idsrules) {
446 # Loop through the single rules of the rulefile.
447 foreach my $sid (keys %{$idsrules{$rulefile}}) {
448 # Skip the current sid if it is not numeric.
449 next unless ($sid =~ /\d+/ );
451 # Check if there exists a key in the cgiparams hash for this sid.
452 if (exists($cgiparams{$sid})) {
453 # Look if the rule is disabled.
454 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
455 # Check if the state has been set to 'on'.
456 if ($cgiparams{$sid} eq "on") {
457 # Add/Modify the sid to/in the enabled_disabled_sids hash.
458 $enabled_disabled_sids{$sid} = "enabled";
460 # Drop item from cgiparams hash.
461 delete $cgiparams{$rulefile}{$sid};
465 # Look if the rule is enabled.
466 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
467 # Check if the state is 'on' and should be disabled.
468 # In this case there is no entry
469 # for the sid in the cgiparams hash.
470 # Add/Modify it to/in the enabled_disabled_sids hash.
471 $enabled_disabled_sids{$sid} = "disabled";
473 # Drop item from cgiparams hash.
474 delete $cgiparams{$rulefile}{$sid};
480 # Open enabled sid's file for writing.
481 open(ENABLED_FILE
, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
483 # Open disabled sid's file for writing.
484 open(DISABLED_FILE
, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
486 # Write header to the files.
487 print ENABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
488 print DISABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
490 # Check if the hash for enabled/disabled files contains any entries.
491 if (%enabled_disabled_sids) {
492 # Loop through the hash.
493 foreach my $sid (keys %enabled_disabled_sids) {
494 # Check if the sid is enabled.
495 if ($enabled_disabled_sids{$sid} eq "enabled") {
496 # Print the sid to the enabled_sids file.
497 print ENABLED_FILE
"enablesid $sid\n";
498 # Check if the sid is disabled.
499 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
500 # Print the sid to the disabled_sids file.
501 print DISABLED_FILE
"disablesid $sid\n";
502 # Something strange happende - skip the current sid.
509 # Close file for enabled_sids after writing.
512 # Close file for disabled_sids after writing.
513 close(DISABLED_FILE
);
515 # Call function to generate and write the used rulefiles file.
516 &IDS
::write_used_rulefiles_file
(@enabled_rulefiles);
518 # Lock the webpage and print message.
519 &working_notice
("$Lang::tr{'ids apply ruleset changes'}");
521 # Call oinkmaster to alter the ruleset.
524 # Check if the IDS is running.
525 if(&IDS
::ids_is_running
()) {
526 # Check if a restart of suricata is required.
527 if ($suricata_restart_required) {
528 # Call suricatactrl to perform the restart.
529 &IDS
::call_suricatactrl
("restart");
531 # Call suricatactrl to perform a reload.
532 &IDS
::call_suricatactrl
("reload");
539 # Download new ruleset.
540 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'update ruleset'}) {
541 # Check if the red device is active.
542 unless (-e
"${General::swroot}/red/active") {
543 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
546 # Check if enought free disk space is availabe.
547 if(&IDS
::checkdiskspace
()) {
548 $errormessage = "$Lang::tr{'not enough disk space'}";
551 # Check if any errors happend.
552 unless ($errormessage) {
553 # Lock the webpage and print notice about downloading
555 &working_notice
("$Lang::tr{'ids download new ruleset'}");
557 # Call subfunction to download the ruleset.
558 if(&IDS
::downloadruleset
()) {
559 $errormessage = $Lang::tr
{'could not download latest updates'};
561 # Call function to store the errormessage.
562 &IDS
::_store_error_message
($errormessage);
564 # Preform a reload of the page.
567 # Call subfunction to launch oinkmaster.
570 # Check if the IDS is running.
571 if(&IDS
::ids_is_running
()) {
572 # Call suricatactrl to perform a reload.
573 &IDS
::call_suricatactrl
("reload");
576 # Perform a reload of the page.
581 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
584 my $monitored_zones = 0;
586 # Read-in current (old) IDS settings.
587 &General
::readhash
("$IDS::ids_settings_file", \
%oldidssettings);
589 # Prevent form name from been stored in conf file.
590 delete $cgiparams{'IDS'};
592 # Check if the IDS should be enabled.
593 if ($cgiparams{'ENABLE_IDS'} eq "on") {
594 # Check if any ruleset is available. Otherwise abort and display an error.
596 $errormessage = $Lang::tr
{'ids no ruleset available'};
599 # Loop through the array of available interfaces.
600 foreach my $zone (@network_zones) {
601 # Convert interface name into upper case.
602 my $zone_upper = uc($zone);
604 # Check if the IDS is enabled for this interaces.
605 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
611 # Check if at least one zone should be monitored, or show an error.
612 unless ($monitored_zones >= 1) {
613 $errormessage = $Lang::tr
{'ids no network zone'};
617 # Go on if there are no error messages.
618 if (!$errormessage) {
619 # Store settings into settings file.
620 &General
::writehash
("$IDS::ids_settings_file", \
%cgiparams);
623 # Generate file to store the home net.
624 &IDS
::generate_home_net_file
();
626 # Generate file to the store the DNS servers.
627 &IDS
::generate_dns_servers_file
();
629 # Generate file to store the HTTP ports.
630 &IDS
::generate_http_ports_file
();
632 # Write the modify sid's file and pass the taken ruleaction.
633 &IDS
::write_modify_sids_file
();
635 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
636 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
637 # Check if a ruleset exists.
639 # Lock the webpage and print message.
640 &working_notice
("$Lang::tr{'ids working'}");
642 # Call oinkmaster to alter the ruleset.
645 # Set reload_page to "True".
650 # Check if the IDS currently is running.
651 if(&IDS
::ids_is_running
()) {
652 # Check if ENABLE_IDS is set to on.
653 if($cgiparams{'ENABLE_IDS'} eq "on") {
654 # Call suricatactrl to perform a reload of suricata.
655 &IDS
::call_suricatactrl
("reload");
657 # Call suricatactrl to stop suricata.
658 &IDS
::call_suricatactrl
("stop");
661 # Call suricatactrl to start suricata.
662 &IDS
::call_suricatactrl
("start");
665 # Check if the page should be reloaded.
667 # Perform a reload of the page.
672 # Read-in idssettings and rulesetsettings
673 &General
::readhash
("$IDS::ids_settings_file", \
%idssettings);
674 &General
::readhash
("$IDS::rules_settings_file", \
%rulessettings);
676 # If no autoupdate intervall has been configured yet, set default value.
677 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
678 # Set default to "weekly".
679 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
682 # Read-in ignored hosts.
683 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
685 $checked{'ENABLE_IDS'}{'off'} = '';
686 $checked{'ENABLE_IDS'}{'on'} = '';
687 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
688 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
689 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
690 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
691 $selected{'RULES'}{'nothing'} = '';
692 $selected{'RULES'}{'community'} = '';
693 $selected{'RULES'}{'emerging'} = '';
694 $selected{'RULES'}{'registered'} = '';
695 $selected{'RULES'}{'subscripted'} = '';
696 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
697 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
698 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
699 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
700 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
702 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
707 # Java script variable declaration for show and hide.
708 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
709 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
712 // Java Script function to show/hide the text input field for
713 // Oinkcode/Subscription code.
714 var update_code = function() {
715 if(\$('#RULES').val() == 'registered') {
717 } else if(\$('#RULES').val() == 'subscripted') {
719 } else if(\$('#RULES').val() == 'emerging_pro') {
726 // JQuery function to call corresponding function when
727 // the ruleset is changed or the page is loaded for showing/hiding
729 \$(document).ready(function() {
730 \$('#RULES').change(update_code);
734 // Tiny java script function to show/hide the rules
735 // of a given category.
736 function showhide(tblname) {
737 \$("#" + tblname).toggle();
739 // Get current content of the span element.
740 var content = document.getElementById("span_" + tblname);
742 if (content.innerHTML === show) {
743 content.innerHTML = hide;
745 content.innerHTML = show;
752 &Header
::openbigbox
('100%', 'left', '', $errormessage);
755 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
756 print "<class name='base'>$errormessage\n";
757 print " </class>\n";
761 # Draw current state of the IDS
762 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
764 # Check if the IDS is running and obtain the process-id.
765 my $pid = &IDS
::ids_is_running
();
767 # Display some useful information, if suricata daemon is running.
769 # Gather used memory.
770 my $memory = &get_memory_usage
($pid);
773 <table width='95%' cellspacing='0' class='tbl'>
775 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
779 <td class='base'>$Lang::tr{'guardian daemon'}</td>
780 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
784 <td class='base'></td>
785 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
786 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
790 <td class='base'></td>
791 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
792 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
797 # Otherwise display a hint that the service is not launched.
799 <table width='95%' cellspacing='0' class='tbl'>
801 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
805 <td class='base'>$Lang::tr{'guardian daemon'}</td>
806 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
812 # Only show this area, if a ruleset is present.
817 <br><br><h2>$Lang::tr{'settings'}</h2>
819 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
820 <table width='100%' border='0'>
822 <td class='base' colspan='2'>
823 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}> $Lang::tr{'ids enable'}
826 <td class='base' colspan='2'>
827 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}> $Lang::tr{'ids monitor traffic only'}
839 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
846 # Loop through the array of available networks and print config options.
847 foreach my $zone (@network_zones) {
851 # Convert current zone name to upper case.
852 my $zone_upper = uc($zone);
855 my $zone_name = $zone;
857 # Dirty hack to get the correct language string for the red zone.
858 if ($zone eq "red") {
862 # Grab checkbox status from settings hash.
863 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
864 $checked_input = "checked = 'checked'";
867 print "<td class='base' width='20%'>\n";
868 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
869 print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
881 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
892 # Draw elements for ruleset configuration.
893 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
896 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
897 <table width='100%' border='0'>
899 <td><b>$Lang::tr{'ids rules update'}</b></td>
900 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
904 <td><select name='RULES' id='RULES'>
905 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
906 <option value='emerging_pro' $selected{'RULES'}{'emerging_pro'} >$Lang::tr{'emerging pro rules'}</option>
907 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
908 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
909 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
914 <select name='AUTOUPDATE_INTERVAL'>
915 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
916 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
917 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
923 <td colspan='2'><br><br></td>
926 <tr style='display:none' id='code'>
927 <td colspan='2'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
936 # Show the "Update Ruleset"-Button only if a ruleset has been downloaded yet and automatic updates are disabled.
937 if ((%idsrules) && ($rulessettings{'AUTOUPDATE_INTERVAL'} eq "off")) {
938 # Display button to update the ruleset.
939 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
942 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
954 # Whitelist / Ignorelist
956 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ignored hosts'});
961 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
962 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
963 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
966 # Check if some hosts have been added to be ignored.
967 if (keys (%ignored)) {
970 # Loop through all entries of the hash.
971 while( (my $key) = each %ignored) {
972 # Assign data array positions to some nice variable names.
973 my $address = $ignored{$key}[0];
974 my $remark = $ignored{$key}[1];
975 my $status = $ignored{$key}[2];
977 # Check if the key (id) number is even or not.
978 if ($cgiparams{'ID'} eq $key) {
979 $col="bgcolor='${Header::colouryellow}'";
981 $col="bgcolor='$color{'color22'}'";
983 $col="bgcolor='$color{'color20'}'";
986 # Choose icon for the checkbox.
990 # Check if the status is enabled and select the correct image and description.
991 if ($status eq 'enabled' ) {
993 $gdesc = $Lang::tr
{'click to disable'};
996 $gdesc = $Lang::tr
{'click to enable'};
1001 <td width='20%' class='base' $col>$address</td>
1002 <td width='65%' class='base' $col>$remark</td>
1004 <td align='center' $col>
1005 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1006 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
1007 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
1008 <input type='hidden' name='ID' value='$key' />
1012 <td align='center' $col>
1013 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1014 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
1015 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
1016 <input type='hidden' name='ID' value='$key' />
1020 <td align='center' $col>
1021 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
1022 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1023 <input type='hidden' name='ID' value='$key'>
1024 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1031 # Print notice that currently no hosts are ignored.
1033 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1039 # Section to add new elements or edit existing ones.
1045 <div align='center'>
1046 <table width='100%'>
1049 # Assign correct headline and button text.
1054 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1055 if ($cgiparams{'ID'} ne '') {
1056 $buttontext = $Lang::tr
{'update'};
1057 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1059 # Grab address and remark for the given key.
1060 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1061 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1063 $buttontext = $Lang::tr
{'add'};
1064 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1068 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1069 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1071 <td width='30%'>$Lang::tr{'ip address'}: </td>
1072 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1074 <td width='30%'>$Lang::tr{'remark'}: </td>
1075 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1076 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1083 &Header
::closebox
();
1085 # Only show the section for configuring the ruleset if one is present.
1087 # Load neccessary perl modules for file stat and to format the timestamp.
1089 use POSIX
qw( strftime );
1091 # Call stat on the rulestarball.
1092 my $stat = stat("$IDS::rulestarball");
1094 if (defined $stat) {
1095 # Get timestamp the file creation.
1096 my $mtime = $stat->mtime;
1098 # Convert into human read-able format.
1099 my $rulesdate = strftime
('%Y-%m-%d %H:%M:%S', localtime($mtime));
1101 &Header
::openbox
('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'} ($rulesdate)" );
1103 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1105 # Output display table for rule files
1106 print "<table width='100%'>\n";
1108 # Loop over each rule file
1109 foreach my $rulefile (sort keys(%idsrules)) {
1110 my $rulechecked = '';
1112 # Check if rule file is enabled
1113 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1114 $rulechecked = 'CHECKED';
1117 # Convert rulefile name into category name.
1118 my $categoryname = &_rulefile_to_category
($rulefile);
1120 # Table and rows for the rule files.
1122 print"<td class='base' width='5%'>\n";
1123 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1125 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1126 print"<td class='base' width='5%' align='right'>\n";
1127 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1131 # Rows which will be hidden per default and will contain the single rules.
1132 print"<tr style='display:none' id='$categoryname'>\n";
1133 print"<td colspan='3'>\n";
1140 # New table for the single rules.
1141 print "<table width='100%'>\n";
1143 # Loop over rule file rules
1144 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1146 my $ruledefchecked = '';
1148 # Skip rulefile itself.
1149 next if ($sid eq "Rulefile");
1151 # If 2 rules have been displayed, start a new row
1152 if (($lines % 2) == 0) {
1153 print "</tr><tr>\n";
1155 # Increase rows by once.
1161 $col="bgcolor='$color{'color20'}'";
1163 $col="bgcolor='$color{'color22'}'";
1167 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1168 $ruledefchecked = 'CHECKED';
1171 # Create rule checkbox and display rule description
1172 print "<td class='base' width='5%' align='right' $col>\n";
1173 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1175 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1177 # Increment rule count
1181 # If do not have a second rule for row, create empty cell
1182 if (($lines % 2) != 0) {
1183 print "<td class='base'></td>";
1186 # Close display table
1187 print "</tr></table></td></tr>";
1190 # Close display table
1194 <table width='100%'>
1196 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'></td>
1202 &Header
::closebox
();
1206 &Header
::closebigbox
();
1207 &Header
::closepage
();
1210 ## A function to display a notice, to lock the webpage and
1211 ## tell the user which action currently will be performed.
1213 sub working_notice
($) {
1216 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1217 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1218 &Header
::openbox
( 'Waiting', 1,);
1222 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1227 &Header
::closebox
();
1228 &Header
::closebigbox
();
1229 &Header
::closepage
();
1233 ## A tiny function to perform a reload of the webpage after one second.
1236 print "<meta http-equiv='refresh' content='1'>\n";
1243 ## Private function to read-in and parse rules of a given rulefile.
1245 ## The given file will be read, parsed and all valid rules will be stored by ID,
1246 ## message/description and it's state in the idsrules hash.
1248 sub readrulesfile
($) {
1249 my $rulefile = shift;
1251 # Open rule file and read in contents
1252 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1254 # Store file content in an array.
1255 my @lines = <RULEFILE
>;
1260 # Loop over rule file contents
1261 foreach my $line (@lines) {
1262 # Remove whitespaces.
1266 next if ($line =~ /^\s*$/);
1272 # Gather rule sid and message from the ruleline.
1273 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1277 # Check if a rule has been found.
1279 # Add rule to the idsrules hash.
1280 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1282 # Grab status of the rule. Check if ruleline starts with a "dash".
1283 if ($line =~ /^\#/) {
1284 # If yes, the rule is disabled.
1285 $idsrules{$rulefile}{$sid}{'State'} = "off";
1287 # Otherwise the rule is enabled.
1288 $idsrules{$rulefile}{$sid}{'State'} = "on";
1296 ## Function to get the used memory of a given process-id.
1298 sub get_memory_usage
($) {
1303 # Try to open the status file for the given process-id on the pseudo
1305 if (open(FILE
, "/proc/$pid/status")) {
1306 # Loop through the entire file.
1308 # Splitt current line content and store them into variables.
1309 my ($key, $value) = split(":", $_, 2);
1311 # Check if the current key is the one which contains the memory usage.
1312 # The wanted one is VmRSS which contains the Real-memory (resident set)
1313 # of the entire process.
1314 if ($key eq "VmRSS") {
1315 # Found the memory usage add it to the memory variable.
1323 # Close file handle.
1326 # Return memory usage.
1330 # If the file could not be open, return nothing.
1335 ## Function to read-in the given enabled or disables sids file.
1337 sub read_enabled_disabled_sids_file
($) {
1340 # Temporary hash to store the sids and their state. It will be
1341 # returned at the end of this function.
1344 # Open the given filename.
1345 open(FILE
, "$file") or die "Could not open $file. $!\n";
1347 # Loop through the file.
1353 next if ($_ =~ /^\s*$/);
1356 next if ($_ =~ /^\#/);
1358 # Splitt line into sid and state part.
1359 my ($state, $sid) = split(" ", $_);
1361 # Skip line if the sid is not numeric.
1362 next unless ($sid =~ /\d+/ );
1364 # Check if the sid was enabled.
1365 if ($state eq "enablesid") {
1366 # Add the sid and its state as enabled to the temporary hash.
1367 $temphash{$sid} = "enabled";
1368 # Check if the sid was disabled.
1369 } elsif ($state eq "disablesid") {
1370 # Add the sid and its state as disabled to the temporary hash.
1371 $temphash{$sid} = "disabled";
1372 # Invalid state - skip the current sid and state.
1386 ## Private function to convert a given rulefile to a category name.
1387 ## ( No file extension anymore and if the name contained a dot, it
1388 ## would be replaced by a underline sign.)
1390 sub _rulefile_to_category
($) {
1391 my ($filename) = @_;
1393 # Splitt the filename into single chunks and store them in a
1395 my @parts = split(/\./, $filename);
1397 # Return / Remove last element of the temporary array.
1398 # This removes the file extension.
1401 # Join together the single elements of the temporary array.
1402 # If these are more than one, use a "underline" for joining.
1403 my $category = join '_', @parts;
1405 # Return the converted filename.