]>
git.ipfire.org Git - 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{'ACTION'} = '';
47 $snortsettings{'RULES'} = '';
48 $snortsettings{'OINKCODE'} = '';
49 $snortsettings{'INSTALLDATE'} = '';
50 $snortsettings{'INSTALLMD5'} = '';
52 &Header
::getcgihash
(\
%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
54 ####################### Added for snort rules control #################################
55 my $snortrulepath; # change to "/etc/snort/rules" - maniac
57 my $restartsnortrequired = 0;
60 my $table1colour = '';
61 my $table2colour = '';
65 my $linkedrulefile = '';
67 my $checkboxname = '';
69 if (-e
"/etc/snort/snort.conf") {
70 # Open snort.conf file, read it in, close it, and re-open for writing
71 open(FILE
, "/etc/snort/snort.conf") or die 'Unable to read snort config file.';
72 @snortconfig = <FILE
>;
74 open(FILE
, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.';
77 foreach my $line (@snortconfig) {
81 # my @rules = `ls $snortrulepath`; With this loop the rule might be display with correct rulepath set
82 # foreach my $line (@rules) {
86 # Check for a line with .rules
87 if ($line =~ /\.rules$/) {
88 # Parse out rule file name
90 $rule =~ s/\$RULE_PATH\///i
;
91 $rule =~ s/ ?include ?//i;
93 my $snortrulepathrule = "$snortrulepath/$rule";
95 # Open rule file and read in contents
96 open(RULEFILE
, "$snortrulepath/$rule") or die "Unable to read snort rule file for reading => $snortrulepath/$rule.";
97 my @snortrulefile = <RULEFILE
>;
99 open(RULEFILE
, ">$snortrulepath/$rule") or die "Unable to write snort rule file for writing $snortrulepath/$rule";
104 my $snortruledesc = '';
105 my %snortruledef = ();
108 # Loop over rule file contents
109 foreach my $ruleline (@snortrulefile) {
112 # If still looking for a description
114 # If line does not start with a # anymore, then done looking for a description
115 if ($ruleline !~ /^\#/) {
119 # If see more than one dashed line, (start to) create rule file description
120 if ($dashlinecnt > 1) {
121 # Check for a line starting with a #
122 if ($ruleline =~ /^\#/) {
123 # Create tempruleline
124 my $tempruleline = $ruleline;
126 # Strip off # and clean up line
127 $tempruleline =~ s/\# ?//i;
129 # Check for part of a description
130 if ($snortruledesc eq '') {
131 $snortruledesc = $tempruleline;
133 $snortruledesc .= " $tempruleline";
141 # If have a dashed line, increment count
142 if ($ruleline =~ /\# ?\-+/) {
146 # Parse out rule file rule's message for display
147 if ($ruleline =~ /(msg\:\"[^\"]+\";)/) {
150 $msg =~ s/msg\:\"//i;
152 $snortruledef{$rulecnt}{'Description'} = $msg;
154 # Check for 'Save' and rule file displayed in query string
155 if (($snortsettings{'ACTION'} eq $Lang::tr
{'update'}) && ($ENV{'QUERY_STRING'} =~ /$rule/i)) {
156 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
157 if ((($ruleline =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"})) || (($ruleline !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}))) {
158 $restartsnortrequired = 1;
161 # Strip out leading # from rule line
162 $ruleline =~ s/\# ?//i;
164 # Check if it does not exists (which means it is disabled), append a #
165 if (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}) {
166 $ruleline = "#"." $ruleline";
170 # Check if ruleline does not begin with a #, so it is enabled
171 if ($ruleline !~ /^\#/) {
172 $snortruledef{$rulecnt++}{'State'} = 'Enabled';
174 # Otherwise it is disabled
175 $snortruledef{$rulecnt++}{'State'} = 'Disabled';
180 # Print ruleline to RULEFILE
181 print RULEFILE
"$ruleline\n";
188 if ($snortsettings{'ACTION'} eq $Lang::tr
{'update'}) {
189 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
190 if ((($line =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule"})) || (($line !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule"}))) {
191 $restartsnortrequired = 1;
194 # Strip out leading # from rule line
197 # Check if it does not exists (which means it is disabled), append a #
198 if (!exists $snortsettings{"SNORT_RULE_$rule"}) {
203 # Check for rule state
204 if ($line =~ /^\#/) {
205 $snortrules{$rule}{"State"} = "Disabled";
207 $snortrules{$rule}{"State"} = "Enabled";
210 # Set rule description
211 $snortrules{$rule}{"Description"} = $snortruledesc;
213 # Loop over sorted rules
214 foreach my $ruledef (sort {$a <=> $b} keys(%snortruledef)) {
215 $snortrules{$rule}{"Definition"}{$ruledef}{'Description'} = $snortruledef{$ruledef}{'Description'};
216 $snortrules{$rule}{"Definition"}{$ruledef}{'State'} = $snortruledef{$ruledef}{'State'};
220 print FILE
"$line\n";
221 } elsif ($line =~ /var RULE_PATH/) {
222 ($tmp, $tmp, $snortrulepath) = split(' ', $line);
223 print FILE
"$line\n";
225 print FILE
"$line\n";
230 if ($restartsnortrequired) {
231 system('/usr/local/bin/snortctrl restart >/dev/null');
235 ####################### End added for snort rules control #################################
237 if ($snortsettings{'RULES'} eq 'subscripted') {
238 $url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-CURRENT_s.tar.gz";
239 } elsif ($snortsettings{'RULES'} eq 'registered') {
240 $url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-CURRENT.tar.gz";
242 $url="http://www.snort.org/pub-bin/downloads.cgi/Download/comm_rules/Community-Rules-CURRENT.tar.gz";
245 if ($snortsettings{'ACTION'} eq $Lang::tr
{'save'})
247 $errormessage = $Lang::tr
{'invalid input for oink code'} unless (
248 ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/) ||
249 ($snortsettings{'RULESTYPE'} eq 'nothing' ) );
251 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
252 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
254 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
256 unlink "${General::swroot}/snort/enable";
258 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
260 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
262 unlink "${General::swroot}/snort/enable_green";
264 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
266 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
268 unlink "${General::swroot}/snort/enable_blue";
270 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
272 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
274 unlink "${General::swroot}/snort/enable_orange";
277 system('/usr/local/bin/snortctrl restart >/dev/null');
280 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
281 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
284 if ($snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
286 if (($snortsettings{'INSTALLMD5'} ne $md5) && defined $md5 ) {
288 my $filename = &downloadrulesfile
();
289 if (defined $filename) {
291 $realmd5 = `/usr/bin/md5sum $filename`;
293 $realmd5 =~ s/^(\w+)\s.*$/$1/;
294 if ($md5 ne $realmd5) {
295 $errormessage = "$Lang::tr{'invalid md5sum'}";
297 $results = "<b>$Lang::tr{'installed updates'}</b>\n<pre>";
298 $results .=`/usr/local/bin/oinkmaster.pl -s -u file://$filename -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules 2>&1`;
299 $results .= "</pre>";
306 $checked{'ENABLE_SNORT'}{'off'} = '';
307 $checked{'ENABLE_SNORT'}{'on'} = '';
308 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
309 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
310 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
311 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
312 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
313 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
314 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
315 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
316 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
317 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
318 $selected{'RULES'}{'nothing'} = '';
319 $selected{'RULES'}{'community'} = '';
320 $selected{'RULES'}{'registered'} = '';
321 $selected{'RULES'}{'subscripted'} = '';
322 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
324 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
326 ####################### Added for snort rules control #################################
327 print "<SCRIPT LANGUAGE='JavaScript' SRC='/include/snortupdateutility.js'></SCRIPT>";
329 <STYLE TYPE="text/css">
336 background-color: $color{'color22'};
340 background-color: $color{'color20'};
343 border: double #FF0000;
344 background-color: #DCDCDC;
350 ####################### End added for snort rules control #################################
352 &Header
::openbigbox
('100%', 'left', '', $errormessage);
355 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
356 print "<class name='base'>$errormessage\n";
357 print " </class>\n";
361 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system2'});
363 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
365 <td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />
370 if ($netsettings{'BLUE_DEV'} ne '') {
373 <td class='base'><input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} />
379 if ($netsettings{'ORANGE_DEV'} ne '') {
382 <td class='base'><input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} />
390 <td class='base'><input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} />
397 <td><b>$Lang::tr{'ids rules update'}</b></td>
400 <td><select name='RULES'>
401 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
402 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
403 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
404 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
410 $Lang::tr{'ids rules license'} <a href='http://www.snort.org/' target='_blank'>http://www.snort.org</a>.<br />
412 $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 />
416 <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
419 <td width='30%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
423 if ($snortsettings{'INSTALLMD5'} eq $md5) {
424 print " $Lang::tr{'rules already up to date'}</td>";
426 if ( $snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'} && $md5 eq $realmd5 ) {
427 $snortsettings{'INSTALLMD5'} = $realmd5;
428 $snortsettings{'INSTALLDATE'} = `/bin/date +'%Y-%m-%d'`;
429 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
431 print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
439 <td width='55%'> </td>
440 <td width='40%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
442 <!-- space for future online help link -->
450 if ($results ne '') {
455 ####################### Added for snort rules control #################################
456 #if ( -e "${General::swroot}/snort/enable" || -e "${General::swroot}/snort/enable_green" || -e "${General::swroot}/snort/enable_blue" || -e "${General::swroot}/snort/enable_orange" ) {
457 # &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
458 # # Output display table for rule files
459 # print "<TABLE width='100%'><TR><TD VALIGN='TOP'><TABLE>";
461 # print "<form method='post'>";
464 # my $ruledisplaycnt = 1;
465 # my $rulecnt = keys %snortrules;
467 # $rulecnt = $rulecnt / 2;
469 # # Loop over each rule file
470 # foreach my $rulefile (sort keys(%snortrules)) {
471 # my $rulechecked = '';
473 # # Check if reached half-way through rule file rules to start new column
474 # if ($ruledisplaycnt > $rulecnt) {
475 # print "</TABLE></TD><TD VALIGN='TOP'><TABLE>";
476 # $ruledisplaycnt = 0;
479 # # Check if rule file is enabled
480 # if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
481 # $rulechecked = 'CHECKED';
484 # # Create rule file link, vars array, and display flag
485 # my $rulefilelink = "?RULEFILE=$rulefile";
486 # my $rulefiletoclose = '';
487 # my @queryvars = ();
488 # my $displayrulefilerules = 0;
490 # # Check for passed in query string
491 # if ($ENV{'QUERY_STRING'}) {
493 # @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
496 # foreach $value (@queryvars) {
497 # # Split out var pairs
498 # ($var, $linkedrulefile) = split(/=/, $value);
500 # # Check if var is 'RULEFILE'
501 # if ($var eq 'RULEFILE') {
502 # # Check if rulefile equals linkedrulefile
503 # if ($rulefile eq $linkedrulefile) {
505 # $displayrulefilerules = 1;
507 # # Strip out rulefile from rulefilelink
508 # $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
510 # # Add linked rule file to rulefilelink
511 # $rulefilelink .= "&RULEFILE=$linkedrulefile";
517 # # Strip out extra & & ? from rulefilelink
518 # $rulefilelink =~ s/^\?\&/\?/i;
520 # # Check for a single '?' and replace with page for proper link display
521 # if ($rulefilelink eq '?') {
522 # $rulefilelink = "ids.cgi";
525 # # Output rule file name and checkbox
526 # print "<TR><TD CLASS='base' VALIGN='TOP'><INPUT TYPE='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <A HREF='$rulefilelink'>$rulefile</A></TD></TR>";
527 # print "<TR><TD CLASS='base' VALIGN='TOP'>";
529 # # Check for empty 'Description'
530 # if ($snortrules{$rulefile}{'Description'} eq '') {
531 # print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>No description available</TD></TR>";
533 # # Output rule file 'Description'
534 # print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>$snortrules{$rulefile}{'Description'}</TD></TR>";
537 # # Check for display flag
538 # if ($displayrulefilerules) {
539 # # Rule file definition rule display
540 # print "<TR><TD CLASS='base' VALIGN='TOP'><TABLE border=1><TR>";
543 # my $ruledefdisplaycnt = 0;
544 # my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
546 # $ruledefcnt = $ruledefcnt / 2;
548 # # Loop over rule file rules
549 # foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
551 # my $ruledefchecked = '';
553 # # If have display 2 rules, start new row
554 # if (($ruledefdisplaycnt % 2) == 0) {
556 # $ruledefdisplaycnt = 0;
559 # # Check for rules state
560 # if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
561 # $ruledefchecked = 'CHECKED';
564 # # Create rule file rule's checkbox
565 # $checkboxname = "SNORT_RULE_$rulefile";
566 # $checkboxname .= "_$ruledef";
567 # print "<TD CLASS='base'><INPUT TYPE='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</TD>";
570 # $ruledefdisplaycnt++;
573 # # If do not have second rule for row, create empty cell
574 # if (($ruledefdisplaycnt % 2) != 0) {
575 # print "<TD CLASS='base'></TD>";
578 # # Close display table
579 # print "</TR></TABLE></TD></TR>";
582 # # Close display table
585 # # Increment ruledisplaycnt
589 # print "</TD></TR></TABLE></TD></TR></TABLE>";
591 #<table width='100%'>
593 # <td width='33%'> </td>
594 # <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
596 # <!-- space for future online help link -->
603 # &Header::closebox();
606 ####################### End added for snort rules control #################################
607 &Header
::closebigbox
();
608 &Header
::closepage
();
611 # Retrieve MD5 sum from $url.md5 file
613 my $md5buf = &geturl
("$url.md5");
614 return undef unless $md5buf;
616 if (0) { # 1 to debug
619 ($fh, $filename) = tempfile
('/tmp/XXXXXXXX',SUFFIX
=> '.md5' );
621 syswrite ($fh, $md5buf->content);
624 return $md5buf->content;
626 sub downloadrulesfile
{
627 my $return = &geturl
($url);
628 return undef unless $return;
630 if (index($return->content, "\037\213") == -1 ) { # \037\213 is .gz beginning
631 $errormessage = $Lang::tr
{'invalid loaded file'};
637 ($fh, $filename) = tempfile
('/tmp/XXXXXXXX',SUFFIX
=> '.tar.gz' );#oinkmaster work only with this extension
639 syswrite ($fh, $return->content);
647 unless (-e
"${General::swroot}/red/active") {
648 $errormessage = $Lang::tr
{'could not download latest updates'};
652 my $downloader = LWP
::UserAgent
->new;
653 $downloader->timeout(5);
655 my %proxysettings=();
656 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
658 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
659 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
660 if ($proxysettings{'UPSTREAM_USER'}) {
661 $downloader->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$peer:$peerport/");
663 $downloader->proxy("http","http://$peer:$peerport/");
667 my $return = $downloader->get($url,'Cache-Control','no-cache');
669 if ($return->code == 403) {
670 $errormessage = $Lang::tr
{'access refused with this oinkcode'};
672 } elsif (!$return->is_success()) {
673 $errormessage = $Lang::tr
{'could not download latest updates'};