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 = ();
37 my %rulesetsources = ();
43 # Read-in main settings, for language, theme and colors.
44 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
45 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
47 # Get the available network zones, based on the config type of the system and store
48 # the list of zones in an array.
49 my @network_zones = &IDS
::get_available_network_zones
();
51 # File where the used rulefiles are stored.
52 my $idsusedrulefilesfile = "$IDS::settingsdir/suricata-used-rulefiles.yaml";
54 # File where the addresses of the homenet are stored.
55 my $idshomenetfile = "$IDS::settingsdir/suricata-homenet.yaml";
57 # File which contains the enabled sids.
58 my $enabled_sids_file = "$IDS::settingsdir/oinkmaster-enabled-sids.conf";
60 # File which contains the disabled sids.
61 my $disabled_sids_file = "$IDS::settingsdir/oinkmaster-disabled-sids.conf";
63 # File which contains wheater the rules should be changed.
64 my $modify_sids_file = "$IDS::settingsdir/oinkmaster-modify-sids.conf";
66 # File which stores the configured settings for whitelisted addresses.
67 my $ignoredfile = "$IDS::settingsdir/ignored";
69 # File which contains the rules to whitelist addresses on suricata.
70 my $whitelistfile = "$IDS::rulespath/whitelist.rules";
74 # Create files if they does not exist yet.
75 unless (-f
"$enabled_sids_file") { &IDS
::create_empty_file
($enabled_sids_file); }
76 unless (-f
"$disabled_sids_file") { &IDS
::create_empty_file
($disabled_sids_file); }
77 unless (-f
"$modify_sids_file") { &IDS
::create_empty_file
($modify_sids_file); }
78 unless (-f
"$idsusedrulefilesfile") { &IDS
::create_empty_file
($idsusedrulefilesfile); }
79 unless (-f
"$ignoredfile") { &IDS
::create_empty_file
($ignoredfile); }
80 unless (-f
"$whitelistfile" ) { &IDS
::create_empty_file
($whitelistfile); }
82 &Header
::showhttpheaders
();
85 &Header
::getcgihash
(\
%cgiparams);
87 ## Add/edit an entry to the ignore file.
89 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'})) {
91 # Check if any input has been performed.
92 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
94 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
95 if ((!&General
::validip
($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
96 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
99 $errormessage = "$Lang::tr{'guardian empty input'}";
102 # Go further if there was no error.
103 if ($errormessage eq '') {
108 # Assign hash values.
109 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
110 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
112 # Read-in ignoredfile.
113 &General
::readhasharray
($ignoredfile, \
%ignored);
115 # Check if we should edit an existing entry and got an ID.
116 if (($cgiparams{'WHITELIST'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
117 # Assin the provided id.
118 $id = $cgiparams{'ID'};
120 # Undef the given ID.
121 undef($cgiparams{'ID'});
123 # Grab the configured status of the corresponding entry.
124 $status = $ignored{$id}[2];
126 # Each newly added entry automatically should be enabled.
129 # Generate the ID for the new entry.
131 # Sort the keys by their ID and store them in an array.
132 my @keys = sort { $a <=> $b } keys %ignored;
134 # Reverse the key array.
135 my @reversed = reverse(@keys);
137 # Obtain the last used id.
138 my $last_id = @reversed[0];
140 # Increase the last id by one and use it as id for the new entry.
144 # Add/Modify the entry to/in the ignored hash.
145 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
147 # Write the changed ignored hash to the ignored file.
148 &General
::writehasharray
($ignoredfile, \
%ignored);
150 # Regenerate the ignore file.
151 &GenerateIgnoreFile
();
154 # Check if the IDS is running.
155 if(&IDS
::ids_is_running
()) {
156 # Call suricatactrl to perform a reload.
157 &IDS
::call_suricatactrl
("reload");
160 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
163 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'toggle enable disable'}) {
166 # Only go further, if an ID has been passed.
167 if ($cgiparams{'ID'}) {
168 # Assign the given ID.
169 my $id = $cgiparams{'ID'};
171 # Undef the given ID.
172 undef($cgiparams{'ID'});
174 # Read-in ignoredfile.
175 &General
::readhasharray
($ignoredfile, \
%ignored);
177 # Grab the configured status of the corresponding entry.
178 my $status = $ignored{$id}[2];
181 if ($status eq "disabled") {
184 $status = "disabled";
187 # Modify the status of the existing entry.
188 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
190 # Write the changed ignored hash to the ignored file.
191 &General
::writehasharray
($ignoredfile, \
%ignored);
193 # Regenerate the ignore file.
194 &GenerateIgnoreFile
();
196 # Check if the IDS is running.
197 if(&IDS
::ids_is_running
()) {
198 # Call suricatactrl to perform a reload.
199 &IDS
::call_suricatactrl
("reload");
203 ## Remove entry from ignore list.
205 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr
{'remove'}) {
208 # Read-in ignoredfile.
209 &General
::readhasharray
($ignoredfile, \
%ignored);
211 # Drop entry from the hash.
212 delete($ignored{$cgiparams{'ID'}});
214 # Undef the given ID.
215 undef($cgiparams{'ID'});
217 # Write the changed ignored hash to the ignored file.
218 &General
::writehasharray
($ignoredfile, \
%ignored);
220 # Regenerate the ignore file.
221 &GenerateIgnoreFile
();
223 # Check if the IDS is running.
224 if(&IDS
::ids_is_running
()) {
225 # Call suricatactrl to perform a reload.
226 &IDS
::call_suricatactrl
("reload");
230 # Check if any error has been stored.
231 if (-e
$IDS::storederrorfile
) {
232 # Open file to read in the stored error message.
233 open(FILE
, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
235 # Read the stored error message.
236 $errormessage = <FILE
>;
241 # Delete the file, which is now not longer required.
242 unlink($IDS::storederrorfile
);
246 ## Grab all available snort rules and store them in the idsrules hash.
248 # Open snort rules directory and do a directory listing.
249 opendir(DIR
, $IDS::rulespath
) or die $!;
250 # Loop through the direcory.
251 while (my $file = readdir(DIR
)) {
253 # We only want files.
254 next unless (-f
"$IDS::rulespath/$file");
256 # Ignore empty files.
257 next if (-z
"$IDS::rulespath/$file");
259 # Use a regular expression to find files ending in .rules
260 next unless ($file =~ m/\.rules$/);
262 # Ignore files which are not read-able.
263 next unless (-R
"$IDS::rulespath/$file");
265 # Skip whitelist rules file.
266 next if( $file eq "whitelist.rules");
268 # Call subfunction to read-in rulefile and add rules to
270 &readrulesfile
("$file");
275 # Gather used rulefiles.
277 # Check if the file for activated rulefiles is not empty.
278 if(-f
$idsusedrulefilesfile) {
279 # Open the file for used rulefile and read-in content.
280 open(FILE
, $idsusedrulefilesfile) or die "Could not open $idsusedrulefilesfile. $!\n";
288 # Loop through the array.
289 foreach my $line (@lines) {
294 next if ($line =~ /\#/);
297 next if ($line =~ /^\s*$/);
299 # Gather rule sid and message from the ruleline.
300 if ($line =~ /.*- (.*)/) {
303 # Check if the current rulefile exists in the %idsrules hash.
304 # If not, the file probably does not exist anymore or contains
306 if($idsrules{$rulefile}) {
307 # Add the rulefile state to the %idsrules hash.
308 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
315 if ($cgiparams{'RULESET'} eq $Lang::tr
{'update'}) {
316 # Arrays to store which rulefiles have been enabled and will be used.
317 my @enabled_rulefiles;
319 # Hash to store the user-enabled and disabled sids.
320 my %enabled_disabled_sids;
322 # Loop through the hash of idsrules.
323 foreach my $rulefile(keys %idsrules) {
324 # Check if the rulefile is enabled.
325 if ($cgiparams{$rulefile} eq "on") {
326 # Add rulefile to the array of enabled rulefiles.
327 push(@enabled_rulefiles, $rulefile);
329 # Drop item from cgiparams hash.
330 delete $cgiparams{$rulefile};
334 # Read-in the files for enabled/disabled sids.
335 # This will be done by calling the read_enabled_disabled_sids_file function two times
336 # and merge the returned hashes together into the enabled_disabled_sids hash.
337 %enabled_disabled_sids = (
338 &read_enabled_disabled_sids_file
($disabled_sids_file),
339 &read_enabled_disabled_sids_file
($enabled_sids_file));
341 # Loop through the hash of idsrules.
342 foreach my $rulefile (keys %idsrules) {
343 # Loop through the single rules of the rulefile.
344 foreach my $sid (keys %{$idsrules{$rulefile}}) {
345 # Skip the current sid if it is not numeric.
346 next unless ($sid =~ /\d+/ );
348 # Check if there exists a key in the cgiparams hash for this sid.
349 if (exists($cgiparams{$sid})) {
350 # Look if the rule is disabled.
351 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
352 # Check if the state has been set to 'on'.
353 if ($cgiparams{$sid} eq "on") {
354 # Add/Modify the sid to/in the enabled_disabled_sids hash.
355 $enabled_disabled_sids{$sid} = "enabled";
357 # Drop item from cgiparams hash.
358 delete $cgiparams{$rulefile}{$sid};
362 # Look if the rule is enabled.
363 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
364 # Check if the state is 'on' and should be disabled.
365 # In this case there is no entry
366 # for the sid in the cgiparams hash.
367 # Add/Modify it to/in the enabled_disabled_sids hash.
368 $enabled_disabled_sids{$sid} = "disabled";
370 # Drop item from cgiparams hash.
371 delete $cgiparams{$rulefile}{$sid};
377 # Open enabled sid's file for writing.
378 open(ENABLED_FILE
, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
380 # Open disabled sid's file for writing.
381 open(DISABLED_FILE
, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
383 # Write header to the files.
384 print ENABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
385 print DISABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
387 # Check if the hash for enabled/disabled files contains any entries.
388 if (%enabled_disabled_sids) {
389 # Loop through the hash.
390 foreach my $sid (keys %enabled_disabled_sids) {
391 # Check if the sid is enabled.
392 if ($enabled_disabled_sids{$sid} eq "enabled") {
393 # Print the sid to the enabled_sids file.
394 print ENABLED_FILE
"enablesid $sid\n";
395 # Check if the sid is disabled.
396 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
397 # Print the sid to the disabled_sids file.
398 print DISABLED_FILE
"disablesid $sid\n";
399 # Something strange happende - skip the current sid.
406 # Close file for enabled_sids after writing.
409 # Close file for disabled_sids after writing.
410 close(DISABLED_FILE
);
412 # Open file for used rulefiles.
413 open (FILE
, ">$idsusedrulefilesfile") or die "Could not write to $idsusedrulefilesfile. $!\n";
415 # Write yaml header to the file.
416 print FILE
"%YAML 1.1\n";
417 print FILE
"---\n\n";
419 # Write header to file.
420 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
422 # Allways load the whitelist.
423 print FILE
" - whitelist.rules\n";
425 # Check if the enabled_rulefiles array contains any entries.
426 if (@enabled_rulefiles) {
427 # Loop through the array of rulefiles which should be loaded and write them to the file.
428 foreach my $file (@enabled_rulefiles) {
429 print FILE
" - $file\n";
433 # Close file after writing.
436 # Lock the webpage and print message.
437 &working_notice
("$Lang::tr{'snort working'}");
439 # Call oinkmaster to alter the ruleset.
442 # Check if the IDS is running.
443 if(&IDS
::ids_is_running
()) {
444 # Call suricatactrl to perform a reload.
445 &IDS
::call_suricatactrl
("reload");
451 # Download new ruleset.
452 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'download new ruleset'}) {
453 # Check if the red device is active.
454 unless (-e
"${General::swroot}/red/active") {
455 $errormessage = $Lang::tr
{'could not download latest updates'};
458 # Check if enought free disk space is availabe.
459 if(&IDS
::checkdiskspace
()) {
460 $errormessage = "$Lang::tr{'not enough disk space'}";
463 # Check if any errors happend.
464 unless ($errormessage) {
465 # Lock the webpage and print notice about downloading
467 &working_notice
("$Lang::tr{'snort working'}");
469 # Call subfunction to download the ruleset.
470 if(&IDS
::downloadruleset
()) {
471 $errormessage = $Lang::tr
{'could not download latest updates'};
473 # Call function to store the errormessage.
474 &IDS
::_store_error_message
($errormessage);
476 # Preform a reload of the page.
479 # Call subfunction to launch oinkmaster.
482 # Check if the IDS is running.
483 if(&IDS
::ids_is_running
()) {
484 # Call suricatactrl to perform a reload.
485 &IDS
::call_suricatactrl
("reload");
488 # Perform a reload of the page.
492 # Save snort settings.
493 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
496 my $monitored_zones = 0;
498 # Read-in current (old) IDS settings.
499 &General
::readhash
("$IDS::settingsdir/settings", \
%oldidssettings);
501 # Prevent form name from been stored in conf file.
502 delete $cgiparams{'IDS'};
504 # Check if an oinkcode has been provided.
505 if ($cgiparams{'OINKCODE'}) {
506 # Check if the oinkcode contains unallowed chars.
507 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
508 $errormessage = $Lang::tr
{'invalid input for oink code'};
512 # Check if the IDS should be enabled.
513 if ($cgiparams{'ENABLE_IDS'} eq "on") {
514 # Check if any ruleset is available. Otherwise abort and display an error.
516 $errormessage = $Lang::tr
{'ids no ruleset available'};
519 # Loop through the array of available interfaces.
520 foreach my $zone (@network_zones) {
521 # Convert interface name into upper case.
522 my $zone_upper = uc($zone);
524 # Check if the IDS is enabled for this interaces.
525 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
531 # Check if at least one zone should be monitored, or show an error.
532 unless ($monitored_zones >= 1) {
533 $errormessage = $Lang::tr
{'ids no network zone'};
537 # Go on if there are no error messages.
538 if (!$errormessage) {
539 # Store settings into settings file.
540 &General
::writehash
("$IDS::settingsdir/settings", \
%cgiparams);
543 # Generate file to store the home net.
544 &generate_home_net_file
();
546 # Check if the the automatic rule update hass been touched.
547 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldidssettings{'AUTOUPDATE_INTERVAL'}) {
548 # Call suricatactrl to set the new interval.
549 &IDS
::call_suricatactrl
("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
552 # Check if the runmode has been changed.
553 if($cgiparams{'RUN_MODE'} ne $oldidssettings{'RUN_MODE'}) {
554 # Open modify sid's file for writing.
555 open(FILE
, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
558 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
560 # Check if the configured runmode is IPS.
561 if ($cgiparams{'RUN_MODE'} eq 'IPS') {
562 # Tell oinkmaster to switch all rules from alert to drop.
563 print FILE
"modifysid \* \"alert\" \| \"drop\"\n";
569 # Check if a ruleset exists.
571 # Lock the webpage and print message.
572 &working_notice
("$Lang::tr{'snort working'}");
574 # Call oinkmaster to alter the ruleset.
577 # Set reload_page to "True".
582 # Check if the IDS currently is running.
583 if(&IDS
::ids_is_running
()) {
584 # Check if ENABLE_IDS is set to on.
585 if($cgiparams{'ENABLE_IDS'} eq "on") {
586 # Call suricatactrl to perform a reload of suricata.
587 &IDS
::call_suricatactrl
("reload");
589 # Call suricatactrl to stop suricata.
590 &IDS
::call_suricatactrl
("stop");
593 # Call suricatactrl to start suricata.
594 &IDS
::call_suricatactrl
("start");
597 # Check if the page should be reloaded.
599 # Perform a reload of the page.
604 # Read-in idssettings
605 &General
::readhash
("$IDS::settingsdir/settings", \
%idssettings);
607 # If the runmode has not been configured yet, set default value.
608 unless(exists($idssettings{'RUN_MODE'})) {
609 # Set default to IPS.
610 $idssettings{'RUN_MODE'} = 'IPS';
613 # Read-in ignored hosts.
614 &General
::readhasharray
("$IDS::settingsdir/ignored", \
%ignored);
616 $checked{'ENABLE_IDS'}{'off'} = '';
617 $checked{'ENABLE_IDS'}{'on'} = '';
618 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
619 $checked{'RUN_MODE'}{'IDS'} = '';
620 $checked{'RUN_MODE'}{'IPS'} = '';
621 $checked{'RUN_MODE'}{$idssettings{'RUN_MODE'}} = "checked='checked'";
622 $selected{'RULES'}{'nothing'} = '';
623 $selected{'RULES'}{'community'} = '';
624 $selected{'RULES'}{'emerging'} = '';
625 $selected{'RULES'}{'registered'} = '';
626 $selected{'RULES'}{'subscripted'} = '';
627 $selected{'RULES'}{$idssettings{'RULES'}} = "selected='selected'";
628 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
629 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
630 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
631 $selected{'AUTOUPDATE_INTERVAL'}{$idssettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
633 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
638 // Tiny java script function to show/hide the rules
639 // of a given category.
640 function showhide(tblname) {
641 \$("#" + tblname).toggle();
647 &Header
::openbigbox
('100%', 'left', '', $errormessage);
650 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
651 print "<class name='base'>$errormessage\n";
652 print " </class>\n";
656 # Draw current state of the IDS
657 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
659 # Check if the IDS is running and obtain the process-id.
660 my $pid = &IDS
::ids_is_running
();
662 # Display some useful information, if suricata daemon is running.
664 # Gather used memory.
665 my $memory = &get_memory_usage
($pid);
668 <table width='95%' cellspacing='0' class='tbl'>
670 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
674 <td class='base'>$Lang::tr{'guardian daemon'}</td>
675 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
679 <td class='base'></td>
680 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
681 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
685 <td class='base'></td>
686 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
687 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
692 # Otherwise display a hint that the service is not launched.
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' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
708 # Draw elements for IDS configuration.
709 &Header
::openbox
('100%', 'center', $Lang::tr
{'settings'});
713 # Check if a ruleset allready has been downloaded.
714 if ( -f
"$IDS::rulestarball"){
715 # Call stat on the filename to obtain detailed information.
716 my @Info = stat("$IDS::rulestarball");
718 # Grab details about the creation time.
719 $rulesdate = localtime($Info[9]);
723 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
724 <table width='100%' border='0'>
726 <td class='base' colspan='4'>
727 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
732 <td colspan='4'><br><br></td>
736 <td class='base' colspan='4'><b>$Lang::tr{'runmode'}</b></td>
740 <td class='base' colspan='4'>
741 <input type='radio' name='RUN_MODE' value='IDS' $checked{'RUN_MODE'}{'IDS'}>$Lang::tr{'intrusion detection system2'}    
742 <input type='radio' name='RUN_MODE' value='IPS' $checked{'RUN_MODE'}{'IPS'}>$Lang::tr{'intrusion prevention system'}
747 <td colspan='4'><br></td>
751 <td colspan='4'><b>$Lang::tr{'ids traffic analyze'}</b><br></td>
758 # Loop through the array of available networks and print config options.
759 foreach my $zone (@network_zones) {
763 # Convert current zone name to upper case.
764 my $zone_upper = uc($zone);
766 # Grab checkbox status from settings hash.
767 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
768 $checked_input = "checked = 'checked'";
771 print "<td class='base' width='25%'>\n";
772 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>$Lang::tr{'enabled on'} $Lang::tr{$zone}\n";
780 <td colspan='4'><br><br></td>
784 <td colspan='2'><b>$Lang::tr{'ids rules update'}</b></td>
785 <td colspan='2'><b>$Lang::tr{'ids automatic rules update'}</b></td>
789 <td colspan='2'><select name='RULES'>
790 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
791 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
792 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
793 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
794 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
799 <select name='AUTOUPDATE_INTERVAL'>
800 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option>
801 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option>
802 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option>
809 <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>
810 <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>
815 <td colspan='4' nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$idssettings{'OINKCODE'}'></td>
819 <td colspan='4' align='left'><br>
820 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'> $Lang::tr{'updates installed'}: $rulesdate
830 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
840 # Whitelist / Ignorelist
842 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
847 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
848 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
849 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
852 # Check if some hosts have been added to be ignored.
853 if (keys (%ignored)) {
856 # Loop through all entries of the hash.
857 while( (my $key) = each %ignored) {
858 # Assign data array positions to some nice variable names.
859 my $address = $ignored{$key}[0];
860 my $remark = $ignored{$key}[1];
861 my $status = $ignored{$key}[2];
863 # Check if the key (id) number is even or not.
864 if ($cgiparams{'ID'} eq $key) {
865 $col="bgcolor='${Header::colouryellow}'";
867 $col="bgcolor='$color{'color22'}'";
869 $col="bgcolor='$color{'color20'}'";
872 # Choose icon for the checkbox.
876 # Check if the status is enabled and select the correct image and description.
877 if ($status eq 'enabled' ) {
879 $gdesc = $Lang::tr
{'click to disable'};
882 $gdesc = $Lang::tr
{'click to enable'};
887 <td width='20%' class='base' $col>$address</td>
888 <td width='65%' class='base' $col>$remark</td>
890 <td align='center' $col>
891 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
892 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
893 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
894 <input type='hidden' name='ID' value='$key' />
898 <td align='center' $col>
899 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
900 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
901 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
902 <input type='hidden' name='ID' value='$key' />
906 <td align='center' $col>
907 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
908 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
909 <input type='hidden' name='ID' value='$key'>
910 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
917 # Print notice that currently no hosts are ignored.
919 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
925 # Section to add new elements or edit existing ones.
935 # Assign correct headline and button text.
940 # Check if an ID (key) has been given, in this case an existing entry should be edited.
941 if ($cgiparams{'ID'} ne '') {
942 $buttontext = $Lang::tr
{'update'};
943 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
945 # Grab address and remark for the given key.
946 $entry_address = $ignored{$cgiparams{'ID'}}[0];
947 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
949 $buttontext = $Lang::tr
{'add'};
950 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
954 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
955 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
957 <td width='30%'>$Lang::tr{'ip address'}: </td>
958 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
960 <td width='30%'>$Lang::tr{'remark'}: </td>
961 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
962 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
971 # Only show the section for configuring the ruleset if one is present.
973 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
975 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
977 # Output display table for rule files
978 print "<table width='100%'>\n";
980 # Loop over each rule file
981 foreach my $rulefile (sort keys(%idsrules)) {
982 my $rulechecked = '';
984 # Check if rule file is enabled
985 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
986 $rulechecked = 'CHECKED';
989 # Convert rulefile name into category name.
990 my $categoryname = &_rulefile_to_category
($rulefile);
992 # Table and rows for the rule files.
994 print"<td class='base' width='5%'>\n";
995 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
997 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
998 print"<td class='base' width='5%' align='right'>\n";
999 print"<a href=\"javascript:showhide('$categoryname')\">SHOW</a>\n";
1003 # Rows which will be hidden per default and will contain the single rules.
1004 print"<tr style='display:none' id='$categoryname'>\n";
1005 print"<td colspan='3'>\n";
1012 # New table for the single rules.
1013 print "<table width='100%'>\n";
1015 # Loop over rule file rules
1016 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1018 my $ruledefchecked = '';
1020 # Skip rulefile itself.
1021 next if ($sid eq "Rulefile");
1023 # If 2 rules have been displayed, start a new row
1024 if (($lines % 2) == 0) {
1025 print "</tr><tr>\n";
1027 # Increase rows by once.
1033 $col="bgcolor='$color{'color20'}'";
1035 $col="bgcolor='$color{'color22'}'";
1039 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1040 $ruledefchecked = 'CHECKED';
1043 # Create rule checkbox and display rule description
1044 print "<td class='base' width='5%' align='right' $col>\n";
1045 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1047 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1049 # Increment rule count
1053 # If do not have a second rule for row, create empty cell
1054 if (($lines % 2) != 0) {
1055 print "<td class='base'></td>";
1058 # Close display table
1059 print "</tr></table></td></tr>";
1062 # Close display table
1066 <table width='100%'>
1068 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
1069 <!-- space for future online help link -->
1076 &Header
::closebox
();
1079 &Header
::closebigbox
();
1080 &Header
::closepage
();
1083 ## A function to display a notice, to lock the webpage and
1084 ## tell the user which action currently will be performed.
1086 sub working_notice
($) {
1089 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
1090 &Header
::openbigbox
('100%', 'left', '', $errormessage);
1091 &Header
::openbox
( 'Waiting', 1,);
1095 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1100 &Header
::closebox
();
1101 &Header
::closebigbox
();
1102 &Header
::closepage
();
1106 ## A tiny function to perform a reload of the webpage after one second.
1109 print "<meta http-equiv='refresh' content='1'>\n";
1116 ## Private function to read-in and parse rules of a given rulefile.
1118 ## The given file will be read, parsed and all valid rules will be stored by ID,
1119 ## message/description and it's state in the idsrules hash.
1121 sub readrulesfile
($) {
1122 my $rulefile = shift;
1124 # Open rule file and read in contents
1125 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1127 # Store file content in an array.
1128 my @lines = <RULEFILE
>;
1133 # Loop over rule file contents
1134 foreach my $line (@lines) {
1135 # Remove whitespaces.
1139 next if ($line =~ /^\s*$/);
1145 # Gather rule sid and message from the ruleline.
1146 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1150 # Check if a rule has been found.
1152 # Add rule to the idsrules hash.
1153 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1155 # Grab status of the rule. Check if ruleline starts with a "dash".
1156 if ($line =~ /^\#/) {
1157 # If yes, the rule is disabled.
1158 $idsrules{$rulefile}{$sid}{'State'} = "off";
1160 # Otherwise the rule is enabled.
1161 $idsrules{$rulefile}{$sid}{'State'} = "on";
1169 ## Function to get the used memory of a given process-id.
1171 sub get_memory_usage
($) {
1176 # Try to open the status file for the given process-id on the pseudo
1178 if (open(FILE
, "/proc/$pid/status")) {
1179 # Loop through the entire file.
1181 # Splitt current line content and store them into variables.
1182 my ($key, $value) = split(":", $_, 2);
1184 # Check if the current key is the one which contains the memory usage.
1185 # The wanted one is VmRSS which contains the Real-memory (resident set)
1186 # of the entire process.
1187 if ($key eq "VmRSS") {
1188 # Found the memory usage add it to the memory variable.
1196 # Close file handle.
1199 # Return memory usage.
1203 # If the file could not be open, return nothing.
1208 ## Function to generate the file which contains the home net information.
1210 sub generate_home_net_file
() {
1213 # Read-in network settings.
1214 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
1216 # Get available network zones.
1217 my @network_zones = &IDS
::get_available_network_zones
();
1219 # Temporary array to store network address and prefix of the configured
1223 # Loop through the array of available network zones.
1224 foreach my $zone (@network_zones) {
1225 # Skip the red network - It never can be part to the home_net!
1226 next if($zone eq "red");
1228 # Convert current zone name into upper case.
1231 # Generate key to access the required data from the netsettings hash.
1232 my $zone_netaddress = $zone . "_NETADDRESS";
1233 my $zone_netmask = $zone . "_NETMASK";
1235 # Obtain the settings from the netsettings hash.
1236 my $netaddress = $netsettings{$zone_netaddress};
1237 my $netmask = $netsettings{$zone_netmask};
1239 # Convert the subnetmask into prefix notation.
1240 my $prefix = &Network
::convert_netmask2prefix
($netmask);
1242 # Generate full network string.
1243 my $network = join("/", $netaddress,$prefix);
1245 # Check if the network is valid.
1246 if(&Network
::check_subnet
($network)) {
1247 # Add the generated network to the array of networks.
1248 push(@networks, $network);
1252 # Format home net declaration.
1255 # Loop through the array of networks.
1256 foreach my $network (@networks) {
1257 # Add the network to the line.
1258 $line = "$line" . "$network";
1260 # Check if the current network was the last in the array.
1261 if ($network eq $networks[-1]) {
1263 $line = "$line" . "\]\"";
1265 # Add "," for the next network.
1266 $line = "$line" . "\,";
1270 # Open file to store the addresses of the home net.
1271 open(FILE
, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n";
1273 # Print yaml header.
1274 print FILE
"%YAML 1.1\n";
1275 print FILE
"---\n\n";
1277 # Print notice about autogenerated file.
1278 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1280 # Print the generated and required HOME_NET declaration to the file.
1281 print FILE
"HOME_NET:\t$line\n";
1283 # Close file handle.
1289 ## Function to generate the rules file with whitelisted addresses.
1291 sub GenerateIgnoreFile
() {
1294 # SID range 1000000-1999999 Reserved for Local Use
1295 # Put your custom rules in this range to avoid conflicts
1298 # Read-in ignoredfile.
1299 &General
::readhasharray
($ignoredfile, \
%ignored);
1301 # Open ignorefile for writing.
1302 open(FILE
, ">$whitelistfile") or die "Could not write to $whitelistfile. $!\n";
1304 # Config file header.
1305 print FILE
"# Autogenerated file.\n";
1306 print FILE
"# All user modifications will be overwritten.\n\n";
1308 # Add all user defined addresses to the whitelist.
1310 # Check if the hash contains any elements.
1311 if (keys (%ignored)) {
1312 # Loop through the entire hash and write the host/network
1313 # and remark to the ignore file.
1314 while ( (my $key) = each %ignored) {
1315 my $address = $ignored{$key}[0];
1316 my $remark = $ignored{$key}[1];
1317 my $status = $ignored{$key}[2];
1319 # Check if the status of the entry is "enabled".
1320 if ($status eq "enabled") {
1321 # Check if the address/network is valid.
1322 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
1323 # Write rule line to the file to pass any traffic from this IP
1324 print FILE
"pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n";
1337 ## Function to read-in the given enabled or disables sids file.
1339 sub read_enabled_disabled_sids_file
($) {
1342 # Temporary hash to store the sids and their state. It will be
1343 # returned at the end of this function.
1346 # Open the given filename.
1347 open(FILE
, "$file") or die "Could not open $file. $!\n";
1349 # Loop through the file.
1355 next if ($_ =~ /^\s*$/);
1358 next if ($_ =~ /^\#/);
1360 # Splitt line into sid and state part.
1361 my ($state, $sid) = split(" ", $_);
1363 # Skip line if the sid is not numeric.
1364 next unless ($sid =~ /\d+/ );
1366 # Check if the sid was enabled.
1367 if ($state eq "enablesid") {
1368 # Add the sid and its state as enabled to the temporary hash.
1369 $temphash{$sid} = "enabled";
1370 # Check if the sid was disabled.
1371 } elsif ($state eq "disablesid") {
1372 # Add the sid and its state as disabled to the temporary hash.
1373 $temphash{$sid} = "disabled";
1374 # Invalid state - skip the current sid and state.
1388 ## Private function to convert a given rulefile to a category name.
1389 ## ( No file extension anymore and if the name contained a dot, it
1390 ## would be replaced by a underline sign.)
1392 sub _rulefile_to_category
($) {
1393 my ($filename) = @_;
1395 # Splitt the filename into single chunks and store them in a
1397 my @parts = split(/\./, $filename);
1399 # Return / Remove last element of the temporary array.
1400 # This removes the file extension.
1403 # Join together the single elements of the temporary array.
1404 # If these are more than one, use a "underline" for joining.
1405 my $category = join '_', @parts;
1407 # Return the converted filename.