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