]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - config/cfgroot/ids-functions.pl
ids-functions.pl: Use If-Modified-Since header to reduce file downloads.
[people/pmueller/ipfire-2.x.git] / config / cfgroot / ids-functions.pl
index 5b92edabecff9123693402cc92eea580a1632b25..61aecc250bba2ce1ab806582c39c7f18ed831926 100644 (file)
@@ -29,6 +29,36 @@ require '/var/ipfire/general-functions.pl';
 require "${General::swroot}/network-functions.pl";
 require "${General::swroot}/suricata/ruleset-sources";
 
+# Load perl module to deal with Archives.
+use Archive::Tar;
+
+# Load perl module to deal with files and path.
+use File::Basename;
+
+# Load module to move files.
+use File::Copy;
+
+# Load module to recursely remove files and a folder.
+use File::Path qw(rmtree);
+
+# Load module to get file stats.
+use File::stat;
+
+# Load module to deal with temporary files.
+use File::Temp;
+
+# Load module to deal with the date formats used by the HTTP protocol.
+use HTTP::Date;
+
+# Load the libwwwperl User Agent module.
+use LWP::UserAgent;
+
+# Load function from posix module to format time strings.
+use POSIX qw (strftime);
+
+# Load module to talk to the kernel log daemon.
+use Sys::Syslog qw(:DEFAULT setlogsock);
+
 # Location where all config and settings files are stored.
 our $settingsdir = "${General::swroot}/suricata";
 
@@ -36,7 +66,7 @@ our $settingsdir = "${General::swroot}/suricata";
 our $suricata_used_providers_file = "$settingsdir/suricata-used-providers.yaml";
 
 # File for static ruleset inclusions.
-our $suricata_static_rulefiles_file = "$settingsdir/suricata-static-included-rulefiles.yaml";
+our $suricata_default_rulefiles_file = "$settingsdir/suricata-default-rules.yaml";
 
 # File where the addresses of the homenet are stored.
 our $homenet_file = "$settingsdir/suricata-homenet.yaml";
@@ -74,8 +104,14 @@ our $ids_page_lock_file = "/tmp/ids_page_locked";
 # Location where the rulefiles are stored.
 our $rulespath = "/var/lib/suricata";
 
+# Location where the default rulefils are stored.
+our $default_rulespath = "/usr/share/suricata/rules";
+
+# Location where the addition config files are stored.
+our $configspath = "/usr/share/suricata";
+
 # Location of the classification file.
-our $classification_file = "$rulespath/classification.config";
+our $classification_file = "$configspath/classification.config";
 
 # Location of the sid to msg mappings file.
 our $sid_msg_file = "$rulespath/sid-msg.map";
@@ -119,7 +155,10 @@ my @cron_intervals = ('off', 'daily', 'weekly' );
 my @http_ports = ('80', '81');
 
 # Array which contains a list of rulefiles which always will be included if they exist.
