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";
# 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";
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 = (
"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.
#
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); }
}
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);
# 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'};
unlink("$tmpfile");
# Return "1" - false.
+ return 1;
}
# Load file copy module, which contains the move() function.
# Load perl module to deal with archives.
use Archive::Tar;
+ # Disable chown functionality when uncompressing files.
+ $Archive::Tar::CHOWN = "0";
+
# Load perl module to deal with files and path.
use File::Basename;
# 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 {
+ # Load perl module to deal with temporary files.
+ use File::Temp;
+
+ # 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");
+ }
}
}
}
# Generate the full filename and path for the stored rules file.
my $rulesfile = "$dl_rules_path/$dl_rulesfile_prefix-$provider$suffix";
- # Check if the file exists.
- unless (-f "$rulesfile") {
- # Abort return - nothing.
- return;
- }
-
# Return the generated filename.
return $rulesfile;
}
# 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);
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";
}
}
- # 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";
# 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";
}
}
sub get_ruleset_date($) {
my ($provider) = @_;
my $date;
+ my $mtime;
# Load neccessary perl modules for file stat and to format the timestamp.
use File::stat;
# 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) {
}
}
+#
+## 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.
#
# 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++;