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 IPFire Team <info@ipfire.org>. #
22 ############################################################################
26 require '/var/ipfire/general-functions.pl';
28 # Location where all config and settings files are stored.
29 our $settingsdir = "${General::swroot}/suricata";
31 # File where the used rulefiles are stored.
32 our $used_rulefiles_file = "$settingsdir/suricata-used-rulefiles.yaml";
34 # File where the addresses of the homenet are stored.
35 our $homenet_file = "$settingsdir/suricata-homenet.yaml";
37 # File which contains the enabled sids.
38 our $enabled_sids_file = "$settingsdir/oinkmaster-enabled-sids.conf";
40 # File which contains the disabled sids.
41 our $disabled_sids_file = "$settingsdir/oinkmaster-disabled-sids.conf";
43 # File which contains wheater the rules should be changed.
44 our $modify_sids_file = "$settingsdir/oinkmaster-modify-sids.conf";
46 # File which stores the configured IPS settings.
47 our $ids_settings_file = "$settingsdir/settings";
49 # File which stores the configured rules-settings.
50 our $rules_settings_file = "$settingsdir/rules-settings";
52 # File which stores the configured settings for whitelisted addresses.
53 our $ignored_file = "$settingsdir/ignored";
55 # Location and name of the tarball which contains the ruleset.
56 our $rulestarball = "/var/tmp/idsrules.tar.gz";
58 # File to store any errors, which also will be read and displayed by the wui.
59 our $storederrorfile = "/tmp/ids_storederror";
61 # Location where the rulefiles are stored.
62 our $rulespath = "/var/lib/suricata";
64 # File which contains the rules to whitelist addresses on suricata.
65 our $whitelist_file = "$rulespath/whitelist.rules";
67 # File which contains a list of all supported ruleset sources.
68 # (Sourcefire, Emergingthreads, etc..)
69 our $rulesetsourcesfile = "$settingsdir/ruleset-sources";
71 # The pidfile of the IDS.
72 our $idspidfile = "/var/run/suricata.pid";
74 # Location of suricatactrl.
75 my $suricatactrl = "/usr/local/bin/suricatactrl";
77 # Array with allowed commands of suricatactrl.
78 my @suricatactrl_cmds = ( 'start', 'stop', 'restart', 'reload', 'fix-rules-dir', 'cron' );
80 # Array with supported cron intervals.
81 my @cron_intervals = ('off', 'daily', 'weekly' );
84 ## Function to check and create all IDS related files, if the does not exist.
86 sub check_and_create_filelayout
() {
87 # Check if the files exist and if not, create them.
88 unless (-f
"$enabled_sids_file") { &create_empty_file
($enabled_sids_file); }
89 unless (-f
"$disabled_sids_file") { &create_empty_file
($disabled_sids_file); }
90 unless (-f
"$modify_sids_file") { &create_empty_file
($modify_sids_file); }
91 unless (-f
"$used_rulefiles_file") { &create_empty_file
($used_rulefiles_file); }
92 unless (-f
"$ids_settings_file") { &create_empty_file
($ids_settings_file); }
93 unless (-f
"$rules_settings_file") { &create_empty_file
($rules_settings_file); }
94 unless (-f
"$ignored_file") { &create_empty_file
($ignored_file); }
95 unless (-f
"$whitelist_file" ) { &create_empty_file
($whitelist_file); }
99 ## Function for checking if at least 300MB of free disk space are available
100 ## on the "/var" partition.
102 sub checkdiskspace
() {
103 # Call diskfree to gather the free disk space of /var.
104 my @df = `/bin/df -B M /var`;
106 # Loop through the output.
107 foreach my $line (@df) {
108 # Ignore header line.
109 next if $line =~ m/^Filesystem/;
111 # Search for a line with the device information.
112 if ($line =~ m/dev/ ) {
113 # Split the line into single pieces.
114 my @values = split(' ', $line);
115 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
117 # Check if the available disk space is more than 300MB.
118 if ($available < 300) {
119 # Log error to syslog.
120 &_log_to_syslog
("Not enough free disk space on /var. Only $available MB from 300 MB available.");
122 # Exit function and return "1" - False.
128 # Everything okay, return nothing.
133 ## This function is responsible for downloading the configured IDS ruleset.
135 ## * At first it obtains from the stored rules settings which ruleset should be downloaded.
136 ## * The next step is to get the download locations for all available rulesets.
137 ## * After that, the function will check if an upstream proxy should be used and grab the settings.
138 ## * The last step will be to generate the final download url, by obtaining the URL for the desired
139 ## ruleset, add the settings for the upstream proxy and final grab the rules tarball from the server.
141 sub downloadruleset
{
142 # Get rules settings.
143 my %rulessettings=();
144 &General
::readhash
("$rules_settings_file", \
%rulessettings);
146 # Check if a ruleset has been configured.
147 unless($rulessettings{'RULES'}) {
148 # Log that no ruleset has been configured and abort.
149 &_log_to_syslog
("No ruleset source has been configured.");
155 # Get all available ruleset locations.
156 my %rulesetsources=();
157 &General
::readhash
($rulesetsourcesfile, \
%rulesetsources);
159 # Read proxysettings.
160 my %proxysettings=();
161 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
163 # Load required perl module to handle the download.
166 # Init the download module.
167 my $downloader = LWP
::UserAgent
->new;
169 # Set timeout to 10 seconds.
170 $downloader->timeout(10);
172 # Check if an upstream proxy is configured.
173 if ($proxysettings{'UPSTREAM_PROXY'}) {
174 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
177 # Check if we got a peer.
179 $proxy_url = "http://";
181 # Check if the proxy requires authentication.
182 if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) {
183 $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@";
186 # Add proxy server address and port.
187 $proxy_url .= "$peer\:$peerport";
189 # Log error message and break.
190 &_log_to_syslog
("Could not proper configure the proxy server access.");
192 # Return "1" - false.
196 # Setup proxy settings.
197 $downloader->proxy(['http', 'https'], $proxy_url);
200 # Grab the right url based on the configured vendor.
201 my $url = $rulesetsources{$rulessettings{'RULES'}};
203 # Check if the vendor requires an oinkcode and add it if needed.
204 $url =~ s/\<oinkcode\>/$rulessettings{'OINKCODE'}/g;
206 # Abort if no url could be determined for the vendor.
208 # Log error and abort.
209 &_log_to_syslog
("Unable to gather a download URL for the selected ruleset.");
213 # Pass the requrested url to the downloader.
214 my $request = HTTP
::Request
->new(HEAD
=> $url);
216 # Accept the html header.
217 $request->header('Accept' => 'text/html');
219 # Perform the request and fetch the html header.
220 my $response = $downloader->request($request);
222 # Check if there was any error.
223 unless ($response->is_success) {
225 my $error = $response->content;
228 &_log_to_syslog
("Unable to download the ruleset. \($error\)");
230 # Return "1" - false.
234 # Assign the fetched header object.
235 my $header = $response->headers;
237 # Grab the remote file size from the object and store it in the
239 my $remote_filesize = $header->content_length;
241 # Load perl module to deal with temporary files.
244 # Generate temporay file name, located in "/var/tmp" and with a suffix of ".tar.gz".
245 my $tmp = File
::Temp
->new( SUFFIX
=> ".tar.gz", DIR
=> "/var/tmp/", UNLINK
=> 0 );
246 my $tmpfile = $tmp->filename();
248 # Pass the requested url to the downloader.
249 my $request = HTTP
::Request
->new(GET
=> $url);
251 # Perform the request and save the output into the tmpfile.
252 my $response = $downloader->request($request, $tmpfile);
254 # Check if there was any error.
255 unless ($response->is_success) {
257 my $error = $response->content;
260 &_log_to_syslog
("Unable to download the ruleset. \($error\)");
262 # Return "1" - false.
266 # Load perl stat module.
269 # Perform stat on the tmpfile.
270 my $stat = stat($tmpfile);
272 # Grab the local filesize of the downloaded tarball.
273 my $local_filesize = $stat->size;
275 # Check if both file sizes match.
276 unless ($remote_filesize eq $local_filesize) {
278 &_log_to_syslog
("Unable to completely download the ruleset. ");
279 &_log_to_syslog
("Only got $local_filesize Bytes instead of $remote_filesize Bytes. ");
281 # Delete temporary file.
284 # Return "1" - false.
288 # Load file copy module, which contains the move() function.
291 # Overwrite existing rules tarball with the new downloaded one.
292 move
("$tmpfile", "$rulestarball");
294 # If we got here, everything worked fine. Return nothing.
299 ## A tiny wrapper function to call the oinkmaster script.
302 # Check if the files in rulesdir have the correct permissions.
303 &_check_rulesdir_permissions
();
305 # Cleanup the rules directory before filling it with the new rulest.
306 &_cleanup_rulesdir
();
308 # Load perl module to talk to the kernel syslog.
309 use Sys
::Syslog
qw(:DEFAULT setlogsock);
311 # Establish the connection to the syslog service.
312 openlog
('oinkmaster', 'cons,pid', 'user');
314 # Call oinkmaster to generate ruleset.
315 open(OINKMASTER
, "/usr/local/bin/oinkmaster.pl -v -s -u file://$rulestarball -C $settingsdir/oinkmaster.conf -o $rulespath|") or die "Could not execute oinkmaster $!\n";
317 # Log output of oinkmaster to syslog.
318 while(<OINKMASTER
>) {
319 # The syslog function works best with an array based input,
320 # so generate one before passing the message details to syslog.
321 my @syslog = ("INFO", "$_");
323 # Send the log message.
327 # Close the pipe to oinkmaster process.
330 # Close the log handle.
335 ## Function to do all the logging stuff if the downloading or updating of the ruleset fails.
340 # Remove any newline.
343 # Call private function to log the error message to syslog.
344 &_log_to_syslog
($error);
346 # Call private function to write/store the error message in the storederrorfile.
347 &_store_error_message
($error);
351 ## Function to log a given error message to the kernel syslog.
353 sub _log_to_syslog
($) {
356 # Load perl module to talk to the kernel syslog.
357 use Sys
::Syslog
qw(:DEFAULT setlogsock);
359 # The syslog function works best with an array based input,
360 # so generate one before passing the message details to syslog.
361 my @syslog = ("ERR", "<ERROR> $message");
363 # Establish the connection to the syslog service.
364 openlog
('oinkmaster', 'cons,pid', 'user');
366 # Send the log message.
369 # Close the log handle.
374 ## Private function to write a given error message to the storederror file.
376 sub _store_error_message
($) {
379 # Remove any newline.
382 # Open file for writing.
383 open (ERRORFILE
, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n";
385 # Write error to file.
386 print ERRORFILE
"$message\n";
391 # Set correct ownership for the file.
392 &set_ownership
("$storederrorfile");
396 ## Function to get a list of all available network zones.
398 sub get_available_network_zones
() {
400 my %netsettings = ();
401 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
403 # Obtain the configuration type from the netsettings hash.
404 my $config_type = $netsettings{'CONFIG_TYPE'};
406 # Hash which contains the conversation from the config mode
407 # to the existing network interface names. They are stored like
410 # Mode "0" red is a modem and green
411 # Mode "1" red is a netdev and green
412 # Mode "2" red, green and orange
413 # Mode "3" red, green and blue
414 # Mode "4" red, green, blue, orange
415 my %config_type_to_interfaces = (
416 "0" => [ "red", "green" ],
417 "1" => [ "red", "green" ],
418 "2" => [ "red", "green", "orange" ],
419 "3" => [ "red", "green", "blue" ],
420 "4" => [ "red", "green", "blue", "orange" ]
423 # Obtain and dereference the corresponding network interaces based on the read
424 # network config type.
425 my @network_zones = @
{ $config_type_to_interfaces{$config_type} };
428 return @network_zones;
432 ## Function to check if the IDS is running.
434 sub ids_is_running
() {
436 # Open PID file for reading.
437 open(PIDFILE
, "$idspidfile") or die "Could not open $idspidfile. $!\n";
439 # Grab the process-id.
445 # Remove any newline.
448 # Check if a directory for the process-id exists in proc.
449 if(-d
"/proc/$pid") {
450 # The IDS daemon is running return the process id.
455 # Return nothing - IDS is not running.
460 ## Function to call suricatactrl binary with a given command.
462 sub call_suricatactrl
($) {
464 my ($option, $interval) = @_;
466 # Loop through the array of supported commands and check if
467 # the given one is part of it.
468 foreach my $cmd (@suricatactrl_cmds) {
469 # Skip current command unless the given one has been found.
470 next unless($cmd eq $option);
472 # Check if the given command is "cron".
473 if ($option eq "cron") {
474 # Check if an interval has been given.
476 # Check if the given interval is valid.
477 foreach my $element (@cron_intervals) {
478 # Skip current element until the given one has been found.
479 next unless($element eq $interval);
481 # Call the suricatactrl binary and pass the "cron" command
482 # with the requrested interval.
483 system("$suricatactrl $option $interval &>/dev/null");
490 # If we got here, the given interval is not supported or none has been given. - Return nothing.
493 # Call the suricatactrl binary and pass the requrested
495 system("$suricatactrl $option &>/dev/null");
502 # Command not found - return nothing.
507 ## Function to create a new empty file.
509 sub create_empty_file
($) {
512 # Check if the given file exists.
514 # Do nothing to prevent from overwriting existing files.
518 # Open the file for writing.
519 open(FILE
, ">$file") or die "Could not write to $file. $!\n";
529 ## Private function to check if the file permission of the rulespath are correct.
530 ## If not, call suricatactrl to fix them.
532 sub _check_rulesdir_permissions
() {
533 # Check if the rulepath main directory is writable.
534 unless (-W
$rulespath) {
535 # If not call suricatctrl to fix it.
536 &call_suricatactrl
("fix-rules-dir");
539 # Open snort rules directory and do a directory listing.
540 opendir(DIR
, $rulespath) or die $!;
541 # Loop through the direcory.
542 while (my $file = readdir(DIR
)) {
543 # We only want files.
544 next unless (-f
"$rulespath/$file");
546 # Check if the file is writable by the user.
547 if (-W
"$rulespath/$file") {
548 # Everything is okay - go on to the next file.
551 # There are wrong permissions, call suricatactrl to fix it.
552 &call_suricatactrl
("fix-rules-dir");
558 ## Private function to cleanup the directory which contains
559 ## the IDS rules, before extracting and modifing the new ruleset.
561 sub _cleanup_rulesdir
() {
562 # Open rules directory and do a directory listing.
563 opendir(DIR
, $rulespath) or die $!;
565 # Loop through the direcory.
566 while (my $file = readdir(DIR
)) {
567 # We only want files.
568 next unless (-f
"$rulespath/$file");
570 # Skip element if it has config as file extension.
571 next if ($file =~ m/\.config$/);
573 # Delete the current processed file, if not, exit this function
574 # and return an error message.
575 unlink("$rulespath/$file") or return "Could not delete $rulespath/$file. $!\n";
583 ## Function to generate the file which contains the home net information.
585 sub generate_home_net_file
() {
588 # Read-in network settings.
589 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
591 # Get available network zones.
592 my @network_zones = &get_available_network_zones
();
594 # Temporary array to store network address and prefix of the configured
598 # Loop through the array of available network zones.
599 foreach my $zone (@network_zones) {
600 # Convert current zone name into upper case.
603 # Generate key to access the required data from the netsettings hash.
604 my $zone_netaddress = $zone . "_NETADDRESS";
605 my $zone_netmask = $zone . "_NETMASK";
607 # Obtain the settings from the netsettings hash.
608 my $netaddress = $netsettings{$zone_netaddress};
609 my $netmask = $netsettings{$zone_netmask};
611 # Convert the subnetmask into prefix notation.
612 my $prefix = &Network
::convert_netmask2prefix
($netmask);
614 # Generate full network string.
615 my $network = join("/", $netaddress,$prefix);
617 # Check if the network is valid.
618 if(&Network
::check_subnet
($network)) {
619 # Add the generated network to the array of networks.
620 push(@networks, $network);
623 # Check if the current processed zone is red.
625 # Check if the configured RED_TYPE is static.
626 if ($netsettings{'RED_TYPE'} eq "STATIC") {
627 # Get configured and enabled aliases.
628 my @aliases = &get_aliases
();
630 # Loop through the array.
631 foreach my $alias (@aliases) {
633 my $network = join("/", $alias, "32");
635 # Add the generated network to the array of networks.
636 push(@networks, $network);
642 # Format home net declaration.
645 # Loop through the array of networks.
646 foreach my $network (@networks) {
647 # Add the network to the line.
648 $line = "$line" . "$network";
650 # Check if the current network was the last in the array.
651 if ($network eq $networks[-1]) {
653 $line = "$line" . "\]\"";
655 # Add "," for the next network.
656 $line = "$line" . "\,";
660 # Open file to store the addresses of the home net.
661 open(FILE
, ">$homenet_file") or die "Could not open $homenet_file. $!\n";
664 print FILE
"%YAML 1.1\n";
665 print FILE
"---\n\n";
667 # Print notice about autogenerated file.
668 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
670 # Print the generated and required HOME_NET declaration to the file.
671 print FILE
"HOME_NET:\t$line\n";
678 ## Function to generate and write the file for used rulefiles.
680 sub write_used_rulefiles_file
(@
) {
683 # Open file for used rulefiles.
684 open (FILE
, ">$used_rulefiles_file") or die "Could not write to $used_rulefiles_file. $!\n";
686 # Write yaml header to the file.
687 print FILE
"%YAML 1.1\n";
688 print FILE
"---\n\n";
690 # Write header to file.
691 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
693 # Allways use the whitelist.
694 print FILE
" - whitelist.rules\n";
696 # Loop through the array of given files.
697 foreach my $file (@files) {
698 # Check if the given filename exists and write it to the file of used rulefiles.
699 if(-f
"$rulespath/$file") {
700 print FILE
" - $file\n";
704 # Close file after writing.
709 ## Function to generate and write the file for modify the ruleset.
711 sub write_modify_sids_file
($) {
712 my ($ruleaction) = @_;
714 # Open modify sid's file for writing.
715 open(FILE
, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
718 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
720 # Check if the traffic only should be monitored.
721 unless($ruleaction eq "alert") {
722 # Tell oinkmaster to switch all rules from alert to drop.
723 print FILE
"modifysid \* \"alert\" \| \"drop\"\n";
731 ## Function to gather the version of suricata.
733 sub get_suricata_version
($) {
736 # Execute piped suricata command and return the version information.
737 open(SURICATA
, "suricata -V |") or die "Couldn't execute program: $!";
739 # Grab and store the output of the piped program.
740 my $version_string = <SURICATA
>;
746 chomp($version_string);
748 # Grab the version from the version string.
749 $version_string =~ /([0-9]+([.][0-9]+)+)/;
751 # Splitt the version into single chunks.
752 my ($major_ver, $minor_ver, $patchlevel) = split(/\./, $1);
754 # Check and return the requested version sheme.
755 if ($format eq "major") {
756 # Return the full version.
758 } elsif ($format eq "minor") {
759 # Return the major and minor part.
760 return "$major_ver.$minor_ver";
762 # Return the full version string.
763 return "$major_ver.$minor_ver.$patchlevel";
768 ## Function to generate the rules file with whitelisted addresses.
770 sub generate_ignore_file
() {
773 # SID range 1000000-1999999 Reserved for Local Use
774 # Put your custom rules in this range to avoid conflicts
777 # Read-in ignoredfile.
778 &General
::readhasharray
($IDS::ignored_file
, \
%ignored);
780 # Open ignorefile for writing.
781 open(FILE
, ">$IDS::whitelist_file") or die "Could not write to $IDS::whitelist_file. $!\n";
783 # Config file header.
784 print FILE
"# Autogenerated file.\n";
785 print FILE
"# All user modifications will be overwritten.\n\n";
787 # Add all user defined addresses to the whitelist.
789 # Check if the hash contains any elements.
790 if (keys (%ignored)) {
791 # Loop through the entire hash and write the host/network
792 # and remark to the ignore file.
793 while ( (my $key) = each %ignored) {
794 my $address = $ignored{$key}[0];
795 my $remark = $ignored{$key}[1];
796 my $status = $ignored{$key}[2];
798 # Check if the status of the entry is "enabled".
799 if ($status eq "enabled") {
800 # Check if the address/network is valid.
801 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
802 # Write rule line to the file to pass any traffic from this IP
803 print FILE
"pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n";
816 ## Function to set correct ownership for single files and directories.
819 sub set_ownership
($) {
822 # User and group of the WUI.
823 my $uname = "nobody";
824 my $grname = "nobody";
826 # The chown function implemented in perl requies the user and group as nummeric id's.
827 my $uid = getpwnam($uname);
828 my $gid = getgrnam($grname);
830 # Check if the given target exists.
832 # Stop the script and print error message.
833 die "The $target does not exist. Cannot change the ownership!\n";
836 # Check weather the target is a file or directory.
838 # Change ownership ot the single file.
839 chown($uid, $gid, "$target");
840 } elsif (-d
$target) {
841 # Do a directory listing.
842 opendir(DIR
, $target) or die $!;
843 # Loop through the direcory.
844 while (my $file = readdir(DIR
)) {
846 # We only want files.
847 next unless (-f
"$target/$file");
849 # Set correct ownership for the files.
850 chown($uid, $gid, "$target/$file");
855 # Change ownership of the directory.
856 chown($uid, $gid, "$target");
861 ## Function to read-in the aliases file and returns all configured and enabled aliases.
864 # Location of the aliases file.
865 my $aliases_file = "${General::swroot}/ethernet/aliases";
867 # Array to store the aliases.
870 # Check if the file is empty.
871 if (-z
$aliases_file) {
872 # Abort nothing to do.
876 # Open the aliases file.
877 open(ALIASES
, $aliases_file) or die "Could not open $aliases_file. $!\n";
879 # Loop through the file content.
880 while (my $line = <ALIASES
>) {
884 # Splitt line content into single chunks.
885 my ($address, $state, $remark) = split(/\,/, $line);
887 # Check if the state of the current processed alias is "on".
888 if ($state eq "on") {
889 # Check if the address is valid.
890 if(&Network
::check_ip_address
($address)) {
891 # Add the alias to the array of aliases.
892 push(@aliases, $address);