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