]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - html/cgi-bin/ids.cgi
ids.cgi: Refactor reading-in rule files.
[ipfire-2.x.git] / html / cgi-bin / ids.cgi
index 17cb23292da418f08281b186dd2659983060a022..4ece996aa0188c23bc5526bcc3fc0c86e26953b4 100644 (file)
@@ -30,10 +30,6 @@ require '/var/ipfire/general-functions.pl';
 require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
 
-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();}
-
-$a = new CGI;
-
 my %color = ();
 my %mainsettings = ();
 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
@@ -60,13 +56,12 @@ $snortsettings{'RULES'} = '';
 $snortsettings{'OINKCODE'} = '';
 $snortsettings{'INSTALLDATE'} = '';
 $snortsettings{'FILE'} = '';
-$snortsettings{'UPLOAD'} = '';
 
 &Header::getcgihash(\%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
 
 ####################### Added for snort rules control #################################
-my $snortrulepath; # change to "/etc/snort/rules" - maniac
-my @snortconfig;
+
+my $snortrulepath = "/etc/snort/rules";
 my $restartsnortrequired = 0;
 my %snortrules;
 my $rule = '';
@@ -79,177 +74,31 @@ my $linkedrulefile = '';
 my $border = '';
 my $checkboxname = '';
 
-if (-e "/etc/snort/snort.conf") {
-
-
-       # Open snort.conf file, read it in, close it, and re-open for writing
-       open(FILE, "/etc/snort/snort.conf") or die 'Unable to read snort config file.';
-       @snortconfig = <FILE>;
-       close(FILE);
-       open(FILE, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.';
-
-    my @rules = `cd /etc/snort/rules/ && ls *.rules 2>/dev/null`;    # With this loop the rule might be display with correct rulepath set
-       foreach (@rules) {
-       chomp $_;
-       my $temp = join(";",@snortconfig);
-    if ( $temp =~ /$_/ ){next;}
-    else { push(@snortconfig,"#include \$RULE_PATH/".$_);}
-       }
-
-       # Loop over each line
-       foreach my $line (@snortconfig) {
-               # Trim the line
-               chomp $line;
-
-               # Check for a line with .rules
-               if ($line =~ /\.rules$/) {
-                       # Parse out rule file name
-                       $rule = $line;
-                       $rule =~ s/\$RULE_PATH\///i;
-                       $rule =~ s/ ?include ?//i;
-                       $rule =~ s/\#//i;
-                       my $snortrulepathrule = "$snortrulepath/$rule";
-
-                       # Open rule file and read in contents
-                       open(RULEFILE, "$snortrulepath/$rule") or die "Unable to read snort rule file for reading => $snortrulepath/$rule.";
-                       my @snortrulefile = <RULEFILE>;
-                       close(RULEFILE);
-                       open(RULEFILE, ">$snortrulepath/$rule") or die "Unable to write snort rule file for writing $snortrulepath/$rule";
-
-                       # Local vars
-                       my $dashlinecnt = 0;
-                       my $desclook = 1;
-                       my $snortruledesc = '';
-                       my %snortruledef = ();
-                       my $rulecnt = 1;
-
-                       # Loop over rule file contents
-                       foreach my $ruleline (@snortrulefile) {
-                               chomp $ruleline;
-
-                               # If still looking for a description
-                               if ($desclook) {
-                                       # If line does not start with a # anymore, then done looking for a description
-                                       if ($ruleline !~ /^\#/) {
-                                               $desclook = 0;
-                                       }
-
-                                       # If see more than one dashed line, (start to) create rule file description
-                                       if ($dashlinecnt > 1) {
-                                               # Check for a line starting with a #
-                                               if ($ruleline =~ /^\#/ and $ruleline !~ /^\#alert/) {
-                                                       # Create tempruleline
-                                                       my $tempruleline = $ruleline;
-
-                                                       # Strip off # and clean up line
-                                                       $tempruleline =~ s/\# ?//i;
-
-                                                       # Check for part of a description
-                                                       if ($snortruledesc eq '') {
-                                                               $snortruledesc = $tempruleline;
-                                                       } else {
-                                                               $snortruledesc .= " $tempruleline";
-                                                       }
-                                               } else {
-                                                       # Must be done
-                                                       $desclook = 0;
-                                               }
-                                       }
-
-                                       # If have a dashed line, increment count
-                                       if ($ruleline =~ /\# ?\-+/) {
-                                               $dashlinecnt++;
-                                       }
-                               } else {
-                                       # Parse out rule file rule's message for display
-                                       if ($ruleline =~ /(msg\:\"[^\"]+\";)/) {
-                                               my $msg = '';
-                                               $msg = $1;
-                                               $msg =~ s/msg\:\"//i;
-                                               $msg =~ s/\";//i;
-                                               $snortruledef{$rulecnt}{'Description'} = $msg;
-
-                                               # Check for 'Save' and rule file displayed in query string
-                                               if (($snortsettings{'ACTION'} eq $Lang::tr{'update'}) && ($ENV{'QUERY_STRING'} =~ /$rule/i)) {
-                                                       # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
-                                                       if ((($ruleline =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"})) || (($ruleline !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}))) {
-                                                               $restartsnortrequired = 1;
-                                                       }
-
-                                                       # Strip out leading # from rule line
-                                                       $ruleline =~ s/\# ?//i;
-
-                                                       # Check if it does not exists (which means it is disabled), append a #
-                                                       if (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}) {
-                                                               $ruleline = "#"." $ruleline";
-                                                       }
-                                               }
-
-                                               # Check if ruleline does not begin with a #, so it is enabled
-                                               if ($ruleline !~ /^\#/) {
-                                                       $snortruledef{$rulecnt++}{'State'} = 'Enabled';
-                                               } else {
-                                                       # Otherwise it is disabled
-                                                       $snortruledef{$rulecnt++}{'State'} = 'Disabled';
-                                               }
-                                       }
-                               }
-
-                               # Print ruleline to RULEFILE
-                               print RULEFILE "$ruleline\n";
-                       }
-
-                       # Close RULEFILE
-                       close(RULEFILE);
-
-                       # Check for 'Save'
-                       if ($snortsettings{'ACTION'} eq $Lang::tr{'update'}) {
-                               # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
-                               if ((($line =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule"})) || (($line !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule"}))) {
-                                       $restartsnortrequired = 1;
-                               }
-
-                               # Strip out leading # from rule line
-                               $line =~ s/\# ?//i;
-
-                               # Check if it does not exists (which means it is disabled), append a #
-                               if (!exists $snortsettings{"SNORT_RULE_$rule"}) {
-                                       $line = "# $line";
-                               }
+## Grab all available snort rules and store them in the snortrules hash.
+#
+# Open snort rules directory and do a directory listing.
+opendir(DIR, $snortrulepath) or die $!;
+       # Loop through the direcory.
+       while (my $file = readdir(DIR)) {
 
-                       }
+               # We only want files.
+               next unless (-f "$snortrulepath/$file");
 
-                       # Check for rule state
-                       if ($line =~ /^\#/) {
-                               $snortrules{$rule}{"State"} = "Disabled";
-                       } else {
-                               $snortrules{$rule}{"State"} = "Enabled";
-                       }
+               # Ignore empty files.
+               next if (-z "$snortrulepath/$file");
 
-                       # Set rule description
-                       $snortrules{$rule}{"Description"} = $snortruledesc;
+               # Use a regular expression to find files ending in .rules
+               next unless ($file =~ m/\.rules$/);
 
-                       # Loop over sorted rules
-                       foreach my $ruledef (sort {$a <=> $b} keys(%snortruledef)) {
-                               $snortrules{$rule}{"Definition"}{$ruledef}{'Description'} = $snortruledef{$ruledef}{'Description'};
-                               $snortrules{$rule}{"Definition"}{$ruledef}{'State'} = $snortruledef{$ruledef}{'State'};
-                       }
+               # Ignore files which are not read-able.
+               next unless (-R "$snortrulepath/$file");
 
-                       $snortruledesc = '';
-                       print FILE "$line\n";
-               } elsif ($line =~ /var RULE_PATH/) {
-                       ($tmp, $tmp, $snortrulepath) = split(' ', $line);
-                       print FILE "$line\n";
-               } else {
-                       print FILE "$line\n";
-               }
+               # Call subfunction to read-in rulefile and add rules to
+               # the snortrules hash.
+               &readrulesfile("$file");
        }
-       close(FILE);
 
-       if ($restartsnortrequired) {
-               system('/usr/local/bin/snortctrl restart >/dev/null');
-       }
-}
+closedir(DIR);
 
 #######################  End added for snort rules control  #################################
 
@@ -307,7 +156,7 @@ if (!$errormessage) {
        # INSTALLMD5 is not in the form, so not retrieved by getcgihash
        &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
 
-       if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'} || $snortsettings{'ACTION'} eq $Lang::tr{'upload new ruleset'}) {
+       if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
                my @df = `/bin/df -B M /var`;
                foreach my $line (@df) {
                        next if $line =~ m/^Filesystem/;
@@ -324,14 +173,6 @@ if (!$errormessage) {
                                                sleep(3);
                                                $return = `cat /var/tmp/log 2>/dev/null`;
 
-                                       } elsif ( $snortsettings{'ACTION'} eq $Lang::tr{'upload new ruleset'}) {
-                                               my $upload = $a->param("UPLOAD");
-                                               open UPLOADFILE, ">/var/tmp/snortrules.tar.gz";
-                                               binmode $upload;
-                                               while ( <$upload> ) {
-                                                       print UPLOADFILE;
-                                               }
-                                               close UPLOADFILE;
                                        }
 
                                        if ($return =~ "ERROR") {
@@ -673,6 +514,12 @@ END
 &Header::closebigbox();
 &Header::closepage();
 
+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();
+}
+
 sub downloadrulesfile {
        my $peer;
        my $peerport;
@@ -692,8 +539,55 @@ sub downloadrulesfile {
        }
 
        if ($peer) {
-               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 --no-check-certificate --output-document=/var/tmp/snortrules.tar.gz $url");
+               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");
        } else {
-               system("wget -r --no-check-certificate -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
+               system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
        }
 }
+
+sub readrulesfile ($) {
+       my $rulefile = shift;
+
+       # Open rule file and read in contents
+       open(RULEFILE, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
+
+       # Store file content in an array.
+       my @lines = <RULEFILE>;
+
+       # Close file.
+       close(RULEFILE);
+
+       # Loop over rule file contents
+       foreach my $line (@lines) {
+               # Remove whitespaces.
+               chomp $line;
+
+               # Skip blank  lines.
+               next if ($line =~ /^\s*$/);
+
+               # Local vars.
+               my $sid;
+               my $msg;
+
+               # Gather rule sid and message from the ruleline.
+               if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
+                       $msg = $1;
+                       $sid = $2;
+
+                       # Check if a rule has been found.
+                       if ($sid && $msg) {
+                               # Add rule to the snortrules hash.
+                               $snortrules{$rulefile}{$sid}{'Description'} = $msg;
+
+                               # Grab status of the rule. Check if ruleline starts with a "dash".
+                               if ($line =~ /^\#/) {
+                                       # If yes, the rule is disabled.
+                                       $snortrules{$rulefile}{$sid}{'State'} = "Disabled";
+                               } else {
+                                       # Otherwise the rule is enabled.
+                                       $snortrules{$rulefile}{$sid}{'State'} = "Enabled";
+                               }
+                       }
+               }
+        }
+}