2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2018 IPFire Team <info@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
24 # enable only the following on debugging purpose
26 #use CGI::Carp 'fatalsToBrowser';
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";
34 my %mainsettings = ();
38 my %rulesetsources = ();
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);
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
();
52 # File where the used rulefiles are stored.
53 my $idsusedrulefilesfile = "$IDS::settingsdir/suricata-used-rulefiles.yaml";
55 # File where the addresses of the homenet are stored.
56 my $idshomenetfile = "$IDS::settingsdir/suricata-homenet.yaml";
58 # File which contains the enabled sids.
59 my $enabled_sids_file = "$IDS::settingsdir/oinkmaster-enabled-sids.conf";
61 # File which contains the disabled sids.
62 my $disabled_sids_file = "$IDS::settingsdir/oinkmaster-disabled-sids.conf";
64 # File which contains wheater the rules should be changed.
65 my $modify_sids_file = "$IDS::settingsdir/oinkmaster-modify-sids.conf";
67 # File which stores the configured IPS settings.
68 my $idssettingsfile = "$IDS::settingsdir/settings";
70 # File which stores the configured rules-settings.
71 my $rulessettingsfile = "$IDS::settingsdir/rules-settings";
73 # File which stores the configured settings for whitelisted addresses.
74 my $ignoredfile = "$IDS::settingsdir/ignored";
76 # File which contains the rules to whitelist addresses on suricata.
77 my $whitelistfile = "$IDS::rulespath/whitelist.rules";
81 # Create files if they does not exist yet.
82 unless (-f
"$enabled_sids_file") { &IDS
::create_empty_file
($enabled_sids_file); }
83 unless (-f
"$disabled_sids_file") { &IDS
::create_empty_file
($disabled_sids_file); }
84 unless (-f
"$modify_sids_file") { &IDS
::create_empty_file
($modify_sids_file); }
85 unless (-f
"$idsusedrulefilesfile") { &IDS
::create_empty_file
($idsusedrulefilesfile); }
86 unless (-f
"$idssettingsfile") { &IDS
::create_empty_file
($idssettingsfile); }
87 unless (-f
"$rulessettingsfile") { &IDS
::create_empty_file
($rulessettingsfile); }
88 unless (-f
"$ignoredfile") { &IDS
::create_empty_file
($ignoredfile); }
89 unless (-f
"$whitelistfile" ) { &IDS
::create_empty_file
($whitelistfile); }
91 # Hash which contains the colour code of a network zone.
93 'red' => $Header::colourred
,
94 'green' => $Header::colourgreen
,
95 'blue' => $Header::colourblue
,
96 'orange' => $Header::colourorange
99 &Header
::showhttpheaders
();
102 &Header
::getcgihash
(\
%cgiparams);
104 ## Add/edit an entry to the ignore file.
106 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'})) {
108 # Check if any input has been performed.
109 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
111 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
112 if ((!&General
::validip
($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
113 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
116 $errormessage = "$Lang::tr{'guardian empty input'}";
119 # Go further if there was no error.
120 if ($errormessage eq '') {
125 # Assign hash values.
126 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
127 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
129 # Read-in ignoredfile.
130 &General
::readhasharray
($ignoredfile, \
%ignored);
132 # Check if we should edit an existing entry and got an ID.
133 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
134 # Assin the provided id.
135 $id = $cgiparams{'ID'};
137 # Undef the given ID.
138 undef($cgiparams{'ID'});
140 # Grab the configured status of the corresponding entry.
141 $status = $ignored{$id}[2];
143 # Each newly added entry automatically should be enabled.
146 # Generate the ID for the new entry.
148 # Sort the keys by their ID and store them in an array.
149 my @keys = sort { $a <=> $b } keys %ignored;
151 # Reverse the key array.
152 my @reversed = reverse(@keys);
154 # Obtain the last used id.
155 my $last_id = @reversed[0];
157 # Increase the last id by one and use it as id for the new entry.
161 # Add/Modify the entry to/in the ignored hash.
162 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
164 # Write the changed ignored hash to the ignored file.
165 &General
::writehasharray
($ignoredfile, \
%ignored);
167 # Regenerate the ignore file.
168 &GenerateIgnoreFile
();
171 # Check if the IDS is running.
172 if(&IDS
::ids_is_running
()) {
173 # Call suricatactrl to perform a reload.
174 &IDS
::call_suricatactrl
("reload");
177 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
180 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'toggle enable disable'}) {
183 # Only go further, if an ID has been passed.
184 if ($cgiparams{'ID'}) {
185 # Assign the given ID.
186 my $id = $cgiparams{'ID'};
188 # Undef the given ID.
189 undef($cgiparams{'ID'});
191 # Read-in ignoredfile.
192 &General
::readhasharray
($ignoredfile, \
%ignored);
194 # Grab the configured status of the corresponding entry.
195 my $status = $ignored{$id}[2];
198 if ($status eq "disabled") {
201 $status = "disabled";
204 # Modify the status of the existing entry.
205 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
207 # Write the changed ignored hash to the ignored file.
208 &General
::writehasharray
($ignoredfile, \
%ignored);
210 # Regenerate the ignore file.
211 &GenerateIgnoreFile
();
213 # Check if the IDS is running.
214 if(&IDS
::ids_is_running
()) {
215 # Call suricatactrl to perform a reload.
216 &IDS
::call_suricatactrl
("reload");
220 ## Remove entry from ignore list.
222 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'remove'}) {
225 # Read-in ignoredfile.
226 &General
::readhasharray
($ignoredfile, \
%ignored);
228 # Drop entry from the hash.
229 delete($ignored{$cgiparams{'ID'}});
231 # Undef the given ID.
232 undef($cgiparams{'ID'});
234 # Write the changed ignored hash to the ignored file.
235 &General
::writehasharray
($ignoredfile, \
%ignored);
237 # Regenerate the ignore file.
238 &GenerateIgnoreFile
();
240 # Check if the IDS is running.
241 if(&IDS
::ids_is_running
()) {
242 # Call suricatactrl to perform a reload.
243 &IDS
::call_suricatactrl
("reload");
247 # Check if any error has been stored.
248 if (-e
$IDS::storederrorfile
) {
249 # Open file to read in the stored error message.
250 open(FILE
, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
252 # Read the stored error message.
253 $errormessage = <FILE
>;
258 # Delete the file, which is now not longer required.
259 unlink($IDS::storederrorfile
);
263 ## Grab all available snort rules and store them in the idsrules hash.
265 # Open snort rules directory and do a directory listing.
266 opendir(DIR
, $IDS::rulespath
) or die $!;
267 # Loop through the direcory.
268 while (my $file = readdir(DIR
)) {
270 # We only want files.
271 next unless (-f
"$IDS::rulespath/$file");
273 # Ignore empty files.
274 next if (-z
"$IDS::rulespath/$file");
276 # Use a regular expression to find files ending in .rules
277 next unless ($file =~ m/\.rules$/);
279 # Ignore files which are not read-able.
280 next unless (-R
"$IDS::rulespath/$file");
282 # Skip whitelist rules file.
283 next if( $file eq "whitelist.rules");
285 # Call subfunction to read-in rulefile and add rules to
287 &readrulesfile
("$file");
292 # Gather used rulefiles.
294 # Check if the file for activated rulefiles is not empty.
295 if(-f
$idsusedrulefilesfile) {
296 # Open the file for used rulefile and read-in content.
297 open(FILE
, $idsusedrulefilesfile) or die "Could not open $idsusedrulefilesfile. $!\n";
305 # Loop through the array.
306 foreach my $line (@lines) {
311 next if ($line =~ /\#/);
314 next if ($line =~ /^\s*$/);
316 # Gather rule sid and message from the ruleline.
317 if ($line =~ /.*- (.*)/) {
320 # Check if the current rulefile exists in the %idsrules hash.
321 # If not, the file probably does not exist anymore or contains
323 if($idsrules{$rulefile}) {
324 # Add the rulefile state to the %idsrules hash.
325 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
331 # Save ruleset configuration.
332 if ($cgiparams{'RULESET'} eq $Lang::tr
{'save'}) {
335 # Read-in current (old) IDS settings.
336 &General
::readhash
("$rulessettingsfile", \
%oldsettings);
338 # Prevent form name from been stored in conf file.
339 delete $cgiparams{'RULESET'};
341 # Check if an oinkcode has been provided.
342 if ($cgiparams{'OINKCODE'}) {
343 # Check if the oinkcode contains unallowed chars.
344 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
345 $errormessage = $Lang::tr
{'invalid input for oink code'};
349 # Go on if there are no error messages.
350 if (!$errormessage) {
351 # Store settings into settings file.
352 &General
::writehash
("$rulessettingsfile", \
%cgiparams);
355 # Check if the the automatic rule update hass been touched.
356 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldsettings{'AUTOUPDATE_INTERVAL'}) {
357 # Call suricatactrl to set the new interval.
358 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
362 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'update'}) {
363 # Arrays to store which rulefiles have been enabled and will be used.
364 my @enabled_rulefiles;
366 # Hash to store the user-enabled and disabled sids.
367 my %enabled_disabled_sids;
369 # Loop through the hash of idsrules.
370 foreach my $rulefile(keys %idsrules) {
371 # Check if the rulefile is enabled.
372 if ($cgiparams{$rulefile} eq "on") {
373 # Add rulefile to the array of enabled rulefiles.
374 push(@enabled_rulefiles, $rulefile);
376 # Drop item from cgiparams hash.
377 delete $cgiparams{$rulefile};
381 # Read-in the files for enabled/disabled sids.
382 # This will be done by calling the read_enabled_disabled_sids_file function two times
383 # and merge the returned hashes together into the enabled_disabled_sids hash.
384 %enabled_disabled_sids = (
385 &read_enabled_disabled_sids_file
($disabled_sids_file),
386 &read_enabled_disabled_sids_file
($enabled_sids_file));
388 # Loop through the hash of idsrules.
389 foreach my $rulefile (keys %idsrules) {
390 # Loop through the single rules of the rulefile.
391 foreach my $sid (keys %{$idsrules{$rulefile}}) {
392 # Skip the current sid if it is not numeric.
393 next unless ($sid =~ /\d+/ );
395 # Check if there exists a key in the cgiparams hash for this sid.
396 if (exists($cgiparams{$sid})) {
397 # Look if the rule is disabled.
398 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
399 # Check if the state has been set to 'on'.
400 if ($cgiparams{$sid} eq "on") {
401 # Add/Modify the sid to/in the enabled_disabled_sids hash.
402 $enabled_disabled_sids{$sid} = "enabled";
404 # Drop item from cgiparams hash.
405 delete $cgiparams{$rulefile}{$sid};
409 # Look if the rule is enabled.
410 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
411 # Check if the state is 'on' and should be disabled.
412 # In this case there is no entry
413 # for the sid in the cgiparams hash.
414 # Add/Modify it to/in the enabled_disabled_sids hash.
415 $enabled_disabled_sids{$sid} = "disabled";
417 # Drop item from cgiparams hash.
418 delete $cgiparams{$rulefile}{$sid};
424 # Open enabled sid's file for writing.
425 open(ENABLED_FILE
, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
427 # Open disabled sid's file for writing.
428 open(DISABLED_FILE
, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
430 # Write header to the files.
431 print ENABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
432 print DISABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
434 # Check if the hash for enabled/disabled files contains any entries.
435 if (%enabled_disabled_sids) {
436 # Loop through the hash.
437 foreach my $sid (keys %enabled_disabled_sids) {
438 # Check if the sid is enabled.
439 if ($enabled_disabled_sids{$sid} eq "enabled") {
440 # Print the sid to the enabled_sids file.
441 print ENABLED_FILE
"enablesid $sid\n";
442 # Check if the sid is disabled.
443 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
444 # Print the sid to the disabled_sids file.
445 print DISABLED_FILE
"disablesid $sid\n";
446 # Something strange happende - skip the current sid.
453 # Close file for enabled_sids after writing.
456 # Close file for disabled_sids after writing.
457 close(DISABLED_FILE
);
459 # Open file for used rulefiles.
460 open (FILE
, ">$idsusedrulefilesfile") or die "Could not write to $idsusedrulefilesfile. $!\n";
462 # Write yaml header to the file.
463 print FILE
"%YAML 1.1\n";
464 print FILE
"---\n\n";
466 # Write header to file.
467 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
469 # Allways load the whitelist.
470 print FILE
" - whitelist.rules\n";
472 # Check if the enabled_rulefiles array contains any entries.
473 if (@enabled_rulefiles) {
474 # Loop through the array of rulefiles which should be loaded and write them to the file.
475 foreach my $file (@enabled_rulefiles) {
476 print FILE
" - $file\n";
480 # Close file after writing.
483 # Lock the webpage and print message.
484 &working_notice
("$Lang::tr{'snort working'}");
486 # Call oinkmaster to alter the ruleset.
489 # Check if the IDS is running.
490 if(&IDS
::ids_is_running
()) {
491 # Call suricatactrl to perform a reload.
492 &IDS
::call_suricatactrl
("reload");
498 # Download new ruleset.
499 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'download new ruleset'}) {
500 # Check if the red device is active.
501 unless (-e
"${General::swroot}/red/active") {
502 $errormessage = $Lang::tr
{'could not download latest updates'};
505 # Check if enought free disk space is availabe.
506 if(&IDS
::checkdiskspace
()) {
507 $errormessage = "$Lang::tr{'not enough disk space'}";
510 # Check if any errors happend.
511 unless ($errormessage) {
512 # Lock the webpage and print notice about downloading
514 &working_notice
("$Lang::tr{'snort working'}");
516 # Call subfunction to download the ruleset.
517 if(&IDS
::downloadruleset
()) {
518 $errormessage = $Lang::tr
{'could not download latest updates'};
520 # Call function to store the errormessage.
521 &IDS
::_store_error_message
($errormessage);
523 # Preform a reload of the page.
526 # Call subfunction to launch oinkmaster.
529 # Check if the IDS is running.
530 if(&IDS
::ids_is_running
()) {
531 # Call suricatactrl to perform a reload.
532 &IDS
::call_suricatactrl
("reload");
535 # Perform a reload of the page.
539 # Save snort settings.
540 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
543 my $monitored_zones = 0;
545 # Read-in current (old) IDS settings.
546 &General
::readhash
("$idssettingsfile", \
%oldidssettings);
548 # Prevent form name from been stored in conf file.
549 delete $cgiparams{'IDS'};
551 # Check if the IDS should be enabled.
552 if ($cgiparams{'ENABLE_IDS'} eq "on") {
553 # Check if any ruleset is available. Otherwise abort and display an error.
555 $errormessage = $Lang::tr
{'ids no ruleset available'};
558 # Loop through the array of available interfaces.
559 foreach my $zone (@network_zones) {
560 # Convert interface name into upper case.
561 my $zone_upper = uc($zone);
563 # Check if the IDS is enabled for this interaces.
564 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
570 # Check if at least one zone should be monitored, or show an error.
571 unless ($monitored_zones >= 1) {
572 $errormessage = $Lang::tr
{'ids no network zone'};
576 # Go on if there are no error messages.
577 if (!$errormessage) {
578 # Store settings into settings file.
579 &General
::writehash
("$idssettingsfile", \
%cgiparams);
582 # Generate file to store the home net.
583 &generate_home_net_file
();
585 # Open modify sid's file for writing.
586 open(FILE
, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
589 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
591 # Check if the traffic only should be monitored.
592 unless($cgiparams{'MONITOR_TRAFFIC_ONLY'} eq 'on') {
593 # Tell oinkmaster to switch all rules from alert to drop.
594 print FILE
"modifysid \* \"alert\" \| \"drop\"\n";
600 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
601 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
602 # Check if a ruleset exists.
604 # Lock the webpage and print message.
605 &working_notice
("$Lang::tr{'snort working'}");
607 # Call oinkmaster to alter the ruleset.
610 # Set reload_page to "True".
615 # Check if the IDS currently is running.
616 if(&IDS
::ids_is_running
()) {
617 # Check if ENABLE_IDS is set to on.
618 if($cgiparams{'ENABLE_IDS'} eq "on") {
619 # Call suricatactrl to perform a reload of suricata.
620 &IDS
::call_suricatactrl
("reload");
622 # Call suricatactrl to stop suricata.
623 &IDS
::call_suricatactrl
("stop");
626 # Call suricatactrl to start suricata.
627 &IDS
::call_suricatactrl
("start");
630 # Check if the page should be reloaded.
632 # Perform a reload of the page.
637 # Read-in idssettings and rulesetsettings
638 &General
::readhash
("$idssettingsfile", \
%idssettings);
639 &General
::readhash
("$rulessettingsfile", \
%rulessettings);
641 # If no autoupdate intervall has been configured yet, set default value.
642 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
643 # Set default to "weekly".
644 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
647 # Read-in ignored hosts.
648 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
650 $checked{'ENABLE_IDS'}{'off'} = '';
651 $checked{'ENABLE_IDS'}{'on'} = '';
652 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
653 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
654 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
655 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
656 $selected{'RULES'}{'nothing'} = '';
657 $selected{'RULES'}{'community'} = '';
658 $selected{'RULES'}{'emerging'} = '';
659 $selected{'RULES'}{'registered'} = '';
660 $selected{'RULES'}{'subscripted'} = '';
661 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
662 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
663 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
664 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
665 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
667 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
672 // Tiny java script function to show/hide the rules
673 // of a given category.
674 function showhide(tblname) {
675 \$("#" + tblname).toggle();
681 &Header
::openbigbox
('100%', 'left', '', $errormessage);
684 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
685 print "<class name='base'>$errormessage\n";
686 print " </class>\n";
690 # Draw current state of the IDS
691 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
693 # Check if the IDS is running and obtain the process-id.
694 my $pid = &IDS
::ids_is_running
();
696 # Display some useful information, if suricata daemon is running.
698 # Gather used memory.
699 my $memory = &get_memory_usage
($pid);
702 <table width='95%' cellspacing='0' class='tbl'>
704 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
708 <td class='base'>$Lang::tr{'guardian daemon'}</td>
709 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
713 <td class='base'></td>
714 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
715 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
719 <td class='base'></td>
720 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
721 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
726 # Otherwise display a hint that the service is not launched.
728 <table width='95%' cellspacing='0' class='tbl'>
730 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
734 <td class='base'>$Lang::tr{'guardian daemon'}</td>
735 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
744 # Check if a ruleset allready has been downloaded.
745 if ( -f
"$IDS::rulestarball"){
746 # Call stat on the filename to obtain detailed information.
747 my @Info = stat("$IDS::rulestarball");
749 # Grab details about the creation time.
750 $rulesdate = localtime($Info[9]);
753 # Draw elements for IDS configuration.
754 &Header
::openbox
('100%', 'center', $Lang::tr
{'settings'});
757 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
758 <table width='100%' border='0'>
760 <td class='base' colspan='2'>
761 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
764 <td class='base' colspan='2'>
765 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>$Lang::tr{'ids monitor traffic only'}
777 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
784 # Loop through the array of available networks and print config options.
785 foreach my $zone (@network_zones) {
789 # Convert current zone name to upper case.
790 my $zone_upper = uc($zone);
793 my $zone_name = $zone;
795 # Dirty hack to get the correct language string for the red zone.
796 if ($zone eq "red") {
800 # Grab checkbox status from settings hash.
801 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
802 $checked_input = "checked = 'checked'";
805 print "<td class='base' width='25%'>\n";
806 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
807 print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
819 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
828 # Draw elements for ruleset configuration.
829 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
832 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
833 <table width='100%' border='0'>
835 <td><b>$Lang::tr{'ids rules update'}</b></td>
836 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
840 <td><select name='RULES'>
841 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
842 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
843 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
844 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
849 <select name='AUTOUPDATE_INTERVAL'>
850 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option>
851 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option>
852 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option>
859 <br>$Lang::tr{'ids rules license'} <a href='https://www.snort.org/subscribe' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}</br>
860 <br>$Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}</br>
865 <td colspan='2' nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
874 # Check if a ruleset source has been configured yet.
875 unless($rulessettings{'RULES'}) {
876 # If no ruleset settings have been saved yet, disable the button to download / update the ruleset.
877 print"<input type='submit' name='RULESET' disabled='disabled' value='$Lang::tr{'download new ruleset'}'>\n";
879 # Ruleset setting have been saved. - Check if a ruleset already is downloaded.
881 # Allow to press the button and show it as "update ruleset".
882 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
884 # Also allow to press the button, but show it as "download new ruleset".
885 print"<input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>\n";
889 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
901 # Whitelist / Ignorelist
903 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
908 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
909 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
910 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
913 # Check if some hosts have been added to be ignored.
914 if (keys (%ignored)) {
917 # Loop through all entries of the hash.
918 while( (my $key) = each %ignored) {
919 # Assign data array positions to some nice variable names.
920 my $address = $ignored{$key}[0];
921 my $remark = $ignored{$key}[1];
922 my $status = $ignored{$key}[2];
924 # Check if the key (id) number is even or not.
925 if ($cgiparams{'ID'} eq $key) {
926 $col="bgcolor='${Header::colouryellow}'";
928 $col="bgcolor='$color{'color22'}'";
930 $col="bgcolor='$color{'color20'}'";
933 # Choose icon for the checkbox.
937 # Check if the status is enabled and select the correct image and description.
938 if ($status eq 'enabled' ) {
940 $gdesc = $Lang::tr
{'click to disable'};
943 $gdesc = $Lang::tr
{'click to enable'};
948 <td width='20%' class='base' $col>$address</td>
949 <td width='65%' class='base' $col>$remark</td>
951 <td align='center' $col>
952 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
953 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
954 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
955 <input type='hidden' name='ID' value='$key' />
959 <td align='center' $col>
960 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
961 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
962 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
963 <input type='hidden' name='ID' value='$key' />
967 <td align='center' $col>
968 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
969 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
970 <input type='hidden' name='ID' value='$key'>
971 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
978 # Print notice that currently no hosts are ignored.
980 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
986 # Section to add new elements or edit existing ones.
996 # Assign correct headline and button text.
1001 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1002 if ($cgiparams{'ID'} ne '') {
1003 $buttontext = $Lang::tr
{'update'};
1004 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1006 # Grab address and remark for the given key.
1007 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1008 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1010 $buttontext = $Lang::tr
{'add'};
1011 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1015 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1016 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1018 <td width='30%'>$Lang::tr{'ip address'}: </td>
1019 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1021 <td width='30%'>$Lang::tr{'remark'}: </td>
1022 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1023 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1030 &Header
::closebox
();
1032 # Only show the section for configuring the ruleset if one is present.
1034 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
1036 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1038 # Output display table for rule files
1039 print "<table width='100%'>\n";
1041 # Loop over each rule file
1042 foreach my $rulefile (sort keys(%idsrules)) {
1043 my $rulechecked = '';
1045 # Check if rule file is enabled
1046 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1047 $rulechecked = 'CHECKED';
1050 # Convert rulefile name into category name.
1051 my $categoryname = &_rulefile_to_category
($rulefile);
1053 # Table and rows for the rule files.
1055 print"<td class='base' width='5%'>\n";
1056 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1058 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1059 print"<td class='base' width='5%' align='right'>\n";
1060 print"<a href=\"javascript:showhide('$categoryname')\">SHOW</a>\n";
1064 # Rows which will be hidden per default and will contain the single rules.
1065 print"<tr style='display:none' id='$categoryname'>\n";
1066 print"<td colspan='3'>\n";
1073 # New table for the single rules.
1074 print "<table width='100%'>\n";
1076 # Loop over rule file rules
1077 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1079 my $ruledefchecked = '';
1081 # Skip rulefile itself.
1082 next if ($sid eq "Rulefile");
1084 # If 2 rules have been displayed, start a new row
1085 if (($lines % 2) == 0) {
1086 print "</tr><tr>\n";
1088 # Increase rows by once.
1094 $col="bgcolor='$color{'color20'}'";
1096 $col="bgcolor='$color{'color22'}'";
1100 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1101 $ruledefchecked = 'CHECKED';
1104 # Create rule checkbox and display rule description
1105 print "<td class='base' width='5%' align='right' $col>\n";
1106 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1108 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1110 # Increment rule count
1114 # If do not have a second rule for row, create empty cell
1115 if (($lines % 2) != 0) {
1116 print "<td class='base'></td>";
1119 # Close display table
1120 print "</tr></table></td></tr>";
1123 # Close display table
1127 <table width='100%'>
1129 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
1130 <!-- space for future online help link -->
1137 &Header
::closebox
();
1140 &Header
::closebigbox
();
1141 &Header
::closepage
();
1144 ## A function to display a notice, to lock the webpage and
1145 ## tell the user which action currently will be performed.
1147 sub working_notice
($) {
1150 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1151 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1152 &Header
::openbox
( 'Waiting', 1,);
1156 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1161 &Header
::closebox
();
1162 &Header
::closebigbox
();
1163 &Header
::closepage
();
1167 ## A tiny function to perform a reload of the webpage after one second.
1170 print "<meta http-equiv='refresh' content='1'>\n";
1177 ## Private function to read-in and parse rules of a given rulefile.
1179 ## The given file will be read, parsed and all valid rules will be stored by ID,
1180 ## message/description and it's state in the idsrules hash.
1182 sub readrulesfile
($) {
1183 my $rulefile = shift;
1185 # Open rule file and read in contents
1186 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1188 # Store file content in an array.
1189 my @lines = <RULEFILE
>;
1194 # Loop over rule file contents
1195 foreach my $line (@lines) {
1196 # Remove whitespaces.
1200 next if ($line =~ /^\s*$/);
1206 # Gather rule sid and message from the ruleline.
1207 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1211 # Check if a rule has been found.
1213 # Add rule to the idsrules hash.
1214 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1216 # Grab status of the rule. Check if ruleline starts with a "dash".
1217 if ($line =~ /^\#/) {
1218 # If yes, the rule is disabled.
1219 $idsrules{$rulefile}{$sid}{'State'} = "off";
1221 # Otherwise the rule is enabled.
1222 $idsrules{$rulefile}{$sid}{'State'} = "on";
1230 ## Function to get the used memory of a given process-id.
1232 sub get_memory_usage
($) {
1237 # Try to open the status file for the given process-id on the pseudo
1239 if (open(FILE
, "/proc/$pid/status")) {
1240 # Loop through the entire file.
1242 # Splitt current line content and store them into variables.
1243 my ($key, $value) = split(":", $_, 2);
1245 # Check if the current key is the one which contains the memory usage.
1246 # The wanted one is VmRSS which contains the Real-memory (resident set)
1247 # of the entire process.
1248 if ($key eq "VmRSS") {
1249 # Found the memory usage add it to the memory variable.
1257 # Close file handle.
1260 # Return memory usage.
1264 # If the file could not be open, return nothing.
1269 ## Function to generate the file which contains the home net information.
1271 sub generate_home_net_file
() {
1274 # Read-in network settings.
1275 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
1277 # Get available network zones.
1278 my @network_zones = &IDS
::get_available_network_zones
();
1280 # Temporary array to store network address and prefix of the configured
1284 # Loop through the array of available network zones.
1285 foreach my $zone (@network_zones) {
1286 # Skip the red network - It never can be part to the home_net!
1287 next if($zone eq "red");
1289 # Convert current zone name into upper case.
1292 # Generate key to access the required data from the netsettings hash.
1293 my $zone_netaddress = $zone . "_NETADDRESS";
1294 my $zone_netmask = $zone . "_NETMASK";
1296 # Obtain the settings from the netsettings hash.
1297 my $netaddress = $netsettings{$zone_netaddress};
1298 my $netmask = $netsettings{$zone_netmask};
1300 # Convert the subnetmask into prefix notation.
1301 my $prefix = &Network
::convert_netmask2prefix
($netmask);
1303 # Generate full network string.
1304 my $network = join("/", $netaddress,$prefix);
1306 # Check if the network is valid.
1307 if(&Network
::check_subnet
($network)) {
1308 # Add the generated network to the array of networks.
1309 push(@networks, $network);
1313 # Format home net declaration.
1316 # Loop through the array of networks.
1317 foreach my $network (@networks) {
1318 # Add the network to the line.
1319 $line = "$line" . "$network";
1321 # Check if the current network was the last in the array.
1322 if ($network eq $networks[-1]) {
1324 $line = "$line" . "\]\"";
1326 # Add "," for the next network.
1327 $line = "$line" . "\,";
1331 # Open file to store the addresses of the home net.
1332 open(FILE
, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n";
1334 # Print yaml header.
1335 print FILE
"%YAML 1.1\n";
1336 print FILE
"---\n\n";
1338 # Print notice about autogenerated file.
1339 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1341 # Print the generated and required HOME_NET declaration to the file.
1342 print FILE
"HOME_NET:\t$line\n";
1344 # Close file handle.
1350 ## Function to generate the rules file with whitelisted addresses.
1352 sub GenerateIgnoreFile
() {
1355 # SID range 1000000-1999999 Reserved for Local Use
1356 # Put your custom rules in this range to avoid conflicts
1359 # Read-in ignoredfile.
1360 &General
::readhasharray
($ignoredfile, \
%ignored);
1362 # Open ignorefile for writing.
1363 open(FILE
, ">$whitelistfile") or die "Could not write to $whitelistfile. $!\n";
1365 # Config file header.
1366 print FILE
"# Autogenerated file.\n";
1367 print FILE
"# All user modifications will be overwritten.\n\n";
1369 # Add all user defined addresses to the whitelist.
1371 # Check if the hash contains any elements.
1372 if (keys (%ignored)) {
1373 # Loop through the entire hash and write the host/network
1374 # and remark to the ignore file.
1375 while ( (my $key) = each %ignored) {
1376 my $address = $ignored{$key}[0];
1377 my $remark = $ignored{$key}[1];
1378 my $status = $ignored{$key}[2];
1380 # Check if the status of the entry is "enabled".
1381 if ($status eq "enabled") {
1382 # Check if the address/network is valid.
1383 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
1384 # Write rule line to the file to pass any traffic from this IP
1385 print FILE
"pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n";
1398 ## Function to read-in the given enabled or disables sids file.
1400 sub read_enabled_disabled_sids_file
($) {
1403 # Temporary hash to store the sids and their state. It will be
1404 # returned at the end of this function.
1407 # Open the given filename.
1408 open(FILE
, "$file") or die "Could not open $file. $!\n";
1410 # Loop through the file.
1416 next if ($_ =~ /^\s*$/);
1419 next if ($_ =~ /^\#/);
1421 # Splitt line into sid and state part.
1422 my ($state, $sid) = split(" ", $_);
1424 # Skip line if the sid is not numeric.
1425 next unless ($sid =~ /\d+/ );
1427 # Check if the sid was enabled.
1428 if ($state eq "enablesid") {
1429 # Add the sid and its state as enabled to the temporary hash.
1430 $temphash{$sid} = "enabled";
1431 # Check if the sid was disabled.
1432 } elsif ($state eq "disablesid") {
1433 # Add the sid and its state as disabled to the temporary hash.
1434 $temphash{$sid} = "disabled";
1435 # Invalid state - skip the current sid and state.
1449 ## Private function to convert a given rulefile to a category name.
1450 ## ( No file extension anymore and if the name contained a dot, it
1451 ## would be replaced by a underline sign.)
1453 sub _rulefile_to_category
($) {
1454 my ($filename) = @_;
1456 # Splitt the filename into single chunks and store them in a
1458 my @parts = split(/\./, $filename);
1460 # Return / Remove last element of the temporary array.
1461 # This removes the file extension.
1464 # Join together the single elements of the temporary array.
1465 # If these are more than one, use a "underline" for joining.
1466 my $category = join '_', @parts;
1468 # Return the converted filename.