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}/suricata/ruleset-sources";
32 # Load perl module to deal with Archives.
35 # Load perl module to deal with files and path.
38 # Load module to move files.
41 # Load module to recursely remove files and a folder.
42 use File
::Path
qw(rmtree);
44 # Load module to get file stats.
47 # Load module to deal with temporary files.
50 # Load module to deal with the date formats used by the HTTP protocol.
53 # Load the libwwwperl User Agent module.
56 # Load function from posix module to format time strings.
57 use POSIX qw
(strftime
);
59 # Load module to talk to the kernel log daemon.
60 use Sys
::Syslog
qw(:DEFAULT setlogsock);
62 # Location where all config and settings files are stored.
63 our $settingsdir = "${General::swroot}/suricata";
65 # File where the main file for providers ruleset inclusion exists.
66 our $suricata_used_providers_file = "$settingsdir/suricata-used-providers.yaml";
68 # File for static ruleset inclusions.
69 our $suricata_default_rulefiles_file = "$settingsdir/suricata-default-rules.yaml";
71 # File where the addresses of the homenet are stored.
72 our $homenet_file = "$settingsdir/suricata-homenet.yaml";
74 # File where the addresses of the used DNS servers are stored.
75 our $dns_servers_file = "$settingsdir/suricata-dns-servers.yaml";
77 # File where the HTTP ports definition is stored.
78 our $http_ports_file = "$settingsdir/suricata-http-ports.yaml";
80 # File which contains includes for provider specific rule modifications.
81 our $oinkmaster_provider_includes_file = "$settingsdir/oinkmaster-provider-includes.conf";
83 # File which contains wheater the rules should be changed.
84 our $modify_sids_file = "$settingsdir/oinkmaster-modify-sids.conf";
86 # File which stores the configured IPS settings.
87 our $ids_settings_file = "$settingsdir/settings";
89 # File which stores the used and configured ruleset providers.
90 our $providers_settings_file = "$settingsdir/providers-settings";
92 # File which stores the configured settings for whitelisted addresses.
93 our $ignored_file = "$settingsdir/ignored";
95 # Location where the downloaded rulesets are stored.
96 our $dl_rules_path = "/var/tmp";
98 # File to store any errors, which also will be read and displayed by the wui.
99 our $storederrorfile = "/tmp/ids_storederror";
101 # File to lock the WUI, while the autoupdate script runs.
102 our $ids_page_lock_file = "/tmp/ids_page_locked";
104 # Location where the rulefiles are stored.
105 our $rulespath = "/var/lib/suricata";
107 # Location where the default rulefils are stored.
108 our $default_rulespath = "/usr/share/suricata/rules";
110 # Location where the addition config files are stored.
111 our $configspath = "/usr/share/suricata";
113 # Location of the classification file.
114 our $classification_file = "$configspath/classification.config";
116 # Location of the sid to msg mappings file.
117 our $sid_msg_file = "$rulespath/sid-msg.map";
119 # Location to store local rules. This file will not be touched.
120 our $local_rules_file = "$rulespath/local.rules";
122 # File which contains the rules to whitelist addresses on suricata.
123 our $whitelist_file = "$rulespath/whitelist.rules";
125 # File which contains a list of all supported ruleset sources.
126 # (Sourcefire, Emergingthreads, etc..)
127 our $rulesetsourcesfile = "$settingsdir/ruleset-sources";
129 # The pidfile of the IDS.
130 our $idspidfile = "/var/run/suricata.pid";
132 # Location of suricatactrl.
133 my $suricatactrl = "/usr/local/bin/suricatactrl";
135 # Prefix for each downloaded ruleset.
136 my $dl_rulesfile_prefix = "idsrules";
138 # Temporary directory where the rulesets will be extracted.
139 my $tmp_directory = "/tmp/ids_tmp";
141 # Temporary directory where the extracted rules files will be stored.
142 my $tmp_rules_directory = "$tmp_directory/rules";
144 # Temporary directory where the extracted additional config files will be stored.
145 my $tmp_conf_directory = "$tmp_directory/conf";
147 # Array with allowed commands of suricatactrl.
148 my @suricatactrl_cmds = ( 'start', 'stop', 'restart', 'reload', 'fix-rules-dir', 'cron' );
150 # Array with supported cron intervals.
151 my @cron_intervals = ('off', 'daily', 'weekly' );
153 # Array which contains the HTTP ports, which statically will be declared as HTTP_PORTS in the
155 my @http_ports = ('80', '81');
157 # Array which contains a list of rulefiles which always will be included if they exist.
158 my @static_included_rulefiles = ('local.rules', 'whitelist.rules');
160 # Array which contains a list of allways enabled application layer protocols.
161 my @static_enabled_app_layer_protos = ('app-layer', 'decoder', 'files', 'stream');
163 # Hash which allows to convert the download type (dl_type) to a file suffix.
164 my %dl_type_to_suffix = (
165 "archive" => ".tar.gz",
169 # Hash to translate an application layer protocol to the application name.
170 my %tr_app_layer_proto = (
172 "krb5" => "kerberos",
176 ## Function to check and create all IDS related files, if the does not exist.
178 sub check_and_create_filelayout
() {
179 # Check if the files exist and if not, create them.
180 unless (-f
"$oinkmaster_provider_includes_file") { &create_empty_file
($oinkmaster_provider_includes_file); }
181 unless (-f
"$modify_sids_file") { &create_empty_file
($modify_sids_file); }
182 unless (-f
"$suricata_used_providers_file") { &create_empty_file
($suricata_used_providers_file); }
183 unless (-f
"$suricata_default_rulefiles_file") { &create_empty_file
($suricata_default_rulefiles_file); }
184 unless (-f
"$ids_settings_file") { &create_empty_file
($ids_settings_file); }
185 unless (-f
"$providers_settings_file") { &create_empty_file
($providers_settings_file); }
186 unless (-f
"$whitelist_file" ) { &create_empty_file
($whitelist_file); }
190 ## Function to get a list of all available ruleset providers.
192 ## They will be returned as a sorted array.
194 sub get_ruleset_providers
() {
197 # Loop through the hash of providers.
198 foreach my $provider ( keys %IDS::Ruleset
::Providers
) {
199 # Add the provider to the array.
200 push(@providers, $provider);
203 # Sort and return the array.
204 return sort(@providers);
208 ## Function to get a list of all enabled ruleset providers.
210 ## They will be returned as an array.
212 sub get_enabled_providers
() {
213 my %used_providers = ();
215 # Array to store the enabled providers.
216 my @enabled_providers = ();
218 # Read-in the providers config file.
219 &General
::readhasharray
("$providers_settings_file", \
%used_providers);
221 # Loop through the hash of used_providers.
222 foreach my $id (keys %used_providers) {
223 # Skip disabled providers.
224 next unless ($used_providers{$id}[3] eq "enabled");
226 # Grab the provider handle.
227 my $provider = "$used_providers{$id}[0]";
229 # Add the provider to the array of enabled providers.
230 push(@enabled_providers, $provider);
234 return @enabled_providers;
238 ## Function to get a hash of provider handles and their configured modes (IDS/IPS).
240 sub get_providers_mode
() {
241 my %used_providers = ();
243 # Hash to store the providers and their configured modes.
244 my %providers_mode = ();
246 # Read-in the providers config file.
247 &General
::readhasharray
("$providers_settings_file", \
%used_providers);
249 # Loop through the hash of used_providers.
250 foreach my $id (keys %used_providers) {
251 # Skip disabled providers.
252 next unless ($used_providers{$id}[3] eq "enabled");
254 # Grab the provider handle.
255 my $provider = "$used_providers{$id}[0]";
257 # Grab the provider mode.
258 my $mode = "$used_providers{$id}[4]";
260 # Fall back to IDS if no mode could be obtained.
265 # Add details to provider_modes hash.
266 $providers_mode{$provider} = $mode;
270 return %providers_mode;
274 ## Function for checking if at least 300MB of free disk space are available
275 ## on the "/var" partition.
277 sub checkdiskspace
() {
278 # Call diskfree to gather the free disk space of /var.
279 my @df = &General
::system_output
("/bin/df", "-B", "M", "/var");
281 # Loop through the output.
282 foreach my $line (@df) {
283 # Ignore header line.
284 next if $line =~ m/^Filesystem/;
286 # Search for a line with the device information.
287 if ($line =~ m/dev/ ) {
288 # Split the line into single pieces.
289 my @values = split(' ', $line);
290 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
292 # Check if the available disk space is more than 300MB.
293 if ($available < 300) {
294 # Exit function and return the available disk space.
300 # Everything okay, return nothing.
305 ## This function is responsible for downloading the ruleset for a given provider.
307 ## * At first it initialize the downloader and sets an upstream proxy if configured.
308 ## * The next step will be to generate the final download url, by obtaining the URL for the desired
309 ## ruleset and add the settings for the upstream proxy.
310 ## * Finally the function will grab the rule file or tarball from the server.
311 ## It tries to reduce the amount of download by using the "If-Modified-Since" HTTP header.
315 ## * "no url" - If no download URL could be gathered for the provider.
316 ## * "not modified" - In case the already stored rules file is up to date.
317 ## * "incomplete download" - When the remote file size differs from the downloaded file size.
318 ## * "$error" - The error message generated from the LWP::User Agent module.
320 sub downloadruleset
($) {
323 # The amount of download attempts before giving up and
325 my $max_dl_attempts = 3;
327 # Read proxysettings.
328 my %proxysettings=();
329 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
331 # Init the download module.
333 # Request SSL hostname verification and specify path
335 my $downloader = LWP
::UserAgent
->new(
337 SSL_ca_file
=> '/etc/ssl/cert.pem',
338 verify_hostname
=> 1,
342 # Set timeout to 10 seconds.
343 $downloader->timeout(10);
345 # Check if an upstream proxy is configured.
346 if ($proxysettings{'UPSTREAM_PROXY'}) {
349 $proxy_url = "http://";
351 # Check if the proxy requires authentication.
352 if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) {
353 $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@";
356 # Add proxy server address and port.
357 $proxy_url .= $proxysettings{'UPSTREAM_PROXY'};
359 # Setup proxy settings.
360 $downloader->proxy(['http', 'https'], $proxy_url);
363 # Grab the download url for the provider.
364 my $url = $IDS::Ruleset
::Providers
{$provider}{'dl_url'};
366 # Check if the provider requires a subscription.
367 if ($IDS::Ruleset
::Providers
{$provider}{'requires_subscription'} eq "True") {
368 # Grab the subscription code.
369 my $subscription_code = &get_subscription_code
($provider);
371 # Add the subscription code to the download url.
372 $url =~ s/\<subscription_code\>/$subscription_code/g;
376 # Abort and return "no url", if no url could be determined for the provider.
377 return "no url" unless ($url);
379 # Pass the requested URL to the downloader.
380 my $request = HTTP
::Request
->new(GET
=> $url);
382 # Generate temporary file name, located in "/var/tmp" and with a suffix of ".tmp".
383 # The downloaded file will be stored there until some sanity checks are performed.
384 my $tmp = File
::Temp
->new( SUFFIX
=> ".tmp", DIR
=> "/var/tmp/", UNLINK
=> 0 );
385 my $tmpfile = $tmp->filename();
387 # Call function to get the final path and filename for the downloaded file.
388 my $dl_rulesfile = &_get_dl_rulesfile
($provider);
390 # Check if the rulesfile already exits, because it has been downloaded in the past.
392 # In this case we are requesting the server if the remote file has been changed or not.
393 # This will be done by sending the modification time in a special HTTP header.
394 if (-f
$dl_rulesfile) {
395 # Call stat on the file.
396 my $stat = stat($dl_rulesfile);
398 # Omit the mtime of the existing file.
399 my $mtime = $stat->mtime;
401 # Convert the timestamp into right format.
402 my $http_date = time2str
($mtime);
404 # Add the If-Modified-Since header to the request to ask the server if the
405 # file has been modified.
406 $request->header( 'If-Modified-Since' => "$http_date" );
412 # Download and retry on failure.
413 while ($dl_attempt <= $max_dl_attempts) {
414 # Perform the request and save the output into the tmpfile.
415 $response = $downloader->request($request, $tmpfile);
417 # Check if the download was successfull.
418 if($response->is_success) {
422 # Check if the server responds with 304 (Not Modified).
423 } elsif ($response->code == 304) {
424 # Return "not modified".
425 return "not modified";
427 # Check if we ran out of download re-tries.
428 } elsif ($dl_attempt eq $max_dl_attempts) {
430 my $error = $response->content;
432 # Return the error message from response..
436 # Remove temporary file, if one exists.
437 unlink("$tmpfile") if (-e
"$tmpfile");
439 # Increase download attempt counter.
443 # Obtain the connection headers.
444 my $headers = $response->headers;
446 # Get the timestamp from header, when the file has been modified the
448 my $last_modified = $headers->last_modified;
450 # Get the remote size of the downloaded file.
451 my $remote_filesize = $headers->content_length;
453 # Perform stat on the tmpfile.
454 my $stat = stat($tmpfile);
456 # Grab the local filesize of the downloaded tarball.
457 my $local_filesize = $stat->size;
459 # Check if both file sizes match.
460 if (($remote_filesize) && ($remote_filesize ne $local_filesize)) {
461 # Delete temporary file.
464 # Return "1" - false.
465 return "incomplete download";
468 # Overwrite the may existing rulefile or tarball with the downloaded one.
469 move
("$tmpfile", "$dl_rulesfile");
471 # Check if we got a last-modified value from the server.
472 if ($last_modified) {
473 # Assign the last-modified timestamp as mtime to the
475 utime(time(), "$last_modified", "$dl_rulesfile");
478 # Delete temporary file.
481 # Set correct ownership for the tarball.
482 set_ownership
("$dl_rulesfile");
484 # If we got here, everything worked fine. Return nothing.
489 ## Function to extract a given ruleset.
491 ## In case the ruleset provider offers a plain file, it simply will
494 sub extractruleset
($) {
497 # Disable chown functionality when uncompressing files.
498 $Archive::Tar
::CHOWN
= "0";
500 # Get full path and downloaded rulesfile for the given provider.
501 my $tarball = &_get_dl_rulesfile
($provider);
503 # Check if the file exists.
504 unless (-f
$tarball) {
505 &_log_to_syslog
("Could not find ruleset file: $tarball");
511 # Check if the temporary directories exist, otherwise create them.
512 mkdir("$tmp_directory") unless (-d
"$tmp_directory");
513 mkdir("$tmp_rules_directory") unless (-d
"$tmp_rules_directory");
514 mkdir("$tmp_conf_directory") unless (-d
"$tmp_conf_directory");
516 # Omit the type (dl_type) of the stored ruleset.
517 my $type = $IDS::Ruleset
::Providers
{$provider}{'dl_type'};
519 # Handle the different ruleset types.
520 if ($type eq "plain") {
521 # Generate destination filename an full path.
522 my $destination = "$tmp_rules_directory/$provider\-ruleset.rules";
524 # Copy the file into the temporary rules directory.
525 copy
($tarball, $destination);
527 } elsif ( $type eq "archive") {
528 # Initialize the tar module.
529 my $tar = Archive
::Tar
->new($tarball);
531 # Get the filelist inside the tarball.
532 my @packed_files = $tar->list_files;
534 # Loop through the filelist.
535 foreach my $packed_file (@packed_files) {
538 # Splitt the packed file into chunks.
539 my $file = fileparse
($packed_file);
541 # Handle msg-id.map file.
542 if ("$file" eq "sid-msg.map") {
543 # Set extract destination to temporary config_dir.
544 $destination = "$tmp_conf_directory/$provider\-sid-msg.map";
546 # Handle classification.conf
547 } elsif ("$file" eq "classification.config") {
548 # Set extract destination to temporary config_dir.
549 $destination = "$tmp_conf_directory/$provider\-classification.config";
551 # Handle rules files.
552 } elsif ($file =~ m/\.rules$/) {
553 # Skip rule files which are not located in the rules directory or archive root.
554 next unless(($packed_file =~ /^rules\//) || ($packed_file !~ /\
//));
556 # Skip deleted.rules.
558 # Mostly they have been taken out for correctness or performance reasons and therfore
559 # it is not a great idea to enable any of them.
560 next if($file =~ m/deleted.rules$/);
564 # Splitt the filename into chunks.
565 my @filename = split("-", $file);
568 @filename = reverse(@filename);
570 # Get the amount of elements in the array.
571 my $elements = @filename;
573 # Remove last element of the hash.
574 # It contains the vendor name, which will be replaced.
575 if ($elements >= 3) {
576 # Remove last element from hash.
580 # Check if the last element of the filename does not
581 # contain the providers name.
582 if ($filename[-1] ne "$provider") {
583 # Add provider name as last element.
584 push(@filename, $provider);
587 # Reverse the array back.
588 @filename = reverse(@filename);
590 # Generate the name for the rulesfile.
591 $rulesfilename = join("-", @filename);
593 # Set extract destination to temporaray rules_dir.
594 $destination = "$tmp_rules_directory/$rulesfilename";
596 # Skip all other files.
600 # Check if the destination file exists.
601 unless(-e
"$destination") {
602 # Extract the file to the temporary directory.
603 $tar->extract_file("$packed_file", "$destination");
605 # Generate temporary file name, located in the temporary rules directory and a suffix of ".tmp".
606 my $tmp = File
::Temp
->new( SUFFIX
=> ".tmp", DIR
=> "$tmp_rules_directory", UNLINK
=> 0 );
607 my $tmpfile = $tmp->filename();
609 # Extract the file to the new temporary file name.
610 $tar->extract_file("$packed_file", "$tmpfile");
612 # Open the the existing file.
613 open(DESTFILE
, ">>", "$destination") or die "Could not open $destination. $!\n";
614 open(TMPFILE
, "<", "$tmpfile") or die "Could not open $tmpfile. $!\n";
616 # Loop through the content of the temporary file.
618 # Append the content line by line to the destination file.
622 # Close the file handles.
626 # Remove the temporary file.
634 ## A wrapper function to call the oinkmaster script, setup the rules structues and
635 ## call the functions to merge the additional config files. (classification, sid-msg, etc.).
638 # Check if the files in rulesdir have the correct permissions.
639 &_check_rulesdir_permissions
();
641 # Cleanup the rules directory before filling it with the new rulests.
642 &_cleanup_rulesdir
();
644 # Get all enabled providers.
645 my @enabled_providers = &get_enabled_providers
();
647 # Loop through the array of enabled providers.
648 foreach my $provider (@enabled_providers) {
649 # Call the extractruleset function.
650 &extractruleset
($provider);
653 # Establish the connection to the syslog service.
654 openlog
('oinkmaster', 'cons,pid', 'user');
656 # Call oinkmaster to generate ruleset.
657 open(OINKMASTER
, "/usr/local/bin/oinkmaster.pl -s -u dir://$tmp_rules_directory -C $settingsdir/oinkmaster.conf -o $rulespath 2>&1 |") or die "Could not execute oinkmaster $!\n";
659 # Log output of oinkmaster to syslog.
660 while(<OINKMASTER
>) {
661 # The syslog function works best with an array based input,
662 # so generate one before passing the message details to syslog.
663 my @syslog = ("INFO", "$_");
665 # Send the log message.
669 # Close the pipe to oinkmaster process.
672 # Close the log handle.
675 # Call function to merge the classification files.
676 &merge_classifications
(@enabled_providers);
678 # Call function to merge the sid to message mapping files.
679 &merge_sid_msg
(@enabled_providers);
681 # Cleanup temporary directory.
682 &cleanup_tmp_directory
();
686 ## Function to alter the ruleset.
688 sub process_ruleset
(@
) {
689 my (@providers) = @_;
691 # Hash to store the configured provider modes.
692 my %providers_mode = &get_providers_mode
();
694 # Array to store the extracted rulefile from the temporary rules directory.
695 my @extracted_rulefiles;
697 # Get names of the extracted raw rulefiles.
698 opendir(DIR
, $tmp_rules_directory) or die "Could not read from $tmp_rules_directory. $!\n";
699 while (my $file = readdir(DIR
)) {
700 # Ignore single and double dotted files.
701 next if $file =~ /^\.\.?$/;
703 # Add file to the array of extracted files.
704 push(@extracted_rulefiles, $file);
707 # Close directory handle.
710 # Loop through the array of providers.
711 foreach my $provider (@providers) {
712 # Hash to store the obtained SIDs and REV of each provider.
715 # Hash which holds modifications to apply to the rules.
716 my %modifications = ();
718 # Loop through the array of extraced rulefiles.
719 foreach my $file (@extracted_rulefiles) {
720 # Skip file if it does not belong to the current processed provider.
721 next unless ($file =~ m/^$provider/);
724 open(FILE
, "$tmp_rules_directory/$file") or die "Could not read $tmp_rules_directory/$file. $!\n";
726 # Loop through the file content.
727 while (my $line = <FILE
>) {
729 next if ($line =~ /^\s*$/);
731 # Call function to get the sid and rev of the rule.
732 my ($sid, $rev) = &_get_sid_and_rev
($line);
734 # Skip rule if a sid with a higher rev already has added to the rules hash.
735 next if ($rev le $rules{$sid});
737 # Add the new or rule with higher rev to the hash of rules.
745 # Get filename which contains the ruleset modifications for this provider.
746 my $modification_file = &get_provider_ruleset_modifications_file
($provider);
748 # Read file which holds the modifications of the ruleset for the current provider.
749 &General
::readhash
($modification_file, \
%modifications) if (-f
$modification_file);
751 # Loop again through the array of extracted rulesfiles.
752 foreach my $file (@extracted_rulefiles) {
753 # Skip the file if it does not belong to the current provider.
754 next unless ($file =~ m/^$provider/);
756 # Open the rulefile for writing.
757 open(RULEFILE
, ">", "$rulespath/$file") or die "Could not write to file $rulespath/$file. $!\n";
759 # Open the rulefile for reading.
760 open(TMP_RULEFILE
, "$tmp_rules_directory/$file") or die "Could not read $tmp_rules_directory/$file. $!\n";
762 # Loop through the raw temporary rulefile.
763 while (my $line = <TMP_RULEFILE
>) {
764 # Get the sid and rev of the rule.
765 my ($sid, $rev) = &_get_sid_and_rev
($line);
767 # Check if the current rule is obsoleted by a newer one.
769 # In this case the rev number in the rules hash is higher than the current one.
770 next if ($rev lt $rules{$sid});
772 # Check if the rule should be enabled or disabled.
773 if ($modifications{$sid} eq "on") {
774 # Drop the # at the start of the line.
776 } elsif ($modifications{$sid} eq "off") {
777 # Add a # at the start of the line to disable the rule.
778 $line = "#$line" unless ($line =~ /^#/);
781 # Check if the Provider is set so IPS mode.
782 if ($providers_mode{$provider} eq "IPS") {
783 # Replacements for sourcefire rules.
784 $line =~ s/^#\s*(?:alert|drop)(.+policy balanced-ips alert)/alert${1}/;
785 $line =~ s/^#\s*(?:alert|drop)(.+policy balanced-ips drop)/drop${1}/;
787 # Replacements for generic rules.
788 $line =~ s/^(#?)\s*(?:alert|drop)/${1}drop/;
789 $line =~ s/^(#?)\s*drop(.+flowbits:noalert;)/${1}alert${2}/;
792 # Write line / rule to the target rule file.
793 print RULEFILE
"$line";
804 ## Function to merge the classifications for a given amount of providers and write them
805 ## to the classifications file.
807 sub merge_classifications
(@
) {
810 # Hash to store all collected classifications.
811 my %classifications = ();
813 # Loop through the given array of providers.
814 foreach my $provider (@providers) {
815 # Generate full path to classification file.
816 my $classification_file = "$tmp_conf_directory/$provider\-classification.config";
818 # Skip provider if no classification file exists.
819 next unless (-f
"$classification_file");
821 # Open the classification file.
822 open(CLASSIFICATION
, $classification_file) or die "Could not open file $classification_file. $!\n";
824 # Loop through the file content.
825 while(<CLASSIFICATION
>) {
826 # Parse the file and grab the classification details.
827 if ($_ =~/.*config classification\: (.*)/) {
828 # Split the grabbed details.
829 my ($short_name, $short_desc, $priority) = split("\,", $1);
831 # Check if the grabbed classification is allready known and the priority value is greater
832 # than the stored one (which causes less priority in the IDS).
833 if (($classifications{$short_name}) && ($classifications{$short_name}[1] >= $priority)) {
834 #Change the priority value to the stricter one.
835 $classifications{$short_name} = [ "$classifications{$short_name}[0]", "$priority" ];
837 # Add the classification to the hash.
838 $classifications{$short_name} = [ "$short_desc", "$priority" ];
844 close(CLASSIFICATION
);
847 # Open classification file for writing.
848 open(FILE
, ">", "$classification_file") or die "Could not write to $classification_file. $!\n";
850 # Print notice about autogenerated file.
851 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n\n";
853 # Sort and loop through the hash of classifications.
854 foreach my $key (sort keys %classifications) {
855 # Assign some nice variable names for the items.
856 my $short_name = $key;
857 my $short_desc = $classifications{$key}[0];
858 my $priority = $classifications{$key}[1];
860 # Write the classification to the file.
861 print FILE
"config classification: $short_name,$short_desc,$priority\n";
869 ## Function to merge the "sid to message mapping" files of various given providers.
871 sub merge_sid_msg
(@
) {
874 # Hash which contains all the sid to message mappings.
877 # Loop through the array of given providers.
878 foreach my $provider (@providers) {
879 # Generate full path and filename.
880 my $sid_msg_file = "$tmp_conf_directory/$provider\-sid-msg.map";
882 # Skip provider if no sid to msg mapping file for this provider exists.
883 next unless (-f
$sid_msg_file);
886 open(MAPPING
, $sid_msg_file) or die "Could not open $sid_msg_file. $!\n";
888 # Loop through the file content.
893 # Skip lines which do not start with a number,
894 next unless ($_ =~ /^\d+/);
896 # Split line content and assign it to an array.
897 my @line = split(/ \|\| /, $_);
899 # Grab the first element (and remove it) from the line array.
900 # It contains the sid.
901 my $sid = shift(@line);
903 # Store the grabbed sid and the remain array as hash value.
904 # It still contains the messages, references etc.
905 $mappings{$sid} = [@line];
912 # Open mappings file for writing.
913 open(FILE
, ">", $sid_msg_file) or die "Could not write $sid_msg_file. $!\n";
915 # Write notice about autogenerated file.
916 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n\n";
918 # Loop through the hash of mappings.
919 foreach my $sid ( sort keys %mappings) {
920 # Grab data for the sid.
921 my @data = @
{$mappings{$sid}};
923 # Add the sid to the data array.
924 unshift(@data, $sid);
927 my $line = join(" \|\| ", @data);
929 print FILE
"$line\n";
938 ## A very tiny function to move an extracted ruleset from the temporary directory into
939 ## the rules directory.
941 sub move_tmp_ruleset
() {
942 # Do a directory listing of the temporary directory.
943 opendir DH
, $tmp_rules_directory;
945 # Loop over all files.
946 while(my $file = readdir DH
) {
947 # Move them to the rules directory.
948 move
"$tmp_rules_directory/$file" , "$rulespath/$file";
951 # Close directory handle.
956 ## Function to cleanup the temporary IDS directroy.
958 sub cleanup_tmp_directory
() {
960 # Delete temporary directory and all containing files.
961 rmtree
([ "$tmp_directory" ]);
965 ## Function to do all the logging stuff if the downloading or updating of the ruleset fails.
970 # Remove any newline.
973 # Call private function to log the error message to syslog.
974 &_log_to_syslog
($error);
976 # Call private function to write/store the error message in the storederrorfile.
977 &_store_error_message
($error);
981 ## Function to log a given error message to the kernel syslog.
983 sub _log_to_syslog
($) {
986 # The syslog function works best with an array based input,
987 # so generate one before passing the message details to syslog.
988 my @syslog = ("ERR", "<ERROR> $message");
990 # Establish the connection to the syslog service.
991 openlog
('oinkmaster', 'cons,pid', 'user');
993 # Send the log message.
996 # Close the log handle.
1001 ## Private function to write a given error message to the storederror file.
1003 sub _store_error_message
($) {
1006 # Remove any newline.
1009 # Open file for writing.
1010 open (ERRORFILE
, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n";
1012 # Write error to file.
1013 print ERRORFILE
"$message\n";
1018 # Set correct ownership for the file.
1019 &set_ownership
("$storederrorfile");
1023 ## Private function to get the path and filename for a downloaded ruleset by a given provider.
1025 sub _get_dl_rulesfile
($) {
1026 my ($provider) = @_;
1028 # Gather the download type for the given provider.
1029 my $dl_type = $IDS::Ruleset
::Providers
{$provider}{'dl_type'};
1031 # Obtain the file suffix for the download file type.
1032 my $suffix = $dl_type_to_suffix{$dl_type};
1034 # Check if a suffix has been found.
1036 # Abort return - nothing.
1040 # Generate the full filename and path for the stored rules file.
1041 my $rulesfile = "$dl_rules_path/$dl_rulesfile_prefix-$provider$suffix";
1043 # Return the generated filename.
1048 ## Private function to obtain the sid and rev of a rule.
1050 ## Returns an array with the sid as first and the rev as second value.
1052 sub _get_sid_and_rev
($) {
1057 # Use regex to obtain the sid and rev.
1058 if ($line =~ m/.*sid:\s*(.*?);.*rev:\s*(.*?);/) {
1059 # Add the sid and rev to the array.
1069 ## Tiny function to delete the stored ruleset file or tarball for a given provider.
1071 sub drop_dl_rulesfile
($) {
1072 my ($provider) = @_;
1074 # Gather the full path and name of the stored rulesfile.
1075 my $rulesfile = &_get_dl_rulesfile
($provider);
1077 # Check if the given rulesfile exists.
1078 if (-f
$rulesfile) {
1079 # Delete the stored rulesfile.
1080 unlink($rulesfile) or die "Could not delete $rulesfile. $!\n";
1085 ## Tiny function to get/generate the full path and filename for the providers oinkmaster
1086 ## modified sids file.
1088 sub get_oinkmaster_provider_modified_sids_file
($) {
1089 my ($provider) = @_;
1091 # Generate the filename.
1092 my $filename = "$settingsdir/oinkmaster-$provider-modified-sids.conf";
1094 # Return the filename.
1099 ## Function to directly altering the oinkmaster provider includes file.
1101 ## Requires tha acition "remove" or "add" and a provider handle.
1103 sub alter_oinkmaster_provider_includes_file
($$) {
1104 my ($action, $provider) = @_;
1106 # Call function to get the path and name for the given providers
1107 # oinkmaster modified sids file.
1108 my $provider_modified_sids_file = &get_oinkmaster_provider_modified_sids_file
($provider);
1110 # Open the file for reading..
1111 open (FILE
, $oinkmaster_provider_includes_file) or die "Could not read $oinkmaster_provider_includes_file. $!\n";
1113 # Read-in file content.
1116 # Close file after reading.
1119 # Re-open the file for writing.
1120 open(FILE
, ">", $oinkmaster_provider_includes_file) or die "Could not write to $oinkmaster_provider_includes_file. $!\n";
1122 # Loop through the file content.
1123 foreach my $line (@lines) {
1127 # Skip line if we found our given provider and the action should be remove.
1128 next if (($line =~ /$provider/) && ($action eq "remove"));
1130 # Write the read-in line back to the file.
1131 print FILE
"$line\n";
1134 # Check if the file exists and add the provider if requested.
1135 if ((-f
$provider_modified_sids_file) && ($action eq "add")) {
1136 print FILE
"include $provider_modified_sids_file\n";
1139 # Close file handle.
1144 ## Function to read-in the given enabled or disables sids file.
1146 sub read_enabled_disabled_sids_file
($) {
1149 # Temporary hash to store the sids and their state. It will be
1150 # returned at the end of this function.
1153 # Open the given filename.
1154 open(FILE
, "$file") or die "Could not open $file. $!\n";
1156 # Loop through the file.
1162 next if ($_ =~ /^\s*$/);
1165 next if ($_ =~ /^\#/);
1167 # Splitt line into sid and state part.
1168 my ($state, $sid) = split(" ", $_);
1170 # Skip line if the sid is not numeric.
1171 next unless ($sid =~ /\d+/ );
1173 # Check if the sid was enabled.
1174 if ($state eq "enablesid") {
1175 # Add the sid and its state as enabled to the temporary hash.
1176 $temphash{$sid} = "enabled";
1177 # Check if the sid was disabled.
1178 } elsif ($state eq "disablesid") {
1179 # Add the sid and its state as disabled to the temporary hash.
1180 $temphash{$sid} = "disabled";
1181 # Invalid state - skip the current sid and state.
1195 ## Function to check if the IDS is running.
1197 sub ids_is_running
() {
1198 if(-f
$idspidfile) {
1199 # Open PID file for reading.
1200 open(PIDFILE
, "$idspidfile") or die "Could not open $idspidfile. $!\n";
1202 # Grab the process-id.
1203 my $pid = <PIDFILE
>;
1208 # Remove any newline.
1211 # Check if a directory for the process-id exists in proc.
1212 if(-d
"/proc/$pid") {
1213 # The IDS daemon is running return the process id.
1218 # Return nothing - IDS is not running.
1223 ## Function to call suricatactrl binary with a given command.
1225 sub call_suricatactrl
($) {
1226 # Get called option.
1227 my ($option, $interval) = @_;
1229 # Loop through the array of supported commands and check if
1230 # the given one is part of it.
1231 foreach my $cmd (@suricatactrl_cmds) {
1232 # Skip current command unless the given one has been found.
1233 next unless($cmd eq $option);
1235 # Check if the given command is "cron".
1236 if ($option eq "cron") {
1237 # Check if an interval has been given.
1239 # Check if the given interval is valid.
1240 foreach my $element (@cron_intervals) {
1241 # Skip current element until the given one has been found.
1242 next unless($element eq $interval);
1244 # Call the suricatactrl binary and pass the "cron" command
1245 # with the requrested interval.
1246 &General
::system("$suricatactrl", "$option", "$interval");
1248 # Return "1" - True.
1253 # If we got here, the given interval is not supported or none has been given. - Return nothing.
1256 # Call the suricatactrl binary and pass the requrested
1258 &General
::system("$suricatactrl", "$option");
1260 # Return "1" - True.
1265 # Command not found - return nothing.
1270 ## Function to create a new empty file.
1272 sub create_empty_file
($) {
1275 # Check if the given file exists.
1277 # Do nothing to prevent from overwriting existing files.
1281 # Open the file for writing.
1282 open(FILE
, ">$file") or die "Could not write to $file. $!\n";
1284 # Close file handle.
1292 ## Private function to check if the file permission of the rulespath are correct.
1293 ## If not, call suricatactrl to fix them.
1295 sub _check_rulesdir_permissions
() {
1296 # Check if the rulepath main directory is writable.
1297 unless (-W
$rulespath) {
1298 # If not call suricatctrl to fix it.
1299 &call_suricatactrl
("fix-rules-dir");
1302 # Open snort rules directory and do a directory listing.
1303 opendir(DIR
, $rulespath) or die $!;
1304 # Loop through the direcory.
1305 while (my $file = readdir(DIR
)) {
1306 # We only want files.
1307 next unless (-f
"$rulespath/$file");
1309 # Check if the file is writable by the user.
1310 if (-W
"$rulespath/$file") {
1311 # Everything is okay - go on to the next file.
1314 # There are wrong permissions, call suricatactrl to fix it.
1315 &call_suricatactrl
("fix-rules-dir");
1321 ## Private function to cleanup the directory which contains
1322 ## the IDS rules, before extracting and modifing the new ruleset.
1324 sub _cleanup_rulesdir
() {
1325 # Open rules directory and do a directory listing.
1326 opendir(DIR
, $rulespath) or die $!;
1328 # Loop through the direcory.
1329 while (my $file = readdir(DIR
)) {
1330 # We only want files.
1331 next unless (-f
"$rulespath/$file");
1333 # Skip rules file for whitelisted hosts.
1334 next if ("$rulespath/$file" eq $whitelist_file);
1336 # Skip rules file with local rules.
1337 next if ("$rulespath/$file" eq $local_rules_file);
1339 # Delete the current processed file, if not, exit this function
1340 # and return an error message.
1341 unlink("$rulespath/$file") or return "Could not delete $rulespath/$file. $!\n";
1349 ## Function to generate the file which contains the home net information.
1351 sub generate_home_net_file
() {
1354 # Read-in network settings.
1355 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
1357 # Get available network zones.
1358 my @network_zones = &Network
::get_available_network_zones
();
1360 # Temporary array to store network address and prefix of the configured
1364 # Loop through the array of available network zones.
1365 foreach my $zone (@network_zones) {
1366 # Check if the current processed zone is red.
1367 if($zone eq "red") {
1368 # Grab the IP-address of the red interface.
1369 my $red_address = &get_red_address
();
1371 # Check if an address has been obtained.
1373 # Generate full network string.
1374 my $red_network = join("/", $red_address, "32");
1376 # Add the red network to the array of networks.
1377 push(@networks, $red_network);
1380 # Check if the configured RED_TYPE is static.
1381 if ($netsettings{'RED_TYPE'} eq "STATIC") {
1382 # Get configured and enabled aliases.
1383 my @aliases = &get_aliases
();
1385 # Loop through the array.
1386 foreach my $alias (@aliases) {
1388 my $network = join("/", $alias, "32");
1390 # Add the generated network to the array of networks.
1391 push(@networks, $network);
1394 # Process remaining network zones.
1396 # Convert current zone name into upper case.
1399 # Generate key to access the required data from the netsettings hash.
1400 my $zone_netaddress = $zone . "_NETADDRESS";
1401 my $zone_netmask = $zone . "_NETMASK";
1403 # Obtain the settings from the netsettings hash.
1404 my $netaddress = $netsettings{$zone_netaddress};
1405 my $netmask = $netsettings{$zone_netmask};
1407 # Convert the subnetmask into prefix notation.
1408 my $prefix = &Network
::convert_netmask2prefix
($netmask);
1410 # Generate full network string.
1411 my $network = join("/", $netaddress,$prefix);
1413 # Check if the network is valid.
1414 if(&Network
::check_subnet
($network)) {
1415 # Add the generated network to the array of networks.
1416 push(@networks, $network);
1421 # Format home net declaration.
1422 my $line = "\"[" . join(',', @networks) . "]\"";
1424 # Open file to store the addresses of the home net.
1425 open(FILE
, ">$homenet_file") or die "Could not open $homenet_file. $!\n";
1427 # Print yaml header.
1428 print FILE
"%YAML 1.1\n";
1429 print FILE
"---\n\n";
1431 # Print notice about autogenerated file.
1432 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1434 # Print the generated and required HOME_NET declaration to the file.
1435 print FILE
"HOME_NET:\t$line\n";
1437 # Close file handle.
1442 # Function to generate and write the file which contains the configured and used DNS servers.
1444 sub generate_dns_servers_file
() {
1445 # Get the used DNS servers.
1446 my @nameservers = &General
::get_nameservers
();
1448 # Get network settings.
1450 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
1452 # Format dns servers declaration.
1455 # Check if the system has configured nameservers.
1457 # Add the GREEN address as DNS servers.
1458 push(@nameservers, $netsettings{'GREEN_ADDRESS'});
1460 # Check if a BLUE zone exists.
1461 if ($netsettings{'BLUE_ADDRESS'}) {
1462 # Add the BLUE address to the array of nameservers.
1463 push(@nameservers, $netsettings{'BLUE_ADDRESS'});
1466 # Generate the line which will be written to the DNS servers file.
1467 $line = join(",", @nameservers);
1469 # External net simply contains (any).
1470 $line = "\$EXTERNAL_NET";
1473 # Open file to store the used DNS server addresses.
1474 open(FILE
, ">$dns_servers_file") or die "Could not open $dns_servers_file. $!\n";
1476 # Print yaml header.
1477 print FILE
"%YAML 1.1\n";
1478 print FILE
"---\n\n";
1480 # Print notice about autogenerated file.
1481 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1483 # Print the generated DNS declaration to the file.
1484 print FILE
"DNS_SERVERS:\t\"[$line]\"\n";
1486 # Close file handle.
1491 # Function to generate and write the file which contains the HTTP_PORTS definition.
1493 sub generate_http_ports_file
() {
1496 # Read-in proxy settings
1497 &General
::readhash
("${General::swroot}/proxy/advanced/settings", \
%proxysettings);
1499 # Check if the proxy is enabled.
1500 if (( -e
"${General::swroot}/proxy/enable") || (-e
"${General::swroot}/proxy/enable_blue")) {
1501 # Add the proxy port to the array of HTTP ports.
1502 push(@http_ports, $proxysettings{'PROXY_PORT'});
1505 # Check if the transparent mode of the proxy is enabled.
1506 if ((-e
"${General::swroot}/proxy/transparent") || (-e
"${General::swroot}/proxy/transparent_blue")) {
1507 # Add the transparent proxy port to the array of HTTP ports.
1508 push(@http_ports, $proxysettings{'TRANSPARENT_PORT'});
1511 # Format HTTP_PORTS declaration.
1514 # Generate line which will be written to the http ports file.
1515 $line = join(",", @http_ports);
1517 # Open file to store the HTTP_PORTS.
1518 open(FILE
, ">$http_ports_file") or die "Could not open $http_ports_file. $!\n";
1520 # Print yaml header.
1521 print FILE
"%YAML 1.1\n";
1522 print FILE
"---\n\n";
1524 # Print notice about autogenerated file.
1525 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1527 # Print the generated HTTP_PORTS declaration to the file.
1528 print FILE
"HTTP_PORTS:\t\"[$line]\"\n";
1530 # Close file handle.
1535 ## Function to generate and write the file for used rulefiles file for a given provider.
1537 ## The function requires as first argument a provider handle, and as second an array with files.
1539 sub write_used_provider_rulefiles_file
($@
) {
1540 my ($provider, @files) = @_;
1542 # Get the path and file for the provider specific used rulefiles file.
1543 my $used_provider_rulesfile_file = &get_used_provider_rulesfile_file
($provider);
1545 # Open file for used rulefiles.
1546 open (FILE
, ">", "$used_provider_rulesfile_file") or die "Could not write to $used_provider_rulesfile_file. $!\n";
1548 # Write yaml header to the file.
1549 print FILE
"%YAML 1.1\n";
1550 print FILE
"---\n\n";
1552 # Write header to file.
1553 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1555 # Loop through the array of given files.
1556 foreach my $file (@files) {
1557 # Check if the given filename exists and write it to the file of used rulefiles.
1558 if(-f
"$rulespath/$file") {
1559 print FILE
" - $file\n";
1563 # Close file after writing.
1568 ## Function to write the main file for provider rulesfiles inclusions.
1570 ## This function requires an array of provider handles.
1572 sub write_main_used_rulefiles_file
(@
) {
1573 my (@providers) = @_;
1575 # Call function to write the static rulefiles file.
1576 &_write_default_rulefiles_file
();
1578 # Open file for used rulefils inclusion.
1579 open (FILE
, ">", "$suricata_used_providers_file") or die "Could not write to $suricata_used_providers_file. $!\n";
1581 # Write yaml header to the file.
1582 print FILE
"%YAML 1.1\n";
1583 print FILE
"---\n\n";
1585 # Write header to file.
1586 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1588 # Loop through the list of given providers.
1589 foreach my $provider (@providers) {
1590 # Call function to get the providers used rulefiles file.
1591 my $filename = &get_used_provider_rulesfile_file
($provider);
1593 # Check if the file exists and write it into the used rulefiles file.
1595 # Print the provider to the file.
1596 print FILE
"include\: $filename\n";
1600 # Close the filehandle after writing.
1604 sub _write_default_rulefiles_file
() {
1605 # Get enabled application layer protocols.
1606 my @enabled_app_layer_protos = &get_suricata_enabled_app_layer_protos
();
1609 open (FILE
, ">", $suricata_default_rulefiles_file) or die "Could not write to $suricata_default_rulefiles_file. $!\n";
1611 # Write yaml header to the file.
1612 print FILE
"%YAML 1.1\n";
1613 print FILE
"---\n\n";
1615 # Write notice about autogenerated file.
1616 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1618 # Loop through the array of static included rulesfiles.
1619 foreach my $file (@static_included_rulefiles) {
1620 # Check if the file exists.
1621 if (-f
"$rulespath/$file") {
1622 # Write the rulesfile name to the file.
1623 print FILE
" - $rulespath/$file\n";
1627 print FILE
"\n#Default rules for used application layer protocols.\n";
1628 foreach my $enabled_app_layer_proto (@enabled_app_layer_protos) {
1629 # Check if the current processed app layer proto needs to be translated
1630 # into an application name.
1631 if (exists($tr_app_layer_proto{$enabled_app_layer_proto})) {
1632 # Obtain the translated application name for this protocol.
1633 $enabled_app_layer_proto = $tr_app_layer_proto{$enabled_app_layer_proto};
1636 # Generate filename.
1637 my $rulesfile = "$default_rulespath/$enabled_app_layer_proto\.rules";
1639 # Check if such a file exists.
1640 if (-f
"$rulesfile") {
1641 # Write the rulesfile name to the file.
1642 print FILE
" - $rulesfile\n";
1645 # Generate filename with "events" in filename.
1646 $rulesfile = "$default_rulespath/$enabled_app_layer_proto\-events.rules";
1648 # Check if this file exists.
1649 if (-f
"$rulesfile" ) {
1650 # Write the rulesfile name to the file.
1651 print FILE
" - $rulesfile\n";
1655 # Close the file handle
1660 ## Tiny function to generate the full path and name for the used_provider_rulesfile file of a given provider.
1662 sub get_used_provider_rulesfile_file
($) {
1663 my ($provider) = @_;
1665 my $filename = "$settingsdir/suricata\-$provider\-used\-rulefiles.yaml";
1667 # Return the gernerated file.
1672 ## Tiny function to generate the full path and name for the file which stores the modifications of a ruleset.
1674 sub get_provider_ruleset_modifications_file
($) {
1675 my ($provider) = @_;
1677 my $filename = "$settingsdir/$provider\-modifications";
1679 # Return the filename.
1684 ## Function to generate and write the file for modify the ruleset.
1686 sub write_modify_sids_file
() {
1687 # Get configured settings.
1689 &General
::readhash
("$ids_settings_file", \
%idssettings);
1691 # Open modify sid's file for writing.
1692 open(FILE
, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
1694 # Write file header.
1695 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
1697 # Check if the traffic only should be monitored.
1698 unless($idssettings{'MONITOR_TRAFFIC_ONLY'} eq 'on') {
1699 # Suricata is in IPS mode, which means that the rule actions have to be changed
1700 # from 'alert' to 'drop', however not all rules should be changed. Some rules
1701 # exist purely to set a flowbit which is used to convey other information, such
1702 # as a specific type of file being downloaded, to other rulewhich then check for
1703 # malware in that file. Rules which fall into the first category should stay as
1704 # alert since not all flows of that type contain malware.
1706 # These types of rulesfiles contain meta-data which gives the action that should
1707 # be used when in IPS mode. Do the following:
1709 # 1. Disable all rules and set the action to 'drop'
1710 # 2. Set the action back to 'alert' if the rule contains 'flowbits:noalert;'
1711 # This should give rules not in the policy a reasonable default if the user
1712 # manually enables them.
1713 # 3. Enable rules and set actions according to the meta-data strings.
1715 my $policy = 'balanced'; # Placeholder to allow policy to be changed.
1718 modifysid * "^#(?:alert|drop)(.+policy $policy-ips alert)" | "alert\${1}"
1719 modifysid * "^#(?:alert|drop)(.+policy $policy-ips drop)" | "drop\${1}"
1720 modifysid * "^(#?)(?:alert|drop)" | "\${1}drop"
1721 modifysid * "^(#?)drop(.+flowbits:noalert;)" | "\${1}alert\${2}"
1725 # Close file handle.
1730 ## Function to get the subscription code of a configured provider.
1732 sub get_subscription_code
($) {
1733 my ($provider) = @_;
1735 my %configured_providers = ();
1737 # Read-in providers settings file.
1738 &General
::readhasharray
($providers_settings_file, \
%configured_providers);
1740 # Loop through the hash of configured providers.
1741 foreach my $id (keys %configured_providers) {
1742 # Assign nice human-readable values to the data fields.
1743 my $provider_handle = $configured_providers{$id}[0];
1744 my $subscription_code = $configured_providers{$id}[1];
1746 # Check if the current processed provider is the requested one.
1747 if ($provider_handle eq $provider) {
1748 # Return the obtained subscription code.
1749 return $subscription_code;
1753 # No subscription code found - return nothing.
1758 ## Function to get the ruleset date for a given provider.
1760 ## The function simply return the creation date in a human read-able format
1761 ## of the stored providers rulesfile.
1763 sub get_ruleset_date
($) {
1764 my ($provider) = @_;
1768 # Get the stored rulesfile for this provider.
1769 my $stored_rulesfile = &_get_dl_rulesfile
($provider);
1771 # Check if we got a file.
1772 if (-f
$stored_rulesfile) {
1773 # Call stat on the rulestarball.
1774 my $stat = stat("$stored_rulesfile");
1776 # Get timestamp the file creation.
1777 $mtime = $stat->mtime;
1780 # Check if the timestamp has not been grabbed.
1782 # Return N/A for Not available.
1786 # Convert into human read-able format.
1787 $date = strftime
('%Y-%m-%d %H:%M:%S', localtime($mtime));
1794 ## Function to gather the version of suricata.
1796 sub get_suricata_version
($) {
1799 # Execute piped suricata command and return the version information.
1800 open(SURICATA
, "suricata -V |") or die "Couldn't execute program: $!";
1802 # Grab and store the output of the piped program.
1803 my $version_string = <SURICATA
>;
1809 chomp($version_string);
1811 # Grab the version from the version string.
1812 $version_string =~ /([0-9]+([.][0-9]+)+)/;
1814 # Splitt the version into single chunks.
1815 my ($major_ver, $minor_ver, $patchlevel) = split(/\./, $1);
1817 # Check and return the requested version sheme.
1818 if ($format eq "major") {
1819 # Return the full version.
1820 return "$major_ver";
1821 } elsif ($format eq "minor") {
1822 # Return the major and minor part.
1823 return "$major_ver.$minor_ver";
1825 # Return the full version string.
1826 return "$major_ver.$minor_ver.$patchlevel";
1831 ## Function to get the enabled application layer protocols.
1833 sub get_suricata_enabled_app_layer_protos
() {
1834 # Array to store and return the enabled app layer protos.
1835 my @enabled_app_layer_protos = ();
1837 # Execute piped suricata command and return the list of
1838 # enabled application layer protocols.
1839 open(SURICATA
, "suricata --list-app-layer-protos |") or die "Could not execute program: $!";
1841 # Grab and store the list of enabled application layer protocols.
1842 my @output = <SURICATA
>;
1847 # Merge allways enabled static application layers protocols array.
1848 @enabled_app_layer_protos = @static_enabled_app_layer_protos;
1850 # Loop through the array which contains the output of suricata.
1851 foreach my $line (@output) {
1852 # Skip header line which starts with "===".
1853 next if ($line =~ /^\s*=/);
1855 # Skip info or warning lines.
1856 next if ($line =~ /\s*--/);
1861 # Add enabled app layer proto to the array.
1862 push(@enabled_app_layer_protos, $line);
1866 @enabled_app_layer_protos = sort(@enabled_app_layer_protos);
1869 return @enabled_app_layer_protos;
1873 ## Function to generate the rules file with whitelisted addresses.
1875 sub generate_ignore_file
() {
1878 # SID range 1000000-1999999 Reserved for Local Use
1879 # Put your custom rules in this range to avoid conflicts
1882 # Read-in ignoredfile.
1883 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
1885 # Open ignorefile for writing.
1886 open(FILE
, ">$IDS::whitelist_file") or die "Could not write to $IDS::whitelist_file. $!\n";
1888 # Config file header.
1889 print FILE
"# Autogenerated file.\n";
1890 print FILE
"# All user modifications will be overwritten.\n\n";
1892 # Add all user defined addresses to the whitelist.
1894 # Check if the hash contains any elements.
1895 if (keys (%ignored)) {
1896 # Loop through the entire hash and write the host/network
1897 # and remark to the ignore file.
1898 while ( (my $key) = each %ignored) {
1899 my $address = $ignored{$key}[0];
1900 my $remark = $ignored{$key}[1];
1901 my $status = $ignored{$key}[2];
1903 # Check if the status of the entry is "enabled".
1904 if ($status eq "enabled") {
1905 # Check if the address/network is valid.
1906 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
1907 # Write rule line to the file to pass any traffic from this IP
1908 print FILE
"pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; bypass; sid:$sid\;)\n";
1921 ## Function to set correct ownership for single files and directories.
1924 sub set_ownership
($) {
1927 # User and group of the WUI.
1928 my $uname = "nobody";
1929 my $grname = "nobody";
1931 # The chown function implemented in perl requies the user and group as nummeric id's.
1932 my $uid = getpwnam($uname);
1933 my $gid = getgrnam($grname);
1935 # Check if the given target exists.
1937 # Stop the script and print error message.
1938 die "The $target does not exist. Cannot change the ownership!\n";
1941 # Check weather the target is a file or directory.
1943 # Change ownership ot the single file.
1944 chown($uid, $gid, "$target");
1945 } elsif (-d
$target) {
1946 # Do a directory listing.
1947 opendir(DIR
, $target) or die $!;
1948 # Loop through the direcory.
1949 while (my $file = readdir(DIR
)) {
1951 # We only want files.
1952 next unless (-f
"$target/$file");
1954 # Set correct ownership for the files.
1955 chown($uid, $gid, "$target/$file");
1960 # Change ownership of the directory.
1961 chown($uid, $gid, "$target");
1966 ## Function to read-in the aliases file and returns all configured and enabled aliases.
1969 # Location of the aliases file.
1970 my $aliases_file = "${General::swroot}/ethernet/aliases";
1972 # Array to store the aliases.
1975 # Check if the file is empty.
1976 if (-z
$aliases_file) {
1977 # Abort nothing to do.
1981 # Open the aliases file.
1982 open(ALIASES
, $aliases_file) or die "Could not open $aliases_file. $!\n";
1984 # Loop through the file content.
1985 while (my $line = <ALIASES
>) {
1989 # Splitt line content into single chunks.
1990 my ($address, $state, $remark) = split(/\,/, $line);
1992 # Check if the state of the current processed alias is "on".
1993 if ($state eq "on") {
1994 # Check if the address is valid.
1995 if(&Network
::check_ip_address
($address)) {
1996 # Add the alias to the array of aliases.
1997 push(@aliases, $address);
2002 # Close file handle.
2010 ## Function to grab the current assigned IP-address on red.
2012 sub get_red_address
() {
2013 # File, which contains the current IP-address of the red interface.
2014 my $file = "${General::swroot}/red/local-ipaddress";
2016 # Check if the file exists.
2018 # Open the given file.
2019 open(FILE
, "$file") or die "Could not open $file.";
2021 # Obtain the address from the first line of the file.
2022 my $address = <FILE
>;
2030 # Check if the grabbed address is valid.
2031 if (&General
::validip
($address)) {
2032 # Return the address.
2042 ## Function to get the used rules files of a given provider.
2044 sub read_used_provider_rulesfiles
($) {
2045 my ($provider) = @_;
2047 # Array to store the used rulefiles.
2048 my @used_rulesfiles = ();
2050 # Get the used rulesefile file for the provider.
2051 my $rulesfile_file = &get_used_provider_rulesfile_file
($provider);
2053 # Check if the a used rulesfile exists for this provider.
2054 if (-f
$rulesfile_file) {
2055 # Open the file or used rulefiles and read-in content.
2056 open(FILE
, $rulesfile_file) or die "Could not open $rulesfile_file. $!\n";
2059 # Assign the current line to a nice variable.
2066 next if ($line =~ /\#/);
2069 next if ($line =~ /^\s*$/);
2071 # Gather the rulefile.
2072 if ($line =~ /.*- (.*)/) {
2075 # Add the rulefile to the array of used rulesfiles.
2076 push(@used_rulesfiles, $rulefile);
2084 # Return the array of used rulesfiles.
2085 return @used_rulesfiles;
2089 ## Function to write the lock file for locking the WUI, while
2090 ## the autoupdate script runs.
2092 sub lock_ids_page
() {
2093 # Call subfunction to create the file.
2094 &create_empty_file
($ids_page_lock_file);
2098 ## Function to release the lock of the WUI, again.
2100 sub unlock_ids_page
() {
2102 unlink($ids_page_lock_file);