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