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