]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - config/cfgroot/ids-functions.pl
IDS: Redesign backend for used provider rulesfiles.
[ipfire-2.x.git] / config / cfgroot / ids-functions.pl
index daa0044931df789ce6932703c813d7443f3c64ab..245a267bf264ab0ef09d8b11c0852509ee01130d 100644 (file)
@@ -27,12 +27,15 @@ package IDS;
 
 require '/var/ipfire/general-functions.pl';
 require "${General::swroot}/network-functions.pl";
-require "${General::swroot}/suricata/ruleset-sources";
+require "${General::swroot}/suricata/ruleset-sources-new";
 
 # Location where all config and settings files are stored.
 our $settingsdir = "${General::swroot}/suricata";
 
-# File where the used rulefiles are stored.
+# File where the main file for providers ruleset inclusion exists.
+our $suricata_used_providers_file = "$settingsdir/suricata-used-providers.yaml";
+
+# DEPRECATED - File where the used rulefiles are stored.
 our $used_rulefiles_file = "$settingsdir/suricata-used-rulefiles.yaml";
 
 # File where the addresses of the homenet are stored.
@@ -138,7 +141,7 @@ sub check_and_create_filelayout() {
        unless (-f "$enabled_sids_file") { &create_empty_file($enabled_sids_file); }
        unless (-f "$disabled_sids_file") { &create_empty_file($disabled_sids_file); }
        unless (-f "$modify_sids_file") { &create_empty_file($modify_sids_file); }
-       unless (-f "$used_rulefiles_file") { &create_empty_file($used_rulefiles_file); }
+       unless (-f "$suricata_used_providers_file") { &create_empty_file($suricata_used_providers_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); }
@@ -163,6 +166,36 @@ sub get_ruleset_providers() {
        return sort(@providers);
 }
 
+#
+## Function to get a list of all enabled ruleset providers.
+##
+## They will be returned as an array.
+#
+sub get_enabled_providers () {
+       my %used_providers = ();
+
+       # Array to store the enabled providers.
+       my @enabled_providers = ();
+
+       # Read-in the providers config file.
+       &General::readhasharray("$providers_settings_file", \%used_providers);
+
+       # Loop through the hash of used_providers.
+       foreach my $id (keys %used_providers) {
+               # Skip disabled providers.
+               next unless ($used_providers{$id}[3] eq "enabled");
+
+               # Grab the provider handle.
+               my $provider = "$used_providers{$id}[0]";
+
+               # Add the provider to the array of enabled providers.
+               push(@enabled_providers, $provider);
+       }
+
+       # Return the array.
+       return @enabled_providers;
+}
+
 #
 ## Function for checking if at least 300MB of free disk space are available
 ## on the "/var" partition.
@@ -519,32 +552,17 @@ sub oinkmaster () {
        # Load perl module for file copying.
        use File::Copy;
 
-       # Hash to store the used providers.
-       my %used_providers = ();
-
-       # Array to store the enabled providers.
-       my @enabled_providers = ();
-
        # Check if the files in rulesdir have the correct permissions.
        &_check_rulesdir_permissions();
 
        # Cleanup the rules directory before filling it with the new rulests.
        &_cleanup_rulesdir();
 
-       # Read-in the providers config file.
-       &General::readhasharray("$providers_settings_file", \%used_providers);
-
-       # Loop through the hash of used_providers.
-       foreach my $id (keys %used_providers) {
-               # Skip disabled providers.
-               next unless ($used_providers{$id}[3] eq "enabled");
-
-               # Grab the provider handle.
-               my $provider = "$used_providers{$id}[0]";
-
-               # Add the provider handle to the array of enabled providers.
-               push(@enabled_providers, $provider);
+       # Get all enabled providers.
+       my @enabled_providers = &get_enabled_providers();
 
+       # 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'};
 
@@ -592,10 +610,6 @@ sub oinkmaster () {
        # Close the log handle.
        closelog();
 
-       use Data::Dumper;
-
-       print Dumper \@enabled_providers;
-
        # Call function to merge the classification files.
        &merge_classifications(@enabled_providers);
 
@@ -603,8 +617,7 @@ sub oinkmaster () {
        &merge_sid_msg(@enabled_providers);
 
        # Cleanup temporary directory.
-       # XXX - not implemented yet.
-       # &cleanup_tmp_directory();
+       &cleanup_tmp_directory();
 }
 
 #
@@ -741,6 +754,38 @@ sub merge_sid_msg (@) {
        close(FILE);
 }
 
+#
+## A very tiny function to move an extracted ruleset from the temporary directory into
+## 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;
+
+       # Loop over all files.
+       while(my $file = readdir DH) {
+               # Move them to the rules directory.
+               move "$tmp_rules_directory/$file" , "$rulespath/$file";
+       }
+
+       # Close directory handle.
+       closedir DH;
+}
+
+#
+## 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" ]);
+}
+
 #
 ## Function to do all the logging stuff if the downloading or updating of the ruleset fails.
 #
@@ -827,6 +872,22 @@ sub _get_dl_rulesfile($) {
        return $rulesfile;
 }
 
+#
+## Tiny function to delete the stored ruleset file or tarball for a given provider.
+#
+sub drop_dl_rulesfile ($) {
+       my ($provider) = @_;
+
+       # Gather the full path and name of the stored rulesfile.
+       my $rulesfile = &_get_dl_rulesfile($provider);
+
+       # Check if the given rulesfile exists.
+       if (-f $rulesfile) {
+               # Delete the stored rulesfile.
+               unlink($rulesfile) or die "Could not delete $rulesfile. $!\n";
+       }
+}
+
 #
 ## Function to check if the IDS is running.
 #
@@ -966,9 +1027,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);
 
@@ -1171,13 +1229,18 @@ sub generate_http_ports_file() {
 }
 
 #
-## Function to generate and write the file for used rulefiles.
+## Function to generate and write the file for used rulefiles file for a given provider.
+##
+## The function requires as first argument a provider handle, and as second an array with files.
 #
-sub write_used_rulefiles_file(@) {
-       my @files = @_;
+sub write_used_provider_rulefiles_file($@) {
+       my ($provider, @files) = @_;
+
+       # Get the path and file for the provider specific used rulefiles file.
+       my $used_provider_rulesfile_file = &get_used_provider_rulesfile_file($provider);
 
        # Open file for used rulefiles.
-       open (FILE, ">$used_rulefiles_file") or die "Could not write to $used_rulefiles_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";
@@ -1186,9 +1249,6 @@ sub write_used_rulefiles_file(@) {
        # Write header to file.
        print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
 
-       # Allways use the whitelist.
-       print FILE " - whitelist.rules\n";
-
        # Loop through the array of given files.
        foreach my $file (@files) {
                # Check if the given filename exists and write it to the file of used rulefiles.
@@ -1201,18 +1261,60 @@ sub write_used_rulefiles_file(@) {
        close(FILE);
 }
 
+#
+## Function to write the main file for provider rulesfiles inclusions.
+##
+## This function requires an array of provider handles.
+#
+sub write_main_used_rulefiles_file (@) {
+       my (@providers) = @_;
+
+       # Open file for used rulefils inclusion.
+       open (FILE, ">", "$suricata_used_providers_file") or die "Could not write to $suricata_used_providers_file. $!\n";
+
+       # Write yaml header to the file.
+       print FILE "%YAML 1.1\n";
+       print FILE "---\n\n";
+
+       # Write header to file.
+       print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
+
+       # Loop through the list of given providers.
+       foreach my $provider (@providers) {
+               # 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";
+       }
+
+       # XXX - whitelist.rules is not allowed directly, needs to be in a yaml file which has to be included.
+       # Always use the whitelist file.
+       #print FILE "\n - whitelist.rules\n";
+
+       # Close the filehandle after writing.
+       close(FILE);
+}
+
+#
+## Tiny function to generate the full path and name for the used_provider_rulesfile file of a given provider.
+#
+sub get_used_provider_rulesfile_file ($) {
+       my ($provider) = @_;
+
+       my $filename = "$settingsdir/suricata\-$provider\-used\-rulefiles.yaml";
+
+       # Return the gernerated file.
+       return $filename;
+}
+
 #
 ## Function to generate and write the file for modify the ruleset.
 #
 sub write_modify_sids_file() {
        # Get configured settings.
        my %idssettings=();
-       my %rulessettings=();
        &General::readhash("$ids_settings_file", \%idssettings);
-       &General::readhash("$rules_settings_file", \%rulessettings);
-
-       # Gather the configured ruleset.
-       my $ruleset = $rulessettings{'RULES'};
 
        # Open modify sid's file for writing.
        open(FILE, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
@@ -1229,33 +1331,24 @@ sub write_modify_sids_file() {
                # malware in that file.  Rules which fall into the first category should stay as
                # alert since not all flows of that type contain malware.
 
-               if($ruleset eq 'registered' or $ruleset eq 'subscripted' or $ruleset eq 'community') {
-                       # These types of rulesfiles contain meta-data which gives the action that should
-                       # be used when in IPS mode.  Do the following:
-                       #
-                       # 1. Disable all rules and set the action to 'drop'
-                       # 2. Set the action back to 'alert' if the rule contains 'flowbits:noalert;'
-                       #    This should give rules not in the policy a reasonable default if the user
-                       #    manually enables them.
-                       # 3. Enable rules and set actions according to the meta-data strings.
+               # These types of rulesfiles contain meta-data which gives the action that should
+               # be used when in IPS mode.  Do the following:
+               #
+               # 1. Disable all rules and set the action to 'drop'
+               # 2. Set the action back to 'alert' if the rule contains 'flowbits:noalert;'
+               #    This should give rules not in the policy a reasonable default if the user
+               #    manually enables them.
+               # 3. Enable rules and set actions according to the meta-data strings.
 
-                       my $policy = 'balanced';  # Placeholder to allow policy to be changed.
+               my $policy = 'balanced';  # Placeholder to allow policy to be changed.
 
                        print FILE <<END;
-modifysid * "^#?(?:alert|drop)" | "#drop"
-modifysid * "^#drop(.+flowbits:noalert;)" | "#alert\${1}"
 modifysid * "^#(?:alert|drop)(.+policy $policy-ips alert)" | "alert\${1}"
 modifysid * "^#(?:alert|drop)(.+policy $policy-ips drop)" | "drop\${1}"
-END
-               } else {
-                       # These rulefiles don't have the metadata, so set rules to 'drop' unless they
-                       # contain the string 'flowbits:noalert;'.
-                       print FILE <<END;
 modifysid * "^(#?)(?:alert|drop)" | "\${1}drop"
 modifysid * "^(#?)drop(.+flowbits:noalert;)" | "\${1}alert\${2}"
 END
                }
-       }
 
        # Close file handle.
        close(FILE);
@@ -1467,6 +1560,53 @@ sub get_red_address() {
        return;
 }
 
+#
+## Function to get the used rules files of a given provider.
+#
+sub read_used_provider_rulesfiles($) {
+       my ($provider) = @_;
+
+       # Array to store the used rulefiles.
+       my @used_rulesfiles = ();
+
+       # 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) {
+               # Open the file or used rulefiles and read-in content.
+               open(FILE, $rulesfile_file) or die "Could not open $rulesfile_file. $!\n";
+
+               while (<FILE>) {
+                       # Assign the current line to a nice variable.
+                       my $line = $_;
+
+                       # Remove newlines.
+                       chomp($line);
+
+                       # Skip comments.
+                       next if ($line =~ /\#/);
+
+                       # Skip blank  lines.
+                       next if ($line =~ /^\s*$/);
+
+                       # Gather the rulefile.
+                       if ($line =~ /.*- (.*)/) {
+                               my $rulefile = $1;
+
+                               # Add the rulefile to the array of used rulesfiles.
+                               push(@used_rulesfiles, $rulefile);
+                       }
+               }
+
+               # Close the file.
+               close(FILE);
+       }
+
+       # Return the array of used rulesfiles.
+       return @used_rulesfiles;
+}
+
 #
 ## Function to write the lock file for locking the WUI, while
 ## the autoupdate script runs.