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 any error has been stored.
214 if (-e
$IDS::storederrorfile
) {
215 # Open file to read in the stored error message.
216 open(FILE
, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
218 # Read the stored error message.
219 $errormessage = <FILE
>;
224 # Delete the file, which is now not longer required.
225 unlink($IDS::storederrorfile
);
228 ## Grab all available snort rules and store them in the idsrules hash.
230 # Open snort rules directory and do a directory listing.
231 opendir(DIR
, $IDS::rulespath
) or die $!;
232 # Loop through the direcory.
233 while (my $file = readdir(DIR
)) {
235 # We only want files.
236 next unless (-f
"$IDS::rulespath/$file");
238 # Ignore empty files.
239 next if (-z
"$IDS::rulespath/$file");
241 # Use a regular expression to find files ending in .rules
242 next unless ($file =~ m/\.rules$/);
244 # Ignore files which are not read-able.
245 next unless (-R
"$IDS::rulespath/$file");
247 # Skip whitelist rules file.
248 next if( $file eq "whitelist.rules");
250 # Call subfunction to read-in rulefile and add rules to
252 &readrulesfile
("$file");
257 # Gather used rulefiles.
259 # Check if the file for activated rulefiles is not empty.
260 if(-f
$IDS::used_rulefiles_file
) {
261 # Open the file for used rulefile and read-in content.
262 open(FILE
, $IDS::used_rulefiles_file
) or die "Could not open $IDS::used_rulefiles_file. $!\n";
270 # Loop through the array.
271 foreach my $line (@lines) {
276 next if ($line =~ /\#/);
279 next if ($line =~ /^\s*$/);
281 # Gather rule sid and message from the ruleline.
282 if ($line =~ /.*- (.*)/) {
285 # Check if the current rulefile exists in the %idsrules hash.
286 # If not, the file probably does not exist anymore or contains
288 if($idsrules{$rulefile}) {
289 # Add the rulefile state to the %idsrules hash.
290 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
296 # Save ruleset configuration.
297 if ($cgiparams{'RULESET'} eq $Lang::tr
{'save'}) {
300 # Read-in current (old) IDS settings.
301 &General
::readhash
("$IDS::rules_settings_file", \
%oldsettings);
303 # Prevent form name from been stored in conf file.
304 delete $cgiparams{'RULESET'};
306 # Check if an oinkcode has been provided.
307 if ($cgiparams{'OINKCODE'}) {
308 # Check if the oinkcode contains unallowed chars.
309 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
310 $errormessage = $Lang::tr
{'invalid input for oink code'};
314 # Go on if there are no error messages.
315 if (!$errormessage) {
316 # Store settings into settings file.
317 &General
::writehash
("$IDS::rules_settings_file", \
%cgiparams);
320 # Check if the the automatic rule update hass been touched.
321 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldsettings{'AUTOUPDATE_INTERVAL'}) {
322 # Call suricatactrl to set the new interval.
323 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
326 # Check if a ruleset is present - if not or the source has been changed download it.
327 unless ((%idsrules) || ($oldsettings{'RULES'} eq $cgiparams{'RULES'})) {
328 # Check if the red device is active.
329 unless (-e
"${General::swroot}/red/active") {
330 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
333 # Check if enought free disk space is availabe.
334 if(&IDS
::checkdiskspace
()) {
335 $errormessage = "$Lang::tr{'not enough disk space'}";
338 # Check if any errors happend.
339 unless ($errormessage) {
340 # Lock the webpage and print notice about downloading
342 &working_notice
("$Lang::tr{'snort working'}");
344 # Call subfunction to download the ruleset.
345 if(&IDS
::downloadruleset
()) {
346 $errormessage = $Lang::tr
{'could not download latest updates'};
348 # Call function to store the errormessage.
349 &IDS
::_store_error_message
($errormessage);
351 # Call subfunction to launch oinkmaster.
355 # Perform a reload of the page.
361 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'update'}) {
362 # Arrays to store which rulefiles have been enabled and will be used.
363 my @enabled_rulefiles;
365 # Hash to store the user-enabled and disabled sids.
366 my %enabled_disabled_sids;
368 # Loop through the hash of idsrules.
369 foreach my $rulefile(keys %idsrules) {
370 # Check if the rulefile is enabled.
371 if ($cgiparams{$rulefile} eq "on") {
372 # Add rulefile to the array of enabled rulefiles.
373 push(@enabled_rulefiles, $rulefile);
375 # Drop item from cgiparams hash.
376 delete $cgiparams{$rulefile};
380 # Read-in the files for enabled/disabled sids.
381 # This will be done by calling the read_enabled_disabled_sids_file function two times
382 # and merge the returned hashes together into the enabled_disabled_sids hash.
383 %enabled_disabled_sids = (
384 &read_enabled_disabled_sids_file
($IDS::disabled_sids_file
),
385 &read_enabled_disabled_sids_file
($IDS::enabled_sids_file
));
387 # Loop through the hash of idsrules.
388 foreach my $rulefile (keys %idsrules) {
389 # Loop through the single rules of the rulefile.
390 foreach my $sid (keys %{$idsrules{$rulefile}}) {
391 # Skip the current sid if it is not numeric.
392 next unless ($sid =~ /\d+/ );
394 # Check if there exists a key in the cgiparams hash for this sid.
395 if (exists($cgiparams{$sid})) {
396 # Look if the rule is disabled.
397 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
398 # Check if the state has been set to 'on'.
399 if ($cgiparams{$sid} eq "on") {
400 # Add/Modify the sid to/in the enabled_disabled_sids hash.
401 $enabled_disabled_sids{$sid} = "enabled";
403 # Drop item from cgiparams hash.
404 delete $cgiparams{$rulefile}{$sid};
408 # Look if the rule is enabled.
409 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
410 # Check if the state is 'on' and should be disabled.
411 # In this case there is no entry
412 # for the sid in the cgiparams hash.
413 # Add/Modify it to/in the enabled_disabled_sids hash.
414 $enabled_disabled_sids{$sid} = "disabled";
416 # Drop item from cgiparams hash.
417 delete $cgiparams{$rulefile}{$sid};
423 # Open enabled sid's file for writing.
424 open(ENABLED_FILE
, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
426 # Open disabled sid's file for writing.
427 open(DISABLED_FILE
, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
429 # Write header to the files.
430 print ENABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
431 print DISABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
433 # Check if the hash for enabled/disabled files contains any entries.
434 if (%enabled_disabled_sids) {
435 # Loop through the hash.
436 foreach my $sid (keys %enabled_disabled_sids) {
437 # Check if the sid is enabled.
438 if ($enabled_disabled_sids{$sid} eq "enabled") {
439 # Print the sid to the enabled_sids file.
440 print ENABLED_FILE
"enablesid $sid\n";
441 # Check if the sid is disabled.
442 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
443 # Print the sid to the disabled_sids file.
444 print DISABLED_FILE
"disablesid $sid\n";
445 # Something strange happende - skip the current sid.
452 # Close file for enabled_sids after writing.
455 # Close file for disabled_sids after writing.
456 close(DISABLED_FILE
);
458 # Call function to generate and write the used rulefiles file.
459 &IDS
::write_used_rulefiles_file
(@enabled_rulefiles);
461 # Lock the webpage and print message.
462 &working_notice
("$Lang::tr{'snort working'}");
464 # Call oinkmaster to alter the ruleset.
467 # Check if the IDS is running.
468 if(&IDS
::ids_is_running
()) {
469 # Call suricatactrl to perform a reload.
470 &IDS
::call_suricatactrl
("reload");
476 # Download new ruleset.
477 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'update ruleset'}) {
478 # Check if the red device is active.
479 unless (-e
"${General::swroot}/red/active") {
480 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
483 # Check if enought free disk space is availabe.
484 if(&IDS
::checkdiskspace
()) {
485 $errormessage = "$Lang::tr{'not enough disk space'}";
488 # Check if any errors happend.
489 unless ($errormessage) {
490 # Lock the webpage and print notice about downloading
492 &working_notice
("$Lang::tr{'snort working'}");
494 # Call subfunction to download the ruleset.
495 if(&IDS
::downloadruleset
()) {
496 $errormessage = $Lang::tr
{'could not download latest updates'};
498 # Call function to store the errormessage.
499 &IDS
::_store_error_message
($errormessage);
501 # Preform a reload of the page.
504 # Call subfunction to launch oinkmaster.
507 # Check if the IDS is running.
508 if(&IDS
::ids_is_running
()) {
509 # Call suricatactrl to perform a reload.
510 &IDS
::call_suricatactrl
("reload");
513 # Perform a reload of the page.
517 # Save snort settings.
518 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
521 my $monitored_zones = 0;
523 # Read-in current (old) IDS settings.
524 &General
::readhash
("$IDS::ids_settings_file", \
%oldidssettings);
526 # Prevent form name from been stored in conf file.
527 delete $cgiparams{'IDS'};
529 # Check if the IDS should be enabled.
530 if ($cgiparams{'ENABLE_IDS'} eq "on") {
531 # Check if any ruleset is available. Otherwise abort and display an error.
533 $errormessage = $Lang::tr
{'ids no ruleset available'};
536 # Loop through the array of available interfaces.
537 foreach my $zone (@network_zones) {
538 # Convert interface name into upper case.
539 my $zone_upper = uc($zone);
541 # Check if the IDS is enabled for this interaces.
542 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
548 # Check if at least one zone should be monitored, or show an error.
549 unless ($monitored_zones >= 1) {
550 $errormessage = $Lang::tr
{'ids no network zone'};
554 # Go on if there are no error messages.
555 if (!$errormessage) {
556 # Store settings into settings file.
557 &General
::writehash
("$IDS::ids_settings_file", \
%cgiparams);
560 # Generate file to store the home net.
561 &IDS
::generate_home_net_file
();
563 # Temporary variable to set the ruleaction.
564 # Default is "drop" to use suricata as IPS.
565 my $ruleaction="drop";
567 # Check if the traffic only should be monitored.
568 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} eq 'on') {
569 # Switch the ruleaction to "alert".
570 # Suricata acts as an IDS only.
574 # Write the modify sid's file and pass the taken ruleaction.
575 &IDS
::write_modify_sids_file
($ruleaction);
577 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
578 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
579 # Check if a ruleset exists.
581 # Lock the webpage and print message.
582 &working_notice
("$Lang::tr{'snort working'}");
584 # Call oinkmaster to alter the ruleset.
587 # Set reload_page to "True".
592 # Check if the IDS currently is running.
593 if(&IDS
::ids_is_running
()) {
594 # Check if ENABLE_IDS is set to on.
595 if($cgiparams{'ENABLE_IDS'} eq "on") {
596 # Call suricatactrl to perform a reload of suricata.
597 &IDS
::call_suricatactrl
("reload");
599 # Call suricatactrl to stop suricata.
600 &IDS
::call_suricatactrl
("stop");
603 # Call suricatactrl to start suricata.
604 &IDS
::call_suricatactrl
("start");
607 # Check if the page should be reloaded.
609 # Perform a reload of the page.
614 # Read-in idssettings and rulesetsettings
615 &General
::readhash
("$IDS::ids_settings_file", \
%idssettings);
616 &General
::readhash
("$IDS::rules_settings_file", \
%rulessettings);
618 # If no autoupdate intervall has been configured yet, set default value.
619 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
620 # Set default to "weekly".
621 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
624 # Read-in ignored hosts.
625 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
627 $checked{'ENABLE_IDS'}{'off'} = '';
628 $checked{'ENABLE_IDS'}{'on'} = '';
629 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
630 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
631 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
632 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
633 $selected{'RULES'}{'nothing'} = '';
634 $selected{'RULES'}{'community'} = '';
635 $selected{'RULES'}{'emerging'} = '';
636 $selected{'RULES'}{'registered'} = '';
637 $selected{'RULES'}{'subscripted'} = '';
638 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
639 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
640 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
641 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
642 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
644 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
649 # Java script variable declaration for show and hide.
650 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
651 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
654 // JQuery function to show/hide the text input field for
655 // Oinkcode/Subscription code.
657 \$('#RULES').change(function(){
658 if(\$('#RULES').val() == 'registered') {
660 } else if(\$('#RULES').val() == 'subscripted') {
662 } else if(\$('#RULES').val() == 'emerging_pro') {
670 // Tiny java script function to show/hide the rules
671 // of a given category.
672 function showhide(tblname) {
673 \$("#" + tblname).toggle();
675 // Get current content of the span element.
676 var content = document.getElementById("span_" + tblname);
678 if (content.innerHTML === show) {
679 content.innerHTML = hide;
681 content.innerHTML = show;
688 &Header
::openbigbox
('100%', 'left', '', $errormessage);
691 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
692 print "<class name='base'>$errormessage\n";
693 print " </class>\n";
697 # Draw current state of the IDS
698 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
700 # Check if the IDS is running and obtain the process-id.
701 my $pid = &IDS
::ids_is_running
();
703 # Display some useful information, if suricata daemon is running.
705 # Gather used memory.
706 my $memory = &get_memory_usage
($pid);
709 <table width='95%' cellspacing='0' class='tbl'>
711 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
715 <td class='base'>$Lang::tr{'guardian daemon'}</td>
716 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
720 <td class='base'></td>
721 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
722 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
726 <td class='base'></td>
727 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
728 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
733 # Otherwise display a hint that the service is not launched.
735 <table width='95%' cellspacing='0' class='tbl'>
737 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
741 <td class='base'>$Lang::tr{'guardian daemon'}</td>
742 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
750 # Check if a ruleset allready has been downloaded.
751 if ( -f
"$IDS::rulestarball"){
752 # Call stat on the filename to obtain detailed information.
753 my @Info = stat("$IDS::rulestarball");
755 # Grab details about the creation time.
756 $rulesdate = localtime($Info[9]);
759 # Only show this area, if a ruleset is present.
764 <br><br><h2>$Lang::tr{'settings'}</h2>
766 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
767 <table width='100%' border='0'>
769 <td class='base' colspan='2'>
770 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
773 <td class='base' colspan='2'>
774 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>$Lang::tr{'ids monitor traffic only'}
786 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
793 # Loop through the array of available networks and print config options.
794 foreach my $zone (@network_zones) {
798 # Convert current zone name to upper case.
799 my $zone_upper = uc($zone);
802 my $zone_name = $zone;
804 # Dirty hack to get the correct language string for the red zone.
805 if ($zone eq "red") {
809 # Grab checkbox status from settings hash.
810 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
811 $checked_input = "checked = 'checked'";
814 print "<td class='base' width='25%'>\n";
815 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
816 print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
828 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
839 # Draw elements for ruleset configuration.
840 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
843 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
844 <table width='100%' border='0'>
846 <td><b>$Lang::tr{'ids rules update'}</b></td>
847 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
851 <td><select name='RULES' id='RULES'>
852 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
853 <option value='emerging_pro' $selected{'RULES'}{'emerging_pro'} >$Lang::tr{'emerging pro rules'}</option>
854 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
855 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
856 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
861 <select name='AUTOUPDATE_INTERVAL'>
862 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option>
863 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option>
864 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option>
870 <td colspan='2'><br><br></td>
873 <tr style='display:none' id='code'>
874 <td colspan='2'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
883 # Check if a ruleset has been downloaded yet.
885 # Display button to update the ruleset.
886 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
889 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
901 # Whitelist / Ignorelist
903 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
908 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
909 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
910 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
913 # Check if some hosts have been added to be ignored.
914 if (keys (%ignored)) {
917 # Loop through all entries of the hash.
918 while( (my $key) = each %ignored) {
919 # Assign data array positions to some nice variable names.
920 my $address = $ignored{$key}[0];
921 my $remark = $ignored{$key}[1];
922 my $status = $ignored{$key}[2];
924 # Check if the key (id) number is even or not.
925 if ($cgiparams{'ID'} eq $key) {
926 $col="bgcolor='${Header::colouryellow}'";
928 $col="bgcolor='$color{'color22'}'";
930 $col="bgcolor='$color{'color20'}'";
933 # Choose icon for the checkbox.
937 # Check if the status is enabled and select the correct image and description.
938 if ($status eq 'enabled' ) {
940 $gdesc = $Lang::tr
{'click to disable'};
943 $gdesc = $Lang::tr
{'click to enable'};
948 <td width='20%' class='base' $col>$address</td>
949 <td width='65%' class='base' $col>$remark</td>
951 <td align='center' $col>
952 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
953 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
954 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
955 <input type='hidden' name='ID' value='$key' />
959 <td align='center' $col>
960 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
961 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
962 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
963 <input type='hidden' name='ID' value='$key' />
967 <td align='center' $col>
968 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
969 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
970 <input type='hidden' name='ID' value='$key'>
971 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
978 # Print notice that currently no hosts are ignored.
980 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
986 # Section to add new elements or edit existing ones.
996 # Assign correct headline and button text.
1001 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1002 if ($cgiparams{'ID'} ne '') {
1003 $buttontext = $Lang::tr
{'update'};
1004 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1006 # Grab address and remark for the given key.
1007 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1008 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1010 $buttontext = $Lang::tr
{'add'};
1011 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1015 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1016 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1018 <td width='30%'>$Lang::tr{'ip address'}: </td>
1019 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1021 <td width='30%'>$Lang::tr{'remark'}: </td>
1022 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1023 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1030 &Header
::closebox
();
1032 # Only show the section for configuring the ruleset if one is present.
1034 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
1036 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1038 # Output display table for rule files
1039 print "<table width='100%'>\n";
1041 # Loop over each rule file
1042 foreach my $rulefile (sort keys(%idsrules)) {
1043 my $rulechecked = '';
1045 # Check if rule file is enabled
1046 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1047 $rulechecked = 'CHECKED';
1050 # Convert rulefile name into category name.
1051 my $categoryname = &_rulefile_to_category
($rulefile);
1053 # Table and rows for the rule files.
1055 print"<td class='base' width='5%'>\n";
1056 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1058 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1059 print"<td class='base' width='5%' align='right'>\n";
1060 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1064 # Rows which will be hidden per default and will contain the single rules.
1065 print"<tr style='display:none' id='$categoryname'>\n";
1066 print"<td colspan='3'>\n";
1073 # New table for the single rules.
1074 print "<table width='100%'>\n";
1076 # Loop over rule file rules
1077 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1079 my $ruledefchecked = '';
1081 # Skip rulefile itself.
1082 next if ($sid eq "Rulefile");
1084 # If 2 rules have been displayed, start a new row
1085 if (($lines % 2) == 0) {
1086 print "</tr><tr>\n";
1088 # Increase rows by once.
1094 $col="bgcolor='$color{'color20'}'";
1096 $col="bgcolor='$color{'color22'}'";
1100 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1101 $ruledefchecked = 'CHECKED';
1104 # Create rule checkbox and display rule description
1105 print "<td class='base' width='5%' align='right' $col>\n";
1106 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1108 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1110 # Increment rule count
1114 # If do not have a second rule for row, create empty cell
1115 if (($lines % 2) != 0) {
1116 print "<td class='base'></td>";
1119 # Close display table
1120 print "</tr></table></td></tr>";
1123 # Close display table
1127 <table width='100%'>
1129 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
1130 <!-- space for future online help link -->
1137 &Header
::closebox
();
1140 &Header
::closebigbox
();
1141 &Header
::closepage
();
1144 ## A function to display a notice, to lock the webpage and
1145 ## tell the user which action currently will be performed.
1147 sub working_notice
($) {
1150 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1151 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1152 &Header
::openbox
( 'Waiting', 1,);
1156 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1161 &Header
::closebox
();
1162 &Header
::closebigbox
();
1163 &Header
::closepage
();
1167 ## A tiny function to perform a reload of the webpage after one second.
1170 print "<meta http-equiv='refresh' content='1'>\n";
1177 ## Private function to read-in and parse rules of a given rulefile.
1179 ## The given file will be read, parsed and all valid rules will be stored by ID,
1180 ## message/description and it's state in the idsrules hash.
1182 sub readrulesfile
($) {
1183 my $rulefile = shift;
1185 # Open rule file and read in contents
1186 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1188 # Store file content in an array.
1189 my @lines = <RULEFILE
>;
1194 # Loop over rule file contents
1195 foreach my $line (@lines) {
1196 # Remove whitespaces.
1200 next if ($line =~ /^\s*$/);
1206 # Gather rule sid and message from the ruleline.
1207 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1211 # Check if a rule has been found.
1213 # Add rule to the idsrules hash.
1214 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1216 # Grab status of the rule. Check if ruleline starts with a "dash".
1217 if ($line =~ /^\#/) {
1218 # If yes, the rule is disabled.
1219 $idsrules{$rulefile}{$sid}{'State'} = "off";
1221 # Otherwise the rule is enabled.
1222 $idsrules{$rulefile}{$sid}{'State'} = "on";
1230 ## Function to get the used memory of a given process-id.
1232 sub get_memory_usage
($) {
1237 # Try to open the status file for the given process-id on the pseudo
1239 if (open(FILE
, "/proc/$pid/status")) {
1240 # Loop through the entire file.
1242 # Splitt current line content and store them into variables.
1243 my ($key, $value) = split(":", $_, 2);
1245 # Check if the current key is the one which contains the memory usage.
1246 # The wanted one is VmRSS which contains the Real-memory (resident set)
1247 # of the entire process.
1248 if ($key eq "VmRSS") {
1249 # Found the memory usage add it to the memory variable.
1257 # Close file handle.
1260 # Return memory usage.
1264 # If the file could not be open, return nothing.
1269 ## Function to read-in the given enabled or disables sids file.
1271 sub read_enabled_disabled_sids_file
($) {
1274 # Temporary hash to store the sids and their state. It will be
1275 # returned at the end of this function.
1278 # Open the given filename.
1279 open(FILE
, "$file") or die "Could not open $file. $!\n";
1281 # Loop through the file.
1287 next if ($_ =~ /^\s*$/);
1290 next if ($_ =~ /^\#/);
1292 # Splitt line into sid and state part.
1293 my ($state, $sid) = split(" ", $_);
1295 # Skip line if the sid is not numeric.
1296 next unless ($sid =~ /\d+/ );
1298 # Check if the sid was enabled.
1299 if ($state eq "enablesid") {
1300 # Add the sid and its state as enabled to the temporary hash.
1301 $temphash{$sid} = "enabled";
1302 # Check if the sid was disabled.
1303 } elsif ($state eq "disablesid") {
1304 # Add the sid and its state as disabled to the temporary hash.
1305 $temphash{$sid} = "disabled";
1306 # Invalid state - skip the current sid and state.
1320 ## Private function to convert a given rulefile to a category name.
1321 ## ( No file extension anymore and if the name contained a dot, it
1322 ## would be replaced by a underline sign.)
1324 sub _rulefile_to_category
($) {
1325 my ($filename) = @_;
1327 # Splitt the filename into single chunks and store them in a
1329 my @parts = split(/\./, $filename);
1331 # Return / Remove last element of the temporary array.
1332 # This removes the file extension.
1335 # Join together the single elements of the temporary array.
1336 # If these are more than one, use a "underline" for joining.
1337 my $category = join '_', @parts;
1339 # Return the converted filename.