]>
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") {
72 # Open snort.conf file, read it in, close it, and re-open for writing
73 open(FILE
, "/etc/snort/snort.conf") or die 'Unable to read snort config file.';
74 @snortconfig = <FILE
>;
76 open(FILE
, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.';
78 my @rules = `cd /etc/snort/rules/ && ls *.rules`; # With this loop the rule might be display with correct rulepath set
81 my $temp = join(";",@snortconfig);
82 if ( $temp =~ /$_/ ){next;}
83 else { push(@snortconfig,"#include \$RULE_PATH/".$_);}
87 foreach my $line (@snortconfig) {
91 # Check for a line with .rules
92 if ($line =~ /\.rules$/) {
93 # Parse out rule file name
95 $rule =~ s/\$RULE_PATH\///i
;
96 $rule =~ s/ ?include ?//i;
98 my $snortrulepathrule = "$snortrulepath/$rule";
100 # Open rule file and read in contents
101 open(RULEFILE
, "$snortrulepath/$rule") or die "Unable to read snort rule file for reading => $snortrulepath/$rule.";
102 my @snortrulefile = <RULEFILE
>;
104 open(RULEFILE
, ">$snortrulepath/$rule") or die "Unable to write snort rule file for writing $snortrulepath/$rule";
109 my $snortruledesc = '';
110 my %snortruledef = ();
113 # Loop over rule file contents
114 foreach my $ruleline (@snortrulefile) {
117 # If still looking for a description
119 # If line does not start with a # anymore, then done looking for a description
120 if ($ruleline !~ /^\#/) {
124 # If see more than one dashed line, (start to) create rule file description
125 if ($dashlinecnt > 1) {
126 # Check for a line starting with a #
127 if ($ruleline =~ /^\#/) {
128 # Create tempruleline
129 my $tempruleline = $ruleline;
131 # Strip off # and clean up line
132 $tempruleline =~ s/\# ?//i;
134 # Check for part of a description
135 if ($snortruledesc eq '') {
136 $snortruledesc = $tempruleline;
138 $snortruledesc .= " $tempruleline";
146 # If have a dashed line, increment count
147 if ($ruleline =~ /\# ?\-+/) {
151 # Parse out rule file rule's message for display
152 if ($ruleline =~ /(msg\:\"[^\"]+\";)/) {
155 $msg =~ s/msg\:\"//i;
157 $snortruledef{$rulecnt}{'Description'} = $msg;
159 # Check for 'Save' and rule file displayed in query string
160 if (($snortsettings{'ACTION'} eq $Lang::tr
{'update'}) && ($ENV{'QUERY_STRING'} =~ /$rule/i)) {
161 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
162 if ((($ruleline =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"})) || (($ruleline !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}))) {
163 $restartsnortrequired = 1;
166 # Strip out leading # from rule line
167 $ruleline =~ s/\# ?//i;
169 # Check if it does not exists (which means it is disabled), append a #
170 if (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}) {
171 $ruleline = "#"." $ruleline";
175 # Check if ruleline does not begin with a #, so it is enabled
176 if ($ruleline !~ /^\#/) {
177 $snortruledef{$rulecnt++}{'State'} = 'Enabled';
179 # Otherwise it is disabled
180 $snortruledef{$rulecnt++}{'State'} = 'Disabled';
185 # Print ruleline to RULEFILE
186 print RULEFILE
"$ruleline\n";
193 if ($snortsettings{'ACTION'} eq $Lang::tr
{'update'}) {
194 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
195 if ((($line =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule"})) || (($line !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule"}))) {
196 $restartsnortrequired = 1;
199 # Strip out leading # from rule line
202 # Check if it does not exists (which means it is disabled), append a #
203 if (!exists $snortsettings{"SNORT_RULE_$rule"}) {
208 # Check for rule state
209 if ($line =~ /^\#/) {
210 $snortrules{$rule}{"State"} = "Disabled";
212 $snortrules{$rule}{"State"} = "Enabled";
215 # Set rule description
216 $snortrules{$rule}{"Description"} = $snortruledesc;
218 # Loop over sorted rules
219 foreach my $ruledef (sort {$a <=> $b} keys(%snortruledef)) {
220 $snortrules{$rule}{"Definition"}{$ruledef}{'Description'} = $snortruledef{$ruledef}{'Description'};
221 $snortrules{$rule}{"Definition"}{$ruledef}{'State'} = $snortruledef{$ruledef}{'State'};
225 print FILE
"$line\n";
226 } elsif ($line =~ /var RULE_PATH/) {
227 ($tmp, $tmp, $snortrulepath) = split(' ', $line);
228 print FILE
"$line\n";
230 print FILE
"$line\n";
235 if ($restartsnortrequired) {
236 system('/usr/local/bin/snortctrl restart >/dev/null');
240 ####################### End added for snort rules control #################################
242 if ($snortsettings{'RULES'} eq 'subscripted') {
243 $url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-CURRENT_s.tar.gz";
244 } elsif ($snortsettings{'RULES'} eq 'registered') {
245 $url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-CURRENT.tar.gz";
247 $url="http://www.snort.org/pub-bin/downloads.cgi/Download/comm_rules/Community-Rules-CURRENT.tar.gz";
250 if ($snortsettings{'ACTION'} eq $Lang::tr
{'save'})
252 $errormessage = $Lang::tr
{'invalid input for oink code'} unless (
253 ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/) ||
254 ($snortsettings{'RULESTYPE'} eq 'nothing' ) );
256 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
257 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
259 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
261 unlink "${General::swroot}/snort/enable";
263 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
265 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
267 unlink "${General::swroot}/snort/enable_green";
269 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
271 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
273 unlink "${General::swroot}/snort/enable_blue";
275 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
277 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
279 unlink "${General::swroot}/snort/enable_orange";
282 system('/usr/local/bin/snortctrl restart >/dev/null');
285 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
286 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
289 if ($snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
291 if (($snortsettings{'INSTALLMD5'} ne $md5) && defined $md5 ) {
293 my $filename = &downloadrulesfile
();
294 if (defined $filename) {
296 $realmd5 = `/usr/bin/md5sum $filename`;
298 $realmd5 =~ s/^(\w+)\s.*$/$1/;
299 if ($md5 ne $realmd5) {
300 $errormessage = "$Lang::tr{'invalid md5sum'}";
302 $results = "<b>$Lang::tr{'installed updates'}</b>\n<pre>";
303 $results .=`/usr/local/bin/oinkmaster.pl -s -u file://$filename -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules 2>&1`;
304 $results .= "</pre>";
311 $checked{'ENABLE_SNORT'}{'off'} = '';
312 $checked{'ENABLE_SNORT'}{'on'} = '';
313 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
314 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
315 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
316 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
317 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
318 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
319 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
320 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
321 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
322 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
323 $selected{'RULES'}{'nothing'} = '';
324 $selected{'RULES'}{'community'} = '';
325 $selected{'RULES'}{'registered'} = '';
326 $selected{'RULES'}{'subscripted'} = '';
327 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
329 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
331 ####################### Added for snort rules control #################################
332 print "<SCRIPT LANGUAGE='JavaScript' SRC='/include/snortupdateutility.js'></SCRIPT>";
334 <STYLE TYPE="text/css">
341 background-color: $color{'color22'};
345 background-color: $color{'color20'};
348 border: double #FF0000;
349 background-color: #DCDCDC;
355 ####################### End added for snort rules control #################################
357 &Header
::openbigbox
('100%', 'left', '', $errormessage);
360 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
361 print "<class name='base'>$errormessage\n";
362 print " </class>\n";
366 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system2'});
368 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
370 <td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />
375 if ($netsettings{'BLUE_DEV'} ne '') {
378 <td class='base'><input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} />
384 if ($netsettings{'ORANGE_DEV'} ne '') {
387 <td class='base'><input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} />
395 <td class='base'><input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} />
402 <td><b>$Lang::tr{'ids rules update'}</b></td>
405 <td><select name='RULES'>
406 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
407 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
408 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
409 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
415 $Lang::tr{'ids rules license'} <a href='http://www.snort.org/' target='_blank'>http://www.snort.org</a>.<br />
417 $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 />
421 <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
424 <td width='30%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
428 if ($snortsettings{'INSTALLMD5'} eq $md5) {
429 print " $Lang::tr{'rules already up to date'}</td>";
431 if ( $snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'} && $md5 eq $realmd5 ) {
432 $snortsettings{'INSTALLMD5'} = $realmd5;
433 $snortsettings{'INSTALLDATE'} = `/bin/date +'%Y-%m-%d'`;
434 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
436 print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
444 <td width='55%'> </td>
445 <td width='40%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
447 <!-- space for future online help link -->
455 if ($results ne '') {
460 ####################### Added for snort rules control #################################
461 if ( -e
"${General::swroot}/snort/enable" || -e
"${General::swroot}/snort/enable_green" || -e
"${General::swroot}/snort/enable_blue" || -e
"${General::swroot}/snort/enable_orange" ) {
462 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
463 # Output display table for rule files
464 print "<TABLE width='100%'><TR><TD VALIGN='TOP'><TABLE>";
466 print "<form method='post'>";
469 my $ruledisplaycnt = 1;
470 my $rulecnt = keys %snortrules;
472 $rulecnt = $rulecnt / 2;
474 # Loop over each rule file
475 foreach my $rulefile (sort keys(%snortrules)) {
476 my $rulechecked = '';
478 # Check if reached half-way through rule file rules to start new column
479 if ($ruledisplaycnt > $rulecnt) {
480 print "</TABLE></TD><TD VALIGN='TOP'><TABLE>";
484 # Check if rule file is enabled
485 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
486 $rulechecked = 'CHECKED';
489 # Create rule file link, vars array, and display flag
490 my $rulefilelink = "?RULEFILE=$rulefile";
491 my $rulefiletoclose = '';
493 my $displayrulefilerules = 0;
495 # Check for passed in query string
496 if ($ENV{'QUERY_STRING'}) {
498 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
501 foreach $value (@queryvars) {
502 # Split out var pairs
503 ($var, $linkedrulefile) = split(/=/, $value);
505 # Check if var is 'RULEFILE'
506 if ($var eq 'RULEFILE') {
507 # Check if rulefile equals linkedrulefile
508 if ($rulefile eq $linkedrulefile) {
510 $displayrulefilerules = 1;
512 # Strip out rulefile from rulefilelink
513 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
515 # Add linked rule file to rulefilelink
516 $rulefilelink .= "&RULEFILE=$linkedrulefile";
522 # Strip out extra & & ? from rulefilelink
523 $rulefilelink =~ s/^\?\&/\?/i;
525 # Check for a single '?' and replace with page for proper link display
526 if ($rulefilelink eq '?') {
527 $rulefilelink = "ids.cgi";
530 # Output rule file name and checkbox
531 print "<TR><TD CLASS='base' VALIGN='TOP'><INPUT TYPE='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <A HREF='$rulefilelink'>$rulefile</A></TD></TR>";
532 print "<TR><TD CLASS='base' VALIGN='TOP'>";
534 # Check for empty 'Description'
535 if ($snortrules{$rulefile}{'Description'} eq '') {
536 print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>No description available</TD></TR>";
538 # Output rule file 'Description'
539 print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>$snortrules{$rulefile}{'Description'}</TD></TR>";
542 # Check for display flag
543 if ($displayrulefilerules) {
544 # Rule file definition rule display
545 print "<TR><TD CLASS='base' VALIGN='TOP'><TABLE border=1><TR>";
548 my $ruledefdisplaycnt = 0;
549 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
551 $ruledefcnt = $ruledefcnt / 2;
553 # Loop over rule file rules
554 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
556 my $ruledefchecked = '';
558 # If have display 2 rules, start new row
559 if (($ruledefdisplaycnt % 2) == 0) {
561 $ruledefdisplaycnt = 0;
564 # Check for rules state
565 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
566 $ruledefchecked = 'CHECKED';
569 # Create rule file rule's checkbox
570 $checkboxname = "SNORT_RULE_$rulefile";
571 $checkboxname .= "_$ruledef";
572 print "<TD CLASS='base'><INPUT TYPE='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</TD>";
575 $ruledefdisplaycnt++;
578 # If do not have second rule for row, create empty cell
579 if (($ruledefdisplaycnt % 2) != 0) {
580 print "<TD CLASS='base'></TD>";
583 # Close display table
584 print "</TR></TABLE></TD></TR>";
587 # Close display table
590 # Increment ruledisplaycnt
593 print "</TD></TR></TABLE></TD></TR></TABLE>";
597 <td width='33%'> </td>
598 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
600 <!-- space for future online help link -->
610 ####################### End added for snort rules control #################################
611 &Header
::closebigbox
();
612 &Header
::closepage
();
615 # Retrieve MD5 sum from $url.md5 file
617 my $md5buf = &geturl
("$url.md5");
618 return undef unless $md5buf;
620 if (0) { # 1 to debug
623 ($fh, $filename) = tempfile
('/tmp/XXXXXXXX',SUFFIX
=> '.md5' );
625 syswrite ($fh, $md5buf->content);
628 return $md5buf->content;
630 sub downloadrulesfile
{
631 my $return = &geturl
($url);
632 return undef unless $return;
634 if (index($return->content, "\037\213") == -1 ) { # \037\213 is .gz beginning
635 $errormessage = $Lang::tr
{'invalid loaded file'};
641 ($fh, $filename) = tempfile
('/tmp/XXXXXXXX',SUFFIX
=> '.tar.gz' );#oinkmaster work only with this extension
643 syswrite ($fh, $return->content);
651 unless (-e
"${General::swroot}/red/active") {
652 $errormessage = $Lang::tr
{'could not download latest updates'};
656 my $downloader = LWP
::UserAgent
->new;
657 $downloader->timeout(5);
659 my %proxysettings=();
660 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
662 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
663 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
664 if ($proxysettings{'UPSTREAM_USER'}) {
665 $downloader->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$peer:$peerport/");
667 $downloader->proxy("http","http://$peer:$peerport/");
671 my $return = $downloader->get($url,'Cache-Control','no-cache');
673 if ($return->code == 403) {
674 $errormessage = $Lang::tr
{'access refused with this oinkcode'};
676 } elsif (!$return->is_success()) {
677 $errormessage = $Lang::tr
{'could not download latest updates'};