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