]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Show IDS setting area only if a ruleset is present.
[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-2018 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
33 my %color = ();
34 my %mainsettings = ();
35 my %idsrules = ();
36 my %idssettings=();
37 my %rulessettings=();
38 my %rulesetsources = ();
39 my %cgiparams=();
40 my %checked=();
41 my %selected=();
42 my %ignored=();
43
44 # Read-in main settings, for language, theme and colors.
45 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
46 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
47
48 # Get the available network zones, based on the config type of the system and store
49 # the list of zones in an array.
50 my @network_zones = &IDS::get_available_network_zones();
51
52 my $errormessage;
53
54 # Create files if they does not exist yet.
55 &IDS::check_and_create_filelayout();
56
57 # Hash which contains the colour code of a network zone.
58 my %colourhash = (
59 'red' => $Header::colourred,
60 'green' => $Header::colourgreen,
61 'blue' => $Header::colourblue,
62 'orange' => $Header::colourorange
63 );
64
65 &Header::showhttpheaders();
66
67 #Get GUI values
68 &Header::getcgihash(\%cgiparams);
69
70 ## Add/edit an entry to the ignore file.
71 #
72 if (($cgiparams{'WHITELIST'} eq $Lang::tr{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr{'update'})) {
73
74 # Check if any input has been performed.
75 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
76
77 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
78 if ((!&General::validip($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General::validipandmask($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
79 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
80 }
81 } else {
82 $errormessage = "$Lang::tr{'guardian empty input'}";
83 }
84
85 # Go further if there was no error.
86 if ($errormessage eq '') {
87 my %ignored = ();
88 my $id;
89 my $status;
90
91 # Assign hash values.
92 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
93 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
94
95 # Read-in ignoredfile.
96 &General::readhasharray($IDS::ignored_file, \%ignored);
97
98 # Check if we should edit an existing entry and got an ID.
99 if (($cgiparams{'WHITELIST'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) {
100 # Assin the provided id.
101 $id = $cgiparams{'ID'};
102
103 # Undef the given ID.
104 undef($cgiparams{'ID'});
105
106 # Grab the configured status of the corresponding entry.
107 $status = $ignored{$id}[2];
108 } else {
109 # Each newly added entry automatically should be enabled.
110 $status = "enabled";
111
112 # Generate the ID for the new entry.
113 #
114 # Sort the keys by their ID and store them in an array.
115 my @keys = sort { $a <=> $b } keys %ignored;
116
117 # Reverse the key array.
118 my @reversed = reverse(@keys);
119
120 # Obtain the last used id.
121 my $last_id = @reversed[0];
122
123 # Increase the last id by one and use it as id for the new entry.
124 $id = ++$last_id;
125 }
126
127 # Add/Modify the entry to/in the ignored hash.
128 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
129
130 # Write the changed ignored hash to the ignored file.
131 &General::writehasharray($IDS::ignored_file, \%ignored);
132
133 # Regenerate the ignore file.
134 &IDS::generate_ignore_file();
135 }
136
137 # Check if the IDS is running.
138 if(&IDS::ids_is_running()) {
139 # Call suricatactrl to perform a reload.
140 &IDS::call_suricatactrl("reload");
141 }
142
143 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
144 #
145
146 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'toggle enable disable'}) {
147 my %ignored = ();
148
149 # Only go further, if an ID has been passed.
150 if ($cgiparams{'ID'}) {
151 # Assign the given ID.
152 my $id = $cgiparams{'ID'};
153
154 # Undef the given ID.
155 undef($cgiparams{'ID'});
156
157 # Read-in ignoredfile.
158 &General::readhasharray($IDS::ignored_file, \%ignored);
159
160 # Grab the configured status of the corresponding entry.
161 my $status = $ignored{$id}[2];
162
163 # Switch the status.
164 if ($status eq "disabled") {
165 $status = "enabled";
166 } else {
167 $status = "disabled";
168 }
169
170 # Modify the status of the existing entry.
171 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
172
173 # Write the changed ignored hash to the ignored file.
174 &General::writehasharray($IDS::ignored_file, \%ignored);
175
176 # Regenerate the ignore file.
177 &IDS::generate_ignore_file();
178
179 # Check if the IDS is running.
180 if(&IDS::ids_is_running()) {
181 # Call suricatactrl to perform a reload.
182 &IDS::call_suricatactrl("reload");
183 }
184 }
185
186 ## Remove entry from ignore list.
187 #
188 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'remove'}) {
189 my %ignored = ();
190
191 # Read-in ignoredfile.
192 &General::readhasharray($IDS::ignored_file, \%ignored);
193
194 # Drop entry from the hash.
195 delete($ignored{$cgiparams{'ID'}});
196
197 # Undef the given ID.
198 undef($cgiparams{'ID'});
199
200 # Write the changed ignored hash to the ignored file.
201 &General::writehasharray($IDS::ignored_file, \%ignored);
202
203 # Regenerate the ignore file.
204 &IDS::generate_ignore_file();
205
206 # Check if the IDS is running.
207 if(&IDS::ids_is_running()) {
208 # Call suricatactrl to perform a reload.
209 &IDS::call_suricatactrl("reload");
210 }
211 }
212
213 # Check if any error has been stored.
214 if (-e $IDS::storederrorfile) {
215 # Open file to read in the stored error message.
216 open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
217
218 # Read the stored error message.
219 $errormessage = <FILE>;
220
221 # Close file.
222 close (FILE);
223
224 # Delete the file, which is now not longer required.
225 unlink($IDS::storederrorfile);
226 }
227
228 ## Grab all available snort rules and store them in the idsrules hash.
229 #
230 # Open snort rules directory and do a directory listing.
231 opendir(DIR, $IDS::rulespath) or die $!;
232 # Loop through the direcory.
233 while (my $file = readdir(DIR)) {
234
235 # We only want files.
236 next unless (-f "$IDS::rulespath/$file");
237
238 # Ignore empty files.
239 next if (-z "$IDS::rulespath/$file");
240
241 # Use a regular expression to find files ending in .rules
242 next unless ($file =~ m/\.rules$/);
243
244 # Ignore files which are not read-able.
245 next unless (-R "$IDS::rulespath/$file");
246
247 # Skip whitelist rules file.
248 next if( $file eq "whitelist.rules");
249
250 # Call subfunction to read-in rulefile and add rules to
251 # the idsrules hash.
252 &readrulesfile("$file");
253 }
254
255 closedir(DIR);
256
257 # Gather used rulefiles.
258 #
259 # Check if the file for activated rulefiles is not empty.
260 if(-f $IDS::used_rulefiles_file) {
261 # Open the file for used rulefile and read-in content.
262 open(FILE, $IDS::used_rulefiles_file) or die "Could not open $IDS::used_rulefiles_file. $!\n";
263
264 # Read-in content.
265 my @lines = <FILE>;
266
267 # Close file.
268 close(FILE);
269
270 # Loop through the array.
271 foreach my $line (@lines) {
272 # Remove newlines.
273 chomp($line);
274
275 # Skip comments.
276 next if ($line =~ /\#/);
277
278 # Skip blank lines.
279 next if ($line =~ /^\s*$/);
280
281 # Gather rule sid and message from the ruleline.
282 if ($line =~ /.*- (.*)/) {
283 my $rulefile = $1;
284
285 # Check if the current rulefile exists in the %idsrules hash.
286 # If not, the file probably does not exist anymore or contains
287 # no rules.
288 if($idsrules{$rulefile}) {
289 # Add the rulefile state to the %idsrules hash.
290 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
291 }
292 }
293 }
294 }
295
296 # Save ruleset configuration.
297 if ($cgiparams{'RULESET'} eq $Lang::tr{'save'}) {
298 my %oldsettings;
299
300 # Read-in current (old) IDS settings.
301 &General::readhash("$IDS::rules_settings_file", \%oldsettings);
302
303 # Prevent form name from been stored in conf file.
304 delete $cgiparams{'RULESET'};
305
306 # Check if an oinkcode has been provided.
307 if ($cgiparams{'OINKCODE'}) {
308 # Check if the oinkcode contains unallowed chars.
309 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
310 $errormessage = $Lang::tr{'invalid input for oink code'};
311 }
312 }
313
314 # Go on if there are no error messages.
315 if (!$errormessage) {
316 # Store settings into settings file.
317 &General::writehash("$IDS::rules_settings_file", \%cgiparams);
318 }
319
320 # Check if the the automatic rule update hass been touched.
321 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldsettings{'AUTOUPDATE_INTERVAL'}) {
322 # Call suricatactrl to set the new interval.
323 &IDS::call_suricatactrl("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
324 }
325
326 # Check if a ruleset is present - if not download it.
327 unless (%idsrules) {
328 # Check if the red device is active.
329 unless (-e "${General::swroot}/red/active") {
330 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
331 }
332
333 # Check if enought free disk space is availabe.
334 if(&IDS::checkdiskspace()) {
335 $errormessage = "$Lang::tr{'not enough disk space'}";
336 }
337
338 # Check if any errors happend.
339 unless ($errormessage) {
340 # Lock the webpage and print notice about downloading
341 # a new ruleset.
342 &working_notice("$Lang::tr{'snort working'}");
343
344 # Call subfunction to download the ruleset.
345 if(&IDS::downloadruleset()) {
346 $errormessage = $Lang::tr{'could not download latest updates'};
347
348 # Call function to store the errormessage.
349 &IDS::_store_error_message($errormessage);
350 } else {
351 # Call subfunction to launch oinkmaster.
352 &IDS::oinkmaster();
353 }
354
355 # Perform a reload of the page.
356 &reload();
357 }
358 }
359
360 # Save ruleset.
361 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
362 # Arrays to store which rulefiles have been enabled and will be used.
363 my @enabled_rulefiles;
364
365 # Hash to store the user-enabled and disabled sids.
366 my %enabled_disabled_sids;
367
368 # Loop through the hash of idsrules.
369 foreach my $rulefile(keys %idsrules) {
370 # Check if the rulefile is enabled.
371 if ($cgiparams{$rulefile} eq "on") {
372 # Add rulefile to the array of enabled rulefiles.
373 push(@enabled_rulefiles, $rulefile);
374
375 # Drop item from cgiparams hash.
376 delete $cgiparams{$rulefile};
377 }
378 }
379
380 # Read-in the files for enabled/disabled sids.
381 # This will be done by calling the read_enabled_disabled_sids_file function two times
382 # and merge the returned hashes together into the enabled_disabled_sids hash.
383 %enabled_disabled_sids = (
384 &read_enabled_disabled_sids_file($IDS::disabled_sids_file),
385 &read_enabled_disabled_sids_file($IDS::enabled_sids_file));
386
387 # Loop through the hash of idsrules.
388 foreach my $rulefile (keys %idsrules) {
389 # Loop through the single rules of the rulefile.
390 foreach my $sid (keys %{$idsrules{$rulefile}}) {
391 # Skip the current sid if it is not numeric.
392 next unless ($sid =~ /\d+/ );
393
394 # Check if there exists a key in the cgiparams hash for this sid.
395 if (exists($cgiparams{$sid})) {
396 # Look if the rule is disabled.
397 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
398 # Check if the state has been set to 'on'.
399 if ($cgiparams{$sid} eq "on") {
400 # Add/Modify the sid to/in the enabled_disabled_sids hash.
401 $enabled_disabled_sids{$sid} = "enabled";
402
403 # Drop item from cgiparams hash.
404 delete $cgiparams{$rulefile}{$sid};
405 }
406 }
407 } else {
408 # Look if the rule is enabled.
409 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
410 # Check if the state is 'on' and should be disabled.
411 # In this case there is no entry
412 # for the sid in the cgiparams hash.
413 # Add/Modify it to/in the enabled_disabled_sids hash.
414 $enabled_disabled_sids{$sid} = "disabled";
415
416 # Drop item from cgiparams hash.
417 delete $cgiparams{$rulefile}{$sid};
418 }
419 }
420 }
421 }
422
423 # Open enabled sid's file for writing.
424 open(ENABLED_FILE, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
425
426 # Open disabled sid's file for writing.
427 open(DISABLED_FILE, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
428
429 # Write header to the files.
430 print ENABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
431 print DISABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
432
433 # Check if the hash for enabled/disabled files contains any entries.
434 if (%enabled_disabled_sids) {
435 # Loop through the hash.
436 foreach my $sid (keys %enabled_disabled_sids) {
437 # Check if the sid is enabled.
438 if ($enabled_disabled_sids{$sid} eq "enabled") {
439 # Print the sid to the enabled_sids file.
440 print ENABLED_FILE "enablesid $sid\n";
441 # Check if the sid is disabled.
442 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
443 # Print the sid to the disabled_sids file.
444 print DISABLED_FILE "disablesid $sid\n";
445 # Something strange happende - skip the current sid.
446 } else {
447 next;
448 }
449 }
450 }
451
452 # Close file for enabled_sids after writing.
453 close(ENABLED_FILE);
454
455 # Close file for disabled_sids after writing.
456 close(DISABLED_FILE);
457
458 # Call function to generate and write the used rulefiles file.
459 &IDS::write_used_rulefiles_file(@enabled_rulefiles);
460
461 # Lock the webpage and print message.
462 &working_notice("$Lang::tr{'snort working'}");
463
464 # Call oinkmaster to alter the ruleset.
465 &IDS::oinkmaster();
466
467 # Check if the IDS is running.
468 if(&IDS::ids_is_running()) {
469 # Call suricatactrl to perform a reload.
470 &IDS::call_suricatactrl("reload");
471 }
472
473 # Reload page.
474 &reload();
475
476 # Download new ruleset.
477 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update ruleset'}) {
478 # Check if the red device is active.
479 unless (-e "${General::swroot}/red/active") {
480 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
481 }
482
483 # Check if enought free disk space is availabe.
484 if(&IDS::checkdiskspace()) {
485 $errormessage = "$Lang::tr{'not enough disk space'}";
486 }
487
488 # Check if any errors happend.
489 unless ($errormessage) {
490 # Lock the webpage and print notice about downloading
491 # a new ruleset.
492 &working_notice("$Lang::tr{'snort working'}");
493
494 # Call subfunction to download the ruleset.
495 if(&IDS::downloadruleset()) {
496 $errormessage = $Lang::tr{'could not download latest updates'};
497
498 # Call function to store the errormessage.
499 &IDS::_store_error_message($errormessage);
500
501 # Preform a reload of the page.
502 &reload();
503 } else {
504 # Call subfunction to launch oinkmaster.
505 &IDS::oinkmaster();
506
507 # Check if the IDS is running.
508 if(&IDS::ids_is_running()) {
509 # Call suricatactrl to perform a reload.
510 &IDS::call_suricatactrl("reload");
511 }
512
513 # Perform a reload of the page.
514 &reload();
515 }
516 }
517 # Save snort settings.
518 } elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
519 my %oldidssettings;
520 my $reload_page;
521 my $monitored_zones = 0;
522
523 # Read-in current (old) IDS settings.
524 &General::readhash("$IDS::ids_settings_file", \%oldidssettings);
525
526 # Prevent form name from been stored in conf file.
527 delete $cgiparams{'IDS'};
528
529 # Check if the IDS should be enabled.
530 if ($cgiparams{'ENABLE_IDS'} eq "on") {
531 # Check if any ruleset is available. Otherwise abort and display an error.
532 unless(%idsrules) {
533 $errormessage = $Lang::tr{'ids no ruleset available'};
534 }
535
536 # Loop through the array of available interfaces.
537 foreach my $zone (@network_zones) {
538 # Convert interface name into upper case.
539 my $zone_upper = uc($zone);
540
541 # Check if the IDS is enabled for this interaces.
542 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
543 # Increase count.
544 $monitored_zones++;
545 }
546 }
547
548 # Check if at least one zone should be monitored, or show an error.
549 unless ($monitored_zones >= 1) {
550 $errormessage = $Lang::tr{'ids no network zone'};
551 }
552 }
553
554 # Go on if there are no error messages.
555 if (!$errormessage) {
556 # Store settings into settings file.
557 &General::writehash("$IDS::ids_settings_file", \%cgiparams);
558 }
559
560 # Generate file to store the home net.
561 &IDS::generate_home_net_file();
562
563 # Temporary variable to set the ruleaction.
564 # Default is "drop" to use suricata as IPS.
565 my $ruleaction="drop";
566
567 # Check if the traffic only should be monitored.
568 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} eq 'on') {
569 # Switch the ruleaction to "alert".
570 # Suricata acts as an IDS only.
571 $ruleaction="alert";
572 }
573
574 # Write the modify sid's file and pass the taken ruleaction.
575 &IDS::write_modify_sids_file($ruleaction);
576
577 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
578 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
579 # Check if a ruleset exists.
580 if (%idsrules) {
581 # Lock the webpage and print message.
582 &working_notice("$Lang::tr{'snort working'}");
583
584 # Call oinkmaster to alter the ruleset.
585 &IDS::oinkmaster();
586
587 # Set reload_page to "True".
588 $reload_page="True";
589 }
590 }
591
592 # Check if the IDS currently is running.
593 if(&IDS::ids_is_running()) {
594 # Check if ENABLE_IDS is set to on.
595 if($cgiparams{'ENABLE_IDS'} eq "on") {
596 # Call suricatactrl to perform a reload of suricata.
597 &IDS::call_suricatactrl("reload");
598 } else {
599 # Call suricatactrl to stop suricata.
600 &IDS::call_suricatactrl("stop");
601 }
602 } else {
603 # Call suricatactrl to start suricata.
604 &IDS::call_suricatactrl("start");
605 }
606
607 # Check if the page should be reloaded.
608 if ($reload_page) {
609 # Perform a reload of the page.
610 &reload();
611 }
612 }
613
614 # Read-in idssettings and rulesetsettings
615 &General::readhash("$IDS::ids_settings_file", \%idssettings);
616 &General::readhash("$IDS::rules_settings_file", \%rulessettings);
617
618 # If no autoupdate intervall has been configured yet, set default value.
619 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
620 # Set default to "weekly".
621 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
622 }
623
624 # Read-in ignored hosts.
625 &General::readhasharray("$IDS::settingsdir/ignored", \%ignored);
626
627 $checked{'ENABLE_IDS'}{'off'} = '';
628 $checked{'ENABLE_IDS'}{'on'} = '';
629 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
630 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
631 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
632 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
633 $selected{'RULES'}{'nothing'} = '';
634 $selected{'RULES'}{'community'} = '';
635 $selected{'RULES'}{'emerging'} = '';
636 $selected{'RULES'}{'registered'} = '';
637 $selected{'RULES'}{'subscripted'} = '';
638 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
639 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
640 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
641 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
642 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
643
644 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
645
646 ### Java Script ###
647 print <<END
648 <script>
649 // JQuery function to show/hide the text input field for
650 // Oinkcode/Subscription code.
651 \$(function() {
652 \$('#RULES').change(function(){
653 if(\$('#RULES').val() == 'registered') {
654 \$('#code').show();
655 } else if(\$('#RULES').val() == 'subscripted') {
656 \$('#code').show();
657 } else if(\$('#RULES').val() == 'emerging_pro') {
658 \$('#code').show();
659 } else {
660 \$('#code').hide();
661 }
662 });
663 });
664
665 // Tiny java script function to show/hide the rules
666 // of a given category.
667 function showhide(tblname) {
668 \$("#" + tblname).toggle();
669 }
670 </script>
671 END
672 ;
673
674 &Header::openbigbox('100%', 'left', '', $errormessage);
675
676 if ($errormessage) {
677 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
678 print "<class name='base'>$errormessage\n";
679 print "&nbsp;</class>\n";
680 &Header::closebox();
681 }
682
683 # Draw current state of the IDS
684 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
685
686 # Check if the IDS is running and obtain the process-id.
687 my $pid = &IDS::ids_is_running();
688
689 # Display some useful information, if suricata daemon is running.
690 if ($pid) {
691 # Gather used memory.
692 my $memory = &get_memory_usage($pid);
693
694 print <<END;
695 <table width='95%' cellspacing='0' class='tbl'>
696 <tr>
697 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
698 </tr>
699
700 <tr>
701 <td class='base'>$Lang::tr{'guardian daemon'}</td>
702 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
703 </tr>
704
705 <tr>
706 <td class='base'></td>
707 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
708 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
709 </tr>
710
711 <tr>
712 <td class='base'></td>
713 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
714 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
715 </tr>
716 </table>
717 END
718 } else {
719 # Otherwise display a hint that the service is not launched.
720 print <<END;
721 <table width='95%' cellspacing='0' class='tbl'>
722 <tr>
723 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
724 </tr>
725
726 <tr>
727 <td class='base'>$Lang::tr{'guardian daemon'}</td>
728 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
729 </tr>
730 </table>
731 END
732 }
733
734 my $rulesdate;
735
736 # Check if a ruleset allready has been downloaded.
737 if ( -f "$IDS::rulestarball"){
738 # Call stat on the filename to obtain detailed information.
739 my @Info = stat("$IDS::rulestarball");
740
741 # Grab details about the creation time.
742 $rulesdate = localtime($Info[9]);
743 }
744
745 # Only show this area, if a ruleset is present.
746 if (%idsrules) {
747
748 print <<END
749
750 <br><br><h2>$Lang::tr{'settings'}</h2>
751
752 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
753 <table width='100%' border='0'>
754 <tr>
755 <td class='base' colspan='2'>
756 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
757 </td>
758
759 <td class='base' colspan='2'>
760 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>$Lang::tr{'ids monitor traffic only'}
761 </td>
762 </tr>
763
764 <tr>
765 <td><br><br></td>
766 <td><br><br></td>
767 <td><br><br></td>
768 <td><br><br></td>
769 </tr>
770
771 <tr>
772 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
773 </tr>
774
775 <tr>
776 END
777 ;
778
779 # Loop through the array of available networks and print config options.
780 foreach my $zone (@network_zones) {
781 my $checked_input;
782 my $checked_forward;
783
784 # Convert current zone name to upper case.
785 my $zone_upper = uc($zone);
786
787 # Set zone name.
788 my $zone_name = $zone;
789
790 # Dirty hack to get the correct language string for the red zone.
791 if ($zone eq "red") {
792 $zone_name = "red1";
793 }
794
795 # Grab checkbox status from settings hash.
796 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
797 $checked_input = "checked = 'checked'";
798 }
799
800 print "<td class='base' width='25%'>\n";
801 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
802 print "&nbsp$Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
803 print "</td>\n";
804 }
805
806 print <<END
807 </tr>
808 </table>
809
810 <br><br>
811
812 <table width='100%'>
813 <tr>
814 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
815 </tr>
816 </table>
817 </form>
818 END
819 ;
820
821 }
822
823 &Header::closebox();
824
825 # Draw elements for ruleset configuration.
826 &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'});
827
828 print <<END
829 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
830 <table width='100%' border='0'>
831 <tr>
832 <td><b>$Lang::tr{'ids rules update'}</b></td>
833 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
834 </tr>
835
836 <tr>
837 <td><select name='RULES' id='RULES'>
838 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
839 <option value='emerging_pro' $selected{'RULES'}{'emerging_pro'} >$Lang::tr{'emerging pro rules'}</option>
840 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
841 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
842 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
843 </select>
844 </td>
845
846 <td>
847 <select name='AUTOUPDATE_INTERVAL'>
848 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option>
849 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option>
850 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option>
851 </select>
852 </td>
853 </tr>
854
855 <tr>
856 <td colspan='2'><br><br></td>
857 </tr>
858
859 <tr style='display:none' id='code'>
860 <td colspan='2'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
861 </tr>
862
863 <tr>
864 <td>&nbsp;</td>
865
866 <td align='right'>
867 END
868 ;
869 # Check if a ruleset has been downloaded yet.
870 if (%idsrules) {
871 # Display button to update the ruleset.
872 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
873 }
874 print <<END;
875 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
876 </td>
877
878 </tr>
879 </table>
880 </form>
881 END
882 ;
883
884 &Header::closebox();
885
886 #
887 # Whitelist / Ignorelist
888 #
889 &Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'});
890
891 print <<END;
892 <table width='100%'>
893 <tr>
894 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
895 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
896 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
897 </tr>
898 END
899 # Check if some hosts have been added to be ignored.
900 if (keys (%ignored)) {
901 my $col = "";
902
903 # Loop through all entries of the hash.
904 while( (my $key) = each %ignored) {
905 # Assign data array positions to some nice variable names.
906 my $address = $ignored{$key}[0];
907 my $remark = $ignored{$key}[1];
908 my $status = $ignored{$key}[2];
909
910 # Check if the key (id) number is even or not.
911 if ($cgiparams{'ID'} eq $key) {
912 $col="bgcolor='${Header::colouryellow}'";
913 } elsif ($key % 2) {
914 $col="bgcolor='$color{'color22'}'";
915 } else {
916 $col="bgcolor='$color{'color20'}'";
917 }
918
919 # Choose icon for the checkbox.
920 my $gif;
921 my $gdesc;
922
923 # Check if the status is enabled and select the correct image and description.
924 if ($status eq 'enabled' ) {
925 $gif = 'on.gif';
926 $gdesc = $Lang::tr{'click to disable'};
927 } else {
928 $gif = 'off.gif';
929 $gdesc = $Lang::tr{'click to enable'};
930 }
931
932 print <<END;
933 <tr>
934 <td width='20%' class='base' $col>$address</td>
935 <td width='65%' class='base' $col>$remark</td>
936
937 <td align='center' $col>
938 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
939 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
940 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
941 <input type='hidden' name='ID' value='$key' />
942 </form>
943 </td>
944
945 <td align='center' $col>
946 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
947 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
948 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
949 <input type='hidden' name='ID' value='$key' />
950 </form>
951 </td>
952
953 <td align='center' $col>
954 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
955 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
956 <input type='hidden' name='ID' value='$key'>
957 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
958 </form>
959 </td>
960 </tr>
961 END
962 }
963 } else {
964 # Print notice that currently no hosts are ignored.
965 print "<tr>\n";
966 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
967 print "</tr>\n";
968 }
969
970 print "</table>\n";
971
972 # Section to add new elements or edit existing ones.
973 print <<END;
974 <br>
975 <hr>
976 <br>
977
978 <div align='center'>
979 <table width='100%'>
980 END
981
982 # Assign correct headline and button text.
983 my $buttontext;
984 my $entry_address;
985 my $entry_remark;
986
987 # Check if an ID (key) has been given, in this case an existing entry should be edited.
988 if ($cgiparams{'ID'} ne '') {
989 $buttontext = $Lang::tr{'update'};
990 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
991
992 # Grab address and remark for the given key.
993 $entry_address = $ignored{$cgiparams{'ID'}}[0];
994 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
995 } else {
996 $buttontext = $Lang::tr{'add'};
997 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
998 }
999
1000 print <<END;
1001 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1002 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1003 <tr>
1004 <td width='30%'>$Lang::tr{'ip address'}: </td>
1005 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1006
1007 <td width='30%'>$Lang::tr{'remark'}: </td>
1008 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1009 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1010 </tr>
1011 </form>
1012 </table>
1013 </div>
1014 END
1015
1016 &Header::closebox();
1017
1018 # Only show the section for configuring the ruleset if one is present.
1019 if (%idsrules) {
1020 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
1021
1022 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1023
1024 # Output display table for rule files
1025 print "<table width='100%'>\n";
1026
1027 # Loop over each rule file
1028 foreach my $rulefile (sort keys(%idsrules)) {
1029 my $rulechecked = '';
1030
1031 # Check if rule file is enabled
1032 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1033 $rulechecked = 'CHECKED';
1034 }
1035
1036 # Convert rulefile name into category name.
1037 my $categoryname = &_rulefile_to_category($rulefile);
1038
1039 # Table and rows for the rule files.
1040 print"<tr>\n";
1041 print"<td class='base' width='5%'>\n";
1042 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1043 print"</td>\n";
1044 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1045 print"<td class='base' width='5%' align='right'>\n";
1046 print"<a href=\"javascript:showhide('$categoryname')\">SHOW</a>\n";
1047 print"</td>\n";
1048 print"</tr>\n";
1049
1050 # Rows which will be hidden per default and will contain the single rules.
1051 print"<tr style='display:none' id='$categoryname'>\n";
1052 print"<td colspan='3'>\n";
1053
1054 # Local vars
1055 my $lines;
1056 my $rows;
1057 my $col;
1058
1059 # New table for the single rules.
1060 print "<table width='100%'>\n";
1061
1062 # Loop over rule file rules
1063 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1064 # Local vars
1065 my $ruledefchecked = '';
1066
1067 # Skip rulefile itself.
1068 next if ($sid eq "Rulefile");
1069
1070 # If 2 rules have been displayed, start a new row
1071 if (($lines % 2) == 0) {
1072 print "</tr><tr>\n";
1073
1074 # Increase rows by once.
1075 $rows++;
1076 }
1077
1078 # Colour lines.
1079 if ($rows % 2) {
1080 $col="bgcolor='$color{'color20'}'";
1081 } else {
1082 $col="bgcolor='$color{'color22'}'";
1083 }
1084
1085 # Set rule state
1086 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1087 $ruledefchecked = 'CHECKED';
1088 }
1089
1090 # Create rule checkbox and display rule description
1091 print "<td class='base' width='5%' align='right' $col>\n";
1092 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1093 print "</td>\n";
1094 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1095
1096 # Increment rule count
1097 $lines++;
1098 }
1099
1100 # If do not have a second rule for row, create empty cell
1101 if (($lines % 2) != 0) {
1102 print "<td class='base'></td>";
1103 }
1104
1105 # Close display table
1106 print "</tr></table></td></tr>";
1107 }
1108
1109 # Close display table
1110 print "</table>";
1111
1112 print <<END
1113 <table width='100%'>
1114 <tr>
1115 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
1116 &nbsp; <!-- space for future online help link -->
1117 </td>
1118 </tr>
1119 </table>
1120 </form>
1121 END
1122 ;
1123 &Header::closebox();
1124 }
1125
1126 &Header::closebigbox();
1127 &Header::closepage();
1128
1129 #
1130 ## A function to display a notice, to lock the webpage and
1131 ## tell the user which action currently will be performed.
1132 #
1133 sub working_notice ($) {
1134 my ($message) = @_;
1135
1136 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
1137 &Header::openbigbox('100%', 'left', '', $errormessage);
1138 &Header::openbox( 'Waiting', 1,);
1139 print <<END;
1140 <table>
1141 <tr>
1142 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1143 <td>$message</td>
1144 </tr>
1145 </table>
1146 END
1147 &Header::closebox();
1148 &Header::closebigbox();
1149 &Header::closepage();
1150 }
1151
1152 #
1153 ## A tiny function to perform a reload of the webpage after one second.
1154 #
1155 sub reload () {
1156 print "<meta http-equiv='refresh' content='1'>\n";
1157
1158 # Stop the script.
1159 exit;
1160 }
1161
1162 #
1163 ## Private function to read-in and parse rules of a given rulefile.
1164 #
1165 ## The given file will be read, parsed and all valid rules will be stored by ID,
1166 ## message/description and it's state in the idsrules hash.
1167 #
1168 sub readrulesfile ($) {
1169 my $rulefile = shift;
1170
1171 # Open rule file and read in contents
1172 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1173
1174 # Store file content in an array.
1175 my @lines = <RULEFILE>;
1176
1177 # Close file.
1178 close(RULEFILE);
1179
1180 # Loop over rule file contents
1181 foreach my $line (@lines) {
1182 # Remove whitespaces.
1183 chomp $line;
1184
1185 # Skip blank lines.
1186 next if ($line =~ /^\s*$/);
1187
1188 # Local vars.
1189 my $sid;
1190 my $msg;
1191
1192 # Gather rule sid and message from the ruleline.
1193 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1194 $msg = $1;
1195 $sid = $2;
1196
1197 # Check if a rule has been found.
1198 if ($sid && $msg) {
1199 # Add rule to the idsrules hash.
1200 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1201
1202 # Grab status of the rule. Check if ruleline starts with a "dash".
1203 if ($line =~ /^\#/) {
1204 # If yes, the rule is disabled.
1205 $idsrules{$rulefile}{$sid}{'State'} = "off";
1206 } else {
1207 # Otherwise the rule is enabled.
1208 $idsrules{$rulefile}{$sid}{'State'} = "on";
1209 }
1210 }
1211 }
1212 }
1213 }
1214
1215 #
1216 ## Function to get the used memory of a given process-id.
1217 #
1218 sub get_memory_usage($) {
1219 my ($pid) = @_;
1220
1221 my $memory = 0;
1222
1223 # Try to open the status file for the given process-id on the pseudo
1224 # file system proc.
1225 if (open(FILE, "/proc/$pid/status")) {
1226 # Loop through the entire file.
1227 while (<FILE>) {
1228 # Splitt current line content and store them into variables.
1229 my ($key, $value) = split(":", $_, 2);
1230
1231 # Check if the current key is the one which contains the memory usage.
1232 # The wanted one is VmRSS which contains the Real-memory (resident set)
1233 # of the entire process.
1234 if ($key eq "VmRSS") {
1235 # Found the memory usage add it to the memory variable.
1236 $memory += $value;
1237
1238 # Break the loop.
1239 last;
1240 }
1241 }
1242
1243 # Close file handle.
1244 close(FILE);
1245
1246 # Return memory usage.
1247 return $memory;
1248 }
1249
1250 # If the file could not be open, return nothing.
1251 return;
1252 }
1253
1254 #
1255 ## Function to read-in the given enabled or disables sids file.
1256 #
1257 sub read_enabled_disabled_sids_file($) {
1258 my ($file) = @_;
1259
1260 # Temporary hash to store the sids and their state. It will be
1261 # returned at the end of this function.
1262 my %temphash;
1263
1264 # Open the given filename.
1265 open(FILE, "$file") or die "Could not open $file. $!\n";
1266
1267 # Loop through the file.
1268 while(<FILE>) {
1269 # Remove newlines.
1270 chomp $_;
1271
1272 # Skip blank lines.
1273 next if ($_ =~ /^\s*$/);
1274
1275 # Skip coments.
1276 next if ($_ =~ /^\#/);
1277
1278 # Splitt line into sid and state part.
1279 my ($state, $sid) = split(" ", $_);
1280
1281 # Skip line if the sid is not numeric.
1282 next unless ($sid =~ /\d+/ );
1283
1284 # Check if the sid was enabled.
1285 if ($state eq "enablesid") {
1286 # Add the sid and its state as enabled to the temporary hash.
1287 $temphash{$sid} = "enabled";
1288 # Check if the sid was disabled.
1289 } elsif ($state eq "disablesid") {
1290 # Add the sid and its state as disabled to the temporary hash.
1291 $temphash{$sid} = "disabled";
1292 # Invalid state - skip the current sid and state.
1293 } else {
1294 next;
1295 }
1296 }
1297
1298 # Close filehandle.
1299 close(FILE);
1300
1301 # Return the hash.
1302 return %temphash;
1303 }
1304
1305 #
1306 ## Private function to convert a given rulefile to a category name.
1307 ## ( No file extension anymore and if the name contained a dot, it
1308 ## would be replaced by a underline sign.)
1309 #
1310 sub _rulefile_to_category($) {
1311 my ($filename) = @_;
1312
1313 # Splitt the filename into single chunks and store them in a
1314 # temorary array.
1315 my @parts = split(/\./, $filename);
1316
1317 # Return / Remove last element of the temporary array.
1318 # This removes the file extension.
1319 pop @parts;
1320
1321 # Join together the single elements of the temporary array.
1322 # If these are more than one, use a "underline" for joining.
1323 my $category = join '_', @parts;
1324
1325 # Return the converted filename.
1326 return $category;
1327 }