]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids-functions.pl: Bring back usage of whitelist.rules and local.rules
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / ids.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2020 IPFire Team <info@ipfire.org> #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
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";
33
34 # Import ruleset providers file.
35 require "$IDS::rulesetsourcesfile";
36
37 my %color = ();
38 my %mainsettings = ();
39 my %idsrules = ();
40 my %idssettings=();
41 my %used_providers=();
42 my %cgiparams=();
43 my %checked=();
44 my %selected=();
45 my %ignored=();
46
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);
50
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();
54
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");
58 }
59
60 my $errormessage;
61
62 # Create files if they does not exist yet.
63 &IDS::check_and_create_filelayout();
64
65 # Hash which contains the colour code of a network zone.
66 my %colourhash = (
67 'red' => $Header::colourred,
68 'green' => $Header::colourgreen,
69 'blue' => $Header::colourblue,
70 'orange' => $Header::colourorange,
71 'ovpn' => $Header::colourovpn
72 );
73
74 &Header::showhttpheaders();
75
76 #Get GUI values
77 &Header::getcgihash(\%cgiparams);
78
79 ## Add/edit an entry to the ignore file.
80 #
81 if (($cgiparams{'WHITELIST'} eq $Lang::tr{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr{'update'})) {
82
83 # Check if any input has been performed.
84 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
85
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'}";
89 }
90 } else {
91 $errormessage = "$Lang::tr{'guardian empty input'}";
92 }
93
94 # Go further if there was no error.
95 if ($errormessage eq '') {
96 my %ignored = ();
97 my $id;
98 my $status;
99
100 # Assign hash values.
101 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
102 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
103
104 # Read-in ignoredfile.
105 &General::readhasharray($IDS::ignored_file, \%ignored);
106
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'};
111
112 # Undef the given ID.
113 undef($cgiparams{'ID'});
114
115 # Grab the configured status of the corresponding entry.
116 $status = $ignored{$id}[2];
117 } else {
118 # Each newly added entry automatically should be enabled.
119 $status = "enabled";
120
121 # Generate the ID for the new entry.
122 #
123 # Sort the keys by their ID and store them in an array.
124 my @keys = sort { $a <=> $b } keys %ignored;
125
126 # Reverse the key array.
127 my @reversed = reverse(@keys);
128
129 # Obtain the last used id.
130 my $last_id = @reversed[0];
131
132 # Increase the last id by one and use it as id for the new entry.
133 $id = ++$last_id;
134 }
135
136 # Add/Modify the entry to/in the ignored hash.
137 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
138
139 # Write the changed ignored hash to the ignored file.
140 &General::writehasharray($IDS::ignored_file, \%ignored);
141
142 # Regenerate the ignore file.
143 &IDS::generate_ignore_file();
144 }
145
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");
150 }
151
152 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
153 #
154
155 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'toggle enable disable'}) {
156 my %ignored = ();
157
158 # Only go further, if an ID has been passed.
159 if ($cgiparams{'ID'}) {
160 # Assign the given ID.
161 my $id = $cgiparams{'ID'};
162
163 # Undef the given ID.
164 undef($cgiparams{'ID'});
165
166 # Read-in ignoredfile.
167 &General::readhasharray($IDS::ignored_file, \%ignored);
168
169 # Grab the configured status of the corresponding entry.
170 my $status = $ignored{$id}[2];
171
172 # Switch the status.
173 if ($status eq "disabled") {
174 $status = "enabled";
175 } else {
176 $status = "disabled";
177 }
178
179 # Modify the status of the existing entry.
180 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
181
182 # Write the changed ignored hash to the ignored file.
183 &General::writehasharray($IDS::ignored_file, \%ignored);
184
185 # Regenerate the ignore file.
186 &IDS::generate_ignore_file();
187
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");
192 }
193 }
194
195 ## Remove entry from ignore list.
196 #
197 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'remove'}) {
198 my %ignored = ();
199
200 # Read-in ignoredfile.
201 &General::readhasharray($IDS::ignored_file, \%ignored);
202
203 # Drop entry from the hash.
204 delete($ignored{$cgiparams{'ID'}});
205
206 # Undef the given ID.
207 undef($cgiparams{'ID'});
208
209 # Write the changed ignored hash to the ignored file.
210 &General::writehasharray($IDS::ignored_file, \%ignored);
211
212 # Regenerate the ignore file.
213 &IDS::generate_ignore_file();
214
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");
219 }
220 }
221
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
225 # is in progess.
226 &working_notice("$Lang::tr{'ids ruleset autoupdate in progress'}");
227
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.
231 sleep 1;
232 }
233
234 # Page has been unlocked, perform a reload.
235 &reload();
236 }
237
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";
242
243 # Read the stored error message.
244 $errormessage = <FILE>;
245
246 # Close file.
247 close (FILE);
248
249 # Delete the file, which is now not longer required.
250 unlink($IDS::storederrorfile);
251 }
252
253 # Gather ruleset details.
254 if ($cgiparams{'RULESET'}) {
255 ## Grab all available rules and store them in the idsrules hash.
256 #
257
258 # Get enabled providers.
259 my @enabled_providers = &IDS::get_enabled_providers();
260
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)) {
265
266 # We only want files.
267 next unless (-f "$IDS::rulespath/$file");
268
269 # Ignore empty files.
270 next if (-z "$IDS::rulespath/$file");
271
272 # Use a regular expression to find files ending in .rules
273 next unless ($file =~ m/\.rules$/);
274
275 # Ignore files which are not read-able.
276 next unless (-R "$IDS::rulespath/$file");
277
278 # Skip whitelist rules file.
279 next if( $file eq "whitelist.rules");
280
281 # Splitt vendor from filename.
282 my @filename_parts = split(/-/, $file);
283
284 # Assign vendor name for easy processing.
285 my $vendor = @filename_parts[0];
286
287 # Skip rulefile if the provider is disabled.
288 next unless ($vendor ~~ @enabled_providers);
289
290 # Call subfunction to read-in rulefile and add rules to
291 # the idsrules hash.
292 &readrulesfile("$file");
293 }
294
295 closedir(DIR);
296
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);
301
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
306 # no rules.
307 if($idsrules{$rulefile}) {
308 # Add the rulefile state to the %idsrules hash.
309 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
310 }
311 }
312 }
313 }
314
315 # Save ruleset.
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;
319
320 # Hash to store the user-enabled and disabled sids.
321 my %enabled_disabled_sids;
322
323 # Store if a restart of suricata is required.
324 my $suricata_restart_required;
325
326 # Loop through the hash of idsrules.
327 foreach my $rulefile(keys %idsrules) {
328 # Check if the state of the rulefile has been changed.
329 unless ($cgiparams{$rulefile} eq $idsrules{$rulefile}{'Rulefile'}{'State'}) {
330 # A restart of suricata is required to apply the changes of the used rulefiles.
331 $suricata_restart_required = 1;
332 }
333
334 # Check if the rulefile is enabled.
335 if ($cgiparams{$rulefile} eq "on") {
336 # Add rulefile to the array of enabled rulefiles.
337 push(@enabled_rulefiles, $rulefile);
338
339 # Drop item from cgiparams hash.
340 delete $cgiparams{$rulefile};
341 }
342 }
343
344 # Read-in the files for enabled/disabled sids.
345 # This will be done by calling the read_enabled_disabled_sids_file function two times
346 # and merge the returned hashes together into the enabled_disabled_sids hash.
347 %enabled_disabled_sids = (
348 &read_enabled_disabled_sids_file($IDS::disabled_sids_file),
349 &read_enabled_disabled_sids_file($IDS::enabled_sids_file));
350
351 # Loop through the hash of idsrules.
352 foreach my $rulefile (keys %idsrules) {
353 # Loop through the single rules of the rulefile.
354 foreach my $sid (keys %{$idsrules{$rulefile}}) {
355 # Skip the current sid if it is not numeric.
356 next unless ($sid =~ /\d+/ );
357
358 # Check if there exists a key in the cgiparams hash for this sid.
359 if (exists($cgiparams{$sid})) {
360 # Look if the rule is disabled.
361 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
362 # Check if the state has been set to 'on'.
363 if ($cgiparams{$sid} eq "on") {
364 # Add/Modify the sid to/in the enabled_disabled_sids hash.
365 $enabled_disabled_sids{$sid} = "enabled";
366
367 # Drop item from cgiparams hash.
368 delete $cgiparams{$rulefile}{$sid};
369 }
370 }
371 } else {
372 # Look if the rule is enabled.
373 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
374 # Check if the state is 'on' and should be disabled.
375 # In this case there is no entry
376 # for the sid in the cgiparams hash.
377 # Add/Modify it to/in the enabled_disabled_sids hash.
378 $enabled_disabled_sids{$sid} = "disabled";
379
380 # Drop item from cgiparams hash.
381 delete $cgiparams{$rulefile}{$sid};
382 }
383 }
384 }
385 }
386
387 # Open enabled sid's file for writing.
388 open(ENABLED_FILE, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
389
390 # Open disabled sid's file for writing.
391 open(DISABLED_FILE, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
392
393 # Write header to the files.
394 print ENABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
395 print DISABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
396
397 # Check if the hash for enabled/disabled files contains any entries.
398 if (%enabled_disabled_sids) {
399 # Loop through the hash.
400 foreach my $sid (keys %enabled_disabled_sids) {
401 # Check if the sid is enabled.
402 if ($enabled_disabled_sids{$sid} eq "enabled") {
403 # Print the sid to the enabled_sids file.
404 print ENABLED_FILE "enablesid $sid\n";
405 # Check if the sid is disabled.
406 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
407 # Print the sid to the disabled_sids file.
408 print DISABLED_FILE "disablesid $sid\n";
409 # Something strange happende - skip the current sid.
410 } else {
411 next;
412 }
413 }
414 }
415
416 # Close file for enabled_sids after writing.
417 close(ENABLED_FILE);
418
419 # Close file for disabled_sids after writing.
420 close(DISABLED_FILE);
421
422 # Handle enabled / disabled rulefiles.
423 #
424 # Get enabled providers.
425 my @enabled_providers = &IDS::get_enabled_providers();
426
427 # Loop through the array of enabled providers.
428 foreach my $provider(@enabled_providers) {
429 # Array to store the rulefiles which belong to the current processed provider.
430 my @provider_rulefiles = ();
431
432 # Loop through the array of enabled rulefiles.
433 foreach my $rulesfile (@enabled_rulefiles) {
434 # Split the rulefile name.
435 my @filename_parts = split(/-/, "$rulesfile");
436
437 # Assign vendor name for easy processings.
438 my $vendor = @filename_parts[0];
439
440 # Check if the rulesvendor is our current processed enabled provider.
441 if ("$vendor" eq "$provider") {
442 # Add the rulesfile to the array of provider rulesfiles.
443 push(@provider_rulefiles, $rulesfile);
444 }
445
446 # Check if any rulesfiles have been found for this provider.
447 if (@provider_rulefiles) {
448 # Call function and write the providers used rulesfile file.
449 &IDS::write_used_provider_rulefiles_file($provider, @provider_rulefiles);
450 }
451 }
452 }
453
454 # Call function to generate and write the used rulefiles file.
455 &IDS::write_main_used_rulefiles_file(@enabled_providers);
456
457 # Lock the webpage and print message.
458 &working_notice("$Lang::tr{'ids apply ruleset changes'}");
459
460 # Call oinkmaster to alter the ruleset.
461 &IDS::oinkmaster();
462
463 # Check if the IDS is running.
464 if(&IDS::ids_is_running()) {
465 # Check if a restart of suricata is required.
466 if ($suricata_restart_required) {
467 # Call suricatactrl to perform the restart.
468 &IDS::call_suricatactrl("restart");
469 } else {
470 # Call suricatactrl to perform a reload.
471 &IDS::call_suricatactrl("reload");
472 }
473 }
474
475 # Reload page.
476 &reload();
477
478 # Download new ruleset.
479 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update ruleset'}) {
480 # Check if the red device is active.
481 unless (-e "${General::swroot}/red/active") {
482 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
483 }
484
485 # Check if enought free disk space is availabe.
486 if(&IDS::checkdiskspace()) {
487 $errormessage = "$Lang::tr{'not enough disk space'}";
488 }
489
490 # Check if any errors happend.
491 unless ($errormessage) {
492 # Lock the webpage and print notice about downloading
493 # a new ruleset.
494 &working_notice("$Lang::tr{'ids download new ruleset'}");
495
496 # Call subfunction to download the ruleset.
497 if(&IDS::downloadruleset()) {
498 $errormessage = $Lang::tr{'could not download latest updates'};
499
500 # Call function to store the errormessage.
501 &IDS::_store_error_message($errormessage);
502
503 # Preform a reload of the page.
504 &reload();
505 } else {
506 # Call subfunction to launch oinkmaster.
507 &IDS::oinkmaster();
508
509 # Check if the IDS is running.
510 if(&IDS::ids_is_running()) {
511 # Call suricatactrl to perform a reload.
512 &IDS::call_suricatactrl("reload");
513 }
514
515 # Perform a reload of the page.
516 &reload();
517 }
518 }
519 # Save IDS settings.
520 } elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
521 my %oldidssettings;
522 my $reload_page;
523 my $monitored_zones = 0;
524
525 # Read-in current (old) IDS settings.
526 &General::readhash("$IDS::ids_settings_file", \%oldidssettings);
527
528 # Prevent form name from been stored in conf file.
529 delete $cgiparams{'IDS'};
530
531 # Check if the IDS should be enabled.
532 if ($cgiparams{'ENABLE_IDS'} eq "on") {
533 # Get enabled providers.
534 my @enabled_providers = &IDS::get_enabled_providers();
535
536 # Check if any ruleset is available. Otherwise abort and display an error.
537 unless(@enabled_providers) {
538 $errormessage = $Lang::tr{'ids no enabled ruleset provider'};
539 }
540
541 # Loop through the array of available interfaces.
542 foreach my $zone (@network_zones) {
543 # Convert interface name into upper case.
544 my $zone_upper = uc($zone);
545
546 # Check if the IDS is enabled for this interaces.
547 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
548 # Increase count.
549 $monitored_zones++;
550 }
551 }
552
553 # Check if at least one zone should be monitored, or show an error.
554 unless ($monitored_zones >= 1) {
555 $errormessage = $Lang::tr{'ids no network zone'};
556 }
557 }
558
559 # Go on if there are no error messages.
560 if (!$errormessage) {
561 # Store settings into settings file.
562 &General::writehash("$IDS::ids_settings_file", \%cgiparams);
563 }
564
565 # Check if the the automatic rule update hass been touched.
566 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldidssettings{'AUTOUPDATE_INTERVAL'}) {
567 # Call suricatactrl to set the new interval.
568 &IDS::call_suricatactrl("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
569 }
570
571 # Generate file to store the home net.
572 &IDS::generate_home_net_file();
573
574 # Generate file to the store the DNS servers.
575 &IDS::generate_dns_servers_file();
576
577 # Generate file to store the HTTP ports.
578 &IDS::generate_http_ports_file();
579
580 # Write the modify sid's file and pass the taken ruleaction.
581 &IDS::write_modify_sids_file();
582
583 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
584 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
585 # Check if a ruleset exists.
586 if (%used_providers) {
587 # Lock the webpage and print message.
588 &working_notice("$Lang::tr{'ids working'}");
589
590 # Call oinkmaster to alter the ruleset.
591 &IDS::oinkmaster();
592
593 # Set reload_page to "True".
594 $reload_page="True";
595 }
596 }
597
598 # Check if the IDS currently is running.
599 if(&IDS::ids_is_running()) {
600 # Check if ENABLE_IDS is set to on.
601 if($cgiparams{'ENABLE_IDS'} eq "on") {
602 # Call suricatactrl to perform a reload of suricata.
603 &IDS::call_suricatactrl("reload");
604 } else {
605 # Call suricatactrl to stop suricata.
606 &IDS::call_suricatactrl("stop");
607 }
608 } else {
609 # Call suricatactrl to start suricata.
610 &IDS::call_suricatactrl("start");
611 }
612
613 # Check if the page should be reloaded.
614 if ($reload_page) {
615 # Perform a reload of the page.
616 &reload();
617 }
618
619 # Toggle Enable/Disable autoupdate for a provider
620 } elsif ($cgiparams{'AUTOUPDATE'} eq $Lang::tr{'toggle enable disable'}) {
621 my %used_providers = ();
622
623 # Only go further, if an ID has been passed.
624 if ($cgiparams{'ID'}) {
625 # Assign the given ID.
626 my $id = $cgiparams{'ID'};
627
628 # Undef the given ID.
629 undef($cgiparams{'ID'});
630
631 # Read-in providers settings file.
632 &General::readhasharray($IDS::providers_settings_file, \%used_providers);
633
634 # Grab the configured status of the corresponding entry.
635 my $status_autoupdate = $used_providers{$id}[2];
636
637 # Switch the status.
638 if ($status_autoupdate eq "disabled") {
639 $status_autoupdate = "enabled";
640 } else {
641 $status_autoupdate = "disabled";
642 }
643
644 # Modify the status of the existing entry.
645 $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$status_autoupdate", "$used_providers{$id}[3]"];
646
647 # Write the changed hash to the providers settings file.
648 &General::writehasharray($IDS::providers_settings_file, \%used_providers);
649 }
650
651 # Add/Edit a provider to the list of used providers.
652 #
653 } elsif (($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'update'}")) {
654 my %used_providers = ();
655
656 # Read-in providers settings file.
657 &General::readhasharray("$IDS::providers_settings_file", \%used_providers);
658
659 # Assign some nice human-readable values.
660 my $provider = $cgiparams{'PROVIDER'};
661 my $subscription_code = $cgiparams{'SUBSCRIPTION_CODE'};
662 my $status_autoupdate;
663
664 # Handle autoupdate checkbox.
665 if ($cgiparams{'ENABLE_AUTOUPDATE'} eq "on") {
666 $status_autoupdate = "enabled";
667 } else {
668 $status_autoupdate = "disabled";
669 }
670
671 # Check if we are going to add a new provider.
672 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") {
673 # Loop through the hash of used providers.
674 foreach my $id ( keys %used_providers) {
675 # Check if the choosen provider is already in use.
676 if ($used_providers{$id}[0] eq "$provider") {
677 # Assign error message.
678 $errormessage = "$Lang::tr{'ids the choosen provider is already in use'}";
679 }
680 }
681 }
682
683 # Check if the provider requires a subscription code.
684 if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") {
685 # Check if an subscription code has been provided.
686 if ($subscription_code) {
687 # Check if the code contains unallowed chars.
688 unless ($subscription_code =~ /^[a-z0-9]+$/) {
689 $errormessage = $Lang::tr{'invalid input for subscription code'};
690 }
691 } else {
692 # Print an error message, that an subsription code is required for this
693 # provider.
694 $errormessage = $Lang::tr{'ids subscription code required'};
695 }
696 }
697
698 # Go further if there was no error.
699 if ($errormessage eq '') {
700 my $id;
701 my $status;
702
703 # Check if we should edit an existing entry and got an ID.
704 if (($cgiparams{'PROVIDERS'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) {
705 # Assin the provided id.
706 $id = $cgiparams{'ID'};
707
708 # Undef the given ID.
709 undef($cgiparams{'ID'});
710
711 # Grab the configured status of the corresponding entry.
712 $status = $used_providers{$id}[3];
713 } else {
714 # Each newly added entry automatically should be enabled.
715 $status = "enabled";
716
717 # Generate the ID for the new entry.
718 #
719 # Sort the keys by their ID and store them in an array.
720 my @keys = sort { $a <=> $b } keys %used_providers;
721
722 # Reverse the key array.
723 my @reversed = reverse(@keys);
724
725 # Obtain the last used id.
726 my $last_id = @reversed[0];
727
728 # Increase the last id by one and use it as id for the new entry.
729 $id = ++$last_id;
730 }
731
732 # Add/Modify the entry to/in the used providers hash..
733 $used_providers{$id} = ["$provider", "$subscription_code", "$status_autoupdate", "$status"];
734
735 # Write the changed hash to the providers settings file.
736 &General::writehasharray($IDS::providers_settings_file, \%used_providers);
737
738 # Check if a new provider will be added.
739 if ($cgiparams{'PROVIDERS'} eq $Lang::tr{'add'}) {
740 # Lock the webpage and print notice about downloading
741 # a new ruleset.
742 &working_notice("$Lang::tr{'ids working'}");
743
744 # Download the ruleset.
745 &IDS::downloadruleset($provider);
746
747 # Extract the ruleset
748 &IDS::extractruleset($provider);
749
750 # Move the ruleset.
751 &IDS::move_tmp_ruleset();
752
753 # Cleanup temporary directory.
754 &IDS::cleanup_tmp_directory();
755
756 # Create new empty file for used rulefiles
757 # for this provider.
758 &IDS::write_used_provider_rulefiles_file($provider);
759
760 # Perform a reload of the page.
761 &reload();
762 }
763
764 # Undefine providers flag.
765 undef($cgiparams{'PROVIDERS'});
766 }
767
768 ## Toggle Enabled/Disabled for an existing provider.
769 #
770 } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr{'toggle enable disable'}) {
771 my %used_providers = ();
772
773 # Only go further, if an ID has been passed.
774 if ($cgiparams{'ID'}) {
775 # Assign the given ID.
776 my $id = $cgiparams{'ID'};
777
778 # Undef the given ID.
779 undef($cgiparams{'ID'});
780
781 # Read-in file which contains the provider settings.
782 &General::readhasharray($IDS::providers_settings_file, \%used_providers);
783
784 # Grab the configured status of the corresponding entry.
785 my $status = $used_providers{$id}[3];
786
787 # Switch the status.
788 if ($status eq "enabled") {
789 $status = "disabled";
790 } else {
791 $status = "enabled";
792 }
793
794 # Modify the status of the existing entry.
795 $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$used_providers{$id}[2]", "$status"];
796
797 # Write the changed hash to the providers settings file.
798 &General::writehasharray($IDS::providers_settings_file, \%used_providers);
799
800 # Get all enabled providers.
801 my @enabled_providers = &IDS::get_enabled_providers();
802
803 # Write the main providers include file.
804 &IDS::write_main_used_rulefiles_file(@enabled_providers);
805
806 # Check if the IDS is running.
807 if(&IDS::ids_is_running()) {
808 # Gather the amount of enabled providers (elements in the array).
809 my $amount = @enabled_providers;
810
811 # Check if there are still enabled ruleset providers.
812 if ($amount >= 1) {
813 # Call suricatactrl to perform a restart.
814 &IDS::call_suricatactrl("restart");
815
816 # No active ruleset provider, suricata has to be stopped.
817 } else {
818 # Stop suricata.
819 &IDS::call_suricatactrl("stop");
820 }
821 }
822
823 # Undefine providers flag.
824 undef($cgiparams{'PROVIDERS'});
825 }
826
827 ## Remove provider from the list of used providers.
828 #
829 } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr{'remove'}) {
830 my %used_providers = ();
831
832 # Read-in provider settings file.
833 &General::readhasharray($IDS::providers_settings_file, \%used_providers);
834
835 # Grab the provider name bevore deleting it from hash.
836 my $provider = $used_providers{$cgiparams{'ID'}}[0];
837
838 # Drop entry from the hash.
839 delete($used_providers{$cgiparams{'ID'}});
840
841 # Undef the given ID.
842 undef($cgiparams{'ID'});
843
844 # Write the changed hash to the provide settings file.
845 &General::writehasharray($IDS::providers_settings_file, \%used_providers);
846
847 # Lock the webpage and print message.
848 &working_notice("$Lang::tr{'ids apply ruleset changes'}");
849
850 # Drop the stored ruleset file.
851 &IDS::drop_dl_rulesfile($provider);
852
853 # Get the name of the provider rulessets include file.
854 my $provider_used_rulefile = &get_used_provider_rulesfile_file($provider);
855
856 # Drop the file, it is not longer needed.
857 unlink("$provider_used_rulefile");
858
859 # Regenerate ruleset.
860 &IDS::oinkmaster();
861
862 # Gather all enabled providers.
863 my @enabled_providers = &IDS::get_enabled_providers();
864
865 # Regenerate main providers include file.
866 &IDS::write_main_used_rulefiles_file(@enabled_providers);
867
868 # Check if the IDS is running.
869 if(&IDS::ids_is_running()) {
870 # Get amount of enabled providers.
871 my $amount = @enabled_providers;
872
873 # Check if at least one enabled provider remains.
874 if ($amount >= 1) {
875 # Call suricatactrl to perform a reload.
876 &IDS::call_suricatactrl("restart");
877
878 # Stop suricata if no enabled provider remains.
879 } else {
880 # Call suricatactrel to perform the stop.
881 &IDS::call_suricatactrl("stop");
882 }
883 }
884
885 # Undefine providers flag.
886 undef($cgiparams{'PROVIDERS'});
887
888 # Reload page.
889 &reload();
890 }
891
892 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
893
894 &Header::openbigbox('100%', 'left', '', $errormessage);
895
896 &show_display_error_message();
897
898 if ($cgiparams{'RULESET'} eq "$Lang::tr{'ids customize ruleset'}" ) {
899 &show_customize_ruleset();
900 } elsif ($cgiparams{'PROVIDERS'} ne "") {
901 &show_add_provider();
902 } else {
903 &show_mainpage();
904 }
905
906 &Header::closebigbox();
907 &Header::closepage();
908
909 #
910 ## Tiny function to show if a error message happened.
911 #
912 sub show_display_error_message() {
913 if ($errormessage) {
914 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
915 print "<class name='base'>$errormessage\n";
916 print "&nbsp;</class>\n";
917 &Header::closebox();
918 }
919 }
920
921 #
922 ## Function to display the main IDS page.
923 #
924 sub show_mainpage() {
925 # Read-in idssettings and provider settings.
926 &General::readhash("$IDS::ids_settings_file", \%idssettings);
927 &General::readhasharray("$IDS::providers_settings_file", \%used_providers);
928
929 # If no autoupdate intervall has been configured yet, set default value.
930 unless(exists($idssettings{'AUTOUPDATE_INTERVAL'})) {
931 # Set default to "weekly".
932 $idssettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
933 }
934
935 # Read-in ignored hosts.
936 &General::readhasharray("$IDS::settingsdir/ignored", \%ignored);
937
938 $checked{'ENABLE_IDS'}{'off'} = '';
939 $checked{'ENABLE_IDS'}{'on'} = '';
940 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
941 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
942 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
943 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
944 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
945 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
946 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
947 $selected{'AUTOUPDATE_INTERVAL'}{$idssettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
948
949 # Draw current state of the IDS
950 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
951
952 # Check if the IDS is running and obtain the process-id.
953 my $pid = &IDS::ids_is_running();
954
955 # Display some useful information, if suricata daemon is running.
956 if ($pid) {
957 # Gather used memory.
958 my $memory = &get_memory_usage($pid);
959
960 print <<END;
961 <table width='95%' cellspacing='0' class='tbl'>
962 <tr>
963 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
964 </tr>
965
966 <tr>
967 <td class='base'>$Lang::tr{'guardian daemon'}</td>
968 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
969 </tr>
970
971 <tr>
972 <td class='base'></td>
973 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
974 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
975 </tr>
976
977 <tr>
978 <td class='base'></td>
979 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
980 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
981 </tr>
982 </table>
983 END
984 } else {
985 # Otherwise display a hint that the service is not launched.
986 print <<END;
987 <table width='95%' cellspacing='0' class='tbl'>
988 <tr>
989 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
990 </tr>
991
992 <tr>
993 <td class='base'>$Lang::tr{'guardian daemon'}</td>
994 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
995 </tr>
996 </table>
997 END
998 }
999
1000 # Only show this area, if at least one ruleset provider is configured.
1001 if (%used_providers) {
1002
1003 print <<END
1004
1005 <br><br><h2>$Lang::tr{'settings'}</h2>
1006
1007 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1008 <table width='100%' border='0'>
1009 <tr>
1010 <td class='base' colspan='2'>
1011 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>&nbsp;$Lang::tr{'ids enable'}
1012 </td>
1013
1014 <td class='base' colspan='2'>
1015 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>&nbsp;$Lang::tr{'ids monitor traffic only'}
1016 </td>
1017 </tr>
1018
1019 <tr>
1020 <td><br><br></td>
1021 <td><br><br></td>
1022 <td><br><br></td>
1023 <td><br><br></td>
1024 </tr>
1025
1026 <tr>
1027 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
1028 </tr>
1029
1030 <tr>
1031 END
1032 ;
1033
1034 # Loop through the array of available networks and print config options.
1035 foreach my $zone (@network_zones) {
1036 my $checked_input;
1037 my $checked_forward;
1038
1039 # Convert current zone name to upper case.
1040 my $zone_upper = uc($zone);
1041
1042 # Set zone name.
1043 my $zone_name = $zone;
1044
1045 # Dirty hack to get the correct language string for the red zone.
1046 if ($zone eq "red") {
1047 $zone_name = "red1";
1048 }
1049
1050 # Grab checkbox status from settings hash.
1051 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
1052 $checked_input = "checked = 'checked'";
1053 }
1054
1055 print "<td class='base' width='20%'>\n";
1056 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
1057 print "&nbsp;$Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
1058 print "</td>\n";
1059 }
1060
1061 print <<END
1062 </tr>
1063
1064 <tr>
1065 <td><br><br></td>
1066 <td><br><br></td>
1067 <td><br><br></td>
1068 <td><br><br></td>
1069 </tr>
1070
1071 <tr>
1072 <td colspan='4'><b>$Lang::tr{'ids automatic rules update'}</b></td>
1073 </tr>
1074
1075 <tr>
1076 <td>
1077 <select name='AUTOUPDATE_INTERVAL'>
1078 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
1079 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
1080 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
1081 </select>
1082 </td>
1083 </tr>
1084 </table>
1085
1086 <br><br>
1087
1088 <table width='100%'>
1089 <tr>
1090 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
1091 </tr>
1092 </table>
1093 </form>
1094 END
1095 ;
1096
1097 }
1098
1099 &Header::closebox();
1100
1101 #
1102 # Used Ruleset Providers section.
1103 #
1104 &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'});
1105
1106 print <<END;
1107 <table width='100%' border='0'>
1108 <tr>
1109 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ids provider'}</b></td>
1110 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'date'}</b></td>
1111 <td class='base' bgcolor='$color{'color20'}' align='center'><b>$Lang::tr{'ids autoupdates'}</b></td>
1112 <td class='base' bgcolor='$color{'color20'}'></td>
1113 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1114 </tr>
1115 END
1116 # Check if some providers has been configured.
1117 if (keys (%used_providers)) {
1118 my $col = "";
1119
1120 # Loop through all entries of the hash.
1121 foreach my $id (sort keys(%used_providers)) {
1122 # Assign data array positions to some nice variable names.
1123 my $provider = $used_providers{$id}[0];
1124 my $provider_name = &get_provider_name($provider);
1125 my $rulesetdate = &IDS::get_ruleset_date($provider);
1126
1127 my $subscription_code = $used_providers{$id}[1];
1128 my $autoupdate_status = $used_providers{$id}[2];
1129 my $status = $used_providers{$id}[3];
1130
1131 # Check if the item number is even or not.
1132 if ($id % 2) {
1133 $col="bgcolor='$color{'color22'}'";
1134 } else {
1135 $col="bgcolor='$color{'color20'}'";
1136 }
1137
1138 # Choose icons for the checkboxes.
1139 my $status_gif;
1140 my $status_gdesc;
1141 my $autoupdate_status_gif;
1142 my $autoupdate_status_gdesc;
1143
1144 # Check if the status is enabled and select the correct image and description.
1145 if ($status eq 'enabled' ) {
1146 $status_gif = 'on.gif';
1147 $status_gdesc = $Lang::tr{'click to disable'};
1148 } else {
1149 $status_gif = 'off.gif';
1150 $status_gdesc = $Lang::tr{'click to enable'};
1151 }
1152
1153 # Check if the autoupdate status is enabled and select the correct image and description.
1154 if ($autoupdate_status eq 'enabled') {
1155 $autoupdate_status_gif = 'on.gif';
1156 $autoupdate_status_gdesc = $Lang::tr{'click to disable'};
1157 } else {
1158 $autoupdate_status_gif = 'off.gif';
1159 $autoupdate_status_gdesc = $Lang::tr{'click to enable'};
1160 }
1161
1162 print <<END;
1163 <tr>
1164 <td width='33%' class='base' $col>$provider_name</td>
1165 <td width='30%' class='base' $col>$rulesetdate</td>
1166
1167 <td align='center' $col>
1168 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1169 <input type='hidden' name='AUTOUPDATE' value='$Lang::tr{'toggle enable disable'}' />
1170 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$autoupdate_status_gif' alt='$autoupdate_status_gdesc' title='$autoupdate_status_gdesc' />
1171 <input type='hidden' name='ID' value='$id' />
1172 </form>
1173 </td>
1174
1175 <td align='center' $col>
1176 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1177 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'toggle enable disable'}'>
1178 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$status_gif' alt='$status_gdesc' title='$status_gdesc'>
1179 <input type='hidden' name='ID' value='$id'>
1180 </form>
1181 </td>
1182
1183 <td align='center' $col>
1184 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1185 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'edit'}'>
1186 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1187 <input type='hidden' name='ID' value='$id'>
1188 </form>
1189 </td>
1190
1191 <td align='center' $col>
1192 <form method='post' name='$provider' action='$ENV{'SCRIPT_NAME'}'>
1193 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1194 <input type='hidden' name='ID' value='$id'>
1195 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'remove'}'>
1196 </form>
1197 </td>
1198 </tr>
1199 END
1200 }
1201
1202 } else {
1203 # Print notice that currently no hosts are ignored.
1204 print "<tr>\n";
1205 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1206 print "</tr>\n";
1207 }
1208
1209 print "</table>\n";
1210
1211 # Section to add new elements or edit existing ones.
1212 print <<END;
1213 <br>
1214 <hr>
1215 <br>
1216
1217 <div align='right'>
1218 <table width='100%'>
1219 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1220 <tr>
1221 END
1222
1223 # Only show this button if a ruleset provider is configured.
1224 if (%used_providers) {
1225 print "<input type='submit' name='RULESET' value='$Lang::tr{'ids customize ruleset'}'>\n";
1226 }
1227 print <<END;
1228 <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids add provider'}'>
1229 </tr>
1230 </form>
1231 </table>
1232 </div>
1233 END
1234
1235 &Header::closebox();
1236
1237 #
1238 # Whitelist / Ignorelist
1239 #
1240 &Header::openbox('100%', 'center', $Lang::tr{'ids ignored hosts'});
1241
1242 print <<END;
1243 <table width='100%'>
1244 <tr>
1245 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
1246 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
1247 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1248 </tr>
1249 END
1250 # Check if some hosts have been added to be ignored.
1251 if (keys (%ignored)) {
1252 my $col = "";
1253
1254 # Loop through all entries of the hash.
1255 while( (my $key) = each %ignored) {
1256 # Assign data array positions to some nice variable names.
1257 my $address = $ignored{$key}[0];
1258 my $remark = $ignored{$key}[1];
1259 my $status = $ignored{$key}[2];
1260
1261 # Check if the key (id) number is even or not.
1262 if ($cgiparams{'ID'} eq $key) {
1263 $col="bgcolor='${Header::colouryellow}'";
1264 } elsif ($key % 2) {
1265 $col="bgcolor='$color{'color22'}'";
1266 } else {
1267 $col="bgcolor='$color{'color20'}'";
1268 }
1269
1270 # Choose icon for the checkbox.
1271 my $gif;
1272 my $gdesc;
1273
1274 # Check if the status is enabled and select the correct image and description.
1275 if ($status eq 'enabled' ) {
1276 $gif = 'on.gif';
1277 $gdesc = $Lang::tr{'click to disable'};
1278 } else {
1279 $gif = 'off.gif';
1280 $gdesc = $Lang::tr{'click to enable'};
1281 }
1282
1283 print <<END;
1284 <tr>
1285 <td width='20%' class='base' $col>$address</td>
1286 <td width='65%' class='base' $col>$remark</td>
1287
1288 <td align='center' $col>
1289 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1290 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}'>
1291 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc'>
1292 <input type='hidden' name='ID' value='$key'>
1293 </form>
1294 </td>
1295
1296 <td align='center' $col>
1297 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1298 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}'>
1299 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1300 <input type='hidden' name='ID' value='$key'>
1301 </form>
1302 </td>
1303
1304 <td align='center' $col>
1305 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
1306 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1307 <input type='hidden' name='ID' value='$key'>
1308 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1309 </form>
1310 </td>
1311 </tr>
1312 END
1313 }
1314 } else {
1315 # Print notice that currently no hosts are ignored.
1316 print "<tr>\n";
1317 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1318 print "</tr>\n";
1319 }
1320
1321 print "</table>\n";
1322
1323 # Section to add new elements or edit existing ones.
1324 print <<END;
1325 <br>
1326 <hr>
1327 <br>
1328
1329 <div align='center'>
1330 <table width='100%'>
1331 END
1332
1333 # Assign correct headline and button text.
1334 my $buttontext;
1335 my $entry_address;
1336 my $entry_remark;
1337
1338 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1339 if ($cgiparams{'ID'} ne '') {
1340 $buttontext = $Lang::tr{'update'};
1341 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1342
1343 # Grab address and remark for the given key.
1344 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1345 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1346 } else {
1347 $buttontext = $Lang::tr{'add'};
1348 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1349 }
1350
1351 print <<END;
1352 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1353 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1354 <tr>
1355 <td width='30%'>$Lang::tr{'ip address'}: </td>
1356 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1357
1358 <td width='30%'>$Lang::tr{'remark'}: </td>
1359 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1360 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1361 </tr>
1362 </form>
1363 </table>
1364 </div>
1365 END
1366
1367 &Header::closebox();
1368 }
1369
1370 #
1371 ## Function to show the customize ruleset section.
1372 #
1373 sub show_customize_ruleset() {
1374 ### Java Script ###
1375 print"<script>\n";
1376
1377 # Java script variable declaration for show and hide.
1378 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
1379 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
1380
1381 print <<END
1382 // Tiny java script function to show/hide the rules
1383 // of a given category.
1384 function showhide(tblname) {
1385 \$("#" + tblname).toggle();
1386
1387 // Get current content of the span element.
1388 var content = document.getElementById("span_" + tblname);
1389
1390 if (content.innerHTML === show) {
1391 content.innerHTML = hide;
1392 } else {
1393 content.innerHTML = show;
1394 }
1395 }
1396 </script>
1397 END
1398 ;
1399 &Header::openbox('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'}" );
1400 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1401
1402 # Output display table for rule files
1403 print "<table width='100%'>\n";
1404
1405 # Loop over each rule file
1406 foreach my $rulefile (sort keys(%idsrules)) {
1407 my $rulechecked = '';
1408
1409 # Check if rule file is enabled
1410 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1411 $rulechecked = 'CHECKED';
1412 }
1413
1414 # Convert rulefile name into category name.
1415 my $categoryname = &_rulefile_to_category($rulefile);
1416
1417 # Table and rows for the rule files.
1418 print"<tr>\n";
1419 print"<td class='base' width='5%'>\n";
1420 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1421 print"</td>\n";
1422 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1423 print"<td class='base' width='5%' align='right'>\n";
1424 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1425 print"</td>\n";
1426 print"</tr>\n";
1427
1428 # Rows which will be hidden per default and will contain the single rules.
1429 print"<tr style='display:none' id='$categoryname'>\n";
1430 print"<td colspan='3'>\n";
1431
1432 # Local vars
1433 my $lines;
1434 my $rows;
1435 my $col;
1436
1437 # New table for the single rules.
1438 print "<table width='100%'>\n";
1439
1440 # Loop over rule file rules
1441 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1442 # Local vars
1443 my $ruledefchecked = '';
1444
1445 # Skip rulefile itself.
1446 next if ($sid eq "Rulefile");
1447
1448 # If 2 rules have been displayed, start a new row
1449 if (($lines % 2) == 0) {
1450 print "</tr><tr>\n";
1451
1452 # Increase rows by once.
1453 $rows++;
1454 }
1455
1456 # Colour lines.
1457 if ($rows % 2) {
1458 $col="bgcolor='$color{'color20'}'";
1459 } else {
1460 $col="bgcolor='$color{'color22'}'";
1461 }
1462
1463 # Set rule state
1464 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1465 $ruledefchecked = 'CHECKED';
1466 }
1467
1468 # Create rule checkbox and display rule description
1469 print "<td class='base' width='5%' align='right' $col>\n";
1470 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1471 print "</td>\n";
1472 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1473
1474 # Increment rule count
1475 $lines++;
1476 }
1477
1478 # If do not have a second rule for row, create empty cell
1479 if (($lines % 2) != 0) {
1480 print "<td class='base'></td>";
1481 }
1482
1483 # Close display table
1484 print "</tr></table></td></tr>";
1485 }
1486
1487 # Close display table
1488 print "</table>";
1489
1490 print <<END
1491 <table width='100%'>
1492 <tr>
1493 <td width='100%' align='right'>
1494 <input type='submit' value='$Lang::tr{'fwhost back'}'>
1495 <input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'>
1496 </td>
1497 </tr>
1498 </table>
1499 </form>
1500 END
1501 ;
1502 &Header::closebox();
1503 }
1504 }
1505
1506 #
1507 ## Function to show section for add/edit a provider.
1508 #
1509 sub show_add_provider() {
1510 my %used_providers = ();
1511 my @subscription_providers;
1512
1513 # Read -in providers settings file.
1514 &General::readhasharray("$IDS::providers_settings_file", \%used_providers);
1515
1516 # Get all supported ruleset providers.
1517 my @ruleset_providers = &IDS::get_ruleset_providers();
1518
1519 ### Java Script ###
1520 print "<script>\n";
1521
1522 # Generate Java Script Object which contains the URL of the providers.
1523 print "\t// Object, which contains the webpages of the ruleset providers.\n";
1524 print "\tvar url = {\n";
1525
1526 # Loop through the array of supported providers.
1527 foreach my $provider (@ruleset_providers) {
1528 # Check if the provider requires a subscription.
1529 if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") {
1530 # Add the provider to the array of subscription_providers.
1531 push(@subscription_providers, $provider);
1532 }
1533
1534 # Grab the URL for the provider.
1535 my $url = $IDS::Ruleset::Providers{$provider}{'website'};
1536
1537 # Print the URL to the Java Script Object.
1538 print "\t\t$provider: \"$url\"\,\n";
1539 }
1540
1541 # Close the Java Script Object declaration.
1542 print "\t}\;\n\n";
1543
1544 # Generate Java Script Array which contains the provider that requires a subscription.
1545 my $line = "";
1546 $line = join("', '", @subscription_providers);
1547
1548 print "\t// Array which contains the providers that requires a subscription.\n";
1549 print "\tsubscription_provider = ['$line']\;\n\n";
1550
1551 print <<END
1552 // Java Script function to swap the text input field for
1553 // entering a subscription code.
1554 var update_provider = function() {
1555 if(inArray(\$('#PROVIDER').val(), subscription_provider)) {
1556 \$('.subscription_code').show();
1557 } else {
1558 \$('.subscription_code').hide();
1559 }
1560
1561 // Call function to change the website url.
1562 change_url(\$('#PROVIDER').val());
1563 };
1564
1565 // Java Script function to check if a given value is part of
1566 // an array.
1567 function inArray(value,array) {
1568 var count=array.length;
1569
1570 for(var i=0;i<count;i++) {
1571 if(array[i]===value){
1572 return true;
1573 }
1574 }
1575
1576 return false;
1577 }
1578
1579 // Tiny function to change the website url based on the selected element in the "PROVIDERS"
1580 // dropdown menu.
1581 function change_url(provider) {
1582 // Get and change the href to the corresponding url.
1583 document.getElementById("website").href = url[provider];
1584 }
1585
1586 // JQuery function to call corresponding function when
1587 // the ruleset provider is changed or the page is loaded for showing/hiding
1588 // the subscription_code area.
1589 \$(document).ready(function() {
1590 \$('#PROVIDER').change(update_provider);
1591 update_provider();
1592 });
1593
1594 </script>
1595 END
1596 ;
1597
1598 &Header::openbox('100%', 'center', $Lang::tr{'ids provider settings'});
1599
1600 # Check if an existing provider should be edited.
1601 if($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1602 # Check if autoupdate is enabled for this provider.
1603 if ($used_providers{$cgiparams{'ID'}}[2] eq "enabled") {
1604 # Set the checkbox to be checked.
1605 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1606 }
1607 } elsif ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'ids add provider'}") {
1608 # Set the autoupdate to true as default.
1609 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1610 }
1611
1612 print <<END
1613 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1614 <table width='100%' border='0'>
1615 <tr>
1616 <td colspan='2'><b>$Lang::tr{'ids provider'}</b></td>
1617 </tr>
1618
1619 <tr>
1620 <td width='40%'>
1621 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1622 END
1623 ;
1624 # Value to allow disabling the dropdown menu.
1625 my $disabled;
1626
1627 # Check if we are in edit mode.
1628 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1629 $disabled = "disabled";
1630
1631 # Add hidden input with the provider because the disable select does not provider
1632 # this.
1633 print "<input type='hidden' name='PROVIDER' value='$used_providers{$cgiparams{'ID'}}[0]'>\n";
1634 }
1635
1636 print "<select name='PROVIDER' id='PROVIDER' $disabled>\n";
1637 # Loop through the array of ruleset providers.
1638 foreach my $provider (@ruleset_providers) {
1639 # Get the provider name.
1640 my $provider_name = &get_provider_name($provider);
1641
1642 # Pre-select the provider if one is given.
1643 if (($used_providers{$cgiparams{'ID'}}[0] eq "$provider") || ($cgiparams{'PROVIDER'} eq "$provider")) {
1644 $selected{$provider} = "selected='selected'";
1645 }
1646
1647 # Add the provider to the dropdown menu.
1648 print "<option value='$provider' $selected{$provider}>$provider_name</option>\n";
1649 }
1650 print <<END
1651 </select>
1652 </td>
1653
1654 <td width='60%'>
1655 <b><a id="website" target="_blank" href="#">$Lang::tr{'ids visit provider website'}</a></b>
1656 </td>
1657 </tr>
1658
1659 <tr>
1660 <td colspan='2'><br><br></td>
1661 </tr>
1662
1663 <tr class='subscription_code' style='display:none' id='subscription_code'>
1664 <td colspan='2'>
1665 <table border='0'>
1666 <tr>
1667 <td>
1668 <b>$Lang::tr{'subscription code'}</b>
1669 </td>
1670 </tr>
1671
1672 <tr>
1673 <td>
1674 <input type='text' size='40' name='SUBSCRIPTION_CODE' value='$used_providers{$cgiparams{'ID'}}[1]'>
1675 </td>
1676 </tr>
1677
1678 <tr>
1679 <td><br><br></td>
1680 </tr>
1681 </table>
1682 </td>
1683 </tr>
1684
1685 <tr>
1686 <td colspan='2'>
1687 <input type='checkbox' name='ENABLE_AUTOUPDATE' $checked{'ENABLE_AUTOUPDATE'}>&nbsp;$Lang::tr{'ids enable automatic updates'}
1688 </td>
1689 </tr>
1690
1691 <tr>
1692 <td colspan='2' align='right'>
1693 <input type='submit' value='$Lang::tr{'back'}'>
1694 END
1695 ;
1696 # Check if a provider should be added or edited.
1697 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1698 # Display button for updating the existing provider.
1699 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'update'}'>\n";
1700 } else {
1701 # Display button to add the new provider.
1702 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'add'}'>\n";
1703 }
1704 print <<END
1705 </td>
1706 </tr>
1707 </table>
1708 </form>
1709 END
1710 ;
1711 &Header::closebox();
1712 }
1713
1714 #
1715 ## A function to display a notice, to lock the webpage and
1716 ## tell the user which action currently will be performed.
1717 #
1718 sub working_notice ($) {
1719 my ($message) = @_;
1720
1721 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
1722 &Header::openbigbox('100%', 'left', '', $errormessage);
1723 &Header::openbox( 'Waiting', 1,);
1724 print <<END;
1725 <table>
1726 <tr>
1727 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1728 <td>$message</td>
1729 </tr>
1730 </table>
1731 END
1732 &Header::closebox();
1733 &Header::closebigbox();
1734 &Header::closepage();
1735 }
1736
1737 #
1738 ## A tiny function to perform a reload of the webpage after one second.
1739 #
1740 sub reload () {
1741 print "<meta http-equiv='refresh' content='1'>\n";
1742
1743 # Stop the script.
1744 exit;
1745 }
1746
1747 #
1748 ## Private function to read-in and parse rules of a given rulefile.
1749 #
1750 ## The given file will be read, parsed and all valid rules will be stored by ID,
1751 ## message/description and it's state in the idsrules hash.
1752 #
1753 sub readrulesfile ($) {
1754 my $rulefile = shift;
1755
1756 # Open rule file and read in contents
1757 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1758
1759 # Store file content in an array.
1760 my @lines = <RULEFILE>;
1761
1762 # Close file.
1763 close(RULEFILE);
1764
1765 # Loop over rule file contents
1766 foreach my $line (@lines) {
1767 # Remove whitespaces.
1768 chomp $line;
1769
1770 # Skip blank lines.
1771 next if ($line =~ /^\s*$/);
1772
1773 # Local vars.
1774 my $sid;
1775 my $msg;
1776
1777 # Gather rule sid and message from the ruleline.
1778 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1779 $msg = $1;
1780 $sid = $2;
1781
1782 # Check if a rule has been found.
1783 if ($sid && $msg) {
1784 # Add rule to the idsrules hash.
1785 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1786
1787 # Grab status of the rule. Check if ruleline starts with a "dash".
1788 if ($line =~ /^\#/) {
1789 # If yes, the rule is disabled.
1790 $idsrules{$rulefile}{$sid}{'State'} = "off";
1791 } else {
1792 # Otherwise the rule is enabled.
1793 $idsrules{$rulefile}{$sid}{'State'} = "on";
1794 }
1795 }
1796 }
1797 }
1798 }
1799
1800 #
1801 ## Function to get the used memory of a given process-id.
1802 #
1803 sub get_memory_usage($) {
1804 my ($pid) = @_;
1805
1806 my $memory = 0;
1807
1808 # Try to open the status file for the given process-id on the pseudo
1809 # file system proc.
1810 if (open(FILE, "/proc/$pid/status")) {
1811 # Loop through the entire file.
1812 while (<FILE>) {
1813 # Splitt current line content and store them into variables.
1814 my ($key, $value) = split(":", $_, 2);
1815
1816 # Check if the current key is the one which contains the memory usage.
1817 # The wanted one is VmRSS which contains the Real-memory (resident set)
1818 # of the entire process.
1819 if ($key eq "VmRSS") {
1820 # Found the memory usage add it to the memory variable.
1821 $memory += $value;
1822
1823 # Break the loop.
1824 last;
1825 }
1826 }
1827
1828 # Close file handle.
1829 close(FILE);
1830
1831 # Return memory usage.
1832 return $memory;
1833 }
1834
1835 # If the file could not be open, return nothing.
1836 return;
1837 }
1838
1839 #
1840 ## Function to read-in the given enabled or disables sids file.
1841 #
1842 sub read_enabled_disabled_sids_file($) {
1843 my ($file) = @_;
1844
1845 # Temporary hash to store the sids and their state. It will be
1846 # returned at the end of this function.
1847 my %temphash;
1848
1849 # Open the given filename.
1850 open(FILE, "$file") or die "Could not open $file. $!\n";
1851
1852 # Loop through the file.
1853 while(<FILE>) {
1854 # Remove newlines.
1855 chomp $_;
1856
1857 # Skip blank lines.
1858 next if ($_ =~ /^\s*$/);
1859
1860 # Skip coments.
1861 next if ($_ =~ /^\#/);
1862
1863 # Splitt line into sid and state part.
1864 my ($state, $sid) = split(" ", $_);
1865
1866 # Skip line if the sid is not numeric.
1867 next unless ($sid =~ /\d+/ );
1868
1869 # Check if the sid was enabled.
1870 if ($state eq "enablesid") {
1871 # Add the sid and its state as enabled to the temporary hash.
1872 $temphash{$sid} = "enabled";
1873 # Check if the sid was disabled.
1874 } elsif ($state eq "disablesid") {
1875 # Add the sid and its state as disabled to the temporary hash.
1876 $temphash{$sid} = "disabled";
1877 # Invalid state - skip the current sid and state.
1878 } else {
1879 next;
1880 }
1881 }
1882
1883 # Close filehandle.
1884 close(FILE);
1885
1886 # Return the hash.
1887 return %temphash;
1888 }
1889
1890 #
1891 ## Function to get the provider name from the language file or providers file for a given handle.
1892 #
1893 sub get_provider_name($) {
1894 my ($handle) = @_;
1895 my $provider_name;
1896
1897 # Get the required translation string for the given provider handle.
1898 my $tr_string = $IDS::Ruleset::Providers{$handle}{'tr_string'};
1899
1900 # Check if the translation string is available in the language files.
1901 if ($Lang::tr{$tr_string}) {
1902 # Use the translated string from the language file.
1903 $provider_name = $Lang::tr{$tr_string};
1904 } else {
1905 # Fallback and use the provider summary from the providers file.
1906 $provider_name = $IDS::Ruleset::Providers{$handle}{'summary'};
1907 }
1908
1909 # Return the obtained provider name.
1910 return $provider_name;
1911 }
1912
1913 #
1914 ## Private function to convert a given rulefile to a category name.
1915 ## ( No file extension anymore and if the name contained a dot, it
1916 ## would be replaced by a underline sign.)
1917 #
1918 sub _rulefile_to_category($) {
1919 my ($filename) = @_;
1920
1921 # Splitt the filename into single chunks and store them in a
1922 # temorary array.
1923 my @parts = split(/\./, $filename);
1924
1925 # Return / Remove last element of the temporary array.
1926 # This removes the file extension.
1927 pop @parts;
1928
1929 # Join together the single elements of the temporary array.
1930 # If these are more than one, use a "underline" for joining.
1931 my $category = join '_', @parts;
1932
1933 # Return the converted filename.
1934 return $category;
1935 }