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