]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/cfgroot/ids-functions.pl
update-ids-ruleset: Fix typo in return code.
[people/pmueller/ipfire-2.x.git] / config / cfgroot / ids-functions.pl
CommitLineData
8dcebe53
SS
1#!/usr/bin/perl -w
2############################################################################
3# #
4# This file is part of the IPFire Firewall. #
5# #
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. #
10# #
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. #
15# #
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 #
19# #
fd2dccaa 20# Copyright (C) 2018-2019 IPFire Team <info@ipfire.org> #
8dcebe53
SS
21# #
22############################################################################
23
e55fa2f7
SS
24use strict;
25
8dcebe53
SS
26package IDS;
27
28require '/var/ipfire/general-functions.pl';
abffcc99 29require "${General::swroot}/network-functions.pl";
35bc92a3 30require "${General::swroot}/suricata/ruleset-sources";
8dcebe53 31
7d895608
SS
32# Load perl module to deal with Archives.
33use Archive::Tar;
34
35# Load perl module to deal with files and path.
36use File::Basename;
37
38# Load module to move files.
39use File::Copy;
40
41# Load module to recursely remove files and a folder.
42use File::Path qw(rmtree);
43
44# Load module to get file stats.
45use File::stat;
46
47# Load module to deal with temporary files.
48use File::Temp;
49
061391e7
SS
50# Load module to deal with the date formats used by the HTTP protocol.
51use HTTP::Date;
52
7d895608
SS
53# Load the libwwwperl User Agent module.
54use LWP::UserAgent;
55
56# Load function from posix module to format time strings.
57use POSIX qw (strftime);
58
59# Load module to talk to the kernel log daemon.
60use Sys::Syslog qw(:DEFAULT setlogsock);
61
02844177 62# Location where all config and settings files are stored.
164eab66 63our $settingsdir = "${General::swroot}/suricata";
02844177 64
ddaf8ae1
SS
65# File where the main file for providers ruleset inclusion exists.
66our $suricata_used_providers_file = "$settingsdir/suricata-used-providers.yaml";
67
88eb5626 68# File for static ruleset inclusions.
43d12991 69our $suricata_default_rulefiles_file = "$settingsdir/suricata-default-rules.yaml";
88eb5626 70
b02e30fd
SS
71# File where the addresses of the homenet are stored.
72our $homenet_file = "$settingsdir/suricata-homenet.yaml";
73
30ee98e9
SS
74# File where the addresses of the used DNS servers are stored.
75our $dns_servers_file = "$settingsdir/suricata-dns-servers.yaml";
76
e698090e
SS
77# File where the HTTP ports definition is stored.
78our $http_ports_file = "$settingsdir/suricata-http-ports.yaml";
79
15832b10
SS
80# File which contains includes for provider specific rule modifications.
81our $oinkmaster_provider_includes_file = "$settingsdir/oinkmaster-provider-includes.conf";
82
b02e30fd
SS
83# File which contains wheater the rules should be changed.
84our $modify_sids_file = "$settingsdir/oinkmaster-modify-sids.conf";
85
86# File which stores the configured IPS settings.
87our $ids_settings_file = "$settingsdir/settings";
88
a8d36d3e
SS
89# File which stores the used and configured ruleset providers.
90our $providers_settings_file = "$settingsdir/providers-settings";
91
b02e30fd
SS
92# File which stores the configured settings for whitelisted addresses.
93our $ignored_file = "$settingsdir/ignored";
94
788a71f5
SS
95# Location where the downloaded rulesets are stored.
96our $dl_rules_path = "/var/tmp";
97
3983aebd 98# File to store any errors, which also will be read and displayed by the wui.
77910792 99our $storederrorfile = "/tmp/ids_storederror";
3983aebd 100
8076deba
SS
101# File to lock the WUI, while the autoupdate script runs.
102our $ids_page_lock_file = "/tmp/ids_page_locked";
103
298ef5ba 104# Location where the rulefiles are stored.
21cab141 105our $rulespath = "/var/lib/suricata";
298ef5ba 106
43d12991
SS
107# Location where the default rulefils are stored.
108our $default_rulespath = "/usr/share/suricata/rules";
109
6983a96e
SS
110# Location where the addition config files are stored.
111our $configspath = "/usr/share/suricata";
112
23b56052 113# Location of the classification file.
6983a96e 114our $classification_file = "$configspath/classification.config";
23b56052 115
8bd74e12
SS
116# Location of the sid to msg mappings file.
117our $sid_msg_file = "$rulespath/sid-msg.map";
118
612bb2df
SS
119# Location to store local rules. This file will not be touched.
120our $local_rules_file = "$rulespath/local.rules";
121
b02e30fd
SS
122# File which contains the rules to whitelist addresses on suricata.
123our $whitelist_file = "$rulespath/whitelist.rules";
124
bce84f39
SS
125# File which contains a list of all supported ruleset sources.
126# (Sourcefire, Emergingthreads, etc..)
127our $rulesetsourcesfile = "$settingsdir/ruleset-sources";
128
796eea21
SS
129# The pidfile of the IDS.
130our $idspidfile = "/var/run/suricata.pid";
131
5240a809
SS
132# Location of suricatactrl.
133my $suricatactrl = "/usr/local/bin/suricatactrl";
134
788a71f5
SS
135# Prefix for each downloaded ruleset.
136my $dl_rulesfile_prefix = "idsrules";
137
0fbfffea
SS
138# Temporary directory where the rulesets will be extracted.
139my $tmp_directory = "/tmp/ids_tmp";
140
6c9d3eee
SS
141# Temporary directory where the extracted rules files will be stored.
142my $tmp_rules_directory = "$tmp_directory/rules";
143
144# Temporary directory where the extracted additional config files will be stored.
145my $tmp_conf_directory = "$tmp_directory/conf";
146
5240a809 147# Array with allowed commands of suricatactrl.
ed06bc81
SS
148my @suricatactrl_cmds = ( 'start', 'stop', 'restart', 'reload', 'fix-rules-dir', 'cron' );
149
150# Array with supported cron intervals.
151my @cron_intervals = ('off', 'daily', 'weekly' );
5240a809 152
e698090e
SS
153# Array which contains the HTTP ports, which statically will be declared as HTTP_PORTS in the
154# http_ports_file.
155my @http_ports = ('80', '81');
156
88eb5626 157# Array which contains a list of rulefiles which always will be included if they exist.
bb39fac4
SS
158my @static_included_rulefiles = ('local.rules', 'whitelist.rules');
159
160# Array which contains a list of allways enabled application layer protocols.
161my @static_enabled_app_layer_protos = ('app-layer', 'decoder', 'files', 'stream');
88eb5626 162
788a71f5
SS
163# Hash which allows to convert the download type (dl_type) to a file suffix.
164my %dl_type_to_suffix = (
165 "archive" => ".tar.gz",
166 "plain" => ".rules",
167);
168
43d12991
SS
169# Hash to translate an application layer protocol to the application name.
170my %tr_app_layer_proto = (
171 "ikev2" => "ipsec",
172 "krb5" => "kerberos",
173);
174
b02e30fd
SS
175#
176## Function to check and create all IDS related files, if the does not exist.
177#
178sub check_and_create_filelayout() {
179 # Check if the files exist and if not, create them.
15832b10 180 unless (-f "$oinkmaster_provider_includes_file") { &create_empty_file($oinkmaster_provider_includes_file); }
b02e30fd 181 unless (-f "$modify_sids_file") { &create_empty_file($modify_sids_file); }
ddaf8ae1 182 unless (-f "$suricata_used_providers_file") { &create_empty_file($suricata_used_providers_file); }
2f62fd00 183 unless (-f "$suricata_default_rulefiles_file") { &create_empty_file($suricata_default_rulefiles_file); }
b02e30fd 184 unless (-f "$ids_settings_file") { &create_empty_file($ids_settings_file); }
a8d36d3e 185 unless (-f "$providers_settings_file") { &create_empty_file($providers_settings_file); }
b02e30fd
SS
186 unless (-f "$whitelist_file" ) { &create_empty_file($whitelist_file); }
187}
188
70cc1315
SS
189#
190## Function to get a list of all available ruleset providers.
191##
192## They will be returned as a sorted array.
193#
194sub get_ruleset_providers() {
195 my @providers;
196
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);
201 }
202
203 # Sort and return the array.
204 return sort(@providers);
205}
206
5e20d6cb
SS
207#
208## Function to get a list of all enabled ruleset providers.
209##
210## They will be returned as an array.
211#
212sub get_enabled_providers () {
213 my %used_providers = ();
214
215 # Array to store the enabled providers.
216 my @enabled_providers = ();
217
218 # Read-in the providers config file.
219 &General::readhasharray("$providers_settings_file", \%used_providers);
220
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");
225
226 # Grab the provider handle.
227 my $provider = "$used_providers{$id}[0]";
228
229 # Add the provider to the array of enabled providers.
230 push(@enabled_providers, $provider);
231 }
232
233 # Return the array.
234 return @enabled_providers;
235}
236
8dcebe53
SS
237#
238## Function for checking if at least 300MB of free disk space are available
239## on the "/var" partition.
240#
241sub checkdiskspace () {
242 # Call diskfree to gather the free disk space of /var.
81631920 243 my @df = &General::system_output("/bin/df", "-B", "M", "/var");
8dcebe53
SS
244
245 # Loop through the output.
246 foreach my $line (@df) {
247 # Ignore header line.
248 next if $line =~ m/^Filesystem/;
249
250 # Search for a line with the device information.
251 if ($line =~ m/dev/ ) {
252 # Split the line into single pieces.
253 my @values = split(' ', $line);
254 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
255
256 # Check if the available disk space is more than 300MB.
257 if ($available < 300) {
65e3aef5
SS
258 # Exit function and return the available disk space.
259 return $available;
8dcebe53
SS
260 }
261 }
262 }
263
264 # Everything okay, return nothing.
265 return;
266}
267
eea2670b 268#
0f2c5211 269## This function is responsible for downloading the ruleset for a given provider.
eea2670b 270##
0f2c5211 271## * At first it initialize the downloader and sets an upstream proxy if configured.
b3c2c336 272## * The next step will be to generate the final download url, by obtaining the URL for the desired
0f2c5211
SS
273## ruleset and add the settings for the upstream proxy.
274## * Finally the function will grab the rule file or tarball from the server.
275## It tries to reduce the amount of download by using the "If-Modified-Since" HTTP header.
eea2670b 276#
e26edcc1
SS
277## Return codes:
278##
279## * "no url" - If no download URL could be gathered for the provider.
280## * "not modified" - In case the already stored rules file is up to date.
281## * "incomplete download" - When the remote file size differs from the downloaded file size.
282## * "$error" - The error message generated from the LWP::User Agent module.
283#
b3c2c336
SS
284sub downloadruleset ($) {
285 my ($provider) = @_;
286
e71804fb
SS
287 # The amount of download attempts before giving up and
288 # logging an error.
289 my $max_dl_attempts = 3;
290
eea2670b
SS
291 # Read proxysettings.
292 my %proxysettings=();
293 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
294
eea2670b 295 # Init the download module.
954dbdad
SS
296 #
297 # Request SSL hostname verification and specify path
298 # to the CA file.
299 my $downloader = LWP::UserAgent->new(
300 ssl_opts => {
301 SSL_ca_file => '/etc/ssl/cert.pem',
302 verify_hostname => 1,
303 }
304 );
eea2670b
SS
305
306 # Set timeout to 10 seconds.
307 $downloader->timeout(10);
308
309 # Check if an upstream proxy is configured.
310 if ($proxysettings{'UPSTREAM_PROXY'}) {
eea2670b
SS
311 my $proxy_url;
312
40407aee 313 $proxy_url = "http://";
eea2670b 314
40407aee
PM
315 # Check if the proxy requires authentication.
316 if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) {
317 $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@";
eea2670b
SS
318 }
319
40407aee
PM
320 # Add proxy server address and port.
321 $proxy_url .= $proxysettings{'UPSTREAM_PROXY'};
322
eea2670b 323 # Setup proxy settings.
6994f001 324 $downloader->proxy(['http', 'https'], $proxy_url);
eea2670b
SS
325 }
326
0f2c5211
SS
327 # Grab the download url for the provider.
328 my $url = $IDS::Ruleset::Providers{$provider}{'dl_url'};
b3c2c336 329
0f2c5211
SS
330 # Check if the provider requires a subscription.
331 if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") {
332 # Grab the subscription code.
333 my $subscription_code = &get_subscription_code($provider);
b3c2c336 334
0f2c5211
SS
335 # Add the subscription code to the download url.
336 $url =~ s/\<subscription_code\>/$subscription_code/g;
b3c2c336 337
0f2c5211 338 }
f264adda 339
52a557a8
SS
340 # Abort and return "no url", if no url could be determined for the provider.
341 return "no url" unless ($url);
96da5803 342
0f2c5211
SS
343 # Pass the requested URL to the downloader.
344 my $request = HTTP::Request->new(GET => $url);
061391e7 345
0f2c5211
SS
346 # Generate temporary file name, located in "/var/tmp" and with a suffix of ".tmp".
347 # The downloaded file will be stored there until some sanity checks are performed.
348 my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "/var/tmp/", UNLINK => 0 );
349 my $tmpfile = $tmp->filename();
061391e7 350
0f2c5211
SS
351 # Call function to get the final path and filename for the downloaded file.
352 my $dl_rulesfile = &_get_dl_rulesfile($provider);
061391e7 353
0f2c5211
SS
354 # Check if the rulesfile already exits, because it has been downloaded in the past.
355 #
356 # In this case we are requesting the server if the remote file has been changed or not.
357 # This will be done by sending the modification time in a special HTTP header.
358 if (-f $dl_rulesfile) {
359 # Call stat on the file.
360 my $stat = stat($dl_rulesfile);
061391e7 361
0f2c5211
SS
362 # Omit the mtime of the existing file.
363 my $mtime = $stat->mtime;
061391e7 364
0f2c5211
SS
365 # Convert the timestamp into right format.
366 my $http_date = time2str($mtime);
96da5803 367
0f2c5211
SS
368 # Add the If-Modified-Since header to the request to ask the server if the
369 # file has been modified.
370 $request->header( 'If-Modified-Since' => "$http_date" );
371 }
96da5803 372
0f2c5211
SS
373 my $dl_attempt = 1;
374 my $response;
155b3b56 375
0f2c5211
SS
376 # Download and retry on failure.
377 while ($dl_attempt <= $max_dl_attempts) {
378 # Perform the request and save the output into the tmpfile.
379 $response = $downloader->request($request, $tmpfile);
061391e7 380
0f2c5211
SS
381 # Check if the download was successfull.
382 if($response->is_success) {
383 # Break loop.
384 last;
061391e7 385
0f2c5211
SS
386 # Check if the server responds with 304 (Not Modified).
387 } elsif ($response->code == 304) {
e26edcc1
SS
388 # Return "not modified".
389 return "not modified";
e71804fb 390
0f2c5211
SS
391 # Check if we ran out of download re-tries.
392 } elsif ($dl_attempt eq $max_dl_attempts) {
393 # Obtain error.
394 my $error = $response->content;
e71804fb 395
e26edcc1
SS
396 # Return the error message from response..
397 return "$error";
155b3b56 398 }
96da5803 399
0f2c5211
SS
400 # Remove temporary file, if one exists.
401 unlink("$tmpfile") if (-e "$tmpfile");
de1199e2 402
0f2c5211
SS
403 # Increase download attempt counter.
404 $dl_attempt++;
405 }
061391e7 406
0f2c5211
SS
407 # Obtain the connection headers.
408 my $headers = $response->headers;
de1199e2 409
0f2c5211
SS
410 # Get the timestamp from header, when the file has been modified the
411 # last time.
412 my $last_modified = $headers->last_modified;
96da5803 413
0f2c5211
SS
414 # Get the remote size of the downloaded file.
415 my $remote_filesize = $headers->content_length;
25b6545a 416
0f2c5211
SS
417 # Perform stat on the tmpfile.
418 my $stat = stat($tmpfile);
eea2670b 419
0f2c5211
SS
420 # Grab the local filesize of the downloaded tarball.
421 my $local_filesize = $stat->size;
88daf7eb 422
0f2c5211
SS
423 # Check if both file sizes match.
424 if (($remote_filesize) && ($remote_filesize ne $local_filesize)) {
0f2c5211
SS
425 # Delete temporary file.
426 unlink("$tmpfile");
96da5803 427
0f2c5211 428 # Return "1" - false.
e26edcc1 429 return "incomplete download";
0f2c5211 430 }
96da5803 431
0f2c5211
SS
432 # Overwrite the may existing rulefile or tarball with the downloaded one.
433 move("$tmpfile", "$dl_rulesfile");
ae226132 434
0f2c5211
SS
435 # Check if we got a last-modified value from the server.
436 if ($last_modified) {
437 # Assign the last-modified timestamp as mtime to the
438 # rules file.
439 utime(time(), "$last_modified", "$dl_rulesfile");
96da5803
SS
440 }
441
0f2c5211
SS
442 # Delete temporary file.
443 unlink("$tmpfile");
444
445 # Set correct ownership for the tarball.
446 set_ownership("$dl_rulesfile");
447
eea2670b
SS
448 # If we got here, everything worked fine. Return nothing.
449 return;
450}
8dcebe53 451
0fbfffea
SS
452#
453## Function to extract a given ruleset.
1b5aec1b
SS
454##
455## In case the ruleset provider offers a plain file, it simply will
456## be copied.
0fbfffea
SS
457#
458sub extractruleset ($) {
459 my ($provider) = @_;
460
8353e28a
SS
461 # Disable chown functionality when uncompressing files.
462 $Archive::Tar::CHOWN = "0";
463
0fbfffea
SS
464 # Get full path and downloaded rulesfile for the given provider.
465 my $tarball = &_get_dl_rulesfile($provider);
466
467 # Check if the file exists.
468 unless (-f $tarball) {
1b5aec1b 469 &_log_to_syslog("Could not find ruleset file: $tarball");
0fbfffea
SS
470
471 # Return nothing.
472 return;
473 }
474
0fbfffea
SS
475 # Check if the temporary directories exist, otherwise create them.
476 mkdir("$tmp_directory") unless (-d "$tmp_directory");
6c9d3eee
SS
477 mkdir("$tmp_rules_directory") unless (-d "$tmp_rules_directory");
478 mkdir("$tmp_conf_directory") unless (-d "$tmp_conf_directory");
0fbfffea 479
1b5aec1b
SS
480 # Omit the type (dl_type) of the stored ruleset.
481 my $type = $IDS::Ruleset::Providers{$provider}{'dl_type'};
0fbfffea 482
1b5aec1b
SS
483 # Handle the different ruleset types.
484 if ($type eq "plain") {
485 # Generate destination filename an full path.
486 my $destination = "$tmp_rules_directory/$provider\-ruleset.rules";
0fbfffea 487
1b5aec1b
SS
488 # Copy the file into the temporary rules directory.
489 copy($tarball, $destination);
0fbfffea 490
1b5aec1b
SS
491 } elsif ( $type eq "archive") {
492 # Initialize the tar module.
493 my $tar = Archive::Tar->new($tarball);
0fbfffea 494
1b5aec1b
SS
495 # Get the filelist inside the tarball.
496 my @packed_files = $tar->list_files;
0fbfffea 497
1b5aec1b
SS
498 # Loop through the filelist.
499 foreach my $packed_file (@packed_files) {
500 my $destination;
0fbfffea 501
1b5aec1b
SS
502 # Splitt the packed file into chunks.
503 my $file = fileparse($packed_file);
0fbfffea 504
1b5aec1b
SS
505 # Handle msg-id.map file.
506 if ("$file" eq "sid-msg.map") {
507 # Set extract destination to temporary config_dir.
508 $destination = "$tmp_conf_directory/$provider\-sid-msg.map";
0fbfffea 509
1b5aec1b
SS
510 # Handle classification.conf
511 } elsif ("$file" eq "classification.config") {
512 # Set extract destination to temporary config_dir.
513 $destination = "$tmp_conf_directory/$provider\-classification.config";
0fbfffea 514
1b5aec1b
SS
515 # Handle rules files.
516 } elsif ($file =~ m/\.rules$/) {
f349c960
SS
517 # Skip rule files which are not located in the rules directory or archive root.
518 next unless(($packed_file =~ /^rules\//) || ($packed_file !~ /\//));
519
10beae64
SS
520 # Skip deleted.rules.
521 #
522 # Mostly they have been taken out for correctness or performance reasons and therfore
523 # it is not a great idea to enable any of them.
524 next if($file =~ m/deleted.rules$/);
525
1b5aec1b 526 my $rulesfilename;
0fbfffea 527
1b5aec1b
SS
528 # Splitt the filename into chunks.
529 my @filename = split("-", $file);
0fbfffea 530
1b5aec1b
SS
531 # Reverse the array.
532 @filename = reverse(@filename);
0fbfffea 533
1b5aec1b
SS
534 # Get the amount of elements in the array.
535 my $elements = @filename;
0fbfffea 536
1b5aec1b
SS
537 # Remove last element of the hash.
538 # It contains the vendor name, which will be replaced.
539 if ($elements >= 3) {
540 # Remove last element from hash.
541 pop(@filename);
542 }
543
544 # Check if the last element of the filename does not
545 # contain the providers name.
546 if ($filename[-1] ne "$provider") {
547 # Add provider name as last element.
548 push(@filename, $provider);
549 }
550
551 # Reverse the array back.
552 @filename = reverse(@filename);
553
554 # Generate the name for the rulesfile.
555 $rulesfilename = join("-", @filename);
556
557 # Set extract destination to temporaray rules_dir.
558 $destination = "$tmp_rules_directory/$rulesfilename";
559 } else {
560 # Skip all other files.
561 next;
562 }
563
2b12a010
SS
564 # Check if the destination file exists.
565 unless(-e "$destination") {
566 # Extract the file to the temporary directory.
567 $tar->extract_file("$packed_file", "$destination");
568 } else {
2b12a010
SS
569 # Generate temporary file name, located in the temporary rules directory and a suffix of ".tmp".
570 my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "$tmp_rules_directory", UNLINK => 0 );
571 my $tmpfile = $tmp->filename();
572
573 # Extract the file to the new temporary file name.
574 $tar->extract_file("$packed_file", "$tmpfile");
575
576 # Open the the existing file.
577 open(DESTFILE, ">>", "$destination") or die "Could not open $destination. $!\n";
578 open(TMPFILE, "<", "$tmpfile") or die "Could not open $tmpfile. $!\n";
579
580 # Loop through the content of the temporary file.
581 while (<TMPFILE>) {
582 # Append the content line by line to the destination file.
583 print DESTFILE "$_";
584 }
585
586 # Close the file handles.
587 close(TMPFILE);
588 close(DESTFILE);
589
590 # Remove the temporary file.
591 unlink("$tmpfile");
592 }
1b5aec1b 593 }
0fbfffea
SS
594 }
595}
596
25f5cb0d 597#
b953677b
SS
598## A wrapper function to call the oinkmaster script, setup the rules structues and
599## call the functions to merge the additional config files. (classification, sid-msg, etc.).
25f5cb0d
SS
600#
601sub oinkmaster () {
330759d8
SS
602 # Check if the files in rulesdir have the correct permissions.
603 &_check_rulesdir_permissions();
604
b953677b 605 # Cleanup the rules directory before filling it with the new rulests.
883820bd
SS
606 &_cleanup_rulesdir();
607
0130e0d1 608 # Get all enabled providers.
e31458de 609 my @enabled_providers = &get_enabled_providers();
b953677b 610
0130e0d1
SS
611 # Loop through the array of enabled providers.
612 foreach my $provider (@enabled_providers) {
1b5aec1b
SS
613 # Call the extractruleset function.
614 &extractruleset($provider);
b953677b
SS
615 }
616
0e40e1e7
SS
617 # Establish the connection to the syslog service.
618 openlog('oinkmaster', 'cons,pid', 'user');
619
25f5cb0d 620 # Call oinkmaster to generate ruleset.
b953677b 621 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";
0e40e1e7
SS
622
623 # Log output of oinkmaster to syslog.
624 while(<OINKMASTER>) {
625 # The syslog function works best with an array based input,
626 # so generate one before passing the message details to syslog.
627 my @syslog = ("INFO", "$_");
628
629 # Send the log message.
630 syslog(@syslog);
631 }
632
633 # Close the pipe to oinkmaster process.
634 close(OINKMASTER);
635
636 # Close the log handle.
637 closelog();
b953677b 638
b953677b
SS
639 # Call function to merge the classification files.
640 &merge_classifications(@enabled_providers);
641
642 # Call function to merge the sid to message mapping files.
643 &merge_sid_msg(@enabled_providers);
644
645 # Cleanup temporary directory.
16b2d281 646 &cleanup_tmp_directory();
25f5cb0d
SS
647}
648
23b56052
SS
649#
650## Function to merge the classifications for a given amount of providers and write them
651## to the classifications file.
652#
653sub merge_classifications(@) {
654 my @providers = @_;
655
656 # Hash to store all collected classifications.
657 my %classifications = ();
658
659 # Loop through the given array of providers.
660 foreach my $provider (@providers) {
661 # Generate full path to classification file.
6c9d3eee 662 my $classification_file = "$tmp_conf_directory/$provider\-classification.config";
23b56052
SS
663
664 # Skip provider if no classification file exists.
665 next unless (-f "$classification_file");
666
667 # Open the classification file.
668 open(CLASSIFICATION, $classification_file) or die "Could not open file $classification_file. $!\n";
669
670 # Loop through the file content.
671 while(<CLASSIFICATION>) {
672 # Parse the file and grab the classification details.
673 if ($_ =~/.*config classification\: (.*)/) {
674 # Split the grabbed details.
675 my ($short_name, $short_desc, $priority) = split("\,", $1);
676
677 # Check if the grabbed classification is allready known and the priority value is greater
678 # than the stored one (which causes less priority in the IDS).
679 if (($classifications{$short_name}) && ($classifications{$short_name}[1] >= $priority)) {
680 #Change the priority value to the stricter one.
681 $classifications{$short_name} = [ "$classifications{$short_name}[0]", "$priority" ];
682 } else {
683 # Add the classification to the hash.
684 $classifications{$short_name} = [ "$short_desc", "$priority" ];
685 }
686 }
687 }
688
689 # Close the file.
690 close(CLASSIFICATION);
691 }
692
693 # Open classification file for writing.
694 open(FILE, ">", "$classification_file") or die "Could not write to $classification_file. $!\n";
695
696 # Print notice about autogenerated file.
697 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n\n";
698
699 # Sort and loop through the hash of classifications.
700 foreach my $key (sort keys %classifications) {
701 # Assign some nice variable names for the items.
702 my $short_name = $key;
703 my $short_desc = $classifications{$key}[0];
704 my $priority = $classifications{$key}[1];
705
706 # Write the classification to the file.
707 print FILE "config classification: $short_name,$short_desc,$priority\n";
708 }
709
710 # Close file handle.
711 close(FILE);
712}
713
8bd74e12
SS
714#
715## Function to merge the "sid to message mapping" files of various given providers.
716#
717sub merge_sid_msg (@) {
718 my @providers = @_;
719
720 # Hash which contains all the sid to message mappings.
721 my %mappings = ();
722
723 # Loop through the array of given providers.
724 foreach my $provider (@providers) {
725 # Generate full path and filename.
6c9d3eee 726 my $sid_msg_file = "$tmp_conf_directory/$provider\-sid-msg.map";
8bd74e12
SS
727
728 # Skip provider if no sid to msg mapping file for this provider exists.
729 next unless (-f $sid_msg_file);
730
731 # Open the file.
732 open(MAPPING, $sid_msg_file) or die "Could not open $sid_msg_file. $!\n";
733
734 # Loop through the file content.
735 while (<MAPPING>) {
736 # Remove newlines.
737 chomp($_);
738
739 # Skip lines which do not start with a number,
740 next unless ($_ =~ /^\d+/);
741
742 # Split line content and assign it to an array.
743 my @line = split(/ \|\| /, $_);
744
745 # Grab the first element (and remove it) from the line array.
746 # It contains the sid.
747 my $sid = shift(@line);
748
749 # Store the grabbed sid and the remain array as hash value.
750 # It still contains the messages, references etc.
751 $mappings{$sid} = [@line];
752 }
753
754 # Close file handle.
755 close(MAPPING);
756 }
757
758 # Open mappings file for writing.
759 open(FILE, ">", $sid_msg_file) or die "Could not write $sid_msg_file. $!\n";
760
761 # Write notice about autogenerated file.
762 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n\n";
763
764 # Loop through the hash of mappings.
765 foreach my $sid ( sort keys %mappings) {
766 # Grab data for the sid.
767 my @data = @{$mappings{$sid}};
768
769 # Add the sid to the data array.
770 unshift(@data, $sid);
771
772 # Generate line.
773 my $line = join(" \|\| ", @data);
774
775 print FILE "$line\n";
776
777 }
778
779 # Close file handle.
780 close(FILE);
781}
782
50f348f6
SS
783#
784## A very tiny function to move an extracted ruleset from the temporary directory into
785## the rules directory.
786#
787sub move_tmp_ruleset() {
50f348f6
SS
788 # Do a directory listing of the temporary directory.
789 opendir DH, $tmp_rules_directory;
790
791 # Loop over all files.
792 while(my $file = readdir DH) {
793 # Move them to the rules directory.
794 move "$tmp_rules_directory/$file" , "$rulespath/$file";
795 }
796
797 # Close directory handle.
798 closedir DH;
799}
800
16b2d281
SS
801#
802## Function to cleanup the temporary IDS directroy.
803#
804sub cleanup_tmp_directory () {
16b2d281
SS
805
806 # Delete temporary directory and all containing files.
807 rmtree([ "$tmp_directory" ]);
808}
809
3983aebd
SS
810#
811## Function to do all the logging stuff if the downloading or updating of the ruleset fails.
812#
813sub log_error ($) {
814 my ($error) = @_;
815
816 # Remove any newline.
817 chomp($error);
818
eb5592c1
SS
819 # Call private function to log the error message to syslog.
820 &_log_to_syslog($error);
821
3983aebd
SS
822 # Call private function to write/store the error message in the storederrorfile.
823 &_store_error_message($error);
824}
825
eb5592c1
SS
826#
827## Function to log a given error message to the kernel syslog.
828#
829sub _log_to_syslog ($) {
830 my ($message) = @_;
831
eb5592c1
SS
832 # The syslog function works best with an array based input,
833 # so generate one before passing the message details to syslog.
834 my @syslog = ("ERR", "<ERROR> $message");
835
836 # Establish the connection to the syslog service.
837 openlog('oinkmaster', 'cons,pid', 'user');
838
839 # Send the log message.
840 syslog(@syslog);
841
842 # Close the log handle.
843 closelog();
844}
845
3983aebd
SS
846#
847## Private function to write a given error message to the storederror file.
848#
849sub _store_error_message ($) {
850 my ($message) = @_;
851
852 # Remove any newline.
853 chomp($message);
854
855 # Open file for writing.
856 open (ERRORFILE, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n";
857
858 # Write error to file.
859 print ERRORFILE "$message\n";
860
861 # Close file.
862 close (ERRORFILE);
3c59b1fa
SS
863
864 # Set correct ownership for the file.
865 &set_ownership("$storederrorfile");
3983aebd
SS
866}
867
788a71f5
SS
868#
869## Private function to get the path and filename for a downloaded ruleset by a given provider.
870#
871sub _get_dl_rulesfile($) {
872 my ($provider) = @_;
873
874 # Gather the download type for the given provider.
875 my $dl_type = $IDS::Ruleset::Providers{$provider}{'dl_type'};
876
877 # Obtain the file suffix for the download file type.
878 my $suffix = $dl_type_to_suffix{$dl_type};
879
880 # Check if a suffix has been found.
881 unless ($suffix) {
882 # Abort return - nothing.
883 return;
884 }
885
886 # Generate the full filename and path for the stored rules file.
887 my $rulesfile = "$dl_rules_path/$dl_rulesfile_prefix-$provider$suffix";
888
889 # Return the generated filename.
890 return $rulesfile;
891}
892
61b92664
SS
893#
894## Tiny function to delete the stored ruleset file or tarball for a given provider.
895#
896sub drop_dl_rulesfile ($) {
897 my ($provider) = @_;
898
899 # Gather the full path and name of the stored rulesfile.
900 my $rulesfile = &_get_dl_rulesfile($provider);
901
902 # Check if the given rulesfile exists.
903 if (-f $rulesfile) {
904 # Delete the stored rulesfile.
905 unlink($rulesfile) or die "Could not delete $rulesfile. $!\n";
906 }
907}
908
5b9d877d
SS
909#
910## Tiny function to get/generate the full path and filename for the providers oinkmaster
911## modified sids file.
912#
913sub get_oinkmaster_provider_modified_sids_file ($) {
914 my ($provider) = @_;
915
916 # Generate the filename.
917 my $filename = "$settingsdir/oinkmaster-$provider-modified-sids.conf";
918
919 # Return the filename.
920 return $filename;
921}
922
d878d9c0
SS
923#
924## Function to directly altering the oinkmaster provider includes file.
925##
926## Requires tha acition "remove" or "add" and a provider handle.
927#
928sub alter_oinkmaster_provider_includes_file ($$) {
929 my ($action, $provider) = @_;
930
931 # Call function to get the path and name for the given providers
932 # oinkmaster modified sids file.
933 my $provider_modified_sids_file = &get_oinkmaster_provider_modified_sids_file($provider);
934
935 # Open the file for reading..
936 open (FILE, $oinkmaster_provider_includes_file) or die "Could not read $oinkmaster_provider_includes_file. $!\n";
937
938 # Read-in file content.
939 my @lines = <FILE>;
940
941 # Close file after reading.
942 close(FILE);
943
944 # Re-open the file for writing.
945 open(FILE, ">", $oinkmaster_provider_includes_file) or die "Could not write to $oinkmaster_provider_includes_file. $!\n";
946
947 # Loop through the file content.
948 foreach my $line (@lines) {
949 # Remove newlines.
950 chomp($line);
951
952 # Skip line if we found our given provider and the action should be remove.
953 next if (($line =~ /$provider/) && ($action eq "remove"));
954
955 # Write the read-in line back to the file.
956 print FILE "$line\n";
957 }
958
1d860d89
SS
959 # Check if the file exists and add the provider if requested.
960 if ((-f $provider_modified_sids_file) && ($action eq "add")) {
d878d9c0
SS
961 print FILE "include $provider_modified_sids_file\n";
962 }
963
964 # Close file handle.
965 close(FILE);
966}
967
69b3156f
SS
968#
969## Function to read-in the given enabled or disables sids file.
970#
971sub read_enabled_disabled_sids_file($) {
972 my ($file) = @_;
973
974 # Temporary hash to store the sids and their state. It will be
975 # returned at the end of this function.
976 my %temphash;
977
978 # Open the given filename.
979 open(FILE, "$file") or die "Could not open $file. $!\n";
980
981 # Loop through the file.
982 while(<FILE>) {
983 # Remove newlines.
984 chomp $_;
985
986 # Skip blank lines.
987 next if ($_ =~ /^\s*$/);
988
989 # Skip coments.
990 next if ($_ =~ /^\#/);
991
992 # Splitt line into sid and state part.
993 my ($state, $sid) = split(" ", $_);
994
995 # Skip line if the sid is not numeric.
996 next unless ($sid =~ /\d+/ );
997
998 # Check if the sid was enabled.
999 if ($state eq "enablesid") {
1000 # Add the sid and its state as enabled to the temporary hash.
1001 $temphash{$sid} = "enabled";
1002 # Check if the sid was disabled.
1003 } elsif ($state eq "disablesid") {
1004 # Add the sid and its state as disabled to the temporary hash.
1005 $temphash{$sid} = "disabled";
1006 # Invalid state - skip the current sid and state.
1007 } else {
1008 next;
1009 }
1010 }
1011
1012 # Close filehandle.
1013 close(FILE);
1014
1015 # Return the hash.
1016 return %temphash;
1017}
1018
796eea21
SS
1019#
1020## Function to check if the IDS is running.
1021#
1022sub ids_is_running () {
1023 if(-f $idspidfile) {
1024 # Open PID file for reading.
1025 open(PIDFILE, "$idspidfile") or die "Could not open $idspidfile. $!\n";
1026
1027 # Grab the process-id.
1028 my $pid = <PIDFILE>;
1029
1030 # Close filehandle.
1031 close(PIDFILE);
1032
1033 # Remove any newline.
1034 chomp($pid);
1035
1036 # Check if a directory for the process-id exists in proc.
1037 if(-d "/proc/$pid") {
1038 # The IDS daemon is running return the process id.
1039 return $pid;
1040 }
1041 }
1042
1043 # Return nothing - IDS is not running.
1044 return;
1045}
1046
5240a809
SS
1047#
1048## Function to call suricatactrl binary with a given command.
1049#
1050sub call_suricatactrl ($) {
1051 # Get called option.
ed06bc81 1052 my ($option, $interval) = @_;
5240a809
SS
1053
1054 # Loop through the array of supported commands and check if
1055 # the given one is part of it.
1056 foreach my $cmd (@suricatactrl_cmds) {
1057 # Skip current command unless the given one has been found.
1058 next unless($cmd eq $option);
1059
ed06bc81
SS
1060 # Check if the given command is "cron".
1061 if ($option eq "cron") {
1062 # Check if an interval has been given.
1063 if ($interval) {
1064 # Check if the given interval is valid.
1065 foreach my $element (@cron_intervals) {
1066 # Skip current element until the given one has been found.
1067 next unless($element eq $interval);
1068
1069 # Call the suricatactrl binary and pass the "cron" command
1070 # with the requrested interval.
81631920 1071 &General::system("$suricatactrl", "$option", "$interval");
ed06bc81
SS
1072
1073 # Return "1" - True.
1074 return 1;
1075 }
1076 }
5240a809 1077
ed06bc81
SS
1078 # If we got here, the given interval is not supported or none has been given. - Return nothing.
1079 return;
1080 } else {
1081 # Call the suricatactrl binary and pass the requrested
1082 # option to it.
81631920 1083 &General::system("$suricatactrl", "$option");
ed06bc81
SS
1084
1085 # Return "1" - True.
1086 return 1;
1087 }
5240a809
SS
1088 }
1089
1090 # Command not found - return nothing.
1091 return;
1092}
1093
308ba5e7
SS
1094#
1095## Function to create a new empty file.
1096#
1097sub create_empty_file($) {
1098 my ($file) = @_;
1099
1100 # Check if the given file exists.
1101 if(-e $file) {
1102 # Do nothing to prevent from overwriting existing files.
1103 return;
1104 }
1105
1106 # Open the file for writing.
1107 open(FILE, ">$file") or die "Could not write to $file. $!\n";
1108
1109 # Close file handle.
1110 close(FILE);
1111
1112 # Return true.
1113 return 1;
1114}
1115
330759d8
SS
1116#
1117## Private function to check if the file permission of the rulespath are correct.
1118## If not, call suricatactrl to fix them.
1119#
1120sub _check_rulesdir_permissions() {
e568796b
SS
1121 # Check if the rulepath main directory is writable.
1122 unless (-W $rulespath) {
1123 # If not call suricatctrl to fix it.
1124 &call_suricatactrl("fix-rules-dir");
1125 }
1126
330759d8
SS
1127 # Open snort rules directory and do a directory listing.
1128 opendir(DIR, $rulespath) or die $!;
1129 # Loop through the direcory.
1130 while (my $file = readdir(DIR)) {
1131 # We only want files.
1132 next unless (-f "$rulespath/$file");
1133
1134 # Check if the file is writable by the user.
1135 if (-W "$rulespath/$file") {
1136 # Everything is okay - go on to the next file.
1137 next;
1138 } else {
1139 # There are wrong permissions, call suricatactrl to fix it.
1140 &call_suricatactrl("fix-rules-dir");
1141 }
1142 }
1143}
1144
b59cdbee
SS
1145#
1146## Private function to cleanup the directory which contains
1147## the IDS rules, before extracting and modifing the new ruleset.
1148#
1149sub _cleanup_rulesdir() {
8cf04a16
SS
1150 # Open rules directory and do a directory listing.
1151 opendir(DIR, $rulespath) or die $!;
1152
1153 # Loop through the direcory.
1154 while (my $file = readdir(DIR)) {
1155 # We only want files.
1156 next unless (-f "$rulespath/$file");
1157
fefb5173
SS
1158 # Skip rules file for whitelisted hosts.
1159 next if ("$rulespath/$file" eq $whitelist_file);
1160
612bb2df
SS
1161 # Skip rules file with local rules.
1162 next if ("$rulespath/$file" eq $local_rules_file);
1163
8cf04a16 1164 # Delete the current processed file, if not, exit this function
b59cdbee 1165 # and return an error message.
1201c1e7 1166 unlink("$rulespath/$file") or return "Could not delete $rulespath/$file. $!\n";
b59cdbee
SS
1167 }
1168
4ce42488 1169 # Return nothing;
b59cdbee
SS
1170 return;
1171}
1172
b02e30fd
SS
1173#
1174## Function to generate the file which contains the home net information.
1175#
1176sub generate_home_net_file() {
1177 my %netsettings;
1178
1179 # Read-in network settings.
1180 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
1181
1182 # Get available network zones.
abffcc99 1183 my @network_zones = &Network::get_available_network_zones();
b02e30fd
SS
1184
1185 # Temporary array to store network address and prefix of the configured
1186 # networks.
1187 my @networks;
1188
1189 # Loop through the array of available network zones.
1190 foreach my $zone (@network_zones) {
bcbc9897
SS
1191 # Check if the current processed zone is red.
1192 if($zone eq "red") {
1193 # Grab the IP-address of the red interface.
1194 my $red_address = &get_red_address();
b02e30fd 1195
bcbc9897
SS
1196 # Check if an address has been obtained.
1197 if ($red_address) {
1198 # Generate full network string.
1199 my $red_network = join("/", $red_address, "32");
b02e30fd 1200
bcbc9897
SS
1201 # Add the red network to the array of networks.
1202 push(@networks, $red_network);
1203 }
23c0347a 1204
23c0347a
SS
1205 # Check if the configured RED_TYPE is static.
1206 if ($netsettings{'RED_TYPE'} eq "STATIC") {
1207 # Get configured and enabled aliases.
1208 my @aliases = &get_aliases();
1209
1210 # Loop through the array.
1211 foreach my $alias (@aliases) {
1212 # Add "/32" prefix.
1213 my $network = join("/", $alias, "32");
1214
1215 # Add the generated network to the array of networks.
1216 push(@networks, $network);
1217 }
1218 }
bcbc9897
SS
1219 # Process remaining network zones.
1220 } else {
1221 # Convert current zone name into upper case.
1222 $zone = uc($zone);
1223
1224 # Generate key to access the required data from the netsettings hash.
1225 my $zone_netaddress = $zone . "_NETADDRESS";
1226 my $zone_netmask = $zone . "_NETMASK";
1227
1228 # Obtain the settings from the netsettings hash.
1229 my $netaddress = $netsettings{$zone_netaddress};
1230 my $netmask = $netsettings{$zone_netmask};
1231
1232 # Convert the subnetmask into prefix notation.
1233 my $prefix = &Network::convert_netmask2prefix($netmask);
1234
1235 # Generate full network string.
1236 my $network = join("/", $netaddress,$prefix);
1237
1238 # Check if the network is valid.
1239 if(&Network::check_subnet($network)) {
1240 # Add the generated network to the array of networks.
1241 push(@networks, $network);
1242 }
23c0347a 1243 }
b02e30fd
SS
1244 }
1245
1246 # Format home net declaration.
7479c993 1247 my $line = "\"[" . join(',', @networks) . "]\"";
b02e30fd
SS
1248
1249 # Open file to store the addresses of the home net.
1250 open(FILE, ">$homenet_file") or die "Could not open $homenet_file. $!\n";
1251
1252 # Print yaml header.
1253 print FILE "%YAML 1.1\n";
1254 print FILE "---\n\n";
1255
1256 # Print notice about autogenerated file.
1257 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1258
1259 # Print the generated and required HOME_NET declaration to the file.
1260 print FILE "HOME_NET:\t$line\n";
1261
1262 # Close file handle.
1263 close(FILE);
1264}
1265
30ee98e9
SS
1266#
1267# Function to generate and write the file which contains the configured and used DNS servers.
1268#
1269sub generate_dns_servers_file() {
12c49915
SS
1270 # Get the used DNS servers.
1271 my @nameservers = &General::get_nameservers();
30ee98e9 1272
7b97359b
SS
1273 # Get network settings.
1274 my %netsettings;
1275 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
1276
30ee98e9 1277 # Format dns servers declaration.
b76118c3 1278 my $line = "";
30ee98e9 1279
12c49915
SS
1280 # Check if the system has configured nameservers.
1281 if (@nameservers) {
7b97359b
SS
1282 # Add the GREEN address as DNS servers.
1283 push(@nameservers, $netsettings{'GREEN_ADDRESS'});
1284
1285 # Check if a BLUE zone exists.
1286 if ($netsettings{'BLUE_ADDRESS'}) {
1287 # Add the BLUE address to the array of nameservers.
1288 push(@nameservers, $netsettings{'BLUE_ADDRESS'});
1289 }
1290
1291 # Generate the line which will be written to the DNS servers file.
b76118c3 1292 $line = join(",", @nameservers);
12c49915 1293 } else {
7b97359b 1294 # External net simply contains (any).
99cadb74 1295 $line = "\$EXTERNAL_NET";
fd2dccaa 1296 }
30ee98e9 1297
30ee98e9
SS
1298 # Open file to store the used DNS server addresses.
1299 open(FILE, ">$dns_servers_file") or die "Could not open $dns_servers_file. $!\n";
1300
1301 # Print yaml header.
1302 print FILE "%YAML 1.1\n";
1303 print FILE "---\n\n";
1304
1305 # Print notice about autogenerated file.
1306 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1307
1308 # Print the generated DNS declaration to the file.
b76118c3 1309 print FILE "DNS_SERVERS:\t\"[$line]\"\n";
30ee98e9
SS
1310
1311 # Close file handle.
1312 close(FILE);
1313}
1314
e698090e
SS
1315#
1316# Function to generate and write the file which contains the HTTP_PORTS definition.
1317#
1318sub generate_http_ports_file() {
1319 my %proxysettings;
1320
1321 # Read-in proxy settings
1322 &General::readhash("${General::swroot}/proxy/advanced/settings", \%proxysettings);
1323
1324 # Check if the proxy is enabled.
1325 if (( -e "${General::swroot}/proxy/enable") || (-e "${General::swroot}/proxy/enable_blue")) {
1326 # Add the proxy port to the array of HTTP ports.
1327 push(@http_ports, $proxysettings{'PROXY_PORT'});
1328 }
1329
1330 # Check if the transparent mode of the proxy is enabled.
1331 if ((-e "${General::swroot}/proxy/transparent") || (-e "${General::swroot}/proxy/transparent_blue")) {
1332 # Add the transparent proxy port to the array of HTTP ports.
1333 push(@http_ports, $proxysettings{'TRANSPARENT_PORT'});
1334 }
1335
1336 # Format HTTP_PORTS declaration.
1337 my $line = "";
1338
1339 # Generate line which will be written to the http ports file.
1340 $line = join(",", @http_ports);
1341
1342 # Open file to store the HTTP_PORTS.
1343 open(FILE, ">$http_ports_file") or die "Could not open $http_ports_file. $!\n";
1344
1345 # Print yaml header.
1346 print FILE "%YAML 1.1\n";
1347 print FILE "---\n\n";
1348
1349 # Print notice about autogenerated file.
1350 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1351
1352 # Print the generated HTTP_PORTS declaration to the file.
1353 print FILE "HTTP_PORTS:\t\"[$line]\"\n";
1354
1355 # Close file handle.
1356 close(FILE);
1357}
1358
b02e30fd 1359#
ddaf8ae1
SS
1360## Function to generate and write the file for used rulefiles file for a given provider.
1361##
1362## The function requires as first argument a provider handle, and as second an array with files.
b02e30fd 1363#
ddaf8ae1
SS
1364sub write_used_provider_rulefiles_file($@) {
1365 my ($provider, @files) = @_;
1366
1367 # Get the path and file for the provider specific used rulefiles file.
1368 my $used_provider_rulesfile_file = &get_used_provider_rulesfile_file($provider);
b02e30fd
SS
1369
1370 # Open file for used rulefiles.
a081f203 1371 open (FILE, ">", "$used_provider_rulesfile_file") or die "Could not write to $used_provider_rulesfile_file. $!\n";
b02e30fd
SS
1372
1373 # Write yaml header to the file.
1374 print FILE "%YAML 1.1\n";
1375 print FILE "---\n\n";
1376
1377 # Write header to file.
1378 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1379
b02e30fd
SS
1380 # Loop through the array of given files.
1381 foreach my $file (@files) {
1382 # Check if the given filename exists and write it to the file of used rulefiles.
1383 if(-f "$rulespath/$file") {
1384 print FILE " - $file\n";
1385 }
1386 }
1387
1388 # Close file after writing.
1389 close(FILE);
1390}
1391
ddaf8ae1
SS
1392#
1393## Function to write the main file for provider rulesfiles inclusions.
1394##
1395## This function requires an array of provider handles.
1396#
1397sub write_main_used_rulefiles_file (@) {
1398 my (@providers) = @_;
1399
88eb5626 1400 # Call function to write the static rulefiles file.
43d12991 1401 &_write_default_rulefiles_file();
88eb5626 1402
ddaf8ae1
SS
1403 # Open file for used rulefils inclusion.
1404 open (FILE, ">", "$suricata_used_providers_file") or die "Could not write to $suricata_used_providers_file. $!\n";
1405
1406 # Write yaml header to the file.
1407 print FILE "%YAML 1.1\n";
1408 print FILE "---\n\n";
1409
1410 # Write header to file.
1411 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1412
1413 # Loop through the list of given providers.
1414 foreach my $provider (@providers) {
1415 # Call function to get the providers used rulefiles file.
1416 my $filename = &get_used_provider_rulesfile_file($provider);
1417
1b0e555f
SS
1418 # Check if the file exists and write it into the used rulefiles file.
1419 if (-f $filename) {
1420 # Print the provider to the file.
1421 print FILE "include\: $filename\n";
1422 }
ddaf8ae1
SS
1423 }
1424
ddaf8ae1
SS
1425 # Close the filehandle after writing.
1426 close(FILE);
1427}
1428
43d12991
SS
1429sub _write_default_rulefiles_file () {
1430 # Get enabled application layer protocols.
1431 my @enabled_app_layer_protos = &get_suricata_enabled_app_layer_protos();
1432
88eb5626 1433 # Open file.
43d12991 1434 open (FILE, ">", $suricata_default_rulefiles_file) or die "Could not write to $suricata_default_rulefiles_file. $!\n";
88eb5626
SS
1435
1436 # Write yaml header to the file.
1437 print FILE "%YAML 1.1\n";
1438 print FILE "---\n\n";
1439
1440 # Write notice about autogenerated file.
1441 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1442
1443 # Loop through the array of static included rulesfiles.
1444 foreach my $file (@static_included_rulefiles) {
1445 # Check if the file exists.
1446 if (-f "$rulespath/$file") {
1447 # Write the rulesfile name to the file.
43d12991
SS
1448 print FILE " - $rulespath/$file\n";
1449 }
1450 }
1451
1452 print FILE "\n#Default rules for used application layer protocols.\n";
1453 foreach my $enabled_app_layer_proto (@enabled_app_layer_protos) {
1454 # Check if the current processed app layer proto needs to be translated
1455 # into an application name.
1456 if (exists($tr_app_layer_proto{$enabled_app_layer_proto})) {
43d12991
SS
1457 # Obtain the translated application name for this protocol.
1458 $enabled_app_layer_proto = $tr_app_layer_proto{$enabled_app_layer_proto};
1459 }
1460
1461 # Generate filename.
1462 my $rulesfile = "$default_rulespath/$enabled_app_layer_proto\.rules";
1463
1464 # Check if such a file exists.
1465 if (-f "$rulesfile") {
1466 # Write the rulesfile name to the file.
1467 print FILE " - $rulesfile\n";
1468 }
1469
1470 # Generate filename with "events" in filename.
1471 $rulesfile = "$default_rulespath/$enabled_app_layer_proto\-events.rules";
1472
1473 # Check if this file exists.
1474 if (-f "$rulesfile" ) {
1475 # Write the rulesfile name to the file.
1476 print FILE " - $rulesfile\n";
88eb5626
SS
1477 }
1478 }
1479
1480 # Close the file handle
1481 close(FILE);
1482}
1483
ddaf8ae1
SS
1484#
1485## Tiny function to generate the full path and name for the used_provider_rulesfile file of a given provider.
1486#
1487sub get_used_provider_rulesfile_file ($) {
1488 my ($provider) = @_;
1489
1490 my $filename = "$settingsdir/suricata\-$provider\-used\-rulefiles.yaml";
1491
1492 # Return the gernerated file.
1493 return $filename;
1494}
1495
74cc8f5a
SS
1496#
1497## Function to generate and write the file for modify the ruleset.
1498#
81bae51f
SS
1499sub write_modify_sids_file() {
1500 # Get configured settings.
1501 my %idssettings=();
81bae51f 1502 &General::readhash("$ids_settings_file", \%idssettings);
74cc8f5a
SS
1503
1504 # Open modify sid's file for writing.
2ee51088 1505 open(FILE, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
74cc8f5a
SS
1506
1507 # Write file header.
1508 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1509
1510 # Check if the traffic only should be monitored.
81bae51f 1511 unless($idssettings{'MONITOR_TRAFFIC_ONLY'} eq 'on') {
a5ba473c
TF
1512 # Suricata is in IPS mode, which means that the rule actions have to be changed
1513 # from 'alert' to 'drop', however not all rules should be changed. Some rules
1514 # exist purely to set a flowbit which is used to convey other information, such
1515 # as a specific type of file being downloaded, to other rulewhich then check for
1516 # malware in that file. Rules which fall into the first category should stay as
1517 # alert since not all flows of that type contain malware.
1518
aac869c4
SS
1519 # These types of rulesfiles contain meta-data which gives the action that should
1520 # be used when in IPS mode. Do the following:
1521 #
1522 # 1. Disable all rules and set the action to 'drop'
1523 # 2. Set the action back to 'alert' if the rule contains 'flowbits:noalert;'
1524 # This should give rules not in the policy a reasonable default if the user
1525 # manually enables them.
1526 # 3. Enable rules and set actions according to the meta-data strings.
a5ba473c 1527
aac869c4 1528 my $policy = 'balanced'; # Placeholder to allow policy to be changed.
a5ba473c
TF
1529
1530 print FILE <<END;
a5ba473c
TF
1531modifysid * "^#(?:alert|drop)(.+policy $policy-ips alert)" | "alert\${1}"
1532modifysid * "^#(?:alert|drop)(.+policy $policy-ips drop)" | "drop\${1}"
a5ba473c
TF
1533modifysid * "^(#?)(?:alert|drop)" | "\${1}drop"
1534modifysid * "^(#?)drop(.+flowbits:noalert;)" | "\${1}alert\${2}"
1535END
1536 }
74cc8f5a
SS
1537
1538 # Close file handle.
1539 close(FILE);
1540}
1541
235e3e92
SS
1542#
1543## Function to get the subscription code of a configured provider.
1544#
1545sub get_subscription_code($) {
1546 my ($provider) = @_;
1547
1548 my %configured_providers = ();
1549
1550 # Read-in providers settings file.
1551 &General::readhasharray($providers_settings_file, \%configured_providers);
1552
1553 # Loop through the hash of configured providers.
1554 foreach my $id (keys %configured_providers) {
1555 # Assign nice human-readable values to the data fields.
1556 my $provider_handle = $configured_providers{$id}[0];
1557 my $subscription_code = $configured_providers{$id}[1];
1558
1559 # Check if the current processed provider is the requested one.
1560 if ($provider_handle eq $provider) {
1561 # Return the obtained subscription code.
1562 return $subscription_code;
1563 }
1564 }
1565
1566 # No subscription code found - return nothing.
1567 return;
1568}
1569
f580aa8c
SS
1570#
1571## Function to get the ruleset date for a given provider.
1572##
1573## The function simply return the creation date in a human read-able format
1574## of the stored providers rulesfile.
1575#
1576sub get_ruleset_date($) {
1577 my ($provider) = @_;
fc685a36 1578 my $date;
c68bcbb2 1579 my $mtime;
f580aa8c 1580
f580aa8c
SS
1581 # Get the stored rulesfile for this provider.
1582 my $stored_rulesfile = &_get_dl_rulesfile($provider);
1583
c68bcbb2 1584 # Check if we got a file.
cad087c7 1585 if (-f $stored_rulesfile) {
c68bcbb2
SS
1586 # Call stat on the rulestarball.
1587 my $stat = stat("$stored_rulesfile");
f580aa8c 1588
c68bcbb2
SS
1589 # Get timestamp the file creation.
1590 $mtime = $stat->mtime;
1591 }
f580aa8c 1592
057e8953
SS
1593 # Check if the timestamp has not been grabbed.
1594 unless ($mtime) {
1595 # Return N/A for Not available.
1596 return "N/A";
1597 }
1598
f580aa8c 1599 # Convert into human read-able format.
fc685a36
SS
1600 $date = strftime('%Y-%m-%d %H:%M:%S', localtime($mtime));
1601
f580aa8c
SS
1602 # Return the date.
1603 return $date;
1604}
1605
04a0d07c
SS
1606#
1607## Function to gather the version of suricata.
1608#
1609sub get_suricata_version($) {
1610 my ($format) = @_;
1611
1612 # Execute piped suricata command and return the version information.
1613 open(SURICATA, "suricata -V |") or die "Couldn't execute program: $!";
1614
1615 # Grab and store the output of the piped program.
1616 my $version_string = <SURICATA>;
1617
1618 # Close pipe.
1619 close(SURICATA);
1620
1621 # Remove newlines.
1622 chomp($version_string);
1623
fd2dccaa 1624 # Grab the version from the version string.
04a0d07c
SS
1625 $version_string =~ /([0-9]+([.][0-9]+)+)/;
1626
1627 # Splitt the version into single chunks.
1628 my ($major_ver, $minor_ver, $patchlevel) = split(/\./, $1);
1629
1630 # Check and return the requested version sheme.
1631 if ($format eq "major") {
1632 # Return the full version.
1633 return "$major_ver";
1634 } elsif ($format eq "minor") {
1635 # Return the major and minor part.
1636 return "$major_ver.$minor_ver";
1637 } else {
1638 # Return the full version string.
1639 return "$major_ver.$minor_ver.$patchlevel";
fd2dccaa 1640 }
04a0d07c
SS
1641}
1642
bb39fac4
SS
1643#
1644## Function to get the enabled application layer protocols.
1645#
1646sub get_suricata_enabled_app_layer_protos() {
1647 # Array to store and return the enabled app layer protos.
1648 my @enabled_app_layer_protos = ();
1649
1650 # Execute piped suricata command and return the list of
1651 # enabled application layer protocols.
1652 open(SURICATA, "suricata --list-app-layer-protos |") or die "Could not execute program: $!";
1653
1654 # Grab and store the list of enabled application layer protocols.
1655 my @output = <SURICATA>;
1656
1657 # Close pipe.
1658 close(SURICATA);
1659
1660 # Merge allways enabled static application layers protocols array.
1661 @enabled_app_layer_protos = @static_enabled_app_layer_protos;
1662
1663 # Loop through the array which contains the output of suricata.
1664 foreach my $line (@output) {
1665 # Skip header line which starts with "===".
1666 next if ($line =~ /^\s*=/);
1667
1668 # Skip info or warning lines.
1669 next if ($line =~ /\s*--/);
1670
1671 # Remove newlines.
1672 chomp($line);
1673
1674 # Add enabled app layer proto to the array.
1675 push(@enabled_app_layer_protos, $line);
1676 }
1677
1678 # Sort the array.
1679 @enabled_app_layer_protos = sort(@enabled_app_layer_protos);
1680
1681 # Return the array.
1682 return @enabled_app_layer_protos;
1683}
1684
9283e9b9
SS
1685#
1686## Function to generate the rules file with whitelisted addresses.
1687#
1688sub generate_ignore_file() {
1689 my %ignored = ();
1690
1691 # SID range 1000000-1999999 Reserved for Local Use
1692 # Put your custom rules in this range to avoid conflicts
1693 my $sid = 1500000;
1694
1695 # Read-in ignoredfile.
1696 &General::readhasharray($IDS::ignored_file, \%ignored);
1697
1698 # Open ignorefile for writing.
1699 open(FILE, ">$IDS::whitelist_file") or die "Could not write to $IDS::whitelist_file. $!\n";
1700
1701 # Config file header.
1702 print FILE "# Autogenerated file.\n";
1703 print FILE "# All user modifications will be overwritten.\n\n";
1704
1705 # Add all user defined addresses to the whitelist.
1706 #
1707 # Check if the hash contains any elements.
1708 if (keys (%ignored)) {
1709 # Loop through the entire hash and write the host/network
1710 # and remark to the ignore file.
1711 while ( (my $key) = each %ignored) {
1712 my $address = $ignored{$key}[0];
1713 my $remark = $ignored{$key}[1];
1714 my $status = $ignored{$key}[2];
1715
1716 # Check if the status of the entry is "enabled".
1717 if ($status eq "enabled") {
1718 # Check if the address/network is valid.
1719 if ((&General::validip($address)) || (&General::validipandmask($address))) {
1720 # Write rule line to the file to pass any traffic from this IP
c1ccae1c 1721 print FILE "pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; bypass; sid:$sid\;)\n";
9283e9b9
SS
1722
1723 # Increment sid.
1724 $sid++;
1725 }
1726 }
1727 }
1728 }
1729
1730 close(FILE);
1731}
1732
1fedede6
SS
1733#
1734## Function to set correct ownership for single files and directories.
1735#
1736
1737sub set_ownership($) {
1738 my ($target) = @_;
1739
1740 # User and group of the WUI.
1741 my $uname = "nobody";
1742 my $grname = "nobody";
1743
1744 # The chown function implemented in perl requies the user and group as nummeric id's.
1745 my $uid = getpwnam($uname);
1746 my $gid = getgrnam($grname);
1747
1748 # Check if the given target exists.
1749 unless ($target) {
1750 # Stop the script and print error message.
1751 die "The $target does not exist. Cannot change the ownership!\n";
1752 }
1753
1754 # Check weather the target is a file or directory.
1755 if (-f $target) {
1756 # Change ownership ot the single file.
1757 chown($uid, $gid, "$target");
1758 } elsif (-d $target) {
1759 # Do a directory listing.
1760 opendir(DIR, $target) or die $!;
1761 # Loop through the direcory.
1762 while (my $file = readdir(DIR)) {
1763
1764 # We only want files.
1765 next unless (-f "$target/$file");
1766
1767 # Set correct ownership for the files.
1768 chown($uid, $gid, "$target/$file");
1769 }
1770
1771 closedir(DIR);
1772
1773 # Change ownership of the directory.
1774 chown($uid, $gid, "$target");
1775 }
1776}
77c31301
SS
1777
1778#
1779## Function to read-in the aliases file and returns all configured and enabled aliases.
1780#
1781sub get_aliases() {
1782 # Location of the aliases file.
1783 my $aliases_file = "${General::swroot}/ethernet/aliases";
1784
1785 # Array to store the aliases.
1786 my @aliases;
1787
1788 # Check if the file is empty.
1789 if (-z $aliases_file) {
1790 # Abort nothing to do.
1791 return;
1792 }
1793
1794 # Open the aliases file.
1795 open(ALIASES, $aliases_file) or die "Could not open $aliases_file. $!\n";
1796
1797 # Loop through the file content.
1798 while (my $line = <ALIASES>) {
1799 # Remove newlines.
1800 chomp($line);
1801
1802 # Splitt line content into single chunks.
1803 my ($address, $state, $remark) = split(/\,/, $line);
1804
1805 # Check if the state of the current processed alias is "on".
1806 if ($state eq "on") {
1807 # Check if the address is valid.
1808 if(&Network::check_ip_address($address)) {
1809 # Add the alias to the array of aliases.
1810 push(@aliases, $address);
1811 }
1812 }
1813 }
1814
1815 # Close file handle.
1816 close(ALIASES);
1817
1818 # Return the array.
1819 return @aliases;
1820}
1821
de8e1e5b
SS
1822#
1823## Function to grab the current assigned IP-address on red.
1824#
1825sub get_red_address() {
1826 # File, which contains the current IP-address of the red interface.
1827 my $file = "${General::swroot}/red/local-ipaddress";
1828
1829 # Check if the file exists.
1830 if (-e $file) {
1831 # Open the given file.
1832 open(FILE, "$file") or die "Could not open $file.";
1833
1834 # Obtain the address from the first line of the file.
1835 my $address = <FILE>;
1836
1837 # Close filehandle
1838 close(FILE);
1839
1840 # Remove newlines.
1841 chomp $address;
1842
1843 # Check if the grabbed address is valid.
1844 if (&General::validip($address)) {
1845 # Return the address.
1846 return $address;
1847 }
1848 }
1849
1850 # Return nothing.
1851 return;
1852}
77c31301 1853
6563d449 1854#
ddaf8ae1 1855## Function to get the used rules files of a given provider.
6563d449 1856#
ddaf8ae1
SS
1857sub read_used_provider_rulesfiles($) {
1858 my ($provider) = @_;
1859
6563d449
SS
1860 # Array to store the used rulefiles.
1861 my @used_rulesfiles = ();
1862
ddaf8ae1
SS
1863 # Get the used rulesefile file for the provider.
1864 my $rulesfile_file = &get_used_provider_rulesfile_file($provider);
1865
01fc880c
SS
1866 # Check if the a used rulesfile exists for this provider.
1867 if (-f $rulesfile_file) {
6563d449 1868 # Open the file or used rulefiles and read-in content.
ddaf8ae1 1869 open(FILE, $rulesfile_file) or die "Could not open $rulesfile_file. $!\n";
6563d449
SS
1870
1871 while (<FILE>) {
1872 # Assign the current line to a nice variable.
1873 my $line = $_;
1874
1875 # Remove newlines.
1876 chomp($line);
1877
1878 # Skip comments.
1879 next if ($line =~ /\#/);
1880
1881 # Skip blank lines.
1882 next if ($line =~ /^\s*$/);
1883
1884 # Gather the rulefile.
1885 if ($line =~ /.*- (.*)/) {
1886 my $rulefile = $1;
1887
1888 # Add the rulefile to the array of used rulesfiles.
1889 push(@used_rulesfiles, $rulefile);
1890 }
1891 }
1892
1893 # Close the file.
1894 close(FILE);
1895 }
1896
1897 # Return the array of used rulesfiles.
1898 return @used_rulesfiles;
1899}
1900
8076deba
SS
1901#
1902## Function to write the lock file for locking the WUI, while
1903## the autoupdate script runs.
1904#
1905sub lock_ids_page() {
1906 # Call subfunction to create the file.
1907 &create_empty_file($ids_page_lock_file);
1908}
1909
1910#
1911## Function to release the lock of the WUI, again.
1912#
1913sub unlock_ids_page() {
1914 # Delete lock file.
1915 unlink($ids_page_lock_file);
1916}
1917
8dcebe53 19181;