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.
258 # Get enabled providers.
259 my @enabled_providers = &IDS
::get_enabled_providers
();
261 # Open rules directory and do a directory listing.
262 opendir(DIR
, $IDS::rulespath
) or die $!;
263 # Loop through the direcory.
264 while (my $file = readdir(DIR
)) {
266 # We only want files.
267 next unless (-f
"$IDS::rulespath/$file");
269 # Ignore empty files.
270 next if (-z
"$IDS::rulespath/$file");
272 # Use a regular expression to find files ending in .rules
273 next unless ($file =~ m/\.rules$/);
275 # Ignore files which are not read-able.
276 next unless (-R
"$IDS::rulespath/$file");
278 # Skip whitelist rules file.
279 next if( $file eq "whitelist.rules");
281 # Splitt vendor from filename.
282 my @filename_parts = split(/-/, $file);
284 # Assign vendor name for easy processing.
285 my $vendor = @filename_parts[0];
287 # Skip rulefile if the provider is disabled.
288 next unless ($vendor ~~ @enabled_providers);
290 # Call subfunction to read-in rulefile and add rules to
292 &readrulesfile
("$file");
297 # Loop through the array of used providers.
298 foreach my $provider (@enabled_providers) {
299 # Gather used rulefiles.
300 my @used_rulesfiles = &IDS
::read_used_provider_rulesfiles
($provider);
302 # Loop through the array of used rulesfiles.
303 foreach my $rulefile (@used_rulesfiles) {
304 # Check if the current rulefile exists in the %idsrules hash.
305 # If not, the file probably does not exist anymore or contains
307 if($idsrules{$rulefile}) {
308 # Add the rulefile state to the %idsrules hash.
309 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
316 if ($cgiparams{'RULESET'} eq $Lang::tr
{'ids apply'}) {
317 # Arrays to store which rulefiles have been enabled and will be used.
318 my @enabled_rulefiles;
320 # Store if a restart of suricata is required.
321 my $suricata_restart_required;
323 # Loop through the hash of idsrules.
324 foreach my $rulefile(keys %idsrules) {
325 # Check if the state of the rulefile has been changed.
326 unless ($cgiparams{$rulefile} eq $idsrules{$rulefile}{'Rulefile'}{'State'}) {
327 # A restart of suricata is required to apply the changes of the used rulefiles.
328 $suricata_restart_required = 1;
331 # Check if the rulefile is enabled.
332 if ($cgiparams{$rulefile} eq "on") {
333 # Add rulefile to the array of enabled rulefiles.
334 push(@enabled_rulefiles, $rulefile);
336 # Drop item from cgiparams hash.
337 delete $cgiparams{$rulefile};
341 # Open oinkmaster main include file for provider modifications.
342 open(OINKM_INCL_FILE
, ">", "$IDS::oinkmaster_provider_includes_file") or die "Could not open $IDS::oinkmaster_provider_includes_file. $!\n";
344 # Print file header and notice about autogenerated file.
345 print OINKM_INCL_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
347 # Get enabled providers.
348 my @enabled_providers = &IDS
::get_enabled_providers
();
350 # Loop through the array of enabled providers.
351 foreach my $provider (@enabled_providers) {
352 # Hash to store the used-enabled and disabled sids.
353 my %enabled_disabled_sids;
355 # Generate modified sids file name for the current processed provider.
356 my $providers_modified_sids_file = &IDS
::get_oinkmaster_provider_modified_sids_file
($provider);
358 # Check if a modified sids file for this provider exists.
359 if (-f
$providers_modified_sids_file) {
360 # Read-in the file for enabled/disabled sids.
361 %enabled_disabled_sids = &read_enabled_disabled_sids_file
($providers_modified_sids_file);
364 # Loop through the hash of idsrules.
365 foreach my $rulefile (keys %idsrules) {
366 # Split the rulefile to get the vendor.
367 my @filename_parts = split(/-/, $rulefile);
369 # Assign rulefile vendor.
370 my $rulefile_vendor = @filename_parts[0];
372 # Skip the rulefile if the vendor is not our current processed provider.
373 next unless ($rulefile_vendor eq $provider);
375 # Loop through the single rules of the rulefile.
376 foreach my $sid (keys %{$idsrules{$rulefile}}) {
377 # Skip the current sid if it is not numeric.
378 next unless ($sid =~ /\d+/ );
380 # Check if there exists a key in the cgiparams hash for this sid.
381 if (exists($cgiparams{$sid})) {
382 # Look if the rule is disabled.
383 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
384 # Check if the state has been set to 'on'.
385 if ($cgiparams{$sid} eq "on") {
386 # Add/Modify the sid to/in the enabled_disabled_sids hash.
387 $enabled_disabled_sids{$sid} = "enabled";
389 # Drop item from cgiparams hash.
390 delete $cgiparams{$rulefile}{$sid};
394 # Look if the rule is enabled.
395 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
396 # Check if the state is 'on' and should be disabled.
397 # In this case there is no entry
398 # for the sid in the cgiparams hash.
399 # Add/Modify it to/in the enabled_disabled_sids hash.
400 $enabled_disabled_sids{$sid} = "disabled";
402 # Drop item from cgiparams hash.
403 delete $cgiparams{$rulefile}{$sid};
409 # Check if the hash for enabled/disabled sids contains any entries.
410 if (%enabled_disabled_sids) {
411 # Open providers modified sids file for writing.
412 open(PROVIDER_MOD_FILE
, ">$providers_modified_sids_file") or die "Could not write to $providers_modified_sids_file. $!\n";
414 # Write header to the files.
415 print PROVIDER_MOD_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
417 # Loop through the hash.
418 foreach my $sid (keys %enabled_disabled_sids) {
419 # Check if the sid is enabled.
420 if ($enabled_disabled_sids{$sid} eq "enabled") {
421 # Print the sid to the enabled_sids file.
422 print PROVIDER_MOD_FILE
"enablesid $sid\n";
423 # Check if the sid is disabled.
424 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
425 # Print the sid to the disabled_sids file.
426 print PROVIDER_MOD_FILE
"disablesid $sid\n";
427 # Something strange happende - skip the current sid.
433 # Close file handle for the providers modified sids file.
434 close(PROVIDER_MOD_FILE
);
436 # Add the file to the oinkmasters include file.
437 print OINKM_INCL_FILE
"include $providers_modified_sids_file\n";
441 # Close the file handle after writing.
442 close(OINKM_INCL_FILE
);
444 # Handle enabled / disabled rulefiles.
447 # Loop through the array of enabled providers.
448 foreach my $provider(@enabled_providers) {
449 # Array to store the rulefiles which belong to the current processed provider.
450 my @provider_rulefiles = ();
452 # Loop through the array of enabled rulefiles.
453 foreach my $rulesfile (@enabled_rulefiles) {
454 # Split the rulefile name.
455 my @filename_parts = split(/-/, "$rulesfile");
457 # Assign vendor name for easy processings.
458 my $vendor = @filename_parts[0];
460 # Check if the rulesvendor is our current processed enabled provider.
461 if ("$vendor" eq "$provider") {
462 # Add the rulesfile to the array of provider rulesfiles.
463 push(@provider_rulefiles, $rulesfile);
466 # Check if any rulesfiles have been found for this provider.
467 if (@provider_rulefiles) {
468 # Call function and write the providers used rulesfile file.
469 &IDS
::write_used_provider_rulefiles_file
($provider, @provider_rulefiles);
474 # Call function to generate and write the used rulefiles file.
475 &IDS
::write_main_used_rulefiles_file
(@enabled_providers);
477 # Lock the webpage and print message.
478 &working_notice
("$Lang::tr{'ids apply ruleset changes'}");
480 # Call oinkmaster to alter the ruleset.
483 # Check if the IDS is running.
484 if(&IDS
::ids_is_running
()) {
485 # Check if a restart of suricata is required.
486 if ($suricata_restart_required) {
487 # Call suricatactrl to perform the restart.
488 &IDS
::call_suricatactrl
("restart");
490 # Call suricatactrl to perform a reload.
491 &IDS
::call_suricatactrl
("reload");
498 # Download new ruleset.
499 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'update ruleset'}) {
500 # Check if the red device is active.
501 unless (-e
"${General::swroot}/red/active") {
502 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
505 # Check if enought free disk space is availabe.
506 if(&IDS
::checkdiskspace
()) {
507 $errormessage = "$Lang::tr{'not enough disk space'}";
510 # Check if any errors happend.
511 unless ($errormessage) {
512 # Lock the webpage and print notice about downloading
514 &working_notice
("$Lang::tr{'ids download new ruleset'}");
516 # Call subfunction to download the ruleset.
517 if(&IDS
::downloadruleset
()) {
518 $errormessage = $Lang::tr
{'could not download latest updates'};
520 # Call function to store the errormessage.
521 &IDS
::_store_error_message
($errormessage);
523 # Preform a reload of the page.
526 # Call subfunction to launch oinkmaster.
529 # Check if the IDS is running.
530 if(&IDS
::ids_is_running
()) {
531 # Call suricatactrl to perform a reload.
532 &IDS
::call_suricatactrl
("reload");
535 # Perform a reload of the page.
540 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
543 my $monitored_zones = 0;
545 # Read-in current (old) IDS settings.
546 &General
::readhash
("$IDS::ids_settings_file", \
%oldidssettings);
548 # Prevent form name from been stored in conf file.
549 delete $cgiparams{'IDS'};
551 # Check if the IDS should be enabled.
552 if ($cgiparams{'ENABLE_IDS'} eq "on") {
553 # Get enabled providers.
554 my @enabled_providers = &IDS
::get_enabled_providers
();
556 # Check if any ruleset is available. Otherwise abort and display an error.
557 unless(@enabled_providers) {
558 $errormessage = $Lang::tr
{'ids no enabled ruleset provider'};
561 # Loop through the array of available interfaces.
562 foreach my $zone (@network_zones) {
563 # Convert interface name into upper case.
564 my $zone_upper = uc($zone);
566 # Check if the IDS is enabled for this interaces.
567 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
573 # Check if at least one zone should be monitored, or show an error.
574 unless ($monitored_zones >= 1) {
575 $errormessage = $Lang::tr
{'ids no network zone'};
579 # Go on if there are no error messages.
580 if (!$errormessage) {
581 # Store settings into settings file.
582 &General
::writehash
("$IDS::ids_settings_file", \
%cgiparams);
585 # Check if the the automatic rule update hass been touched.
586 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldidssettings{'AUTOUPDATE_INTERVAL'}) {
587 # Call suricatactrl to set the new interval.
588 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
591 # Generate file to store the home net.
592 &IDS
::generate_home_net_file
();
594 # Generate file to the store the DNS servers.
595 &IDS
::generate_dns_servers_file
();
597 # Generate file to store the HTTP ports.
598 &IDS
::generate_http_ports_file
();
600 # Write the modify sid's file and pass the taken ruleaction.
601 &IDS
::write_modify_sids_file
();
603 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
604 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
605 # Check if a ruleset exists.
606 if (%used_providers) {
607 # Lock the webpage and print message.
608 &working_notice
("$Lang::tr{'ids working'}");
610 # Call oinkmaster to alter the ruleset.
613 # Set reload_page to "True".
618 # Check if the IDS currently is running.
619 if(&IDS
::ids_is_running
()) {
620 # Check if ENABLE_IDS is set to on.
621 if($cgiparams{'ENABLE_IDS'} eq "on") {
622 # Call suricatactrl to perform a reload of suricata.
623 &IDS
::call_suricatactrl
("reload");
625 # Call suricatactrl to stop suricata.
626 &IDS
::call_suricatactrl
("stop");
629 # Call suricatactrl to start suricata.
630 &IDS
::call_suricatactrl
("start");
633 # Check if the page should be reloaded.
635 # Perform a reload of the page.
639 # Toggle Enable/Disable autoupdate for a provider
640 } elsif ($cgiparams{'AUTOUPDATE'} eq $Lang::tr
{'toggle enable disable'}) {
641 my %used_providers = ();
643 # Only go further, if an ID has been passed.
644 if ($cgiparams{'ID'}) {
645 # Assign the given ID.
646 my $id = $cgiparams{'ID'};
648 # Undef the given ID.
649 undef($cgiparams{'ID'});
651 # Read-in providers settings file.
652 &General
::readhasharray
($IDS::providers_settings_file
, \
%used_providers);
654 # Grab the configured status of the corresponding entry.
655 my $status_autoupdate = $used_providers{$id}[2];
658 if ($status_autoupdate eq "disabled") {
659 $status_autoupdate = "enabled";
661 $status_autoupdate = "disabled";
664 # Modify the status of the existing entry.
665 $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$status_autoupdate", "$used_providers{$id}[3]"];
667 # Write the changed hash to the providers settings file.
668 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
671 # Add/Edit a provider to the list of used providers.
673 } elsif (($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'update'}")) {
674 my %used_providers = ();
676 # Read-in providers settings file.
677 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
679 # Assign some nice human-readable values.
680 my $provider = $cgiparams{'PROVIDER'};
681 my $subscription_code = $cgiparams{'SUBSCRIPTION_CODE'};
682 my $status_autoupdate;
684 # Handle autoupdate checkbox.
685 if ($cgiparams{'ENABLE_AUTOUPDATE'} eq "on") {
686 $status_autoupdate = "enabled";
688 $status_autoupdate = "disabled";
691 # Check if we are going to add a new provider.
692 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") {
693 # Loop through the hash of used providers.
694 foreach my $id ( keys %used_providers) {
695 # Check if the choosen provider is already in use.
696 if ($used_providers{$id}[0] eq "$provider") {
697 # Assign error message.
698 $errormessage = "$Lang::tr{'ids the choosen provider is already in use'}";
703 # Check if the provider requires a subscription code.
704 if ($IDS::Ruleset
::Providers
{$provider}{'requires_subscription'} eq "True") {
705 # Check if an subscription code has been provided.
706 if ($subscription_code) {
707 # Check if the code contains unallowed chars.
708 unless ($subscription_code =~ /^[a-z0-9]+$/) {
709 $errormessage = $Lang::tr
{'invalid input for subscription code'};
712 # Print an error message, that an subsription code is required for this
714 $errormessage = $Lang::tr
{'ids subscription code required'};
718 # Go further if there was no error.
719 if ($errormessage eq '') {
723 # Check if we should edit an existing entry and got an ID.
724 if (($cgiparams{'PROVIDERS'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
725 # Assin the provided id.
726 $id = $cgiparams{'ID'};
728 # Undef the given ID.
729 undef($cgiparams{'ID'});
731 # Grab the configured status of the corresponding entry.
732 $status = $used_providers{$id}[3];
734 # Each newly added entry automatically should be enabled.
737 # Generate the ID for the new entry.
739 # Sort the keys by their ID and store them in an array.
740 my @keys = sort { $a <=> $b } keys %used_providers;
742 # Reverse the key array.
743 my @reversed = reverse(@keys);
745 # Obtain the last used id.
746 my $last_id = @reversed[0];
748 # Increase the last id by one and use it as id for the new entry.
752 # Add/Modify the entry to/in the used providers hash..
753 $used_providers{$id} = ["$provider", "$subscription_code", "$status_autoupdate", "$status"];
755 # Write the changed hash to the providers settings file.
756 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
758 # Check if a new provider will be added.
759 if ($cgiparams{'PROVIDERS'} eq $Lang::tr
{'add'}) {
760 # Lock the webpage and print notice about downloading
762 &working_notice
("$Lang::tr{'ids working'}");
764 # Download the ruleset.
765 &IDS
::downloadruleset
($provider);
767 # Extract the ruleset
768 &IDS
::extractruleset
($provider);
771 &IDS
::move_tmp_ruleset
();
773 # Cleanup temporary directory.
774 &IDS
::cleanup_tmp_directory
();
776 # Create new empty file for used rulefiles
778 &IDS
::write_used_provider_rulefiles_file
($provider);
780 # Perform a reload of the page.
784 # Undefine providers flag.
785 undef($cgiparams{'PROVIDERS'});
788 ## Toggle Enabled/Disabled for an existing provider.
790 } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr
{'toggle enable disable'}) {
791 my %used_providers = ();
792 my $provider_includes_action;
794 # Only go further, if an ID has been passed.
795 if ($cgiparams{'ID'}) {
796 # Assign the given ID.
797 my $id = $cgiparams{'ID'};
799 # Undef the given ID.
800 undef($cgiparams{'ID'});
802 # Read-in file which contains the provider settings.
803 &General
::readhasharray
($IDS::providers_settings_file
, \
%used_providers);
805 # Grab the configured status of the corresponding entry.
806 my $status = $used_providers{$id}[3];
808 # Grab the provider handle.
809 my $provider_handle = $used_providers{$id}[0];
812 if ($status eq "enabled") {
813 $status = "disabled";
815 # Set the provider includes action to "remove" for removing the entry.
816 $provider_includes_action = "remove";
820 # Set the provider includes action to "add".
821 $provider_includes_action = "add";
824 # Modify the status of the existing entry.
825 $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$used_providers{$id}[2]", "$status"];
827 # Write the changed hash to the providers settings file.
828 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
830 # Get all enabled providers.
831 my @enabled_providers = &IDS
::get_enabled_providers
();
833 # Write the main providers include file.
834 &IDS
::write_main_used_rulefiles_file
(@enabled_providers);
836 # Call function to alter the oinkmasters provider includes file and
837 # add or remove the provider.
838 &IDS
::alter_oinkmaster_provider_includes_file
($provider_includes_action, $provider_handle);
840 # Check if the IDS is running.
841 if(&IDS
::ids_is_running
()) {
842 # Gather the amount of enabled providers (elements in the array).
843 my $amount = @enabled_providers;
845 # Check if there are still enabled ruleset providers.
847 # Call suricatactrl to perform a restart.
848 &IDS
::call_suricatactrl
("restart");
850 # No active ruleset provider, suricata has to be stopped.
853 &IDS
::call_suricatactrl
("stop");
857 # Undefine providers flag.
858 undef($cgiparams{'PROVIDERS'});
861 ## Remove provider from the list of used providers.
863 } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr
{'remove'}) {
864 my %used_providers = ();
866 # Read-in provider settings file.
867 &General
::readhasharray
($IDS::providers_settings_file
, \
%used_providers);
869 # Grab the provider name bevore deleting it from hash.
870 my $provider = $used_providers{$cgiparams{'ID'}}[0];
872 # Drop entry from the hash.
873 delete($used_providers{$cgiparams{'ID'}});
875 # Undef the given ID.
876 undef($cgiparams{'ID'});
878 # Write the changed hash to the provide settings file.
879 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
881 # Lock the webpage and print message.
882 &working_notice
("$Lang::tr{'ids apply ruleset changes'}");
884 # Drop the stored ruleset file.
885 &IDS
::drop_dl_rulesfile
($provider);
887 # Get the name of the provider rulessets include file.
888 my $provider_used_rulefile = &IDS
::get_used_provider_rulesfile_file
($provider);
890 # Drop the file, it is not longer needed.
891 unlink("$provider_used_rulefile");
893 # Call function to get the path and name for the given providers
894 # oinkmaster modified sids file.
895 my $provider_modified_sids_file = &IDS
::get_oinkmaster_provider_modified_sids_file
($provider);
897 # Check if the file exists.
898 if (-f
$provider_modified_sids_file) {
899 # Remove the file, which is not longer needed.
900 unlink("$provider_modified_sids_file");
903 # Alter the oinkmaster provider includes file and remove the provider.
904 &IDS
::alter_oinkmaster_provider_includes_file
("remove", $provider);
906 # Regenerate ruleset.
909 # Gather all enabled providers.
910 my @enabled_providers = &IDS
::get_enabled_providers
();
912 # Regenerate main providers include file.
913 &IDS
::write_main_used_rulefiles_file
(@enabled_providers);
915 # Check if the IDS is running.
916 if(&IDS
::ids_is_running
()) {
917 # Get amount of enabled providers.
918 my $amount = @enabled_providers;
920 # Check if at least one enabled provider remains.
922 # Call suricatactrl to perform a reload.
923 &IDS
::call_suricatactrl
("restart");
925 # Stop suricata if no enabled provider remains.
927 # Call suricatactrel to perform the stop.
928 &IDS
::call_suricatactrl
("stop");
932 # Undefine providers flag.
933 undef($cgiparams{'PROVIDERS'});
939 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
941 &Header
::openbigbox
('100%', 'left', '', $errormessage);
943 &show_display_error_message
();
945 if ($cgiparams{'RULESET'} eq "$Lang::tr{'ids customize ruleset'}" ) {
946 &show_customize_ruleset
();
947 } elsif ($cgiparams{'PROVIDERS'} ne "") {
948 &show_add_provider
();
953 &Header
::closebigbox
();
954 &Header
::closepage
();
957 ## Tiny function to show if a error message happened.
959 sub show_display_error_message
() {
961 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
962 print "<class name='base'>$errormessage\n";
963 print " </class>\n";
969 ## Function to display the main IDS page.
971 sub show_mainpage
() {
972 # Read-in idssettings and provider settings.
973 &General
::readhash
("$IDS::ids_settings_file", \
%idssettings);
974 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
976 # If no autoupdate intervall has been configured yet, set default value.
977 unless(exists($idssettings{'AUTOUPDATE_INTERVAL'})) {
978 # Set default to "weekly".
979 $idssettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
982 # Read-in ignored hosts.
983 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
985 $checked{'ENABLE_IDS'}{'off'} = '';
986 $checked{'ENABLE_IDS'}{'on'} = '';
987 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
988 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
989 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
990 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
991 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
992 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
993 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
994 $selected{'AUTOUPDATE_INTERVAL'}{$idssettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
996 # Draw current state of the IDS
997 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
999 # Check if the IDS is running and obtain the process-id.
1000 my $pid = &IDS
::ids_is_running
();
1002 # Display some useful information, if suricata daemon is running.
1004 # Gather used memory.
1005 my $memory = &get_memory_usage
($pid);
1008 <table width='95%' cellspacing='0' class='tbl'>
1010 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
1014 <td class='base'>$Lang::tr{'guardian daemon'}</td>
1015 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
1019 <td class='base'></td>
1020 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
1021 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
1025 <td class='base'></td>
1026 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
1027 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
1032 # Otherwise display a hint that the service is not launched.
1034 <table width='95%' cellspacing='0' class='tbl'>
1036 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
1040 <td class='base'>$Lang::tr{'guardian daemon'}</td>
1041 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
1047 # Only show this area, if at least one ruleset provider is configured.
1048 if (%used_providers) {
1052 <br><br><h2>$Lang::tr{'settings'}</h2>
1054 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1055 <table width='100%' border='0'>
1057 <td class='base' colspan='2'>
1058 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}> $Lang::tr{'ids enable'}
1061 <td class='base' colspan='2'>
1062 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}> $Lang::tr{'ids monitor traffic only'}
1074 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
1081 # Loop through the array of available networks and print config options.
1082 foreach my $zone (@network_zones) {
1084 my $checked_forward;
1086 # Convert current zone name to upper case.
1087 my $zone_upper = uc($zone);
1090 my $zone_name = $zone;
1092 # Dirty hack to get the correct language string for the red zone.
1093 if ($zone eq "red") {
1094 $zone_name = "red1";
1097 # Grab checkbox status from settings hash.
1098 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
1099 $checked_input = "checked = 'checked'";
1102 print "<td class='base' width='20%'>\n";
1103 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
1104 print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
1119 <td colspan='4'><b>$Lang::tr{'ids automatic rules update'}</b></td>
1124 <select name='AUTOUPDATE_INTERVAL'>
1125 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
1126 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
1127 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
1135 <table width='100%'>
1137 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
1146 &Header
::closebox
();
1149 # Used Ruleset Providers section.
1151 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
1154 <table width='100%' border='0'>
1156 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ids provider'}</b></td>
1157 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'date'}</b></td>
1158 <td class='base' bgcolor='$color{'color20'}' align='center'><b>$Lang::tr{'ids autoupdates'}</b></td>
1159 <td class='base' bgcolor='$color{'color20'}'></td>
1160 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1163 # Check if some providers has been configured.
1164 if (keys (%used_providers)) {
1167 # Loop through all entries of the hash.
1168 foreach my $id (sort keys(%used_providers)) {
1169 # Assign data array positions to some nice variable names.
1170 my $provider = $used_providers{$id}[0];
1171 my $provider_name = &get_provider_name
($provider);
1172 my $rulesetdate = &IDS
::get_ruleset_date
($provider);
1174 my $subscription_code = $used_providers{$id}[1];
1175 my $autoupdate_status = $used_providers{$id}[2];
1176 my $status = $used_providers{$id}[3];
1178 # Check if the item number is even or not.
1180 $col="bgcolor='$color{'color22'}'";
1182 $col="bgcolor='$color{'color20'}'";
1185 # Choose icons for the checkboxes.
1188 my $autoupdate_status_gif;
1189 my $autoupdate_status_gdesc;
1191 # Check if the status is enabled and select the correct image and description.
1192 if ($status eq 'enabled' ) {
1193 $status_gif = 'on.gif';
1194 $status_gdesc = $Lang::tr
{'click to disable'};
1196 $status_gif = 'off.gif';
1197 $status_gdesc = $Lang::tr
{'click to enable'};
1200 # Check if the autoupdate status is enabled and select the correct image and description.
1201 if ($autoupdate_status eq 'enabled') {
1202 $autoupdate_status_gif = 'on.gif';
1203 $autoupdate_status_gdesc = $Lang::tr
{'click to disable'};
1205 $autoupdate_status_gif = 'off.gif';
1206 $autoupdate_status_gdesc = $Lang::tr
{'click to enable'};
1211 <td width='33%' class='base' $col>$provider_name</td>
1212 <td width='30%' class='base' $col>$rulesetdate</td>
1214 <td align='center' $col>
1215 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1216 <input type='hidden' name='AUTOUPDATE' value='$Lang::tr{'toggle enable disable'}' />
1217 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$autoupdate_status_gif' alt='$autoupdate_status_gdesc' title='$autoupdate_status_gdesc' />
1218 <input type='hidden' name='ID' value='$id' />
1222 <td align='center' $col>
1223 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1224 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'toggle enable disable'}'>
1225 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$status_gif' alt='$status_gdesc' title='$status_gdesc'>
1226 <input type='hidden' name='ID' value='$id'>
1230 <td align='center' $col>
1231 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1232 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'edit'}'>
1233 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1234 <input type='hidden' name='ID' value='$id'>
1238 <td align='center' $col>
1239 <form method='post' name='$provider' action='$ENV{'SCRIPT_NAME'}'>
1240 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1241 <input type='hidden' name='ID' value='$id'>
1242 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'remove'}'>
1250 # Print notice that currently no hosts are ignored.
1252 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1258 # Section to add new elements or edit existing ones.
1265 <table width='100%'>
1266 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1270 # Only show this button if a ruleset provider is configured.
1271 if (%used_providers) {
1272 print "<input type='submit' name='RULESET' value='$Lang::tr{'ids customize ruleset'}'>\n";
1275 <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids add provider'}'>
1282 &Header
::closebox
();
1285 # Whitelist / Ignorelist
1287 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ignored hosts'});
1290 <table width='100%'>
1292 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
1293 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
1294 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1297 # Check if some hosts have been added to be ignored.
1298 if (keys (%ignored)) {
1301 # Loop through all entries of the hash.
1302 while( (my $key) = each %ignored) {
1303 # Assign data array positions to some nice variable names.
1304 my $address = $ignored{$key}[0];
1305 my $remark = $ignored{$key}[1];
1306 my $status = $ignored{$key}[2];
1308 # Check if the key (id) number is even or not.
1309 if ($cgiparams{'ID'} eq $key) {
1310 $col="bgcolor='${Header::colouryellow}'";
1311 } elsif ($key % 2) {
1312 $col="bgcolor='$color{'color22'}'";
1314 $col="bgcolor='$color{'color20'}'";
1317 # Choose icon for the checkbox.
1321 # Check if the status is enabled and select the correct image and description.
1322 if ($status eq 'enabled' ) {
1324 $gdesc = $Lang::tr
{'click to disable'};
1327 $gdesc = $Lang::tr
{'click to enable'};
1332 <td width='20%' class='base' $col>$address</td>
1333 <td width='65%' class='base' $col>$remark</td>
1335 <td align='center' $col>
1336 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1337 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}'>
1338 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc'>
1339 <input type='hidden' name='ID' value='$key'>
1343 <td align='center' $col>
1344 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1345 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}'>
1346 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1347 <input type='hidden' name='ID' value='$key'>
1351 <td align='center' $col>
1352 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
1353 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1354 <input type='hidden' name='ID' value='$key'>
1355 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1362 # Print notice that currently no hosts are ignored.
1364 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1370 # Section to add new elements or edit existing ones.
1376 <div align='center'>
1377 <table width='100%'>
1380 # Assign correct headline and button text.
1385 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1386 if ($cgiparams{'ID'} ne '') {
1387 $buttontext = $Lang::tr
{'update'};
1388 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1390 # Grab address and remark for the given key.
1391 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1392 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1394 $buttontext = $Lang::tr
{'add'};
1395 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1399 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1400 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1402 <td width='30%'>$Lang::tr{'ip address'}: </td>
1403 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1405 <td width='30%'>$Lang::tr{'remark'}: </td>
1406 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1407 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1414 &Header
::closebox
();
1418 ## Function to show the customize ruleset section.
1420 sub show_customize_ruleset
() {
1424 # Java script variable declaration for show and hide.
1425 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
1426 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
1429 // Tiny java script function to show/hide the rules
1430 // of a given category.
1431 function showhide(tblname) {
1432 \$("#" + tblname).toggle();
1434 // Get current content of the span element.
1435 var content = document.getElementById("span_" + tblname);
1437 if (content.innerHTML === show) {
1438 content.innerHTML = hide;
1440 content.innerHTML = show;
1446 &Header
::openbox
('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'}" );
1447 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1449 # Output display table for rule files
1450 print "<table width='100%'>\n";
1452 # Loop over each rule file
1453 foreach my $rulefile (sort keys(%idsrules)) {
1454 my $rulechecked = '';
1456 # Check if rule file is enabled
1457 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1458 $rulechecked = 'CHECKED';
1461 # Convert rulefile name into category name.
1462 my $categoryname = &_rulefile_to_category
($rulefile);
1464 # Table and rows for the rule files.
1466 print"<td class='base' width='5%'>\n";
1467 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1469 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1470 print"<td class='base' width='5%' align='right'>\n";
1471 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1475 # Rows which will be hidden per default and will contain the single rules.
1476 print"<tr style='display:none' id='$categoryname'>\n";
1477 print"<td colspan='3'>\n";
1484 # New table for the single rules.
1485 print "<table width='100%'>\n";
1487 # Loop over rule file rules
1488 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1490 my $ruledefchecked = '';
1492 # Skip rulefile itself.
1493 next if ($sid eq "Rulefile");
1495 # If 2 rules have been displayed, start a new row
1496 if (($lines % 2) == 0) {
1497 print "</tr><tr>\n";
1499 # Increase rows by once.
1505 $col="bgcolor='$color{'color20'}'";
1507 $col="bgcolor='$color{'color22'}'";
1511 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1512 $ruledefchecked = 'CHECKED';
1515 # Create rule checkbox and display rule description
1516 print "<td class='base' width='5%' align='right' $col>\n";
1517 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1519 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1521 # Increment rule count
1525 # If do not have a second rule for row, create empty cell
1526 if (($lines % 2) != 0) {
1527 print "<td class='base'></td>";
1530 # Close display table
1531 print "</tr></table></td></tr>";
1534 # Close display table
1538 <table width='100%'>
1540 <td width='100%' align='right'>
1541 <input type='submit' value='$Lang::tr{'fwhost back'}'>
1542 <input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'>
1549 &Header
::closebox
();
1554 ## Function to show section for add/edit a provider.
1556 sub show_add_provider
() {
1557 my %used_providers = ();
1558 my @subscription_providers;
1560 # Read -in providers settings file.
1561 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
1563 # Get all supported ruleset providers.
1564 my @ruleset_providers = &IDS
::get_ruleset_providers
();
1569 # Generate Java Script Object which contains the URL of the providers.
1570 print "\t// Object, which contains the webpages of the ruleset providers.\n";
1571 print "\tvar url = {\n";
1573 # Loop through the array of supported providers.
1574 foreach my $provider (@ruleset_providers) {
1575 # Check if the provider requires a subscription.
1576 if ($IDS::Ruleset
::Providers
{$provider}{'requires_subscription'} eq "True") {
1577 # Add the provider to the array of subscription_providers.
1578 push(@subscription_providers, $provider);
1581 # Grab the URL for the provider.
1582 my $url = $IDS::Ruleset
::Providers
{$provider}{'website'};
1584 # Print the URL to the Java Script Object.
1585 print "\t\t$provider: \"$url\"\,\n";
1588 # Close the Java Script Object declaration.
1591 # Generate Java Script Array which contains the provider that requires a subscription.
1593 $line = join("', '", @subscription_providers);
1595 print "\t// Array which contains the providers that requires a subscription.\n";
1596 print "\tsubscription_provider = ['$line']\;\n\n";
1599 // Java Script function to swap the text input field for
1600 // entering a subscription code.
1601 var update_provider = function() {
1602 if(inArray(\$('#PROVIDER').val(), subscription_provider)) {
1603 \$('.subscription_code').show();
1605 \$('.subscription_code').hide();
1608 // Call function to change the website url.
1609 change_url(\$('#PROVIDER').val());
1612 // Java Script function to check if a given value is part of
1614 function inArray(value,array) {
1615 var count=array.length;
1617 for(var i=0;i<count;i++) {
1618 if(array[i]===value){
1626 // Tiny function to change the website url based on the selected element in the "PROVIDERS"
1628 function change_url(provider) {
1629 // Get and change the href to the corresponding url.
1630 document.getElementById("website").href = url[provider];
1633 // JQuery function to call corresponding function when
1634 // the ruleset provider is changed or the page is loaded for showing/hiding
1635 // the subscription_code area.
1636 \$(document).ready(function() {
1637 \$('#PROVIDER').change(update_provider);
1645 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids provider settings'});
1647 # Check if an existing provider should be edited.
1648 if($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1649 # Check if autoupdate is enabled for this provider.
1650 if ($used_providers{$cgiparams{'ID'}}[2] eq "enabled") {
1651 # Set the checkbox to be checked.
1652 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1654 } elsif ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'ids add provider'}") {
1655 # Set the autoupdate to true as default.
1656 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1660 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1661 <table width='100%' border='0'>
1663 <td colspan='2'><b>$Lang::tr{'ids provider'}</b></td>
1668 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1671 # Value to allow disabling the dropdown menu.
1674 # Check if we are in edit mode.
1675 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1676 $disabled = "disabled";
1678 # Add hidden input with the provider because the disable select does not provider
1680 print "<input type='hidden' name='PROVIDER' value='$used_providers{$cgiparams{'ID'}}[0]'>\n";
1683 print "<select name='PROVIDER' id='PROVIDER' $disabled>\n";
1684 # Loop through the array of ruleset providers.
1685 foreach my $provider (@ruleset_providers) {
1686 # Get the provider name.
1687 my $provider_name = &get_provider_name
($provider);
1689 # Pre-select the provider if one is given.
1690 if (($used_providers{$cgiparams{'ID'}}[0] eq "$provider") || ($cgiparams{'PROVIDER'} eq "$provider")) {
1691 $selected{$provider} = "selected='selected'";
1694 # Add the provider to the dropdown menu.
1695 print "<option value='$provider' $selected{$provider}>$provider_name</option>\n";
1702 <b><a id="website" target="_blank" href="#">$Lang::tr{'ids visit provider website'}</a></b>
1707 <td colspan='2'><br><br></td>
1710 <tr class='subscription_code' style='display:none' id='subscription_code'>
1715 <b>$Lang::tr{'subscription code'}</b>
1721 <input type='text' size='40' name='SUBSCRIPTION_CODE' value='$used_providers{$cgiparams{'ID'}}[1]'>
1734 <input type='checkbox' name='ENABLE_AUTOUPDATE' $checked{'ENABLE_AUTOUPDATE'}> $Lang::tr{'ids enable automatic updates'}
1739 <td colspan='2' align='right'>
1740 <input type='submit' value='$Lang::tr{'back'}'>
1743 # Check if a provider should be added or edited.
1744 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1745 # Display button for updating the existing provider.
1746 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'update'}'>\n";
1748 # Display button to add the new provider.
1749 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'add'}'>\n";
1758 &Header
::closebox
();
1762 ## A function to display a notice, to lock the webpage and
1763 ## tell the user which action currently will be performed.
1765 sub working_notice
($) {
1768 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1769 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1770 &Header
::openbox
( 'Waiting', 1,);
1774 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1779 &Header
::closebox
();
1780 &Header
::closebigbox
();
1781 &Header
::closepage
();
1785 ## A tiny function to perform a reload of the webpage after one second.
1788 print "<meta http-equiv='refresh' content='1'>\n";
1795 ## Private function to read-in and parse rules of a given rulefile.
1797 ## The given file will be read, parsed and all valid rules will be stored by ID,
1798 ## message/description and it's state in the idsrules hash.
1800 sub readrulesfile
($) {
1801 my $rulefile = shift;
1803 # Open rule file and read in contents
1804 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1806 # Store file content in an array.
1807 my @lines = <RULEFILE
>;
1812 # Loop over rule file contents
1813 foreach my $line (@lines) {
1814 # Remove whitespaces.
1818 next if ($line =~ /^\s*$/);
1824 # Gather rule sid and message from the ruleline.
1825 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1829 # Check if a rule has been found.
1831 # Add rule to the idsrules hash.
1832 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1834 # Grab status of the rule. Check if ruleline starts with a "dash".
1835 if ($line =~ /^\#/) {
1836 # If yes, the rule is disabled.
1837 $idsrules{$rulefile}{$sid}{'State'} = "off";
1839 # Otherwise the rule is enabled.
1840 $idsrules{$rulefile}{$sid}{'State'} = "on";
1848 ## Function to get the used memory of a given process-id.
1850 sub get_memory_usage
($) {
1855 # Try to open the status file for the given process-id on the pseudo
1857 if (open(FILE
, "/proc/$pid/status")) {
1858 # Loop through the entire file.
1860 # Splitt current line content and store them into variables.
1861 my ($key, $value) = split(":", $_, 2);
1863 # Check if the current key is the one which contains the memory usage.
1864 # The wanted one is VmRSS which contains the Real-memory (resident set)
1865 # of the entire process.
1866 if ($key eq "VmRSS") {
1867 # Found the memory usage add it to the memory variable.
1875 # Close file handle.
1878 # Return memory usage.
1882 # If the file could not be open, return nothing.
1887 ## Function to read-in the given enabled or disables sids file.
1889 sub read_enabled_disabled_sids_file
($) {
1892 # Temporary hash to store the sids and their state. It will be
1893 # returned at the end of this function.
1896 # Open the given filename.
1897 open(FILE
, "$file") or die "Could not open $file. $!\n";
1899 # Loop through the file.
1905 next if ($_ =~ /^\s*$/);
1908 next if ($_ =~ /^\#/);
1910 # Splitt line into sid and state part.
1911 my ($state, $sid) = split(" ", $_);
1913 # Skip line if the sid is not numeric.
1914 next unless ($sid =~ /\d+/ );
1916 # Check if the sid was enabled.
1917 if ($state eq "enablesid") {
1918 # Add the sid and its state as enabled to the temporary hash.
1919 $temphash{$sid} = "enabled";
1920 # Check if the sid was disabled.
1921 } elsif ($state eq "disablesid") {
1922 # Add the sid and its state as disabled to the temporary hash.
1923 $temphash{$sid} = "disabled";
1924 # Invalid state - skip the current sid and state.
1938 ## Function to get the provider name from the language file or providers file for a given handle.
1940 sub get_provider_name
($) {
1944 # Get the required translation string for the given provider handle.
1945 my $tr_string = $IDS::Ruleset
::Providers
{$handle}{'tr_string'};
1947 # Check if the translation string is available in the language files.
1948 if ($Lang::tr
{$tr_string}) {
1949 # Use the translated string from the language file.
1950 $provider_name = $Lang::tr
{$tr_string};
1952 # Fallback and use the provider summary from the providers file.
1953 $provider_name = $IDS::Ruleset
::Providers
{$handle}{'summary'};
1956 # Return the obtained provider name.
1957 return $provider_name;
1961 ## Private function to convert a given rulefile to a category name.
1962 ## ( No file extension anymore and if the name contained a dot, it
1963 ## would be replaced by a underline sign.)
1965 sub _rulefile_to_category
($) {
1966 my ($filename) = @_;
1968 # Splitt the filename into single chunks and store them in a
1970 my @parts = split(/\./, $filename);
1972 # Return / Remove last element of the temporary array.
1973 # This removes the file extension.
1976 # Join together the single elements of the temporary array.
1977 # If these are more than one, use a "underline" for joining.
1978 my $category = join '_', @parts;
1980 # Return the converted filename.