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