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