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