]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2015 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 = ();
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);
48 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
50 my $snortrulepath = "/etc/snort/rules";
51 my $snortusedrulefilesfile = "${General::swroot}/snort/snort-used-rulefiles.conf";
54 &Header
::showhttpheaders
();
56 # Default settings for snort.
57 $snortsettings{'ENABLE_SNORT'} = 'off';
58 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
59 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
60 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
61 $snortsettings{'RULES'} = '';
62 $snortsettings{'OINKCODE'} = '';
65 &Header
::getcgihash
(\
%cgiparams);
67 # Try to determine if oinkmaster is running.
68 my $oinkmaster_pid = `pidof oinkmaster.pl -x`;
70 # If oinkmaster is running display output.
71 if ($oinkmaster_pid) {
72 &working
("$Lang::tr{'snort working'}");
75 # Check if any error has been stored.
76 if (-e
$IDS::storederrorfile
) {
77 # Open file to read in the stored error message.
78 open(FILE
, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
80 # Read the stored error message.
81 $errormessage = <FILE
>;
86 # Delete the file, which is now not longer required.
87 unlink($IDS::storederrorfile
);
91 ## Grab all available snort rules and store them in the snortrules hash.
93 # Open snort rules directory and do a directory listing.
94 opendir(DIR
, $snortrulepath) or die $!;
95 # Loop through the direcory.
96 while (my $file = readdir(DIR
)) {
99 next unless (-f
"$snortrulepath/$file");
101 # Ignore empty files.
102 next if (-z
"$snortrulepath/$file");
104 # Use a regular expression to find files ending in .rules
105 next unless ($file =~ m/\.rules$/);
107 # Ignore files which are not read-able.
108 next unless (-R
"$snortrulepath/$file");
110 # Call subfunction to read-in rulefile and add rules to
111 # the snortrules hash.
112 &readrulesfile
("$file");
117 # Gather used rulefiles.
119 # Check if the file for activated rulefiles is not empty.
120 if(-f
$snortusedrulefilesfile) {
121 # Open the file for used rulefile and read-in content.
122 open(FILE
, $snortusedrulefilesfile) or die "Could not open $snortusedrulefilesfile. $!\n";
130 # Loop through the array.
131 foreach my $line (@lines) {
136 next if ($line =~ /\#/);
139 next if ($line =~ /^\s*$/);
141 # Gather rule sid and message from the ruleline.
142 if ($line =~ /.*include \$RULE_PATH\/(.*)/) {
145 # Add the rulefile to the %snortrules hash.
146 $snortrules{$rulefile}{'Rulefile'}{'State'} = "on";
152 if ($cgiparams{'RULESET'} eq $Lang::tr
{'update'}) {
153 my $enabled_sids_file = "${General::swroot}/snort/oinkmaster-enabled-sids.conf";
154 my $disabled_sids_file = "${General::swroot}/snort/oinkmaster-disabled-sids.conf";
156 # Arrays to store sid which should be added to the corresponding files.
159 my @enabled_rulefiles;
161 # Loop through the hash of snortrules.
162 foreach my $rulefile(keys %snortrules) {
163 # Check if the rulefile is enabled.
164 if ($cgiparams{$rulefile} eq "on") {
165 # Add rulefile to the array of enabled rulefiles.
166 push(@enabled_rulefiles, $rulefile);
168 # Drop item from cgiparams hash.
169 delete $cgiparams{$rulefile};
173 # Loop through the hash of snortrules.
174 foreach my $rulefile (keys %snortrules) {
175 # Loop through the single rules of the rulefile.
176 foreach my $sid (keys %{$snortrules{$rulefile}}) {
177 # Skip the current sid if it is not numeric.
178 next unless ($sid =~ /\d+/ );
180 # Check if there exists a key in the cgiparams hash for this sid.
181 if (exists($cgiparams{$sid})) {
182 # Look if the rule is disabled.
183 if ($snortrules{$rulefile}{$sid}{'State'} eq "off") {
184 # Check if the state has been set to 'on'.
185 if ($cgiparams{$sid} eq "on") {
186 # Add the sid to the enabled_sids array.
187 push(@enabled_sids, $sid);
189 # Drop item from cgiparams hash.
190 delete $cgiparams{$rulefile}{$sid};
194 # Look if the rule is enabled.
195 if ($snortrules{$rulefile}{$sid}{'State'} eq "on") {
196 # Check if the state is 'on' and should be disabled.
197 # In this case there is no entry
198 # for the sid in the cgiparams hash.
199 # Add it to the disabled_sids array.
200 push(@disabled_sids, $sid);
202 # Drop item from cgiparams hash.
203 delete $cgiparams{$rulefile}{$sid};
209 # Open enabled sid's file for writing.
210 open(FILE
, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
212 # Write header to file.
213 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
215 # Check if the enabled_sids array contains any sid's.
217 # Loop through the array of enabled sids and write them to the file.
218 foreach my $sid (@enabled_sids) {
219 print FILE
"enablesid $sid\n";
223 # Close file after writing.
226 # Open disabled sid's file for writing.
227 open(FILE
, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
229 # Write header to file.
230 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
232 # Check if the enabled_sids array contains any sid's.
233 if (@disabled_sids) {
234 # Loop through the array of disabled sids and write them to the file.
235 foreach my $sid (@disabled_sids) {
236 print FILE
"disablesid $sid\n";
240 # Close file after writing.
243 # Open file for used rulefiles.
244 open (FILE
, ">$snortusedrulefilesfile") or die "Could not write to $snortusedrulefilesfile. $!\n";
246 # Write header to file.
247 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
249 # Check if the enabled_rulefiles array contains any entries.
250 if (@enabled_rulefiles) {
251 # Loop through the array of rulefiles which should be loaded and write the to the file.
252 foreach my $file (@enabled_rulefiles) {
253 print FILE
"include \$RULE_PATH/$file\n";
257 # Close file after writing.
260 # Lock the webpage and print message.
261 &working_notice
("$Lang::tr{'snort working'}");
263 # Call oinkmaster to alter the ruleset.
269 # Download new ruleset.
270 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'download new ruleset'}) {
271 # Check if the red device is active.
272 unless (-e
"${General::swroot}/red/active") {
273 $errormessage = $Lang::tr
{'could not download latest updates'};
276 # Check if enought free disk space is availabe.
277 $errormessage = &IDS
::checkdiskspace
();
279 # Check if any errors happend.
280 unless ($errormessage) {
281 # Lock the webpage and print notice about downloading
283 &working_notice
("$Lang::tr{'snort working'}");
285 # Call subfunction to download the ruleset.
286 $errormessage = &IDS
::downloadruleset
();
288 # Check if the downloader returned an error.
290 # Call function to store the errormessage.
291 &IDS
::log_error
($errormessage);
293 # Preform a reload of the page.
296 # Call subfunction to launch oinkmaster.
299 # Perform a reload of the page.
303 # Save snort settings.
304 } elsif ($cgiparams{'SNORT'} eq $Lang::tr
{'save'}) {
305 # Prevent form name from been stored in conf file.
306 delete $cgiparams{'SNORT'};
308 # Check if an oinkcode has been provided.
309 if ($cgiparams{'OINKCODE'}) {
310 # Check if the oinkcode contains unallowed chars.
311 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
312 $errormessage = $Lang::tr
{'invalid input for oink code'};
316 # Go on if there are no error messages.
317 if (!$errormessage) {
318 # Store settings into settings file.
319 &General
::writehash
("${General::swroot}/snort/settings", \
%cgiparams);
321 # Call snortctrl to restart snort
322 system('/usr/local/bin/snortctrl restart >/dev/null');
326 # Read-in snortsettings
327 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
329 $checked{'ENABLE_SNORT'}{'off'} = '';
330 $checked{'ENABLE_SNORT'}{'on'} = '';
331 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
332 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
333 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
334 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
335 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
336 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
337 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
338 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
339 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
340 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
341 $selected{'RULES'}{'nothing'} = '';
342 $selected{'RULES'}{'community'} = '';
343 $selected{'RULES'}{'emerging'} = '';
344 $selected{'RULES'}{'registered'} = '';
345 $selected{'RULES'}{'subscripted'} = '';
346 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
348 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
353 // Tiny java script function to show/hide the rules
354 // of a given category.
355 function showhide(tblname) {
356 \$("#" + tblname).toggle();
362 &Header
::openbigbox
('100%', 'left', '', $errormessage);
365 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
366 print "<class name='base'>$errormessage\n";
367 print " </class>\n";
371 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
375 # Check if a ruleset allready has been downloaded.
376 if ( -f
"$IDS::rulestarball"){
377 # Call stat on the filename to obtain detailed information.
378 my @Info = stat("$IDS::rulestarball");
380 # Grab details about the creation time.
381 $rulesdate = localtime($Info[9]);
385 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
386 <table width='100%' border='0'>
388 <td class='base' width='25%'>
389 <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'}>RED Snort
392 <td class='base' width='25%'>
393 <input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'}>GREEN Snort
396 <td class='base' width='25%'>
400 # Check if a blue device is configured.
401 if ($netsettings{'BLUE_DEV'}) {
402 print "<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} />BLUE Snort\n";
407 print "<td width='25%'>\n";
409 # Check if an orange device is configured.
410 if ($netsettings{'ORANGE_DEV'}) {
411 print "<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} />ORANGE Snort\n";
419 <td colspan='4'><br><br></td>
423 <td colspan='4'><b>$Lang::tr{'ids rules update'}</b></td>
427 <td colspan='4'><select name='RULES'>
428 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
429 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
430 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
431 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
432 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
439 <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>
440 <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>
445 <td colspan='4' nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}'></td>
449 <td colspan='4' align='left'><br>
450 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'> $Lang::tr{'updates installed'}: $rulesdate
460 <td align='right'><input type='submit' name='SNORT' value='$Lang::tr{'save'}' /></td>
469 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
470 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
472 # Output display table for rule files
473 print "<table width='100%'>\n";
475 # Local variable required for java script to show/hide
476 # rules of a rulefile.
477 my $rulesetcount = 1;
479 # Loop over each rule file
480 foreach my $rulefile (sort keys(%snortrules)) {
481 my $rulechecked = '';
483 # Check if rule file is enabled
484 if ($snortrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
485 $rulechecked = 'CHECKED';
488 # Table and rows for the rule files.
490 print"<td class='base' width='5%'>\n";
491 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
493 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
494 print"<td class='base' width='5%' align='right'>\n";
495 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
499 # Rows which will be hidden per default and will contain the single rules.
500 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
501 print"<td colspan='3'>\n";
508 # New table for the single rules.
509 print "<table width='100%'>\n";
511 # Loop over rule file rules
512 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
514 my $ruledefchecked = '';
516 # Skip rulefile itself.
517 next if ($sid eq "Rulefile");
519 # If 2 rules have been displayed, start a new row
520 if (($lines % 2) == 0) {
523 # Increase rows by once.
529 $col="bgcolor='$color{'color20'}'";
531 $col="bgcolor='$color{'color22'}'";
535 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
536 $ruledefchecked = 'CHECKED';
539 # Create rule checkbox and display rule description
540 print "<td class='base' width='5%' align='right' $col>\n";
541 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
543 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
545 # Increment rule count
549 # If do not have a second rule for row, create empty cell
550 if (($lines % 2) != 0) {
551 print "<td class='base'></td>";
554 # Close display table
555 print "</tr></table></td></tr>";
557 # Finished whith the rule file, increase count.
561 # Close display table
567 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
568 <!-- space for future online help link -->
576 &Header
::closebigbox
();
577 &Header
::closepage
();
580 ## A function to display a notice, to lock the webpage and
581 ## tell the user which action currently will be performed.
583 sub working_notice
($) {
586 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
587 &Header
::openbigbox
('100%', 'left', '', $errormessage);
588 &Header
::openbox
( 'Waiting', 1,);
592 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
598 &Header
::closebigbox
();
599 &Header
::closepage
();
603 ## A tiny function to perform a reload of the webpage after one second.
606 print "<meta http-equiv='refresh' content='1'>\n";
613 ## Private function to read-in and parse rules of a given rulefile.
615 ## The given file will be read, parsed and all valid rules will be stored by ID,
616 ## message/description and it's state in the snortrules hash.
618 sub readrulesfile
($) {
619 my $rulefile = shift;
621 # Open rule file and read in contents
622 open(RULEFILE
, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
624 # Store file content in an array.
625 my @lines = <RULEFILE
>;
630 # Loop over rule file contents
631 foreach my $line (@lines) {
632 # Remove whitespaces.
636 next if ($line =~ /^\s*$/);
642 # Gather rule sid and message from the ruleline.
643 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
647 # Check if a rule has been found.
649 # Add rule to the snortrules hash.
650 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
652 # Grab status of the rule. Check if ruleline starts with a "dash".
653 if ($line =~ /^\#/) {
654 # If yes, the rule is disabled.
655 $snortrules{$rulefile}{$sid}{'State'} = "off";
657 # Otherwise the rule is enabled.
658 $snortrules{$rulefile}{$sid}{'State'} = "on";