require '/var/ipfire/general-functions.pl';
require "${General::swroot}/network-functions.pl";
-require "${General::swroot}/suricata/ruleset-sources-new";
+require "${General::swroot}/suricata/ruleset-sources";
# Location where all config and settings files are stored.
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";
-
-# DEPRECATED - File where the used rulefiles are stored.
-our $used_rulefiles_file = "$settingsdir/suricata-used-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";
# File where the HTTP ports definition is stored.
our $http_ports_file = "$settingsdir/suricata-http-ports.yaml";
-# File which contains the enabled sids.
-our $enabled_sids_file = "$settingsdir/oinkmaster-enabled-sids.conf";
-
-# File which contains the disabled sids.
-our $disabled_sids_file = "$settingsdir/oinkmaster-disabled-sids.conf";
+# File which contains includes for provider specific rule modifications.
+our $oinkmaster_provider_includes_file = "$settingsdir/oinkmaster-provider-includes.conf";
# File which contains wheater the rules should be changed.
our $modify_sids_file = "$settingsdir/oinkmaster-modify-sids.conf";
# File which stores the configured IPS settings.
our $ids_settings_file = "$settingsdir/settings";
-# DEPRECATED - File which stores the configured rules-settings.
-our $rules_settings_file = "$settingsdir/rules-settings";
-
# File which stores the used and configured ruleset providers.
our $providers_settings_file = "$settingsdir/providers-settings";
# File which stores the configured settings for whitelisted addresses.
our $ignored_file = "$settingsdir/ignored";
-# DEPRECATED - Location and name of the tarball which contains the ruleset.
-our $rulestarball = "/var/tmp/idsrules.tar.gz";
-
# Location where the downloaded rulesets are stored.
our $dl_rules_path = "/var/tmp";
# 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.
#
sub check_and_create_filelayout() {
# Check if the files exist and if not, create them.
- unless (-f "$enabled_sids_file") { &create_empty_file($enabled_sids_file); }
- unless (-f "$disabled_sids_file") { &create_empty_file($disabled_sids_file); }
+ 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.
#
## Function to extract a given ruleset.
+##
+## In case the ruleset provider offers a plain file, it simply will
+## be copied.
#
sub extractruleset ($) {
my ($provider) = @_;
# 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;
+ # Load perl module for file copying.
+ use File::Copy;
+
# Get full path and downloaded rulesfile for the given provider.
my $tarball = &_get_dl_rulesfile($provider);
# Check if the file exists.
unless (-f $tarball) {
- &_log_to_syslog("Could not extract ruleset file: $tarball");
+ &_log_to_syslog("Could not find ruleset file: $tarball");
# Return nothing.
return;
mkdir("$tmp_rules_directory") unless (-d "$tmp_rules_directory");
mkdir("$tmp_conf_directory") unless (-d "$tmp_conf_directory");
- # Initialize the tar module.
- my $tar = Archive::Tar->new($tarball);
+ # Omit the type (dl_type) of the stored ruleset.
+ my $type = $IDS::Ruleset::Providers{$provider}{'dl_type'};
+
+ # Handle the different ruleset types.
+ if ($type eq "plain") {
+ # Generate destination filename an full path.
+ my $destination = "$tmp_rules_directory/$provider\-ruleset.rules";
+
+ # Copy the file into the temporary rules directory.
+ copy($tarball, $destination);
+
+ } elsif ( $type eq "archive") {
+ # Initialize the tar module.
+ my $tar = Archive::Tar->new($tarball);
+
+ # Get the filelist inside the tarball.
+ my @packed_files = $tar->list_files;
+
+ # Loop through the filelist.
+ foreach my $packed_file (@packed_files) {
+ my $destination;
+
+ # Splitt the packed file into chunks.
+ my $file = fileparse($packed_file);
+
+ # Handle msg-id.map file.
+ if ("$file" eq "sid-msg.map") {
+ # Set extract destination to temporary config_dir.
+ $destination = "$tmp_conf_directory/$provider\-sid-msg.map";
- # Get the filelist inside the tarball.
- my @packed_files = $tar->list_files;
+ # Handle classification.conf
+ } elsif ("$file" eq "classification.config") {
+ # Set extract destination to temporary config_dir.
+ $destination = "$tmp_conf_directory/$provider\-classification.config";
- # Loop through the filelist.
- foreach my $packed_file (@packed_files) {
- my $destination;
+ # 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 !~ /\//));
- # Splitt the packed file into chunks.
- my $file = fileparse($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$/);
- # Handle msg-id.map file.
- if ("$file" eq "sid-msg.map") {
- # Set extract destination to temporary config_dir.
- $destination = "$tmp_conf_directory/$provider\-sid-msg.map";
- # Handle classification.conf
- } elsif ("$file" eq "classification.config") {
- # Set extract destination to temporary config_dir.
- $destination = "$tmp_conf_directory/$provider\-classification.config";
- # Handle rules files.
- } elsif ($file =~ m/\.rules$/) {
- my $rulesfilename;
+ my $rulesfilename;
- # Splitt the filename into chunks.
- my @filename = split("-", $file);
+ # Splitt the filename into chunks.
+ my @filename = split("-", $file);
- # Reverse the array.
- @filename = reverse(@filename);
+ # Reverse the array.
+ @filename = reverse(@filename);
- # Get the amount of elements in the array.
- my $elements = @filename;
+ # Get the amount of elements in the array.
+ my $elements = @filename;
- # Remove last element of the hash.
- # It contains the vendor name, which will be replaced.
- if ($elements >= 3) {
+ # Remove last element of the hash.
+ # It contains the vendor name, which will be replaced.
+ if ($elements >= 3) {
# Remove last element from hash.
- pop(@filename);
- }
+ pop(@filename);
+ }
+
+ # Check if the last element of the filename does not
+ # contain the providers name.
+ if ($filename[-1] ne "$provider") {
+ # Add provider name as last element.
+ push(@filename, $provider);
+ }
- # Check if the last element of the filename does not
- # contain the providers name.
- if ($filename[-1] ne "$provider") {
- # Add provider name as last element.
- push(@filename, $provider);
+ # Reverse the array back.
+ @filename = reverse(@filename);
+
+ # Generate the name for the rulesfile.
+ $rulesfilename = join("-", @filename);
+
+ # Set extract destination to temporaray rules_dir.
+ $destination = "$tmp_rules_directory/$rulesfilename";
+ } else {
+ # Skip all other files.
+ next;
}
- # Reverse the array back.
- @filename = reverse(@filename);
+ # 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 "$_";
+ }
- # Generate the name for the rulesfile.
- $rulesfilename = join("-", @filename);
+ # Close the file handles.
+ close(TMPFILE);
+ close(DESTFILE);
- # Set extract destination to temporaray rules_dir.
- $destination = "$tmp_rules_directory/$rulesfilename";
- } else {
- # Skip all other files.
- next;
+ # Remove the temporary file.
+ unlink("$tmpfile");
+ }
}
-
- # Extract the file to the temporary directory.
- $tar->extract_file("$packed_file", "$destination");
}
}
## call the functions to merge the additional config files. (classification, sid-msg, etc.).
#
sub oinkmaster () {
- # Load perl module for file copying.
- use File::Copy;
-
# Check if the files in rulesdir have the correct permissions.
&_check_rulesdir_permissions();
# Loop through the array of enabled providers.
foreach my $provider (@enabled_providers) {
- # Omit the type (dl_type) of the stored ruleset.
- my $type = $IDS::Ruleset::Providers{$provider}{'dl_type'};
-
- # Handle the different ruleset types.
- if ($type eq "archive") {
- # Call the extractruleset function.
- &extractruleset($provider);
- } elsif ($type eq "plain") {
- # Generate filename and full path for the stored rulesfile.
- my $dl_rulesfile = &_get_dl_rulesfile($provider);
-
- # Generate destination filename an full path.
- my $destination = "$tmp_rules_directory/$provider\-ruleset.rules";
-
- # Copy the file into the temporary rules directory.
- copy($dl_rulesfile, $destination);
- } else {
- # Skip unknown type.
- next;
- }
+ # Call the extractruleset function.
+ &extractruleset($provider);
}
# Load perl module to talk to the kernel syslog.
}
}
+#
+## Tiny function to get/generate the full path and filename for the providers oinkmaster
+## modified sids file.
+#
+sub get_oinkmaster_provider_modified_sids_file ($) {
+ my ($provider) = @_;
+
+ # Generate the filename.
+ my $filename = "$settingsdir/oinkmaster-$provider-modified-sids.conf";
+
+ # Return the filename.
+ return $filename;
+}
+
+#
+## Function to directly altering the oinkmaster provider includes file.
+##
+## Requires tha acition "remove" or "add" and a provider handle.
+#
+sub alter_oinkmaster_provider_includes_file ($$) {
+ my ($action, $provider) = @_;
+
+ # Call function to get the path and name for the given providers
+ # oinkmaster modified sids file.
+ my $provider_modified_sids_file = &get_oinkmaster_provider_modified_sids_file($provider);
+
+ # Open the file for reading..
+ open (FILE, $oinkmaster_provider_includes_file) or die "Could not read $oinkmaster_provider_includes_file. $!\n";
+
+ # Read-in file content.
+ my @lines = <FILE>;
+
+ # Close file after reading.
+ close(FILE);
+
+ # Re-open the file for writing.
+ open(FILE, ">", $oinkmaster_provider_includes_file) or die "Could not write to $oinkmaster_provider_includes_file. $!\n";
+
+ # Loop through the file content.
+ foreach my $line (@lines) {
+ # Remove newlines.
+ chomp($line);
+
+ # Skip line if we found our given provider and the action should be remove.
+ next if (($line =~ /$provider/) && ($action eq "remove"));
+
+ # Write the read-in line back to the file.
+ print FILE "$line\n";
+ }
+
+ # Check if the file exists and add the provider if requested.
+ if ((-f $provider_modified_sids_file) && ($action eq "add")) {
+ print FILE "include $provider_modified_sids_file\n";
+ }
+
+ # Close file handle.
+ close(FILE);
+}
+
+#
+## Function to read-in the given enabled or disables sids file.
+#
+sub read_enabled_disabled_sids_file($) {
+ my ($file) = @_;
+
+ # Temporary hash to store the sids and their state. It will be
+ # returned at the end of this function.
+ my %temphash;
+
+ # Open the given filename.
+ open(FILE, "$file") or die "Could not open $file. $!\n";
+
+ # Loop through the file.
+ while(<FILE>) {
+ # Remove newlines.
+ chomp $_;
+
+ # Skip blank lines.
+ next if ($_ =~ /^\s*$/);
+
+ # Skip coments.
+ next if ($_ =~ /^\#/);
+
+ # Splitt line into sid and state part.
+ my ($state, $sid) = split(" ", $_);
+
+ # Skip line if the sid is not numeric.
+ next unless ($sid =~ /\d+/ );
+
+ # Check if the sid was enabled.
+ if ($state eq "enablesid") {
+ # Add the sid and its state as enabled to the temporary hash.
+ $temphash{$sid} = "enabled";
+ # Check if the sid was disabled.
+ } elsif ($state eq "disablesid") {
+ # Add the sid and its state as disabled to the temporary hash.
+ $temphash{$sid} = "disabled";
+ # Invalid state - skip the current sid and state.
+ } else {
+ next;
+ }
+ }
+
+ # Close filehandle.
+ close(FILE);
+
+ # Return the hash.
+ return %temphash;
+}
+
#
## Function to check if the IDS is running.
#
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";
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";
# Call function to get the providers used rulefiles file.
my $filename = &get_used_provider_rulesfile_file($provider);
- # Print the provider to the file.
- print FILE "include\: $filename\n";
+ # Check if the file exists and write it into the used rulefiles file.
+ if (-f $filename) {
+ # Print the provider to the file.
+ print FILE "include\: $filename\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.
+ $mtime = $stat->mtime;
+ }
- # Get timestamp the file creation.
- my $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;
}
}
+#
+## 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++;
# Get the used rulesefile file for the provider.
my $rulesfile_file = &get_used_provider_rulesfile_file($provider);
- # Check if the used rulesfile is empty.
- unless (-z $rulesfile_file) {
+ # Check if the a used rulesfile exists for this provider.
+ if (-f $rulesfile_file) {
# Open the file or used rulefiles and read-in content.
open(FILE, $rulesfile_file) or die "Could not open $rulesfile_file. $!\n";