]>
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 # Call oinkmaster to alter the ruleset.
263 # Download new ruleset.
264 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'download new ruleset'}) {
265 # Check if the red device is active.
266 unless (-e
"${General::swroot}/red/active") {
267 $errormessage = $Lang::tr
{'could not download latest updates'};
270 # Check if enought free disk space is availabe.
271 $errormessage = &IDS
::checkdiskspace
();
273 # Check if any errors happend.
274 unless ($errormessage) {
275 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
276 &Header
::openbigbox
('100%', 'left', '', $errormessage);
277 &Header
::openbox
( 'Waiting', 1,);
281 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
282 <td>$Lang::tr{'snort working'}</td>
287 &Header
::closebigbox
();
288 &Header
::closepage
();
290 # Call subfunction to download the ruleset.
291 $errormessage = &IDS
::downloadruleset
();
293 # Check if the downloader returned an error.
295 # Call function to store the errormessage.
296 &IDS
::log_error
($errormessage);
298 # Preform a reload of the page.
301 # Call subfunction to launch oinkmaster.
304 # Perform a reload of the page.
308 # Save snort settings.
309 } elsif ($cgiparams{'SNORT'} eq $Lang::tr
{'save'}) {
310 # Prevent form name from been stored in conf file.
311 delete $cgiparams{'SNORT'};
313 # Check if an oinkcode has been provided.
314 if ($cgiparams{'OINKCODE'}) {
315 # Check if the oinkcode contains unallowed chars.
316 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
317 $errormessage = $Lang::tr
{'invalid input for oink code'};
321 # Go on if there are no error messages.
322 if (!$errormessage) {
323 # Store settings into settings file.
324 &General
::writehash
("${General::swroot}/snort/settings", \
%cgiparams);
326 # Call snortctrl to restart snort
327 system('/usr/local/bin/snortctrl restart >/dev/null');
331 # Read-in snortsettings
332 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
334 $checked{'ENABLE_SNORT'}{'off'} = '';
335 $checked{'ENABLE_SNORT'}{'on'} = '';
336 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
337 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
338 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
339 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
340 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
341 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
342 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
343 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
344 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
345 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
346 $selected{'RULES'}{'nothing'} = '';
347 $selected{'RULES'}{'community'} = '';
348 $selected{'RULES'}{'emerging'} = '';
349 $selected{'RULES'}{'registered'} = '';
350 $selected{'RULES'}{'subscripted'} = '';
351 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
353 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
358 // Tiny java script function to show/hide the rules
359 // of a given category.
360 function showhide(tblname) {
361 \$("#" + tblname).toggle();
367 &Header
::openbigbox
('100%', 'left', '', $errormessage);
370 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
371 print "<class name='base'>$errormessage\n";
372 print " </class>\n";
376 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
380 # Check if a ruleset allready has been downloaded.
381 if ( -f
"$IDS::rulestarball"){
382 # Call stat on the filename to obtain detailed information.
383 my @Info = stat("$IDS::rulestarball");
385 # Grab details about the creation time.
386 $rulesdate = localtime($Info[9]);
390 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
391 <table width='100%' border='0'>
393 <td class='base' width='25%'>
394 <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'}>RED Snort
397 <td class='base' width='25%'>
398 <input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'}>GREEN Snort
401 <td class='base' width='25%'>
405 # Check if a blue device is configured.
406 if ($netsettings{'BLUE_DEV'}) {
407 print "<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} />BLUE Snort\n";
412 print "<td width='25%'>\n";
414 # Check if an orange device is configured.
415 if ($netsettings{'ORANGE_DEV'}) {
416 print "<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} />ORANGE Snort\n";
424 <td colspan='4'><br><br></td>
428 <td colspan='4'><b>$Lang::tr{'ids rules update'}</b></td>
432 <td colspan='4'><select name='RULES'>
433 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
434 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
435 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
436 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
437 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
444 <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>
445 <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>
450 <td colspan='4' nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}'></td>
454 <td colspan='4' align='left'><br>
455 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'> $Lang::tr{'updates installed'}: $rulesdate
465 <td align='right'><input type='submit' name='SNORT' value='$Lang::tr{'save'}' /></td>
474 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
475 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
477 # Output display table for rule files
478 print "<table width='100%'>\n";
480 # Local variable required for java script to show/hide
481 # rules of a rulefile.
482 my $rulesetcount = 1;
484 # Loop over each rule file
485 foreach my $rulefile (sort keys(%snortrules)) {
486 my $rulechecked = '';
488 # Check if rule file is enabled
489 if ($snortrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
490 $rulechecked = 'CHECKED';
493 # Table and rows for the rule files.
495 print"<td class='base' width='5%'>\n";
496 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
498 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
499 print"<td class='base' width='5%' align='right'>\n";
500 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
504 # Rows which will be hidden per default and will contain the single rules.
505 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
506 print"<td colspan='3'>\n";
513 # New table for the single rules.
514 print "<table width='100%'>\n";
516 # Loop over rule file rules
517 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
519 my $ruledefchecked = '';
521 # Skip rulefile itself.
522 next if ($sid eq "Rulefile");
524 # If 2 rules have been displayed, start a new row
525 if (($lines % 2) == 0) {
528 # Increase rows by once.
534 $col="bgcolor='$color{'color20'}'";
536 $col="bgcolor='$color{'color22'}'";
540 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
541 $ruledefchecked = 'CHECKED';
544 # Create rule checkbox and display rule description
545 print "<td class='base' width='5%' align='right' $col>\n";
546 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
548 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
550 # Increment rule count
554 # If do not have a second rule for row, create empty cell
555 if (($lines % 2) != 0) {
556 print "<td class='base'></td>";
559 # Close display table
560 print "</tr></table></td></tr>";
562 # Finished whith the rule file, increase count.
566 # Close display table
572 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
573 <!-- space for future online help link -->
581 &Header
::closebigbox
();
582 &Header
::closepage
();
585 ## A tiny function to perform a reload of the webpage after one second.
588 print "<meta http-equiv='refresh' content='1'>\n";
595 ## Private function to read-in and parse rules of a given rulefile.
597 ## The given file will be read, parsed and all valid rules will be stored by ID,
598 ## message/description and it's state in the snortrules hash.
600 sub readrulesfile
($) {
601 my $rulefile = shift;
603 # Open rule file and read in contents
604 open(RULEFILE
, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
606 # Store file content in an array.
607 my @lines = <RULEFILE
>;
612 # Loop over rule file contents
613 foreach my $line (@lines) {
614 # Remove whitespaces.
618 next if ($line =~ /^\s*$/);
624 # Gather rule sid and message from the ruleline.
625 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
629 # Check if a rule has been found.
631 # Add rule to the snortrules hash.
632 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
634 # Grab status of the rule. Check if ruleline starts with a "dash".
635 if ($line =~ /^\#/) {
636 # If yes, the rule is disabled.
637 $snortrules{$rulefile}{$sid}{'State'} = "off";
639 # Otherwise the rule is enabled.
640 $snortrules{$rulefile}{$sid}{'State'} = "on";