]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
5 # This code is distributed under the terms of the GPL
7 # (c) The SmoothWall Team
12 use File
::Temp qw
/ tempfile tempdir /;
15 # enable only the following on debugging purpose
17 use CGI
::Carp
'fatalsToBrowser';
19 require '/var/ipfire/general-functions.pl';
20 require "${General::swroot}/lang.pl";
21 require "${General::swroot}/header.pl";
24 my %mainsettings = ();
25 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
26 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
32 our $errormessage = '';
33 our $md5 = '0';# not '' to avoid displaying the wrong message when INSTALLMD5 not set
38 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
40 &Header
::showhttpheaders
();
42 $snortsettings{'ENABLE_SNORT'} = 'off';
43 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
44 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
45 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
46 $snortsettings{'ENABLE_GUARDIAN'} = 'off';
47 $snortsettings{'ACTION'} = '';
48 $snortsettings{'RULES'} = '';
49 $snortsettings{'OINKCODE'} = '';
50 $snortsettings{'INSTALLDATE'} = '';
51 $snortsettings{'INSTALLMD5'} = '';
53 &Header
::getcgihash
(\
%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
55 ####################### Added for snort rules control #################################
56 my $snortrulepath; # change to "/etc/snort/rules" - maniac
58 my $restartsnortrequired = 0;
61 my $table1colour = '';
62 my $table2colour = '';
66 my $linkedrulefile = '';
68 my $checkboxname = '';
70 if (-e
"/etc/snort/snort.conf") {
73 # Open snort.conf file, read it in, close it, and re-open for writing
74 open(FILE
, "/etc/snort/snort.conf") or die 'Unable to read snort config file.';
75 @snortconfig = <FILE
>;
77 open(FILE
, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.';
79 my @rules = `cd /etc/snort/rules/ && ls *.rules`; # With this loop the rule might be display with correct rulepath set
82 my $temp = join(";",@snortconfig);
83 if ( $temp =~ /$_/ ){next;}
84 else { push(@snortconfig,"#include \$RULE_PATH/".$_);}
88 foreach my $line (@snortconfig) {
92 # Check for a line with .rules
93 if ($line =~ /\.rules$/) {
94 # Parse out rule file name
96 $rule =~ s/\$RULE_PATH\///i
;
97 $rule =~ s/ ?include ?//i;
99 my $snortrulepathrule = "$snortrulepath/$rule";
101 # Open rule file and read in contents
102 open(RULEFILE
, "$snortrulepath/$rule") or die "Unable to read snort rule file for reading => $snortrulepath/$rule.";
103 my @snortrulefile = <RULEFILE
>;
105 open(RULEFILE
, ">$snortrulepath/$rule") or die "Unable to write snort rule file for writing $snortrulepath/$rule";
110 my $snortruledesc = '';
111 my %snortruledef = ();
114 # Loop over rule file contents
115 foreach my $ruleline (@snortrulefile) {
118 # If still looking for a description
120 # If line does not start with a # anymore, then done looking for a description
121 if ($ruleline !~ /^\#/) {
125 # If see more than one dashed line, (start to) create rule file description
126 if ($dashlinecnt > 1) {
127 # Check for a line starting with a #
128 if ($ruleline =~ /^\#/) {
129 # Create tempruleline
130 my $tempruleline = $ruleline;
132 # Strip off # and clean up line
133 $tempruleline =~ s/\# ?//i;
135 # Check for part of a description
136 if ($snortruledesc eq '') {
137 $snortruledesc = $tempruleline;
139 $snortruledesc .= " $tempruleline";
147 # If have a dashed line, increment count
148 if ($ruleline =~ /\# ?\-+/) {
152 # Parse out rule file rule's message for display
153 if ($ruleline =~ /(msg\:\"[^\"]+\";)/) {
156 $msg =~ s/msg\:\"//i;
158 $snortruledef{$rulecnt}{'Description'} = $msg;
160 # Check for 'Save' and rule file displayed in query string
161 if (($snortsettings{'ACTION'} eq $Lang::tr
{'update'}) && ($ENV{'QUERY_STRING'} =~ /$rule/i)) {
162 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
163 if ((($ruleline =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"})) || (($ruleline !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}))) {
164 $restartsnortrequired = 1;
167 # Strip out leading # from rule line
168 $ruleline =~ s/\# ?//i;
170 # Check if it does not exists (which means it is disabled), append a #
171 if (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}) {
172 $ruleline = "#"." $ruleline";
176 # Check if ruleline does not begin with a #, so it is enabled
177 if ($ruleline !~ /^\#/) {
178 $snortruledef{$rulecnt++}{'State'} = 'Enabled';
180 # Otherwise it is disabled
181 $snortruledef{$rulecnt++}{'State'} = 'Disabled';
186 # Print ruleline to RULEFILE
187 print RULEFILE
"$ruleline\n";
194 if ($snortsettings{'ACTION'} eq $Lang::tr
{'update'}) {
195 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
196 if ((($line =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule"})) || (($line !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule"}))) {
197 $restartsnortrequired = 1;
200 # Strip out leading # from rule line
203 # Check if it does not exists (which means it is disabled), append a #
204 if (!exists $snortsettings{"SNORT_RULE_$rule"}) {
209 # Check for rule state
210 if ($line =~ /^\#/) {
211 $snortrules{$rule}{"State"} = "Disabled";
213 $snortrules{$rule}{"State"} = "Enabled";
216 # Set rule description
217 $snortrules{$rule}{"Description"} = $snortruledesc;
219 # Loop over sorted rules
220 foreach my $ruledef (sort {$a <=> $b} keys(%snortruledef)) {
221 $snortrules{$rule}{"Definition"}{$ruledef}{'Description'} = $snortruledef{$ruledef}{'Description'};
222 $snortrules{$rule}{"Definition"}{$ruledef}{'State'} = $snortruledef{$ruledef}{'State'};
226 print FILE
"$line\n";
227 } elsif ($line =~ /var RULE_PATH/) {
228 ($tmp, $tmp, $snortrulepath) = split(' ', $line);
229 print FILE
"$line\n";
231 print FILE
"$line\n";
236 if ($restartsnortrequired) {
237 system('/usr/local/bin/snortctrl restart >/dev/null');
241 ####################### End added for snort rules control #################################
243 if ($snortsettings{'RULES'} eq 'subscripted') {
244 $url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-CURRENT_s.tar.gz";
245 } elsif ($snortsettings{'RULES'} eq 'registered') {
246 $url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-CURRENT.tar.gz";
248 $url="http://www.snort.org/pub-bin/downloads.cgi/Download/comm_rules/Community-Rules-CURRENT.tar.gz";
251 if ($snortsettings{'ACTION'} eq $Lang::tr
{'save'})
253 $errormessage = $Lang::tr
{'invalid input for oink code'} unless (
254 ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/) ||
255 ($snortsettings{'RULESTYPE'} eq 'nothing' ) );
257 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
258 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
260 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
262 unlink "${General::swroot}/snort/enable";
264 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
266 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
268 unlink "${General::swroot}/snort/enable_green";
270 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
272 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
274 unlink "${General::swroot}/snort/enable_blue";
276 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
278 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
280 unlink "${General::swroot}/snort/enable_orange";
282 if ($snortsettings{'ENABLE_GUARDIAN'} eq 'on')
284 system ('/usr/bin/touch', "${General::swroot}/guardian/enable");
286 unlink "${General::swroot}/guardian/enable";
289 system('/usr/local/bin/snortctrl restart >/dev/null');
292 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
293 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
296 if ($snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
298 if (($snortsettings{'INSTALLMD5'} ne $md5) && defined $md5 ) {
300 my $filename = &downloadrulesfile
();
301 if (defined $filename) {
303 $realmd5 = `/usr/bin/md5sum $filename`;
305 $realmd5 =~ s/^(\w+)\s.*$/$1/;
306 if ($md5 ne $realmd5) {
307 $errormessage = "$Lang::tr{'invalid md5sum'}";
309 $results = "<b>$Lang::tr{'installed updates'}</b>\n<pre>";
310 $results .=`/usr/local/bin/oinkmaster.pl -s -u file://$filename -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules 2>&1`;
311 $results .= "</pre>";
318 $checked{'ENABLE_SNORT'}{'off'} = '';
319 $checked{'ENABLE_SNORT'}{'on'} = '';
320 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
321 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
322 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
323 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
324 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
325 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
326 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
327 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
328 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
329 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
330 $checked{'ENABLE_GUARDIAN'}{'off'} = '';
331 $checked{'ENABLE_GUARDIAN'}{'on'} = '';
332 $checked{'ENABLE_GUARDIAN'}{$snortsettings{'ENABLE_GUARDIAN'}} = "checked='checked'";
333 $selected{'RULES'}{'nothing'} = '';
334 $selected{'RULES'}{'community'} = '';
335 $selected{'RULES'}{'registered'} = '';
336 $selected{'RULES'}{'subscripted'} = '';
337 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
339 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
341 ####################### Added for snort rules control #################################
342 print "<SCRIPT LANGUAGE='JavaScript' SRC='/include/snortupdateutility.js'></SCRIPT>";
344 <STYLE TYPE="text/css">
351 background-color: $color{'color22'};
355 background-color: $color{'color20'};
358 border: double #FF0000;
359 background-color: #DCDCDC;
365 ####################### End added for snort rules control #################################
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 system2'});
378 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
379 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
382 if ($netsettings{'BLUE_DEV'} ne '') {
383 print " <input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
385 if ($netsettings{'ORANGE_DEV'} ne '') {
386 print " <input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
388 print " <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
389 if ( -e
"/var/ipfire/guardian/guardian.conf" ) {
390 print " <input type='checkbox' name='ENABLE_GUARDIAN' $checked{'ENABLE_GUARDIAN'}{'on'} /> Guardian";
399 <td><b>$Lang::tr{'ids rules update'}</b></td>
402 <td><select name='RULES'>
403 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
404 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
405 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
406 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
412 $Lang::tr{'ids rules license'} <a href='http://www.snort.org/' target='_blank'>http://www.snort.org</a>.<br />
414 $Lang::tr{'ids rules license2'} <a href='http://www.snort.org/reg-bin/userprefs.cgi' target='_blank'>USER PREFERENCES</a>, $Lang::tr{'ids rules license3'}<br />
418 <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
421 <td width='30%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
425 if ($snortsettings{'INSTALLMD5'} eq $md5) {
426 print " $Lang::tr{'rules already up to date'}</td>";
428 if ( $snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'} && $md5 eq $realmd5 ) {
429 $snortsettings{'INSTALLMD5'} = $realmd5;
430 $snortsettings{'INSTALLDATE'} = `/bin/date +'%Y-%m-%d'`;
431 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
433 print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
441 <td width='55%'> </td>
442 <td width='40%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
444 <!-- space for future online help link -->
452 if ($results ne '') {
457 ####################### Added for snort rules control #################################
458 if ( -e
"${General::swroot}/snort/enable" || -e
"${General::swroot}/snort/enable_green" || -e
"${General::swroot}/snort/enable_blue" || -e
"${General::swroot}/snort/enable_orange" ) {
459 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
460 # Output display table for rule files
461 print "<TABLE width='100%'><TR><TD VALIGN='TOP'><TABLE>";
463 print "<form method='post'>";
466 my $ruledisplaycnt = 1;
467 my $rulecnt = keys %snortrules;
469 $rulecnt = $rulecnt / 2;
471 # Loop over each rule file
472 foreach my $rulefile (sort keys(%snortrules)) {
473 my $rulechecked = '';
475 # Check if reached half-way through rule file rules to start new column
476 if ($ruledisplaycnt > $rulecnt) {
477 print "</TABLE></TD><TD VALIGN='TOP'><TABLE>";
481 # Check if rule file is enabled
482 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
483 $rulechecked = 'CHECKED';
486 # Create rule file link, vars array, and display flag
487 my $rulefilelink = "?RULEFILE=$rulefile";
488 my $rulefiletoclose = '';
490 my $displayrulefilerules = 0;
492 # Check for passed in query string
493 if ($ENV{'QUERY_STRING'}) {
495 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
498 foreach $value (@queryvars) {
499 # Split out var pairs
500 ($var, $linkedrulefile) = split(/=/, $value);
502 # Check if var is 'RULEFILE'
503 if ($var eq 'RULEFILE') {
504 # Check if rulefile equals linkedrulefile
505 if ($rulefile eq $linkedrulefile) {
507 $displayrulefilerules = 1;
509 # Strip out rulefile from rulefilelink
510 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
512 # Add linked rule file to rulefilelink
513 $rulefilelink .= "&RULEFILE=$linkedrulefile";
519 # Strip out extra & & ? from rulefilelink
520 $rulefilelink =~ s/^\?\&/\?/i;
522 # Check for a single '?' and replace with page for proper link display
523 if ($rulefilelink eq '?') {
524 $rulefilelink = "ids.cgi";
527 # Output rule file name and checkbox
528 print "<TR><TD CLASS='base' VALIGN='TOP'><INPUT TYPE='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <A HREF='$rulefilelink'>$rulefile</A></TD></TR>";
529 print "<TR><TD CLASS='base' VALIGN='TOP'>";
531 # Check for empty 'Description'
532 if ($snortrules{$rulefile}{'Description'} eq '') {
533 print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>No description available</TD></TR>";
535 # Output rule file 'Description'
536 print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>$snortrules{$rulefile}{'Description'}</TD></TR>";
539 # Check for display flag
540 if ($displayrulefilerules) {
541 # Rule file definition rule display
542 print "<TR><TD CLASS='base' VALIGN='TOP'><TABLE border=1><TR>";
545 my $ruledefdisplaycnt = 0;
546 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
548 $ruledefcnt = $ruledefcnt / 2;
550 # Loop over rule file rules
551 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
553 my $ruledefchecked = '';
555 # If have display 2 rules, start new row
556 if (($ruledefdisplaycnt % 2) == 0) {
558 $ruledefdisplaycnt = 0;
561 # Check for rules state
562 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
563 $ruledefchecked = 'CHECKED';
566 # Create rule file rule's checkbox
567 $checkboxname = "SNORT_RULE_$rulefile";
568 $checkboxname .= "_$ruledef";
569 print "<TD CLASS='base'><INPUT TYPE='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</TD>";
572 $ruledefdisplaycnt++;
575 # If do not have second rule for row, create empty cell
576 if (($ruledefdisplaycnt % 2) != 0) {
577 print "<TD CLASS='base'></TD>";
580 # Close display table
581 print "</TR></TABLE></TD></TR>";
584 # Close display table
587 # Increment ruledisplaycnt
590 print "</TD></TR></TABLE></TD></TR></TABLE>";
594 <td width='33%'> </td>
595 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
597 <!-- space for future online help link -->
607 ####################### End added for snort rules control #################################
608 &Header
::closebigbox
();
609 &Header
::closepage
();
612 # Retrieve MD5 sum from $url.md5 file
614 my $md5buf = &geturl
("$url.md5");
615 return undef unless $md5buf;
617 if (0) { # 1 to debug
620 ($fh, $filename) = tempfile
('/tmp/XXXXXXXX',SUFFIX
=> '.md5' );
622 syswrite ($fh, $md5buf->content);
625 return $md5buf->content;
627 sub downloadrulesfile
{
628 my $return = &geturl
($url);
629 return undef unless $return;
631 if (index($return->content, "\037\213") == -1 ) { # \037\213 is .gz beginning
632 $errormessage = $Lang::tr
{'invalid loaded file'};
638 ($fh, $filename) = tempfile
('/tmp/XXXXXXXX',SUFFIX
=> '.tar.gz' );#oinkmaster work only with this extension
640 syswrite ($fh, $return->content);
648 unless (-e
"${General::swroot}/red/active") {
649 $errormessage = $Lang::tr
{'could not download latest updates'};
653 my $downloader = LWP
::UserAgent
->new;
654 $downloader->timeout(5);
656 my %proxysettings=();
657 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
659 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
660 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
661 if ($proxysettings{'UPSTREAM_USER'}) {
662 $downloader->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$peer:$peerport/");
664 $downloader->proxy("http","http://$peer:$peerport/");
668 my $return = $downloader->get($url,'Cache-Control','no-cache');
670 if ($return->code == 403) {
671 $errormessage = $Lang::tr
{'access refused with this oinkcode'};
673 } elsif (!$return->is_success()) {
674 $errormessage = $Lang::tr
{'could not download latest updates'};