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 &Header
::showhttpheaders
();
94 &Header
::getcgihash
(\
%cgiparams);
96 ## Add/edit an entry to the ignore file.
98 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'})) {
100 # Check if any input has been performed.
101 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
103 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
104 if ((!&General
::validip
($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
105 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
108 $errormessage = "$Lang::tr{'guardian empty input'}";
111 # Go further if there was no error.
112 if ($errormessage eq '') {
117 # Assign hash values.
118 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
119 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
121 # Read-in ignoredfile.
122 &General
::readhasharray
($ignoredfile, \
%ignored);
124 # Check if we should edit an existing entry and got an ID.
125 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
126 # Assin the provided id.
127 $id = $cgiparams{'ID'};
129 # Undef the given ID.
130 undef($cgiparams{'ID'});
132 # Grab the configured status of the corresponding entry.
133 $status = $ignored{$id}[2];
135 # Each newly added entry automatically should be enabled.
138 # Generate the ID for the new entry.
140 # Sort the keys by their ID and store them in an array.
141 my @keys = sort { $a <=> $b } keys %ignored;
143 # Reverse the key array.
144 my @reversed = reverse(@keys);
146 # Obtain the last used id.
147 my $last_id = @reversed[0];
149 # Increase the last id by one and use it as id for the new entry.
153 # Add/Modify the entry to/in the ignored hash.
154 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
156 # Write the changed ignored hash to the ignored file.
157 &General
::writehasharray
($ignoredfile, \
%ignored);
159 # Regenerate the ignore file.
160 &GenerateIgnoreFile
();
163 # Check if the IDS is running.
164 if(&IDS
::ids_is_running
()) {
165 # Call suricatactrl to perform a reload.
166 &IDS
::call_suricatactrl
("reload");
169 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
172 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'toggle enable disable'}) {
175 # Only go further, if an ID has been passed.
176 if ($cgiparams{'ID'}) {
177 # Assign the given ID.
178 my $id = $cgiparams{'ID'};
180 # Undef the given ID.
181 undef($cgiparams{'ID'});
183 # Read-in ignoredfile.
184 &General
::readhasharray
($ignoredfile, \
%ignored);
186 # Grab the configured status of the corresponding entry.
187 my $status = $ignored{$id}[2];
190 if ($status eq "disabled") {
193 $status = "disabled";
196 # Modify the status of the existing entry.
197 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
199 # Write the changed ignored hash to the ignored file.
200 &General
::writehasharray
($ignoredfile, \
%ignored);
202 # Regenerate the ignore file.
203 &GenerateIgnoreFile
();
205 # Check if the IDS is running.
206 if(&IDS
::ids_is_running
()) {
207 # Call suricatactrl to perform a reload.
208 &IDS
::call_suricatactrl
("reload");
212 ## Remove entry from ignore list.
214 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'remove'}) {
217 # Read-in ignoredfile.
218 &General
::readhasharray
($ignoredfile, \
%ignored);
220 # Drop entry from the hash.
221 delete($ignored{$cgiparams{'ID'}});
223 # Undef the given ID.
224 undef($cgiparams{'ID'});
226 # Write the changed ignored hash to the ignored file.
227 &General
::writehasharray
($ignoredfile, \
%ignored);
229 # Regenerate the ignore file.
230 &GenerateIgnoreFile
();
232 # Check if the IDS is running.
233 if(&IDS
::ids_is_running
()) {
234 # Call suricatactrl to perform a reload.
235 &IDS
::call_suricatactrl
("reload");
239 # Check if any error has been stored.
240 if (-e
$IDS::storederrorfile
) {
241 # Open file to read in the stored error message.
242 open(FILE
, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
244 # Read the stored error message.
245 $errormessage = <FILE
>;
250 # Delete the file, which is now not longer required.
251 unlink($IDS::storederrorfile
);
255 ## Grab all available snort rules and store them in the idsrules hash.
257 # Open snort rules directory and do a directory listing.
258 opendir(DIR
, $IDS::rulespath
) or die $!;
259 # Loop through the direcory.
260 while (my $file = readdir(DIR
)) {
262 # We only want files.
263 next unless (-f
"$IDS::rulespath/$file");
265 # Ignore empty files.
266 next if (-z
"$IDS::rulespath/$file");
268 # Use a regular expression to find files ending in .rules
269 next unless ($file =~ m/\.rules$/);
271 # Ignore files which are not read-able.
272 next unless (-R
"$IDS::rulespath/$file");
274 # Skip whitelist rules file.
275 next if( $file eq "whitelist.rules");
277 # Call subfunction to read-in rulefile and add rules to
279 &readrulesfile
("$file");
284 # Gather used rulefiles.
286 # Check if the file for activated rulefiles is not empty.
287 if(-f
$idsusedrulefilesfile) {
288 # Open the file for used rulefile and read-in content.
289 open(FILE
, $idsusedrulefilesfile) or die "Could not open $idsusedrulefilesfile. $!\n";
297 # Loop through the array.
298 foreach my $line (@lines) {
303 next if ($line =~ /\#/);
306 next if ($line =~ /^\s*$/);
308 # Gather rule sid and message from the ruleline.
309 if ($line =~ /.*- (.*)/) {
312 # Check if the current rulefile exists in the %idsrules hash.
313 # If not, the file probably does not exist anymore or contains
315 if($idsrules{$rulefile}) {
316 # Add the rulefile state to the %idsrules hash.
317 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
323 # Save ruleset configuration.
324 if ($cgiparams{'RULESET'} eq $Lang::tr
{'save'}) {
327 # Read-in current (old) IDS settings.
328 &General
::readhash
("$rulessettingsfile", \
%oldsettings);
330 # Prevent form name from been stored in conf file.
331 delete $cgiparams{'RULESET'};
333 # Check if an oinkcode has been provided.
334 if ($cgiparams{'OINKCODE'}) {
335 # Check if the oinkcode contains unallowed chars.
336 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
337 $errormessage = $Lang::tr
{'invalid input for oink code'};
341 # Go on if there are no error messages.
342 if (!$errormessage) {
343 # Store settings into settings file.
344 &General
::writehash
("$rulessettingsfile", \
%cgiparams);
347 # Check if the the automatic rule update hass been touched.
348 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldsettings{'AUTOUPDATE_INTERVAL'}) {
349 # Call suricatactrl to set the new interval.
350 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
354 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'update'}) {
355 # Arrays to store which rulefiles have been enabled and will be used.
356 my @enabled_rulefiles;
358 # Hash to store the user-enabled and disabled sids.
359 my %enabled_disabled_sids;
361 # Loop through the hash of idsrules.
362 foreach my $rulefile(keys %idsrules) {
363 # Check if the rulefile is enabled.
364 if ($cgiparams{$rulefile} eq "on") {
365 # Add rulefile to the array of enabled rulefiles.
366 push(@enabled_rulefiles, $rulefile);
368 # Drop item from cgiparams hash.
369 delete $cgiparams{$rulefile};
373 # Read-in the files for enabled/disabled sids.
374 # This will be done by calling the read_enabled_disabled_sids_file function two times
375 # and merge the returned hashes together into the enabled_disabled_sids hash.
376 %enabled_disabled_sids = (
377 &read_enabled_disabled_sids_file
($disabled_sids_file),
378 &read_enabled_disabled_sids_file
($enabled_sids_file));
380 # Loop through the hash of idsrules.
381 foreach my $rulefile (keys %idsrules) {
382 # Loop through the single rules of the rulefile.
383 foreach my $sid (keys %{$idsrules{$rulefile}}) {
384 # Skip the current sid if it is not numeric.
385 next unless ($sid =~ /\d+/ );
387 # Check if there exists a key in the cgiparams hash for this sid.
388 if (exists($cgiparams{$sid})) {
389 # Look if the rule is disabled.
390 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
391 # Check if the state has been set to 'on'.
392 if ($cgiparams{$sid} eq "on") {
393 # Add/Modify the sid to/in the enabled_disabled_sids hash.
394 $enabled_disabled_sids{$sid} = "enabled";
396 # Drop item from cgiparams hash.
397 delete $cgiparams{$rulefile}{$sid};
401 # Look if the rule is enabled.
402 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
403 # Check if the state is 'on' and should be disabled.
404 # In this case there is no entry
405 # for the sid in the cgiparams hash.
406 # Add/Modify it to/in the enabled_disabled_sids hash.
407 $enabled_disabled_sids{$sid} = "disabled";
409 # Drop item from cgiparams hash.
410 delete $cgiparams{$rulefile}{$sid};
416 # Open enabled sid's file for writing.
417 open(ENABLED_FILE
, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
419 # Open disabled sid's file for writing.
420 open(DISABLED_FILE
, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
422 # Write header to the files.
423 print ENABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
424 print DISABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
426 # Check if the hash for enabled/disabled files contains any entries.
427 if (%enabled_disabled_sids) {
428 # Loop through the hash.
429 foreach my $sid (keys %enabled_disabled_sids) {
430 # Check if the sid is enabled.
431 if ($enabled_disabled_sids{$sid} eq "enabled") {
432 # Print the sid to the enabled_sids file.
433 print ENABLED_FILE
"enablesid $sid\n";
434 # Check if the sid is disabled.
435 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
436 # Print the sid to the disabled_sids file.
437 print DISABLED_FILE
"disablesid $sid\n";
438 # Something strange happende - skip the current sid.
445 # Close file for enabled_sids after writing.
448 # Close file for disabled_sids after writing.
449 close(DISABLED_FILE
);
451 # Open file for used rulefiles.
452 open (FILE
, ">$idsusedrulefilesfile") or die "Could not write to $idsusedrulefilesfile. $!\n";
454 # Write yaml header to the file.
455 print FILE
"%YAML 1.1\n";
456 print FILE
"---\n\n";
458 # Write header to file.
459 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
461 # Allways load the whitelist.
462 print FILE
" - whitelist.rules\n";
464 # Check if the enabled_rulefiles array contains any entries.
465 if (@enabled_rulefiles) {
466 # Loop through the array of rulefiles which should be loaded and write them to the file.
467 foreach my $file (@enabled_rulefiles) {
468 print FILE
" - $file\n";
472 # Close file after writing.
475 # Lock the webpage and print message.
476 &working_notice
("$Lang::tr{'snort working'}");
478 # Call oinkmaster to alter the ruleset.
481 # Check if the IDS is running.
482 if(&IDS
::ids_is_running
()) {
483 # Call suricatactrl to perform a reload.
484 &IDS
::call_suricatactrl
("reload");
490 # Download new ruleset.
491 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'download new ruleset'}) {
492 # Check if the red device is active.
493 unless (-e
"${General::swroot}/red/active") {
494 $errormessage = $Lang::tr
{'could not download latest updates'};
497 # Check if enought free disk space is availabe.
498 if(&IDS
::checkdiskspace
()) {
499 $errormessage = "$Lang::tr{'not enough disk space'}";
502 # Check if any errors happend.
503 unless ($errormessage) {
504 # Lock the webpage and print notice about downloading
506 &working_notice
("$Lang::tr{'snort working'}");
508 # Call subfunction to download the ruleset.
509 if(&IDS
::downloadruleset
()) {
510 $errormessage = $Lang::tr
{'could not download latest updates'};
512 # Call function to store the errormessage.
513 &IDS
::_store_error_message
($errormessage);
515 # Preform a reload of the page.
518 # Call subfunction to launch oinkmaster.
521 # Check if the IDS is running.
522 if(&IDS
::ids_is_running
()) {
523 # Call suricatactrl to perform a reload.
524 &IDS
::call_suricatactrl
("reload");
527 # Perform a reload of the page.
531 # Save snort settings.
532 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
535 my $monitored_zones = 0;
537 # Read-in current (old) IDS settings.
538 &General
::readhash
("$idssettingsfile", \
%oldidssettings);
540 # Prevent form name from been stored in conf file.
541 delete $cgiparams{'IDS'};
543 # Check if the IDS should be enabled.
544 if ($cgiparams{'ENABLE_IDS'} eq "on") {
545 # Check if any ruleset is available. Otherwise abort and display an error.
547 $errormessage = $Lang::tr
{'ids no ruleset available'};
550 # Loop through the array of available interfaces.
551 foreach my $zone (@network_zones) {
552 # Convert interface name into upper case.
553 my $zone_upper = uc($zone);
555 # Check if the IDS is enabled for this interaces.
556 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
562 # Check if at least one zone should be monitored, or show an error.
563 unless ($monitored_zones >= 1) {
564 $errormessage = $Lang::tr
{'ids no network zone'};
568 # Go on if there are no error messages.
569 if (!$errormessage) {
570 # Store settings into settings file.
571 &General
::writehash
("$idssettingsfile", \
%cgiparams);
574 # Generate file to store the home net.
575 &generate_home_net_file
();
577 # Open modify sid's file for writing.
578 open(FILE
, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
581 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
583 # Check if the traffic only should be monitored.
584 unless($cgiparams{'MONITOR_TRAFFIC_ONLY'} eq 'on') {
585 # Tell oinkmaster to switch all rules from alert to drop.
586 print FILE
"modifysid \* \"alert\" \| \"drop\"\n";
592 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
593 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
594 # Check if a ruleset exists.
596 # Lock the webpage and print message.
597 &working_notice
("$Lang::tr{'snort working'}");
599 # Call oinkmaster to alter the ruleset.
602 # Set reload_page to "True".
607 # Check if the IDS currently is running.
608 if(&IDS
::ids_is_running
()) {
609 # Check if ENABLE_IDS is set to on.
610 if($cgiparams{'ENABLE_IDS'} eq "on") {
611 # Call suricatactrl to perform a reload of suricata.
612 &IDS
::call_suricatactrl
("reload");
614 # Call suricatactrl to stop suricata.
615 &IDS
::call_suricatactrl
("stop");
618 # Call suricatactrl to start suricata.
619 &IDS
::call_suricatactrl
("start");
622 # Check if the page should be reloaded.
624 # Perform a reload of the page.
629 # Read-in idssettings and rulesetsettings
630 &General
::readhash
("$idssettingsfile", \
%idssettings);
631 &General
::readhash
("$rulessettingsfile", \
%rulessettings);
633 # If no autoupdate intervall has been configured yet, set default value.
634 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
635 # Set default to "weekly".
636 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
639 # Read-in ignored hosts.
640 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
642 $checked{'ENABLE_IDS'}{'off'} = '';
643 $checked{'ENABLE_IDS'}{'on'} = '';
644 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
645 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
646 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
647 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
648 $selected{'RULES'}{'nothing'} = '';
649 $selected{'RULES'}{'community'} = '';
650 $selected{'RULES'}{'emerging'} = '';
651 $selected{'RULES'}{'registered'} = '';
652 $selected{'RULES'}{'subscripted'} = '';
653 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
654 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
655 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
656 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
657 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
659 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
664 // Tiny java script function to show/hide the rules
665 // of a given category.
666 function showhide(tblname) {
667 \$("#" + tblname).toggle();
673 &Header
::openbigbox
('100%', 'left', '', $errormessage);
676 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
677 print "<class name='base'>$errormessage\n";
678 print " </class>\n";
682 # Draw current state of the IDS
683 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
685 # Check if the IDS is running and obtain the process-id.
686 my $pid = &IDS
::ids_is_running
();
688 # Display some useful information, if suricata daemon is running.
690 # Gather used memory.
691 my $memory = &get_memory_usage
($pid);
694 <table width='95%' cellspacing='0' class='tbl'>
696 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
700 <td class='base'>$Lang::tr{'guardian daemon'}</td>
701 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
705 <td class='base'></td>
706 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
707 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
711 <td class='base'></td>
712 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
713 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
718 # Otherwise display a hint that the service is not launched.
720 <table width='95%' cellspacing='0' class='tbl'>
722 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
726 <td class='base'>$Lang::tr{'guardian daemon'}</td>
727 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
736 # Check if a ruleset allready has been downloaded.
737 if ( -f
"$IDS::rulestarball"){
738 # Call stat on the filename to obtain detailed information.
739 my @Info = stat("$IDS::rulestarball");
741 # Grab details about the creation time.
742 $rulesdate = localtime($Info[9]);
745 # Draw elements for IDS configuration.
746 &Header
::openbox
('100%', 'center', $Lang::tr
{'settings'});
749 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
750 <table width='100%' border='0'>
752 <td class='base' colspan='2'>
753 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
756 <td class='base' colspan='2'>
757 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>$Lang::tr{'ids monitor traffic only'}
769 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
776 # Loop through the array of available networks and print config options.
777 foreach my $zone (@network_zones) {
781 # Convert current zone name to upper case.
782 my $zone_upper = uc($zone);
784 # Grab checkbox status from settings hash.
785 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
786 $checked_input = "checked = 'checked'";
789 print "<td class='base' width='25%'>\n";
790 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>$Lang::tr{$zone}\n";
802 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
811 # Draw elements for ruleset configuration.
812 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
815 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
816 <table width='100%' border='0'>
818 <td><b>$Lang::tr{'ids rules update'}</b></td>
819 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
823 <td><select name='RULES'>
824 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
825 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
826 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
827 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
832 <select name='AUTOUPDATE_INTERVAL'>
833 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option>
834 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option>
835 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option>
842 <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>
843 <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>
848 <td colspan='2' nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
857 # Check if a ruleset source has been configured yet.
858 unless($rulessettings{'RULES'}) {
859 # If no ruleset settings have been saved yet, disable the button to download / update the ruleset.
860 print"<input type='submit' name='RULESET' disabled='disabled' value='$Lang::tr{'download new ruleset'}'>\n";
862 # Ruleset setting have been saved. - Check if a ruleset already is downloaded.
864 # Allow to press the button and show it as "update ruleset".
865 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
867 # Also allow to press the button, but show it as "download new ruleset".
868 print"<input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>\n";
872 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
884 # Whitelist / Ignorelist
886 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
891 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
892 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
893 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
896 # Check if some hosts have been added to be ignored.
897 if (keys (%ignored)) {
900 # Loop through all entries of the hash.
901 while( (my $key) = each %ignored) {
902 # Assign data array positions to some nice variable names.
903 my $address = $ignored{$key}[0];
904 my $remark = $ignored{$key}[1];
905 my $status = $ignored{$key}[2];
907 # Check if the key (id) number is even or not.
908 if ($cgiparams{'ID'} eq $key) {
909 $col="bgcolor='${Header::colouryellow}'";
911 $col="bgcolor='$color{'color22'}'";
913 $col="bgcolor='$color{'color20'}'";
916 # Choose icon for the checkbox.
920 # Check if the status is enabled and select the correct image and description.
921 if ($status eq 'enabled' ) {
923 $gdesc = $Lang::tr
{'click to disable'};
926 $gdesc = $Lang::tr
{'click to enable'};
931 <td width='20%' class='base' $col>$address</td>
932 <td width='65%' class='base' $col>$remark</td>
934 <td align='center' $col>
935 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
936 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
937 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
938 <input type='hidden' name='ID' value='$key' />
942 <td align='center' $col>
943 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
944 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
945 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
946 <input type='hidden' name='ID' value='$key' />
950 <td align='center' $col>
951 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
952 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
953 <input type='hidden' name='ID' value='$key'>
954 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
961 # Print notice that currently no hosts are ignored.
963 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
969 # Section to add new elements or edit existing ones.
979 # Assign correct headline and button text.
984 # Check if an ID (key) has been given, in this case an existing entry should be edited.
985 if ($cgiparams{'ID'} ne '') {
986 $buttontext = $Lang::tr
{'update'};
987 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
989 # Grab address and remark for the given key.
990 $entry_address = $ignored{$cgiparams{'ID'}}[0];
991 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
993 $buttontext = $Lang::tr
{'add'};
994 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
998 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
999 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1001 <td width='30%'>$Lang::tr{'ip address'}: </td>
1002 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1004 <td width='30%'>$Lang::tr{'remark'}: </td>
1005 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1006 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1013 &Header
::closebox
();
1015 # Only show the section for configuring the ruleset if one is present.
1017 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
1019 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1021 # Output display table for rule files
1022 print "<table width='100%'>\n";
1024 # Loop over each rule file
1025 foreach my $rulefile (sort keys(%idsrules)) {
1026 my $rulechecked = '';
1028 # Check if rule file is enabled
1029 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1030 $rulechecked = 'CHECKED';
1033 # Convert rulefile name into category name.
1034 my $categoryname = &_rulefile_to_category
($rulefile);
1036 # Table and rows for the rule files.
1038 print"<td class='base' width='5%'>\n";
1039 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1041 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1042 print"<td class='base' width='5%' align='right'>\n";
1043 print"<a href=\"javascript:showhide('$categoryname')\">SHOW</a>\n";
1047 # Rows which will be hidden per default and will contain the single rules.
1048 print"<tr style='display:none' id='$categoryname'>\n";
1049 print"<td colspan='3'>\n";
1056 # New table for the single rules.
1057 print "<table width='100%'>\n";
1059 # Loop over rule file rules
1060 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1062 my $ruledefchecked = '';
1064 # Skip rulefile itself.
1065 next if ($sid eq "Rulefile");
1067 # If 2 rules have been displayed, start a new row
1068 if (($lines % 2) == 0) {
1069 print "</tr><tr>\n";
1071 # Increase rows by once.
1077 $col="bgcolor='$color{'color20'}'";
1079 $col="bgcolor='$color{'color22'}'";
1083 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1084 $ruledefchecked = 'CHECKED';
1087 # Create rule checkbox and display rule description
1088 print "<td class='base' width='5%' align='right' $col>\n";
1089 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1091 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1093 # Increment rule count
1097 # If do not have a second rule for row, create empty cell
1098 if (($lines % 2) != 0) {
1099 print "<td class='base'></td>";
1102 # Close display table
1103 print "</tr></table></td></tr>";
1106 # Close display table
1110 <table width='100%'>
1112 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
1113 <!-- space for future online help link -->
1120 &Header
::closebox
();
1123 &Header
::closebigbox
();
1124 &Header
::closepage
();
1127 ## A function to display a notice, to lock the webpage and
1128 ## tell the user which action currently will be performed.
1130 sub working_notice
($) {
1133 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1134 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1135 &Header
::openbox
( 'Waiting', 1,);
1139 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1144 &Header
::closebox
();
1145 &Header
::closebigbox
();
1146 &Header
::closepage
();
1150 ## A tiny function to perform a reload of the webpage after one second.
1153 print "<meta http-equiv='refresh' content='1'>\n";
1160 ## Private function to read-in and parse rules of a given rulefile.
1162 ## The given file will be read, parsed and all valid rules will be stored by ID,
1163 ## message/description and it's state in the idsrules hash.
1165 sub readrulesfile
($) {
1166 my $rulefile = shift;
1168 # Open rule file and read in contents
1169 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1171 # Store file content in an array.
1172 my @lines = <RULEFILE
>;
1177 # Loop over rule file contents
1178 foreach my $line (@lines) {
1179 # Remove whitespaces.
1183 next if ($line =~ /^\s*$/);
1189 # Gather rule sid and message from the ruleline.
1190 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1194 # Check if a rule has been found.
1196 # Add rule to the idsrules hash.
1197 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1199 # Grab status of the rule. Check if ruleline starts with a "dash".
1200 if ($line =~ /^\#/) {
1201 # If yes, the rule is disabled.
1202 $idsrules{$rulefile}{$sid}{'State'} = "off";
1204 # Otherwise the rule is enabled.
1205 $idsrules{$rulefile}{$sid}{'State'} = "on";
1213 ## Function to get the used memory of a given process-id.
1215 sub get_memory_usage
($) {
1220 # Try to open the status file for the given process-id on the pseudo
1222 if (open(FILE
, "/proc/$pid/status")) {
1223 # Loop through the entire file.
1225 # Splitt current line content and store them into variables.
1226 my ($key, $value) = split(":", $_, 2);
1228 # Check if the current key is the one which contains the memory usage.
1229 # The wanted one is VmRSS which contains the Real-memory (resident set)
1230 # of the entire process.
1231 if ($key eq "VmRSS") {
1232 # Found the memory usage add it to the memory variable.
1240 # Close file handle.
1243 # Return memory usage.
1247 # If the file could not be open, return nothing.
1252 ## Function to generate the file which contains the home net information.
1254 sub generate_home_net_file
() {
1257 # Read-in network settings.
1258 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
1260 # Get available network zones.
1261 my @network_zones = &IDS
::get_available_network_zones
();
1263 # Temporary array to store network address and prefix of the configured
1267 # Loop through the array of available network zones.
1268 foreach my $zone (@network_zones) {
1269 # Skip the red network - It never can be part to the home_net!
1270 next if($zone eq "red");
1272 # Convert current zone name into upper case.
1275 # Generate key to access the required data from the netsettings hash.
1276 my $zone_netaddress = $zone . "_NETADDRESS";
1277 my $zone_netmask = $zone . "_NETMASK";
1279 # Obtain the settings from the netsettings hash.
1280 my $netaddress = $netsettings{$zone_netaddress};
1281 my $netmask = $netsettings{$zone_netmask};
1283 # Convert the subnetmask into prefix notation.
1284 my $prefix = &Network
::convert_netmask2prefix
($netmask);
1286 # Generate full network string.
1287 my $network = join("/", $netaddress,$prefix);
1289 # Check if the network is valid.
1290 if(&Network
::check_subnet
($network)) {
1291 # Add the generated network to the array of networks.
1292 push(@networks, $network);
1296 # Format home net declaration.
1299 # Loop through the array of networks.
1300 foreach my $network (@networks) {
1301 # Add the network to the line.
1302 $line = "$line" . "$network";
1304 # Check if the current network was the last in the array.
1305 if ($network eq $networks[-1]) {
1307 $line = "$line" . "\]\"";
1309 # Add "," for the next network.
1310 $line = "$line" . "\,";
1314 # Open file to store the addresses of the home net.
1315 open(FILE
, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n";
1317 # Print yaml header.
1318 print FILE
"%YAML 1.1\n";
1319 print FILE
"---\n\n";
1321 # Print notice about autogenerated file.
1322 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1324 # Print the generated and required HOME_NET declaration to the file.
1325 print FILE
"HOME_NET:\t$line\n";
1327 # Close file handle.
1333 ## Function to generate the rules file with whitelisted addresses.
1335 sub GenerateIgnoreFile
() {
1338 # SID range 1000000-1999999 Reserved for Local Use
1339 # Put your custom rules in this range to avoid conflicts
1342 # Read-in ignoredfile.
1343 &General
::readhasharray
($ignoredfile, \
%ignored);
1345 # Open ignorefile for writing.
1346 open(FILE
, ">$whitelistfile") or die "Could not write to $whitelistfile. $!\n";
1348 # Config file header.
1349 print FILE
"# Autogenerated file.\n";
1350 print FILE
"# All user modifications will be overwritten.\n\n";
1352 # Add all user defined addresses to the whitelist.
1354 # Check if the hash contains any elements.
1355 if (keys (%ignored)) {
1356 # Loop through the entire hash and write the host/network
1357 # and remark to the ignore file.
1358 while ( (my $key) = each %ignored) {
1359 my $address = $ignored{$key}[0];
1360 my $remark = $ignored{$key}[1];
1361 my $status = $ignored{$key}[2];
1363 # Check if the status of the entry is "enabled".
1364 if ($status eq "enabled") {
1365 # Check if the address/network is valid.
1366 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
1367 # Write rule line to the file to pass any traffic from this IP
1368 print FILE
"pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n";
1381 ## Function to read-in the given enabled or disables sids file.
1383 sub read_enabled_disabled_sids_file
($) {
1386 # Temporary hash to store the sids and their state. It will be
1387 # returned at the end of this function.
1390 # Open the given filename.
1391 open(FILE
, "$file") or die "Could not open $file. $!\n";
1393 # Loop through the file.
1399 next if ($_ =~ /^\s*$/);
1402 next if ($_ =~ /^\#/);
1404 # Splitt line into sid and state part.
1405 my ($state, $sid) = split(" ", $_);
1407 # Skip line if the sid is not numeric.
1408 next unless ($sid =~ /\d+/ );
1410 # Check if the sid was enabled.
1411 if ($state eq "enablesid") {
1412 # Add the sid and its state as enabled to the temporary hash.
1413 $temphash{$sid} = "enabled";
1414 # Check if the sid was disabled.
1415 } elsif ($state eq "disablesid") {
1416 # Add the sid and its state as disabled to the temporary hash.
1417 $temphash{$sid} = "disabled";
1418 # Invalid state - skip the current sid and state.
1432 ## Private function to convert a given rulefile to a category name.
1433 ## ( No file extension anymore and if the name contained a dot, it
1434 ## would be replaced by a underline sign.)
1436 sub _rulefile_to_category
($) {
1437 my ($filename) = @_;
1439 # Splitt the filename into single chunks and store them in a
1441 my @parts = split(/\./, $filename);
1443 # Return / Remove last element of the temporary array.
1444 # This removes the file extension.
1447 # Join together the single elements of the temporary array.
1448 # If these are more than one, use a "underline" for joining.
1449 my $category = join '_', @parts;
1451 # Return the converted filename.