2 ############################################################################
4 # This file is part of the IPFire Firewall. #
6 # IPFire is free software; you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation; either version 2 of the License, or #
9 # (at your option) any later version. #
11 # IPFire is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with IPFire; if not, write to the Free Software #
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
20 # Copyright (C) 2018-2019 IPFire Team <info@ipfire.org> #
22 ############################################################################
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/network-functions.pl";
30 require "${General::swroot}/http-client-functions.pl";
31 require "${General::swroot}/suricata/ruleset-sources";
33 # Load perl module to deal with Archives.
34 use Archive
::Peek
::Libarchive
;
36 # Load perl module to deal with files and path.
39 # Load module to move files.
42 # Load module to recursely remove files and a folder.
43 use File
::Path
qw(rmtree);
45 # Load module to get file stats.
48 # Load function from posix module to format time strings.
49 use POSIX qw
(strftime
);
51 # Load module to talk to the kernel log daemon.
52 use Sys
::Syslog
qw(:DEFAULT setlogsock);
54 # Location where all config and settings files are stored.
55 our $settingsdir = "${General::swroot}/suricata";
57 # File where the main file for providers ruleset inclusion exists.
58 our $suricata_used_rulesfiles_file = "$settingsdir/suricata-used-rulesfiles.yaml";
60 # File where the addresses of the homenet are stored.
61 our $homenet_file = "$settingsdir/suricata-homenet.yaml";
63 # File where the addresses of the used DNS servers are stored.
64 our $dns_servers_file = "$settingsdir/suricata-dns-servers.yaml";
66 # File where the HTTP ports definition is stored.
67 our $http_ports_file = "$settingsdir/suricata-http-ports.yaml";
69 # File which stores the configured IPS settings.
70 our $ids_settings_file = "$settingsdir/settings";
72 # File which stores the used and configured ruleset providers.
73 our $providers_settings_file = "$settingsdir/providers-settings";
75 # File which stores the configured settings for whitelisted addresses.
76 our $ignored_file = "$settingsdir/ignored";
78 # File which stores HTTP Etags for providers which supports them
79 # for cache management.
80 our $etags_file = "$settingsdir/etags";
82 # Location where the downloaded rulesets are stored.
83 our $dl_rules_path = "/var/cache/suricata";
85 # File to store any errors, which also will be read and displayed by the wui.
86 our $storederrorfile = "/tmp/ids_storederror";
88 # File to lock the WUI, while the autoupdate script runs.
89 our $ids_page_lock_file = "/tmp/ids_page_locked";
91 # Location where the rulefiles are stored.
92 our $rulespath = "/var/lib/suricata";
94 # Location where the default rulefils are stored.
95 our $default_rulespath = "/usr/share/suricata/rules";
97 # Location where the addition config files are stored.
98 our $configspath = "/usr/share/suricata";
100 # Location of the classification file.
101 our $classification_file = "$configspath/classification.config";
103 # Location of the sid to msg mappings file.
104 our $sid_msg_file = "$rulespath/sid-msg.map";
106 # Location to store local rules. This file will not be touched.
107 our $local_rules_file = "$rulespath/local.rules";
109 # File which contains the rules to whitelist addresses on suricata.
110 our $whitelist_file = "$rulespath/whitelist.rules";
112 # File which contains a list of all supported ruleset sources.
113 # (Sourcefire, Emergingthreads, etc..)
114 our $rulesetsourcesfile = "$settingsdir/ruleset-sources";
116 # The pidfile of the IDS.
117 our $idspidfile = "/var/run/suricata.pid";
119 # Location of suricatactrl.
120 my $suricatactrl = "/usr/local/bin/suricatactrl";
122 # Prefix for each downloaded ruleset.
123 my $dl_rulesfile_prefix = "idsrules";
125 # Temporary directory where the rulesets will be extracted.
126 my $tmp_directory = "/tmp/ids_tmp";
128 # Temporary directory where the extracted rules files will be stored.
129 my $tmp_rules_directory = "$tmp_directory/rules";
131 # Temporary directory where the extracted additional config files will be stored.
132 my $tmp_conf_directory = "$tmp_directory/conf";
134 # Array with allowed commands of suricatactrl.
135 my @suricatactrl_cmds = ( 'start', 'stop', 'restart', 'reload', 'fix-rules-dir' );
137 # Array which contains the HTTP ports, which statically will be declared as HTTP_PORTS in the
139 my @http_ports = ('80', '81');
141 # Array which contains a list of rulefiles which always will be included if they exist.
142 my @static_included_rulefiles = ('local.rules', 'whitelist.rules');
144 # Log App Layer Events? (Useful for debugging only)
145 my $LOG_APP_LAYER_EVENTS = 0;
147 # Array which contains a list of allways enabled application layer protocols.
148 my @static_enabled_app_layer_protos = ('app-layer', 'decoder', 'files', 'stream');
150 # Hash which allows to convert the download type (dl_type) to a file suffix.
151 my %dl_type_to_suffix = (
152 "archive" => ".tar.gz",
156 # Hash to translate an application layer protocol to the application name.
157 my %tr_app_layer_proto = (
159 "krb5" => "kerberos",
163 ## Function to check and create all IDS related files, if the does not exist.
165 sub check_and_create_filelayout
() {
166 # Check if the files exist and if not, create them.
167 unless (-f
"$suricata_used_rulesfiles_file") { &create_empty_file
($suricata_used_rulesfiles_file); }
168 unless (-f
"$ids_settings_file") { &create_empty_file
($ids_settings_file); }
169 unless (-f
"$providers_settings_file") { &create_empty_file
($providers_settings_file); }
170 unless (-f
"$whitelist_file" ) { &create_empty_file
($whitelist_file); }
174 ## Function to get a list of all available ruleset providers.
176 ## They will be returned as a sorted array.
178 sub get_ruleset_providers
() {
181 # Loop through the hash of providers.
182 foreach my $provider ( keys %IDS::Ruleset
::Providers
) {
183 # Add the provider to the array.
184 push(@providers, $provider);
187 # Sort and return the array.
188 return sort(@providers);
192 ## Function to get a list of all enabled ruleset providers.
194 ## They will be returned as an array.
196 sub get_enabled_providers
() {
197 my %used_providers = ();
199 # Array to store the enabled providers.
200 my @enabled_providers = ();
202 # Read-in the providers config file.
203 &General
::readhasharray
("$providers_settings_file", \
%used_providers);
205 # Loop through the hash of used_providers.
206 foreach my $id (keys %used_providers) {
207 # Skip disabled providers.
208 next unless ($used_providers{$id}[3] eq "enabled");
210 # Grab the provider handle.
211 my $provider = "$used_providers{$id}[0]";
213 # Add the provider to the array of enabled providers.
214 push(@enabled_providers, $provider);
218 return @enabled_providers;
222 ## Function to get a hash of provider handles and their configured modes (IDS/IPS).
224 sub get_providers_mode
() {
225 my %used_providers = ();
227 # Hash to store the providers and their configured modes.
228 my %providers_mode = ();
230 # Read-in the providers config file.
231 &General
::readhasharray
("$providers_settings_file", \
%used_providers);
233 # Loop through the hash of used_providers.
234 foreach my $id (keys %used_providers) {
235 # Skip disabled providers.
236 next unless ($used_providers{$id}[3] eq "enabled");
238 # Grab the provider handle.
239 my $provider = "$used_providers{$id}[0]";
241 # Grab the provider mode.
242 my $mode = "$used_providers{$id}[4]";
244 # Fall back to IDS if no mode could be obtained.
249 # Add details to provider_modes hash.
250 $providers_mode{$provider} = $mode;
254 return %providers_mode;
258 ## Function for checking if at least 300MB of free disk space are available
259 ## on the "/var" partition.
261 sub checkdiskspace
() {
262 # Call diskfree to gather the free disk space of /var.
263 my @df = &General
::system_output
("/bin/df", "-B", "M", "/var");
265 # Loop through the output.
266 foreach my $line (@df) {
267 # Ignore header line.
268 next if $line =~ m/^Filesystem/;
270 # Search for a line with the device information.
271 if ($line =~ m/dev/ ) {
272 # Split the line into single pieces.
273 my @values = split(' ', $line);
274 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
276 # Check if the available disk space is more than 300MB.
277 if ($available < 300) {
278 # Exit function and return the available disk space.
284 # Everything okay, return nothing.
289 ## This function is responsible for downloading the ruleset for a given provider.
291 ## It uses the LWP-based downloader function from the general-functions.pl to
292 ## download the ruleset for a requested provider.
294 sub downloadruleset
($) {
299 # Grab the download url for the provider.
300 my $url = $IDS::Ruleset
::Providers
{$provider}{'dl_url'};
302 # Check if the provider requires a subscription.
303 if ($IDS::Ruleset
::Providers
{$provider}{'requires_subscription'} eq "True") {
304 # Grab the subscription code.
305 my $subscription_code = &get_subscription_code
($provider);
307 # Add the subscription code to the download url.
308 $url =~ s/\<subscription_code\>/$subscription_code/g;
312 # Abort and return "no url", if no url could be determined for the provider.
313 return "no url" unless ($url);
315 # Pass the requested URL to the settings hash.
316 $settings{'URL'} = $url;
318 # Call function to get the final path and filename for the downloaded file.
319 my $dl_rulesfile = &_get_dl_rulesfile
($provider);
321 # Add the file information to the settings hash.
322 $settings{'FILE'} = $dl_rulesfile;
324 # Add Etag details to the settings hash.
325 $settings{'ETAGSFILE'} = $etags_file;
326 $settings{'ETAGPREFIX'} = $provider;
328 # Call the downloader and pass the settings hash.
329 my $response = &HTTPClient
::downloader
(%settings);
331 # Return the response message if the downloader provided one.
336 # Set correct ownership for the downloaded rules file.
337 &set_ownership
("$dl_rulesfile");
343 ## Function to extract a given ruleset.
345 ## In case the ruleset provider offers a plain file, it simply will
348 sub extractruleset
($) {
351 # Get full path and downloaded rulesfile for the given provider.
352 my $tarball = &_get_dl_rulesfile
($provider);
354 # Check if the file exists.
355 unless (-f
$tarball) {
356 &_log_to_syslog
("Could not find ruleset file: $tarball");
362 # Check if the temporary directories exist, otherwise create them.
363 mkdir("$tmp_directory") unless (-d
"$tmp_directory");
364 mkdir("$tmp_rules_directory") unless (-d
"$tmp_rules_directory");
365 mkdir("$tmp_conf_directory") unless (-d
"$tmp_conf_directory");
367 # Omit the type (dl_type) of the stored ruleset.
368 my $type = $IDS::Ruleset
::Providers
{$provider}{'dl_type'};
370 # Handle the different ruleset types.
371 if ($type eq "plain") {
372 # Generate destination filename an full path.
373 my $destination = "$tmp_rules_directory/$provider\-ruleset.rules";
375 # Copy the file into the temporary rules directory.
376 copy
($tarball, $destination);
378 } elsif ( $type eq "archive") {
379 # Initialize the tar module.
380 my $tar = Archive
::Peek
::Libarchive-
>new(filename
=> $tarball);
382 # Loop through the archive
384 my ($packed_file, $content) = @_;
387 # Splitt the packed file into chunks.
388 my $file = fileparse
($packed_file);
390 # Handle msg-id.map file.
391 if ("$file" eq "sid-msg.map") {
392 # Set extract destination to temporary config_dir.
393 $destination = "$tmp_conf_directory/$provider\-sid-msg.map";
395 # Handle classification.conf
396 } elsif ("$file" eq "classification.config") {
397 # Set extract destination to temporary config_dir.
398 $destination = "$tmp_conf_directory/$provider\-classification.config";
400 # Handle rules files.
401 } elsif ($file =~ m/\.rules$/) {
402 # Skip rule files which are not located in the rules directory or archive root.
403 return unless(($packed_file =~ /^rules\//) || ($packed_file =~ /^$provider-rules\
//) || ($packed_file !~ /\//));
405 # Skip deleted.rules.
407 # Mostly they have been taken out for correctness or performance reasons and therfore
408 # it is not a great idea to enable any of them.
409 return if($file =~ m/deleted.rules$/);
413 # Splitt the filename into chunks.
414 my @filename = split("-", $file);
417 @filename = reverse(@filename);
419 # Get the amount of elements in the array.
420 my $elements = @filename;
422 # Remove last element of the hash.
423 # It contains the vendor name, which will be replaced.
424 if ($elements >= 3) {
425 # Remove last element from hash.
429 # Check if the last element of the filename does not
430 # contain the providers name.
431 if ($filename[-1] ne "$provider") {
432 # Add provider name as last element.
433 push(@filename, $provider);
436 # Reverse the array back.
437 @filename = reverse(@filename);
439 # Generate the name for the rulesfile.
440 $rulesfilename = join("-", @filename);
442 # Set extract destination to temporaray rules_dir.
443 $destination = "$tmp_rules_directory/$rulesfilename";
445 # Skip all other files.
449 # Check if the destination file exists.
450 unless(-e
"$destination") {
451 # Open filehandle to write the content to a new file.
452 open(FILE
, ">", "$destination") or die "Could not open $destination. $!\n";
454 # Open filehandle to append the content to the existing file.
455 open(FILE
, ">>", "$destination") or die "Could not open $destination. $!\n";
458 # Write the extracted file content to the filehandle.
459 print FILE
"$content" if ($content);
461 # Close the file handle.
468 ## A wrapper function to call the oinkmaster script, setup the rules structues and
469 ## call the functions to merge the additional config files. (classification, sid-msg, etc.).
472 # Check if the files in rulesdir have the correct permissions.
473 &_check_rulesdir_permissions
();
475 # Cleanup the rules directory before filling it with the new rulests.
476 &_cleanup_rulesdir
();
478 # Get all enabled providers.
479 my @enabled_providers = &get_enabled_providers
();
481 # Loop through the array of enabled providers.
482 foreach my $provider (@enabled_providers) {
483 # Call the extractruleset function.
484 &extractruleset
($provider);
487 # Call function to process the ruleset and do all modifications.
488 &process_ruleset
(@enabled_providers);
490 # Call function to merge the classification files.
491 &merge_classifications
(@enabled_providers);
493 # Call function to merge the sid to message mapping files.
494 &merge_sid_msg
(@enabled_providers);
496 # Cleanup temporary directory.
497 &cleanup_tmp_directory
();
501 ## Function to alter the ruleset.
503 sub process_ruleset
(@) {
504 my (@providers) = @_;
506 # Hash to store the configured provider modes.
507 my %providers_mode = &get_providers_mode
();
509 # Array to store the extracted rulefile from the temporary rules directory.
510 my @extracted_rulefiles;
512 # Get names of the extracted raw rulefiles.
513 opendir(DIR
, $tmp_rules_directory) or die "Could not read from $tmp_rules_directory. $!\n";
514 while (my $file = readdir(DIR
)) {
515 # Ignore single and double dotted files.
516 next if $file =~ /^\.\.?$/;
518 # Add file to the array of extracted files.
519 push(@extracted_rulefiles, $file);
522 # Close directory handle.
525 # Loop through the array of providers.
526 foreach my $provider (@providers) {
527 # Hash to store the obtained SIDs and REV of each provider.
530 # Hash which holds modifications to apply to the rules.
531 my %modifications = ();
533 # Loop through the array of extraced rulefiles.
534 foreach my $file (@extracted_rulefiles) {
535 # Skip file if it does not belong to the current processed provider.
536 next unless ($file =~ m/^$provider/);
539 open(FILE
, "$tmp_rules_directory/$file") or die "Could not read $tmp_rules_directory/$file. $!\n";
541 # Loop through the file content.
542 while (my $line = <FILE
>) {
544 next if ($line =~ /^\s*$/);
546 # Call function to get the sid and rev of the rule.
547 my ($sid, $rev) = &_get_sid_and_rev
($line);
549 # Skip rule if a sid with a higher rev already has added to the rules hash.
550 next if ($rev le $rules{$sid});
552 # Add the new or rule with higher rev to the hash of rules.
560 # Get filename which contains the ruleset modifications for this provider.
561 my $modification_file = &get_provider_ruleset_modifications_file
($provider);
563 # Read file which holds the modifications of the ruleset for the current provider.
564 &General
::readhash
($modification_file, \
%modifications) if (-f
$modification_file);
566 # Loop again through the array of extracted rulesfiles.
567 foreach my $file (@extracted_rulefiles) {
568 # Skip the file if it does not belong to the current provider.
569 next unless ($file =~ m/^$provider/);
571 # Open the rulefile for writing.
572 open(RULEFILE
, ">", "$rulespath/$file") or die "Could not write to file $rulespath/$file. $!\n";
574 # Open the rulefile for reading.
575 open(TMP_RULEFILE
, "$tmp_rules_directory/$file") or die "Could not read $tmp_rules_directory/$file. $!\n";
577 # Loop through the raw temporary rulefile.
578 while (my $line = <TMP_RULEFILE
>) {
579 # Get the sid and rev of the rule.
580 my ($sid, $rev) = &_get_sid_and_rev
($line);
582 # Check if the current rule is obsoleted by a newer one.
584 # In this case the rev number in the rules hash is higher than the current one.
585 next if ($rev lt $rules{$sid});
587 # Check if the rule should be enabled or disabled.
588 if ($modifications{$sid} eq "enabled") {
589 # Drop the # at the start of the line.
591 } elsif ($modifications{$sid} eq "disabled") {
592 # Add a # at the start of the line to disable the rule.
593 $line = "#$line" unless ($line =~ /^#/);
596 # Check if the Provider is set so IPS mode.
597 if ($providers_mode{$provider} eq "IPS") {
598 # Replacements for sourcefire rules.
599 $line =~ s/^#\s*(?:alert|drop)(.+policy balanced-ips alert)/alert${1}/;
600 $line =~ s/^#\s*(?:alert|drop)(.+policy balanced-ips drop)/drop${1}/;
602 # Replacements for generic rules.
603 $line =~ s/^(#?)\s*(?:alert|drop)/${1}drop/;
604 $line =~ s/^(#?)\s*drop(.+flowbits:noalert;)/${1}alert${2}/;
607 # Write line / rule to the target rule file.
608 print RULEFILE
"$line";
619 ## Function to merge the classifications for a given amount of providers and write them
620 ## to the classifications file.
622 sub merge_classifications
(@) {
625 # Hash to store all collected classifications.
626 my %classifications = ();
628 # Loop through the given array of providers.
629 foreach my $provider (@providers) {
630 # Generate full path to classification file.
631 my $classification_file = "$tmp_conf_directory/$provider\-classification.config";
633 # Skip provider if no classification file exists.
634 next unless (-f
"$classification_file");
636 # Open the classification file.
637 open(CLASSIFICATION
, $classification_file) or die "Could not open file $classification_file. $!\n";
639 # Loop through the file content.
640 while(<CLASSIFICATION
>) {
641 # Parse the file and grab the classification details.
642 if ($_ =~/.*config classification\: (.*)/) {
643 # Split the grabbed details.
644 my ($short_name, $short_desc, $priority) = split("\,", $1);
646 # Check if the grabbed classification is allready known and the priority value is greater
647 # than the stored one (which causes less priority in the IDS).
648 if (($classifications{$short_name}) && ($classifications{$short_name}[1] >= $priority)) {
649 #Change the priority value to the stricter one.
650 $classifications{$short_name} = [ "$classifications{$short_name}[0]", "$priority" ];
652 # Add the classification to the hash.
653 $classifications{$short_name} = [ "$short_desc", "$priority" ];
659 close(CLASSIFICATION
);
662 # Open classification file for writing.
663 open(FILE
, ">", "$classification_file") or die "Could not write to $classification_file. $!\n";
665 # Print notice about autogenerated file.
666 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n\n";
668 # Sort and loop through the hash of classifications.
669 foreach my $key (sort keys %classifications) {
670 # Assign some nice variable names for the items.
671 my $short_name = $key;
672 my $short_desc = $classifications{$key}[0];
673 my $priority = $classifications{$key}[1];
675 # Write the classification to the file.
676 print FILE
"config classification: $short_name,$short_desc,$priority\n";
684 ## Function to merge the "sid to message mapping" files of various given providers.
686 sub merge_sid_msg
(@) {
689 # Hash which contains all the sid to message mappings.
692 # Loop through the array of given providers.
693 foreach my $provider (@providers) {
694 # Generate full path and filename.
695 my $sid_msg_file = "$tmp_conf_directory/$provider\-sid-msg.map";
697 # Skip provider if no sid to msg mapping file for this provider exists.
698 next unless (-f
$sid_msg_file);
701 open(MAPPING
, $sid_msg_file) or die "Could not open $sid_msg_file. $!\n";
703 # Loop through the file content.
708 # Skip lines which do not start with a number,
709 next unless ($_ =~ /^\d+/);
711 # Split line content and assign it to an array.
712 my @line = split(/ \|\| /, $_);
714 # Grab the first element (and remove it) from the line array.
715 # It contains the sid.
716 my $sid = shift(@line);
718 # Store the grabbed sid and the remain array as hash value.
719 # It still contains the messages, references etc.
720 $mappings{$sid} = [@line];
727 # Open mappings file for writing.
728 open(FILE
, ">", $sid_msg_file) or die "Could not write $sid_msg_file. $!\n";
730 # Write notice about autogenerated file.
731 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n\n";
733 # Loop through the hash of mappings.
734 foreach my $sid ( sort keys %mappings) {
735 # Grab data for the sid.
736 my @data = @{$mappings{$sid}};
738 # Add the sid to the data array.
739 unshift(@data, $sid);
742 my $line = join(" \|\| ", @data);
744 print FILE
"$line\n";
753 ## A very tiny function to move an extracted ruleset from the temporary directory into
754 ## the rules directory.
756 sub move_tmp_ruleset
() {
757 # Do a directory listing of the temporary directory.
758 opendir DH
, $tmp_rules_directory;
760 # Loop over all files.
761 while(my $file = readdir DH
) {
762 # Move them to the rules directory.
763 move
"$tmp_rules_directory/$file" , "$rulespath/$file";
766 # Close directory handle.
771 ## Function to cleanup the temporary IDS directroy.
773 sub cleanup_tmp_directory
() {
775 # Delete temporary directory and all containing files.
776 rmtree
([ "$tmp_directory" ]);
780 ## Function to do all the logging stuff if the downloading or updating of the ruleset fails.
785 # Remove any newline.
788 # Call private function to log the error message to syslog.
789 &_log_to_syslog
($error);
791 # Call private function to write/store the error message in the storederrorfile.
792 &_store_error_message
($error);
796 ## Function to log a given error message to the kernel syslog.
798 sub _log_to_syslog
($) {
801 # The syslog function works best with an array based input,
802 # so generate one before passing the message details to syslog.
803 my @syslog = ("ERR", "<ERROR> $message");
805 # Establish the connection to the syslog service.
806 openlog
('oinkmaster', 'cons,pid', 'user');
808 # Send the log message.
811 # Close the log handle.
816 ## Private function to write a given error message to the storederror file.
818 sub _store_error_message
($) {
821 # Remove any newline.
824 # Open file for writing.
825 open (ERRORFILE
, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n";
827 # Write error to file.
828 print ERRORFILE
"$message\n";
833 # Set correct ownership for the file.
834 &set_ownership
("$storederrorfile");
838 ## Private function to get the path and filename for a downloaded ruleset by a given provider.
840 sub _get_dl_rulesfile
($) {
843 # Abort if the requested provider is not known.
844 return unless($IDS::Ruleset
::Providers
{$provider});
846 # Try to gather the download type for the given provider.
847 my $dl_type = $IDS::Ruleset
::Providers
{$provider}{'dl_type'};
849 # Check if a download type could be grabbed.
851 # Obtain the file suffix for the download file type.
852 my $suffix = $dl_type_to_suffix{$dl_type};
854 # Check if a suffix has been found.
856 # Abort return - nothing.
860 # Generate the full filename and path for the stored rules file.
861 my $rulesfile = "$dl_rules_path/$dl_rulesfile_prefix-$provider$suffix";
863 # Return the generated filename.
867 # A downloaded ruleset for a provider which is not supported anymore is requested.
869 # Try to enumerate the downloaded ruleset file.
870 foreach my $dl_type (keys %dl_type_to_suffix) {
871 # Get the file suffix for the supported type.
872 my $suffix = $dl_type_to_suffix{$dl_type};
874 # Generate possible ruleset file name.
875 my $rulesfile = "$dl_rules_path/$dl_rulesfile_prefix-$provider$suffix";
877 # Check if such a file exists.
879 # Downloaded rulesfile found - Return the filename.
885 # If we got here, no rulesfile could be determined - return nothing.
890 ## Private function to obtain the sid and rev of a rule.
892 ## Returns an array with the sid as first and the rev as second value.
894 sub _get_sid_and_rev
($) {
899 # Use regex to obtain the sid and rev.
900 if ($line =~ m/.*sid:\s*(.*?);.*rev:\s*(.*?);/) {
901 # Add the sid and rev to the array.
911 ## Tiny function to delete the stored ruleset file or tarball for a given provider.
913 sub drop_dl_rulesfile
($) {
916 # Gather the full path and name of the stored rulesfile.
917 my $rulesfile = &_get_dl_rulesfile
($provider);
919 # Check if the given rulesfile exists.
921 # Delete the stored rulesfile.
922 unlink($rulesfile) or die "Could not delete $rulesfile. $!\n";
927 ## Function to check if the IDS is running.
929 sub ids_is_running
() {
931 # Open PID file for reading.
932 open(PIDFILE
, "$idspidfile") or die "Could not open $idspidfile. $!\n";
934 # Grab the process-id.
940 # Remove any newline.
943 # Check if a directory for the process-id exists in proc.
944 if(-d
"/proc/$pid") {
945 # The IDS daemon is running return the process id.
950 # Return nothing - IDS is not running.
955 ## Function to call suricatactrl binary with a given command.
957 sub call_suricatactrl
($) {
959 my ($option, $interval) = @_;
961 # Loop through the array of supported commands and check if
962 # the given one is part of it.
963 foreach my $cmd (@suricatactrl_cmds) {
964 # Skip current command unless the given one has been found.
965 next unless($cmd eq $option);
967 # Call the suricatactrl binary and pass the requrested
969 &General
::system("$suricatactrl", "$option");
975 # Command not found - return nothing.
980 ## Function to create a new empty file.
982 sub create_empty_file
($) {
985 # Check if the given file exists.
987 # Do nothing to prevent from overwriting existing files.
991 # Open the file for writing.
992 open(FILE
, ">$file") or die "Could not write to $file. $!\n";
1002 ## Private function to check if the file permission of the rulespath are correct.
1003 ## If not, call suricatactrl to fix them.
1005 sub _check_rulesdir_permissions
() {
1006 # Check if the rulepath main directory is writable.
1007 unless (-W
$rulespath) {
1008 # If not call suricatctrl to fix it.
1009 &call_suricatactrl
("fix-rules-dir");
1012 # Open snort rules directory and do a directory listing.
1013 opendir(DIR
, $rulespath) or die $!;
1014 # Loop through the direcory.
1015 while (my $file = readdir(DIR
)) {
1016 # We only want files.
1017 next unless (-f
"$rulespath/$file");
1019 # Check if the file is writable by the user.
1020 if (-W
"$rulespath/$file") {
1021 # Everything is okay - go on to the next file.
1024 # There are wrong permissions, call suricatactrl to fix it.
1025 &call_suricatactrl
("fix-rules-dir");
1031 ## Private function to cleanup the directory which contains
1032 ## the IDS rules, before extracting and modifing the new ruleset.
1034 sub _cleanup_rulesdir
() {
1035 # Open rules directory and do a directory listing.
1036 opendir(DIR
, $rulespath) or die $!;
1038 # Loop through the direcory.
1039 while (my $file = readdir(DIR
)) {
1040 # We only want files.
1041 next unless (-f
"$rulespath/$file");
1043 # Skip rules file for whitelisted hosts.
1044 next if ("$rulespath/$file" eq $whitelist_file);
1046 # Skip rules file with local rules.
1047 next if ("$rulespath/$file" eq $local_rules_file);
1049 # Delete the current processed file, if not, exit this function
1050 # and return an error message.
1051 unlink("$rulespath/$file") or return "Could not delete $rulespath/$file. $!\n";
1059 ## Function to generate the file which contains the home net information.
1061 sub generate_home_net_file
() {
1064 # Read-in network settings.
1065 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
1067 # Get available network zones.
1068 my @network_zones = &Network
::get_available_network_zones
();
1070 # Temporary array to store network address and prefix of the configured
1074 # Loop through the array of available network zones.
1075 foreach my $zone (@network_zones) {
1076 # Check if the current processed zone is red.
1077 if($zone eq "red") {
1078 # Grab the IP-address of the red interface.
1079 my $red_address = &get_red_address
();
1081 # Check if an address has been obtained.
1083 # Generate full network string.
1084 my $red_network = join("/", $red_address, "32");
1086 # Add the red network to the array of networks.
1087 push(@networks, $red_network);
1090 # Check if the configured RED_TYPE is static.
1091 if ($netsettings{'RED_TYPE'} eq "STATIC") {
1092 # Get configured and enabled aliases.
1093 my @aliases = &get_aliases
();
1095 # Loop through the array.
1096 foreach my $alias (@aliases) {
1098 my $network = join("/", $alias, "32");
1100 # Add the generated network to the array of networks.
1101 push(@networks, $network);
1104 # Process remaining network zones.
1106 # Convert current zone name into upper case.
1109 # Generate key to access the required data from the netsettings hash.
1110 my $zone_netaddress = $zone . "_NETADDRESS";
1111 my $zone_netmask = $zone . "_NETMASK";
1113 # Obtain the settings from the netsettings hash.
1114 my $netaddress = $netsettings{$zone_netaddress};
1115 my $netmask = $netsettings{$zone_netmask};
1117 # Convert the subnetmask into prefix notation.
1118 my $prefix = &Network
::convert_netmask2prefix
($netmask);
1120 # Generate full network string.
1121 my $network = join("/", $netaddress,$prefix);
1123 # Check if the network is valid.
1124 if(&Network
::check_subnet
($network)) {
1125 # Add the generated network to the array of networks.
1126 push(@networks, $network);
1131 # Format home net declaration.
1132 my $line = "\"[" . join(',', @networks) . "]\"";
1134 # Open file to store the addresses of the home net.
1135 open(FILE
, ">$homenet_file") or die "Could not open $homenet_file. $!\n";
1137 # Print yaml header.
1138 print FILE
"%YAML 1.1\n";
1139 print FILE
"---\n\n";
1141 # Print notice about autogenerated file.
1142 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1144 # Print the generated and required HOME_NET declaration to the file.
1145 print FILE
"HOME_NET:\t$line\n";
1147 # Close file handle.
1152 # Function to generate and write the file which contains the configured and used DNS servers.
1154 sub generate_dns_servers_file
() {
1155 # Get the used DNS servers.
1156 my @nameservers = &General
::get_nameservers
();
1158 # Get network settings.
1160 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
1162 # Format dns servers declaration.
1165 # Check if the system has configured nameservers.
1167 # Add the GREEN address as DNS servers.
1168 push(@nameservers, $netsettings{'GREEN_ADDRESS'});
1170 # Check if a BLUE zone exists.
1171 if ($netsettings{'BLUE_ADDRESS'}) {
1172 # Add the BLUE address to the array of nameservers.
1173 push(@nameservers, $netsettings{'BLUE_ADDRESS'});
1176 # Generate the line which will be written to the DNS servers file.
1177 $line = join(",", @nameservers);
1179 # External net simply contains (any).
1180 $line = "\$EXTERNAL_NET";
1183 # Open file to store the used DNS server addresses.
1184 open(FILE
, ">$dns_servers_file") or die "Could not open $dns_servers_file. $!\n";
1186 # Print yaml header.
1187 print FILE
"%YAML 1.1\n";
1188 print FILE
"---\n\n";
1190 # Print notice about autogenerated file.
1191 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1193 # Print the generated DNS declaration to the file.
1194 print FILE
"DNS_SERVERS:\t\"[$line]\"\n";
1196 # Close file handle.
1201 # Function to generate and write the file which contains the HTTP_PORTS definition.
1203 sub generate_http_ports_file
() {
1206 # Read-in proxy settings
1207 &General
::readhash
("${General::swroot}/proxy/advanced/settings", \
%proxysettings);
1209 # Check if the proxy is enabled.
1210 if (( -e
"${General::swroot}/proxy/enable") || (-e
"${General::swroot}/proxy/enable_blue")) {
1211 # Add the proxy port to the array of HTTP ports.
1212 push(@http_ports, $proxysettings{'PROXY_PORT'});
1215 # Check if the transparent mode of the proxy is enabled.
1216 if ((-e
"${General::swroot}/proxy/transparent") || (-e
"${General::swroot}/proxy/transparent_blue")) {
1217 # Add the transparent proxy port to the array of HTTP ports.
1218 push(@http_ports, $proxysettings{'TRANSPARENT_PORT'});
1221 # Format HTTP_PORTS declaration.
1224 # Generate line which will be written to the http ports file.
1225 $line = join(",", @http_ports);
1227 # Open file to store the HTTP_PORTS.
1228 open(FILE
, ">$http_ports_file") or die "Could not open $http_ports_file. $!\n";
1230 # Print yaml header.
1231 print FILE
"%YAML 1.1\n";
1232 print FILE
"---\n\n";
1234 # Print notice about autogenerated file.
1235 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1237 # Print the generated HTTP_PORTS declaration to the file.
1238 print FILE
"HTTP_PORTS:\t\"[$line]\"\n";
1240 # Close file handle.
1245 ## Function to write the file that contains the rulefiles which are loaded by suricaa.
1247 ## This function requires an array of used provider handles.
1249 sub write_used_rulefiles_file
(@) {
1250 my (@providers) = @_;
1252 # Get the enabled application layer protocols.
1253 my @enabled_app_layer_protos = &get_suricata_enabled_app_layer_protos
();
1256 open (FILE
, ">", $suricata_used_rulesfiles_file) or die "Could not write to $suricata_used_rulesfiles_file. $!\n";
1258 print FILE
"%YAML 1.1\n";
1259 print FILE
"---\n\n";
1261 # Write notice about autogenerated file.
1262 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1264 # Loop through the array of static included rulesfiles.
1265 foreach my $file (@static_included_rulefiles) {
1266 # Check if the file exists.
1267 if (-f
"$rulespath/$file") {
1268 # Write the rulesfile name to the file.
1269 print FILE
" - $rulespath/$file\n";
1273 if ($LOG_APP_LAYER_EVENTS) {
1274 print FILE
"\n#Default rules for used application layer protocols.\n";
1275 foreach my $enabled_app_layer_proto (@enabled_app_layer_protos) {
1276 # Check if the current processed app layer proto needs to be translated
1277 # into an application name.
1278 if (exists($tr_app_layer_proto{$enabled_app_layer_proto})) {
1279 # Obtain the translated application name for this protocol.
1280 $enabled_app_layer_proto = $tr_app_layer_proto{$enabled_app_layer_proto};
1283 # Generate filename.
1284 my $rulesfile = "$default_rulespath/$enabled_app_layer_proto\.rules";
1286 # Check if such a file exists.
1287 if (-f
"$rulesfile") {
1288 # Write the rulesfile name to the file.
1289 print FILE
" - $rulesfile\n";
1292 # Generate filename with "events" in filename.
1293 $rulesfile = "$default_rulespath/$enabled_app_layer_proto\-events.rules";
1295 # Check if this file exists.
1296 if (-f
"$rulesfile" ) {
1297 # Write the rulesfile name to the file.
1298 print FILE
" - $rulesfile\n";
1303 # Loop through the array of enabled providers.
1304 foreach my $provider (@providers) {
1305 # Skip unsupported providers.
1306 next unless ($IDS::Ruleset
::Providers
{$provider});
1308 # Get the used rulefile for this provider.
1309 my @used_rulesfiles = &get_provider_used_rulesfiles
($provider);
1311 # Check if there are
1312 if(@used_rulesfiles) {
1313 # Add notice to the file.
1314 print FILE
"\n#Used Rulesfiles for provider $provider.\n";
1316 # Loop through the array of used rulefiles.
1317 foreach my $enabled_rulesfile (@used_rulesfiles) {
1318 # Generate name and full path to the rulesfile.
1319 my $rulesfile = "$rulespath/$enabled_rulesfile";
1321 # Write the ruelsfile name to the file.
1322 print FILE
" - $rulesfile\n";
1327 # Close the file handle
1332 ## Tiny function to generate the full path and name for the file which stores the used rulefiles of a given provider.
1334 sub get_provider_used_rulesfiles_file
($) {
1335 my ($provider) = @_;
1337 my $filename = "$settingsdir/$provider\-used\-rulesfiles";
1339 # Return the gernerated file.
1344 ## Tiny function to generate the full path and name for the file which stores the modifications of a ruleset.
1346 sub get_provider_ruleset_modifications_file
($) {
1347 my ($provider) = @_;
1349 my $filename = "$settingsdir/$provider\-modifications";
1351 # Return the filename.
1356 ## Function to get the subscription code of a configured provider.
1358 sub get_subscription_code
($) {
1359 my ($provider) = @_;
1361 my %configured_providers = ();
1363 # Read-in providers settings file.
1364 &General
::readhasharray
($providers_settings_file, \
%configured_providers);
1366 # Loop through the hash of configured providers.
1367 foreach my $id (keys %configured_providers) {
1368 # Assign nice human-readable values to the data fields.
1369 my $provider_handle = $configured_providers{$id}[0];
1370 my $subscription_code = $configured_providers{$id}[1];
1372 # Check if the current processed provider is the requested one.
1373 if ($provider_handle eq $provider) {
1374 # Return the obtained subscription code.
1375 return $subscription_code;
1379 # No subscription code found - return nothing.
1384 ## Function to get the ruleset date for a given provider.
1386 ## The function simply return the creation date in a human read-able format
1387 ## of the stored providers rulesfile.
1389 sub get_ruleset_date
($) {
1390 my ($provider) = @_;
1394 # Get the stored rulesfile for this provider.
1395 my $stored_rulesfile = &_get_dl_rulesfile
($provider);
1397 # Check if we got a file.
1398 if (-f
$stored_rulesfile) {
1399 # Call stat on the rulestarball.
1400 my $stat = stat("$stored_rulesfile");
1402 # Get timestamp the file creation.
1403 $mtime = $stat->mtime;
1406 # Check if the timestamp has not been grabbed.
1408 # Return N/A for Not available.
1412 # Convert into human read-able format.
1413 $date = strftime
('%Y-%m-%d %H:%M:%S', localtime($mtime));
1420 ## Function to gather the version of suricata.
1422 sub get_suricata_version
($) {
1425 # Execute piped suricata command and return the version information.
1426 open(SURICATA
, "suricata -V |") or die "Couldn't execute program: $!";
1428 # Grab and store the output of the piped program.
1429 my $version_string = <SURICATA
>;
1435 chomp($version_string);
1437 # Grab the version from the version string.
1438 $version_string =~ /([0-9]+([.][0-9]+)+)/;
1440 # Splitt the version into single chunks.
1441 my ($major_ver, $minor_ver, $patchlevel) = split(/\./, $1);
1443 # Check and return the requested version sheme.
1444 if ($format eq "major") {
1445 # Return the full version.
1446 return "$major_ver";
1447 } elsif ($format eq "minor") {
1448 # Return the major and minor part.
1449 return "$major_ver.$minor_ver";
1451 # Return the full version string.
1452 return "$major_ver.$minor_ver.$patchlevel";
1457 ## Function to get the enabled application layer protocols.
1459 sub get_suricata_enabled_app_layer_protos
() {
1460 # Array to store and return the enabled app layer protos.
1461 my @enabled_app_layer_protos = ();
1463 # Execute piped suricata command and return the list of
1464 # enabled application layer protocols.
1465 open(SURICATA
, "suricata --list-app-layer-protos |") or die "Could not execute program: $!";
1467 # Grab and store the list of enabled application layer protocols.
1468 my @output = <SURICATA
>;
1473 # Merge allways enabled static application layers protocols array.
1474 @enabled_app_layer_protos = @static_enabled_app_layer_protos;
1476 # Loop through the array which contains the output of suricata.
1477 foreach my $line (@output) {
1478 # Skip header line which starts with "===".
1479 next if ($line =~ /^\s*=/);
1481 # Skip info or warning lines.
1482 next if ($line =~ /\s*--/);
1487 # Add enabled app layer proto to the array.
1488 push(@enabled_app_layer_protos, $line);
1492 @enabled_app_layer_protos = sort(@enabled_app_layer_protos);
1495 return @enabled_app_layer_protos;
1499 ## Function to generate the rules file with whitelisted addresses.
1501 sub generate_ignore_file
() {
1504 # SID range 1000000-1999999 Reserved for Local Use
1505 # Put your custom rules in this range to avoid conflicts
1508 # Read-in ignoredfile.
1509 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
1511 # Open ignorefile for writing.
1512 open(FILE
, ">$IDS::whitelist_file") or die "Could not write to $IDS::whitelist_file. $!\n";
1514 # Config file header.
1515 print FILE
"# Autogenerated file.\n";
1516 print FILE
"# All user modifications will be overwritten.\n\n";
1518 # Add all user defined addresses to the whitelist.
1520 # Check if the hash contains any elements.
1521 if (keys (%ignored)) {
1522 # Loop through the entire hash and write the host/network
1523 # and remark to the ignore file.
1524 while ( (my $key) = each %ignored) {
1525 my $address = $ignored{$key}[0];
1526 my $remark = $ignored{$key}[1];
1527 my $status = $ignored{$key}[2];
1529 # Check if the status of the entry is "enabled".
1530 if ($status eq "enabled") {
1531 # Check if the address/network is valid.
1532 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
1533 # Write rule line to the file to pass any traffic from this IP
1534 print FILE
"pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; bypass; sid:$sid\;)\n";
1547 ## Function to set correct ownership for single files and directories.
1550 sub set_ownership
($) {
1553 # User and group of the WUI.
1554 my $uname = "nobody";
1555 my $grname = "nobody";
1557 # The chown function implemented in perl requies the user and group as nummeric id's.
1558 my $uid = getpwnam($uname);
1559 my $gid = getgrnam($grname);
1561 # Check if the given target exists.
1563 # Stop the script and print error message.
1564 die "The $target does not exist. Cannot change the ownership!\n";
1567 # Check weather the target is a file or directory.
1569 # Change ownership ot the single file.
1570 chown($uid, $gid, "$target");
1571 } elsif (-d
$target) {
1572 # Do a directory listing.
1573 opendir(DIR
, $target) or die $!;
1574 # Loop through the direcory.
1575 while (my $file = readdir(DIR
)) {
1577 # We only want files.
1578 next unless (-f
"$target/$file");
1580 # Set correct ownership for the files.
1581 chown($uid, $gid, "$target/$file");
1586 # Change ownership of the directory.
1587 chown($uid, $gid, "$target");
1592 ## Function to read-in the aliases file and returns all configured and enabled aliases.
1595 # Location of the aliases file.
1596 my $aliases_file = "${General::swroot}/ethernet/aliases";
1598 # Array to store the aliases.
1601 # Check if the file is empty.
1602 if (-z
$aliases_file) {
1603 # Abort nothing to do.
1607 # Open the aliases file.
1608 open(ALIASES
, $aliases_file) or die "Could not open $aliases_file. $!\n";
1610 # Loop through the file content.
1611 while (my $line = <ALIASES
>) {
1615 # Splitt line content into single chunks.
1616 my ($address, $state, $remark) = split(/\,/, $line);
1618 # Check if the state of the current processed alias is "on".
1619 if ($state eq "on") {
1620 # Check if the address is valid.
1621 if(&Network
::check_ip_address
($address)) {
1622 # Add the alias to the array of aliases.
1623 push(@aliases, $address);
1628 # Close file handle.
1636 ## Function to grab the current assigned IP-address on red.
1638 sub get_red_address
() {
1639 # File, which contains the current IP-address of the red interface.
1640 my $file = "${General::swroot}/red/local-ipaddress";
1642 # Check if the file exists.
1644 # Open the given file.
1645 open(FILE
, "$file") or die "Could not open $file.";
1647 # Obtain the address from the first line of the file.
1648 my $address = <FILE
>;
1656 # Check if the grabbed address is valid.
1657 if (&General
::validip
($address)) {
1658 # Return the address.
1668 ## Function to get the used rules files of a given provider.
1670 sub get_provider_used_rulesfiles
($) {
1671 my ($provider) = @_;
1673 # Hash to store the used rulefiles of the provider.
1674 my %provider_rulefiles = ();
1676 # Array to store the used rulefiles.
1677 my @used_rulesfiles = ();
1679 # Get the filename which contains the used rulefiles for this provider.
1680 my $used_rulesfiles_file = &get_provider_used_rulesfiles_file
($provider);
1682 # Read-in file, if it exists.
1683 &General
::readhash
("$used_rulesfiles_file", \
%provider_rulefiles) if (-f
$used_rulesfiles_file);
1685 # Loop through the hash of rulefiles which does the provider offer.
1686 foreach my $rulefile (keys %provider_rulefiles) {
1687 # Skip disabled rulefiles.
1688 next unless($provider_rulefiles{$rulefile} eq "enabled");
1690 # The General::readhash function does not allow dots as
1691 # key value and limits the key "string" to the part before
1692 # the dot, in case it contains one.
1694 # So add the file extension for the rules file manually again.
1695 $rulefile = "$rulefile.rules";
1697 # Add the enabled rulefile to the array of enabled rulefiles.
1698 push(@used_rulesfiles, $rulefile);
1701 # Return the array of used rulesfiles.
1702 return @used_rulesfiles;
1706 ## Function to delete the stored etag data of a given provider.
1708 sub remove_from_etags
($) {
1709 my ($provider) = @_;
1713 # Early exit function if the etags file does not exist.
1714 return unless (-f
$etags_file);
1716 # Read-in etag file.
1717 &General
::readhash
("$etags_file", \
%etags);
1719 # Check if the hash contains an entry for the given provider.
1720 if ($etags{$provider}) {
1722 delete($etags{$provider});
1724 # Write back the etags file.
1725 &General
::writehash
("$etags_file", \
%etags);
1730 ## Function to write the lock file for locking the WUI, while
1731 ## the autoupdate script runs.
1733 sub lock_ids_page
() {
1734 # Call subfunction to create the file.
1735 &create_empty_file
($ids_page_lock_file);
1739 ## Function to release the lock of the WUI, again.
1741 sub unlock_ids_page
() {
1743 unlink($ids_page_lock_file);