]>
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 #################################
66 my $snortrulepath = "/etc/snort/rules";
68 my $restartsnortrequired = 0;
71 my $table1colour = '';
72 my $table2colour = '';
76 my $linkedrulefile = '';
78 my $checkboxname = '';
80 # Grab all available snort rules.
83 # Open snort rules directory and do a directory listing.
84 opendir(DIR
, $snortrulepath) or die $!;
85 # Loop through the direcory.
86 while (my $file = readdir(DIR
)) {
89 next unless (-f
"$snortrulepath/$file");
92 next if (-z
"$snortrulepath/$file");
94 # Use a regular expression to find files ending in .conf
95 next unless ($file =~ m/\.rules$/);
97 # Add the file to rulecategories array.
103 if (-e
"/etc/snort/snort.conf") {
106 # Open snort.conf file, read it in, close it, and re-open for writing
107 open(FILE
, "/etc/snort/snort.conf") or die 'Unable to read snort config file.';
108 @snortconfig = <FILE
>;
110 open(FILE
, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.';
114 my $temp = join(";",@snortconfig);
115 if ( $temp =~ /$_/ ){next;}
116 else { push(@snortconfig,"#include \$RULE_PATH/".$_);}
119 # Loop over each line
120 foreach my $line (@snortconfig) {
124 # Check for a line with .rules
125 if ($line =~ /\.rules$/) {
126 # Parse out rule file name
128 $rule =~ s/\$RULE_PATH\///i
;
129 $rule =~ s/ ?include ?//i;
131 my $snortrulepathrule = "$snortrulepath/$rule";
133 # Open rule file and read in contents
134 open(RULEFILE
, "$snortrulepath/$rule") or die "Unable to read snort rule file for reading => $snortrulepath/$rule.";
135 my @snortrulefile = <RULEFILE
>;
137 open(RULEFILE
, ">$snortrulepath/$rule") or die "Unable to write snort rule file for writing $snortrulepath/$rule";
142 my $snortruledesc = '';
143 my %snortruledef = ();
146 # Loop over rule file contents
147 foreach my $ruleline (@snortrulefile) {
150 # If still looking for a description
152 # If line does not start with a # anymore, then done looking for a description
153 if ($ruleline !~ /^\#/) {
157 # If see more than one dashed line, (start to) create rule file description
158 if ($dashlinecnt > 1) {
159 # Check for a line starting with a #
160 if ($ruleline =~ /^\#/ and $ruleline !~ /^\#alert/) {
161 # Create tempruleline
162 my $tempruleline = $ruleline;
164 # Strip off # and clean up line
165 $tempruleline =~ s/\# ?//i;
167 # Check for part of a description
168 if ($snortruledesc eq '') {
169 $snortruledesc = $tempruleline;
171 $snortruledesc .= " $tempruleline";
179 # If have a dashed line, increment count
180 if ($ruleline =~ /\# ?\-+/) {
184 # Parse out rule file rule's message for display
185 if ($ruleline =~ /(msg\:\"[^\"]+\";)/) {
188 $msg =~ s/msg\:\"//i;
190 $snortruledef{$rulecnt}{'Description'} = $msg;
192 # Check for 'Save' and rule file displayed in query string
193 if (($snortsettings{'ACTION'} eq $Lang::tr
{'update'}) && ($ENV{'QUERY_STRING'} =~ /$rule/i)) {
194 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
195 if ((($ruleline =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"})) || (($ruleline !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}))) {
196 $restartsnortrequired = 1;
199 # Strip out leading # from rule line
200 $ruleline =~ s/\# ?//i;
202 # Check if it does not exists (which means it is disabled), append a #
203 if (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}) {
204 $ruleline = "#"." $ruleline";
208 # Check if ruleline does not begin with a #, so it is enabled
209 if ($ruleline !~ /^\#/) {
210 $snortruledef{$rulecnt++}{'State'} = 'Enabled';
212 # Otherwise it is disabled
213 $snortruledef{$rulecnt++}{'State'} = 'Disabled';
218 # Print ruleline to RULEFILE
219 print RULEFILE
"$ruleline\n";
226 if ($snortsettings{'ACTION'} eq $Lang::tr
{'update'}) {
227 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
228 if ((($line =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule"})) || (($line !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule"}))) {
229 $restartsnortrequired = 1;
232 # Strip out leading # from rule line
235 # Check if it does not exists (which means it is disabled), append a #
236 if (!exists $snortsettings{"SNORT_RULE_$rule"}) {
242 # Check for rule state
243 if ($line =~ /^\#/) {
244 $snortrules{$rule}{"State"} = "Disabled";
246 $snortrules{$rule}{"State"} = "Enabled";
249 # Set rule description
250 $snortrules{$rule}{"Description"} = $snortruledesc;
252 # Loop over sorted rules
253 foreach my $ruledef (sort {$a <=> $b} keys(%snortruledef)) {
254 $snortrules{$rule}{"Definition"}{$ruledef}{'Description'} = $snortruledef{$ruledef}{'Description'};
255 $snortrules{$rule}{"Definition"}{$ruledef}{'State'} = $snortruledef{$ruledef}{'State'};
259 print FILE
"$line\n";
260 } elsif ($line =~ /var RULE_PATH/) {
261 ($tmp, $tmp, $snortrulepath) = split(' ', $line);
262 print FILE
"$line\n";
264 print FILE
"$line\n";
269 if ($restartsnortrequired) {
270 system('/usr/local/bin/snortctrl restart >/dev/null');
274 ####################### End added for snort rules control #################################
276 if ($snortsettings{'OINKCODE'} ne "") {
277 $errormessage = $Lang::tr
{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
280 if (!$errormessage) {
281 if ($snortsettings{'RULES'} eq 'subscripted') {
282 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
283 } elsif ($snortsettings{'RULES'} eq 'registered') {
284 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
285 } elsif ($snortsettings{'RULES'} eq 'community') {
286 $url=" https://www.snort.org/rules/community";
288 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
291 if ($snortsettings{'ACTION'} eq $Lang::tr
{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
292 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
293 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
295 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
297 unlink "${General::swroot}/snort/enable";
299 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
301 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
303 unlink "${General::swroot}/snort/enable_green";
305 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
307 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
309 unlink "${General::swroot}/snort/enable_blue";
311 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
313 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
315 unlink "${General::swroot}/snort/enable_orange";
317 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
319 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
321 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
324 system('/usr/local/bin/snortctrl restart >/dev/null');
327 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
328 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
330 if ($snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
331 my @df = `/bin/df -B M /var`;
332 foreach my $line (@df) {
333 next if $line =~ m/^Filesystem/;
336 if ($line =~ m/dev/ ) {
337 $line =~ m/^.* (\d+)M.*$/;
338 my @temp = split(/ +/,$line);
340 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
342 if ( $snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
343 &downloadrulesfile
();
345 $return = `cat /var/tmp/log 2>/dev/null`;
349 if ($return =~ "ERROR") {
350 $errormessage = "<br /><pre>".$return."</pre>";
352 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 &");
361 $checked{'ENABLE_SNORT'}{'off'} = '';
362 $checked{'ENABLE_SNORT'}{'on'} = '';
363 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
364 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
365 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
366 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
367 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
368 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
369 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
370 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
371 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
372 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
373 $selected{'RULES'}{'nothing'} = '';
374 $selected{'RULES'}{'community'} = '';
375 $selected{'RULES'}{'emerging'} = '';
376 $selected{'RULES'}{'registered'} = '';
377 $selected{'RULES'}{'subscripted'} = '';
378 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
380 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
382 ####################### Added for snort rules control #################################
383 print "<script type='text/javascript' src='/include/snortupdateutility.js'></script>";
385 <style type="text/css">
392 background-color: $color{'color22'};
396 background-color: $color{'color20'};
399 border: double #FF0000;
400 background-color: #DCDCDC;
406 ####################### End added for snort rules control #################################
408 &Header
::openbigbox
('100%', 'left', '', $errormessage);
412 # &Header::openbox('100%', 'left', 'DEBUG');
413 # my $debugCount = 0;
414 # foreach my $line (sort keys %snortsettings) {
415 # print "$line = $snortsettings{$line}<br />\n";
418 # print " Count: $debugCount\n";
419 # &Header::closebox();
424 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
425 print "<class name='base'>$errormessage\n";
426 print " </class>\n";
430 my $return = `pidof oinkmaster.pl -x`;
433 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
437 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />
439 $Lang::tr{'snort working'}
440 <tr><td colspan='2' align='center'>
441 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
442 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
444 <tr><td colspan='2' align='left'><pre>
446 my @output = `tail -20 /var/tmp/log`;
455 &Header
::closebigbox
();
456 &Header
::closepage
();
461 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
463 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
464 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
467 if ($netsettings{'BLUE_DEV'} ne '') {
468 print " <input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
470 if ($netsettings{'ORANGE_DEV'} ne '') {
471 print " <input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
473 print " <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
481 <td><b>$Lang::tr{'ids rules update'}</b></td>
484 <td><select name='RULES'>
485 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
486 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
487 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
488 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
489 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
495 $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 />
496 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
500 <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
503 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
506 if ( -e
"/var/tmp/snortrules.tar.gz"){
507 my @Info = stat("/var/tmp/snortrules.tar.gz");
508 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
510 print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
518 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
525 if ($results ne '') {
531 ####################### Added for snort rules control #################################
532 if ( -e
"${General::swroot}/snort/enable" || -e
"${General::swroot}/snort/enable_green" || -e
"${General::swroot}/snort/enable_blue" || -e
"${General::swroot}/snort/enable_orange" ) {
533 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
534 # Output display table for rule files
535 print "<table width='100%'><tr><td valign='top'><table>";
537 print "<form method='post'>";
540 my $ruledisplaycnt = 1;
541 my $rulecnt = keys %snortrules;
543 $rulecnt = $rulecnt / 2;
545 # Loop over each rule file
546 foreach my $rulefile (sort keys(%snortrules)) {
547 my $rulechecked = '';
549 # Hide inkompatible Block rules
550 if ($rulefile =~'-BLOCK.rules') {
554 # Check if reached half-way through rule file rules to start new column
555 if ($ruledisplaycnt > $rulecnt) {
556 print "</table></td><td valign='top'><table>";
560 # Check if rule file is enabled
561 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
562 $rulechecked = 'CHECKED';
565 # Create rule file link, vars array, and display flag
566 my $rulefilelink = "?RULEFILE=$rulefile";
567 my $rulefiletoclose = '';
569 my $displayrulefilerules = 0;
571 # Check for passed in query string
572 if ($ENV{'QUERY_STRING'}) {
574 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
577 foreach $value (@queryvars) {
578 # Split out var pairs
579 ($var, $linkedrulefile) = split(/=/, $value);
581 # Check if var is 'RULEFILE'
582 if ($var eq 'RULEFILE') {
583 # Check if rulefile equals linkedrulefile
584 if ($rulefile eq $linkedrulefile) {
586 $displayrulefilerules = 1;
588 # Strip out rulefile from rulefilelink
589 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
591 # Add linked rule file to rulefilelink
592 $rulefilelink .= "&RULEFILE=$linkedrulefile";
598 # Strip out extra & & ? from rulefilelink
599 $rulefilelink =~ s/^\?\&/\?/i;
601 # Check for a single '?' and replace with page for proper link display
602 if ($rulefilelink eq '?') {
603 $rulefilelink = "ids.cgi";
606 # Output rule file name and checkbox
607 print "<tr><td class='base' valign='top'><input type='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <a href='$rulefilelink'>$rulefile</a></td></tr>";
608 print "<tr><td class='base' valign='top'>";
610 # Check for empty 'Description'
611 if ($snortrules{$rulefile}{'Description'} eq '') {
612 print "<table width='100%'><tr><td class='base'>No description available</td></tr>";
614 # Output rule file 'Description'
615 print "<table width='100%'><tr><td class='base'>$snortrules{$rulefile}{'Description'}</td></tr>";
618 # Check for display flag
619 if ($displayrulefilerules) {
620 # Rule file definition rule display
621 print "<tr><td class='base' valign='top'><table border='0'><tr>";
624 my $ruledefdisplaycnt = 0;
625 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
627 $ruledefcnt = $ruledefcnt / 2;
629 # Loop over rule file rules
630 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
632 my $ruledefchecked = '';
634 # If have display 2 rules, start new row
635 if (($ruledefdisplaycnt % 2) == 0) {
637 $ruledefdisplaycnt = 0;
640 # Check for rules state
641 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
642 $ruledefchecked = 'CHECKED';
645 # Create rule file rule's checkbox
646 $checkboxname = "SNORT_RULE_$rulefile";
647 $checkboxname .= "_$ruledef";
648 print "<td class='base'><input type='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</td>";
651 $ruledefdisplaycnt++;
654 # If do not have second rule for row, create empty cell
655 if (($ruledefdisplaycnt % 2) != 0) {
656 print "<td class='base'></td>";
659 # Close display table
660 print "</tr></table></td></tr>";
663 # Close display table
666 # Increment ruledisplaycnt
669 print "</td></tr></table></td></tr></table>";
673 <td width='100%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
674 <!-- space for future online help link -->
684 ####################### End added for snort rules control #################################
685 &Header
::closebigbox
();
686 &Header
::closepage
();
688 sub downloadrulesfile
{
692 unlink("/var/tmp/log");
694 unless (-e
"${General::swroot}/red/active") {
695 $errormessage = $Lang::tr
{'could not download latest updates'};
699 my %proxysettings=();
700 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
702 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
703 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
707 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");
709 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");