-my @static_included_rulefiles = ('local.rules', 'whitelist.rules' );
+my @static_included_rulefiles = ('local.rules', 'whitelist.rules');
+
+# Array which contains a list of allways enabled application layer protocols.
+my @static_enabled_app_layer_protos = ('app-layer', 'decoder', 'files', 'stream');
 
 # Hash which allows to convert the download type (dl_type) to a file suffix.
 my %dl_type_to_suffix = (
@@ -127,6 +166,12 @@ my %dl_type_to_suffix = (
        "plain" => ".rules",
 );
 
+# Hash to translate an application layer protocol to the application name.
+my %tr_app_layer_proto = (
+       "ikev2" => "ipsec",
+       "krb5" => "kerberos",
+);
+
 #
 ## Function to check and create all IDS related files, if the does not exist.
 #
@@ -135,9 +180,9 @@ sub check_and_create_filelayout() {
        unless (-f "$oinkmaster_provider_includes_file") { &create_empty_file($oinkmaster_provider_includes_file); }
        unless (-f "$modify_sids_file") { &create_empty_file($modify_sids_file); }
        unless (-f "$suricata_used_providers_file") { &create_empty_file($suricata_used_providers_file); }
+       unless (-f "$suricata_default_rulefiles_file") { &create_empty_file($suricata_default_rulefiles_file); }
        unless (-f "$ids_settings_file") { &create_empty_file($ids_settings_file); }
        unless (-f "$providers_settings_file") { &create_empty_file($providers_settings_file); }
-       unless (-f "$ignored_file") { &create_empty_file($ignored_file); }
        unless (-f "$whitelist_file" ) { &create_empty_file($whitelist_file); }
 }
 
@@ -241,6 +286,10 @@ sub downloadruleset ($) {
        # If no provider is given default to "all".
        $provider //= 'all';
 
+       # The amount of download attempts before giving up and
+       # logging an error.
+       my $max_dl_attempts = 3;
+
        # Hash to store the providers and access id's, for which rules should be downloaded.
        my %sheduled_providers = ();
 
@@ -261,11 +310,16 @@ sub downloadruleset ($) {
        my %proxysettings=();
        &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
 
-       # Load required perl module to handle the download.
-       use LWP::UserAgent;
-
        # Init the download module.
-       my $downloader = LWP::UserAgent->new;
+       #
+       # Request SSL hostname verification and specify path
+       # to the CA file.
+       my $downloader = LWP::UserAgent->new(
+               ssl_opts => {
+                       SSL_ca_file     => '/etc/ssl/cert.pem',
+                       verify_hostname => 1,
+               }
+       );
 
        # Set timeout to 10 seconds.
        $downloader->timeout(10);
@@ -305,6 +359,9 @@ sub downloadruleset ($) {
 
        # Loop through the hash of sheduled providers.
        foreach my $provider ( keys %sheduled_providers) {
+               # Log download/update of the ruleset.
+               &_log_to_syslog("Downloading ruleset for provider: $provider.");
+
                # Grab the download url for the provider.
                my $url = $IDS::Ruleset::Providers{$provider}{'dl_url'};
 
@@ -328,27 +385,61 @@ sub downloadruleset ($) {
                        return 1;
                }
 
-               # Variable to store the filesize of the remote object.
-               my $remote_filesize;
+               # Pass the requested URL to the downloader.
+               my $request = HTTP::Request->new(GET => $url);
+
+               # Generate temporary file name, located in "/var/tmp" and with a suffix of ".tmp".
+               # The downloaded file will be stored there until some sanity checks are performed.
+               my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "/var/tmp/", UNLINK => 0 );
+               my $tmpfile = $tmp->filename();
+
+               # Call function to get the final path and filename for the downloaded file.
+               my $dl_rulesfile = &_get_dl_rulesfile($provider);
 
-               # The sourcfire (snort rules) does not allow to send "HEAD" requests, so skip this check
-               # for this webserver.
+               # Check if the rulesfile already exits, because it has been downloaded in the past.
                #
-               # Check if the ruleset source contains "snort.org".
-               unless ($url =~ /\.snort\.org/) {
-                       # Pass the requrested url to the downloader.
-                       my $request = HTTP::Request->new(HEAD => $url);
+               # In this case we are requesting the server if the remote file has been changed or not.
+               # This will be done by sending the modification time in a special HTTP header.
+               if (-f $dl_rulesfile) {
+                       # Call stat on the file.
+                       my $stat = stat($dl_rulesfile);
 
-                       # Accept the html header.
-                       $request->header('Accept' => 'text/html');
+                       # Omit the mtime of the existing file.
+                       my $mtime = $stat->mtime;
 
-                       # Perform the request and fetch the html header.
-                       my $response = $downloader->request($request);
+                       # Convert the timestamp into right format.
+                       my $http_date = time2str($mtime);
 
-                       # Check if there was any error.
-                       unless ($response->is_success) {
+                       # Add the If-Modified-Since header to the request to ask the server if the
+                       # file has been modified.
+                       $request->header( 'If-Modified-Since' => "$http_date" );
+               }
+
+               my $dl_attempt = 1;
+               my $response;
+
+               # Download and retry on failure.
+               while ($dl_attempt <= $max_dl_attempts) {
+                       # Perform the request and save the output into the tmpfile.
+                       $response = $downloader->request($request, $tmpfile);
+
+                       # Check if the download was successfull.
+                       if($response->is_success) {
+                               # Break loop.
+                               last;
+
+                       # Check if the server responds with 304 (Not Modified).
+                       } elsif ($response->code == 304) {
+                               # Log to syslog.
+                               &_log_to_syslog("Ruleset is up-to-date, no update required.");
+
+                               # Nothing to do, the ruleset is up-to-date.
+                               return;
+
+                       # Check if we ran out of download re-tries.
+                       } elsif ($dl_attempt eq $max_dl_attempts) {
                                # Obtain error.
-                               my $error = $response->status_line();
+                               my $error = $response->content;
 
                                # Log error message.
                                &_log_to_syslog("Unable to download the ruleset. \($error\)");
@@ -357,41 +448,22 @@ sub downloadruleset ($) {
                                return 1;
                        }
 
-                       # Assign the fetched header object.
-                       my $header = $response->headers();
+                       # Remove temporary file, if one exists.
+                       unlink("$tmpfile") if (-e "$tmpfile");
 
-                       # Grab the remote file size from the object and store it in the
-                       # variable.
-                       $remote_filesize = $header->content_length;
+                       # Increase download attempt counter.
+                       $dl_attempt++;
                }
 
-               # Load perl module to deal with temporary files.
-               use File::Temp;
-
-               # Generate temporary file name, located in "/var/tmp" and with a suffix of ".tmp".
-               my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "/var/tmp/", UNLINK => 0 );
-               my $tmpfile = $tmp->filename();
-
-               # Pass the requested url to the downloader.
-               my $request = HTTP::Request->new(GET => $url);
-
-               # Perform the request and save the output into the tmpfile.
-               my $response = $downloader->request($request, $tmpfile);
-
-               # Check if there was any error.
-               unless ($response->is_success) {
-                       # Obtain error.
-                       my $error = $response->content;
-
-                       # Log error message.
-                       &_log_to_syslog("Unable to download the ruleset. \($error\)");
+               # Obtain the connection headers.
+               my $headers = $response->headers;
 
-                       # Return "1" - false.
-                       return 1;
-               }
+               # Get the timestamp from header, when the file has been modified the
+               # last time.
+               my $last_modified = $headers->last_modified;
 
-               # Load perl stat module.
-               use File::stat;
+               # Get the remote size of the downloaded file.
+               my $remote_filesize = $headers->content_length;
 
                # Perform stat on the tmpfile.
                my $stat = stat($tmpfile);
@@ -412,9 +484,6 @@ sub downloadruleset ($) {
                        return 1;
                }
 
-               # Genarate and assign file name and path to store the downloaded rules file.
-               my $dl_rulesfile = &_get_dl_rulesfile($provider);
-
                # Check if a file name could be obtained.
                unless ($dl_rulesfile) {
                        # Log error message.
@@ -424,14 +493,19 @@ sub downloadruleset ($) {
                        unlink("$tmpfile");
 
                        # Return "1" - false.
+                       return 1;
                }
 
-               # Load file copy module, which contains the move() function.
-               use File::Copy;
-
                # Overwrite the may existing rulefile or tarball with the downloaded one.
                move("$tmpfile", "$dl_rulesfile");
 
+               # Check if we got a last-modified value from the server.
+               if ($last_modified) {
+                       # Assign the last-modified timestamp as mtime to the
+                       # rules file.
+                       utime(time(), "$last_modified", "$dl_rulesfile");
+               }
+
                # Delete temporary file.
                unlink("$tmpfile");
 
@@ -452,14 +526,8 @@ sub downloadruleset ($) {
 sub extractruleset ($) {
        my ($provider) = @_;
 
-       # Load perl module to deal with archives.
-       use Archive::Tar;
-
-       # Load perl module to deal with files and path.
-       use File::Basename;
-
-       # Load perl module for file copying.
-       use File::Copy;
+       # Disable chown functionality when uncompressing files.
+       $Archive::Tar::CHOWN = "0";
 
        # Get full path and downloaded rulesfile for the given provider.
        my $tarball = &_get_dl_rulesfile($provider);
@@ -514,6 +582,15 @@ sub extractruleset ($) {
 
                        # Handle rules files.
                        } elsif ($file =~ m/\.rules$/) {
+                               # Skip rule files which are not located in the rules directory or archive root.
+                               next unless(($packed_file =~ /^rules\//) || ($packed_file !~ /\//));
+
+                               # Skip deleted.rules.
+                               #
+                               # Mostly they have been taken out for correctness or performance reasons and therfore
+                               # it is not a great idea to enable any of them.
+                               next if($file =~ m/deleted.rules$/);
+
                                my $rulesfilename;
 
                                # Splitt the filename into chunks.
@@ -552,8 +629,35 @@ sub extractruleset ($) {
                                next;
                        }
 
-                       # Extract the file to the temporary directory.
-                       $tar->extract_file("$packed_file", "$destination");
+                       # Check if the destination file exists.
+                       unless(-e "$destination") {
+                               # Extract the file to the temporary directory.
+                               $tar->extract_file("$packed_file", "$destination");
+                       } else {
+                               # Generate temporary file name, located in the temporary rules directory and a suffix of ".tmp".
+                               my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "$tmp_rules_directory", UNLINK => 0 );
+                               my $tmpfile = $tmp->filename();
+
+                               # Extract the file to the new temporary file name.
+                               $tar->extract_file("$packed_file", "$tmpfile");
+
+                               # Open the the existing file.
+                               open(DESTFILE, ">>", "$destination") or die "Could not open $destination. $!\n";
+                               open(TMPFILE, "<", "$tmpfile") or die "Could not open $tmpfile. $!\n";
+
+                               # Loop through the content of the temporary file.
+                               while (<TMPFILE>) {
+                                       # Append the content line by line to the destination file.
+                                       print DESTFILE "$_";
+                               }
+
+                               # Close the file handles.
+                               close(TMPFILE);
+                               close(DESTFILE);
+
+                               # Remove the temporary file.
+                               unlink("$tmpfile");
+                       }
                }
        }
 }
@@ -578,9 +682,6 @@ sub oinkmaster () {
                &extractruleset($provider);
        }
 
-       # Load perl module to talk to the kernel syslog.
-       use Sys::Syslog qw(:DEFAULT setlogsock);
-
        # Establish the connection to the syslog service.
        openlog('oinkmaster', 'cons,pid', 'user');
 
@@ -752,9 +853,6 @@ sub merge_sid_msg (@) {
 ## the rules directory.
 #
 sub move_tmp_ruleset() {
-       # Load perl module.
-       use File::Copy;
-
        # Do a directory listing of the temporary directory.
        opendir  DH, $tmp_rules_directory;
 
@@ -772,8 +870,6 @@ sub move_tmp_ruleset() {
 ## Function to cleanup the temporary IDS directroy.
 #
 sub cleanup_tmp_directory () {
-       # Load rmtree() function from file path perl module.
-       use File::Path 'rmtree';
 
        # Delete temporary directory and all containing files.
        rmtree([ "$tmp_directory" ]);
@@ -801,9 +897,6 @@ sub log_error ($) {
 sub _log_to_syslog ($) {
        my ($message) = @_;
 
-       # Load perl module to talk to the kernel syslog.
-       use Sys::Syslog qw(:DEFAULT setlogsock);
-
        # The syslog function works best with an array based input,
        # so generate one before passing the message details to syslog.
        my @syslog = ("ERR", "<ERROR> $message");
@@ -1130,9 +1223,6 @@ sub _cleanup_rulesdir() {
                # We only want files.
                next unless (-f "$rulespath/$file");
 
-               # Skip element if it has config as file extension.
-               next if ($file =~ m/\.config$/);
-
                # Skip rules file for whitelisted hosts.
                next if ("$rulespath/$file" eq $whitelist_file);
 
@@ -1346,7 +1436,7 @@ sub write_used_provider_rulefiles_file($@) {
        my $used_provider_rulesfile_file = &get_used_provider_rulesfile_file($provider);
 
        # Open file for used rulefiles.
-       open (FILE, ">$used_provider_rulesfile_file") or die "Could not write to $used_provider_rulesfile_file. $!\n";
+       open (FILE, ">", "$used_provider_rulesfile_file") or die "Could not write to $used_provider_rulesfile_file. $!\n";
 
        # Write yaml header to the file.
        print FILE "%YAML 1.1\n";
@@ -1376,7 +1466,7 @@ sub write_main_used_rulefiles_file (@) {
        my (@providers) = @_;
 
        # Call function to write the static rulefiles file.
-       &_write_static_rulefiles_file();
+       &_write_default_rulefiles_file();
 
        # Open file for used rulefils inclusion.
        open (FILE, ">", "$suricata_used_providers_file") or die "Could not write to $suricata_used_providers_file. $!\n";
@@ -1400,16 +1490,16 @@ sub write_main_used_rulefiles_file (@) {
                }
        }
 
-       # Always include the file which hold the static includes.
-       print FILE "include\: $suricata_static_rulefiles_file\n";
-
        # Close the filehandle after writing.
        close(FILE);
 }
 
-sub _write_static_rulefiles_file () {
+sub _write_default_rulefiles_file () {
+       # Get enabled application layer protocols.
+       my @enabled_app_layer_protos = &get_suricata_enabled_app_layer_protos();
+
        # Open file.
-       open (FILE, ">", $suricata_static_rulefiles_file) or die "Could not write to $suricata_static_rulefiles_file. $!\n";
+       open (FILE, ">", $suricata_default_rulefiles_file) or die "Could not write to $suricata_default_rulefiles_file. $!\n";
 
        # Write yaml header to the file.
        print FILE "%YAML 1.1\n";
@@ -1423,7 +1513,35 @@ sub _write_static_rulefiles_file () {
                # Check if the file exists.
                if (-f "$rulespath/$file") {
                        # Write the rulesfile name to the file.
-                       print FILE " - $file\n";
+                       print FILE " - $rulespath/$file\n";
+               }
+       }
+
+       print FILE "\n#Default rules for used application layer protocols.\n";
+       foreach my $enabled_app_layer_proto (@enabled_app_layer_protos) {
+               # Check if the current processed app layer proto needs to be translated
+               # into an application name.
+               if (exists($tr_app_layer_proto{$enabled_app_layer_proto})) {
+                       # Obtain the translated application name for this protocol.
+                       $enabled_app_layer_proto = $tr_app_layer_proto{$enabled_app_layer_proto};
+               }
+
+               # Generate filename.
+               my $rulesfile = "$default_rulespath/$enabled_app_layer_proto\.rules";
+
+               # Check if such a file exists.
+               if (-f "$rulesfile") {
+                       # Write the rulesfile name to the file.
+                       print FILE " - $rulesfile\n";
+               }
+
+               # Generate filename with "events" in filename.
+               $rulesfile = "$default_rulespath/$enabled_app_layer_proto\-events.rules";
+
+               # Check if this file exists.
+               if (-f "$rulesfile" ) {
+                       # Write the rulesfile name to the file.
+                       print FILE " - $rulesfile\n";
                }
        }
 
@@ -1497,22 +1615,29 @@ END
 #
 sub get_ruleset_date($) {
        my ($provider) = @_;
-
-       # Load neccessary perl modules for file stat and to format the timestamp.
-       use File::stat;
-       use POSIX qw( strftime );
+       my $date;
+       my $mtime;
 
        # Get the stored rulesfile for this provider.
        my $stored_rulesfile = &_get_dl_rulesfile($provider);
 
-       # Call stat on the rulestarball.
-       my $stat = stat("$stored_rulesfile");
+       # Check if we got a file.
+       if (-f $stored_rulesfile) {
+               # Call stat on the rulestarball.
+               my $stat = stat("$stored_rulesfile");
 
-       # Get timestamp the file creation.
-       my $mtime = $stat->mtime;
+               # Get timestamp the file creation.
+               $mtime = $stat->mtime;
+       }
+
+       # Check if the timestamp has not been grabbed.
+       unless ($mtime) {
+               # Return N/A for Not available.
+               return "N/A";
+       }
 
        # Convert into human read-able format.
-       my $date = strftime('%Y-%m-%d %H:%M:%S', localtime($mtime));
+       $date = strftime('%Y-%m-%d %H:%M:%S', localtime($mtime));
 
        # Return the date.
        return $date;
@@ -1555,6 +1680,48 @@ sub get_suricata_version($) {
        }
 }
 
+#
+## Function to get the enabled application layer protocols.
+#
+sub get_suricata_enabled_app_layer_protos() {
+       # Array to store and return the enabled app layer protos.
+       my @enabled_app_layer_protos = ();
+
+       # Execute piped suricata command and return the list of
+       # enabled application layer protocols.
+       open(SURICATA, "suricata --list-app-layer-protos |") or die "Could not execute program: $!";
+
+       # Grab and store the list of enabled application layer protocols.
+       my @output = <SURICATA>;
+
+       # Close pipe.
+       close(SURICATA);
+
+       # Merge allways enabled static application layers protocols array.
+       @enabled_app_layer_protos = @static_enabled_app_layer_protos;
+
+       # Loop through the array which contains the output of suricata.
+       foreach my $line (@output) {
+               # Skip header line which starts with "===".
+               next if ($line =~ /^\s*=/);
+
+               # Skip info or warning lines.
+               next if ($line =~ /\s*--/);
+
+               # Remove newlines.
+               chomp($line);
+
+               # Add enabled app layer proto to the array.
+               push(@enabled_app_layer_protos, $line);
+       }
+
+       # Sort the array.
+       @enabled_app_layer_protos = sort(@enabled_app_layer_protos);
+
+       # Return the array.
+       return @enabled_app_layer_protos;
+}
+
 #
 ## Function to generate the rules file with whitelisted addresses.
 #
@@ -1591,7 +1758,7 @@ sub generate_ignore_file() {
                                # Check if the address/network is valid.
                                if ((&General::validip($address)) || (&General::validipandmask($address))) {
                                        # Write rule line to the file to pass any traffic from this IP
-                                       print FILE "pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n";
+                                       print FILE "pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; bypass; sid:$sid\;)\n";
 
                                        # Increment sid.
                                        $sid++;