]> git.ipfire.org Git - ipfire-2.x.git/blame - config/cfgroot/ids-functions.pl
ids-functions.pl: Introduce function write_modify_sids_file()
[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# #
20# Copyright (C) 2018 IPFire Team <info@ipfire.org>. #
21# #
22############################################################################
23
24package IDS;
25
26require '/var/ipfire/general-functions.pl';
8dcebe53 27
02844177 28# Location where all config and settings files are stored.
164eab66 29our $settingsdir = "${General::swroot}/suricata";
02844177 30
b02e30fd
SS
31# File where the used rulefiles are stored.
32our $used_rulefiles_file = "$settingsdir/suricata-used-rulefiles.yaml";
33
34# File where the addresses of the homenet are stored.
35our $homenet_file = "$settingsdir/suricata-homenet.yaml";
36
37# File which contains the enabled sids.
38our $enabled_sids_file = "$settingsdir/oinkmaster-enabled-sids.conf";
39
40# File which contains the disabled sids.
41our $disabled_sids_file = "$settingsdir/oinkmaster-disabled-sids.conf";
42
43# File which contains wheater the rules should be changed.
44our $modify_sids_file = "$settingsdir/oinkmaster-modify-sids.conf";
45
46# File which stores the configured IPS settings.
47our $ids_settings_file = "$settingsdir/settings";
48
49# File which stores the configured rules-settings.
50our $rules_settings_file = "$settingsdir/rules-settings";
51
52# File which stores the configured settings for whitelisted addresses.
53our $ignored_file = "$settingsdir/ignored";
54
eea2670b 55# Location and name of the tarball which contains the ruleset.
164eab66 56our $rulestarball = "/var/tmp/idsrules.tar.gz";
eea2670b 57
3983aebd 58# File to store any errors, which also will be read and displayed by the wui.
77910792 59our $storederrorfile = "/tmp/ids_storederror";
3983aebd 60
298ef5ba 61# Location where the rulefiles are stored.
21cab141 62our $rulespath = "/var/lib/suricata";
298ef5ba 63
b02e30fd
SS
64# File which contains the rules to whitelist addresses on suricata.
65our $whitelist_file = "$rulespath/whitelist.rules";
66
bce84f39
SS
67# File which contains a list of all supported ruleset sources.
68# (Sourcefire, Emergingthreads, etc..)
69our $rulesetsourcesfile = "$settingsdir/ruleset-sources";
70
796eea21
SS
71# The pidfile of the IDS.
72our $idspidfile = "/var/run/suricata.pid";
73
5240a809
SS
74# Location of suricatactrl.
75my $suricatactrl = "/usr/local/bin/suricatactrl";
76
77# Array with allowed commands of suricatactrl.
ed06bc81
SS
78my @suricatactrl_cmds = ( 'start', 'stop', 'restart', 'reload', 'fix-rules-dir', 'cron' );
79
80# Array with supported cron intervals.
81my @cron_intervals = ('off', 'daily', 'weekly' );
5240a809 82
b02e30fd
SS
83#
84## Function to check and create all IDS related files, if the does not exist.
85#
86sub 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); }
96}
97
8dcebe53
SS
98#
99## Function for checking if at least 300MB of free disk space are available
100## on the "/var" partition.
101#
102sub checkdiskspace () {
103 # Call diskfree to gather the free disk space of /var.
104 my @df = `/bin/df -B M /var`;
105
106 # Loop through the output.
107 foreach my $line (@df) {
108 # Ignore header line.
109 next if $line =~ m/^Filesystem/;
110
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;
116
117 # Check if the available disk space is more than 300MB.
118 if ($available < 300) {
434001d0
SS
119 # Log error to syslog.
120 &_log_to_syslog("Not enough free disk space on /var. Only $available MB from 300 MB available.");
8dcebe53 121
434001d0
SS
122 # Exit function and return "1" - False.
123 return 1;
8dcebe53
SS
124 }
125 }
126 }
127
128 # Everything okay, return nothing.
129 return;
130}
131
eea2670b
SS
132#
133## This function is responsible for downloading the configured snort ruleset.
134##
135## * At first it obtains from the stored snortsettings 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.
140#
141sub downloadruleset {
142 # Get snort settings.
143 my %snortsettings=();
02844177 144 &General::readhash("$settingsdir/settings", \%snortsettings);
eea2670b 145
be52c68a
SS
146 # Check if a ruleset has been configured.
147 unless($snortsettings{'RULES'}) {
148 # Log that no ruleset has been configured and abort.
149 &_log_to_syslog("No ruleset source has been configured.");
150
151 # Return "1".
152 return 1;
153 }
154
eea2670b
SS
155 # Get all available ruleset locations.
156 my %rulesetsources=();
bce84f39 157 &General::readhash($rulesetsourcesfile, \%rulesetsources);
eea2670b
SS
158
159 # Read proxysettings.
160 my %proxysettings=();
161 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
162
163 # Load required perl module to handle the download.
164 use LWP::UserAgent;
165
166 # Init the download module.
167 my $downloader = LWP::UserAgent->new;
168
169 # Set timeout to 10 seconds.
170 $downloader->timeout(10);
171
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}))?(?:\/.*?)?$/);
175 my $proxy_url;
176
177 # Check if we got a peer.
178 if ($peer) {
179 $proxy_url = "http://";
180
181 # Check if the proxy requires authentication.
182 if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) {
183 $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@";
184 }
185
186 # Add proxy server address and port.
187 $proxy_url .= "$peer\:$peerport";
188 } else {
434001d0
SS
189 # Log error message and break.
190 &_log_to_syslog("Could not proper configure the proxy server access.");
191
192 # Return "1" - false.
193 return 1;
eea2670b
SS
194 }
195
196 # Setup proxy settings.
197 $downloader->proxy('http', $proxy_url);
198 }
199
200 # Grab the right url based on the configured vendor.
201 my $url = $rulesetsources{$snortsettings{'RULES'}};
202
203 # Check if the vendor requires an oinkcode and add it if needed.
204 $url =~ s/\<oinkcode\>/$snortsettings{'OINKCODE'}/g;
205
206 # Abort if no url could be determined for the vendor.
207 unless ($url) {
434001d0
SS
208 # Log error and abort.
209 &_log_to_syslog("Unable to gather a download URL for the selected ruleset.");
210 return 1;
eea2670b
SS
211 }
212
96da5803
SS
213 # Pass the requrested url to the downloader.
214 my $request = HTTP::Request->new(HEAD => $url);
215
216 # Accept the html header.
217 $request->header('Accept' => 'text/html');
218
219 # Perform the request and fetch the html header.
220 my $response = $downloader->request($request);
221
222 # Check if there was any error.
223 unless ($response->is_success) {
224 # Obtain error.
225 my $error = $response->content;
226
227 # Log error message.
228 &_log_to_syslog("Unable to download the ruleset. \($error\)");
229
230 # Return "1" - false.
231 return 1;
232 }
233
234 # Assign the fetched header object.
235 my $header = $response->headers;
236
237 # Grab the remote file size from the object and store it in the
238 # variable.
239 my $remote_filesize = $header->content_length;
240
25b6545a
SS
241 # Load perl module to deal with temporary files.
242 use File::Temp;
243
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();
247
eea2670b
SS
248 # Pass the requested url to the downloader.
249 my $request = HTTP::Request->new(GET => $url);
250
25b6545a
SS
251 # Perform the request and save the output into the tmpfile.
252 my $response = $downloader->request($request, $tmpfile);
eea2670b
SS
253
254 # Check if there was any error.
255 unless ($response->is_success) {
88daf7eb
SS
256 # Obtain error.
257 my $error = $response->content;
258
434001d0 259 # Log error message.
88daf7eb 260 &_log_to_syslog("Unable to download the ruleset. \($error\)");
434001d0
SS
261
262 # Return "1" - false.
263 return 1;
eea2670b
SS
264 }
265
96da5803
SS
266 # Load perl stat module.
267 use File::stat;
268
25b6545a
SS
269 # Perform stat on the tmpfile.
270 my $stat = stat($tmpfile);
96da5803
SS
271
272 # Grab the local filesize of the downloaded tarball.
273 my $local_filesize = $stat->size;
274
275 # Check if both file sizes match.
276 unless ($remote_filesize eq $local_filesize) {
277 # Log error message.
278 &_log_to_syslog("Unable to completely download the ruleset. ");
279 &_log_to_syslog("Only got $local_filesize Bytes instead of $remote_filesize Bytes. ");
280
25b6545a
SS
281 # Delete temporary file.
282 unlink("$tmpfile");
283
96da5803
SS
284 # Return "1" - false.
285 return 1;
286 }
287
25b6545a
SS
288 # Load file copy module, which contains the move() function.
289 use File::Copy;
290
291 # Overwrite existing rules tarball with the new downloaded one.
292 move("$tmpfile", "$rulestarball");
293
eea2670b
SS
294 # If we got here, everything worked fine. Return nothing.
295 return;
296}
8dcebe53 297
25f5cb0d
SS
298#
299## A tiny wrapper function to call the oinkmaster script.
300#
301sub oinkmaster () {
330759d8
SS
302 # Check if the files in rulesdir have the correct permissions.
303 &_check_rulesdir_permissions();
304
883820bd
SS
305 # Cleanup the rules directory before filling it with the new rulest.
306 &_cleanup_rulesdir();
307
0e40e1e7
SS
308 # Load perl module to talk to the kernel syslog.
309 use Sys::Syslog qw(:DEFAULT setlogsock);
310
311 # Establish the connection to the syslog service.
312 openlog('oinkmaster', 'cons,pid', 'user');
313
25f5cb0d 314 # Call oinkmaster to generate ruleset.
d9711d91 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";
0e40e1e7
SS
316
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", "$_");
322
323 # Send the log message.
324 syslog(@syslog);
325 }
326
327 # Close the pipe to oinkmaster process.
328 close(OINKMASTER);
329
330 # Close the log handle.
331 closelog();
25f5cb0d
SS
332}
333
3983aebd
SS
334#
335## Function to do all the logging stuff if the downloading or updating of the ruleset fails.
336#
337sub log_error ($) {
338 my ($error) = @_;
339
340 # Remove any newline.
341 chomp($error);
342
eb5592c1
SS
343 # Call private function to log the error message to syslog.
344 &_log_to_syslog($error);
345
3983aebd
SS
346 # Call private function to write/store the error message in the storederrorfile.
347 &_store_error_message($error);
348}
349
eb5592c1
SS
350#
351## Function to log a given error message to the kernel syslog.
352#
353sub _log_to_syslog ($) {
354 my ($message) = @_;
355
356 # Load perl module to talk to the kernel syslog.
357 use Sys::Syslog qw(:DEFAULT setlogsock);
358
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");
362
363 # Establish the connection to the syslog service.
364 openlog('oinkmaster', 'cons,pid', 'user');
365
366 # Send the log message.
367 syslog(@syslog);
368
369 # Close the log handle.
370 closelog();
371}
372
3983aebd
SS
373#
374## Private function to write a given error message to the storederror file.
375#
376sub _store_error_message ($) {
377 my ($message) = @_;
378
379 # Remove any newline.
380 chomp($message);
381
382 # Open file for writing.
383 open (ERRORFILE, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n";
384
385 # Write error to file.
386 print ERRORFILE "$message\n";
387
388 # Close file.
389 close (ERRORFILE);
390}
391
1cae702c
SS
392#
393## Function to get a list of all available network zones.
394#
395sub get_available_network_zones () {
396 # Get netsettings.
397 my %netsettings = ();
398 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
399
400 # Obtain the configuration type from the netsettings hash.
401 my $config_type = $netsettings{'CONFIG_TYPE'};
402
403 # Hash which contains the conversation from the config mode
404 # to the existing network interface names. They are stored like
405 # an array.
406 #
407 # Mode "0" red is a modem and green
408 # Mode "1" red is a netdev and green
409 # Mode "2" red, green and orange
410 # Mode "3" red, green and blue
411 # Mode "4" red, green, blue, orange
412 my %config_type_to_interfaces = (
413 "0" => [ "red", "green" ],
414 "1" => [ "red", "green" ],
415 "2" => [ "red", "green", "orange" ],
416 "3" => [ "red", "green", "blue" ],
417 "4" => [ "red", "green", "blue", "orange" ]
418 );
419
420 # Obtain and dereference the corresponding network interaces based on the read
421 # network config type.
422 my @network_zones = @{ $config_type_to_interfaces{$config_type} };
423
424 # Return them.
425 return @network_zones;
426}
427
796eea21
SS
428#
429## Function to check if the IDS is running.
430#
431sub ids_is_running () {
432 if(-f $idspidfile) {
433 # Open PID file for reading.
434 open(PIDFILE, "$idspidfile") or die "Could not open $idspidfile. $!\n";
435
436 # Grab the process-id.
437 my $pid = <PIDFILE>;
438
439 # Close filehandle.
440 close(PIDFILE);
441
442 # Remove any newline.
443 chomp($pid);
444
445 # Check if a directory for the process-id exists in proc.
446 if(-d "/proc/$pid") {
447 # The IDS daemon is running return the process id.
448 return $pid;
449 }
450 }
451
452 # Return nothing - IDS is not running.
453 return;
454}
455
5240a809
SS
456#
457## Function to call suricatactrl binary with a given command.
458#
459sub call_suricatactrl ($) {
460 # Get called option.
ed06bc81 461 my ($option, $interval) = @_;
5240a809
SS
462
463 # Loop through the array of supported commands and check if
464 # the given one is part of it.
465 foreach my $cmd (@suricatactrl_cmds) {
466 # Skip current command unless the given one has been found.
467 next unless($cmd eq $option);
468
ed06bc81
SS
469 # Check if the given command is "cron".
470 if ($option eq "cron") {
471 # Check if an interval has been given.
472 if ($interval) {
473 # Check if the given interval is valid.
474 foreach my $element (@cron_intervals) {
475 # Skip current element until the given one has been found.
476 next unless($element eq $interval);
477
478 # Call the suricatactrl binary and pass the "cron" command
479 # with the requrested interval.
480 system("$suricatactrl $option $interval &>/dev/null");
481
482 # Return "1" - True.
483 return 1;
484 }
485 }
5240a809 486
ed06bc81
SS
487 # If we got here, the given interval is not supported or none has been given. - Return nothing.
488 return;
489 } else {
490 # Call the suricatactrl binary and pass the requrested
491 # option to it.
492 system("$suricatactrl $option &>/dev/null");
493
494 # Return "1" - True.
495 return 1;
496 }
5240a809
SS
497 }
498
499 # Command not found - return nothing.
500 return;
501}
502
308ba5e7
SS
503#
504## Function to create a new empty file.
505#
506sub create_empty_file($) {
507 my ($file) = @_;
508
509 # Check if the given file exists.
510 if(-e $file) {
511 # Do nothing to prevent from overwriting existing files.
512 return;
513 }
514
515 # Open the file for writing.
516 open(FILE, ">$file") or die "Could not write to $file. $!\n";
517
518 # Close file handle.
519 close(FILE);
520
521 # Return true.
522 return 1;
523}
524
330759d8
SS
525#
526## Private function to check if the file permission of the rulespath are correct.
527## If not, call suricatactrl to fix them.
528#
529sub _check_rulesdir_permissions() {
e568796b
SS
530 # Check if the rulepath main directory is writable.
531 unless (-W $rulespath) {
532 # If not call suricatctrl to fix it.
533 &call_suricatactrl("fix-rules-dir");
534 }
535
330759d8
SS
536 # Open snort rules directory and do a directory listing.
537 opendir(DIR, $rulespath) or die $!;
538 # Loop through the direcory.
539 while (my $file = readdir(DIR)) {
540 # We only want files.
541 next unless (-f "$rulespath/$file");
542
543 # Check if the file is writable by the user.
544 if (-W "$rulespath/$file") {
545 # Everything is okay - go on to the next file.
546 next;
547 } else {
548 # There are wrong permissions, call suricatactrl to fix it.
549 &call_suricatactrl("fix-rules-dir");
550 }
551 }
552}
553
b59cdbee
SS
554#
555## Private function to cleanup the directory which contains
556## the IDS rules, before extracting and modifing the new ruleset.
557#
558sub _cleanup_rulesdir() {
8cf04a16
SS
559 # Open rules directory and do a directory listing.
560 opendir(DIR, $rulespath) or die $!;
561
562 # Loop through the direcory.
563 while (my $file = readdir(DIR)) {
564 # We only want files.
565 next unless (-f "$rulespath/$file");
566
567 # Skip element if it has config as file extension.
568 next if ($file =~ m/\.config$/);
b59cdbee 569
8cf04a16 570 # Delete the current processed file, if not, exit this function
b59cdbee 571 # and return an error message.
1201c1e7 572 unlink("$rulespath/$file") or return "Could not delete $rulespath/$file. $!\n";
b59cdbee
SS
573 }
574
4ce42488 575 # Return nothing;
b59cdbee
SS
576 return;
577}
578
b02e30fd
SS
579#
580## Function to generate the file which contains the home net information.
581#
582sub generate_home_net_file() {
583 my %netsettings;
584
585 # Read-in network settings.
586 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
587
588 # Get available network zones.
589 my @network_zones = &get_available_network_zones();
590
591 # Temporary array to store network address and prefix of the configured
592 # networks.
593 my @networks;
594
595 # Loop through the array of available network zones.
596 foreach my $zone (@network_zones) {
597 # Skip the red network - It never can be part to the home_net!
598 next if($zone eq "red");
599
600 # Convert current zone name into upper case.
601 $zone = uc($zone);
602
603 # Generate key to access the required data from the netsettings hash.
604 my $zone_netaddress = $zone . "_NETADDRESS";
605 my $zone_netmask = $zone . "_NETMASK";
606
607 # Obtain the settings from the netsettings hash.
608 my $netaddress = $netsettings{$zone_netaddress};
609 my $netmask = $netsettings{$zone_netmask};
610
611 # Convert the subnetmask into prefix notation.
612 my $prefix = &Network::convert_netmask2prefix($netmask);
613
614 # Generate full network string.
615 my $network = join("/", $netaddress,$prefix);
616
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);
621 }
622 }
623
624 # Format home net declaration.
625 my $line = "\"\[";
626
627 # Loop through the array of networks.
628 foreach my $network (@networks) {
629 # Add the network to the line.
630 $line = "$line" . "$network";
631
632 # Check if the current network was the last in the array.
633 if ($network eq $networks[-1]) {
634 # Close the line.
635 $line = "$line" . "\]\"";
636 } else {
637 # Add "," for the next network.
638 $line = "$line" . "\,";
639 }
640 }
641
642 # Open file to store the addresses of the home net.
643 open(FILE, ">$homenet_file") or die "Could not open $homenet_file. $!\n";
644
645 # Print yaml header.
646 print FILE "%YAML 1.1\n";
647 print FILE "---\n\n";
648
649 # Print notice about autogenerated file.
650 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
651
652 # Print the generated and required HOME_NET declaration to the file.
653 print FILE "HOME_NET:\t$line\n";
654
655 # Close file handle.
656 close(FILE);
657}
658
659#
660## Function to generate and write the file for used rulefiles.
661#
662sub write_used_rulefiles_file(@) {
663 my @files = @_;
664
665 # Open file for used rulefiles.
666 open (FILE, ">$used_rulefiles_file") or die "Could not write to $used_rulefiles_file. $!\n";
667
668 # Write yaml header to the file.
669 print FILE "%YAML 1.1\n";
670 print FILE "---\n\n";
671
672 # Write header to file.
673 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
674
675 # Allways use the whitelist.
676 print FILE " - whitelist.rules\n";
677
678 # Loop through the array of given files.
679 foreach my $file (@files) {
680 # Check if the given filename exists and write it to the file of used rulefiles.
681 if(-f "$rulespath/$file") {
682 print FILE " - $file\n";
683 }
684 }
685
686 # Close file after writing.
687 close(FILE);
688}
689
74cc8f5a
SS
690#
691## Function to generate and write the file for modify the ruleset.
692#
693sub write_modify_sids_file($) {
694 my ($ruleaction) = @_;
695
696 # Open modify sid's file for writing.
697 open(FILE, ">$IDS::modify_sids_file") or die "Could not write to $IDS::modify_sids_file. $!\n";
698
699 # Write file header.
700 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
701
702 # Check if the traffic only should be monitored.
703 unless($ruleaction eq "alert") {
704 # Tell oinkmaster to switch all rules from alert to drop.
705 print FILE "modifysid \* \"alert\" \| \"drop\"\n";
706 }
707
708 # Close file handle.
709 close(FILE);
710}
711
8dcebe53 7121;