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 = ();
41 my %used_providers=();
47 # Read-in main settings, for language, theme and colors.
48 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
49 &General
::readhash
("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \
%color);
51 # Get the available network zones, based on the config type of the system and store
52 # the list of zones in an array.
53 my @network_zones = &Network
::get_available_network_zones
();
55 # Check if openvpn is started and add it to the array of network zones.
56 if ( -e
"/var/run/openvpn.pid") {
57 push(@network_zones, "ovpn");
62 # Create files if they does not exist yet.
63 &IDS
::check_and_create_filelayout
();
65 # Hash which contains the colour code of a network zone.
67 'red' => $Header::colourred
,
68 'green' => $Header::colourgreen
,
69 'blue' => $Header::colourblue
,
70 'orange' => $Header::colourorange
,
71 'ovpn' => $Header::colourovpn
74 &Header
::showhttpheaders
();
77 &Header
::getcgihash
(\
%cgiparams);
79 ## Add/edit an entry to the ignore file.
81 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'})) {
83 # Check if any input has been performed.
84 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
86 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
87 if ((!&General
::validip
($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
88 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
91 $errormessage = "$Lang::tr{'guardian empty input'}";
94 # Go further if there was no error.
95 if ($errormessage eq '') {
100 # Assign hash values.
101 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
102 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
104 # Read-in ignoredfile.
105 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
107 # Check if we should edit an existing entry and got an ID.
108 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
109 # Assin the provided id.
110 $id = $cgiparams{'ID'};
112 # Undef the given ID.
113 undef($cgiparams{'ID'});
115 # Grab the configured status of the corresponding entry.
116 $status = $ignored{$id}[2];
118 # Each newly added entry automatically should be enabled.
121 # Generate the ID for the new entry.
123 # Sort the keys by their ID and store them in an array.
124 my @keys = sort { $a <=> $b } keys %ignored;
126 # Reverse the key array.
127 my @reversed = reverse(@keys);
129 # Obtain the last used id.
130 my $last_id = @reversed[0];
132 # Increase the last id by one and use it as id for the new entry.
136 # Add/Modify the entry to/in the ignored hash.
137 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
139 # Write the changed ignored hash to the ignored file.
140 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
142 # Regenerate the ignore file.
143 &IDS
::generate_ignore_file
();
146 # Check if the IDS is running.
147 if(&IDS
::ids_is_running
()) {
148 # Call suricatactrl to perform a reload.
149 &IDS
::call_suricatactrl
("reload");
152 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
155 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'toggle enable disable'}) {
158 # Only go further, if an ID has been passed.
159 if ($cgiparams{'ID'}) {
160 # Assign the given ID.
161 my $id = $cgiparams{'ID'};
163 # Undef the given ID.
164 undef($cgiparams{'ID'});
166 # Read-in ignoredfile.
167 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
169 # Grab the configured status of the corresponding entry.
170 my $status = $ignored{$id}[2];
173 if ($status eq "disabled") {
176 $status = "disabled";
179 # Modify the status of the existing entry.
180 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
182 # Write the changed ignored hash to the ignored file.
183 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
185 # Regenerate the ignore file.
186 &IDS
::generate_ignore_file
();
188 # Check if the IDS is running.
189 if(&IDS
::ids_is_running
()) {
190 # Call suricatactrl to perform a reload.
191 &IDS
::call_suricatactrl
("reload");
195 ## Remove entry from ignore list.
197 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'remove'}) {
200 # Read-in ignoredfile.
201 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
203 # Drop entry from the hash.
204 delete($ignored{$cgiparams{'ID'}});
206 # Undef the given ID.
207 undef($cgiparams{'ID'});
209 # Write the changed ignored hash to the ignored file.
210 &General
::writehasharray
($IDS::ignored_file
, \
%ignored);
212 # Regenerate the ignore file.
213 &IDS
::generate_ignore_file
();
215 # Check if the IDS is running.
216 if(&IDS
::ids_is_running
()) {
217 # Call suricatactrl to perform a reload.
218 &IDS
::call_suricatactrl
("reload");
222 # Check if the page is locked, in this case, the ids_page_lock_file exists.
223 if (-e
$IDS::ids_page_lock_file
) {
224 # Lock the webpage and print notice about autoupgrade of the ruleset
226 &working_notice
("$Lang::tr{'ids ruleset autoupdate in progress'}");
228 # Loop and check if the file still exists.
229 while(-e
$IDS::ids_page_lock_file
) {
230 # Sleep for a second and re-check.
234 # Page has been unlocked, perform a reload.
238 # Check if any error has been stored.
239 if (-e
$IDS::storederrorfile
) {
240 # Open file to read in the stored error message.
241 open(FILE
, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
243 # Read the stored error message.
244 $errormessage = <FILE
>;
249 # Delete the file, which is now not longer required.
250 unlink($IDS::storederrorfile
);
253 # Gather ruleset details.
254 if ($cgiparams{'RULESET'}) {
255 ## Grab all available rules and store them in the idsrules hash.
257 # Open rules directory and do a directory listing.
258 opendir(DIR
, $IDS::rulespath
) or die $!;
259 # Loop through the direcory.
260 while (my $file = readdir(DIR
)) {
262 # We only want files.
263 next unless (-f
"$IDS::rulespath/$file");
265 # Ignore empty files.
266 next if (-z
"$IDS::rulespath/$file");
268 # Use a regular expression to find files ending in .rules
269 next unless ($file =~ m/\.rules$/);
271 # Ignore files which are not read-able.
272 next unless (-R
"$IDS::rulespath/$file");
274 # Skip whitelist rules file.
275 next if( $file eq "whitelist.rules");
277 # Call subfunction to read-in rulefile and add rules to
279 &readrulesfile
("$file");
284 # Gather used rulefiles.
286 # Check if the file for activated rulefiles is not empty.
287 if(-f
$IDS::used_rulefiles_file
) {
288 # Open the file for used rulefile and read-in content.
289 open(FILE
, $IDS::used_rulefiles_file
) or die "Could not open $IDS::used_rulefiles_file. $!\n";
297 # Loop through the array.
298 foreach my $line (@lines) {
303 next if ($line =~ /\#/);
306 next if ($line =~ /^\s*$/);
308 # Gather rule sid and message from the ruleline.
309 if ($line =~ /.*- (.*)/) {
312 # Check if the current rulefile exists in the %idsrules hash.
313 # If not, the file probably does not exist anymore or contains
315 if($idsrules{$rulefile}) {
316 # Add the rulefile state to the %idsrules hash.
317 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
324 # Save ruleset configuration.
325 if ($cgiparams{'RULESET'} eq $Lang::tr
{'save'}) {
328 # Read-in current (old) IDS settings.
329 &General
::readhash
("$IDS::rules_settings_file", \
%oldsettings);
331 # Prevent form name from been stored in conf file.
332 delete $cgiparams{'RULESET'};
334 # Check if the choosen vendor (URL) requires an subscription/oinkcode.
335 if ($IDS::Ruleset
::Providers
{$cgiparams{'RULES'}}{'requires_subscription'} eq "True") {
336 # Check if an subscription/oinkcode has been provided.
337 if ($cgiparams{'OINKCODE'}) {
338 # Check if the oinkcode contains unallowed chars.
339 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
340 $errormessage = $Lang::tr
{'invalid input for oink code'};
343 # Print an error message, that an subsription/oinkcode is required for this
345 $errormessage = $Lang::tr
{'ids oinkcode required'};
349 # Go on if there are no error messages.
350 if (!$errormessage) {
351 # Store settings into settings file.
352 &General
::writehash
("$IDS::rules_settings_file", \
%cgiparams);
354 # Check if a ruleset is present - if not or the source has been changed download it.
355 if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) {
356 # Check if the red device is active.
357 unless (-e
"${General::swroot}/red/active") {
358 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
361 # Check if enough free disk space is availabe.
362 if(&IDS
::checkdiskspace
()) {
363 $errormessage = "$Lang::tr{'not enough disk space'}";
366 # Check if any errors happend.
367 unless ($errormessage) {
368 # Lock the webpage and print notice about downloading
370 &working_notice
("$Lang::tr{'ids working'}");
372 # Write the modify sid's file and pass the taken ruleaction.
373 &IDS
::write_modify_sids_file
();
375 # Call subfunction to download the ruleset.
376 if(&IDS
::downloadruleset
()) {
377 $errormessage = $Lang::tr
{'could not download latest updates'};
379 # Call function to store the errormessage.
380 &IDS
::_store_error_message
($errormessage);
382 # Call subfunction to launch oinkmaster.
386 # Check if the IDS is running.
387 if(&IDS
::ids_is_running
()) {
388 # Call suricatactrl to stop the IDS - because of the changed
389 # ruleset - the use has to configure it before suricata can be
391 &IDS
::call_suricatactrl
("stop");
394 # Perform a reload of the page.
401 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'ids apply'}) {
402 # Arrays to store which rulefiles have been enabled and will be used.
403 my @enabled_rulefiles;
405 # Hash to store the user-enabled and disabled sids.
406 my %enabled_disabled_sids;
408 # Store if a restart of suricata is required.
409 my $suricata_restart_required;
411 # Loop through the hash of idsrules.
412 foreach my $rulefile(keys %idsrules) {
413 # Check if the state of the rulefile has been changed.
414 unless ($cgiparams{$rulefile} eq $idsrules{$rulefile}{'Rulefile'}{'State'}) {
415 # A restart of suricata is required to apply the changes of the used rulefiles.
416 $suricata_restart_required = 1;
419 # Check if the rulefile is enabled.
420 if ($cgiparams{$rulefile} eq "on") {
421 # Add rulefile to the array of enabled rulefiles.
422 push(@enabled_rulefiles, $rulefile);
424 # Drop item from cgiparams hash.
425 delete $cgiparams{$rulefile};
429 # Read-in the files for enabled/disabled sids.
430 # This will be done by calling the read_enabled_disabled_sids_file function two times
431 # and merge the returned hashes together into the enabled_disabled_sids hash.
432 %enabled_disabled_sids = (
433 &read_enabled_disabled_sids_file
($IDS::disabled_sids_file
),
434 &read_enabled_disabled_sids_file
($IDS::enabled_sids_file
));
436 # Loop through the hash of idsrules.
437 foreach my $rulefile (keys %idsrules) {
438 # Loop through the single rules of the rulefile.
439 foreach my $sid (keys %{$idsrules{$rulefile}}) {
440 # Skip the current sid if it is not numeric.
441 next unless ($sid =~ /\d+/ );
443 # Check if there exists a key in the cgiparams hash for this sid.
444 if (exists($cgiparams{$sid})) {
445 # Look if the rule is disabled.
446 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
447 # Check if the state has been set to 'on'.
448 if ($cgiparams{$sid} eq "on") {
449 # Add/Modify the sid to/in the enabled_disabled_sids hash.
450 $enabled_disabled_sids{$sid} = "enabled";
452 # Drop item from cgiparams hash.
453 delete $cgiparams{$rulefile}{$sid};
457 # Look if the rule is enabled.
458 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
459 # Check if the state is 'on' and should be disabled.
460 # In this case there is no entry
461 # for the sid in the cgiparams hash.
462 # Add/Modify it to/in the enabled_disabled_sids hash.
463 $enabled_disabled_sids{$sid} = "disabled";
465 # Drop item from cgiparams hash.
466 delete $cgiparams{$rulefile}{$sid};
472 # Open enabled sid's file for writing.
473 open(ENABLED_FILE
, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
475 # Open disabled sid's file for writing.
476 open(DISABLED_FILE
, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
478 # Write header to the files.
479 print ENABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
480 print DISABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
482 # Check if the hash for enabled/disabled files contains any entries.
483 if (%enabled_disabled_sids) {
484 # Loop through the hash.
485 foreach my $sid (keys %enabled_disabled_sids) {
486 # Check if the sid is enabled.
487 if ($enabled_disabled_sids{$sid} eq "enabled") {
488 # Print the sid to the enabled_sids file.
489 print ENABLED_FILE
"enablesid $sid\n";
490 # Check if the sid is disabled.
491 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
492 # Print the sid to the disabled_sids file.
493 print DISABLED_FILE
"disablesid $sid\n";
494 # Something strange happende - skip the current sid.
501 # Close file for enabled_sids after writing.
504 # Close file for disabled_sids after writing.
505 close(DISABLED_FILE
);
507 # Call function to generate and write the used rulefiles file.
508 &IDS
::write_used_rulefiles_file
(@enabled_rulefiles);
510 # Lock the webpage and print message.
511 &working_notice
("$Lang::tr{'ids apply ruleset changes'}");
513 # Call oinkmaster to alter the ruleset.
516 # Check if the IDS is running.
517 if(&IDS
::ids_is_running
()) {
518 # Check if a restart of suricata is required.
519 if ($suricata_restart_required) {
520 # Call suricatactrl to perform the restart.
521 &IDS
::call_suricatactrl
("restart");
523 # Call suricatactrl to perform a reload.
524 &IDS
::call_suricatactrl
("reload");
531 # Download new ruleset.
532 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'update ruleset'}) {
533 # Check if the red device is active.
534 unless (-e
"${General::swroot}/red/active") {
535 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
538 # Check if enought free disk space is availabe.
539 if(&IDS
::checkdiskspace
()) {
540 $errormessage = "$Lang::tr{'not enough disk space'}";
543 # Check if any errors happend.
544 unless ($errormessage) {
545 # Lock the webpage and print notice about downloading
547 &working_notice
("$Lang::tr{'ids download new ruleset'}");
549 # Call subfunction to download the ruleset.
550 if(&IDS
::downloadruleset
()) {
551 $errormessage = $Lang::tr
{'could not download latest updates'};
553 # Call function to store the errormessage.
554 &IDS
::_store_error_message
($errormessage);
556 # Preform a reload of the page.
559 # Call subfunction to launch oinkmaster.
562 # Check if the IDS is running.
563 if(&IDS
::ids_is_running
()) {
564 # Call suricatactrl to perform a reload.
565 &IDS
::call_suricatactrl
("reload");
568 # Perform a reload of the page.
573 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
576 my $monitored_zones = 0;
578 # Read-in current (old) IDS settings.
579 &General
::readhash
("$IDS::ids_settings_file", \
%oldidssettings);
581 # Prevent form name from been stored in conf file.
582 delete $cgiparams{'IDS'};
584 # Check if the IDS should be enabled.
585 if ($cgiparams{'ENABLE_IDS'} eq "on") {
586 # Check if any ruleset is available. Otherwise abort and display an error.
587 unless(%used_providers) {
588 $errormessage = $Lang::tr
{'ids no ruleset available'};
591 # Loop through the array of available interfaces.
592 foreach my $zone (@network_zones) {
593 # Convert interface name into upper case.
594 my $zone_upper = uc($zone);
596 # Check if the IDS is enabled for this interaces.
597 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
603 # Check if at least one zone should be monitored, or show an error.
604 unless ($monitored_zones >= 1) {
605 $errormessage = $Lang::tr
{'ids no network zone'};
609 # Go on if there are no error messages.
610 if (!$errormessage) {
611 # Store settings into settings file.
612 &General
::writehash
("$IDS::ids_settings_file", \
%cgiparams);
615 # Check if the the automatic rule update hass been touched.
616 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldidssettings{'AUTOUPDATE_INTERVAL'}) {
617 # Call suricatactrl to set the new interval.
618 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
621 # Generate file to store the home net.
622 &IDS
::generate_home_net_file
();
624 # Generate file to the store the DNS servers.
625 &IDS
::generate_dns_servers_file
();
627 # Generate file to store the HTTP ports.
628 &IDS
::generate_http_ports_file
();
630 # Write the modify sid's file and pass the taken ruleaction.
631 &IDS
::write_modify_sids_file
();
633 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
634 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
635 # Check if a ruleset exists.
636 if (%used_providers) {
637 # Lock the webpage and print message.
638 &working_notice
("$Lang::tr{'ids working'}");
640 # Call oinkmaster to alter the ruleset.
643 # Set reload_page to "True".
648 # Check if the IDS currently is running.
649 if(&IDS
::ids_is_running
()) {
650 # Check if ENABLE_IDS is set to on.
651 if($cgiparams{'ENABLE_IDS'} eq "on") {
652 # Call suricatactrl to perform a reload of suricata.
653 &IDS
::call_suricatactrl
("reload");
655 # Call suricatactrl to stop suricata.
656 &IDS
::call_suricatactrl
("stop");
659 # Call suricatactrl to start suricata.
660 &IDS
::call_suricatactrl
("start");
663 # Check if the page should be reloaded.
665 # Perform a reload of the page.
669 } elsif (($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'update'}")) {
670 my %used_providers = ();
672 # Read-in providers settings file.
673 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
675 # Assign some nice human-readable values.
676 my $provider = $cgiparams{'PROVIDER'};
677 my $subscription_code = $cgiparams{'SUBSCRIPTION_CODE'};
678 my $status_autoupdate;
680 # Handle autoupdate checkbox.
681 if ($cgiparams{'ENABLE_AUTOUPDATE'} eq "on") {
682 $status_autoupdate = "enabled";
684 $status_autoupdate = "disabled";
687 # Check if we are going to add a new provider.
688 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") {
689 # Loop through the hash of used providers.
690 foreach my $id ( keys %used_providers) {
691 # Check if the choosen provider is already in use.
692 if ($used_providers{$id}[0] eq "$provider") {
693 # XXX - add to language file.
694 # Assign error message.
695 $errormessage = "The choosen provider is already in use.";
700 # Check if the provider requires a subscription code.
701 if ($IDS::Ruleset
::Providers
{$provider}{'requires_subscription'} eq "True") {
702 # Check if an subscription code has been provided.
703 if ($subscription_code) {
704 # Check if the code contains unallowed chars.
705 unless ($subscription_code =~ /^[a-z0-9]+$/) {
706 $errormessage = $Lang::tr
{'invalid input for subscription code'};
709 # Print an error message, that an subsription code is required for this
711 $errormessage = $Lang::tr
{'ids subscription code required'};
715 # Go further if there was no error.
716 if ($errormessage eq '') {
720 # Check if we should edit an existing entry and got an ID.
721 if (($cgiparams{'PROVIDERS'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
722 # Assin the provided id.
723 $id = $cgiparams{'ID'};
725 # Undef the given ID.
726 undef($cgiparams{'ID'});
728 # Grab the configured status of the corresponding entry.
729 $status = $used_providers{$id}[3];
731 # Each newly added entry automatically should be enabled.
734 # Generate the ID for the new entry.
736 # Sort the keys by their ID and store them in an array.
737 my @keys = sort { $a <=> $b } keys %used_providers;
739 # Reverse the key array.
740 my @reversed = reverse(@keys);
742 # Obtain the last used id.
743 my $last_id = @reversed[0];
745 # Increase the last id by one and use it as id for the new entry.
749 # Add/Modify the entry to/in the used providers hash..
750 $used_providers{$id} = ["$provider", "$subscription_code", "$status_autoupdate", "$status"];
752 # Write the changed hash to the providers settings file.
753 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
755 # Undefine providers flag.
756 undef($cgiparams{'PROVIDERS'});
760 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
762 &Header
::openbigbox
('100%', 'left', '', $errormessage);
764 &show_display_error_message
();
766 if ($cgiparams{'RULESET'} eq "$Lang::tr{'ids customize ruleset'}" ) {
767 &show_customize_ruleset
();
768 } elsif ($cgiparams{'PROVIDERS'} ne "") {
769 &show_add_provider
();
774 &Header
::closebigbox
();
775 &Header
::closepage
();
778 ## Tiny function to show if a error message happened.
780 sub show_display_error_message
() {
782 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
783 print "<class name='base'>$errormessage\n";
784 print " </class>\n";
790 ## Function to display the main IDS page.
792 sub show_mainpage
() {
793 # Read-in idssettings and provider settings.
794 &General
::readhash
("$IDS::ids_settings_file", \
%idssettings);
795 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
797 # If no autoupdate intervall has been configured yet, set default value.
798 unless(exists($idssettings{'AUTOUPDATE_INTERVAL'})) {
799 # Set default to "weekly".
800 $idssettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
803 # Read-in ignored hosts.
804 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
806 $checked{'ENABLE_IDS'}{'off'} = '';
807 $checked{'ENABLE_IDS'}{'on'} = '';
808 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
809 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
810 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
811 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
812 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
813 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
814 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
815 $selected{'AUTOUPDATE_INTERVAL'}{$idssettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
817 # Draw current state of the IDS
818 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
820 # Check if the IDS is running and obtain the process-id.
821 my $pid = &IDS
::ids_is_running
();
823 # Display some useful information, if suricata daemon is running.
825 # Gather used memory.
826 my $memory = &get_memory_usage
($pid);
829 <table width='95%' cellspacing='0' class='tbl'>
831 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
835 <td class='base'>$Lang::tr{'guardian daemon'}</td>
836 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
840 <td class='base'></td>
841 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
842 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
846 <td class='base'></td>
847 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
848 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
853 # Otherwise display a hint that the service is not launched.
855 <table width='95%' cellspacing='0' class='tbl'>
857 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
861 <td class='base'>$Lang::tr{'guardian daemon'}</td>
862 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
868 # Only show this area, if at least one ruleset provider is configured.
869 if (%used_providers) {
873 <br><br><h2>$Lang::tr{'settings'}</h2>
875 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
876 <table width='100%' border='0'>
878 <td class='base' colspan='2'>
879 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}> $Lang::tr{'ids enable'}
882 <td class='base' colspan='2'>
883 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}> $Lang::tr{'ids monitor traffic only'}
895 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
902 # Loop through the array of available networks and print config options.
903 foreach my $zone (@network_zones) {
907 # Convert current zone name to upper case.
908 my $zone_upper = uc($zone);
911 my $zone_name = $zone;
913 # Dirty hack to get the correct language string for the red zone.
914 if ($zone eq "red") {
918 # Grab checkbox status from settings hash.
919 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
920 $checked_input = "checked = 'checked'";
923 print "<td class='base' width='20%'>\n";
924 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
925 print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
940 <td colspan='4'><b>$Lang::tr{'ids automatic rules update'}</b></td>
945 <select name='AUTOUPDATE_INTERVAL'>
946 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
947 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
948 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
958 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
970 # Used Ruleset Providers section.
972 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
975 <table width='100%' border='0'>
977 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ids provider'}</b></td>
978 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'date'}</b></td>
979 <td class='base' bgcolor='$color{'color20'}' align='center'><b>$Lang::tr{'ids autoupdates'}</b></td>
980 <td class='base' bgcolor='$color{'color20'}'></td>
981 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
984 # Check if some providers has been configured.
985 if (keys (%used_providers)) {
988 # Loop through all entries of the hash.
989 foreach my $id (sort keys(%used_providers)) {
990 # Assign data array positions to some nice variable names.
991 my $provider = $used_providers{$id}[0];
992 my $provider_name = &get_provider_name
($provider);
994 #XXX my $rulesdate = &IDS::get_ruleset_date($provider);
997 my $subscription_code = $used_providers{$id}[1];
998 my $autoupdate_status = $used_providers{$id}[2];
999 my $status = $used_providers{$id}[3];
1001 # Check if the item number is even or not.
1003 $col="bgcolor='$color{'color22'}'";
1005 $col="bgcolor='$color{'color20'}'";
1008 # Choose icons for the checkboxes.
1011 my $autoupdate_status_gif;
1012 my $autoupdate_status_gdesc;
1014 # Check if the status is enabled and select the correct image and description.
1015 if ($status eq 'enabled' ) {
1016 $status_gif = 'on.gif';
1017 $status_gdesc = $Lang::tr
{'click to disable'};
1019 $status_gif = 'off.gif';
1020 $status_gdesc = $Lang::tr
{'click to enable'};
1023 # Check if the autoupdate status is enabled and select the correct image and description.
1024 if ($autoupdate_status eq 'enabled') {
1025 $autoupdate_status_gif = 'on.gif';
1026 $autoupdate_status_gdesc = $Lang::tr
{'click to disable'};
1028 $autoupdate_status_gif = 'off.gif';
1029 $autoupdate_status_gdesc = $Lang::tr
{'click to enable'};
1034 <td width='33%' class='base' $col>$provider_name</td>
1035 <td width='30%' class='base' $col>$rulesdate</td>
1037 <td align='center' $col>
1038 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1039 <input type='hidden' name='AUTOUPDATE' value='$Lang::tr{'toggle enable disable'}' />
1040 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$autoupdate_status_gif' alt='$autoupdate_status_gdesc' title='$autoupdate_status_gdesc' />
1041 <input type='hidden' name='ID' value='$id' />
1045 <td align='center' $col>
1046 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1047 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'toggle enable disable'}'>
1048 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$status_gif' alt='$status_gdesc' title='$status_gdesc'>
1049 <input type='hidden' name='ID' value='$id'>
1053 <td align='center' $col>
1054 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1055 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'edit'}'>
1056 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1057 <input type='hidden' name='ID' value='$id'>
1061 <td align='center' $col>
1062 <form method='post' name='$provider' action='$ENV{'SCRIPT_NAME'}'>
1063 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1064 <input type='hidden' name='ID' value='$id'>
1065 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'remove'}'>
1073 # Print notice that currently no hosts are ignored.
1075 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1081 # Section to add new elements or edit existing ones.
1088 <table width='100%'>
1089 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1093 # Only show this button if a ruleset provider is configured.
1094 if (%used_providers) {
1095 print "<input type='submit' name='RULESET' value='$Lang::tr{'ids customize ruleset'}'>\n";
1098 <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids add provider'}'>
1105 &Header
::closebox
();
1108 # Whitelist / Ignorelist
1110 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ignored hosts'});
1113 <table width='100%'>
1115 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
1116 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
1117 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1120 # Check if some hosts have been added to be ignored.
1121 if (keys (%ignored)) {
1124 # Loop through all entries of the hash.
1125 while( (my $key) = each %ignored) {
1126 # Assign data array positions to some nice variable names.
1127 my $address = $ignored{$key}[0];
1128 my $remark = $ignored{$key}[1];
1129 my $status = $ignored{$key}[2];
1131 # Check if the key (id) number is even or not.
1132 if ($cgiparams{'ID'} eq $key) {
1133 $col="bgcolor='${Header::colouryellow}'";
1134 } elsif ($key % 2) {
1135 $col="bgcolor='$color{'color22'}'";
1137 $col="bgcolor='$color{'color20'}'";
1140 # Choose icon for the checkbox.
1144 # Check if the status is enabled and select the correct image and description.
1145 if ($status eq 'enabled' ) {
1147 $gdesc = $Lang::tr
{'click to disable'};
1150 $gdesc = $Lang::tr
{'click to enable'};
1155 <td width='20%' class='base' $col>$address</td>
1156 <td width='65%' class='base' $col>$remark</td>
1158 <td align='center' $col>
1159 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1160 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}'>
1161 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc'>
1162 <input type='hidden' name='ID' value='$key'>
1166 <td align='center' $col>
1167 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1168 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}'>
1169 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1170 <input type='hidden' name='ID' value='$key'>
1174 <td align='center' $col>
1175 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
1176 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1177 <input type='hidden' name='ID' value='$key'>
1178 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1185 # Print notice that currently no hosts are ignored.
1187 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1193 # Section to add new elements or edit existing ones.
1199 <div align='center'>
1200 <table width='100%'>
1203 # Assign correct headline and button text.
1208 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1209 if ($cgiparams{'ID'} ne '') {
1210 $buttontext = $Lang::tr
{'update'};
1211 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1213 # Grab address and remark for the given key.
1214 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1215 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1217 $buttontext = $Lang::tr
{'add'};
1218 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1222 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1223 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1225 <td width='30%'>$Lang::tr{'ip address'}: </td>
1226 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1228 <td width='30%'>$Lang::tr{'remark'}: </td>
1229 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1230 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1237 &Header
::closebox
();
1241 ## Function to show the customize ruleset section.
1243 sub show_customize_ruleset
() {
1247 # Java script variable declaration for show and hide.
1248 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
1249 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
1252 // Tiny java script function to show/hide the rules
1253 // of a given category.
1254 function showhide(tblname) {
1255 \$("#" + tblname).toggle();
1257 // Get current content of the span element.
1258 var content = document.getElementById("span_" + tblname);
1260 if (content.innerHTML === show) {
1261 content.innerHTML = hide;
1263 content.innerHTML = show;
1269 &Header
::openbox
('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'}" );
1270 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1272 # Output display table for rule files
1273 print "<table width='100%'>\n";
1275 # Loop over each rule file
1276 foreach my $rulefile (sort keys(%idsrules)) {
1277 my $rulechecked = '';
1279 # Check if rule file is enabled
1280 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1281 $rulechecked = 'CHECKED';
1284 # Convert rulefile name into category name.
1285 my $categoryname = &_rulefile_to_category
($rulefile);
1287 # Table and rows for the rule files.
1289 print"<td class='base' width='5%'>\n";
1290 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1292 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1293 print"<td class='base' width='5%' align='right'>\n";
1294 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1298 # Rows which will be hidden per default and will contain the single rules.
1299 print"<tr style='display:none' id='$categoryname'>\n";
1300 print"<td colspan='3'>\n";
1307 # New table for the single rules.
1308 print "<table width='100%'>\n";
1310 # Loop over rule file rules
1311 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1313 my $ruledefchecked = '';
1315 # Skip rulefile itself.
1316 next if ($sid eq "Rulefile");
1318 # If 2 rules have been displayed, start a new row
1319 if (($lines % 2) == 0) {
1320 print "</tr><tr>\n";
1322 # Increase rows by once.
1328 $col="bgcolor='$color{'color20'}'";
1330 $col="bgcolor='$color{'color22'}'";
1334 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1335 $ruledefchecked = 'CHECKED';
1338 # Create rule checkbox and display rule description
1339 print "<td class='base' width='5%' align='right' $col>\n";
1340 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1342 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1344 # Increment rule count
1348 # If do not have a second rule for row, create empty cell
1349 if (($lines % 2) != 0) {
1350 print "<td class='base'></td>";
1353 # Close display table
1354 print "</tr></table></td></tr>";
1357 # Close display table
1361 <table width='100%'>
1363 <td width='100%' align='right'>
1364 <input type='submit' value='$Lang::tr{'fwhost back'}'>
1365 <input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'>
1372 &Header
::closebox
();
1377 ## Function to show section for add/edit a provider.
1379 sub show_add_provider
() {
1380 my %used_providers = ();
1381 my @subscription_providers;
1383 # Read -in providers settings file.
1384 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
1386 # Get all supported ruleset providers.
1387 my @ruleset_providers = &IDS
::get_ruleset_providers
();
1392 # Generate Java Script Object which contains the URL of the providers.
1393 print "\t// Object, which contains the webpages of the ruleset providers.\n";
1394 print "\tvar url = {\n";
1396 # Loop through the array of supported providers.
1397 foreach my $provider (@ruleset_providers) {
1398 # Check if the provider requires a subscription.
1399 if ($IDS::Ruleset
::Providers
{$provider}{'requires_subscription'} eq "True") {
1400 # Add the provider to the array of subscription_providers.
1401 push(@subscription_providers, $provider);
1404 # Grab the URL for the provider.
1405 my $url = $IDS::Ruleset
::Providers
{$provider}{'website'};
1407 # Print the URL to the Java Script Object.
1408 print "\t\t$provider: \"$url\"\,\n";
1411 # Close the Java Script Object declaration.
1414 # Generate Java Script Array which contains the provider that requires a subscription.
1416 $line = join("', '", @subscription_providers);
1418 print "\t// Array which contains the providers that requires a subscription.\n";
1419 print "\tsubscription_provider = ['$line']\;\n\n";
1422 // Java Script function to swap the text input field for
1423 // entering a subscription code.
1424 var update_provider = function() {
1425 if(inArray(\$('#PROVIDER').val(), subscription_provider)) {
1426 \$('.subscription_code').show();
1428 \$('.subscription_code').hide();
1431 // Call function to change the website url.
1432 change_url(\$('#PROVIDER').val());
1435 // Java Script function to check if a given value is part of
1437 function inArray(value,array) {
1438 var count=array.length;
1440 for(var i=0;i<count;i++) {
1441 if(array[i]===value){
1449 // Tiny function to change the website url based on the selected element in the "PROVIDERS"
1451 function change_url(provider) {
1452 // Get and change the href to the corresponding url.
1453 document.getElementById("website").href = url[provider];
1456 // JQuery function to call corresponding function when
1457 // the ruleset provider is changed or the page is loaded for showing/hiding
1458 // the subscription_code area.
1459 \$(document).ready(function() {
1460 \$('#PROVIDER').change(update_provider);
1468 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids provider settings'});
1470 # Check if an existing provider should be edited.
1471 if($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1472 # Check if autoupdate is enabled for this provider.
1473 if ($used_providers{$cgiparams{'ID'}}[2] eq "enabled") {
1474 # Set the checkbox to be checked.
1475 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1477 } elsif ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'ids add provider'}") {
1478 # Set the autoupdate to true as default.
1479 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1483 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1484 <table width='100%' border='0'>
1486 <td colspan='2'><b>$Lang::tr{'ids provider'}</b></td>
1491 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1492 <select name='PROVIDER' id='PROVIDER'>
1495 # Loop through the array of ruleset providers.
1496 foreach my $provider (@ruleset_providers) {
1497 # Get the provider name.
1498 my $provider_name = &get_provider_name
($provider);
1500 # Pre-select the provider if one is given.
1501 if (($used_providers{$cgiparams{'ID'}}[0] eq "$provider") || ($cgiparams{'PROVIDER'} eq "$provider")) {
1502 $selected{$provider} = "selected='selected'";
1505 # Add the provider to the dropdown menu.
1506 print "<option value='$provider' $selected{$provider}>$provider_name</option>\n";
1513 <b><a id="website" target="_blank" href="#">$Lang::tr{'ids visit provider website'}</a></b>
1518 <td colspan='2'><br><br></td>
1521 <tr class='subscription_code' style='display:none' id='subscription_code'>
1526 <b>$Lang::tr{'subscription code'}</b>
1532 <input type='text' size='40' name='SUBSCRIPTION_CODE' value='$used_providers{$cgiparams{'ID'}}[1]'>
1545 <input type='checkbox' name='ENABLE_AUTOUPDATE' $checked{'ENABLE_AUTOUPDATE'}> $Lang::tr{'ids enable automatic updates'}
1550 <td colspan='2' align='right'>
1551 <input type='submit' value='$Lang::tr{'back'}'>
1554 # Check if a provider should be added or edited.
1555 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1556 # Display button for updating the existing provider.
1557 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'update'}'>\n";
1559 # Display button to add the new provider.
1560 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'add'}'>\n";
1569 &Header
::closebox
();
1573 ## A function to display a notice, to lock the webpage and
1574 ## tell the user which action currently will be performed.
1576 sub working_notice
($) {
1579 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1580 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1581 &Header
::openbox
( 'Waiting', 1,);
1585 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1590 &Header
::closebox
();
1591 &Header
::closebigbox
();
1592 &Header
::closepage
();
1596 ## A tiny function to perform a reload of the webpage after one second.
1599 print "<meta http-equiv='refresh' content='1'>\n";
1606 ## Private function to read-in and parse rules of a given rulefile.
1608 ## The given file will be read, parsed and all valid rules will be stored by ID,
1609 ## message/description and it's state in the idsrules hash.
1611 sub readrulesfile
($) {
1612 my $rulefile = shift;
1614 # Open rule file and read in contents
1615 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1617 # Store file content in an array.
1618 my @lines = <RULEFILE
>;
1623 # Loop over rule file contents
1624 foreach my $line (@lines) {
1625 # Remove whitespaces.
1629 next if ($line =~ /^\s*$/);
1635 # Gather rule sid and message from the ruleline.
1636 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1640 # Check if a rule has been found.
1642 # Add rule to the idsrules hash.
1643 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1645 # Grab status of the rule. Check if ruleline starts with a "dash".
1646 if ($line =~ /^\#/) {
1647 # If yes, the rule is disabled.
1648 $idsrules{$rulefile}{$sid}{'State'} = "off";
1650 # Otherwise the rule is enabled.
1651 $idsrules{$rulefile}{$sid}{'State'} = "on";
1659 ## Function to get the used memory of a given process-id.
1661 sub get_memory_usage
($) {
1666 # Try to open the status file for the given process-id on the pseudo
1668 if (open(FILE
, "/proc/$pid/status")) {
1669 # Loop through the entire file.
1671 # Splitt current line content and store them into variables.
1672 my ($key, $value) = split(":", $_, 2);
1674 # Check if the current key is the one which contains the memory usage.
1675 # The wanted one is VmRSS which contains the Real-memory (resident set)
1676 # of the entire process.
1677 if ($key eq "VmRSS") {
1678 # Found the memory usage add it to the memory variable.
1686 # Close file handle.
1689 # Return memory usage.
1693 # If the file could not be open, return nothing.
1698 ## Function to read-in the given enabled or disables sids file.
1700 sub read_enabled_disabled_sids_file
($) {
1703 # Temporary hash to store the sids and their state. It will be
1704 # returned at the end of this function.
1707 # Open the given filename.
1708 open(FILE
, "$file") or die "Could not open $file. $!\n";
1710 # Loop through the file.
1716 next if ($_ =~ /^\s*$/);
1719 next if ($_ =~ /^\#/);
1721 # Splitt line into sid and state part.
1722 my ($state, $sid) = split(" ", $_);
1724 # Skip line if the sid is not numeric.
1725 next unless ($sid =~ /\d+/ );
1727 # Check if the sid was enabled.
1728 if ($state eq "enablesid") {
1729 # Add the sid and its state as enabled to the temporary hash.
1730 $temphash{$sid} = "enabled";
1731 # Check if the sid was disabled.
1732 } elsif ($state eq "disablesid") {
1733 # Add the sid and its state as disabled to the temporary hash.
1734 $temphash{$sid} = "disabled";
1735 # Invalid state - skip the current sid and state.
1749 ## Function to get the provider name from the language file or providers file for a given handle.
1751 sub get_provider_name
($) {
1755 # Get the required translation string for the given provider handle.
1756 my $tr_string = $IDS::Ruleset
::Providers
{$handle}{'tr_string'};
1758 # Check if the translation string is available in the language files.
1759 if ($Lang::tr
{$tr_string}) {
1760 # Use the translated string from the language file.
1761 $provider_name = $Lang::tr
{$tr_string};
1763 # Fallback and use the provider summary from the providers file.
1764 $provider_name = $IDS::Ruleset
::Providers
{$handle}{'summary'};
1767 # Return the obtained provider name.
1768 return $provider_name;
1772 ## Private function to convert a given rulefile to a category name.
1773 ## ( No file extension anymore and if the name contained a dot, it
1774 ## would be replaced by a underline sign.)
1776 sub _rulefile_to_category
($) {
1777 my ($filename) = @_;
1779 # Splitt the filename into single chunks and store them in a
1781 my @parts = split(/\./, $filename);
1783 # Return / Remove last element of the temporary array.
1784 # This removes the file extension.
1787 # Join together the single elements of the temporary array.
1788 # If these are more than one, use a "underline" for joining.
1789 my $category = join '_', @parts;
1791 # Return the converted filename.