]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Draw daemon status and setting in the same box.
[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 # Save ruleset.
327 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
328 # Arrays to store which rulefiles have been enabled and will be used.
329 my @enabled_rulefiles;
330
331 # Hash to store the user-enabled and disabled sids.
332 my %enabled_disabled_sids;
333
334 # Loop through the hash of idsrules.
335 foreach my $rulefile(keys %idsrules) {
336 # Check if the rulefile is enabled.
337 if ($cgiparams{$rulefile} eq "on") {
338 # Add rulefile to the array of enabled rulefiles.
339 push(@enabled_rulefiles, $rulefile);
340
341 # Drop item from cgiparams hash.
342 delete $cgiparams{$rulefile};
343 }
344 }
345
346 # Read-in the files for enabled/disabled sids.
347 # This will be done by calling the read_enabled_disabled_sids_file function two times
348 # and merge the returned hashes together into the enabled_disabled_sids hash.
349 %enabled_disabled_sids = (
350 &read_enabled_disabled_sids_file($IDS::disabled_sids_file),
351 &read_enabled_disabled_sids_file($IDS::enabled_sids_file));
352
353 # Loop through the hash of idsrules.
354 foreach my $rulefile (keys %idsrules) {
355 # Loop through the single rules of the rulefile.
356 foreach my $sid (keys %{$idsrules{$rulefile}}) {
357 # Skip the current sid if it is not numeric.
358 next unless ($sid =~ /\d+/ );
359
360 # Check if there exists a key in the cgiparams hash for this sid.
361 if (exists($cgiparams{$sid})) {
362 # Look if the rule is disabled.
363 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
364 # Check if the state has been set to 'on'.
365 if ($cgiparams{$sid} eq "on") {
366 # Add/Modify the sid to/in the enabled_disabled_sids hash.
367 $enabled_disabled_sids{$sid} = "enabled";
368
369 # Drop item from cgiparams hash.
370 delete $cgiparams{$rulefile}{$sid};
371 }
372 }
373 } else {
374 # Look if the rule is enabled.
375 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
376 # Check if the state is 'on' and should be disabled.
377 # In this case there is no entry
378 # for the sid in the cgiparams hash.
379 # Add/Modify it to/in the enabled_disabled_sids hash.
380 $enabled_disabled_sids{$sid} = "disabled";
381
382 # Drop item from cgiparams hash.
383 delete $cgiparams{$rulefile}{$sid};
384 }
385 }
386 }
387 }
388
389 # Open enabled sid's file for writing.
390 open(ENABLED_FILE, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
391
392 # Open disabled sid's file for writing.
393 open(DISABLED_FILE, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
394
395 # Write header to the files.
396 print ENABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
397 print DISABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
398
399 # Check if the hash for enabled/disabled files contains any entries.
400 if (%enabled_disabled_sids) {
401 # Loop through the hash.
402 foreach my $sid (keys %enabled_disabled_sids) {
403 # Check if the sid is enabled.
404 if ($enabled_disabled_sids{$sid} eq "enabled") {
405 # Print the sid to the enabled_sids file.
406 print ENABLED_FILE "enablesid $sid\n";
407 # Check if the sid is disabled.
408 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
409 # Print the sid to the disabled_sids file.
410 print DISABLED_FILE "disablesid $sid\n";
411 # Something strange happende - skip the current sid.
412 } else {
413 next;
414 }
415 }
416 }
417
418 # Close file for enabled_sids after writing.
419 close(ENABLED_FILE);
420
421 # Close file for disabled_sids after writing.
422 close(DISABLED_FILE);
423
424 # Call function to generate and write the used rulefiles file.
425 &IDS::write_used_rulefiles_file(@enabled_rulefiles);
426
427 # Lock the webpage and print message.
428 &working_notice("$Lang::tr{'snort working'}");
429
430 # Call oinkmaster to alter the ruleset.
431 &IDS::oinkmaster();
432
433 # Check if the IDS is running.
434 if(&IDS::ids_is_running()) {
435 # Call suricatactrl to perform a reload.
436 &IDS::call_suricatactrl("reload");
437 }
438
439 # Reload page.
440 &reload();
441
442 # Download new ruleset.
443 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
444 # Check if the red device is active.
445 unless (-e "${General::swroot}/red/active") {
446 $errormessage = $Lang::tr{'could not download latest updates'};
447 }
448
449 # Check if enought free disk space is availabe.
450 if(&IDS::checkdiskspace()) {
451 $errormessage = "$Lang::tr{'not enough disk space'}";
452 }
453
454 # Check if any errors happend.
455 unless ($errormessage) {
456 # Lock the webpage and print notice about downloading
457 # a new ruleset.
458 &working_notice("$Lang::tr{'snort working'}");
459
460 # Call subfunction to download the ruleset.
461 if(&IDS::downloadruleset()) {
462 $errormessage = $Lang::tr{'could not download latest updates'};
463
464 # Call function to store the errormessage.
465 &IDS::_store_error_message($errormessage);
466
467 # Preform a reload of the page.
468 &reload();
469 } else {
470 # Call subfunction to launch oinkmaster.
471 &IDS::oinkmaster();
472
473 # Check if the IDS is running.
474 if(&IDS::ids_is_running()) {
475 # Call suricatactrl to perform a reload.
476 &IDS::call_suricatactrl("reload");
477 }
478
479 # Perform a reload of the page.
480 &reload();
481 }
482 }
483 # Save snort settings.
484 } elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
485 my %oldidssettings;
486 my $reload_page;
487 my $monitored_zones = 0;
488
489 # Read-in current (old) IDS settings.
490 &General::readhash("$IDS::ids_settings_file", \%oldidssettings);
491
492 # Prevent form name from been stored in conf file.
493 delete $cgiparams{'IDS'};
494
495 # Check if the IDS should be enabled.
496 if ($cgiparams{'ENABLE_IDS'} eq "on") {
497 # Check if any ruleset is available. Otherwise abort and display an error.
498 unless(%idsrules) {
499 $errormessage = $Lang::tr{'ids no ruleset available'};
500 }
501
502 # Loop through the array of available interfaces.
503 foreach my $zone (@network_zones) {
504 # Convert interface name into upper case.
505 my $zone_upper = uc($zone);
506
507 # Check if the IDS is enabled for this interaces.
508 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
509 # Increase count.
510 $monitored_zones++;
511 }
512 }
513
514 # Check if at least one zone should be monitored, or show an error.
515 unless ($monitored_zones >= 1) {
516 $errormessage = $Lang::tr{'ids no network zone'};
517 }
518 }
519
520 # Go on if there are no error messages.
521 if (!$errormessage) {
522 # Store settings into settings file.
523 &General::writehash("$IDS::ids_settings_file", \%cgiparams);
524 }
525
526 # Generate file to store the home net.
527 &IDS::generate_home_net_file();
528
529 # Temporary variable to set the ruleaction.
530 # Default is "drop" to use suricata as IPS.
531 my $ruleaction="drop";
532
533 # Check if the traffic only should be monitored.
534 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} eq 'on') {
535 # Switch the ruleaction to "alert".
536 # Suricata acts as an IDS only.
537 $ruleaction="alert";
538 }
539
540 # Write the modify sid's file and pass the taken ruleaction.
541 &IDS::write_modify_sids_file($ruleaction);
542
543 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
544 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
545 # Check if a ruleset exists.
546 if (%idsrules) {
547 # Lock the webpage and print message.
548 &working_notice("$Lang::tr{'snort working'}");
549
550 # Call oinkmaster to alter the ruleset.
551 &IDS::oinkmaster();
552
553 # Set reload_page to "True".
554 $reload_page="True";
555 }
556 }
557
558 # Check if the IDS currently is running.
559 if(&IDS::ids_is_running()) {
560 # Check if ENABLE_IDS is set to on.
561 if($cgiparams{'ENABLE_IDS'} eq "on") {
562 # Call suricatactrl to perform a reload of suricata.
563 &IDS::call_suricatactrl("reload");
564 } else {
565 # Call suricatactrl to stop suricata.
566 &IDS::call_suricatactrl("stop");
567 }
568 } else {
569 # Call suricatactrl to start suricata.
570 &IDS::call_suricatactrl("start");
571 }
572
573 # Check if the page should be reloaded.
574 if ($reload_page) {
575 # Perform a reload of the page.
576 &reload();
577 }
578 }
579
580 # Read-in idssettings and rulesetsettings
581 &General::readhash("$IDS::ids_settings_file", \%idssettings);
582 &General::readhash("$IDS::rules_settings_file", \%rulessettings);
583
584 # If no autoupdate intervall has been configured yet, set default value.
585 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
586 # Set default to "weekly".
587 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
588 }
589
590 # Read-in ignored hosts.
591 &General::readhasharray("$IDS::settingsdir/ignored", \%ignored);
592
593 $checked{'ENABLE_IDS'}{'off'} = '';
594 $checked{'ENABLE_IDS'}{'on'} = '';
595 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
596 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
597 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
598 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
599 $selected{'RULES'}{'nothing'} = '';
600 $selected{'RULES'}{'community'} = '';
601 $selected{'RULES'}{'emerging'} = '';
602 $selected{'RULES'}{'registered'} = '';
603 $selected{'RULES'}{'subscripted'} = '';
604 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
605 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
606 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
607 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
608 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
609
610 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
611
612 ### Java Script ###
613 print <<END
614 <script>
615 // JQuery function to show/hide the text input field for
616 // Oinkcode/Subscription code.
617 \$(function() {
618 \$('#RULES').change(function(){
619 if(\$('#RULES').val() == 'registered') {
620 \$('#code').show();
621 } else if(\$('#RULES').val() == 'subscripted') {
622 \$('#code').show();
623 } else if(\$('#RULES').val() == 'emerging_pro') {
624 \$('#code').show();
625 } else {
626 \$('#code').hide();
627 }
628 });
629 });
630
631 // Tiny java script function to show/hide the rules
632 // of a given category.
633 function showhide(tblname) {
634 \$("#" + tblname).toggle();
635 }
636 </script>
637 END
638 ;
639
640 &Header::openbigbox('100%', 'left', '', $errormessage);
641
642 if ($errormessage) {
643 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
644 print "<class name='base'>$errormessage\n";
645 print "&nbsp;</class>\n";
646 &Header::closebox();
647 }
648
649 # Draw current state of the IDS
650 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
651
652 # Check if the IDS is running and obtain the process-id.
653 my $pid = &IDS::ids_is_running();
654
655 # Display some useful information, if suricata daemon is running.
656 if ($pid) {
657 # Gather used memory.
658 my $memory = &get_memory_usage($pid);
659
660 print <<END;
661 <table width='95%' cellspacing='0' class='tbl'>
662 <tr>
663 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
664 </tr>
665
666 <tr>
667 <td class='base'>$Lang::tr{'guardian daemon'}</td>
668 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
669 </tr>
670
671 <tr>
672 <td class='base'></td>
673 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
674 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
675 </tr>
676
677 <tr>
678 <td class='base'></td>
679 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
680 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
681 </tr>
682 </table>
683 END
684 } else {
685 # Otherwise display a hint that the service is not launched.
686 print <<END;
687 <table width='95%' cellspacing='0' class='tbl'>
688 <tr>
689 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
690 </tr>
691
692 <tr>
693 <td class='base'>$Lang::tr{'guardian daemon'}</td>
694 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
695 </tr>
696 </table>
697 END
698 }
699
700 my $rulesdate;
701
702 # Check if a ruleset allready has been downloaded.
703 if ( -f "$IDS::rulestarball"){
704 # Call stat on the filename to obtain detailed information.
705 my @Info = stat("$IDS::rulestarball");
706
707 # Grab details about the creation time.
708 $rulesdate = localtime($Info[9]);
709 }
710
711 print <<END
712
713 <br><br><h2>$Lang::tr{'settings'}</h2>
714
715 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
716 <table width='100%' border='0'>
717 <tr>
718 <td class='base' colspan='2'>
719 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
720 </td>
721
722 <td class='base' colspan='2'>
723 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>$Lang::tr{'ids monitor traffic only'}
724 </td>
725 </tr>
726
727 <tr>
728 <td><br><br></td>
729 <td><br><br></td>
730 <td><br><br></td>
731 <td><br><br></td>
732 </tr>
733
734 <tr>
735 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
736 </tr>
737
738 <tr>
739 END
740 ;
741
742 # Loop through the array of available networks and print config options.
743 foreach my $zone (@network_zones) {
744 my $checked_input;
745 my $checked_forward;
746
747 # Convert current zone name to upper case.
748 my $zone_upper = uc($zone);
749
750 # Set zone name.
751 my $zone_name = $zone;
752
753 # Dirty hack to get the correct language string for the red zone.
754 if ($zone eq "red") {
755 $zone_name = "red1";
756 }
757
758 # Grab checkbox status from settings hash.
759 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
760 $checked_input = "checked = 'checked'";
761 }
762
763 print "<td class='base' width='25%'>\n";
764 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
765 print "&nbsp$Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
766 print "</td>\n";
767 }
768
769 print <<END
770 </tr>
771 </table>
772
773 <br><br>
774
775 <table width='100%'>
776 <tr>
777 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
778 </tr>
779 </table>
780 </form>
781 END
782 ;
783
784 &Header::closebox();
785
786 # Draw elements for ruleset configuration.
787 &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'});
788
789 print <<END
790 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
791 <table width='100%' border='0'>
792 <tr>
793 <td><b>$Lang::tr{'ids rules update'}</b></td>
794 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
795 </tr>
796
797 <tr>
798 <td><select name='RULES' id='RULES'>
799 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
800 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
801 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
802 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
803 </select>
804 </td>
805
806 <td>
807 <select name='AUTOUPDATE_INTERVAL'>
808 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option>
809 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option>
810 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option>
811 </select>
812 </td>
813 </tr>
814
815 <tr>
816 <td colspan='2'><br><br></td>
817 </tr>
818
819 <tr style='display:none' id='code'>
820 <td colspan='2'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
821 </tr>
822
823 <tr>
824 <td>&nbsp;</td>
825
826 <td align='right'>
827 END
828 ;
829 # Check if a ruleset source has been configured yet.
830 unless($rulessettings{'RULES'}) {
831 # If no ruleset settings have been saved yet, disable the button to download / update the ruleset.
832 print"<input type='submit' name='RULESET' disabled='disabled' value='$Lang::tr{'download new ruleset'}'>\n";
833 } else {
834 # Ruleset setting have been saved. - Check if a ruleset already is downloaded.
835 if (%idsrules) {
836 # Allow to press the button and show it as "update ruleset".
837 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
838 } else {
839 # Also allow to press the button, but show it as "download new ruleset".
840 print"<input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>\n";
841 }
842 }
843 print <<END;
844 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
845 </td>
846
847 </tr>
848 </table>
849 </form>
850 END
851 ;
852
853 &Header::closebox();
854
855 #
856 # Whitelist / Ignorelist
857 #
858 &Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'});
859
860 print <<END;
861 <table width='100%'>
862 <tr>
863 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
864 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
865 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
866 </tr>
867 END
868 # Check if some hosts have been added to be ignored.
869 if (keys (%ignored)) {
870 my $col = "";
871
872 # Loop through all entries of the hash.
873 while( (my $key) = each %ignored) {
874 # Assign data array positions to some nice variable names.
875 my $address = $ignored{$key}[0];
876 my $remark = $ignored{$key}[1];
877 my $status = $ignored{$key}[2];
878
879 # Check if the key (id) number is even or not.
880 if ($cgiparams{'ID'} eq $key) {
881 $col="bgcolor='${Header::colouryellow}'";
882 } elsif ($key % 2) {
883 $col="bgcolor='$color{'color22'}'";
884 } else {
885 $col="bgcolor='$color{'color20'}'";
886 }
887
888 # Choose icon for the checkbox.
889 my $gif;
890 my $gdesc;
891
892 # Check if the status is enabled and select the correct image and description.
893 if ($status eq 'enabled' ) {
894 $gif = 'on.gif';
895 $gdesc = $Lang::tr{'click to disable'};
896 } else {
897 $gif = 'off.gif';
898 $gdesc = $Lang::tr{'click to enable'};
899 }
900
901 print <<END;
902 <tr>
903 <td width='20%' class='base' $col>$address</td>
904 <td width='65%' class='base' $col>$remark</td>
905
906 <td align='center' $col>
907 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
908 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
909 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
910 <input type='hidden' name='ID' value='$key' />
911 </form>
912 </td>
913
914 <td align='center' $col>
915 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
916 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
917 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
918 <input type='hidden' name='ID' value='$key' />
919 </form>
920 </td>
921
922 <td align='center' $col>
923 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
924 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
925 <input type='hidden' name='ID' value='$key'>
926 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
927 </form>
928 </td>
929 </tr>
930 END
931 }
932 } else {
933 # Print notice that currently no hosts are ignored.
934 print "<tr>\n";
935 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
936 print "</tr>\n";
937 }
938
939 print "</table>\n";
940
941 # Section to add new elements or edit existing ones.
942 print <<END;
943 <br>
944 <hr>
945 <br>
946
947 <div align='center'>
948 <table width='100%'>
949 END
950
951 # Assign correct headline and button text.
952 my $buttontext;
953 my $entry_address;
954 my $entry_remark;
955
956 # Check if an ID (key) has been given, in this case an existing entry should be edited.
957 if ($cgiparams{'ID'} ne '') {
958 $buttontext = $Lang::tr{'update'};
959 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
960
961 # Grab address and remark for the given key.
962 $entry_address = $ignored{$cgiparams{'ID'}}[0];
963 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
964 } else {
965 $buttontext = $Lang::tr{'add'};
966 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
967 }
968
969 print <<END;
970 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
971 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
972 <tr>
973 <td width='30%'>$Lang::tr{'ip address'}: </td>
974 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
975
976 <td width='30%'>$Lang::tr{'remark'}: </td>
977 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
978 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
979 </tr>
980 </form>
981 </table>
982 </div>
983 END
984
985 &Header::closebox();
986
987 # Only show the section for configuring the ruleset if one is present.
988 if (%idsrules) {
989 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
990
991 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
992
993 # Output display table for rule files
994 print "<table width='100%'>\n";
995
996 # Loop over each rule file
997 foreach my $rulefile (sort keys(%idsrules)) {
998 my $rulechecked = '';
999
1000 # Check if rule file is enabled
1001 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1002 $rulechecked = 'CHECKED';
1003 }
1004
1005 # Convert rulefile name into category name.
1006 my $categoryname = &_rulefile_to_category($rulefile);
1007
1008 # Table and rows for the rule files.
1009 print"<tr>\n";
1010 print"<td class='base' width='5%'>\n";
1011 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1012 print"</td>\n";
1013 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1014 print"<td class='base' width='5%' align='right'>\n";
1015 print"<a href=\"javascript:showhide('$categoryname')\">SHOW</a>\n";
1016 print"</td>\n";
1017 print"</tr>\n";
1018
1019 # Rows which will be hidden per default and will contain the single rules.
1020 print"<tr style='display:none' id='$categoryname'>\n";
1021 print"<td colspan='3'>\n";
1022
1023 # Local vars
1024 my $lines;
1025 my $rows;
1026 my $col;
1027
1028 # New table for the single rules.
1029 print "<table width='100%'>\n";
1030
1031 # Loop over rule file rules
1032 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1033 # Local vars
1034 my $ruledefchecked = '';
1035
1036 # Skip rulefile itself.
1037 next if ($sid eq "Rulefile");
1038
1039 # If 2 rules have been displayed, start a new row
1040 if (($lines % 2) == 0) {
1041 print "</tr><tr>\n";
1042
1043 # Increase rows by once.
1044 $rows++;
1045 }
1046
1047 # Colour lines.
1048 if ($rows % 2) {
1049 $col="bgcolor='$color{'color20'}'";
1050 } else {
1051 $col="bgcolor='$color{'color22'}'";
1052 }
1053
1054 # Set rule state
1055 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1056 $ruledefchecked = 'CHECKED';
1057 }
1058
1059 # Create rule checkbox and display rule description
1060 print "<td class='base' width='5%' align='right' $col>\n";
1061 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1062 print "</td>\n";
1063 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1064
1065 # Increment rule count
1066 $lines++;
1067 }
1068
1069 # If do not have a second rule for row, create empty cell
1070 if (($lines % 2) != 0) {
1071 print "<td class='base'></td>";
1072 }
1073
1074 # Close display table
1075 print "</tr></table></td></tr>";
1076 }
1077
1078 # Close display table
1079 print "</table>";
1080
1081 print <<END
1082 <table width='100%'>
1083 <tr>
1084 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
1085 &nbsp; <!-- space for future online help link -->
1086 </td>
1087 </tr>
1088 </table>
1089 </form>
1090 END
1091 ;
1092 &Header::closebox();
1093 }
1094
1095 &Header::closebigbox();
1096 &Header::closepage();
1097
1098 #
1099 ## A function to display a notice, to lock the webpage and
1100 ## tell the user which action currently will be performed.
1101 #
1102 sub working_notice ($) {
1103 my ($message) = @_;
1104
1105 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
1106 &Header::openbigbox('100%', 'left', '', $errormessage);
1107 &Header::openbox( 'Waiting', 1,);
1108 print <<END;
1109 <table>
1110 <tr>
1111 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1112 <td>$message</td>
1113 </tr>
1114 </table>
1115 END
1116 &Header::closebox();
1117 &Header::closebigbox();
1118 &Header::closepage();
1119 }
1120
1121 #
1122 ## A tiny function to perform a reload of the webpage after one second.
1123 #
1124 sub reload () {
1125 print "<meta http-equiv='refresh' content='1'>\n";
1126
1127 # Stop the script.
1128 exit;
1129 }
1130
1131 #
1132 ## Private function to read-in and parse rules of a given rulefile.
1133 #
1134 ## The given file will be read, parsed and all valid rules will be stored by ID,
1135 ## message/description and it's state in the idsrules hash.
1136 #
1137 sub readrulesfile ($) {
1138 my $rulefile = shift;
1139
1140 # Open rule file and read in contents
1141 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1142
1143 # Store file content in an array.
1144 my @lines = <RULEFILE>;
1145
1146 # Close file.
1147 close(RULEFILE);
1148
1149 # Loop over rule file contents
1150 foreach my $line (@lines) {
1151 # Remove whitespaces.
1152 chomp $line;
1153
1154 # Skip blank lines.
1155 next if ($line =~ /^\s*$/);
1156
1157 # Local vars.
1158 my $sid;
1159 my $msg;
1160
1161 # Gather rule sid and message from the ruleline.
1162 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1163 $msg = $1;
1164 $sid = $2;
1165
1166 # Check if a rule has been found.
1167 if ($sid && $msg) {
1168 # Add rule to the idsrules hash.
1169 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1170
1171 # Grab status of the rule. Check if ruleline starts with a "dash".
1172 if ($line =~ /^\#/) {
1173 # If yes, the rule is disabled.
1174 $idsrules{$rulefile}{$sid}{'State'} = "off";
1175 } else {
1176 # Otherwise the rule is enabled.
1177 $idsrules{$rulefile}{$sid}{'State'} = "on";
1178 }
1179 }
1180 }
1181 }
1182 }
1183
1184 #
1185 ## Function to get the used memory of a given process-id.
1186 #
1187 sub get_memory_usage($) {
1188 my ($pid) = @_;
1189
1190 my $memory = 0;
1191
1192 # Try to open the status file for the given process-id on the pseudo
1193 # file system proc.
1194 if (open(FILE, "/proc/$pid/status")) {
1195 # Loop through the entire file.
1196 while (<FILE>) {
1197 # Splitt current line content and store them into variables.
1198 my ($key, $value) = split(":", $_, 2);
1199
1200 # Check if the current key is the one which contains the memory usage.
1201 # The wanted one is VmRSS which contains the Real-memory (resident set)
1202 # of the entire process.
1203 if ($key eq "VmRSS") {
1204 # Found the memory usage add it to the memory variable.
1205 $memory += $value;
1206
1207 # Break the loop.
1208 last;
1209 }
1210 }
1211
1212 # Close file handle.
1213 close(FILE);
1214
1215 # Return memory usage.
1216 return $memory;
1217 }
1218
1219 # If the file could not be open, return nothing.
1220 return;
1221 }
1222
1223 #
1224 ## Function to read-in the given enabled or disables sids file.
1225 #
1226 sub read_enabled_disabled_sids_file($) {
1227 my ($file) = @_;
1228
1229 # Temporary hash to store the sids and their state. It will be
1230 # returned at the end of this function.
1231 my %temphash;
1232
1233 # Open the given filename.
1234 open(FILE, "$file") or die "Could not open $file. $!\n";
1235
1236 # Loop through the file.
1237 while(<FILE>) {
1238 # Remove newlines.
1239 chomp $_;
1240
1241 # Skip blank lines.
1242 next if ($_ =~ /^\s*$/);
1243
1244 # Skip coments.
1245 next if ($_ =~ /^\#/);
1246
1247 # Splitt line into sid and state part.
1248 my ($state, $sid) = split(" ", $_);
1249
1250 # Skip line if the sid is not numeric.
1251 next unless ($sid =~ /\d+/ );
1252
1253 # Check if the sid was enabled.
1254 if ($state eq "enablesid") {
1255 # Add the sid and its state as enabled to the temporary hash.
1256 $temphash{$sid} = "enabled";
1257 # Check if the sid was disabled.
1258 } elsif ($state eq "disablesid") {
1259 # Add the sid and its state as disabled to the temporary hash.
1260 $temphash{$sid} = "disabled";
1261 # Invalid state - skip the current sid and state.
1262 } else {
1263 next;
1264 }
1265 }
1266
1267 # Close filehandle.
1268 close(FILE);
1269
1270 # Return the hash.
1271 return %temphash;
1272 }
1273
1274 #
1275 ## Private function to convert a given rulefile to a category name.
1276 ## ( No file extension anymore and if the name contained a dot, it
1277 ## would be replaced by a underline sign.)
1278 #
1279 sub _rulefile_to_category($) {
1280 my ($filename) = @_;
1281
1282 # Splitt the filename into single chunks and store them in a
1283 # temorary array.
1284 my @parts = split(/\./, $filename);
1285
1286 # Return / Remove last element of the temporary array.
1287 # This removes the file extension.
1288 pop @parts;
1289
1290 # Join together the single elements of the temporary array.
1291 # If these are more than one, use a "underline" for joining.
1292 my $category = join '_', @parts;
1293
1294 # Return the converted filename.
1295 return $category;
1296 }