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{'PROVIDERS'} eq $Lang::tr
{'ids force ruleset update'}) {
500 # Assign given provider handle.
501 my $provider = $cgiparams{'PROVIDER'};
503 # Check if the red device is active.
504 unless (-e
"${General::swroot}/red/active") {
505 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
508 # Check if enought free disk space is availabe.
509 if(&IDS
::checkdiskspace
()) {
510 $errormessage = "$Lang::tr{'not enough disk space'}";
513 # Check if any errors happend.
514 unless ($errormessage) {
515 # Lock the webpage and print notice about downloading
517 &working_notice
("$Lang::tr{'ids download new ruleset'}");
519 # Call subfunction to download the ruleset.
520 if(&IDS
::downloadruleset
($provider)) {
521 $errormessage = $Lang::tr
{'could not download latest updates'};
523 # Call function to store the errormessage.
524 &IDS
::_store_error_message
($errormessage);
526 # Preform a reload of the page.
529 # Call subfunction to launch oinkmaster.
532 # Check if the IDS is running.
533 if(&IDS
::ids_is_running
()) {
534 # Call suricatactrl to perform a reload.
535 &IDS
::call_suricatactrl
("reload");
538 # Perform a reload of the page.
543 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
546 my $monitored_zones = 0;
548 # Read-in current (old) IDS settings.
549 &General
::readhash
("$IDS::ids_settings_file", \
%oldidssettings);
551 # Prevent form name from been stored in conf file.
552 delete $cgiparams{'IDS'};
554 # Check if the IDS should be enabled.
555 if ($cgiparams{'ENABLE_IDS'} eq "on") {
556 # Get enabled providers.
557 my @enabled_providers = &IDS
::get_enabled_providers
();
559 # Check if any ruleset is available. Otherwise abort and display an error.
560 unless(@enabled_providers) {
561 $errormessage = $Lang::tr
{'ids no enabled ruleset provider'};
564 # Loop through the array of available interfaces.
565 foreach my $zone (@network_zones) {
566 # Convert interface name into upper case.
567 my $zone_upper = uc($zone);
569 # Check if the IDS is enabled for this interaces.
570 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
576 # Check if at least one zone should be monitored, or show an error.
577 unless ($monitored_zones >= 1) {
578 $errormessage = $Lang::tr
{'ids no network zone'};
582 # Go on if there are no error messages.
583 if (!$errormessage) {
584 # Store settings into settings file.
585 &General
::writehash
("$IDS::ids_settings_file", \
%cgiparams);
588 # Check if the the automatic rule update hass been touched.
589 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldidssettings{'AUTOUPDATE_INTERVAL'}) {
590 # Call suricatactrl to set the new interval.
591 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
594 # Generate file to store the home net.
595 &IDS
::generate_home_net_file
();
597 # Generate file to the store the DNS servers.
598 &IDS
::generate_dns_servers_file
();
600 # Generate file to store the HTTP ports.
601 &IDS
::generate_http_ports_file
();
603 # Write the modify sid's file and pass the taken ruleaction.
604 &IDS
::write_modify_sids_file
();
606 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
607 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
608 # Check if a ruleset exists.
609 if (%used_providers) {
610 # Lock the webpage and print message.
611 &working_notice
("$Lang::tr{'ids working'}");
613 # Call oinkmaster to alter the ruleset.
616 # Set reload_page to "True".
621 # Check if the IDS currently is running.
622 if(&IDS
::ids_is_running
()) {
623 # Check if ENABLE_IDS is set to on.
624 if($cgiparams{'ENABLE_IDS'} eq "on") {
625 # Call suricatactrl to perform a reload of suricata.
626 &IDS
::call_suricatactrl
("reload");
628 # Call suricatactrl to stop suricata.
629 &IDS
::call_suricatactrl
("stop");
632 # Call suricatactrl to start suricata.
633 &IDS
::call_suricatactrl
("start");
636 # Check if the page should be reloaded.
638 # Perform a reload of the page.
642 # Toggle Enable/Disable autoupdate for a provider
643 } elsif ($cgiparams{'AUTOUPDATE'} eq $Lang::tr
{'toggle enable disable'}) {
644 my %used_providers = ();
646 # Only go further, if an ID has been passed.
647 if ($cgiparams{'ID'}) {
648 # Assign the given ID.
649 my $id = $cgiparams{'ID'};
651 # Undef the given ID.
652 undef($cgiparams{'ID'});
654 # Read-in providers settings file.
655 &General
::readhasharray
($IDS::providers_settings_file
, \
%used_providers);
657 # Grab the configured status of the corresponding entry.
658 my $status_autoupdate = $used_providers{$id}[2];
661 if ($status_autoupdate eq "disabled") {
662 $status_autoupdate = "enabled";
664 $status_autoupdate = "disabled";
667 # Modify the status of the existing entry.
668 $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$status_autoupdate", "$used_providers{$id}[3]"];
670 # Write the changed hash to the providers settings file.
671 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
674 # Add/Edit a provider to the list of used providers.
676 } elsif (($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'update'}")) {
677 my %used_providers = ();
679 # Read-in providers settings file.
680 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
682 # Assign some nice human-readable values.
683 my $provider = $cgiparams{'PROVIDER'};
684 my $subscription_code = $cgiparams{'SUBSCRIPTION_CODE'};
685 my $status_autoupdate;
687 # Handle autoupdate checkbox.
688 if ($cgiparams{'ENABLE_AUTOUPDATE'} eq "on") {
689 $status_autoupdate = "enabled";
691 $status_autoupdate = "disabled";
694 # Check if we are going to add a new provider.
695 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") {
696 # Loop through the hash of used providers.
697 foreach my $id ( keys %used_providers) {
698 # Check if the choosen provider is already in use.
699 if ($used_providers{$id}[0] eq "$provider") {
700 # Assign error message.
701 $errormessage = "$Lang::tr{'ids the choosen provider is already in use'}";
706 # Check if the provider requires a subscription code.
707 if ($IDS::Ruleset
::Providers
{$provider}{'requires_subscription'} eq "True") {
708 # Check if an subscription code has been provided.
709 if ($subscription_code) {
710 # Check if the code contains unallowed chars.
711 unless ($subscription_code =~ /^[a-z0-9]+$/) {
712 $errormessage = $Lang::tr
{'invalid input for subscription code'};
715 # Print an error message, that an subsription code is required for this
717 $errormessage = $Lang::tr
{'ids subscription code required'};
721 # Go further if there was no error.
722 if ($errormessage eq '') {
726 # Check if we should edit an existing entry and got an ID.
727 if (($cgiparams{'PROVIDERS'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
728 # Assin the provided id.
729 $id = $cgiparams{'ID'};
731 # Undef the given ID.
732 undef($cgiparams{'ID'});
734 # Grab the configured status of the corresponding entry.
735 $status = $used_providers{$id}[3];
737 # Each newly added entry automatically should be enabled.
740 # Generate the ID for the new entry.
742 # Sort the keys by their ID and store them in an array.
743 my @keys = sort { $a <=> $b } keys %used_providers;
745 # Reverse the key array.
746 my @reversed = reverse(@keys);
748 # Obtain the last used id.
749 my $last_id = @reversed[0];
751 # Increase the last id by one and use it as id for the new entry.
755 # Add/Modify the entry to/in the used providers hash..
756 $used_providers{$id} = ["$provider", "$subscription_code", "$status_autoupdate", "$status"];
758 # Write the changed hash to the providers settings file.
759 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
761 # Check if a new provider will be added.
762 if ($cgiparams{'PROVIDERS'} eq $Lang::tr
{'add'}) {
763 # Lock the webpage and print notice about downloading
765 &working_notice
("$Lang::tr{'ids working'}");
767 # Download the ruleset.
768 &IDS
::downloadruleset
($provider);
770 # Extract the ruleset
771 &IDS
::extractruleset
($provider);
774 &IDS
::move_tmp_ruleset
();
776 # Cleanup temporary directory.
777 &IDS
::cleanup_tmp_directory
();
779 # Create new empty file for used rulefiles
781 &IDS
::write_used_provider_rulefiles_file
($provider);
783 # Perform a reload of the page.
787 # Undefine providers flag.
788 undef($cgiparams{'PROVIDERS'});
791 ## Toggle Enabled/Disabled for an existing provider.
793 } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr
{'toggle enable disable'}) {
794 my %used_providers = ();
795 my $provider_includes_action;
797 # Only go further, if an ID has been passed.
798 if ($cgiparams{'ID'}) {
799 # Assign the given ID.
800 my $id = $cgiparams{'ID'};
802 # Undef the given ID.
803 undef($cgiparams{'ID'});
805 # Read-in file which contains the provider settings.
806 &General
::readhasharray
($IDS::providers_settings_file
, \
%used_providers);
808 # Grab the configured status of the corresponding entry.
809 my $status = $used_providers{$id}[3];
811 # Grab the provider handle.
812 my $provider_handle = $used_providers{$id}[0];
815 if ($status eq "enabled") {
816 $status = "disabled";
818 # Set the provider includes action to "remove" for removing the entry.
819 $provider_includes_action = "remove";
823 # Set the provider includes action to "add".
824 $provider_includes_action = "add";
827 # Modify the status of the existing entry.
828 $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$used_providers{$id}[2]", "$status"];
830 # Write the changed hash to the providers settings file.
831 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
833 # Get all enabled providers.
834 my @enabled_providers = &IDS
::get_enabled_providers
();
836 # Write the main providers include file.
837 &IDS
::write_main_used_rulefiles_file
(@enabled_providers);
839 # Call function to alter the oinkmasters provider includes file and
840 # add or remove the provider.
841 &IDS
::alter_oinkmaster_provider_includes_file
($provider_includes_action, $provider_handle);
843 # Check if the IDS is running.
844 if(&IDS
::ids_is_running
()) {
845 # Gather the amount of enabled providers (elements in the array).
846 my $amount = @enabled_providers;
848 # Check if there are still enabled ruleset providers.
850 # Call suricatactrl to perform a restart.
851 &IDS
::call_suricatactrl
("restart");
853 # No active ruleset provider, suricata has to be stopped.
856 &IDS
::call_suricatactrl
("stop");
860 # Undefine providers flag.
861 undef($cgiparams{'PROVIDERS'});
864 ## Remove provider from the list of used providers.
866 } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr
{'remove'}) {
867 my %used_providers = ();
869 # Read-in provider settings file.
870 &General
::readhasharray
($IDS::providers_settings_file
, \
%used_providers);
872 # Grab the provider name bevore deleting it from hash.
873 my $provider = $used_providers{$cgiparams{'ID'}}[0];
875 # Drop entry from the hash.
876 delete($used_providers{$cgiparams{'ID'}});
878 # Undef the given ID.
879 undef($cgiparams{'ID'});
881 # Write the changed hash to the provide settings file.
882 &General
::writehasharray
($IDS::providers_settings_file
, \
%used_providers);
884 # Lock the webpage and print message.
885 &working_notice
("$Lang::tr{'ids apply ruleset changes'}");
887 # Drop the stored ruleset file.
888 &IDS
::drop_dl_rulesfile
($provider);
890 # Get the name of the provider rulessets include file.
891 my $provider_used_rulefile = &IDS
::get_used_provider_rulesfile_file
($provider);
893 # Drop the file, it is not longer needed.
894 unlink("$provider_used_rulefile");
896 # Call function to get the path and name for the given providers
897 # oinkmaster modified sids file.
898 my $provider_modified_sids_file = &IDS
::get_oinkmaster_provider_modified_sids_file
($provider);
900 # Check if the file exists.
901 if (-f
$provider_modified_sids_file) {
902 # Remove the file, which is not longer needed.
903 unlink("$provider_modified_sids_file");
906 # Alter the oinkmaster provider includes file and remove the provider.
907 &IDS
::alter_oinkmaster_provider_includes_file
("remove", $provider);
909 # Regenerate ruleset.
912 # Gather all enabled providers.
913 my @enabled_providers = &IDS
::get_enabled_providers
();
915 # Regenerate main providers include file.
916 &IDS
::write_main_used_rulefiles_file
(@enabled_providers);
918 # Check if the IDS is running.
919 if(&IDS
::ids_is_running
()) {
920 # Get amount of enabled providers.
921 my $amount = @enabled_providers;
923 # Check if at least one enabled provider remains.
925 # Call suricatactrl to perform a reload.
926 &IDS
::call_suricatactrl
("restart");
928 # Stop suricata if no enabled provider remains.
930 # Call suricatactrel to perform the stop.
931 &IDS
::call_suricatactrl
("stop");
935 # Undefine providers flag.
936 undef($cgiparams{'PROVIDERS'});
942 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
944 &Header
::openbigbox
('100%', 'left', '', $errormessage);
946 &show_display_error_message
();
948 if ($cgiparams{'RULESET'} eq "$Lang::tr{'ids customize ruleset'}" ) {
949 &show_customize_ruleset
();
950 } elsif ($cgiparams{'PROVIDERS'} ne "") {
951 &show_add_provider
();
956 &Header
::closebigbox
();
957 &Header
::closepage
();
960 ## Tiny function to show if a error message happened.
962 sub show_display_error_message
() {
964 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
965 print "<class name='base'>$errormessage\n";
966 print " </class>\n";
972 ## Function to display the main IDS page.
974 sub show_mainpage
() {
975 # Read-in idssettings and provider settings.
976 &General
::readhash
("$IDS::ids_settings_file", \
%idssettings);
977 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
979 # If no autoupdate intervall has been configured yet, set default value.
980 unless(exists($idssettings{'AUTOUPDATE_INTERVAL'})) {
981 # Set default to "weekly".
982 $idssettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
985 # Read-in ignored hosts.
986 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
988 $checked{'ENABLE_IDS'}{'off'} = '';
989 $checked{'ENABLE_IDS'}{'on'} = '';
990 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
991 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
992 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
993 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
994 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
995 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
996 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
997 $selected{'AUTOUPDATE_INTERVAL'}{$idssettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
999 # Draw current state of the IDS
1000 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
1002 # Check if the IDS is running and obtain the process-id.
1003 my $pid = &IDS
::ids_is_running
();
1005 # Display some useful information, if suricata daemon is running.
1007 # Gather used memory.
1008 my $memory = &get_memory_usage
($pid);
1011 <table width='95%' cellspacing='0' class='tbl'>
1013 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
1017 <td class='base'>$Lang::tr{'guardian daemon'}</td>
1018 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
1022 <td class='base'></td>
1023 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
1024 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
1028 <td class='base'></td>
1029 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
1030 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
1035 # Otherwise display a hint that the service is not launched.
1037 <table width='95%' cellspacing='0' class='tbl'>
1039 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
1043 <td class='base'>$Lang::tr{'guardian daemon'}</td>
1044 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
1050 # Only show this area, if at least one ruleset provider is configured.
1051 if (%used_providers) {
1055 <br><br><h2>$Lang::tr{'settings'}</h2>
1057 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1058 <table width='100%' border='0'>
1060 <td class='base' colspan='2'>
1061 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}> $Lang::tr{'ids enable'}
1064 <td class='base' colspan='2'>
1065 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}> $Lang::tr{'ids monitor traffic only'}
1077 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
1084 # Loop through the array of available networks and print config options.
1085 foreach my $zone (@network_zones) {
1087 my $checked_forward;
1089 # Convert current zone name to upper case.
1090 my $zone_upper = uc($zone);
1093 my $zone_name = $zone;
1095 # Dirty hack to get the correct language string for the red zone.
1096 if ($zone eq "red") {
1097 $zone_name = "red1";
1100 # Grab checkbox status from settings hash.
1101 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
1102 $checked_input = "checked = 'checked'";
1105 print "<td class='base' width='20%'>\n";
1106 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
1107 print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
1122 <td colspan='4'><b>$Lang::tr{'ids automatic rules update'}</b></td>
1127 <select name='AUTOUPDATE_INTERVAL'>
1128 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
1129 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
1130 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
1138 <table width='100%'>
1140 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
1149 &Header
::closebox
();
1152 # Used Ruleset Providers section.
1154 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
1157 <table width='100%' border='0'>
1159 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ids provider'}</b></td>
1160 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'date'}</b></td>
1161 <td class='base' bgcolor='$color{'color20'}' align='center'><b>$Lang::tr{'ids autoupdates'}</b></td>
1162 <td class='base' bgcolor='$color{'color20'}'></td>
1163 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1168 # Check if some providers has been configured.
1169 if (keys (%used_providers)) {
1172 # Loop through all entries of the hash.
1173 foreach my $id (sort keys(%used_providers)) {
1174 # Assign data array positions to some nice variable names.
1175 my $provider = $used_providers{$id}[0];
1176 my $provider_name = &get_provider_name
($provider);
1177 my $rulesetdate = &IDS
::get_ruleset_date
($provider);
1179 my $subscription_code = $used_providers{$id}[1];
1180 my $autoupdate_status = $used_providers{$id}[2];
1181 my $status = $used_providers{$id}[3];
1183 # Check if the item number is even or not.
1185 $col="bgcolor='$color{'color22'}'";
1187 $col="bgcolor='$color{'color20'}'";
1190 # Choose icons for the checkboxes.
1193 my $autoupdate_status_gif;
1194 my $autoupdate_status_gdesc;
1196 # Check if the status is enabled and select the correct image and description.
1197 if ($status eq 'enabled' ) {
1198 $status_gif = 'on.gif';
1199 $status_gdesc = $Lang::tr
{'click to disable'};
1201 $status_gif = 'off.gif';
1202 $status_gdesc = $Lang::tr
{'click to enable'};
1205 # Check if the autoupdate status is enabled and select the correct image and description.
1206 if ($autoupdate_status eq 'enabled') {
1207 $autoupdate_status_gif = 'on.gif';
1208 $autoupdate_status_gdesc = $Lang::tr
{'click to disable'};
1210 $autoupdate_status_gif = 'off.gif';
1211 $autoupdate_status_gdesc = $Lang::tr
{'click to enable'};
1216 <td width='33%' class='base' $col>$provider_name</td>
1217 <td width='30%' class='base' $col>$rulesetdate</td>
1219 <td align='center' $col>
1220 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1221 <input type='hidden' name='AUTOUPDATE' value='$Lang::tr{'toggle enable disable'}' />
1222 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$autoupdate_status_gif' alt='$autoupdate_status_gdesc' title='$autoupdate_status_gdesc' />
1223 <input type='hidden' name='ID' value='$id' />
1227 <td align='center' $col>
1228 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1229 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'toggle enable disable'}'>
1230 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$status_gif' alt='$status_gdesc' title='$status_gdesc'>
1231 <input type='hidden' name='ID' value='$id'>
1235 <td align='center' $col>
1236 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1237 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'edit'}'>
1238 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1239 <input type='hidden' name='ID' value='$id'>
1243 <td align='center' $col>
1244 <form method='post' name='$provider' action='$ENV{'SCRIPT_NAME'}'>
1245 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1246 <input type='hidden' name='ID' value='$id'>
1247 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'remove'}'>
1252 # Increment lines value.
1258 # Print notice that currently no hosts are ignored.
1260 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1266 # Section to add new elements or edit existing ones.
1273 <table width='100%'>
1274 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1278 # Only show this button if a ruleset provider is configured.
1279 if (%used_providers) {
1280 print "<input type='submit' name='RULESET' value='$Lang::tr{'ids customize ruleset'}'>\n";
1283 <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids add provider'}'>
1290 &Header
::closebox
();
1293 # Whitelist / Ignorelist
1295 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ignored hosts'});
1298 <table width='100%'>
1300 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
1301 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
1302 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1305 # Check if some hosts have been added to be ignored.
1306 if (keys (%ignored)) {
1309 # Loop through all entries of the hash.
1310 while( (my $key) = each %ignored) {
1311 # Assign data array positions to some nice variable names.
1312 my $address = $ignored{$key}[0];
1313 my $remark = $ignored{$key}[1];
1314 my $status = $ignored{$key}[2];
1316 # Check if the key (id) number is even or not.
1317 if ($cgiparams{'ID'} eq $key) {
1318 $col="bgcolor='${Header::colouryellow}'";
1319 } elsif ($key % 2) {
1320 $col="bgcolor='$color{'color22'}'";
1322 $col="bgcolor='$color{'color20'}'";
1325 # Choose icon for the checkbox.
1329 # Check if the status is enabled and select the correct image and description.
1330 if ($status eq 'enabled' ) {
1332 $gdesc = $Lang::tr
{'click to disable'};
1335 $gdesc = $Lang::tr
{'click to enable'};
1340 <td width='20%' class='base' $col>$address</td>
1341 <td width='65%' class='base' $col>$remark</td>
1343 <td align='center' $col>
1344 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1345 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}'>
1346 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc'>
1347 <input type='hidden' name='ID' value='$key'>
1351 <td align='center' $col>
1352 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1353 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}'>
1354 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1355 <input type='hidden' name='ID' value='$key'>
1359 <td align='center' $col>
1360 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
1361 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1362 <input type='hidden' name='ID' value='$key'>
1363 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1370 # Print notice that currently no hosts are ignored.
1372 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1378 # Section to add new elements or edit existing ones.
1384 <div align='center'>
1385 <table width='100%'>
1388 # Assign correct headline and button text.
1393 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1394 if ($cgiparams{'ID'} ne '') {
1395 $buttontext = $Lang::tr
{'update'};
1396 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1398 # Grab address and remark for the given key.
1399 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1400 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1402 $buttontext = $Lang::tr
{'add'};
1403 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1407 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1408 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1410 <td width='30%'>$Lang::tr{'ip address'}: </td>
1411 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1413 <td width='30%'>$Lang::tr{'remark'}: </td>
1414 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1415 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1422 &Header
::closebox
();
1426 ## Function to show the customize ruleset section.
1428 sub show_customize_ruleset
() {
1432 # Java script variable declaration for show and hide.
1433 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
1434 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
1437 // Tiny java script function to show/hide the rules
1438 // of a given category.
1439 function showhide(tblname) {
1440 \$("#" + tblname).toggle();
1442 // Get current content of the span element.
1443 var content = document.getElementById("span_" + tblname);
1445 if (content.innerHTML === show) {
1446 content.innerHTML = hide;
1448 content.innerHTML = show;
1454 &Header
::openbox
('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'}" );
1455 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1457 # Output display table for rule files
1458 print "<table width='100%'>\n";
1460 # Loop over each rule file
1461 foreach my $rulefile (sort keys(%idsrules)) {
1462 my $rulechecked = '';
1464 # Check if rule file is enabled
1465 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1466 $rulechecked = 'CHECKED';
1469 # Convert rulefile name into category name.
1470 my $categoryname = &_rulefile_to_category
($rulefile);
1472 # Table and rows for the rule files.
1474 print"<td class='base' width='5%'>\n";
1475 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1477 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1478 print"<td class='base' width='5%' align='right'>\n";
1479 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1483 # Rows which will be hidden per default and will contain the single rules.
1484 print"<tr style='display:none' id='$categoryname'>\n";
1485 print"<td colspan='3'>\n";
1492 # New table for the single rules.
1493 print "<table width='100%'>\n";
1495 # Loop over rule file rules
1496 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1498 my $ruledefchecked = '';
1500 # Skip rulefile itself.
1501 next if ($sid eq "Rulefile");
1503 # If 2 rules have been displayed, start a new row
1504 if (($lines % 2) == 0) {
1505 print "</tr><tr>\n";
1507 # Increase rows by once.
1513 $col="bgcolor='$color{'color20'}'";
1515 $col="bgcolor='$color{'color22'}'";
1519 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1520 $ruledefchecked = 'CHECKED';
1523 # Create rule checkbox and display rule description
1524 print "<td class='base' width='5%' align='right' $col>\n";
1525 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1527 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1529 # Increment rule count
1533 # If do not have a second rule for row, create empty cell
1534 if (($lines % 2) != 0) {
1535 print "<td class='base'></td>";
1538 # Close display table
1539 print "</tr></table></td></tr>";
1542 # Close display table
1546 <table width='100%'>
1548 <td width='100%' align='right'>
1549 <input type='submit' value='$Lang::tr{'fwhost back'}'>
1550 <input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'>
1557 &Header
::closebox
();
1562 ## Function to show section for add/edit a provider.
1564 sub show_add_provider
() {
1565 my %used_providers = ();
1566 my @subscription_providers;
1568 # Read -in providers settings file.
1569 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
1571 # Get all supported ruleset providers.
1572 my @ruleset_providers = &IDS
::get_ruleset_providers
();
1577 # Generate Java Script Object which contains the URL of the providers.
1578 print "\t// Object, which contains the webpages of the ruleset providers.\n";
1579 print "\tvar url = {\n";
1581 # Loop through the array of supported providers.
1582 foreach my $provider (@ruleset_providers) {
1583 # Check if the provider requires a subscription.
1584 if ($IDS::Ruleset
::Providers
{$provider}{'requires_subscription'} eq "True") {
1585 # Add the provider to the array of subscription_providers.
1586 push(@subscription_providers, $provider);
1589 # Grab the URL for the provider.
1590 my $url = $IDS::Ruleset
::Providers
{$provider}{'website'};
1592 # Print the URL to the Java Script Object.
1593 print "\t\t$provider: \"$url\"\,\n";
1596 # Close the Java Script Object declaration.
1599 # Generate Java Script Array which contains the provider that requires a subscription.
1601 $line = join("', '", @subscription_providers);
1603 print "\t// Array which contains the providers that requires a subscription.\n";
1604 print "\tsubscription_provider = ['$line']\;\n\n";
1607 // Java Script function to swap the text input field for
1608 // entering a subscription code.
1609 var update_provider = function() {
1610 if(inArray(\$('#PROVIDER').val(), subscription_provider)) {
1611 \$('.subscription_code').show();
1613 \$('.subscription_code').hide();
1616 // Call function to change the website url.
1617 change_url(\$('#PROVIDER').val());
1620 // Java Script function to check if a given value is part of
1622 function inArray(value,array) {
1623 var count=array.length;
1625 for(var i=0;i<count;i++) {
1626 if(array[i]===value){
1634 // Tiny function to change the website url based on the selected element in the "PROVIDERS"
1636 function change_url(provider) {
1637 // Get and change the href to the corresponding url.
1638 document.getElementById("website").href = url[provider];
1641 // JQuery function to call corresponding function when
1642 // the ruleset provider is changed or the page is loaded for showing/hiding
1643 // the subscription_code area.
1644 \$(document).ready(function() {
1645 \$('#PROVIDER').change(update_provider);
1653 # Check if an existing provider should be edited.
1654 if($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1655 # Check if autoupdate is enabled for this provider.
1656 if ($used_providers{$cgiparams{'ID'}}[2] eq "enabled") {
1657 # Set the checkbox to be checked.
1658 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1661 # Display section to force an rules update and to reset the provider.
1662 &show_additional_provider_actions
();
1664 } elsif ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'ids add provider'}") {
1665 # Set the autoupdate to true as default.
1666 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1669 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids provider settings'});
1672 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1673 <table width='100%' border='0'>
1675 <td colspan='2'><b>$Lang::tr{'ids provider'}</b></td>
1680 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1683 # Value to allow disabling the dropdown menu.
1686 # Check if we are in edit mode.
1687 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1688 $disabled = "disabled";
1690 # Add hidden input with the provider because the disable select does not provider
1692 print "<input type='hidden' name='PROVIDER' value='$used_providers{$cgiparams{'ID'}}[0]'>\n";
1695 print "<select name='PROVIDER' id='PROVIDER' $disabled>\n";
1696 # Loop through the array of ruleset providers.
1697 foreach my $provider (@ruleset_providers) {
1698 # Get the provider name.
1699 my $provider_name = &get_provider_name
($provider);
1701 # Pre-select the provider if one is given.
1702 if (($used_providers{$cgiparams{'ID'}}[0] eq "$provider") || ($cgiparams{'PROVIDER'} eq "$provider")) {
1703 $selected{$provider} = "selected='selected'";
1706 # Add the provider to the dropdown menu.
1707 print "<option value='$provider' $selected{$provider}>$provider_name</option>\n";
1714 <b><a id="website" target="_blank" href="#">$Lang::tr{'ids visit provider website'}</a></b>
1719 <td colspan='2'><br><br></td>
1722 <tr class='subscription_code' style='display:none' id='subscription_code'>
1727 <b>$Lang::tr{'subscription code'}</b>
1733 <input type='text' size='40' name='SUBSCRIPTION_CODE' value='$used_providers{$cgiparams{'ID'}}[1]'>
1746 <input type='checkbox' name='ENABLE_AUTOUPDATE' $checked{'ENABLE_AUTOUPDATE'}> $Lang::tr{'ids enable automatic updates'}
1751 <td colspan='2' align='right'>
1752 <input type='submit' value='$Lang::tr{'back'}'>
1755 # Check if a provider should be added or edited.
1756 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1757 # Display button for updating the existing provider.
1758 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'update'}'>\n";
1760 # Display button to add the new provider.
1761 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'add'}'>\n";
1770 &Header
::closebox
();
1774 ## Function to show the area where additional provider actions can be done.
1776 sub show_additional_provider_actions
() {
1778 my %used_providers = ();
1780 # Read-in providers settings file.
1781 &General
::readhasharray
("$IDS::providers_settings_file", \
%used_providers);
1783 # Assign variable for provider handle.
1784 my $provider = "$used_providers{$cgiparams{'ID'}}[0]";
1786 # Call function to get the path and name for the given providers
1787 # oinkmaster modified sids file.
1788 my $provider_modified_sids_file = &IDS
::get_oinkmaster_provider_modified_sids_file
($provider);
1790 # Disable the reset provider button if no provider modified sids file exists.
1791 unless (-f
$provider_modified_sids_file) {
1792 $disabled = "disabled";
1795 &Header
::openbox
('100%', 'center', "");
1797 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1798 <table width='100%' border="0">
1801 <input type='hidden' name='PROVIDER' value='$provider'>
1802 <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids reset provider'}' $disabled>
1803 <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids force ruleset update'}'>
1810 &Header
::closebox
();
1814 ## A function to display a notice, to lock the webpage and
1815 ## tell the user which action currently will be performed.
1817 sub working_notice
($) {
1820 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1821 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1822 &Header
::openbox
( 'Waiting', 1,);
1826 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1831 &Header
::closebox
();
1832 &Header
::closebigbox
();
1833 &Header
::closepage
();
1837 ## A tiny function to perform a reload of the webpage after one second.
1840 print "<meta http-equiv='refresh' content='1'>\n";
1847 ## Private function to read-in and parse rules of a given rulefile.
1849 ## The given file will be read, parsed and all valid rules will be stored by ID,
1850 ## message/description and it's state in the idsrules hash.
1852 sub readrulesfile
($) {
1853 my $rulefile = shift;
1855 # Open rule file and read in contents
1856 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1858 # Store file content in an array.
1859 my @lines = <RULEFILE
>;
1864 # Loop over rule file contents
1865 foreach my $line (@lines) {
1866 # Remove whitespaces.
1870 next if ($line =~ /^\s*$/);
1876 # Gather rule sid and message from the ruleline.
1877 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1881 # Check if a rule has been found.
1883 # Add rule to the idsrules hash.
1884 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1886 # Grab status of the rule. Check if ruleline starts with a "dash".
1887 if ($line =~ /^\#/) {
1888 # If yes, the rule is disabled.
1889 $idsrules{$rulefile}{$sid}{'State'} = "off";
1891 # Otherwise the rule is enabled.
1892 $idsrules{$rulefile}{$sid}{'State'} = "on";
1900 ## Function to get the used memory of a given process-id.
1902 sub get_memory_usage
($) {
1907 # Try to open the status file for the given process-id on the pseudo
1909 if (open(FILE
, "/proc/$pid/status")) {
1910 # Loop through the entire file.
1912 # Splitt current line content and store them into variables.
1913 my ($key, $value) = split(":", $_, 2);
1915 # Check if the current key is the one which contains the memory usage.
1916 # The wanted one is VmRSS which contains the Real-memory (resident set)
1917 # of the entire process.
1918 if ($key eq "VmRSS") {
1919 # Found the memory usage add it to the memory variable.
1927 # Close file handle.
1930 # Return memory usage.
1934 # If the file could not be open, return nothing.
1939 ## Function to read-in the given enabled or disables sids file.
1941 sub read_enabled_disabled_sids_file
($) {
1944 # Temporary hash to store the sids and their state. It will be
1945 # returned at the end of this function.
1948 # Open the given filename.
1949 open(FILE
, "$file") or die "Could not open $file. $!\n";
1951 # Loop through the file.
1957 next if ($_ =~ /^\s*$/);
1960 next if ($_ =~ /^\#/);
1962 # Splitt line into sid and state part.
1963 my ($state, $sid) = split(" ", $_);
1965 # Skip line if the sid is not numeric.
1966 next unless ($sid =~ /\d+/ );
1968 # Check if the sid was enabled.
1969 if ($state eq "enablesid") {
1970 # Add the sid and its state as enabled to the temporary hash.
1971 $temphash{$sid} = "enabled";
1972 # Check if the sid was disabled.
1973 } elsif ($state eq "disablesid") {
1974 # Add the sid and its state as disabled to the temporary hash.
1975 $temphash{$sid} = "disabled";
1976 # Invalid state - skip the current sid and state.
1990 ## Function to get the provider name from the language file or providers file for a given handle.
1992 sub get_provider_name
($) {
1996 # Get the required translation string for the given provider handle.
1997 my $tr_string = $IDS::Ruleset
::Providers
{$handle}{'tr_string'};
1999 # Check if the translation string is available in the language files.
2000 if ($Lang::tr
{$tr_string}) {
2001 # Use the translated string from the language file.
2002 $provider_name = $Lang::tr
{$tr_string};
2004 # Fallback and use the provider summary from the providers file.
2005 $provider_name = $IDS::Ruleset
::Providers
{$handle}{'summary'};
2008 # Return the obtained provider name.
2009 return $provider_name;
2013 ## Private function to convert a given rulefile to a category name.
2014 ## ( No file extension anymore and if the name contained a dot, it
2015 ## would be replaced by a underline sign.)
2017 sub _rulefile_to_category
($) {
2018 my ($filename) = @_;
2020 # Splitt the filename into single chunks and store them in a
2022 my @parts = split(/\./, $filename);
2024 # Return / Remove last element of the temporary array.
2025 # This removes the file extension.
2028 # Join together the single elements of the temporary array.
2029 # If these are more than one, use a "underline" for joining.
2030 my $category = join '_', @parts;
2032 # Return the converted filename.