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