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";
34 # Import ruleset providers file.
35 require "$IDS::rulesetsourcesfile";
38 my %mainsettings = ();
42 my %rulesetsources = ();
48 # Read-in main settings, for language, theme and colors.
49 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
50 &General
::readhash
("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \
%color);
52 # Get the available network zones, based on the config type of the system and store
53 # the list of zones in an array.
54 my @network_zones = &Network
::get_available_network_zones
();
56 # Check if openvpn is started and add it to the array of network zones.
57 if ( -e
"/var/run/openvpn.pid") {
58 push(@network_zones, "ovpn");
63 # Create files if they does not exist yet.
64 &IDS
::check_and_create_filelayout
();
66 # Hash which contains the colour code of a network zone.
68 'red' => $Header::colourred
,
69 'green' => $Header::colourgreen
,
70 'blue' => $Header::colourblue
,
71 'orange' => $Header::colourorange
,
72 'ovpn' => $Header::colourovpn
75 &Header
::showhttpheaders
();
78 &Header
::getcgihash
(\
%cgiparams);
80 ## Add/edit an entry to the ignore file.
82 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'})) {
84 # Check if any input has been performed.
85 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
87 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
88 if ((!&General
::validip
($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
89 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
92 $errormessage = "$Lang::tr{'guardian empty input'}";
95 # Go further if there was no error.
96 if ($errormessage eq '') {
101 # Assign hash values.
102 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
103 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
105 # Read-in ignoredfile.
106 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
108 # Check if we should edit an existing entry and got an ID.
109 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
110 # Assin the provided id.
111 $id = $cgiparams{'ID'};
113 # Undef the given ID.
114 undef($cgiparams{'ID'});
116 # Grab the configured status of the corresponding entry.
117 $status = $ignored{$id}[2];
119 # Each newly added entry automatically should be enabled.
122 # Generate the ID for the new entry.
124 # Sort the keys by their ID and store them in an array.
125 my @keys = sort { $a <=> $b } keys %ignored;
127 # Reverse the key array.
128 my @reversed = reverse(@keys);
130 # Obtain the last used id.
131 my $last_id = @reversed[0];
133 # Increase the last id by one and use it as id for the new entry.
137 # Add/Modify the entry to/in the ignored hash.
138 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
140 # Write the changed ignored hash to the ignored file.
141 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
143 # Regenerate the ignore file.
144 &IDS
::generate_ignore_file
();
147 # Check if the IDS is running.
148 if(&IDS
::ids_is_running
()) {
149 # Call suricatactrl to perform a reload.
150 &IDS
::call_suricatactrl
("reload");
153 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
156 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'toggle enable disable'}) {
159 # Only go further, if an ID has been passed.
160 if ($cgiparams{'ID'}) {
161 # Assign the given ID.
162 my $id = $cgiparams{'ID'};
164 # Undef the given ID.
165 undef($cgiparams{'ID'});
167 # Read-in ignoredfile.
168 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
170 # Grab the configured status of the corresponding entry.
171 my $status = $ignored{$id}[2];
174 if ($status eq "disabled") {
177 $status = "disabled";
180 # Modify the status of the existing entry.
181 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
183 # Write the changed ignored hash to the ignored file.
184 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
186 # Regenerate the ignore file.
187 &IDS
::generate_ignore_file
();
189 # Check if the IDS is running.
190 if(&IDS
::ids_is_running
()) {
191 # Call suricatactrl to perform a reload.
192 &IDS
::call_suricatactrl
("reload");
196 ## Remove entry from ignore list.
198 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'remove'}) {
201 # Read-in ignoredfile.
202 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
204 # Drop entry from the hash.
205 delete($ignored{$cgiparams{'ID'}});
207 # Undef the given ID.
208 undef($cgiparams{'ID'});
210 # Write the changed ignored hash to the ignored file.
211 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
213 # Regenerate the ignore file.
214 &IDS
::generate_ignore_file
();
216 # Check if the IDS is running.
217 if(&IDS
::ids_is_running
()) {
218 # Call suricatactrl to perform a reload.
219 &IDS
::call_suricatactrl
("reload");
223 # Check if the page is locked, in this case, the ids_page_lock_file exists.
224 if (-e
$IDS::ids_page_lock_file
) {
225 # Lock the webpage and print notice about autoupgrade of the ruleset
227 &working_notice
("$Lang::tr{'ids ruleset autoupdate in progress'}");
229 # Loop and check if the file still exists.
230 while(-e
$IDS::ids_page_lock_file
) {
231 # Sleep for a second and re-check.
235 # Page has been unlocked, perform a reload.
239 # Check if any error has been stored.
240 if (-e
$IDS::storederrorfile
) {
241 # Open file to read in the stored error message.
242 open(FILE
, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
244 # Read the stored error message.
245 $errormessage = <FILE
>;
250 # Delete the file, which is now not longer required.
251 unlink($IDS::storederrorfile
);
254 ## Grab all available rules and store them in the idsrules hash.
256 # Open rules directory and do a directory listing.
257 opendir(DIR
, $IDS::rulespath
) or die $!;
258 # Loop through the direcory.
259 while (my $file = readdir(DIR
)) {
261 # We only want files.
262 next unless (-f
"$IDS::rulespath/$file");
264 # Ignore empty files.
265 next if (-z
"$IDS::rulespath/$file");
267 # Use a regular expression to find files ending in .rules
268 next unless ($file =~ m/\.rules$/);
270 # Ignore files which are not read-able.
271 next unless (-R
"$IDS::rulespath/$file");
273 # Skip whitelist rules file.
274 next if( $file eq "whitelist.rules");
276 # Call subfunction to read-in rulefile and add rules to
278 &readrulesfile
("$file");
283 # Gather used rulefiles.
285 # Check if the file for activated rulefiles is not empty.
286 if(-f
$IDS::used_rulefiles_file
) {
287 # Open the file for used rulefile and read-in content.
288 open(FILE
, $IDS::used_rulefiles_file
) or die "Could not open $IDS::used_rulefiles_file. $!\n";
296 # Loop through the array.
297 foreach my $line (@lines) {
302 next if ($line =~ /\#/);
305 next if ($line =~ /^\s*$/);
307 # Gather rule sid and message from the ruleline.
308 if ($line =~ /.*- (.*)/) {
311 # Check if the current rulefile exists in the %idsrules hash.
312 # If not, the file probably does not exist anymore or contains
314 if($idsrules{$rulefile}) {
315 # Add the rulefile state to the %idsrules hash.
316 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
322 # Save ruleset configuration.
323 if ($cgiparams{'RULESET'} eq $Lang::tr
{'save'}) {
327 # Read-in current (old) IDS settings.
328 &General
::readhash
("$IDS::rules_settings_file", \
%oldsettings);
330 # Prevent form name from been stored in conf file.
331 delete $cgiparams{'RULESET'};
333 # Check if the choosen vendor (URL) requires an subscription/oinkcode.
334 if ($IDS::Ruleset
::Providers
{$cgiparams{'RULES'}}{'requires_subscription'} eq "True") {
335 # Check if an subscription/oinkcode has been provided.
336 if ($cgiparams{'OINKCODE'}) {
337 # Check if the oinkcode contains unallowed chars.
338 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
339 $errormessage = $Lang::tr
{'invalid input for oink code'};
342 # Print an error message, that an subsription/oinkcode is required for this
344 $errormessage = $Lang::tr
{'ids oinkcode required'};
348 # Go on if there are no error messages.
349 if (!$errormessage) {
350 # Store settings into settings file.
351 &General
::writehash
("$IDS::rules_settings_file", \
%cgiparams);
353 # Check if the the automatic rule update hass been touched.
354 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldsettings{'AUTOUPDATE_INTERVAL'}) {
355 # Call suricatactrl to set the new interval.
356 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
359 # Check if a ruleset is present - if not or the source has been changed download it.
360 if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) {
361 # Check if the red device is active.
362 unless (-e
"${General::swroot}/red/active") {
363 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
366 # Check if enough free disk space is availabe.
367 if(&IDS
::checkdiskspace
()) {
368 $errormessage = "$Lang::tr{'not enough disk space'}";
371 # Check if any errors happend.
372 unless ($errormessage) {
373 # Lock the webpage and print notice about downloading
375 &working_notice
("$Lang::tr{'ids working'}");
377 # Write the modify sid's file and pass the taken ruleaction.
378 &IDS
::write_modify_sids_file
();
380 # Call subfunction to download the ruleset.
381 if(&IDS
::downloadruleset
()) {
382 $errormessage = $Lang::tr
{'could not download latest updates'};
384 # Call function to store the errormessage.
385 &IDS
::_store_error_message
($errormessage);
387 # Call subfunction to launch oinkmaster.
391 # Check if the IDS is running.
392 if(&IDS
::ids_is_running
()) {
393 # Call suricatactrl to stop the IDS - because of the changed
394 # ruleset - the use has to configure it before suricata can be
396 &IDS
::call_suricatactrl
("stop");
399 # Perform a reload of the page.
406 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'ids apply'}) {
407 # Arrays to store which rulefiles have been enabled and will be used.
408 my @enabled_rulefiles;
410 # Hash to store the user-enabled and disabled sids.
411 my %enabled_disabled_sids;
413 # Store if a restart of suricata is required.
414 my $suricata_restart_required;
416 # Loop through the hash of idsrules.
417 foreach my $rulefile(keys %idsrules) {
418 # Check if the state of the rulefile has been changed.
419 unless ($cgiparams{$rulefile} eq $idsrules{$rulefile}{'Rulefile'}{'State'}) {
420 # A restart of suricata is required to apply the changes of the used rulefiles.
421 $suricata_restart_required = 1;
424 # Check if the rulefile is enabled.
425 if ($cgiparams{$rulefile} eq "on") {
426 # Add rulefile to the array of enabled rulefiles.
427 push(@enabled_rulefiles, $rulefile);
429 # Drop item from cgiparams hash.
430 delete $cgiparams{$rulefile};
434 # Read-in the files for enabled/disabled sids.
435 # This will be done by calling the read_enabled_disabled_sids_file function two times
436 # and merge the returned hashes together into the enabled_disabled_sids hash.
437 %enabled_disabled_sids = (
438 &read_enabled_disabled_sids_file
($IDS::disabled_sids_file
),
439 &read_enabled_disabled_sids_file
($IDS::enabled_sids_file
));
441 # Loop through the hash of idsrules.
442 foreach my $rulefile (keys %idsrules) {
443 # Loop through the single rules of the rulefile.
444 foreach my $sid (keys %{$idsrules{$rulefile}}) {
445 # Skip the current sid if it is not numeric.
446 next unless ($sid =~ /\d+/ );
448 # Check if there exists a key in the cgiparams hash for this sid.
449 if (exists($cgiparams{$sid})) {
450 # Look if the rule is disabled.
451 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
452 # Check if the state has been set to 'on'.
453 if ($cgiparams{$sid} eq "on") {
454 # Add/Modify the sid to/in the enabled_disabled_sids hash.
455 $enabled_disabled_sids{$sid} = "enabled";
457 # Drop item from cgiparams hash.
458 delete $cgiparams{$rulefile}{$sid};
462 # Look if the rule is enabled.
463 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
464 # Check if the state is 'on' and should be disabled.
465 # In this case there is no entry
466 # for the sid in the cgiparams hash.
467 # Add/Modify it to/in the enabled_disabled_sids hash.
468 $enabled_disabled_sids{$sid} = "disabled";
470 # Drop item from cgiparams hash.
471 delete $cgiparams{$rulefile}{$sid};
477 # Open enabled sid's file for writing.
478 open(ENABLED_FILE
, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
480 # Open disabled sid's file for writing.
481 open(DISABLED_FILE
, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
483 # Write header to the files.
484 print ENABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
485 print DISABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
487 # Check if the hash for enabled/disabled files contains any entries.
488 if (%enabled_disabled_sids) {
489 # Loop through the hash.
490 foreach my $sid (keys %enabled_disabled_sids) {
491 # Check if the sid is enabled.
492 if ($enabled_disabled_sids{$sid} eq "enabled") {
493 # Print the sid to the enabled_sids file.
494 print ENABLED_FILE
"enablesid $sid\n";
495 # Check if the sid is disabled.
496 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
497 # Print the sid to the disabled_sids file.
498 print DISABLED_FILE
"disablesid $sid\n";
499 # Something strange happende - skip the current sid.
506 # Close file for enabled_sids after writing.
509 # Close file for disabled_sids after writing.
510 close(DISABLED_FILE
);
512 # Call function to generate and write the used rulefiles file.
513 &IDS
::write_used_rulefiles_file
(@enabled_rulefiles);
515 # Lock the webpage and print message.
516 &working_notice
("$Lang::tr{'ids apply ruleset changes'}");
518 # Call oinkmaster to alter the ruleset.
521 # Check if the IDS is running.
522 if(&IDS
::ids_is_running
()) {
523 # Check if a restart of suricata is required.
524 if ($suricata_restart_required) {
525 # Call suricatactrl to perform the restart.
526 &IDS
::call_suricatactrl
("restart");
528 # Call suricatactrl to perform a reload.
529 &IDS
::call_suricatactrl
("reload");
536 # Download new ruleset.
537 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'update ruleset'}) {
538 # Check if the red device is active.
539 unless (-e
"${General::swroot}/red/active") {
540 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
543 # Check if enought free disk space is availabe.
544 if(&IDS
::checkdiskspace
()) {
545 $errormessage = "$Lang::tr{'not enough disk space'}";
548 # Check if any errors happend.
549 unless ($errormessage) {
550 # Lock the webpage and print notice about downloading
552 &working_notice
("$Lang::tr{'ids download new ruleset'}");
554 # Call subfunction to download the ruleset.
555 if(&IDS
::downloadruleset
()) {
556 $errormessage = $Lang::tr
{'could not download latest updates'};
558 # Call function to store the errormessage.
559 &IDS
::_store_error_message
($errormessage);
561 # Preform a reload of the page.
564 # Call subfunction to launch oinkmaster.
567 # Check if the IDS is running.
568 if(&IDS
::ids_is_running
()) {
569 # Call suricatactrl to perform a reload.
570 &IDS
::call_suricatactrl
("reload");
573 # Perform a reload of the page.
578 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
581 my $monitored_zones = 0;
583 # Read-in current (old) IDS settings.
584 &General
::readhash
("$IDS::ids_settings_file", \
%oldidssettings);
586 # Prevent form name from been stored in conf file.
587 delete $cgiparams{'IDS'};
589 # Check if the IDS should be enabled.
590 if ($cgiparams{'ENABLE_IDS'} eq "on") {
591 # Check if any ruleset is available. Otherwise abort and display an error.
593 $errormessage = $Lang::tr
{'ids no ruleset available'};
596 # Loop through the array of available interfaces.
597 foreach my $zone (@network_zones) {
598 # Convert interface name into upper case.
599 my $zone_upper = uc($zone);
601 # Check if the IDS is enabled for this interaces.
602 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
608 # Check if at least one zone should be monitored, or show an error.
609 unless ($monitored_zones >= 1) {
610 $errormessage = $Lang::tr
{'ids no network zone'};
614 # Go on if there are no error messages.
615 if (!$errormessage) {
616 # Store settings into settings file.
617 &General
::writehash
("$IDS::ids_settings_file", \
%cgiparams);
620 # Generate file to store the home net.
621 &IDS
::generate_home_net_file
();
623 # Generate file to the store the DNS servers.
624 &IDS
::generate_dns_servers_file
();
626 # Generate file to store the HTTP ports.
627 &IDS
::generate_http_ports_file
();
629 # Write the modify sid's file and pass the taken ruleaction.
630 &IDS
::write_modify_sids_file
();
632 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
633 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
634 # Check if a ruleset exists.
636 # Lock the webpage and print message.
637 &working_notice
("$Lang::tr{'ids working'}");
639 # Call oinkmaster to alter the ruleset.
642 # Set reload_page to "True".
647 # Check if the IDS currently is running.
648 if(&IDS
::ids_is_running
()) {
649 # Check if ENABLE_IDS is set to on.
650 if($cgiparams{'ENABLE_IDS'} eq "on") {
651 # Call suricatactrl to perform a reload of suricata.
652 &IDS
::call_suricatactrl
("reload");
654 # Call suricatactrl to stop suricata.
655 &IDS
::call_suricatactrl
("stop");
658 # Call suricatactrl to start suricata.
659 &IDS
::call_suricatactrl
("start");
662 # Check if the page should be reloaded.
664 # Perform a reload of the page.
669 # Read-in idssettings and rulesetsettings
670 &General
::readhash
("$IDS::ids_settings_file", \
%idssettings);
671 &General
::readhash
("$IDS::rules_settings_file", \
%rulessettings);
673 # If no autoupdate intervall has been configured yet, set default value.
674 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
675 # Set default to "weekly".
676 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
679 # Read-in ignored hosts.
680 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
682 $checked{'ENABLE_IDS'}{'off'} = '';
683 $checked{'ENABLE_IDS'}{'on'} = '';
684 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
685 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
686 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
687 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
688 $selected{'RULES'}{'nothing'} = '';
689 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
690 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
691 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
692 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
693 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
695 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
700 # Java script variable declaration for show and hide.
701 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
702 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
705 // Java Script function to show/hide the text input field for
706 // Oinkcode/Subscription code.
707 var update_code = function() {
708 if(\$('#RULES').val() == 'registered') {
710 } else if(\$('#RULES').val() == 'subscripted') {
712 } else if(\$('#RULES').val() == 'emerging_pro') {
719 // JQuery function to call corresponding function when
720 // the ruleset is changed or the page is loaded for showing/hiding
722 \$(document).ready(function() {
723 \$('#RULES').change(update_code);
727 // Tiny java script function to show/hide the rules
728 // of a given category.
729 function showhide(tblname) {
730 \$("#" + tblname).toggle();
732 // Get current content of the span element.
733 var content = document.getElementById("span_" + tblname);
735 if (content.innerHTML === show) {
736 content.innerHTML = hide;
738 content.innerHTML = show;
745 &Header
::openbigbox
('100%', 'left', '', $errormessage);
748 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
749 print "<class name='base'>$errormessage\n";
750 print " </class>\n";
754 # Draw current state of the IDS
755 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
757 # Check if the IDS is running and obtain the process-id.
758 my $pid = &IDS
::ids_is_running
();
760 # Display some useful information, if suricata daemon is running.
762 # Gather used memory.
763 my $memory = &get_memory_usage
($pid);
766 <table width='95%' cellspacing='0' class='tbl'>
768 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
772 <td class='base'>$Lang::tr{'guardian daemon'}</td>
773 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
777 <td class='base'></td>
778 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
779 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
783 <td class='base'></td>
784 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
785 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
790 # Otherwise display a hint that the service is not launched.
792 <table width='95%' cellspacing='0' class='tbl'>
794 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
798 <td class='base'>$Lang::tr{'guardian daemon'}</td>
799 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
805 # Only show this area, if a ruleset is present.
810 <br><br><h2>$Lang::tr{'settings'}</h2>
812 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
813 <table width='100%' border='0'>
815 <td class='base' colspan='2'>
816 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}> $Lang::tr{'ids enable'}
819 <td class='base' colspan='2'>
820 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}> $Lang::tr{'ids monitor traffic only'}
832 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
839 # Loop through the array of available networks and print config options.
840 foreach my $zone (@network_zones) {
844 # Convert current zone name to upper case.
845 my $zone_upper = uc($zone);
848 my $zone_name = $zone;
850 # Dirty hack to get the correct language string for the red zone.
851 if ($zone eq "red") {
855 # Grab checkbox status from settings hash.
856 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
857 $checked_input = "checked = 'checked'";
860 print "<td class='base' width='20%'>\n";
861 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
862 print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
874 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
885 # Draw elements for ruleset configuration.
886 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
889 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
890 <table width='100%' border='0'>
892 <td><b>$Lang::tr{'ids rules update'}</b></td>
893 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
897 <td><select name='RULES' id='RULES'>
900 # Get all available ruleset providers.
901 my @ruleset_providers = &IDS
::get_ruleset_providers
();
903 # Loop throgh the list of providers.
904 foreach my $provider (@ruleset_providers) {
907 # Get the translation required string for the current provider.
908 my $tr_string = $IDS::Ruleset
::Providers
{$provider}{'tr_string'};
910 # Check if a translation string is available in the language files.
911 if ($Lang::tr
{$tr_string}) {
912 # Use the translated string from the language file.
913 $option_string = $Lang::tr
{$tr_string};
915 # Fallback and use the provider summary from the providers file.
916 $option_string = $IDS::Ruleset
::Providers
{$provider}{'summary'};
919 print "<option value='$provider' $selected{'RULES'}{$provider}>$option_string</option>\n";
926 <select name='AUTOUPDATE_INTERVAL'>
927 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
928 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
929 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
935 <td colspan='2'><br><br></td>
938 <tr style='display:none' id='code'>
939 <td colspan='2'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
948 # Show the "Update Ruleset"-Button only if a ruleset has been downloaded yet and automatic updates are disabled.
949 if ((%idsrules) && ($rulessettings{'AUTOUPDATE_INTERVAL'} eq "off")) {
950 # Display button to update the ruleset.
951 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
954 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
966 # Whitelist / Ignorelist
968 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ignored hosts'});
973 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
974 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
975 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
978 # Check if some hosts have been added to be ignored.
979 if (keys (%ignored)) {
982 # Loop through all entries of the hash.
983 while( (my $key) = each %ignored) {
984 # Assign data array positions to some nice variable names.
985 my $address = $ignored{$key}[0];
986 my $remark = $ignored{$key}[1];
987 my $status = $ignored{$key}[2];
989 # Check if the key (id) number is even or not.
990 if ($cgiparams{'ID'} eq $key) {
991 $col="bgcolor='${Header::colouryellow}'";
993 $col="bgcolor='$color{'color22'}'";
995 $col="bgcolor='$color{'color20'}'";
998 # Choose icon for the checkbox.
1002 # Check if the status is enabled and select the correct image and description.
1003 if ($status eq 'enabled' ) {
1005 $gdesc = $Lang::tr
{'click to disable'};
1008 $gdesc = $Lang::tr
{'click to enable'};
1013 <td width='20%' class='base' $col>$address</td>
1014 <td width='65%' class='base' $col>$remark</td>
1016 <td align='center' $col>
1017 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1018 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
1019 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
1020 <input type='hidden' name='ID' value='$key' />
1024 <td align='center' $col>
1025 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1026 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
1027 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
1028 <input type='hidden' name='ID' value='$key' />
1032 <td align='center' $col>
1033 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
1034 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1035 <input type='hidden' name='ID' value='$key'>
1036 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1043 # Print notice that currently no hosts are ignored.
1045 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1051 # Section to add new elements or edit existing ones.
1057 <div align='center'>
1058 <table width='100%'>
1061 # Assign correct headline and button text.
1066 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1067 if ($cgiparams{'ID'} ne '') {
1068 $buttontext = $Lang::tr
{'update'};
1069 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1071 # Grab address and remark for the given key.
1072 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1073 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1075 $buttontext = $Lang::tr
{'add'};
1076 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1080 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1081 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1083 <td width='30%'>$Lang::tr{'ip address'}: </td>
1084 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1086 <td width='30%'>$Lang::tr{'remark'}: </td>
1087 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1088 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1095 &Header
::closebox
();
1097 # Only show the section for configuring the ruleset if one is present.
1099 # Load neccessary perl modules for file stat and to format the timestamp.
1101 use POSIX
qw( strftime );
1103 # Call stat on the rulestarball.
1104 my $stat = stat("$IDS::rulestarball");
1106 if (defined $stat) {
1107 # Get timestamp the file creation.
1108 my $mtime = $stat->mtime;
1110 # Convert into human read-able format.
1111 my $rulesdate = strftime
('%Y-%m-%d %H:%M:%S', localtime($mtime));
1113 &Header
::openbox
('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'} ($rulesdate)" );
1115 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1117 # Output display table for rule files
1118 print "<table width='100%'>\n";
1120 # Loop over each rule file
1121 foreach my $rulefile (sort keys(%idsrules)) {
1122 my $rulechecked = '';
1124 # Check if rule file is enabled
1125 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1126 $rulechecked = 'CHECKED';
1129 # Convert rulefile name into category name.
1130 my $categoryname = &_rulefile_to_category
($rulefile);
1132 # Table and rows for the rule files.
1134 print"<td class='base' width='5%'>\n";
1135 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1137 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1138 print"<td class='base' width='5%' align='right'>\n";
1139 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1143 # Rows which will be hidden per default and will contain the single rules.
1144 print"<tr style='display:none' id='$categoryname'>\n";
1145 print"<td colspan='3'>\n";
1152 # New table for the single rules.
1153 print "<table width='100%'>\n";
1155 # Loop over rule file rules
1156 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1158 my $ruledefchecked = '';
1160 # Skip rulefile itself.
1161 next if ($sid eq "Rulefile");
1163 # If 2 rules have been displayed, start a new row
1164 if (($lines % 2) == 0) {
1165 print "</tr><tr>\n";
1167 # Increase rows by once.
1173 $col="bgcolor='$color{'color20'}'";
1175 $col="bgcolor='$color{'color22'}'";
1179 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1180 $ruledefchecked = 'CHECKED';
1183 # Create rule checkbox and display rule description
1184 print "<td class='base' width='5%' align='right' $col>\n";
1185 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1187 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1189 # Increment rule count
1193 # If do not have a second rule for row, create empty cell
1194 if (($lines % 2) != 0) {
1195 print "<td class='base'></td>";
1198 # Close display table
1199 print "</tr></table></td></tr>";
1202 # Close display table
1206 <table width='100%'>
1208 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'></td>
1214 &Header
::closebox
();
1218 &Header
::closebigbox
();
1219 &Header
::closepage
();
1222 ## A function to display a notice, to lock the webpage and
1223 ## tell the user which action currently will be performed.
1225 sub working_notice
($) {
1228 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1229 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1230 &Header
::openbox
( 'Waiting', 1,);
1234 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1239 &Header
::closebox
();
1240 &Header
::closebigbox
();
1241 &Header
::closepage
();
1245 ## A tiny function to perform a reload of the webpage after one second.
1248 print "<meta http-equiv='refresh' content='1'>\n";
1255 ## Private function to read-in and parse rules of a given rulefile.
1257 ## The given file will be read, parsed and all valid rules will be stored by ID,
1258 ## message/description and it's state in the idsrules hash.
1260 sub readrulesfile
($) {
1261 my $rulefile = shift;
1263 # Open rule file and read in contents
1264 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1266 # Store file content in an array.
1267 my @lines = <RULEFILE
>;
1272 # Loop over rule file contents
1273 foreach my $line (@lines) {
1274 # Remove whitespaces.
1278 next if ($line =~ /^\s*$/);
1284 # Gather rule sid and message from the ruleline.
1285 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1289 # Check if a rule has been found.
1291 # Add rule to the idsrules hash.
1292 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1294 # Grab status of the rule. Check if ruleline starts with a "dash".
1295 if ($line =~ /^\#/) {
1296 # If yes, the rule is disabled.
1297 $idsrules{$rulefile}{$sid}{'State'} = "off";
1299 # Otherwise the rule is enabled.
1300 $idsrules{$rulefile}{$sid}{'State'} = "on";
1308 ## Function to get the used memory of a given process-id.
1310 sub get_memory_usage
($) {
1315 # Try to open the status file for the given process-id on the pseudo
1317 if (open(FILE
, "/proc/$pid/status")) {
1318 # Loop through the entire file.
1320 # Splitt current line content and store them into variables.
1321 my ($key, $value) = split(":", $_, 2);
1323 # Check if the current key is the one which contains the memory usage.
1324 # The wanted one is VmRSS which contains the Real-memory (resident set)
1325 # of the entire process.
1326 if ($key eq "VmRSS") {
1327 # Found the memory usage add it to the memory variable.
1335 # Close file handle.
1338 # Return memory usage.
1342 # If the file could not be open, return nothing.
1347 ## Function to read-in the given enabled or disables sids file.
1349 sub read_enabled_disabled_sids_file
($) {
1352 # Temporary hash to store the sids and their state. It will be
1353 # returned at the end of this function.
1356 # Open the given filename.
1357 open(FILE
, "$file") or die "Could not open $file. $!\n";
1359 # Loop through the file.
1365 next if ($_ =~ /^\s*$/);
1368 next if ($_ =~ /^\#/);
1370 # Splitt line into sid and state part.
1371 my ($state, $sid) = split(" ", $_);
1373 # Skip line if the sid is not numeric.
1374 next unless ($sid =~ /\d+/ );
1376 # Check if the sid was enabled.
1377 if ($state eq "enablesid") {
1378 # Add the sid and its state as enabled to the temporary hash.
1379 $temphash{$sid} = "enabled";
1380 # Check if the sid was disabled.
1381 } elsif ($state eq "disablesid") {
1382 # Add the sid and its state as disabled to the temporary hash.
1383 $temphash{$sid} = "disabled";
1384 # Invalid state - skip the current sid and state.
1398 ## Private function to convert a given rulefile to a category name.
1399 ## ( No file extension anymore and if the name contained a dot, it
1400 ## would be replaced by a underline sign.)
1402 sub _rulefile_to_category
($) {
1403 my ($filename) = @_;
1405 # Splitt the filename into single chunks and store them in a
1407 my @parts = split(/\./, $filename);
1409 # Return / Remove last element of the temporary array.
1410 # This removes the file extension.
1413 # Join together the single elements of the temporary array.
1414 # If these are more than one, use a "underline" for joining.
1415 my $category = join '_', @parts;
1417 # Return the converted filename.