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);
792 # Grab checkbox status from settings hash.
793 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
794 $checked_input = "checked = 'checked'";
797 print "<td class='base' width='25%'>\n";
798 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
799 print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone}</font>\n";
811 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
820 # Draw elements for ruleset configuration.
821 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
824 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
825 <table width='100%' border='0'>
827 <td><b>$Lang::tr{'ids rules update'}</b></td>
828 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
832 <td><select name='RULES'>
833 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
834 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
835 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
836 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
841 <select name='AUTOUPDATE_INTERVAL'>
842 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option>
843 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option>
844 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option>
851 <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>
852 <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>
857 <td colspan='2' nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
866 # Check if a ruleset source has been configured yet.
867 unless($rulessettings{'RULES'}) {
868 # If no ruleset settings have been saved yet, disable the button to download / update the ruleset.
869 print"<input type='submit' name='RULESET' disabled='disabled' value='$Lang::tr{'download new ruleset'}'>\n";
871 # Ruleset setting have been saved. - Check if a ruleset already is downloaded.
873 # Allow to press the button and show it as "update ruleset".
874 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
876 # Also allow to press the button, but show it as "download new ruleset".
877 print"<input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>\n";
881 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
893 # Whitelist / Ignorelist
895 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
900 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
901 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
902 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
905 # Check if some hosts have been added to be ignored.
906 if (keys (%ignored)) {
909 # Loop through all entries of the hash.
910 while( (my $key) = each %ignored) {
911 # Assign data array positions to some nice variable names.
912 my $address = $ignored{$key}[0];
913 my $remark = $ignored{$key}[1];
914 my $status = $ignored{$key}[2];
916 # Check if the key (id) number is even or not.
917 if ($cgiparams{'ID'} eq $key) {
918 $col="bgcolor='${Header::colouryellow}'";
920 $col="bgcolor='$color{'color22'}'";
922 $col="bgcolor='$color{'color20'}'";
925 # Choose icon for the checkbox.
929 # Check if the status is enabled and select the correct image and description.
930 if ($status eq 'enabled' ) {
932 $gdesc = $Lang::tr
{'click to disable'};
935 $gdesc = $Lang::tr
{'click to enable'};
940 <td width='20%' class='base' $col>$address</td>
941 <td width='65%' class='base' $col>$remark</td>
943 <td align='center' $col>
944 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
945 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
946 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
947 <input type='hidden' name='ID' value='$key' />
951 <td align='center' $col>
952 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
953 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
954 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
955 <input type='hidden' name='ID' value='$key' />
959 <td align='center' $col>
960 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
961 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
962 <input type='hidden' name='ID' value='$key'>
963 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
970 # Print notice that currently no hosts are ignored.
972 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
978 # Section to add new elements or edit existing ones.
988 # Assign correct headline and button text.
993 # Check if an ID (key) has been given, in this case an existing entry should be edited.
994 if ($cgiparams{'ID'} ne '') {
995 $buttontext = $Lang::tr
{'update'};
996 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
998 # Grab address and remark for the given key.
999 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1000 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1002 $buttontext = $Lang::tr
{'add'};
1003 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1007 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1008 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1010 <td width='30%'>$Lang::tr{'ip address'}: </td>
1011 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1013 <td width='30%'>$Lang::tr{'remark'}: </td>
1014 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1015 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1022 &Header
::closebox
();
1024 # Only show the section for configuring the ruleset if one is present.
1026 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
1028 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1030 # Output display table for rule files
1031 print "<table width='100%'>\n";
1033 # Loop over each rule file
1034 foreach my $rulefile (sort keys(%idsrules)) {
1035 my $rulechecked = '';
1037 # Check if rule file is enabled
1038 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1039 $rulechecked = 'CHECKED';
1042 # Convert rulefile name into category name.
1043 my $categoryname = &_rulefile_to_category
($rulefile);
1045 # Table and rows for the rule files.
1047 print"<td class='base' width='5%'>\n";
1048 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1050 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1051 print"<td class='base' width='5%' align='right'>\n";
1052 print"<a href=\"javascript:showhide('$categoryname')\">SHOW</a>\n";
1056 # Rows which will be hidden per default and will contain the single rules.
1057 print"<tr style='display:none' id='$categoryname'>\n";
1058 print"<td colspan='3'>\n";
1065 # New table for the single rules.
1066 print "<table width='100%'>\n";
1068 # Loop over rule file rules
1069 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1071 my $ruledefchecked = '';
1073 # Skip rulefile itself.
1074 next if ($sid eq "Rulefile");
1076 # If 2 rules have been displayed, start a new row
1077 if (($lines % 2) == 0) {
1078 print "</tr><tr>\n";
1080 # Increase rows by once.
1086 $col="bgcolor='$color{'color20'}'";
1088 $col="bgcolor='$color{'color22'}'";
1092 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1093 $ruledefchecked = 'CHECKED';
1096 # Create rule checkbox and display rule description
1097 print "<td class='base' width='5%' align='right' $col>\n";
1098 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1100 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1102 # Increment rule count
1106 # If do not have a second rule for row, create empty cell
1107 if (($lines % 2) != 0) {
1108 print "<td class='base'></td>";
1111 # Close display table
1112 print "</tr></table></td></tr>";
1115 # Close display table
1119 <table width='100%'>
1121 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
1122 <!-- space for future online help link -->
1129 &Header
::closebox
();
1132 &Header
::closebigbox
();
1133 &Header
::closepage
();
1136 ## A function to display a notice, to lock the webpage and
1137 ## tell the user which action currently will be performed.
1139 sub working_notice
($) {
1142 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1143 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1144 &Header
::openbox
( 'Waiting', 1,);
1148 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1153 &Header
::closebox
();
1154 &Header
::closebigbox
();
1155 &Header
::closepage
();
1159 ## A tiny function to perform a reload of the webpage after one second.
1162 print "<meta http-equiv='refresh' content='1'>\n";
1169 ## Private function to read-in and parse rules of a given rulefile.
1171 ## The given file will be read, parsed and all valid rules will be stored by ID,
1172 ## message/description and it's state in the idsrules hash.
1174 sub readrulesfile
($) {
1175 my $rulefile = shift;
1177 # Open rule file and read in contents
1178 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1180 # Store file content in an array.
1181 my @lines = <RULEFILE
>;
1186 # Loop over rule file contents
1187 foreach my $line (@lines) {
1188 # Remove whitespaces.
1192 next if ($line =~ /^\s*$/);
1198 # Gather rule sid and message from the ruleline.
1199 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1203 # Check if a rule has been found.
1205 # Add rule to the idsrules hash.
1206 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1208 # Grab status of the rule. Check if ruleline starts with a "dash".
1209 if ($line =~ /^\#/) {
1210 # If yes, the rule is disabled.
1211 $idsrules{$rulefile}{$sid}{'State'} = "off";
1213 # Otherwise the rule is enabled.
1214 $idsrules{$rulefile}{$sid}{'State'} = "on";
1222 ## Function to get the used memory of a given process-id.
1224 sub get_memory_usage
($) {
1229 # Try to open the status file for the given process-id on the pseudo
1231 if (open(FILE
, "/proc/$pid/status")) {
1232 # Loop through the entire file.
1234 # Splitt current line content and store them into variables.
1235 my ($key, $value) = split(":", $_, 2);
1237 # Check if the current key is the one which contains the memory usage.
1238 # The wanted one is VmRSS which contains the Real-memory (resident set)
1239 # of the entire process.
1240 if ($key eq "VmRSS") {
1241 # Found the memory usage add it to the memory variable.
1249 # Close file handle.
1252 # Return memory usage.
1256 # If the file could not be open, return nothing.
1261 ## Function to generate the file which contains the home net information.
1263 sub generate_home_net_file
() {
1266 # Read-in network settings.
1267 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
1269 # Get available network zones.
1270 my @network_zones = &IDS
::get_available_network_zones
();
1272 # Temporary array to store network address and prefix of the configured
1276 # Loop through the array of available network zones.
1277 foreach my $zone (@network_zones) {
1278 # Skip the red network - It never can be part to the home_net!
1279 next if($zone eq "red");
1281 # Convert current zone name into upper case.
1284 # Generate key to access the required data from the netsettings hash.
1285 my $zone_netaddress = $zone . "_NETADDRESS";
1286 my $zone_netmask = $zone . "_NETMASK";
1288 # Obtain the settings from the netsettings hash.
1289 my $netaddress = $netsettings{$zone_netaddress};
1290 my $netmask = $netsettings{$zone_netmask};
1292 # Convert the subnetmask into prefix notation.
1293 my $prefix = &Network
::convert_netmask2prefix
($netmask);
1295 # Generate full network string.
1296 my $network = join("/", $netaddress,$prefix);
1298 # Check if the network is valid.
1299 if(&Network
::check_subnet
($network)) {
1300 # Add the generated network to the array of networks.
1301 push(@networks, $network);
1305 # Format home net declaration.
1308 # Loop through the array of networks.
1309 foreach my $network (@networks) {
1310 # Add the network to the line.
1311 $line = "$line" . "$network";
1313 # Check if the current network was the last in the array.
1314 if ($network eq $networks[-1]) {
1316 $line = "$line" . "\]\"";
1318 # Add "," for the next network.
1319 $line = "$line" . "\,";
1323 # Open file to store the addresses of the home net.
1324 open(FILE
, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n";
1326 # Print yaml header.
1327 print FILE
"%YAML 1.1\n";
1328 print FILE
"---\n\n";
1330 # Print notice about autogenerated file.
1331 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1333 # Print the generated and required HOME_NET declaration to the file.
1334 print FILE
"HOME_NET:\t$line\n";
1336 # Close file handle.
1342 ## Function to generate the rules file with whitelisted addresses.
1344 sub GenerateIgnoreFile
() {
1347 # SID range 1000000-1999999 Reserved for Local Use
1348 # Put your custom rules in this range to avoid conflicts
1351 # Read-in ignoredfile.
1352 &General
::readhasharray
($ignoredfile, \
%ignored);
1354 # Open ignorefile for writing.
1355 open(FILE
, ">$whitelistfile") or die "Could not write to $whitelistfile. $!\n";
1357 # Config file header.
1358 print FILE
"# Autogenerated file.\n";
1359 print FILE
"# All user modifications will be overwritten.\n\n";
1361 # Add all user defined addresses to the whitelist.
1363 # Check if the hash contains any elements.
1364 if (keys (%ignored)) {
1365 # Loop through the entire hash and write the host/network
1366 # and remark to the ignore file.
1367 while ( (my $key) = each %ignored) {
1368 my $address = $ignored{$key}[0];
1369 my $remark = $ignored{$key}[1];
1370 my $status = $ignored{$key}[2];
1372 # Check if the status of the entry is "enabled".
1373 if ($status eq "enabled") {
1374 # Check if the address/network is valid.
1375 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
1376 # Write rule line to the file to pass any traffic from this IP
1377 print FILE
"pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n";
1390 ## Function to read-in the given enabled or disables sids file.
1392 sub read_enabled_disabled_sids_file
($) {
1395 # Temporary hash to store the sids and their state. It will be
1396 # returned at the end of this function.
1399 # Open the given filename.
1400 open(FILE
, "$file") or die "Could not open $file. $!\n";
1402 # Loop through the file.
1408 next if ($_ =~ /^\s*$/);
1411 next if ($_ =~ /^\#/);
1413 # Splitt line into sid and state part.
1414 my ($state, $sid) = split(" ", $_);
1416 # Skip line if the sid is not numeric.
1417 next unless ($sid =~ /\d+/ );
1419 # Check if the sid was enabled.
1420 if ($state eq "enablesid") {
1421 # Add the sid and its state as enabled to the temporary hash.
1422 $temphash{$sid} = "enabled";
1423 # Check if the sid was disabled.
1424 } elsif ($state eq "disablesid") {
1425 # Add the sid and its state as disabled to the temporary hash.
1426 $temphash{$sid} = "disabled";
1427 # Invalid state - skip the current sid and state.
1441 ## Private function to convert a given rulefile to a category name.
1442 ## ( No file extension anymore and if the name contained a dot, it
1443 ## would be replaced by a underline sign.)
1445 sub _rulefile_to_category
($) {
1446 my ($filename) = @_;
1448 # Splitt the filename into single chunks and store them in a
1450 my @parts = split(/\./, $filename);
1452 # Return / Remove last element of the temporary array.
1453 # This removes the file extension.
1456 # Join together the single elements of the temporary array.
1457 # If these are more than one, use a "underline" for joining.
1458 my $category = join '_', @parts;
1460 # Return the converted filename.