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";
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); }
}
}
#
-## This function is responsible for downloading the configured IDS rulesets or if no one is specified
-## all configured rulesets will be downloaded.
+## This function is responsible for downloading the ruleset for a given provider.
##
-## * At first it gathers all configured ruleset providers, initialize the downloader and sets an
-## upstream proxy if configured.
-## * After that, the given ruleset or in case all rulesets should be downloaded, it will determine wether it
-## is enabled or not.
+## * At first it initialize the downloader and sets an upstream proxy if configured.
## * The next step will be to generate the final download url, by obtaining the URL for the desired
-## ruleset, add the settings for the upstream proxy.
-## * Finally the function will grab all the rules files or tarballs from the servers.
+## ruleset and add the settings for the upstream proxy.
+## * Finally the function will grab the rule file or tarball from the server.
+## It tries to reduce the amount of download by using the "If-Modified-Since" HTTP header.
+#
+## Return codes:
+##
+## * "no url" - If no download URL could be gathered for the provider.
+## * "not modified" - In case the already stored rules file is up to date.
+## * "incomplete download" - When the remote file size differs from the downloaded file size.
+## * "$error" - The error message generated from the LWP::User Agent module.
#
sub downloadruleset ($) {
my ($provider) = @_;
- # If no provider is given default to "all".
- $provider //= 'all';
-
- # Hash to store the providers and access id's, for which rules should be downloaded.
- my %sheduled_providers = ();
-
- # Get used provider settings.
- my %used_providers = ();
- &General::readhasharray("$providers_settings_file", \%used_providers);
-
- # Check if a ruleset has been configured.
- unless(%used_providers) {
- # Log that no ruleset has been configured and abort.
- &_log_to_syslog("No ruleset provider has been configured.");
-
- # Return "1".
- return 1;
- }
+ # The amount of download attempts before giving up and
+ # logging an error.
+ my $max_dl_attempts = 3;
# Read proxysettings.
my %proxysettings=();
&General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
- # Load required perl module to handle the download.
- use LWP::UserAgent;
-
# Init the download module.
#
# Request SSL hostname verification and specify path
$downloader->proxy(['http', 'https'], $proxy_url);
}
- # Loop through the hash of configured providers.
- foreach my $id ( keys %used_providers ) {
- # Skip providers which are not enabled.
- next if ($used_providers{$id}[3] ne "enabled");
+ # Log download/update of the ruleset.
+ &_log_to_syslog("Downloading ruleset for provider: $provider.");
- # Obtain the provider handle.
- my $provider_handle = $used_providers{$id}[0];
+ # Grab the download url for the provider.
+ my $url = $IDS::Ruleset::Providers{$provider}{'dl_url'};
- # Handle update off all providers.
- if (($provider eq "all") || ($provider_handle eq "$provider")) {
- # Add provider handle and it's id to the hash of sheduled providers.
- $sheduled_providers{$provider_handle} = $id;
- }
- }
-
- # 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'};
+ # Check if the provider requires a subscription.
+ if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") {
+ # Grab the subscription code.
+ my $subscription_code = &get_subscription_code($provider);
- # Check if the provider requires a subscription.
- if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") {
- # Grab the previously stored access id for the provider from hash.
- my $id = $sheduled_providers{$provider};
+ # Add the subscription code to the download url.
+ $url =~ s/\<subscription_code\>/$subscription_code/g;
- # Grab the subscription code.
- my $subscription_code = $used_providers{$id}[1];
-
- # Add the subscription code to the download url.
- $url =~ s/\<subscription_code\>/$subscription_code/g;
-
- }
-
- # Abort if no url could be determined for the provider.
- unless ($url) {
- # Log error and abort.
- &_log_to_syslog("Unable to gather a download URL for the selected ruleset provider.");
- return 1;
- }
+ }
- # Variable to store the filesize of the remote object.
- my $remote_filesize;
+ # Abort if no url could be determined for the provider.
+ unless ($url) {
+ # Log error and abort.
+ &_log_to_syslog("Unable to gather a download URL for the selected ruleset provider.");
+ return "no url";
+ }
- # The sourcfire (snort rules) does not allow to send "HEAD" requests, so skip this check
- # for this webserver.
- #
- # 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);
+ # Pass the requested URL to the downloader.
+ my $request = HTTP::Request->new(GET => $url);
- # Accept the html header.
- $request->header('Accept' => 'text/html');
+ # 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();
- # Perform the request and fetch the html header.
- my $response = $downloader->request($request);
+ # Call function to get the final path and filename for the downloaded file.
+ my $dl_rulesfile = &_get_dl_rulesfile($provider);
- # Check if there was any error.
- unless ($response->is_success) {
- # Obtain error.
- my $error = $response->status_line();
+ # Check if the rulesfile already exits, because it has been downloaded in the past.
+ #
+ # 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);
- # Log error message.
- &_log_to_syslog("Unable to download the ruleset. \($error\)");
+ # Omit the mtime of the existing file.
+ my $mtime = $stat->mtime;
- # Return "1" - false.
- return 1;
- }
+ # Convert the timestamp into right format.
+ my $http_date = time2str($mtime);
- # Assign the fetched header object.
- my $header = $response->headers();
+ # 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" );
+ }
- # Grab the remote file size from the object and store it in the
- # variable.
- $remote_filesize = $header->content_length;
- }
+ my $dl_attempt = 1;
+ my $response;
- # Load perl module to deal with temporary files.
- use File::Temp;
+ # 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);
- # 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();
+ # Check if the download was successfull.
+ if($response->is_success) {
+ # Break loop.
+ last;
- # Pass the requested url to the downloader.
- my $request = HTTP::Request->new(GET => $url);
+ # 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.");
- # Perform the request and save the output into the tmpfile.
- my $response = $downloader->request($request, $tmpfile);
+ # Return "not modified".
+ return "not modified";
- # Check if there was any error.
- unless ($response->is_success) {
+ # Check if we ran out of download re-tries.
+ } elsif ($dl_attempt eq $max_dl_attempts) {
# Obtain error.
my $error = $response->content;
# Log error message.
&_log_to_syslog("Unable to download the ruleset. \($error\)");
- # Return "1" - false.
- return 1;
+ # Return the error message from response..
+ return "$error";
}
- # Load perl stat module.
- use File::stat;
+ # Remove temporary file, if one exists.
+ unlink("$tmpfile") if (-e "$tmpfile");
- # Perform stat on the tmpfile.
- my $stat = stat($tmpfile);
-
- # Grab the local filesize of the downloaded tarball.
- my $local_filesize = $stat->size;
+ # Increase download attempt counter.
+ $dl_attempt++;
+ }
- # Check if both file sizes match.
- if (($remote_filesize) && ($remote_filesize ne $local_filesize)) {
- # Log error message.
- &_log_to_syslog("Unable to completely download the ruleset. ");
- &_log_to_syslog("Only got $local_filesize Bytes instead of $remote_filesize Bytes. ");
+ # Obtain the connection headers.
+ my $headers = $response->headers;
- # Delete temporary file.
- unlink("$tmpfile");
+ # Get the timestamp from header, when the file has been modified the
+ # last time.
+ my $last_modified = $headers->last_modified;
- # Return "1" - false.
- return 1;
- }
+ # Get the remote size of the downloaded file.
+ my $remote_filesize = $headers->content_length;
- # Genarate and assign file name and path to store the downloaded rules file.
- my $dl_rulesfile = &_get_dl_rulesfile($provider);
+ # Perform stat on the tmpfile.
+ my $stat = stat($tmpfile);
- # Check if a file name could be obtained.
- unless ($dl_rulesfile) {
- # Log error message.
- &_log_to_syslog("Unable to store the downloaded rules file. ");
+ # Grab the local filesize of the downloaded tarball.
+ my $local_filesize = $stat->size;
- # Delete downloaded temporary file.
- unlink("$tmpfile");
+ # Check if both file sizes match.
+ if (($remote_filesize) && ($remote_filesize ne $local_filesize)) {
+ # Log error message.
+ &_log_to_syslog("Unable to completely download the ruleset. ");
+ &_log_to_syslog("Only got $local_filesize Bytes instead of $remote_filesize Bytes. ");
- # Return "1" - false.
- return 1;
- }
+ # Delete temporary file.
+ unlink("$tmpfile");
- # Load file copy module, which contains the move() function.
- use File::Copy;
+ # Return "1" - false.
+ return "incomplete download";
+ }
- # Overwrite the may existing rulefile or tarball with the downloaded one.
- move("$tmpfile", "$dl_rulesfile");
+ # Check if a file name could be obtained.
+ unless ($dl_rulesfile) {
+ # Log error message.
+ &_log_to_syslog("Unable to store the downloaded rules file. ");
- # Delete temporary file.
+ # Delete downloaded temporary file.
unlink("$tmpfile");
- # Set correct ownership for the tarball.
- set_ownership("$dl_rulesfile");
+ # Return "1" - false.
+ return 1;
}
+ # 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");
+
+ # Set correct ownership for the tarball.
+ set_ownership("$dl_rulesfile");
+
# If we got here, everything worked fine. Return nothing.
return;
}
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);
# 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.
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");
+ }
}
}
}
&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');
## 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;
## 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" ]);
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");
close(FILE);
}
+#
+## Function to get the subscription code of a configured provider.
+#
+sub get_subscription_code($) {
+ my ($provider) = @_;
+
+ my %configured_providers = ();
+
+ # Read-in providers settings file.
+ &General::readhasharray($providers_settings_file, \%configured_providers);
+
+ # Loop through the hash of configured providers.
+ foreach my $id (keys %configured_providers) {
+ # Assign nice human-readable values to the data fields.
+ my $provider_handle = $configured_providers{$id}[0];
+ my $subscription_code = $configured_providers{$id}[1];
+
+ # Check if the current processed provider is the requested one.
+ if ($provider_handle eq $provider) {
+ # Return the obtained subscription code.
+ return $subscription_code;
+ }
+ }
+
+ # No subscription code found - return nothing.
+ return;
+}
+
#
## Function to get the ruleset date for a given provider.
##
my $date;
my $mtime;
- # Load neccessary perl modules for file stat and to format the timestamp.
- use File::stat;
- use POSIX qw( strftime );
-
# Get the stored rulesfile for this provider.
my $stored_rulesfile = &_get_dl_rulesfile($provider);