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 # Check if the runmode has been changed.
578 if($cgiparams{'RUN_MODE'} ne $oldidssettings{'RUN_MODE'}) {
579 # Open modify sid's file for writing.
580 open(FILE
, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
583 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
585 # Check if the configured runmode is IPS.
586 if ($cgiparams{'RUN_MODE'} eq 'IPS') {
587 # Tell oinkmaster to switch all rules from alert to drop.
588 print FILE
"modifysid \* \"alert\" \| \"drop\"\n";
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 the runmode has not been configured yet, set default value.
634 unless(exists($idssettings{'RUN_MODE'})) {
635 # Set default to IPS.
636 $idssettings{'RUN_MODE'} = 'IPS';
639 # If no autoupdate intervall has been configured yet, set default value.
640 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
641 # Set default to "weekly".
642 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
645 # Read-in ignored hosts.
646 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
648 $checked{'ENABLE_IDS'}{'off'} = '';
649 $checked{'ENABLE_IDS'}{'on'} = '';
650 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
651 $checked{'RUN_MODE'}{'IDS'} = '';
652 $checked{'RUN_MODE'}{'IPS'} = '';
653 $checked{'RUN_MODE'}{$idssettings{'RUN_MODE'}} = "checked='checked'";
654 $selected{'RULES'}{'nothing'} = '';
655 $selected{'RULES'}{'community'} = '';
656 $selected{'RULES'}{'emerging'} = '';
657 $selected{'RULES'}{'registered'} = '';
658 $selected{'RULES'}{'subscripted'} = '';
659 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
660 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
661 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
662 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
663 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
665 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
670 // Tiny java script function to show/hide the rules
671 // of a given category.
672 function showhide(tblname) {
673 \$("#" + tblname).toggle();
679 &Header
::openbigbox
('100%', 'left', '', $errormessage);
682 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
683 print "<class name='base'>$errormessage\n";
684 print " </class>\n";
688 # Draw current state of the IDS
689 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
691 # Check if the IDS is running and obtain the process-id.
692 my $pid = &IDS
::ids_is_running
();
694 # Display some useful information, if suricata daemon is running.
696 # Gather used memory.
697 my $memory = &get_memory_usage
($pid);
700 <table width='95%' cellspacing='0' class='tbl'>
702 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
706 <td class='base'>$Lang::tr{'guardian daemon'}</td>
707 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
711 <td class='base'></td>
712 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
713 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
717 <td class='base'></td>
718 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
719 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
724 # Otherwise display a hint that the service is not launched.
726 <table width='95%' cellspacing='0' class='tbl'>
728 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
732 <td class='base'>$Lang::tr{'guardian daemon'}</td>
733 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
740 # Draw elements for IDS configuration.
741 &Header
::openbox
('100%', 'center', $Lang::tr
{'settings'});
744 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
745 <table width='100%' border='0'>
747 <td class='base' colspan='4'>
748 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
753 <td colspan='4'><br><br></td>
757 <td class='base' colspan='4'><b>$Lang::tr{'runmode'}</b></td>
761 <td class='base' colspan='4'>
762 <input type='radio' name='RUN_MODE' value='IDS' $checked{'RUN_MODE'}{'IDS'}>$Lang::tr{'intrusion detection system2'}    
763 <input type='radio' name='RUN_MODE' value='IPS' $checked{'RUN_MODE'}{'IPS'}>$Lang::tr{'intrusion prevention system'}
768 <td colspan='4'><br></td>
772 <td colspan='4'><b>$Lang::tr{'ids traffic analyze'}</b><br></td>
779 # Loop through the array of available networks and print config options.
780 foreach my $zone (@network_zones) {
784 # Convert current zone name to upper case.
785 my $zone_upper = uc($zone);
787 # Grab checkbox status from settings hash.
788 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
789 $checked_input = "checked = 'checked'";
792 print "<td class='base' width='25%'>\n";
793 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>$Lang::tr{'enabled on'} $Lang::tr{$zone}\n";
805 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
814 # Draw elements for ruleset configuration.
815 &Header
::openbox
('100%', 'center', $Lang::tr
{'ids ruleset settings'});
819 # Check if a ruleset allready has been downloaded.
820 if ( -f
"$IDS::rulestarball"){
821 # Call stat on the filename to obtain detailed information.
822 my @Info = stat("$IDS::rulestarball");
824 # Grab details about the creation time.
825 $rulesdate = localtime($Info[9]);
829 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
830 <table width='100%' border='0'>
832 <td><b>$Lang::tr{'ids rules update'}</b></td>
833 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
837 <td><select name='RULES'>
838 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
839 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
840 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
841 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
846 <select name='AUTOUPDATE_INTERVAL'>
847 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option>
848 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option>
849 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option>
856 <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>
857 <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>
862 <td colspan='2' nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
871 # Check if a ruleset source has been configured yet.
872 unless($rulessettings{'RULES'}) {
873 # If no ruleset settings have been saved yet, disable the button to download / update the ruleset.
874 print"<input type='submit' name='RULESET' disabled='disabled' value='$Lang::tr{'download new ruleset'}'>\n";
876 # Ruleset setting have been saved. - Check if a ruleset already is downloaded.
878 # Allow to press the button and show it as "update ruleset".
879 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
881 # Also allow to press the button, but show it as "download new ruleset".
882 print"<input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>\n";
886 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
898 # Whitelist / Ignorelist
900 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
905 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
906 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
907 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
910 # Check if some hosts have been added to be ignored.
911 if (keys (%ignored)) {
914 # Loop through all entries of the hash.
915 while( (my $key) = each %ignored) {
916 # Assign data array positions to some nice variable names.
917 my $address = $ignored{$key}[0];
918 my $remark = $ignored{$key}[1];
919 my $status = $ignored{$key}[2];
921 # Check if the key (id) number is even or not.
922 if ($cgiparams{'ID'} eq $key) {
923 $col="bgcolor='${Header::colouryellow}'";
925 $col="bgcolor='$color{'color22'}'";
927 $col="bgcolor='$color{'color20'}'";
930 # Choose icon for the checkbox.
934 # Check if the status is enabled and select the correct image and description.
935 if ($status eq 'enabled' ) {
937 $gdesc = $Lang::tr
{'click to disable'};
940 $gdesc = $Lang::tr
{'click to enable'};
945 <td width='20%' class='base' $col>$address</td>
946 <td width='65%' class='base' $col>$remark</td>
948 <td align='center' $col>
949 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
950 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
951 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
952 <input type='hidden' name='ID' value='$key' />
956 <td align='center' $col>
957 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
958 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
959 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
960 <input type='hidden' name='ID' value='$key' />
964 <td align='center' $col>
965 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
966 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
967 <input type='hidden' name='ID' value='$key'>
968 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
975 # Print notice that currently no hosts are ignored.
977 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
983 # Section to add new elements or edit existing ones.
993 # Assign correct headline and button text.
998 # Check if an ID (key) has been given, in this case an existing entry should be edited.
999 if ($cgiparams{'ID'} ne '') {
1000 $buttontext = $Lang::tr
{'update'};
1001 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1003 # Grab address and remark for the given key.
1004 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1005 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1007 $buttontext = $Lang::tr
{'add'};
1008 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1012 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1013 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1015 <td width='30%'>$Lang::tr{'ip address'}: </td>
1016 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1018 <td width='30%'>$Lang::tr{'remark'}: </td>
1019 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1020 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1027 &Header
::closebox
();
1029 # Only show the section for configuring the ruleset if one is present.
1031 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
1033 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1035 # Output display table for rule files
1036 print "<table width='100%'>\n";
1038 # Loop over each rule file
1039 foreach my $rulefile (sort keys(%idsrules)) {
1040 my $rulechecked = '';
1042 # Check if rule file is enabled
1043 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1044 $rulechecked = 'CHECKED';
1047 # Convert rulefile name into category name.
1048 my $categoryname = &_rulefile_to_category
($rulefile);
1050 # Table and rows for the rule files.
1052 print"<td class='base' width='5%'>\n";
1053 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1055 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1056 print"<td class='base' width='5%' align='right'>\n";
1057 print"<a href=\"javascript:showhide('$categoryname')\">SHOW</a>\n";
1061 # Rows which will be hidden per default and will contain the single rules.
1062 print"<tr style='display:none' id='$categoryname'>\n";
1063 print"<td colspan='3'>\n";
1070 # New table for the single rules.
1071 print "<table width='100%'>\n";
1073 # Loop over rule file rules
1074 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1076 my $ruledefchecked = '';
1078 # Skip rulefile itself.
1079 next if ($sid eq "Rulefile");
1081 # If 2 rules have been displayed, start a new row
1082 if (($lines % 2) == 0) {
1083 print "</tr><tr>\n";
1085 # Increase rows by once.
1091 $col="bgcolor='$color{'color20'}'";
1093 $col="bgcolor='$color{'color22'}'";
1097 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1098 $ruledefchecked = 'CHECKED';
1101 # Create rule checkbox and display rule description
1102 print "<td class='base' width='5%' align='right' $col>\n";
1103 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1105 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1107 # Increment rule count
1111 # If do not have a second rule for row, create empty cell
1112 if (($lines % 2) != 0) {
1113 print "<td class='base'></td>";
1116 # Close display table
1117 print "</tr></table></td></tr>";
1120 # Close display table
1124 <table width='100%'>
1126 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
1127 <!-- space for future online help link -->
1134 &Header
::closebox
();
1137 &Header
::closebigbox
();
1138 &Header
::closepage
();
1141 ## A function to display a notice, to lock the webpage and
1142 ## tell the user which action currently will be performed.
1144 sub working_notice
($) {
1147 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1148 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1149 &Header
::openbox
( 'Waiting', 1,);
1153 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1158 &Header
::closebox
();
1159 &Header
::closebigbox
();
1160 &Header
::closepage
();
1164 ## A tiny function to perform a reload of the webpage after one second.
1167 print "<meta http-equiv='refresh' content='1'>\n";
1174 ## Private function to read-in and parse rules of a given rulefile.
1176 ## The given file will be read, parsed and all valid rules will be stored by ID,
1177 ## message/description and it's state in the idsrules hash.
1179 sub readrulesfile
($) {
1180 my $rulefile = shift;
1182 # Open rule file and read in contents
1183 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1185 # Store file content in an array.
1186 my @lines = <RULEFILE
>;
1191 # Loop over rule file contents
1192 foreach my $line (@lines) {
1193 # Remove whitespaces.
1197 next if ($line =~ /^\s*$/);
1203 # Gather rule sid and message from the ruleline.
1204 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1208 # Check if a rule has been found.
1210 # Add rule to the idsrules hash.
1211 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1213 # Grab status of the rule. Check if ruleline starts with a "dash".
1214 if ($line =~ /^\#/) {
1215 # If yes, the rule is disabled.
1216 $idsrules{$rulefile}{$sid}{'State'} = "off";
1218 # Otherwise the rule is enabled.
1219 $idsrules{$rulefile}{$sid}{'State'} = "on";
1227 ## Function to get the used memory of a given process-id.
1229 sub get_memory_usage
($) {
1234 # Try to open the status file for the given process-id on the pseudo
1236 if (open(FILE
, "/proc/$pid/status")) {
1237 # Loop through the entire file.
1239 # Splitt current line content and store them into variables.
1240 my ($key, $value) = split(":", $_, 2);
1242 # Check if the current key is the one which contains the memory usage.
1243 # The wanted one is VmRSS which contains the Real-memory (resident set)
1244 # of the entire process.
1245 if ($key eq "VmRSS") {
1246 # Found the memory usage add it to the memory variable.
1254 # Close file handle.
1257 # Return memory usage.
1261 # If the file could not be open, return nothing.
1266 ## Function to generate the file which contains the home net information.
1268 sub generate_home_net_file
() {
1271 # Read-in network settings.
1272 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
1274 # Get available network zones.
1275 my @network_zones = &IDS
::get_available_network_zones
();
1277 # Temporary array to store network address and prefix of the configured
1281 # Loop through the array of available network zones.
1282 foreach my $zone (@network_zones) {
1283 # Skip the red network - It never can be part to the home_net!
1284 next if($zone eq "red");
1286 # Convert current zone name into upper case.
1289 # Generate key to access the required data from the netsettings hash.
1290 my $zone_netaddress = $zone . "_NETADDRESS";
1291 my $zone_netmask = $zone . "_NETMASK";
1293 # Obtain the settings from the netsettings hash.
1294 my $netaddress = $netsettings{$zone_netaddress};
1295 my $netmask = $netsettings{$zone_netmask};
1297 # Convert the subnetmask into prefix notation.
1298 my $prefix = &Network
::convert_netmask2prefix
($netmask);
1300 # Generate full network string.
1301 my $network = join("/", $netaddress,$prefix);
1303 # Check if the network is valid.
1304 if(&Network
::check_subnet
($network)) {
1305 # Add the generated network to the array of networks.
1306 push(@networks, $network);
1310 # Format home net declaration.
1313 # Loop through the array of networks.
1314 foreach my $network (@networks) {
1315 # Add the network to the line.
1316 $line = "$line" . "$network";
1318 # Check if the current network was the last in the array.
1319 if ($network eq $networks[-1]) {
1321 $line = "$line" . "\]\"";
1323 # Add "," for the next network.
1324 $line = "$line" . "\,";
1328 # Open file to store the addresses of the home net.
1329 open(FILE
, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n";
1331 # Print yaml header.
1332 print FILE
"%YAML 1.1\n";
1333 print FILE
"---\n\n";
1335 # Print notice about autogenerated file.
1336 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1338 # Print the generated and required HOME_NET declaration to the file.
1339 print FILE
"HOME_NET:\t$line\n";
1341 # Close file handle.
1347 ## Function to generate the rules file with whitelisted addresses.
1349 sub GenerateIgnoreFile
() {
1352 # SID range 1000000-1999999 Reserved for Local Use
1353 # Put your custom rules in this range to avoid conflicts
1356 # Read-in ignoredfile.
1357 &General
::readhasharray
($ignoredfile, \
%ignored);
1359 # Open ignorefile for writing.
1360 open(FILE
, ">$whitelistfile") or die "Could not write to $whitelistfile. $!\n";
1362 # Config file header.
1363 print FILE
"# Autogenerated file.\n";
1364 print FILE
"# All user modifications will be overwritten.\n\n";
1366 # Add all user defined addresses to the whitelist.
1368 # Check if the hash contains any elements.
1369 if (keys (%ignored)) {
1370 # Loop through the entire hash and write the host/network
1371 # and remark to the ignore file.
1372 while ( (my $key) = each %ignored) {
1373 my $address = $ignored{$key}[0];
1374 my $remark = $ignored{$key}[1];
1375 my $status = $ignored{$key}[2];
1377 # Check if the status of the entry is "enabled".
1378 if ($status eq "enabled") {
1379 # Check if the address/network is valid.
1380 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
1381 # Write rule line to the file to pass any traffic from this IP
1382 print FILE
"pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n";
1395 ## Function to read-in the given enabled or disables sids file.
1397 sub read_enabled_disabled_sids_file
($) {
1400 # Temporary hash to store the sids and their state. It will be
1401 # returned at the end of this function.
1404 # Open the given filename.
1405 open(FILE
, "$file") or die "Could not open $file. $!\n";
1407 # Loop through the file.
1413 next if ($_ =~ /^\s*$/);
1416 next if ($_ =~ /^\#/);
1418 # Splitt line into sid and state part.
1419 my ($state, $sid) = split(" ", $_);
1421 # Skip line if the sid is not numeric.
1422 next unless ($sid =~ /\d+/ );
1424 # Check if the sid was enabled.
1425 if ($state eq "enablesid") {
1426 # Add the sid and its state as enabled to the temporary hash.
1427 $temphash{$sid} = "enabled";
1428 # Check if the sid was disabled.
1429 } elsif ($state eq "disablesid") {
1430 # Add the sid and its state as disabled to the temporary hash.
1431 $temphash{$sid} = "disabled";
1432 # Invalid state - skip the current sid and state.
1446 ## Private function to convert a given rulefile to a category name.
1447 ## ( No file extension anymore and if the name contained a dot, it
1448 ## would be replaced by a underline sign.)
1450 sub _rulefile_to_category
($) {
1451 my ($filename) = @_;
1453 # Splitt the filename into single chunks and store them in a
1455 my @parts = split(/\./, $filename);
1457 # Return / Remove last element of the temporary array.
1458 # This removes the file extension.
1461 # Join together the single elements of the temporary array.
1462 # If these are more than one, use a "underline" for joining.
1463 my $category = join '_', @parts;
1465 # Return the converted filename.