2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2018 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
();
54 # Create files if they does not exist yet.
55 &IDS
::check_and_create_filelayout
();
57 # Hash which contains the colour code of a network zone.
59 'red' => $Header::colourred
,
60 'green' => $Header::colourgreen
,
61 'blue' => $Header::colourblue
,
62 'orange' => $Header::colourorange
65 &Header
::showhttpheaders
();
68 &Header
::getcgihash
(\
%cgiparams);
70 ## Add/edit an entry to the ignore file.
72 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'})) {
74 # Check if any input has been performed.
75 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
77 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
78 if ((!&General
::validip
($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
79 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
82 $errormessage = "$Lang::tr{'guardian empty input'}";
85 # Go further if there was no error.
86 if ($errormessage eq '') {
92 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
93 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
95 # Read-in ignoredfile.
96 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
98 # Check if we should edit an existing entry and got an ID.
99 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
100 # Assin the provided id.
101 $id = $cgiparams{'ID'};
103 # Undef the given ID.
104 undef($cgiparams{'ID'});
106 # Grab the configured status of the corresponding entry.
107 $status = $ignored{$id}[2];
109 # Each newly added entry automatically should be enabled.
112 # Generate the ID for the new entry.
114 # Sort the keys by their ID and store them in an array.
115 my @keys = sort { $a <=> $b } keys %ignored;
117 # Reverse the key array.
118 my @reversed = reverse(@keys);
120 # Obtain the last used id.
121 my $last_id = @reversed[0];
123 # Increase the last id by one and use it as id for the new entry.
127 # Add/Modify the entry to/in the ignored hash.
128 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
130 # Write the changed ignored hash to the ignored file.
131 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
133 # Regenerate the ignore file.
134 &IDS
::generate_ignore_file
();
137 # Check if the IDS is running.
138 if(&IDS
::ids_is_running
()) {
139 # Call suricatactrl to perform a reload.
140 &IDS
::call_suricatactrl
("reload");
143 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
146 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'toggle enable disable'}) {
149 # Only go further, if an ID has been passed.
150 if ($cgiparams{'ID'}) {
151 # Assign the given ID.
152 my $id = $cgiparams{'ID'};
154 # Undef the given ID.
155 undef($cgiparams{'ID'});
157 # Read-in ignoredfile.
158 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
160 # Grab the configured status of the corresponding entry.
161 my $status = $ignored{$id}[2];
164 if ($status eq "disabled") {
167 $status = "disabled";
170 # Modify the status of the existing entry.
171 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
173 # Write the changed ignored hash to the ignored file.
174 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
176 # Regenerate the ignore file.
177 &IDS
::generate_ignore_file
();
179 # Check if the IDS is running.
180 if(&IDS
::ids_is_running
()) {
181 # Call suricatactrl to perform a reload.
182 &IDS
::call_suricatactrl
("reload");
186 ## Remove entry from ignore list.
188 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'remove'}) {
191 # Read-in ignoredfile.
192 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
194 # Drop entry from the hash.
195 delete($ignored{$cgiparams{'ID'}});
197 # Undef the given ID.
198 undef($cgiparams{'ID'});
200 # Write the changed ignored hash to the ignored file.
201 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
203 # Regenerate the ignore file.
204 &IDS
::generate_ignore_file
();
206 # Check if the IDS is running.
207 if(&IDS
::ids_is_running
()) {
208 # Call suricatactrl to perform a reload.
209 &IDS
::call_suricatactrl
("reload");
213 # Check if the page is locked, in this case, the ids_page_lock_file exists.
214 if (-e
$IDS::ids_page_lock_file
) {
215 # Lock the webpage and print notice about autoupgrade of the ruleset
217 &working_notice
("$Lang::tr{'ids ruleset autoupdate in progress'}");
219 # Loop and check if the file still exists.
220 while(-e
$IDS::ids_page_lock_file
) {
221 # Sleep for a second and re-check.
225 # Page has been unlocked, perform a reload.
229 # Check if any error has been stored.
230 if (-e
$IDS::storederrorfile
) {
231 # Open file to read in the stored error message.
232 open(FILE
, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
234 # Read the stored error message.
235 $errormessage = <FILE
>;
240 # Delete the file, which is now not longer required.
241 unlink($IDS::storederrorfile
);
244 ## Grab all available rules and store them in the idsrules hash.
246 # Open rules directory and do a directory listing.
247 opendir(DIR
, $IDS::rulespath
) or die $!;
248 # Loop through the direcory.
249 while (my $file = readdir(DIR
)) {
251 # We only want files.
252 next unless (-f
"$IDS::rulespath/$file");
254 # Ignore empty files.
255 next if (-z
"$IDS::rulespath/$file");
257 # Use a regular expression to find files ending in .rules
258 next unless ($file =~ m/\.rules$/);
260 # Ignore files which are not read-able.
261 next unless (-R
"$IDS::rulespath/$file");
263 # Skip whitelist rules file.
264 next if( $file eq "whitelist.rules");
266 # Call subfunction to read-in rulefile and add rules to
268 &readrulesfile
("$file");
273 # Gather used rulefiles.
275 # Check if the file for activated rulefiles is not empty.
276 if(-f
$IDS::used_rulefiles_file
) {
277 # Open the file for used rulefile and read-in content.
278 open(FILE
, $IDS::used_rulefiles_file
) or die "Could not open $IDS::used_rulefiles_file. $!\n";
286 # Loop through the array.
287 foreach my $line (@lines) {
292 next if ($line =~ /\#/);
295 next if ($line =~ /^\s*$/);
297 # Gather rule sid and message from the ruleline.
298 if ($line =~ /.*- (.*)/) {
301 # Check if the current rulefile exists in the %idsrules hash.
302 # If not, the file probably does not exist anymore or contains
304 if($idsrules{$rulefile}) {
305 # Add the rulefile state to the %idsrules hash.
306 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
312 # Save ruleset configuration.
313 if ($cgiparams{'RULESET'} eq $Lang::tr
{'save'}) {
317 # Read-in current (old) IDS settings.
318 &General
::readhash
("$IDS::rules_settings_file", \
%oldsettings);
320 # Get all available ruleset locations.
321 &General
::readhash
("$IDS::rulesetsourcesfile", \
%rulesetsources);
323 # Prevent form name from been stored in conf file.
324 delete $cgiparams{'RULESET'};
326 # Grab the URL based on the choosen vendor.
327 my $url = $rulesetsources{$cgiparams{'RULES'}};
329 # Check if the choosen vendor (URL) requires an subscription/oinkcode.
330 if ($url =~ /\<oinkcode\>/ ) {
331 # Check if an subscription/oinkcode has been provided.
332 if ($cgiparams{'OINKCODE'}) {
333 # Check if the oinkcode contains unallowed chars.
334 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
335 $errormessage = $Lang::tr
{'invalid input for oink code'};
338 # Print an error message, that an subsription/oinkcode is required for this
340 $errormessage = $Lang::tr
{'ids oinkcode required'};
344 # Go on if there are no error messages.
345 if (!$errormessage) {
346 # Store settings into settings file.
347 &General
::writehash
("$IDS::rules_settings_file", \
%cgiparams);
349 # Check if the the automatic rule update hass been touched.
350 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldsettings{'AUTOUPDATE_INTERVAL'}) {
351 # Call suricatactrl to set the new interval.
352 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
355 # Check if a ruleset is present - if not or the source has been changed download it.
356 if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) {
357 # Check if the red device is active.
358 unless (-e
"${General::swroot}/red/active") {
359 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
362 # Check if enough free disk space is availabe.
363 if(&IDS
::checkdiskspace
()) {
364 $errormessage = "$Lang::tr{'not enough disk space'}";
367 # Check if any errors happend.
368 unless ($errormessage) {
369 # Lock the webpage and print notice about downloading
371 &working_notice
("$Lang::tr{'ids working'}");
373 # Write the modify sid's file and pass the taken ruleaction.
374 &IDS
::write_modify_sids_file
();
376 # Call subfunction to download the ruleset.
377 if(&IDS
::downloadruleset
()) {
378 $errormessage = $Lang::tr
{'could not download latest updates'};
380 # Call function to store the errormessage.
381 &IDS
::_store_error_message
($errormessage);
383 # Call subfunction to launch oinkmaster.
387 # Check if the IDS is running.
388 if(&IDS
::ids_is_running
()) {
389 # Call suricatactrl to stop the IDS - because of the changed
390 # ruleset - the use has to configure it before suricata can be
392 &IDS
::call_suricatactrl
("stop");
395 # Perform a reload of the page.
402 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'ids apply'}) {
403 # Arrays to store which rulefiles have been enabled and will be used.
404 my @enabled_rulefiles;
406 # Hash to store the user-enabled and disabled sids.
407 my %enabled_disabled_sids;
409 # Loop through the hash of idsrules.
410 foreach my $rulefile(keys %idsrules) {
411 # Check if the rulefile is enabled.
412 if ($cgiparams{$rulefile} eq "on") {
413 # Add rulefile to the array of enabled rulefiles.
414 push(@enabled_rulefiles, $rulefile);
416 # Drop item from cgiparams hash.
417 delete $cgiparams{$rulefile};
421 # Read-in the files for enabled/disabled sids.
422 # This will be done by calling the read_enabled_disabled_sids_file function two times
423 # and merge the returned hashes together into the enabled_disabled_sids hash.
424 %enabled_disabled_sids = (
425 &read_enabled_disabled_sids_file
($IDS::disabled_sids_file
),
426 &read_enabled_disabled_sids_file
($IDS::enabled_sids_file
));
428 # Loop through the hash of idsrules.
429 foreach my $rulefile (keys %idsrules) {
430 # Loop through the single rules of the rulefile.
431 foreach my $sid (keys %{$idsrules{$rulefile}}) {
432 # Skip the current sid if it is not numeric.
433 next unless ($sid =~ /\d+/ );
435 # Check if there exists a key in the cgiparams hash for this sid.
436 if (exists($cgiparams{$sid})) {
437 # Look if the rule is disabled.
438 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
439 # Check if the state has been set to 'on'.
440 if ($cgiparams{$sid} eq "on") {
441 # Add/Modify the sid to/in the enabled_disabled_sids hash.
442 $enabled_disabled_sids{$sid} = "enabled";
444 # Drop item from cgiparams hash.
445 delete $cgiparams{$rulefile}{$sid};
449 # Look if the rule is enabled.
450 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
451 # Check if the state is 'on' and should be disabled.
452 # In this case there is no entry
453 # for the sid in the cgiparams hash.
454 # Add/Modify it to/in the enabled_disabled_sids hash.
455 $enabled_disabled_sids{$sid} = "disabled";
457 # Drop item from cgiparams hash.
458 delete $cgiparams{$rulefile}{$sid};
464 # Open enabled sid's file for writing.
465 open(ENABLED_FILE
, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
467 # Open disabled sid's file for writing.
468 open(DISABLED_FILE
, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
470 # Write header to the files.
471 print ENABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
472 print DISABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
474 # Check if the hash for enabled/disabled files contains any entries.
475 if (%enabled_disabled_sids) {
476 # Loop through the hash.
477 foreach my $sid (keys %enabled_disabled_sids) {
478 # Check if the sid is enabled.
479 if ($enabled_disabled_sids{$sid} eq "enabled") {
480 # Print the sid to the enabled_sids file.
481 print ENABLED_FILE
"enablesid $sid\n";
482 # Check if the sid is disabled.
483 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
484 # Print the sid to the disabled_sids file.
485 print DISABLED_FILE
"disablesid $sid\n";
486 # Something strange happende - skip the current sid.
493 # Close file for enabled_sids after writing.
496 # Close file for disabled_sids after writing.
497 close(DISABLED_FILE
);
499 # Call function to generate and write the used rulefiles file.
500 &IDS
::write_used_rulefiles_file
(@enabled_rulefiles);
502 # Lock the webpage and print message.
503 &working_notice
("$Lang::tr{'ids apply ruleset changes'}");
505 # Call oinkmaster to alter the ruleset.
508 # Check if the IDS is running.
509 if(&IDS
::ids_is_running
()) {
510 # Call suricatactrl to perform a reload.
511 &IDS
::call_suricatactrl
("reload");
517 # Download new ruleset.
518 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'update ruleset'}) {
519 # Check if the red device is active.
520 unless (-e
"${General::swroot}/red/active") {
521 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
524 # Check if enought free disk space is availabe.
525 if(&IDS
::checkdiskspace
()) {
526 $errormessage = "$Lang::tr{'not enough disk space'}";
529 # Check if any errors happend.
530 unless ($errormessage) {
531 # Lock the webpage and print notice about downloading
533 &working_notice
("$Lang::tr{'ids download new ruleset'}");
535 # Call subfunction to download the ruleset.
536 if(&IDS
::downloadruleset
()) {
537 $errormessage = $Lang::tr
{'could not download latest updates'};
539 # Call function to store the errormessage.
540 &IDS
::_store_error_message
($errormessage);
542 # Preform a reload of the page.
545 # Call subfunction to launch oinkmaster.
548 # Check if the IDS is running.
549 if(&IDS
::ids_is_running
()) {
550 # Call suricatactrl to perform a reload.
551 &IDS
::call_suricatactrl
("reload");
554 # Perform a reload of the page.
559 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
562 my $monitored_zones = 0;
564 # Read-in current (old) IDS settings.
565 &General
::readhash
("$IDS::ids_settings_file", \
%oldidssettings);
567 # Prevent form name from been stored in conf file.
568 delete $cgiparams{'IDS'};
570 # Check if the IDS should be enabled.
571 if ($cgiparams{'ENABLE_IDS'} eq "on") {
572 # Check if any ruleset is available. Otherwise abort and display an error.
574 $errormessage = $Lang::tr
{'ids no ruleset available'};
577 # Loop through the array of available interfaces.
578 foreach my $zone (@network_zones) {
579 # Convert interface name into upper case.
580 my $zone_upper = uc($zone);
582 # Check if the IDS is enabled for this interaces.
583 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
589 # Check if at least one zone should be monitored, or show an error.
590 unless ($monitored_zones >= 1) {
591 $errormessage = $Lang::tr
{'ids no network zone'};
595 # Go on if there are no error messages.
596 if (!$errormessage) {
597 # Store settings into settings file.
598 &General
::writehash
("$IDS::ids_settings_file", \
%cgiparams);
601 # Generate file to store the home net.
602 &IDS
::generate_home_net_file
();
604 # Generate file to the store the DNS servers.
605 &IDS
::generate_dns_servers_file
();
607 # Write the modify sid's file and pass the taken ruleaction.
608 &IDS
::write_modify_sids_file
();
610 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
611 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
612 # Check if a ruleset exists.
614 # Lock the webpage and print message.
615 &working_notice
("$Lang::tr{'ids working'}");
617 # Call oinkmaster to alter the ruleset.
620 # Set reload_page to "True".
625 # Check if the IDS currently is running.
626 if(&IDS
::ids_is_running
()) {
627 # Check if ENABLE_IDS is set to on.
628 if($cgiparams{'ENABLE_IDS'} eq "on") {
629 # Call suricatactrl to perform a reload of suricata.
630 &IDS
::call_suricatactrl
("reload");
632 # Call suricatactrl to stop suricata.
633 &IDS
::call_suricatactrl
("stop");
636 # Call suricatactrl to start suricata.
637 &IDS
::call_suricatactrl
("start");
640 # Check if the page should be reloaded.
642 # Perform a reload of the page.
647 # Read-in idssettings and rulesetsettings
648 &General
::readhash
("$IDS::ids_settings_file", \
%idssettings);
649 &General
::readhash
("$IDS::rules_settings_file", \
%rulessettings);
651 # If no autoupdate intervall has been configured yet, set default value.
652 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
653 # Set default to "weekly".
654 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
657 # Read-in ignored hosts.
658 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
660 $checked{'ENABLE_IDS'}{'off'} = '';
661 $checked{'ENABLE_IDS'}{'on'} = '';
662 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
663 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
664 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
665 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
666 $selected{'RULES'}{'nothing'} = '';
667 $selected{'RULES'}{'community'} = '';
668 $selected{'RULES'}{'emerging'} = '';
669 $selected{'RULES'}{'registered'} = '';
670 $selected{'RULES'}{'subscripted'} = '';
671 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
672 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
673 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
674 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
675 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
677 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
682 # Java script variable declaration for show and hide.
683 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
684 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
687 // Java Script function to show/hide the text input field for
688 // Oinkcode/Subscription code.
689 var update_code = function() {
690 if(\$('#RULES').val() == 'registered') {
692 } else if(\$('#RULES').val() == 'subscripted') {
694 } else if(\$('#RULES').val() == 'emerging_pro') {
701 // JQuery function to call corresponding function when
702 // the ruleset is changed or the page is loaded for showing/hiding
704 \$(document).ready(function() {
705 \$('#RULES').change(update_code);
709 // Tiny java script function to show/hide the rules
710 // of a given category.
711 function showhide(tblname) {
712 \$("#" + tblname).toggle();
714 // Get current content of the span element.
715 var content = document.getElementById("span_" + tblname);
717 if (content.innerHTML === show) {
718 content.innerHTML = hide;
720 content.innerHTML = show;
727 &Header
::openbigbox
('100%', 'left', '', $errormessage);
730 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
731 print "<class name='base'>$errormessage\n";
732 print " </class>\n";
736 # Draw current state of the IDS
737 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
739 # Check if the IDS is running and obtain the process-id.
740 my $pid = &IDS
::ids_is_running
();
742 # Display some useful information, if suricata daemon is running.
744 # Gather used memory.
745 my $memory = &get_memory_usage
($pid);
748 <table width='95%' cellspacing='0' class='tbl'>
750 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
754 <td class='base'>$Lang::tr{'guardian daemon'}</td>
755 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
759 <td class='base'></td>
760 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
761 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
765 <td class='base'></td>
766 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
767 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
772 # Otherwise display a hint that the service is not launched.
774 <table width='95%' cellspacing='0' class='tbl'>
776 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
780 <td class='base'>$Lang::tr{'guardian daemon'}</td>
781 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
787 # Only show this area, if a ruleset is present.
792 <br><br><h2>$Lang::tr{'settings'}</h2>
794 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
795 <table width='100%' border='0'>
797 <td class='base' colspan='2'>
798 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}> $Lang::tr{'ids enable'}
801 <td class='base' colspan='2'>
802 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}> $Lang::tr{'ids monitor traffic only'}
814 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
821 # Loop through the array of available networks and print config options.
822 foreach my $zone (@network_zones) {
826 # Convert current zone name to upper case.
827 my $zone_upper = uc($zone);
830 my $zone_name = $zone;
832 # Dirty hack to get the correct language string for the red zone.
833 if ($zone eq "red") {
837 # Grab checkbox status from settings hash.
838 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
839 $checked_input = "checked = 'checked'";
842 print "<td class='base' width='25%'>\n";
843 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
844 print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
856 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
867 # Draw elements for ruleset configuration.
868 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
871 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
872 <table width='100%' border='0'>
874 <td><b>$Lang::tr{'ids rules update'}</b></td>
875 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
879 <td><select name='RULES' id='RULES'>
880 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
881 <option value='emerging_pro' $selected{'RULES'}{'emerging_pro'} >$Lang::tr{'emerging pro rules'}</option>
882 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
883 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
884 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
889 <select name='AUTOUPDATE_INTERVAL'>
890 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
891 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
892 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
898 <td colspan='2'><br><br></td>
901 <tr style='display:none' id='code'>
902 <td colspan='2'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
911 # Show the "Update Ruleset"-Button only if a ruleset has been downloaded yet and automatic updates are disabled.
912 if ((%idsrules) && ($rulessettings{'AUTOUPDATE_INTERVAL'} eq "off")) {
913 # Display button to update the ruleset.
914 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
917 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
929 # Whitelist / Ignorelist
931 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ignored hosts'});
936 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
937 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
938 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
941 # Check if some hosts have been added to be ignored.
942 if (keys (%ignored)) {
945 # Loop through all entries of the hash.
946 while( (my $key) = each %ignored) {
947 # Assign data array positions to some nice variable names.
948 my $address = $ignored{$key}[0];
949 my $remark = $ignored{$key}[1];
950 my $status = $ignored{$key}[2];
952 # Check if the key (id) number is even or not.
953 if ($cgiparams{'ID'} eq $key) {
954 $col="bgcolor='${Header::colouryellow}'";
956 $col="bgcolor='$color{'color22'}'";
958 $col="bgcolor='$color{'color20'}'";
961 # Choose icon for the checkbox.
965 # Check if the status is enabled and select the correct image and description.
966 if ($status eq 'enabled' ) {
968 $gdesc = $Lang::tr
{'click to disable'};
971 $gdesc = $Lang::tr
{'click to enable'};
976 <td width='20%' class='base' $col>$address</td>
977 <td width='65%' class='base' $col>$remark</td>
979 <td align='center' $col>
980 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
981 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
982 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
983 <input type='hidden' name='ID' value='$key' />
987 <td align='center' $col>
988 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
989 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
990 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
991 <input type='hidden' name='ID' value='$key' />
995 <td align='center' $col>
996 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
997 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
998 <input type='hidden' name='ID' value='$key'>
999 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1006 # Print notice that currently no hosts are ignored.
1008 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1014 # Section to add new elements or edit existing ones.
1020 <div align='center'>
1021 <table width='100%'>
1024 # Assign correct headline and button text.
1029 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1030 if ($cgiparams{'ID'} ne '') {
1031 $buttontext = $Lang::tr
{'update'};
1032 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1034 # Grab address and remark for the given key.
1035 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1036 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1038 $buttontext = $Lang::tr
{'add'};
1039 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1043 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1044 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1046 <td width='30%'>$Lang::tr{'ip address'}: </td>
1047 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1049 <td width='30%'>$Lang::tr{'remark'}: </td>
1050 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1051 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1058 &Header
::closebox
();
1060 # Only show the section for configuring the ruleset if one is present.
1062 # Load neccessary perl modules for file stat and to format the timestamp.
1064 use POSIX
qw( strftime );
1066 # Call stat on the rulestarball.
1067 my $stat = stat("$IDS::rulestarball");
1069 # Get timestamp the file creation.
1070 my $mtime = $stat->mtime;
1072 # Convert into human read-able format.
1073 my $rulesdate = strftime
('%Y-%m-%d %H:%M:%S', localtime($mtime));
1075 &Header
::openbox
('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'} ($rulesdate)" );
1077 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1079 # Output display table for rule files
1080 print "<table width='100%'>\n";
1082 # Loop over each rule file
1083 foreach my $rulefile (sort keys(%idsrules)) {
1084 my $rulechecked = '';
1086 # Check if rule file is enabled
1087 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1088 $rulechecked = 'CHECKED';
1091 # Convert rulefile name into category name.
1092 my $categoryname = &_rulefile_to_category
($rulefile);
1094 # Table and rows for the rule files.
1096 print"<td class='base' width='5%'>\n";
1097 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1099 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1100 print"<td class='base' width='5%' align='right'>\n";
1101 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1105 # Rows which will be hidden per default and will contain the single rules.
1106 print"<tr style='display:none' id='$categoryname'>\n";
1107 print"<td colspan='3'>\n";
1114 # New table for the single rules.
1115 print "<table width='100%'>\n";
1117 # Loop over rule file rules
1118 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1120 my $ruledefchecked = '';
1122 # Skip rulefile itself.
1123 next if ($sid eq "Rulefile");
1125 # If 2 rules have been displayed, start a new row
1126 if (($lines % 2) == 0) {
1127 print "</tr><tr>\n";
1129 # Increase rows by once.
1135 $col="bgcolor='$color{'color20'}'";
1137 $col="bgcolor='$color{'color22'}'";
1141 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1142 $ruledefchecked = 'CHECKED';
1145 # Create rule checkbox and display rule description
1146 print "<td class='base' width='5%' align='right' $col>\n";
1147 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1149 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1151 # Increment rule count
1155 # If do not have a second rule for row, create empty cell
1156 if (($lines % 2) != 0) {
1157 print "<td class='base'></td>";
1160 # Close display table
1161 print "</tr></table></td></tr>";
1164 # Close display table
1168 <table width='100%'>
1170 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'></td>
1176 &Header
::closebox
();
1179 &Header
::closebigbox
();
1180 &Header
::closepage
();
1183 ## A function to display a notice, to lock the webpage and
1184 ## tell the user which action currently will be performed.
1186 sub working_notice
($) {
1189 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1190 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1191 &Header
::openbox
( 'Waiting', 1,);
1195 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1200 &Header
::closebox
();
1201 &Header
::closebigbox
();
1202 &Header
::closepage
();
1206 ## A tiny function to perform a reload of the webpage after one second.
1209 print "<meta http-equiv='refresh' content='1'>\n";
1216 ## Private function to read-in and parse rules of a given rulefile.
1218 ## The given file will be read, parsed and all valid rules will be stored by ID,
1219 ## message/description and it's state in the idsrules hash.
1221 sub readrulesfile
($) {
1222 my $rulefile = shift;
1224 # Open rule file and read in contents
1225 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1227 # Store file content in an array.
1228 my @lines = <RULEFILE
>;
1233 # Loop over rule file contents
1234 foreach my $line (@lines) {
1235 # Remove whitespaces.
1239 next if ($line =~ /^\s*$/);
1245 # Gather rule sid and message from the ruleline.
1246 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1250 # Check if a rule has been found.
1252 # Add rule to the idsrules hash.
1253 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1255 # Grab status of the rule. Check if ruleline starts with a "dash".
1256 if ($line =~ /^\#/) {
1257 # If yes, the rule is disabled.
1258 $idsrules{$rulefile}{$sid}{'State'} = "off";
1260 # Otherwise the rule is enabled.
1261 $idsrules{$rulefile}{$sid}{'State'} = "on";
1269 ## Function to get the used memory of a given process-id.
1271 sub get_memory_usage
($) {
1276 # Try to open the status file for the given process-id on the pseudo
1278 if (open(FILE
, "/proc/$pid/status")) {
1279 # Loop through the entire file.
1281 # Splitt current line content and store them into variables.
1282 my ($key, $value) = split(":", $_, 2);
1284 # Check if the current key is the one which contains the memory usage.
1285 # The wanted one is VmRSS which contains the Real-memory (resident set)
1286 # of the entire process.
1287 if ($key eq "VmRSS") {
1288 # Found the memory usage add it to the memory variable.
1296 # Close file handle.
1299 # Return memory usage.
1303 # If the file could not be open, return nothing.
1308 ## Function to read-in the given enabled or disables sids file.
1310 sub read_enabled_disabled_sids_file
($) {
1313 # Temporary hash to store the sids and their state. It will be
1314 # returned at the end of this function.
1317 # Open the given filename.
1318 open(FILE
, "$file") or die "Could not open $file. $!\n";
1320 # Loop through the file.
1326 next if ($_ =~ /^\s*$/);
1329 next if ($_ =~ /^\#/);
1331 # Splitt line into sid and state part.
1332 my ($state, $sid) = split(" ", $_);
1334 # Skip line if the sid is not numeric.
1335 next unless ($sid =~ /\d+/ );
1337 # Check if the sid was enabled.
1338 if ($state eq "enablesid") {
1339 # Add the sid and its state as enabled to the temporary hash.
1340 $temphash{$sid} = "enabled";
1341 # Check if the sid was disabled.
1342 } elsif ($state eq "disablesid") {
1343 # Add the sid and its state as disabled to the temporary hash.
1344 $temphash{$sid} = "disabled";
1345 # Invalid state - skip the current sid and state.
1359 ## Private function to convert a given rulefile to a category name.
1360 ## ( No file extension anymore and if the name contained a dot, it
1361 ## would be replaced by a underline sign.)
1363 sub _rulefile_to_category
($) {
1364 my ($filename) = @_;
1366 # Splitt the filename into single chunks and store them in a
1368 my @parts = split(/\./, $filename);
1370 # Return / Remove last element of the temporary array.
1371 # This removes the file extension.
1374 # Join together the single elements of the temporary array.
1375 # If these are more than one, use a "underline" for joining.
1376 my $category = join '_', @parts;
1378 # Return the converted filename.