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.
285 my @used_rulesfiles = &IDS
::get_used_rulesfiles
();
287 # Loop through the array of used rulesfiles.
288 foreach my $rulesfile (@used_rulesfiles) {
289 # Check if the current rulefile exists in the %idsrules hash.
290 # If not, the file probably does not exist anymore or contains
292 if($idsrules{$rulefile}) {
293 # Add the rulefile state to the %idsrules hash.
294 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
299 # Save ruleset configuration.
300 if ($cgiparams{'RULESET'} eq $Lang::tr
{'save'}) {
303 # Read-in current (old) IDS settings.
304 &General
::readhash
("$IDS::rules_settings_file", \
%oldsettings);
306 # Prevent form name from been stored in conf file.
307 delete $cgiparams{'RULESET'};
309 # Check if the choosen vendor (URL) requires an subscription/oinkcode.
310 if ($IDS::Ruleset
::Providers
{$cgiparams{'RULES'}}{'requires_subscription'} eq "True") {
311 # Check if an subscription/oinkcode has been provided.
312 if ($cgiparams{'OINKCODE'}) {
313 # Check if the oinkcode contains unallowed chars.
314 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
315 $errormessage = $Lang::tr
{'invalid input for oink code'};
318 # Print an error message, that an subsription/oinkcode is required for this
320 $errormessage = $Lang::tr
{'ids oinkcode required'};
324 # Go on if there are no error messages.
325 if (!$errormessage) {
326 # Store settings into settings file.
327 &General
::writehash
("$IDS::rules_settings_file", \
%cgiparams);
329 # Check if a ruleset is present - if not or the source has been changed download it.
330 if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) {
331 # Check if the red device is active.
332 unless (-e
"${General::swroot}/red/active") {
333 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
336 # Check if enough free disk space is availabe.
337 if(&IDS
::checkdiskspace
()) {
338 $errormessage = "$Lang::tr{'not enough disk space'}";
341 # Check if any errors happend.
342 unless ($errormessage) {
343 # Lock the webpage and print notice about downloading
345 &working_notice
("$Lang::tr{'ids working'}");
347 # Write the modify sid's file and pass the taken ruleaction.
348 &IDS
::write_modify_sids_file
();
350 # Call subfunction to download the ruleset.
351 if(&IDS
::downloadruleset
()) {
352 $errormessage = $Lang::tr
{'could not download latest updates'};
354 # Call function to store the errormessage.
355 &IDS
::_store_error_message
($errormessage);
357 # Call subfunction to launch oinkmaster.
361 # Check if the IDS is running.
362 if(&IDS
::ids_is_running
()) {
363 # Call suricatactrl to stop the IDS - because of the changed
364 # ruleset - the use has to configure it before suricata can be
366 &IDS
::call_suricatactrl
("stop");
369 # Perform a reload of the page.
376 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'ids apply'}) {
377 # Arrays to store which rulefiles have been enabled and will be used.
378 my @enabled_rulefiles;
380 # Hash to store the user-enabled and disabled sids.
381 my %enabled_disabled_sids;
383 # Store if a restart of suricata is required.
384 my $suricata_restart_required;
386 # Loop through the hash of idsrules.
387 foreach my $rulefile(keys %idsrules) {
388 # Check if the state of the rulefile has been changed.
389 unless ($cgiparams{$rulefile} eq $idsrules{$rulefile}{'Rulefile'}{'State'}) {
390 # A restart of suricata is required to apply the changes of the used rulefiles.
391 $suricata_restart_required = 1;
394 # Check if the rulefile is enabled.
395 if ($cgiparams{$rulefile} eq "on") {
396 # Add rulefile to the array of enabled rulefiles.
397 push(@enabled_rulefiles, $rulefile);
399 # Drop item from cgiparams hash.
400 delete $cgiparams{$rulefile};
404 # Read-in the files for enabled/disabled sids.
405 # This will be done by calling the read_enabled_disabled_sids_file function two times
406 # and merge the returned hashes together into the enabled_disabled_sids hash.
407 %enabled_disabled_sids = (
408 &read_enabled_disabled_sids_file
($IDS::disabled_sids_file
),
409 &read_enabled_disabled_sids_file
($IDS::enabled_sids_file
));
411 # Loop through the hash of idsrules.
412 foreach my $rulefile (keys %idsrules) {
413 # Loop through the single rules of the rulefile.
414 foreach my $sid (keys %{$idsrules{$rulefile}}) {
415 # Skip the current sid if it is not numeric.
416 next unless ($sid =~ /\d+/ );
418 # Check if there exists a key in the cgiparams hash for this sid.
419 if (exists($cgiparams{$sid})) {
420 # Look if the rule is disabled.
421 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
422 # Check if the state has been set to 'on'.
423 if ($cgiparams{$sid} eq "on") {
424 # Add/Modify the sid to/in the enabled_disabled_sids hash.
425 $enabled_disabled_sids{$sid} = "enabled";
427 # Drop item from cgiparams hash.
428 delete $cgiparams{$rulefile}{$sid};
432 # Look if the rule is enabled.
433 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
434 # Check if the state is 'on' and should be disabled.
435 # In this case there is no entry
436 # for the sid in the cgiparams hash.
437 # Add/Modify it to/in the enabled_disabled_sids hash.
438 $enabled_disabled_sids{$sid} = "disabled";
440 # Drop item from cgiparams hash.
441 delete $cgiparams{$rulefile}{$sid};
447 # Open enabled sid's file for writing.
448 open(ENABLED_FILE
, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
450 # Open disabled sid's file for writing.
451 open(DISABLED_FILE
, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
453 # Write header to the files.
454 print ENABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
455 print DISABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
457 # Check if the hash for enabled/disabled files contains any entries.
458 if (%enabled_disabled_sids) {
459 # Loop through the hash.
460 foreach my $sid (keys %enabled_disabled_sids) {
461 # Check if the sid is enabled.
462 if ($enabled_disabled_sids{$sid} eq "enabled") {
463 # Print the sid to the enabled_sids file.
464 print ENABLED_FILE
"enablesid $sid\n";
465 # Check if the sid is disabled.
466 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
467 # Print the sid to the disabled_sids file.
468 print DISABLED_FILE
"disablesid $sid\n";
469 # Something strange happende - skip the current sid.
476 # Close file for enabled_sids after writing.
479 # Close file for disabled_sids after writing.
480 close(DISABLED_FILE
);
482 # Call function to generate and write the used rulefiles file.
483 &IDS
::write_used_rulefiles_file
(@enabled_rulefiles);
485 # Lock the webpage and print message.
486 &working_notice
("$Lang::tr{'ids apply ruleset changes'}");
488 # Call oinkmaster to alter the ruleset.
491 # Check if the IDS is running.
492 if(&IDS
::ids_is_running
()) {
493 # Check if a restart of suricata is required.
494 if ($suricata_restart_required) {
495 # Call suricatactrl to perform the restart.
496 &IDS
::call_suricatactrl
("restart");
498 # Call suricatactrl to perform a reload.
499 &IDS
::call_suricatactrl
("reload");
506 # Download new ruleset.
507 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'update ruleset'}) {
508 # Check if the red device is active.
509 unless (-e
"${General::swroot}/red/active") {
510 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
513 # Check if enought free disk space is availabe.
514 if(&IDS
::checkdiskspace
()) {
515 $errormessage = "$Lang::tr{'not enough disk space'}";
518 # Check if any errors happend.
519 unless ($errormessage) {
520 # Lock the webpage and print notice about downloading
522 &working_notice
("$Lang::tr{'ids download new ruleset'}");
524 # Call subfunction to download the ruleset.
525 if(&IDS
::downloadruleset
()) {
526 $errormessage = $Lang::tr
{'could not download latest updates'};
528 # Call function to store the errormessage.
529 &IDS
::_store_error_message
($errormessage);
531 # Preform a reload of the page.
534 # Call subfunction to launch oinkmaster.
537 # Check if the IDS is running.
538 if(&IDS
::ids_is_running
()) {
539 # Call suricatactrl to perform a reload.
540 &IDS
::call_suricatactrl
("reload");
543 # Perform a reload of the page.
548 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
551 my $monitored_zones = 0;
553 # Read-in current (old) IDS settings.
554 &General
::readhash
("$IDS::ids_settings_file", \
%oldidssettings);
556 # Prevent form name from been stored in conf file.
557 delete $cgiparams{'IDS'};
559 # Check if the IDS should be enabled.
560 if ($cgiparams{'ENABLE_IDS'} eq "on") {
561 # Check if any ruleset is available. Otherwise abort and display an error.
562 unless(%used_providers) {
563 $errormessage = $Lang::tr
{'ids no ruleset available'};
566 # Loop through the array of available interfaces.
567 foreach my $zone (@network_zones) {
568 # Convert interface name into upper case.
569 my $zone_upper = uc($zone);
571 # Check if the IDS is enabled for this interaces.
572 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
578 # Check if at least one zone should be monitored, or show an error.
579 unless ($monitored_zones >= 1) {
580 $errormessage = $Lang::tr
{'ids no network zone'};
584 # Go on if there are no error messages.
585 if (!$errormessage) {
586 # Store settings into settings file.
587 &General
::writehash
("$IDS::ids_settings_file", \
%cgiparams);
590 # Check if the the automatic rule update hass been touched.
591 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldidssettings{'AUTOUPDATE_INTERVAL'}) {
592 # Call suricatactrl to set the new interval.
593 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
596 # Generate file to store the home net.
597 &IDS
::generate_home_net_file
();
599 # Generate file to the store the DNS servers.
600 &IDS
::generate_dns_servers_file
();
602 # Generate file to store the HTTP ports.
603 &IDS
::generate_http_ports_file
();
605 # Write the modify sid's file and pass the taken ruleaction.
606 &IDS
::write_modify_sids_file
();
608 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
609 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
610 # Check if a ruleset exists.
611 if (%used_providers) {
612 # Lock the webpage and print message.
613 &working_notice
("$Lang::tr{'ids working'}");
615 # Call oinkmaster to alter the ruleset.
618 # Set reload_page to "True".
623 # Check if the IDS currently is running.
624 if(&IDS
::ids_is_running
()) {
625 # Check if ENABLE_IDS is set to on.
626 if($cgiparams{'ENABLE_IDS'} eq "on") {
627 # Call suricatactrl to perform a reload of suricata.
628 &IDS
::call_suricatactrl
("reload");
630 # Call suricatactrl to stop suricata.
631 &IDS
::call_suricatactrl
("stop");
634 # Call suricatactrl to start suricata.
635 &IDS
::call_suricatactrl
("start");
638 # Check if the page should be reloaded.
640 # Perform a reload of the page.
644 # Toggle Enable/Disable autoupdate for a provider
645 } elsif ($cgiparams{'AUTOUPDATE'} eq $Lang::tr
{'toggle enable disable'}) {
646 my %used_providers = ();
648 # Only go further, if an ID has been passed.
649 if ($cgiparams{'ID'}) {
650 # Assign the given ID.
651 my $id = $cgiparams{'ID'};
653 # Undef the given ID.
654 undef($cgiparams{'ID'});
656 # Read-in providers settings file.
657 &General
::readhasharray
($IDS::providers_settings_file
, \
%used_providers);
659 # Grab the configured status of the corresponding entry.
660 my $status_autoupdate = $used_providers{$id}[2];
663 if ($status_autoupdate eq "disabled") {
664 $status_autoupdate = "enabled";
666 $status_autoupdate = "disabled";
669 # Modify the status of the existing entry.
670 $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$status_autoupdate", "$used_providers{$id}[3]"];
672 # Write the changed hash to the providers settings file.
673 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
676 # Add/Edit a provider to the list of used providers.
678 } elsif (($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'update'}")) {
679 my %used_providers = ();
681 # Read-in providers settings file.
682 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
684 # Assign some nice human-readable values.
685 my $provider = $cgiparams{'PROVIDER'};
686 my $subscription_code = $cgiparams{'SUBSCRIPTION_CODE'};
687 my $status_autoupdate;
689 # Handle autoupdate checkbox.
690 if ($cgiparams{'ENABLE_AUTOUPDATE'} eq "on") {
691 $status_autoupdate = "enabled";
693 $status_autoupdate = "disabled";
696 # Check if we are going to add a new provider.
697 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") {
698 # Loop through the hash of used providers.
699 foreach my $id ( keys %used_providers) {
700 # Check if the choosen provider is already in use.
701 if ($used_providers{$id}[0] eq "$provider") {
702 # XXX - add to language file.
703 # Assign error message.
704 $errormessage = "The choosen provider is already in use.";
709 # Check if the provider requires a subscription code.
710 if ($IDS::Ruleset
::Providers
{$provider}{'requires_subscription'} eq "True") {
711 # Check if an subscription code has been provided.
712 if ($subscription_code) {
713 # Check if the code contains unallowed chars.
714 unless ($subscription_code =~ /^[a-z0-9]+$/) {
715 $errormessage = $Lang::tr
{'invalid input for subscription code'};
718 # Print an error message, that an subsription code is required for this
720 $errormessage = $Lang::tr
{'ids subscription code required'};
724 # Go further if there was no error.
725 if ($errormessage eq '') {
729 # Check if we should edit an existing entry and got an ID.
730 if (($cgiparams{'PROVIDERS'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
731 # Assin the provided id.
732 $id = $cgiparams{'ID'};
734 # Undef the given ID.
735 undef($cgiparams{'ID'});
737 # Grab the configured status of the corresponding entry.
738 $status = $used_providers{$id}[3];
740 # Each newly added entry automatically should be enabled.
743 # Generate the ID for the new entry.
745 # Sort the keys by their ID and store them in an array.
746 my @keys = sort { $a <=> $b } keys %used_providers;
748 # Reverse the key array.
749 my @reversed = reverse(@keys);
751 # Obtain the last used id.
752 my $last_id = @reversed[0];
754 # Increase the last id by one and use it as id for the new entry.
758 # Add/Modify the entry to/in the used providers hash..
759 $used_providers{$id} = ["$provider", "$subscription_code", "$status_autoupdate", "$status"];
761 # Write the changed hash to the providers settings file.
762 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
764 # Undefine providers flag.
765 undef($cgiparams{'PROVIDERS'});
768 ## Toggle Enabled/Disabled for an existing provider.
770 } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr
{'toggle enable disable'}) {
771 my %used_providers = ();
773 # Only go further, if an ID has been passed.
774 if ($cgiparams{'ID'}) {
775 # Assign the given ID.
776 my $id = $cgiparams{'ID'};
778 # Undef the given ID.
779 undef($cgiparams{'ID'});
781 # Read-in file which contains the provider settings.
782 &General
::readhasharray
($IDS::providers_settings_file
, \
%used_providers);
784 # Grab the configured status of the corresponding entry.
785 my $status = $used_providers{$id}[3];
788 if ($status eq "enabled") {
789 $status = "disabled";
794 # Modify the status of the existing entry.
795 $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$used_providers{$id}[2]", "$status"];
797 # Write the changed hash to the providers settings file.
798 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
800 # XXX - The ruleset needs to be regenerated
801 # XXX - Suricata requires a reload or if the last provider
802 # has been disabled suricata needs to be stopped.
803 # Check if the IDS is running.
804 #if(&IDS::ids_is_running()) {
805 # # Call suricatactrl to perform a reload.
806 # &IDS::call_suricatactrl("reload");
809 # Undefine providers flag.
810 undef($cgiparams{'PROVIDERS'});
813 ## Remove provider from the list of used providers.
815 } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr
{'remove'}) {
816 my %used_providers = ();
818 # Read-in provider settings file.
819 &General
::readhasharray
($IDS::providers_settings_file
, \
%used_providers);
821 # Drop entry from the hash.
822 delete($used_providers{$cgiparams{'ID'}});
824 # Undef the given ID.
825 undef($cgiparams{'ID'});
827 # Write the changed hash to the provide settings file.
828 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
830 # XXX - The ruleset of the provider needs to be dropped.
831 # XXX - The remain rulest of suricata needs to be regenerated.
832 # XXX - Suricata requires a reload or if the last provider has
833 # been removed it has to be stopped.
834 # Check if the IDS is running.
835 #if(&IDS::ids_is_running()) {
836 # Call suricatactrl to perform a reload.
837 # &IDS::call_suricatactrl("reload");
840 # Undefine providers flag.
841 undef($cgiparams{'PROVIDERS'});
844 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
846 &Header
::openbigbox
('100%', 'left', '', $errormessage);
848 &show_display_error_message
();
850 if ($cgiparams{'RULESET'} eq "$Lang::tr{'ids customize ruleset'}" ) {
851 &show_customize_ruleset
();
852 } elsif ($cgiparams{'PROVIDERS'} ne "") {
853 &show_add_provider
();
858 &Header
::closebigbox
();
859 &Header
::closepage
();
862 ## Tiny function to show if a error message happened.
864 sub show_display_error_message
() {
866 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
867 print "<class name='base'>$errormessage\n";
868 print " </class>\n";
874 ## Function to display the main IDS page.
876 sub show_mainpage
() {
877 # Read-in idssettings and provider settings.
878 &General
::readhash
("$IDS::ids_settings_file", \
%idssettings);
879 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
881 # If no autoupdate intervall has been configured yet, set default value.
882 unless(exists($idssettings{'AUTOUPDATE_INTERVAL'})) {
883 # Set default to "weekly".
884 $idssettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
887 # Read-in ignored hosts.
888 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
890 $checked{'ENABLE_IDS'}{'off'} = '';
891 $checked{'ENABLE_IDS'}{'on'} = '';
892 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
893 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
894 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
895 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
896 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
897 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
898 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
899 $selected{'AUTOUPDATE_INTERVAL'}{$idssettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
901 # Draw current state of the IDS
902 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
904 # Check if the IDS is running and obtain the process-id.
905 my $pid = &IDS
::ids_is_running
();
907 # Display some useful information, if suricata daemon is running.
909 # Gather used memory.
910 my $memory = &get_memory_usage
($pid);
913 <table width='95%' cellspacing='0' class='tbl'>
915 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
919 <td class='base'>$Lang::tr{'guardian daemon'}</td>
920 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
924 <td class='base'></td>
925 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
926 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
930 <td class='base'></td>
931 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
932 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
937 # Otherwise display a hint that the service is not launched.
939 <table width='95%' cellspacing='0' class='tbl'>
941 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
945 <td class='base'>$Lang::tr{'guardian daemon'}</td>
946 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
952 # Only show this area, if at least one ruleset provider is configured.
953 if (%used_providers) {
957 <br><br><h2>$Lang::tr{'settings'}</h2>
959 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
960 <table width='100%' border='0'>
962 <td class='base' colspan='2'>
963 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}> $Lang::tr{'ids enable'}
966 <td class='base' colspan='2'>
967 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}> $Lang::tr{'ids monitor traffic only'}
979 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
986 # Loop through the array of available networks and print config options.
987 foreach my $zone (@network_zones) {
991 # Convert current zone name to upper case.
992 my $zone_upper = uc($zone);
995 my $zone_name = $zone;
997 # Dirty hack to get the correct language string for the red zone.
998 if ($zone eq "red") {
1002 # Grab checkbox status from settings hash.
1003 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
1004 $checked_input = "checked = 'checked'";
1007 print "<td class='base' width='20%'>\n";
1008 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
1009 print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
1024 <td colspan='4'><b>$Lang::tr{'ids automatic rules update'}</b></td>
1029 <select name='AUTOUPDATE_INTERVAL'>
1030 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
1031 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
1032 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
1040 <table width='100%'>
1042 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
1051 &Header
::closebox
();
1054 # Used Ruleset Providers section.
1056 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
1059 <table width='100%' border='0'>
1061 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ids provider'}</b></td>
1062 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'date'}</b></td>
1063 <td class='base' bgcolor='$color{'color20'}' align='center'><b>$Lang::tr{'ids autoupdates'}</b></td>
1064 <td class='base' bgcolor='$color{'color20'}'></td>
1065 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1068 # Check if some providers has been configured.
1069 if (keys (%used_providers)) {
1072 # Loop through all entries of the hash.
1073 foreach my $id (sort keys(%used_providers)) {
1074 # Assign data array positions to some nice variable names.
1075 my $provider = $used_providers{$id}[0];
1076 my $provider_name = &get_provider_name
($provider);
1078 #XXX my $rulesdate = &IDS::get_ruleset_date($provider);
1081 my $subscription_code = $used_providers{$id}[1];
1082 my $autoupdate_status = $used_providers{$id}[2];
1083 my $status = $used_providers{$id}[3];
1085 # Check if the item number is even or not.
1087 $col="bgcolor='$color{'color22'}'";
1089 $col="bgcolor='$color{'color20'}'";
1092 # Choose icons for the checkboxes.
1095 my $autoupdate_status_gif;
1096 my $autoupdate_status_gdesc;
1098 # Check if the status is enabled and select the correct image and description.
1099 if ($status eq 'enabled' ) {
1100 $status_gif = 'on.gif';
1101 $status_gdesc = $Lang::tr
{'click to disable'};
1103 $status_gif = 'off.gif';
1104 $status_gdesc = $Lang::tr
{'click to enable'};
1107 # Check if the autoupdate status is enabled and select the correct image and description.
1108 if ($autoupdate_status eq 'enabled') {
1109 $autoupdate_status_gif = 'on.gif';
1110 $autoupdate_status_gdesc = $Lang::tr
{'click to disable'};
1112 $autoupdate_status_gif = 'off.gif';
1113 $autoupdate_status_gdesc = $Lang::tr
{'click to enable'};
1118 <td width='33%' class='base' $col>$provider_name</td>
1119 <td width='30%' class='base' $col>$rulesdate</td>
1121 <td align='center' $col>
1122 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1123 <input type='hidden' name='AUTOUPDATE' value='$Lang::tr{'toggle enable disable'}' />
1124 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$autoupdate_status_gif' alt='$autoupdate_status_gdesc' title='$autoupdate_status_gdesc' />
1125 <input type='hidden' name='ID' value='$id' />
1129 <td align='center' $col>
1130 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1131 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'toggle enable disable'}'>
1132 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$status_gif' alt='$status_gdesc' title='$status_gdesc'>
1133 <input type='hidden' name='ID' value='$id'>
1137 <td align='center' $col>
1138 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1139 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'edit'}'>
1140 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1141 <input type='hidden' name='ID' value='$id'>
1145 <td align='center' $col>
1146 <form method='post' name='$provider' action='$ENV{'SCRIPT_NAME'}'>
1147 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1148 <input type='hidden' name='ID' value='$id'>
1149 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'remove'}'>
1157 # Print notice that currently no hosts are ignored.
1159 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1165 # Section to add new elements or edit existing ones.
1172 <table width='100%'>
1173 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1177 # Only show this button if a ruleset provider is configured.
1178 if (%used_providers) {
1179 print "<input type='submit' name='RULESET' value='$Lang::tr{'ids customize ruleset'}'>\n";
1182 <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids add provider'}'>
1189 &Header
::closebox
();
1192 # Whitelist / Ignorelist
1194 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ignored hosts'});
1197 <table width='100%'>
1199 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
1200 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
1201 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1204 # Check if some hosts have been added to be ignored.
1205 if (keys (%ignored)) {
1208 # Loop through all entries of the hash.
1209 while( (my $key) = each %ignored) {
1210 # Assign data array positions to some nice variable names.
1211 my $address = $ignored{$key}[0];
1212 my $remark = $ignored{$key}[1];
1213 my $status = $ignored{$key}[2];
1215 # Check if the key (id) number is even or not.
1216 if ($cgiparams{'ID'} eq $key) {
1217 $col="bgcolor='${Header::colouryellow}'";
1218 } elsif ($key % 2) {
1219 $col="bgcolor='$color{'color22'}'";
1221 $col="bgcolor='$color{'color20'}'";
1224 # Choose icon for the checkbox.
1228 # Check if the status is enabled and select the correct image and description.
1229 if ($status eq 'enabled' ) {
1231 $gdesc = $Lang::tr
{'click to disable'};
1234 $gdesc = $Lang::tr
{'click to enable'};
1239 <td width='20%' class='base' $col>$address</td>
1240 <td width='65%' class='base' $col>$remark</td>
1242 <td align='center' $col>
1243 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1244 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}'>
1245 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc'>
1246 <input type='hidden' name='ID' value='$key'>
1250 <td align='center' $col>
1251 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1252 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}'>
1253 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1254 <input type='hidden' name='ID' value='$key'>
1258 <td align='center' $col>
1259 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
1260 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1261 <input type='hidden' name='ID' value='$key'>
1262 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1269 # Print notice that currently no hosts are ignored.
1271 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1277 # Section to add new elements or edit existing ones.
1283 <div align='center'>
1284 <table width='100%'>
1287 # Assign correct headline and button text.
1292 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1293 if ($cgiparams{'ID'} ne '') {
1294 $buttontext = $Lang::tr
{'update'};
1295 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1297 # Grab address and remark for the given key.
1298 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1299 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1301 $buttontext = $Lang::tr
{'add'};
1302 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1306 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1307 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1309 <td width='30%'>$Lang::tr{'ip address'}: </td>
1310 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1312 <td width='30%'>$Lang::tr{'remark'}: </td>
1313 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1314 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1321 &Header
::closebox
();
1325 ## Function to show the customize ruleset section.
1327 sub show_customize_ruleset
() {
1331 # Java script variable declaration for show and hide.
1332 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
1333 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
1336 // Tiny java script function to show/hide the rules
1337 // of a given category.
1338 function showhide(tblname) {
1339 \$("#" + tblname).toggle();
1341 // Get current content of the span element.
1342 var content = document.getElementById("span_" + tblname);
1344 if (content.innerHTML === show) {
1345 content.innerHTML = hide;
1347 content.innerHTML = show;
1353 &Header
::openbox
('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'}" );
1354 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1356 # Output display table for rule files
1357 print "<table width='100%'>\n";
1359 # Loop over each rule file
1360 foreach my $rulefile (sort keys(%idsrules)) {
1361 my $rulechecked = '';
1363 # Check if rule file is enabled
1364 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1365 $rulechecked = 'CHECKED';
1368 # Convert rulefile name into category name.
1369 my $categoryname = &_rulefile_to_category
($rulefile);
1371 # Table and rows for the rule files.
1373 print"<td class='base' width='5%'>\n";
1374 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1376 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1377 print"<td class='base' width='5%' align='right'>\n";
1378 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1382 # Rows which will be hidden per default and will contain the single rules.
1383 print"<tr style='display:none' id='$categoryname'>\n";
1384 print"<td colspan='3'>\n";
1391 # New table for the single rules.
1392 print "<table width='100%'>\n";
1394 # Loop over rule file rules
1395 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1397 my $ruledefchecked = '';
1399 # Skip rulefile itself.
1400 next if ($sid eq "Rulefile");
1402 # If 2 rules have been displayed, start a new row
1403 if (($lines % 2) == 0) {
1404 print "</tr><tr>\n";
1406 # Increase rows by once.
1412 $col="bgcolor='$color{'color20'}'";
1414 $col="bgcolor='$color{'color22'}'";
1418 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1419 $ruledefchecked = 'CHECKED';
1422 # Create rule checkbox and display rule description
1423 print "<td class='base' width='5%' align='right' $col>\n";
1424 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1426 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1428 # Increment rule count
1432 # If do not have a second rule for row, create empty cell
1433 if (($lines % 2) != 0) {
1434 print "<td class='base'></td>";
1437 # Close display table
1438 print "</tr></table></td></tr>";
1441 # Close display table
1445 <table width='100%'>
1447 <td width='100%' align='right'>
1448 <input type='submit' value='$Lang::tr{'fwhost back'}'>
1449 <input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'>
1456 &Header
::closebox
();
1461 ## Function to show section for add/edit a provider.
1463 sub show_add_provider
() {
1464 my %used_providers = ();
1465 my @subscription_providers;
1467 # Read -in providers settings file.
1468 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
1470 # Get all supported ruleset providers.
1471 my @ruleset_providers = &IDS
::get_ruleset_providers
();
1476 # Generate Java Script Object which contains the URL of the providers.
1477 print "\t// Object, which contains the webpages of the ruleset providers.\n";
1478 print "\tvar url = {\n";
1480 # Loop through the array of supported providers.
1481 foreach my $provider (@ruleset_providers) {
1482 # Check if the provider requires a subscription.
1483 if ($IDS::Ruleset
::Providers
{$provider}{'requires_subscription'} eq "True") {
1484 # Add the provider to the array of subscription_providers.
1485 push(@subscription_providers, $provider);
1488 # Grab the URL for the provider.
1489 my $url = $IDS::Ruleset
::Providers
{$provider}{'website'};
1491 # Print the URL to the Java Script Object.
1492 print "\t\t$provider: \"$url\"\,\n";
1495 # Close the Java Script Object declaration.
1498 # Generate Java Script Array which contains the provider that requires a subscription.
1500 $line = join("', '", @subscription_providers);
1502 print "\t// Array which contains the providers that requires a subscription.\n";
1503 print "\tsubscription_provider = ['$line']\;\n\n";
1506 // Java Script function to swap the text input field for
1507 // entering a subscription code.
1508 var update_provider = function() {
1509 if(inArray(\$('#PROVIDER').val(), subscription_provider)) {
1510 \$('.subscription_code').show();
1512 \$('.subscription_code').hide();
1515 // Call function to change the website url.
1516 change_url(\$('#PROVIDER').val());
1519 // Java Script function to check if a given value is part of
1521 function inArray(value,array) {
1522 var count=array.length;
1524 for(var i=0;i<count;i++) {
1525 if(array[i]===value){
1533 // Tiny function to change the website url based on the selected element in the "PROVIDERS"
1535 function change_url(provider) {
1536 // Get and change the href to the corresponding url.
1537 document.getElementById("website").href = url[provider];
1540 // JQuery function to call corresponding function when
1541 // the ruleset provider is changed or the page is loaded for showing/hiding
1542 // the subscription_code area.
1543 \$(document).ready(function() {
1544 \$('#PROVIDER').change(update_provider);
1552 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids provider settings'});
1554 # Check if an existing provider should be edited.
1555 if($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1556 # Check if autoupdate is enabled for this provider.
1557 if ($used_providers{$cgiparams{'ID'}}[2] eq "enabled") {
1558 # Set the checkbox to be checked.
1559 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1561 } elsif ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'ids add provider'}") {
1562 # Set the autoupdate to true as default.
1563 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1567 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1568 <table width='100%' border='0'>
1570 <td colspan='2'><b>$Lang::tr{'ids provider'}</b></td>
1575 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1576 <select name='PROVIDER' id='PROVIDER'>
1579 # Loop through the array of ruleset providers.
1580 foreach my $provider (@ruleset_providers) {
1581 # Get the provider name.
1582 my $provider_name = &get_provider_name
($provider);
1584 # Pre-select the provider if one is given.
1585 if (($used_providers{$cgiparams{'ID'}}[0] eq "$provider") || ($cgiparams{'PROVIDER'} eq "$provider")) {
1586 $selected{$provider} = "selected='selected'";
1589 # Add the provider to the dropdown menu.
1590 print "<option value='$provider' $selected{$provider}>$provider_name</option>\n";
1597 <b><a id="website" target="_blank" href="#">$Lang::tr{'ids visit provider website'}</a></b>
1602 <td colspan='2'><br><br></td>
1605 <tr class='subscription_code' style='display:none' id='subscription_code'>
1610 <b>$Lang::tr{'subscription code'}</b>
1616 <input type='text' size='40' name='SUBSCRIPTION_CODE' value='$used_providers{$cgiparams{'ID'}}[1]'>
1629 <input type='checkbox' name='ENABLE_AUTOUPDATE' $checked{'ENABLE_AUTOUPDATE'}> $Lang::tr{'ids enable automatic updates'}
1634 <td colspan='2' align='right'>
1635 <input type='submit' value='$Lang::tr{'back'}'>
1638 # Check if a provider should be added or edited.
1639 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1640 # Display button for updating the existing provider.
1641 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'update'}'>\n";
1643 # Display button to add the new provider.
1644 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'add'}'>\n";
1653 &Header
::closebox
();
1657 ## A function to display a notice, to lock the webpage and
1658 ## tell the user which action currently will be performed.
1660 sub working_notice
($) {
1663 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1664 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1665 &Header
::openbox
( 'Waiting', 1,);
1669 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1674 &Header
::closebox
();
1675 &Header
::closebigbox
();
1676 &Header
::closepage
();
1680 ## A tiny function to perform a reload of the webpage after one second.
1683 print "<meta http-equiv='refresh' content='1'>\n";
1690 ## Private function to read-in and parse rules of a given rulefile.
1692 ## The given file will be read, parsed and all valid rules will be stored by ID,
1693 ## message/description and it's state in the idsrules hash.
1695 sub readrulesfile
($) {
1696 my $rulefile = shift;
1698 # Open rule file and read in contents
1699 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1701 # Store file content in an array.
1702 my @lines = <RULEFILE
>;
1707 # Loop over rule file contents
1708 foreach my $line (@lines) {
1709 # Remove whitespaces.
1713 next if ($line =~ /^\s*$/);
1719 # Gather rule sid and message from the ruleline.
1720 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1724 # Check if a rule has been found.
1726 # Add rule to the idsrules hash.
1727 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1729 # Grab status of the rule. Check if ruleline starts with a "dash".
1730 if ($line =~ /^\#/) {
1731 # If yes, the rule is disabled.
1732 $idsrules{$rulefile}{$sid}{'State'} = "off";
1734 # Otherwise the rule is enabled.
1735 $idsrules{$rulefile}{$sid}{'State'} = "on";
1743 ## Function to get the used memory of a given process-id.
1745 sub get_memory_usage
($) {
1750 # Try to open the status file for the given process-id on the pseudo
1752 if (open(FILE
, "/proc/$pid/status")) {
1753 # Loop through the entire file.
1755 # Splitt current line content and store them into variables.
1756 my ($key, $value) = split(":", $_, 2);
1758 # Check if the current key is the one which contains the memory usage.
1759 # The wanted one is VmRSS which contains the Real-memory (resident set)
1760 # of the entire process.
1761 if ($key eq "VmRSS") {
1762 # Found the memory usage add it to the memory variable.
1770 # Close file handle.
1773 # Return memory usage.
1777 # If the file could not be open, return nothing.
1782 ## Function to read-in the given enabled or disables sids file.
1784 sub read_enabled_disabled_sids_file
($) {
1787 # Temporary hash to store the sids and their state. It will be
1788 # returned at the end of this function.
1791 # Open the given filename.
1792 open(FILE
, "$file") or die "Could not open $file. $!\n";
1794 # Loop through the file.
1800 next if ($_ =~ /^\s*$/);
1803 next if ($_ =~ /^\#/);
1805 # Splitt line into sid and state part.
1806 my ($state, $sid) = split(" ", $_);
1808 # Skip line if the sid is not numeric.
1809 next unless ($sid =~ /\d+/ );
1811 # Check if the sid was enabled.
1812 if ($state eq "enablesid") {
1813 # Add the sid and its state as enabled to the temporary hash.
1814 $temphash{$sid} = "enabled";
1815 # Check if the sid was disabled.
1816 } elsif ($state eq "disablesid") {
1817 # Add the sid and its state as disabled to the temporary hash.
1818 $temphash{$sid} = "disabled";
1819 # Invalid state - skip the current sid and state.
1833 ## Function to get the provider name from the language file or providers file for a given handle.
1835 sub get_provider_name
($) {
1839 # Get the required translation string for the given provider handle.
1840 my $tr_string = $IDS::Ruleset
::Providers
{$handle}{'tr_string'};
1842 # Check if the translation string is available in the language files.
1843 if ($Lang::tr
{$tr_string}) {
1844 # Use the translated string from the language file.
1845 $provider_name = $Lang::tr
{$tr_string};
1847 # Fallback and use the provider summary from the providers file.
1848 $provider_name = $IDS::Ruleset
::Providers
{$handle}{'summary'};
1851 # Return the obtained provider name.
1852 return $provider_name;
1856 ## Private function to convert a given rulefile to a category name.
1857 ## ( No file extension anymore and if the name contained a dot, it
1858 ## would be replaced by a underline sign.)
1860 sub _rulefile_to_category
($) {
1861 my ($filename) = @_;
1863 # Splitt the filename into single chunks and store them in a
1865 my @parts = split(/\./, $filename);
1867 # Return / Remove last element of the temporary array.
1868 # This removes the file extension.
1871 # Join together the single elements of the temporary array.
1872 # If these are more than one, use a "underline" for joining.
1873 my $category = join '_', @parts;
1875 # Return the converted filename.