]>
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';
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
34 my %mainsettings = ();
42 # Read-in main settings, for language, theme and colors.
43 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
44 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
47 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
49 &Header
::showhttpheaders
();
51 # Default settings for snort.
52 $snortsettings{'ENABLE_SNORT'} = 'off';
53 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
54 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
55 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
56 $snortsettings{'RULES'} = '';
57 $snortsettings{'OINKCODE'} = '';
58 $snortsettings{'INSTALLDATE'} = '';
61 &Header
::getcgihash
(\
%cgiparams);
63 my $snortrulepath = "/etc/snort/rules";
64 my $snortusedrulefilesfile = "${General::swroot}/snort/snort-used-rulefiles.conf";
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 ## Grab all available snort rules and store them in the snortrules hash.
77 # Open snort rules directory and do a directory listing.
78 opendir(DIR
, $snortrulepath) or die $!;
79 # Loop through the direcory.
80 while (my $file = readdir(DIR
)) {
83 next unless (-f
"$snortrulepath/$file");
86 next if (-z
"$snortrulepath/$file");
88 # Use a regular expression to find files ending in .rules
89 next unless ($file =~ m/\.rules$/);
91 # Ignore files which are not read-able.
92 next unless (-R
"$snortrulepath/$file");
94 # Call subfunction to read-in rulefile and add rules to
95 # the snortrules hash.
96 &readrulesfile
("$file");
101 # Gather used rulefiles.
103 # Check if the file for activated rulefiles is not empty.
104 if(-f
$snortusedrulefilesfile) {
105 # Open the file for used rulefile and read-in content.
106 open(FILE
, $snortusedrulefilesfile) or die "Could not open $snortusedrulefilesfile. $!\n";
114 # Loop through the array.
115 foreach my $line (@lines) {
120 next if ($line =~ /\#/);
123 next if ($line =~ /^\s*$/);
125 # Gather rule sid and message from the ruleline.
126 if ($line =~ /.*include \$RULE_PATH\/(.*)/) {
129 # Add the rulefile to the %snortrules hash.
130 $snortrules{$rulefile}{'Rulefile'}{'State'} = "on";
136 if ($cgiparams{'RULESET'} eq $Lang::tr
{'update'}) {
137 my $enabled_sids_file = "${General::swroot}/snort/oinkmaster-enabled-sids.conf";
138 my $disabled_sids_file = "${General::swroot}/snort/oinkmaster-disabled-sids.conf";
140 # Arrays to store sid which should be added to the corresponding files.
143 my @enabled_rulefiles;
145 # Loop through the hash of snortrules.
146 foreach my $rulefile(keys %snortrules) {
147 # Check if the rulefile is enabled.
148 if ($cgiparams{$rulefile} eq "on") {
149 # Add rulefile to the array of enabled rulefiles.
150 push(@enabled_rulefiles, $rulefile);
152 # Drop item from cgiparams hash.
153 delete $cgiparams{$rulefile};
157 # Loop through the hash of snortrules.
158 foreach my $rulefile (keys %snortrules) {
159 # Loop through the single rules of the rulefile.
160 foreach my $sid (keys %{$snortrules{$rulefile}}) {
161 # Skip the current sid if it is not numeric.
162 next unless ($sid =~ /\d+/ );
164 # Check if there exists a key in the cgiparams hash for this sid.
165 if (exists($cgiparams{$sid})) {
166 # Look if the rule is disabled.
167 if ($snortrules{$rulefile}{$sid}{'State'} eq "off") {
168 # Check if the state has been set to 'on'.
169 if ($cgiparams{$sid} eq "on") {
170 # Add the sid to the enabled_sids array.
171 push(@enabled_sids, $sid);
173 # Drop item from cgiparams hash.
174 delete $cgiparams{$rulefile}{$sid};
178 # Look if the rule is enabled.
179 if ($snortrules{$rulefile}{$sid}{'State'} eq "on") {
180 # Check if the state is 'on' and should be disabled.
181 # In this case there is no entry
182 # for the sid in the cgiparams hash.
183 # Add it to the disabled_sids array.
184 push(@disabled_sids, $sid);
186 # Drop item from cgiparams hash.
187 delete $cgiparams{$rulefile}{$sid};
193 # Open enabled sid's file for writing.
194 open(FILE
, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
196 # Write header to file.
197 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
199 # Check if the enabled_sids array contains any sid's.
201 # Loop through the array of enabled sids and write them to the file.
202 foreach my $sid (@enabled_sids) {
203 print FILE
"enablesid $sid\n";
207 # Close file after writing.
210 # Open disabled sid's file for writing.
211 open(FILE
, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
213 # Write header to file.
214 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
216 # Check if the enabled_sids array contains any sid's.
217 if (@disabled_sids) {
218 # Loop through the array of disabled sids and write them to the file.
219 foreach my $sid (@disabled_sids) {
220 print FILE
"disablesid $sid\n";
224 # Close file after writing.
227 # Open file for used rulefiles.
228 open (FILE
, ">$snortusedrulefilesfile") or die "Could not write to $snortusedrulefilesfile. $!\n";
230 # Write header to file.
231 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
233 # Check if the enabled_rulefiles array contains any entries.
234 if (@enabled_rulefiles) {
235 # Loop through the array of rulefiles which should be loaded and write the to the file.
236 foreach my $file (@enabled_rulefiles) {
237 print FILE
"include \$RULE_PATH/$file\n";
241 # Close file after writing.
244 # Call oinkmaster to alter the ruleset.
247 # Download new ruleset.
248 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'download new ruleset'}) {
252 # Call diskfree to gather the free disk space of /var.
253 my @df = `/bin/df -B M /var`;
255 # Loop through the output.
256 foreach my $line (@df) {
257 # Ignore header line.
258 next if $line =~ m/^Filesystem/;
260 # Search for a line with the device information.
261 if ($line =~ m/dev/ ) {
262 # Split the line into single pieces.
263 my @values = split(' ', $line);
264 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
266 # Check if the available disk space is more than 300MB.
267 if ($available < 300) {
268 # If there is not enough space, print out an error message.
269 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
271 # Call subfunction to download the ruleset.
272 &downloadrulesfile
();
274 # Sleep for 3 seconds.
277 # Gather return of the external wget.
278 $return = `cat /var/tmp/log 2>/dev/null`;
281 # Check if there was an error.
282 if ($return =~ "ERROR") {
283 # Store error message for display.
284 $errormessage = "<br /><pre>".$return."</pre>";
287 unlink("/var/tmp/log");
289 # Call subfunction to launch oinkmaster.
292 # Sleep for 2 seconds.
297 # Save snort settings.
298 } elsif ($cgiparams{'SNORT'} eq $Lang::tr
{'save'}) {
299 # Prevent form name from been stored in conf file.
300 delete $cgiparams{'SNORT'};
302 # Check if an oinkcode has been provided.
303 if ($cgiparams{'OINKCODE'}) {
304 # Check if the oinkcode contains unallowed chars.
305 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
306 $errormessage = $Lang::tr
{'invalid input for oink code'};
310 # Go on if there are no error messages.
311 if (!$errormessage) {
312 # Store settings into settings file.
313 &General
::writehash
("${General::swroot}/snort/settings", \
%cgiparams);
315 # Create/Remove control files for snort.
316 if ($snortsettings{'ENABLE_SNORT'} eq 'on') {
317 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
319 unlink "${General::swroot}/snort/enable";
322 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on') {
323 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
325 unlink "${General::swroot}/snort/enable_green";
328 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on') {
329 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
331 unlink "${General::swroot}/snort/enable_blue";
334 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on') {
335 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
337 unlink "${General::swroot}/snort/enable_orange";
340 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on') {
341 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
343 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
346 # Call snortctrl to restart snort
347 system('/usr/local/bin/snortctrl restart >/dev/null');
351 # Read-in snortsettings
352 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
354 $checked{'ENABLE_SNORT'}{'off'} = '';
355 $checked{'ENABLE_SNORT'}{'on'} = '';
356 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
357 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
358 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
359 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
360 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
361 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
362 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
363 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
364 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
365 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
366 $selected{'RULES'}{'nothing'} = '';
367 $selected{'RULES'}{'community'} = '';
368 $selected{'RULES'}{'emerging'} = '';
369 $selected{'RULES'}{'registered'} = '';
370 $selected{'RULES'}{'subscripted'} = '';
371 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
373 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
378 // Tiny java script function to show/hide the rules
379 // of a given category.
380 function showhide(tblname) {
381 \$("#" + tblname).toggle();
387 &Header
::openbigbox
('100%', 'left', '', $errormessage);
390 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
391 print "<class name='base'>$errormessage\n";
392 print " </class>\n";
396 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
398 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
399 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
402 if ($netsettings{'BLUE_DEV'} ne '') {
403 print " <input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
405 if ($netsettings{'ORANGE_DEV'} ne '') {
406 print " <input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
408 print " <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
416 <td><b>$Lang::tr{'ids rules update'}</b></td>
419 <td><select name='RULES'>
420 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
421 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
422 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
423 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
424 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
430 $Lang::tr{'ids rules license'} <a href='https://www.snort.org/subscribe' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}<br /><br />
431 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
435 <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
438 <td width='30%' align='left'><br><input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}' />
441 if ( -e
"/var/tmp/snortrules.tar.gz"){
442 my @Info = stat("/var/tmp/snortrules.tar.gz");
443 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
445 print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
453 <td align='right'><input type='submit' name='SNORT' value='$Lang::tr{'save'}' /></td>
462 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
463 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
465 # Output display table for rule files
466 print "<table width='100%'>\n";
468 # Local variable required for java script to show/hide
469 # rules of a rulefile.
470 my $rulesetcount = 1;
472 # Loop over each rule file
473 foreach my $rulefile (sort keys(%snortrules)) {
474 my $rulechecked = '';
476 # Check if rule file is enabled
477 if ($snortrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
478 $rulechecked = 'CHECKED';
481 # Table and rows for the rule files.
483 print"<td class='base' width='5%'>\n";
484 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
486 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
487 print"<td class='base' width='5%' align='right'>\n";
488 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
492 # Rows which will be hidden per default and will contain the single rules.
493 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
494 print"<td colspan='3'>\n";
501 # New table for the single rules.
502 print "<table width='100%'>\n";
504 # Loop over rule file rules
505 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
507 my $ruledefchecked = '';
509 # Skip rulefile itself.
510 next if ($sid eq "Rulefile");
512 # If 2 rules have been displayed, start a new row
513 if (($lines % 2) == 0) {
516 # Increase rows by once.
522 $col="bgcolor='$color{'color20'}'";
524 $col="bgcolor='$color{'color22'}'";
528 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
529 $ruledefchecked = 'CHECKED';
532 # Create rule checkbox and display rule description
533 print "<td class='base' width='5%' align='right' $col>\n";
534 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
536 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
538 # Increment rule count
542 # If do not have a second rule for row, create empty cell
543 if (($lines % 2) != 0) {
544 print "<td class='base'></td>";
547 # Close display table
548 print "</tr></table></td></tr>";
550 # Finished whith the rule file, increase count.
554 # Close display table
560 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
561 <!-- space for future online help link -->
569 &Header
::closebigbox
();
570 &Header
::closepage
();
575 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
576 &Header
::openbigbox
('100%', 'left', '', $errormessage);
577 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='1'>" );
581 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
585 <td colspan='2' align='center'>
586 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
587 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
593 &Header
::closebigbox
();
594 &Header
::closepage
();
598 sub downloadrulesfile
{
602 unlink("/var/tmp/log");
604 unless (-e
"${General::swroot}/red/active") {
605 $errormessage = $Lang::tr
{'could not download latest updates'};
609 # Gather snort settings.
610 my %snortsettings = ();
611 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
613 # Get all available ruleset locations.
615 &General
::readhash
("${General::swroot}/snort/ruleset-sources.list", \
%urls);
617 # Grab the right url based on the configured vendor.
618 my $url = $urls{$snortsettings{'RULES'}};
620 # Check and pass oinkcode if the vendor requires one.
621 $url =~ s/\<oinkcode\>/$snortsettings{'OINKCODE'}/g;
623 # Abort if no url could be determined for the vendor.
625 $errormessage = $Lang::tr
{'could not download latest updates'};
629 my %proxysettings=();
630 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
632 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
633 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
637 system("wget -r --proxy=on --proxy-user=$proxysettings{'UPSTREAM_USER'} --proxy-passwd=$proxysettings{'UPSTREAM_PASSWORD'} -e http_proxy=http://$peer:$peerport/ -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
639 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
644 # Call oinkmaster to generate ruleset.
645 system("/usr/local/bin/oinkmaster.pl -v -s -u file:///var/tmp/snortrules.tar.gz -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules >>/var/tmp/log 2>&1 &");
648 sub readrulesfile
($) {
649 my $rulefile = shift;
651 # Open rule file and read in contents
652 open(RULEFILE
, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
654 # Store file content in an array.
655 my @lines = <RULEFILE
>;
660 # Loop over rule file contents
661 foreach my $line (@lines) {
662 # Remove whitespaces.
666 next if ($line =~ /^\s*$/);
672 # Gather rule sid and message from the ruleline.
673 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
677 # Check if a rule has been found.
679 # Add rule to the snortrules hash.
680 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
682 # Grab status of the rule. Check if ruleline starts with a "dash".
683 if ($line =~ /^\#/) {
684 # If yes, the rule is disabled.
685 $snortrules{$rulefile}{$sid}{'State'} = "off";
687 # Otherwise the rule is enabled.
688 $snortrules{$rulefile}{$sid}{'State'} = "on";