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