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