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