]>
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";
33 sub refreshpage
{&Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";&Header
::closebox
();}
36 my %mainsettings = ();
37 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
38 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
44 our $errormessage = '';
48 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
50 &Header
::showhttpheaders
();
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{'ACTION'} = '';
57 $snortsettings{'RULES'} = '';
58 $snortsettings{'OINKCODE'} = '';
59 $snortsettings{'INSTALLDATE'} = '';
60 $snortsettings{'FILE'} = '';
62 &Header
::getcgihash
(\
%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
64 ####################### Added for snort rules control #################################
65 my $snortrulepath; # change to "/etc/snort/rules" - maniac
67 my $restartsnortrequired = 0;
70 my $table1colour = '';
71 my $table2colour = '';
75 my $linkedrulefile = '';
77 my $checkboxname = '';
79 if (-e
"/etc/snort/snort.conf") {
82 # Open snort.conf file, read it in, close it, and re-open for writing
83 open(FILE
, "/etc/snort/snort.conf") or die 'Unable to read snort config file.';
84 @snortconfig = <FILE
>;
86 open(FILE
, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.';
88 my @rules = `cd /etc/snort/rules/ && ls *.rules 2>/dev/null`; # With this loop the rule might be display with correct rulepath set
91 my $temp = join(";",@snortconfig);
92 if ( $temp =~ /$_/ ){next;}
93 else { push(@snortconfig,"#include \$RULE_PATH/".$_);}
97 foreach my $line (@snortconfig) {
101 # Check for a line with .rules
102 if ($line =~ /\.rules$/) {
103 # Parse out rule file name
105 $rule =~ s/\$RULE_PATH\///i
;
106 $rule =~ s/ ?include ?//i;
108 my $snortrulepathrule = "$snortrulepath/$rule";
110 # Open rule file and read in contents
111 open(RULEFILE
, "$snortrulepath/$rule") or die "Unable to read snort rule file for reading => $snortrulepath/$rule.";
112 my @snortrulefile = <RULEFILE
>;
114 open(RULEFILE
, ">$snortrulepath/$rule") or die "Unable to write snort rule file for writing $snortrulepath/$rule";
119 my $snortruledesc = '';
120 my %snortruledef = ();
123 # Loop over rule file contents
124 foreach my $ruleline (@snortrulefile) {
127 # If still looking for a description
129 # If line does not start with a # anymore, then done looking for a description
130 if ($ruleline !~ /^\#/) {
134 # If see more than one dashed line, (start to) create rule file description
135 if ($dashlinecnt > 1) {
136 # Check for a line starting with a #
137 if ($ruleline =~ /^\#/ and $ruleline !~ /^\#alert/) {
138 # Create tempruleline
139 my $tempruleline = $ruleline;
141 # Strip off # and clean up line
142 $tempruleline =~ s/\# ?//i;
144 # Check for part of a description
145 if ($snortruledesc eq '') {
146 $snortruledesc = $tempruleline;
148 $snortruledesc .= " $tempruleline";
156 # If have a dashed line, increment count
157 if ($ruleline =~ /\# ?\-+/) {
161 # Parse out rule file rule's message for display
162 if ($ruleline =~ /(msg\:\"[^\"]+\";)/) {
165 $msg =~ s/msg\:\"//i;
167 $snortruledef{$rulecnt}{'Description'} = $msg;
169 # Check for 'Save' and rule file displayed in query string
170 if (($snortsettings{'ACTION'} eq $Lang::tr
{'update'}) && ($ENV{'QUERY_STRING'} =~ /$rule/i)) {
171 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
172 if ((($ruleline =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"})) || (($ruleline !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}))) {
173 $restartsnortrequired = 1;
176 # Strip out leading # from rule line
177 $ruleline =~ s/\# ?//i;
179 # Check if it does not exists (which means it is disabled), append a #
180 if (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}) {
181 $ruleline = "#"." $ruleline";
185 # Check if ruleline does not begin with a #, so it is enabled
186 if ($ruleline !~ /^\#/) {
187 $snortruledef{$rulecnt++}{'State'} = 'Enabled';
189 # Otherwise it is disabled
190 $snortruledef{$rulecnt++}{'State'} = 'Disabled';
195 # Print ruleline to RULEFILE
196 print RULEFILE
"$ruleline\n";
203 if ($snortsettings{'ACTION'} eq $Lang::tr
{'update'}) {
204 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
205 if ((($line =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule"})) || (($line !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule"}))) {
206 $restartsnortrequired = 1;
209 # Strip out leading # from rule line
212 # Check if it does not exists (which means it is disabled), append a #
213 if (!exists $snortsettings{"SNORT_RULE_$rule"}) {
219 # Check for rule state
220 if ($line =~ /^\#/) {
221 $snortrules{$rule}{"State"} = "Disabled";
223 $snortrules{$rule}{"State"} = "Enabled";
226 # Set rule description
227 $snortrules{$rule}{"Description"} = $snortruledesc;
229 # Loop over sorted rules
230 foreach my $ruledef (sort {$a <=> $b} keys(%snortruledef)) {
231 $snortrules{$rule}{"Definition"}{$ruledef}{'Description'} = $snortruledef{$ruledef}{'Description'};
232 $snortrules{$rule}{"Definition"}{$ruledef}{'State'} = $snortruledef{$ruledef}{'State'};
236 print FILE
"$line\n";
237 } elsif ($line =~ /var RULE_PATH/) {
238 ($tmp, $tmp, $snortrulepath) = split(' ', $line);
239 print FILE
"$line\n";
241 print FILE
"$line\n";
246 if ($restartsnortrequired) {
247 system('/usr/local/bin/snortctrl restart >/dev/null');
251 ####################### End added for snort rules control #################################
253 if ($snortsettings{'OINKCODE'} ne "") {
254 $errormessage = $Lang::tr
{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
257 if (!$errormessage) {
258 if ($snortsettings{'RULES'} eq 'subscripted') {
259 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
260 } elsif ($snortsettings{'RULES'} eq 'registered') {
261 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
262 } elsif ($snortsettings{'RULES'} eq 'community') {
263 $url=" https://www.snort.org/rules/community";
265 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
268 if ($snortsettings{'ACTION'} eq $Lang::tr
{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
269 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
270 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
272 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
274 unlink "${General::swroot}/snort/enable";
276 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
278 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
280 unlink "${General::swroot}/snort/enable_green";
282 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
284 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
286 unlink "${General::swroot}/snort/enable_blue";
288 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
290 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
292 unlink "${General::swroot}/snort/enable_orange";
294 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
296 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
298 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
301 system('/usr/local/bin/snortctrl restart >/dev/null');
304 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
305 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
307 if ($snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
308 my @df = `/bin/df -B M /var`;
309 foreach my $line (@df) {
310 next if $line =~ m/^Filesystem/;
313 if ($line =~ m/dev/ ) {
314 $line =~ m/^.* (\d+)M.*$/;
315 my @temp = split(/ +/,$line);
317 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
319 if ( $snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
320 &downloadrulesfile
();
322 $return = `cat /var/tmp/log 2>/dev/null`;
326 if ($return =~ "ERROR") {
327 $errormessage = "<br /><pre>".$return."</pre>";
329 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 &");
338 $checked{'ENABLE_SNORT'}{'off'} = '';
339 $checked{'ENABLE_SNORT'}{'on'} = '';
340 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
341 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
342 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
343 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
344 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
345 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
346 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
347 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
348 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
349 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
350 $selected{'RULES'}{'nothing'} = '';
351 $selected{'RULES'}{'community'} = '';
352 $selected{'RULES'}{'emerging'} = '';
353 $selected{'RULES'}{'registered'} = '';
354 $selected{'RULES'}{'subscripted'} = '';
355 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
357 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
359 ####################### Added for snort rules control #################################
360 print "<script type='text/javascript' src='/include/snortupdateutility.js'></script>";
362 <style type="text/css">
369 background-color: $color{'color22'};
373 background-color: $color{'color20'};
376 border: double #FF0000;
377 background-color: #DCDCDC;
383 ####################### End added for snort rules control #################################
385 &Header
::openbigbox
('100%', 'left', '', $errormessage);
389 # &Header::openbox('100%', 'left', 'DEBUG');
390 # my $debugCount = 0;
391 # foreach my $line (sort keys %snortsettings) {
392 # print "$line = $snortsettings{$line}<br />\n";
395 # print " Count: $debugCount\n";
396 # &Header::closebox();
401 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
402 print "<class name='base'>$errormessage\n";
403 print " </class>\n";
407 my $return = `pidof oinkmaster.pl -x`;
410 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
414 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />
416 $Lang::tr{'snort working'}
417 <tr><td colspan='2' align='center'>
418 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
419 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
421 <tr><td colspan='2' align='left'><pre>
423 my @output = `tail -20 /var/tmp/log`;
432 &Header
::closebigbox
();
433 &Header
::closepage
();
438 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
440 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
441 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
444 if ($netsettings{'BLUE_DEV'} ne '') {
445 print " <input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
447 if ($netsettings{'ORANGE_DEV'} ne '') {
448 print " <input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
450 print " <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
458 <td><b>$Lang::tr{'ids rules update'}</b></td>
461 <td><select name='RULES'>
462 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
463 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
464 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
465 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
466 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
472 $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 />
473 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
477 <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
480 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
483 if ( -e
"/var/tmp/snortrules.tar.gz"){
484 my @Info = stat("/var/tmp/snortrules.tar.gz");
485 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
487 print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
495 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
502 if ($results ne '') {
508 ####################### Added for snort rules control #################################
509 if ( -e
"${General::swroot}/snort/enable" || -e
"${General::swroot}/snort/enable_green" || -e
"${General::swroot}/snort/enable_blue" || -e
"${General::swroot}/snort/enable_orange" ) {
510 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
511 # Output display table for rule files
512 print "<table width='100%'><tr><td valign='top'><table>";
514 print "<form method='post'>";
517 my $ruledisplaycnt = 1;
518 my $rulecnt = keys %snortrules;
520 $rulecnt = $rulecnt / 2;
522 # Loop over each rule file
523 foreach my $rulefile (sort keys(%snortrules)) {
524 my $rulechecked = '';
526 # Hide inkompatible Block rules
527 if ($rulefile =~'-BLOCK.rules') {
531 # Check if reached half-way through rule file rules to start new column
532 if ($ruledisplaycnt > $rulecnt) {
533 print "</table></td><td valign='top'><table>";
537 # Check if rule file is enabled
538 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
539 $rulechecked = 'CHECKED';
542 # Create rule file link, vars array, and display flag
543 my $rulefilelink = "?RULEFILE=$rulefile";
544 my $rulefiletoclose = '';
546 my $displayrulefilerules = 0;
548 # Check for passed in query string
549 if ($ENV{'QUERY_STRING'}) {
551 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
554 foreach $value (@queryvars) {
555 # Split out var pairs
556 ($var, $linkedrulefile) = split(/=/, $value);
558 # Check if var is 'RULEFILE'
559 if ($var eq 'RULEFILE') {
560 # Check if rulefile equals linkedrulefile
561 if ($rulefile eq $linkedrulefile) {
563 $displayrulefilerules = 1;
565 # Strip out rulefile from rulefilelink
566 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
568 # Add linked rule file to rulefilelink
569 $rulefilelink .= "&RULEFILE=$linkedrulefile";
575 # Strip out extra & & ? from rulefilelink
576 $rulefilelink =~ s/^\?\&/\?/i;
578 # Check for a single '?' and replace with page for proper link display
579 if ($rulefilelink eq '?') {
580 $rulefilelink = "ids.cgi";
583 # Output rule file name and checkbox
584 print "<tr><td class='base' valign='top'><input type='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <a href='$rulefilelink'>$rulefile</a></td></tr>";
585 print "<tr><td class='base' valign='top'>";
587 # Check for empty 'Description'
588 if ($snortrules{$rulefile}{'Description'} eq '') {
589 print "<table width='100%'><tr><td class='base'>No description available</td></tr>";
591 # Output rule file 'Description'
592 print "<table width='100%'><tr><td class='base'>$snortrules{$rulefile}{'Description'}</td></tr>";
595 # Check for display flag
596 if ($displayrulefilerules) {
597 # Rule file definition rule display
598 print "<tr><td class='base' valign='top'><table border='0'><tr>";
601 my $ruledefdisplaycnt = 0;
602 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
604 $ruledefcnt = $ruledefcnt / 2;
606 # Loop over rule file rules
607 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
609 my $ruledefchecked = '';
611 # If have display 2 rules, start new row
612 if (($ruledefdisplaycnt % 2) == 0) {
614 $ruledefdisplaycnt = 0;
617 # Check for rules state
618 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
619 $ruledefchecked = 'CHECKED';
622 # Create rule file rule's checkbox
623 $checkboxname = "SNORT_RULE_$rulefile";
624 $checkboxname .= "_$ruledef";
625 print "<td class='base'><input type='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</td>";
628 $ruledefdisplaycnt++;
631 # If do not have second rule for row, create empty cell
632 if (($ruledefdisplaycnt % 2) != 0) {
633 print "<td class='base'></td>";
636 # Close display table
637 print "</tr></table></td></tr>";
640 # Close display table
643 # Increment ruledisplaycnt
646 print "</td></tr></table></td></tr></table>";
650 <td width='100%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
651 <!-- space for future online help link -->
661 ####################### End added for snort rules control #################################
662 &Header
::closebigbox
();
663 &Header
::closepage
();
665 sub downloadrulesfile
{
669 unlink("/var/tmp/log");
671 unless (-e
"${General::swroot}/red/active") {
672 $errormessage = $Lang::tr
{'could not download latest updates'};
676 my %proxysettings=();
677 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
679 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
680 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
684 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");
686 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");