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