]>
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 | ||
24 | package IDS; | |
25 | ||
26 | require '/var/ipfire/general-functions.pl'; | |
8dcebe53 | 27 | |
02844177 | 28 | # Location where all config and settings files are stored. |
164eab66 | 29 | our $settingsdir = "${General::swroot}/suricata"; |
02844177 | 30 | |
b02e30fd SS |
31 | # File where the used rulefiles are stored. |
32 | our $used_rulefiles_file = "$settingsdir/suricata-used-rulefiles.yaml"; | |
33 | ||
34 | # File where the addresses of the homenet are stored. | |
35 | our $homenet_file = "$settingsdir/suricata-homenet.yaml"; | |
36 | ||
30ee98e9 SS |
37 | # File where the addresses of the used DNS servers are stored. |
38 | our $dns_servers_file = "$settingsdir/suricata-dns-servers.yaml"; | |
39 | ||
e698090e SS |
40 | # File where the HTTP ports definition is stored. |
41 | our $http_ports_file = "$settingsdir/suricata-http-ports.yaml"; | |
42 | ||
b02e30fd SS |
43 | # File which contains the enabled sids. |
44 | our $enabled_sids_file = "$settingsdir/oinkmaster-enabled-sids.conf"; | |
45 | ||
46 | # File which contains the disabled sids. | |
47 | our $disabled_sids_file = "$settingsdir/oinkmaster-disabled-sids.conf"; | |
48 | ||
49 | # File which contains wheater the rules should be changed. | |
50 | our $modify_sids_file = "$settingsdir/oinkmaster-modify-sids.conf"; | |
51 | ||
52 | # File which stores the configured IPS settings. | |
53 | our $ids_settings_file = "$settingsdir/settings"; | |
54 | ||
55 | # File which stores the configured rules-settings. | |
56 | our $rules_settings_file = "$settingsdir/rules-settings"; | |
57 | ||
58 | # File which stores the configured settings for whitelisted addresses. | |
59 | our $ignored_file = "$settingsdir/ignored"; | |
60 | ||
eea2670b | 61 | # Location and name of the tarball which contains the ruleset. |
164eab66 | 62 | our $rulestarball = "/var/tmp/idsrules.tar.gz"; |
eea2670b | 63 | |
3983aebd | 64 | # File to store any errors, which also will be read and displayed by the wui. |
77910792 | 65 | our $storederrorfile = "/tmp/ids_storederror"; |
3983aebd | 66 | |
8076deba SS |
67 | # File to lock the WUI, while the autoupdate script runs. |
68 | our $ids_page_lock_file = "/tmp/ids_page_locked"; | |
69 | ||
298ef5ba | 70 | # Location where the rulefiles are stored. |
21cab141 | 71 | our $rulespath = "/var/lib/suricata"; |
298ef5ba | 72 | |
612bb2df SS |
73 | # Location to store local rules. This file will not be touched. |
74 | our $local_rules_file = "$rulespath/local.rules"; | |
75 | ||
b02e30fd SS |
76 | # File which contains the rules to whitelist addresses on suricata. |
77 | our $whitelist_file = "$rulespath/whitelist.rules"; | |
78 | ||
bce84f39 SS |
79 | # File which contains a list of all supported ruleset sources. |
80 | # (Sourcefire, Emergingthreads, etc..) | |
81 | our $rulesetsourcesfile = "$settingsdir/ruleset-sources"; | |
82 | ||
796eea21 SS |
83 | # The pidfile of the IDS. |
84 | our $idspidfile = "/var/run/suricata.pid"; | |
85 | ||
5240a809 SS |
86 | # Location of suricatactrl. |
87 | my $suricatactrl = "/usr/local/bin/suricatactrl"; | |
88 | ||
89 | # Array with allowed commands of suricatactrl. | |
ed06bc81 SS |
90 | my @suricatactrl_cmds = ( 'start', 'stop', 'restart', 'reload', 'fix-rules-dir', 'cron' ); |
91 | ||
92 | # Array with supported cron intervals. | |
93 | my @cron_intervals = ('off', 'daily', 'weekly' ); | |
5240a809 | 94 | |
e698090e SS |
95 | # Array which contains the HTTP ports, which statically will be declared as HTTP_PORTS in the |
96 | # http_ports_file. | |
97 | my @http_ports = ('80', '81'); | |
98 | ||
b02e30fd SS |
99 | # |
100 | ## Function to check and create all IDS related files, if the does not exist. | |
101 | # | |
102 | sub check_and_create_filelayout() { | |
103 | # Check if the files exist and if not, create them. | |
104 | unless (-f "$enabled_sids_file") { &create_empty_file($enabled_sids_file); } | |
105 | unless (-f "$disabled_sids_file") { &create_empty_file($disabled_sids_file); } | |
106 | unless (-f "$modify_sids_file") { &create_empty_file($modify_sids_file); } | |
107 | unless (-f "$used_rulefiles_file") { &create_empty_file($used_rulefiles_file); } | |
108 | unless (-f "$ids_settings_file") { &create_empty_file($ids_settings_file); } | |
109 | unless (-f "$rules_settings_file") { &create_empty_file($rules_settings_file); } | |
110 | unless (-f "$ignored_file") { &create_empty_file($ignored_file); } | |
111 | unless (-f "$whitelist_file" ) { &create_empty_file($whitelist_file); } | |
112 | } | |
113 | ||
8dcebe53 SS |
114 | # |
115 | ## Function for checking if at least 300MB of free disk space are available | |
116 | ## on the "/var" partition. | |
117 | # | |
118 | sub checkdiskspace () { | |
119 | # Call diskfree to gather the free disk space of /var. | |
120 | my @df = `/bin/df -B M /var`; | |
121 | ||
122 | # Loop through the output. | |
123 | foreach my $line (@df) { | |
124 | # Ignore header line. | |
125 | next if $line =~ m/^Filesystem/; | |
126 | ||
127 | # Search for a line with the device information. | |
128 | if ($line =~ m/dev/ ) { | |
129 | # Split the line into single pieces. | |
130 | my @values = split(' ', $line); | |
131 | my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values; | |
132 | ||
133 | # Check if the available disk space is more than 300MB. | |
134 | if ($available < 300) { | |
434001d0 SS |
135 | # Log error to syslog. |
136 | &_log_to_syslog("Not enough free disk space on /var. Only $available MB from 300 MB available."); | |
8dcebe53 | 137 | |
434001d0 SS |
138 | # Exit function and return "1" - False. |
139 | return 1; | |
8dcebe53 SS |
140 | } |
141 | } | |
142 | } | |
143 | ||
144 | # Everything okay, return nothing. | |
145 | return; | |
146 | } | |
147 | ||
eea2670b | 148 | # |
b749416a | 149 | ## This function is responsible for downloading the configured IDS ruleset. |
eea2670b | 150 | ## |
b749416a | 151 | ## * At first it obtains from the stored rules settings which ruleset should be downloaded. |
eea2670b SS |
152 | ## * The next step is to get the download locations for all available rulesets. |
153 | ## * After that, the function will check if an upstream proxy should be used and grab the settings. | |
154 | ## * The last step will be to generate the final download url, by obtaining the URL for the desired | |
155 | ## ruleset, add the settings for the upstream proxy and final grab the rules tarball from the server. | |
156 | # | |
157 | sub downloadruleset { | |
b749416a SS |
158 | # Get rules settings. |
159 | my %rulessettings=(); | |
160 | &General::readhash("$rules_settings_file", \%rulessettings); | |
eea2670b | 161 | |
be52c68a | 162 | # Check if a ruleset has been configured. |
b749416a | 163 | unless($rulessettings{'RULES'}) { |
be52c68a SS |
164 | # Log that no ruleset has been configured and abort. |
165 | &_log_to_syslog("No ruleset source has been configured."); | |
166 | ||
167 | # Return "1". | |
168 | return 1; | |
169 | } | |
170 | ||
eea2670b SS |
171 | # Get all available ruleset locations. |
172 | my %rulesetsources=(); | |
bce84f39 | 173 | &General::readhash($rulesetsourcesfile, \%rulesetsources); |
eea2670b SS |
174 | |
175 | # Read proxysettings. | |
176 | my %proxysettings=(); | |
177 | &General::readhash("${General::swroot}/proxy/settings", \%proxysettings); | |
178 | ||
179 | # Load required perl module to handle the download. | |
180 | use LWP::UserAgent; | |
181 | ||
182 | # Init the download module. | |
183 | my $downloader = LWP::UserAgent->new; | |
184 | ||
185 | # Set timeout to 10 seconds. | |
186 | $downloader->timeout(10); | |
187 | ||
188 | # Check if an upstream proxy is configured. | |
189 | if ($proxysettings{'UPSTREAM_PROXY'}) { | |
eea2670b SS |
190 | my $proxy_url; |
191 | ||
40407aee | 192 | $proxy_url = "http://"; |
eea2670b | 193 | |
40407aee PM |
194 | # Check if the proxy requires authentication. |
195 | if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) { | |
196 | $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@"; | |
eea2670b SS |
197 | } |
198 | ||
40407aee PM |
199 | # Add proxy server address and port. |
200 | $proxy_url .= $proxysettings{'UPSTREAM_PROXY'}; | |
201 | ||
eea2670b | 202 | # Setup proxy settings. |
6994f001 | 203 | $downloader->proxy(['http', 'https'], $proxy_url); |
eea2670b SS |
204 | } |
205 | ||
206 | # Grab the right url based on the configured vendor. | |
b749416a | 207 | my $url = $rulesetsources{$rulessettings{'RULES'}}; |
eea2670b SS |
208 | |
209 | # Check if the vendor requires an oinkcode and add it if needed. | |
b749416a | 210 | $url =~ s/\<oinkcode\>/$rulessettings{'OINKCODE'}/g; |
eea2670b SS |
211 | |
212 | # Abort if no url could be determined for the vendor. | |
213 | unless ($url) { | |
434001d0 SS |
214 | # Log error and abort. |
215 | &_log_to_syslog("Unable to gather a download URL for the selected ruleset."); | |
216 | return 1; | |
eea2670b SS |
217 | } |
218 | ||
155b3b56 SS |
219 | # Variable to store the filesize of the remote object. |
220 | my $remote_filesize; | |
96da5803 | 221 | |
155b3b56 SS |
222 | # The sourcfire (snort rules) does not allow to send "HEAD" requests, so skip this check |
223 | # for this webserver. | |
224 | # | |
225 | # Check if the ruleset source contains "snort.org". | |
226 | unless ($url =~ /\.snort\.org/) { | |
227 | # Pass the requrested url to the downloader. | |
228 | my $request = HTTP::Request->new(HEAD => $url); | |
96da5803 | 229 | |
155b3b56 SS |
230 | # Accept the html header. |
231 | $request->header('Accept' => 'text/html'); | |
96da5803 | 232 | |
155b3b56 SS |
233 | # Perform the request and fetch the html header. |
234 | my $response = $downloader->request($request); | |
96da5803 | 235 | |
155b3b56 SS |
236 | # Check if there was any error. |
237 | unless ($response->is_success) { | |
238 | # Obtain error. | |
239 | my $error = $response->status_line(); | |
96da5803 | 240 | |
155b3b56 SS |
241 | # Log error message. |
242 | &_log_to_syslog("Unable to download the ruleset. \($error\)"); | |
243 | ||
244 | # Return "1" - false. | |
245 | return 1; | |
246 | } | |
96da5803 | 247 | |
155b3b56 SS |
248 | # Assign the fetched header object. |
249 | my $header = $response->headers(); | |
96da5803 | 250 | |
155b3b56 SS |
251 | # Grab the remote file size from the object and store it in the |
252 | # variable. | |
253 | $remote_filesize = $header->content_length; | |
254 | } | |
96da5803 | 255 | |
25b6545a SS |
256 | # Load perl module to deal with temporary files. |
257 | use File::Temp; | |
258 | ||
a5ba473c | 259 | # Generate temporary file name, located in "/var/tmp" and with a suffix of ".tar.gz". |
25b6545a SS |
260 | my $tmp = File::Temp->new( SUFFIX => ".tar.gz", DIR => "/var/tmp/", UNLINK => 0 ); |
261 | my $tmpfile = $tmp->filename(); | |
262 | ||
eea2670b SS |
263 | # Pass the requested url to the downloader. |
264 | my $request = HTTP::Request->new(GET => $url); | |
265 | ||
25b6545a SS |
266 | # Perform the request and save the output into the tmpfile. |
267 | my $response = $downloader->request($request, $tmpfile); | |
eea2670b SS |
268 | |
269 | # Check if there was any error. | |
270 | unless ($response->is_success) { | |
88daf7eb SS |
271 | # Obtain error. |
272 | my $error = $response->content; | |
273 | ||
434001d0 | 274 | # Log error message. |
88daf7eb | 275 | &_log_to_syslog("Unable to download the ruleset. \($error\)"); |
434001d0 SS |
276 | |
277 | # Return "1" - false. | |
278 | return 1; | |
eea2670b SS |
279 | } |
280 | ||
96da5803 SS |
281 | # Load perl stat module. |
282 | use File::stat; | |
283 | ||
25b6545a SS |
284 | # Perform stat on the tmpfile. |
285 | my $stat = stat($tmpfile); | |
96da5803 SS |
286 | |
287 | # Grab the local filesize of the downloaded tarball. | |
288 | my $local_filesize = $stat->size; | |
289 | ||
290 | # Check if both file sizes match. | |
155b3b56 | 291 | if (($remote_filesize) && ($remote_filesize ne $local_filesize)) { |
96da5803 SS |
292 | # Log error message. |
293 | &_log_to_syslog("Unable to completely download the ruleset. "); | |
294 | &_log_to_syslog("Only got $local_filesize Bytes instead of $remote_filesize Bytes. "); | |
295 | ||
25b6545a SS |
296 | # Delete temporary file. |
297 | unlink("$tmpfile"); | |
298 | ||
96da5803 SS |
299 | # Return "1" - false. |
300 | return 1; | |
301 | } | |
302 | ||
25b6545a SS |
303 | # Load file copy module, which contains the move() function. |
304 | use File::Copy; | |
305 | ||
306 | # Overwrite existing rules tarball with the new downloaded one. | |
307 | move("$tmpfile", "$rulestarball"); | |
308 | ||
a5ba473c TF |
309 | # Set correct ownership for the rulesdir and files. |
310 | set_ownership("$rulestarball"); | |
311 | ||
eea2670b SS |
312 | # If we got here, everything worked fine. Return nothing. |
313 | return; | |
314 | } | |
8dcebe53 | 315 | |
25f5cb0d SS |
316 | # |
317 | ## A tiny wrapper function to call the oinkmaster script. | |
318 | # | |
319 | sub oinkmaster () { | |
330759d8 SS |
320 | # Check if the files in rulesdir have the correct permissions. |
321 | &_check_rulesdir_permissions(); | |
322 | ||
883820bd SS |
323 | # Cleanup the rules directory before filling it with the new rulest. |
324 | &_cleanup_rulesdir(); | |
325 | ||
0e40e1e7 SS |
326 | # Load perl module to talk to the kernel syslog. |
327 | use Sys::Syslog qw(:DEFAULT setlogsock); | |
328 | ||
329 | # Establish the connection to the syslog service. | |
330 | openlog('oinkmaster', 'cons,pid', 'user'); | |
331 | ||
25f5cb0d | 332 | # Call oinkmaster to generate ruleset. |
26dc79a6 | 333 | open(OINKMASTER, "/usr/local/bin/oinkmaster.pl -s -u file://$rulestarball -C $settingsdir/oinkmaster.conf -o $rulespath 2>&1 |") or die "Could not execute oinkmaster $!\n"; |
0e40e1e7 SS |
334 | |
335 | # Log output of oinkmaster to syslog. | |
336 | while(<OINKMASTER>) { | |
337 | # The syslog function works best with an array based input, | |
338 | # so generate one before passing the message details to syslog. | |
339 | my @syslog = ("INFO", "$_"); | |
340 | ||
341 | # Send the log message. | |
342 | syslog(@syslog); | |
343 | } | |
344 | ||
345 | # Close the pipe to oinkmaster process. | |
346 | close(OINKMASTER); | |
347 | ||
348 | # Close the log handle. | |
349 | closelog(); | |
25f5cb0d SS |
350 | } |
351 | ||
3983aebd SS |
352 | # |
353 | ## Function to do all the logging stuff if the downloading or updating of the ruleset fails. | |
354 | # | |
355 | sub log_error ($) { | |
356 | my ($error) = @_; | |
357 | ||
358 | # Remove any newline. | |
359 | chomp($error); | |
360 | ||
eb5592c1 SS |
361 | # Call private function to log the error message to syslog. |
362 | &_log_to_syslog($error); | |
363 | ||
3983aebd SS |
364 | # Call private function to write/store the error message in the storederrorfile. |
365 | &_store_error_message($error); | |
366 | } | |
367 | ||
eb5592c1 SS |
368 | # |
369 | ## Function to log a given error message to the kernel syslog. | |
370 | # | |
371 | sub _log_to_syslog ($) { | |
372 | my ($message) = @_; | |
373 | ||
374 | # Load perl module to talk to the kernel syslog. | |
375 | use Sys::Syslog qw(:DEFAULT setlogsock); | |
376 | ||
377 | # The syslog function works best with an array based input, | |
378 | # so generate one before passing the message details to syslog. | |
379 | my @syslog = ("ERR", "<ERROR> $message"); | |
380 | ||
381 | # Establish the connection to the syslog service. | |
382 | openlog('oinkmaster', 'cons,pid', 'user'); | |
383 | ||
384 | # Send the log message. | |
385 | syslog(@syslog); | |
386 | ||
387 | # Close the log handle. | |
388 | closelog(); | |
389 | } | |
390 | ||
3983aebd SS |
391 | # |
392 | ## Private function to write a given error message to the storederror file. | |
393 | # | |
394 | sub _store_error_message ($) { | |
395 | my ($message) = @_; | |
396 | ||
397 | # Remove any newline. | |
398 | chomp($message); | |
399 | ||
400 | # Open file for writing. | |
401 | open (ERRORFILE, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n"; | |
402 | ||
403 | # Write error to file. | |
404 | print ERRORFILE "$message\n"; | |
405 | ||
406 | # Close file. | |
407 | close (ERRORFILE); | |
3c59b1fa SS |
408 | |
409 | # Set correct ownership for the file. | |
410 | &set_ownership("$storederrorfile"); | |
3983aebd SS |
411 | } |
412 | ||
1cae702c SS |
413 | # |
414 | ## Function to get a list of all available network zones. | |
415 | # | |
416 | sub get_available_network_zones () { | |
417 | # Get netsettings. | |
418 | my %netsettings = (); | |
419 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
420 | ||
421 | # Obtain the configuration type from the netsettings hash. | |
422 | my $config_type = $netsettings{'CONFIG_TYPE'}; | |
423 | ||
424 | # Hash which contains the conversation from the config mode | |
425 | # to the existing network interface names. They are stored like | |
426 | # an array. | |
427 | # | |
428 | # Mode "0" red is a modem and green | |
429 | # Mode "1" red is a netdev and green | |
430 | # Mode "2" red, green and orange | |
431 | # Mode "3" red, green and blue | |
432 | # Mode "4" red, green, blue, orange | |
433 | my %config_type_to_interfaces = ( | |
434 | "0" => [ "red", "green" ], | |
435 | "1" => [ "red", "green" ], | |
436 | "2" => [ "red", "green", "orange" ], | |
437 | "3" => [ "red", "green", "blue" ], | |
438 | "4" => [ "red", "green", "blue", "orange" ] | |
439 | ); | |
440 | ||
441 | # Obtain and dereference the corresponding network interaces based on the read | |
442 | # network config type. | |
443 | my @network_zones = @{ $config_type_to_interfaces{$config_type} }; | |
444 | ||
445 | # Return them. | |
446 | return @network_zones; | |
447 | } | |
448 | ||
796eea21 SS |
449 | # |
450 | ## Function to check if the IDS is running. | |
451 | # | |
452 | sub ids_is_running () { | |
453 | if(-f $idspidfile) { | |
454 | # Open PID file for reading. | |
455 | open(PIDFILE, "$idspidfile") or die "Could not open $idspidfile. $!\n"; | |
456 | ||
457 | # Grab the process-id. | |
458 | my $pid = <PIDFILE>; | |
459 | ||
460 | # Close filehandle. | |
461 | close(PIDFILE); | |
462 | ||
463 | # Remove any newline. | |
464 | chomp($pid); | |
465 | ||
466 | # Check if a directory for the process-id exists in proc. | |
467 | if(-d "/proc/$pid") { | |
468 | # The IDS daemon is running return the process id. | |
469 | return $pid; | |
470 | } | |
471 | } | |
472 | ||
473 | # Return nothing - IDS is not running. | |
474 | return; | |
475 | } | |
476 | ||
5240a809 SS |
477 | # |
478 | ## Function to call suricatactrl binary with a given command. | |
479 | # | |
480 | sub call_suricatactrl ($) { | |
481 | # Get called option. | |
ed06bc81 | 482 | my ($option, $interval) = @_; |
5240a809 SS |
483 | |
484 | # Loop through the array of supported commands and check if | |
485 | # the given one is part of it. | |
486 | foreach my $cmd (@suricatactrl_cmds) { | |
487 | # Skip current command unless the given one has been found. | |
488 | next unless($cmd eq $option); | |
489 | ||
ed06bc81 SS |
490 | # Check if the given command is "cron". |
491 | if ($option eq "cron") { | |
492 | # Check if an interval has been given. | |
493 | if ($interval) { | |
494 | # Check if the given interval is valid. | |
495 | foreach my $element (@cron_intervals) { | |
496 | # Skip current element until the given one has been found. | |
497 | next unless($element eq $interval); | |
498 | ||
499 | # Call the suricatactrl binary and pass the "cron" command | |
500 | # with the requrested interval. | |
501 | system("$suricatactrl $option $interval &>/dev/null"); | |
502 | ||
503 | # Return "1" - True. | |
504 | return 1; | |
505 | } | |
506 | } | |
5240a809 | 507 | |
ed06bc81 SS |
508 | # If we got here, the given interval is not supported or none has been given. - Return nothing. |
509 | return; | |
510 | } else { | |
511 | # Call the suricatactrl binary and pass the requrested | |
512 | # option to it. | |
513 | system("$suricatactrl $option &>/dev/null"); | |
514 | ||
515 | # Return "1" - True. | |
516 | return 1; | |
517 | } | |
5240a809 SS |
518 | } |
519 | ||
520 | # Command not found - return nothing. | |
521 | return; | |
522 | } | |
523 | ||
308ba5e7 SS |
524 | # |
525 | ## Function to create a new empty file. | |
526 | # | |
527 | sub create_empty_file($) { | |
528 | my ($file) = @_; | |
529 | ||
530 | # Check if the given file exists. | |
531 | if(-e $file) { | |
532 | # Do nothing to prevent from overwriting existing files. | |
533 | return; | |
534 | } | |
535 | ||
536 | # Open the file for writing. | |
537 | open(FILE, ">$file") or die "Could not write to $file. $!\n"; | |
538 | ||
539 | # Close file handle. | |
540 | close(FILE); | |
541 | ||
542 | # Return true. | |
543 | return 1; | |
544 | } | |
545 | ||
330759d8 SS |
546 | # |
547 | ## Private function to check if the file permission of the rulespath are correct. | |
548 | ## If not, call suricatactrl to fix them. | |
549 | # | |
550 | sub _check_rulesdir_permissions() { | |
e568796b SS |
551 | # Check if the rulepath main directory is writable. |
552 | unless (-W $rulespath) { | |
553 | # If not call suricatctrl to fix it. | |
554 | &call_suricatactrl("fix-rules-dir"); | |
555 | } | |
556 | ||
330759d8 SS |
557 | # Open snort rules directory and do a directory listing. |
558 | opendir(DIR, $rulespath) or die $!; | |
559 | # Loop through the direcory. | |
560 | while (my $file = readdir(DIR)) { | |
561 | # We only want files. | |
562 | next unless (-f "$rulespath/$file"); | |
563 | ||
564 | # Check if the file is writable by the user. | |
565 | if (-W "$rulespath/$file") { | |
566 | # Everything is okay - go on to the next file. | |
567 | next; | |
568 | } else { | |
569 | # There are wrong permissions, call suricatactrl to fix it. | |
570 | &call_suricatactrl("fix-rules-dir"); | |
571 | } | |
572 | } | |
573 | } | |
574 | ||
b59cdbee SS |
575 | # |
576 | ## Private function to cleanup the directory which contains | |
577 | ## the IDS rules, before extracting and modifing the new ruleset. | |
578 | # | |
579 | sub _cleanup_rulesdir() { | |
8cf04a16 SS |
580 | # Open rules directory and do a directory listing. |
581 | opendir(DIR, $rulespath) or die $!; | |
582 | ||
583 | # Loop through the direcory. | |
584 | while (my $file = readdir(DIR)) { | |
585 | # We only want files. | |
586 | next unless (-f "$rulespath/$file"); | |
587 | ||
588 | # Skip element if it has config as file extension. | |
589 | next if ($file =~ m/\.config$/); | |
b59cdbee | 590 | |
fefb5173 SS |
591 | # Skip rules file for whitelisted hosts. |
592 | next if ("$rulespath/$file" eq $whitelist_file); | |
593 | ||
612bb2df SS |
594 | # Skip rules file with local rules. |
595 | next if ("$rulespath/$file" eq $local_rules_file); | |
596 | ||
8cf04a16 | 597 | # Delete the current processed file, if not, exit this function |
b59cdbee | 598 | # and return an error message. |
1201c1e7 | 599 | unlink("$rulespath/$file") or return "Could not delete $rulespath/$file. $!\n"; |
b59cdbee SS |
600 | } |
601 | ||
4ce42488 | 602 | # Return nothing; |
b59cdbee SS |
603 | return; |
604 | } | |
605 | ||
b02e30fd SS |
606 | # |
607 | ## Function to generate the file which contains the home net information. | |
608 | # | |
609 | sub generate_home_net_file() { | |
610 | my %netsettings; | |
611 | ||
612 | # Read-in network settings. | |
613 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
614 | ||
615 | # Get available network zones. | |
616 | my @network_zones = &get_available_network_zones(); | |
617 | ||
618 | # Temporary array to store network address and prefix of the configured | |
619 | # networks. | |
620 | my @networks; | |
621 | ||
622 | # Loop through the array of available network zones. | |
623 | foreach my $zone (@network_zones) { | |
bcbc9897 SS |
624 | # Check if the current processed zone is red. |
625 | if($zone eq "red") { | |
626 | # Grab the IP-address of the red interface. | |
627 | my $red_address = &get_red_address(); | |
b02e30fd | 628 | |
bcbc9897 SS |
629 | # Check if an address has been obtained. |
630 | if ($red_address) { | |
631 | # Generate full network string. | |
632 | my $red_network = join("/", $red_address, "32"); | |
b02e30fd | 633 | |
bcbc9897 SS |
634 | # Add the red network to the array of networks. |
635 | push(@networks, $red_network); | |
636 | } | |
23c0347a | 637 | |
23c0347a SS |
638 | # Check if the configured RED_TYPE is static. |
639 | if ($netsettings{'RED_TYPE'} eq "STATIC") { | |
640 | # Get configured and enabled aliases. | |
641 | my @aliases = &get_aliases(); | |
642 | ||
643 | # Loop through the array. | |
644 | foreach my $alias (@aliases) { | |
645 | # Add "/32" prefix. | |
646 | my $network = join("/", $alias, "32"); | |
647 | ||
648 | # Add the generated network to the array of networks. | |
649 | push(@networks, $network); | |
650 | } | |
651 | } | |
bcbc9897 SS |
652 | # Process remaining network zones. |
653 | } else { | |
654 | # Convert current zone name into upper case. | |
655 | $zone = uc($zone); | |
656 | ||
657 | # Generate key to access the required data from the netsettings hash. | |
658 | my $zone_netaddress = $zone . "_NETADDRESS"; | |
659 | my $zone_netmask = $zone . "_NETMASK"; | |
660 | ||
661 | # Obtain the settings from the netsettings hash. | |
662 | my $netaddress = $netsettings{$zone_netaddress}; | |
663 | my $netmask = $netsettings{$zone_netmask}; | |
664 | ||
665 | # Convert the subnetmask into prefix notation. | |
666 | my $prefix = &Network::convert_netmask2prefix($netmask); | |
667 | ||
668 | # Generate full network string. | |
669 | my $network = join("/", $netaddress,$prefix); | |
670 | ||
671 | # Check if the network is valid. | |
672 | if(&Network::check_subnet($network)) { | |
673 | # Add the generated network to the array of networks. | |
674 | push(@networks, $network); | |
675 | } | |
23c0347a | 676 | } |
b02e30fd SS |
677 | } |
678 | ||
679 | # Format home net declaration. | |
7479c993 | 680 | my $line = "\"[" . join(',', @networks) . "]\""; |
b02e30fd SS |
681 | |
682 | # Open file to store the addresses of the home net. | |
683 | open(FILE, ">$homenet_file") or die "Could not open $homenet_file. $!\n"; | |
684 | ||
685 | # Print yaml header. | |
686 | print FILE "%YAML 1.1\n"; | |
687 | print FILE "---\n\n"; | |
688 | ||
689 | # Print notice about autogenerated file. | |
690 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
691 | ||
692 | # Print the generated and required HOME_NET declaration to the file. | |
693 | print FILE "HOME_NET:\t$line\n"; | |
694 | ||
695 | # Close file handle. | |
696 | close(FILE); | |
697 | } | |
698 | ||
30ee98e9 SS |
699 | # |
700 | # Function to generate and write the file which contains the configured and used DNS servers. | |
701 | # | |
702 | sub generate_dns_servers_file() { | |
12c49915 SS |
703 | # Get the used DNS servers. |
704 | my @nameservers = &General::get_nameservers(); | |
30ee98e9 | 705 | |
7b97359b SS |
706 | # Get network settings. |
707 | my %netsettings; | |
708 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
709 | ||
30ee98e9 | 710 | # Format dns servers declaration. |
b76118c3 | 711 | my $line = ""; |
30ee98e9 | 712 | |
12c49915 SS |
713 | # Check if the system has configured nameservers. |
714 | if (@nameservers) { | |
7b97359b SS |
715 | # Add the GREEN address as DNS servers. |
716 | push(@nameservers, $netsettings{'GREEN_ADDRESS'}); | |
717 | ||
718 | # Check if a BLUE zone exists. | |
719 | if ($netsettings{'BLUE_ADDRESS'}) { | |
720 | # Add the BLUE address to the array of nameservers. | |
721 | push(@nameservers, $netsettings{'BLUE_ADDRESS'}); | |
722 | } | |
723 | ||
724 | # Generate the line which will be written to the DNS servers file. | |
b76118c3 | 725 | $line = join(",", @nameservers); |
12c49915 | 726 | } else { |
7b97359b | 727 | # External net simply contains (any). |
99cadb74 | 728 | $line = "\$EXTERNAL_NET"; |
fd2dccaa | 729 | } |
30ee98e9 | 730 | |
30ee98e9 SS |
731 | # Open file to store the used DNS server addresses. |
732 | open(FILE, ">$dns_servers_file") or die "Could not open $dns_servers_file. $!\n"; | |
733 | ||
734 | # Print yaml header. | |
735 | print FILE "%YAML 1.1\n"; | |
736 | print FILE "---\n\n"; | |
737 | ||
738 | # Print notice about autogenerated file. | |
739 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
740 | ||
741 | # Print the generated DNS declaration to the file. | |
b76118c3 | 742 | print FILE "DNS_SERVERS:\t\"[$line]\"\n"; |
30ee98e9 SS |
743 | |
744 | # Close file handle. | |
745 | close(FILE); | |
746 | } | |
747 | ||
e698090e SS |
748 | # |
749 | # Function to generate and write the file which contains the HTTP_PORTS definition. | |
750 | # | |
751 | sub generate_http_ports_file() { | |
752 | my %proxysettings; | |
753 | ||
754 | # Read-in proxy settings | |
755 | &General::readhash("${General::swroot}/proxy/advanced/settings", \%proxysettings); | |
756 | ||
757 | # Check if the proxy is enabled. | |
758 | if (( -e "${General::swroot}/proxy/enable") || (-e "${General::swroot}/proxy/enable_blue")) { | |
759 | # Add the proxy port to the array of HTTP ports. | |
760 | push(@http_ports, $proxysettings{'PROXY_PORT'}); | |
761 | } | |
762 | ||
763 | # Check if the transparent mode of the proxy is enabled. | |
764 | if ((-e "${General::swroot}/proxy/transparent") || (-e "${General::swroot}/proxy/transparent_blue")) { | |
765 | # Add the transparent proxy port to the array of HTTP ports. | |
766 | push(@http_ports, $proxysettings{'TRANSPARENT_PORT'}); | |
767 | } | |
768 | ||
769 | # Format HTTP_PORTS declaration. | |
770 | my $line = ""; | |
771 | ||
772 | # Generate line which will be written to the http ports file. | |
773 | $line = join(",", @http_ports); | |
774 | ||
775 | # Open file to store the HTTP_PORTS. | |
776 | open(FILE, ">$http_ports_file") or die "Could not open $http_ports_file. $!\n"; | |
777 | ||
778 | # Print yaml header. | |
779 | print FILE "%YAML 1.1\n"; | |
780 | print FILE "---\n\n"; | |
781 | ||
782 | # Print notice about autogenerated file. | |
783 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
784 | ||
785 | # Print the generated HTTP_PORTS declaration to the file. | |
786 | print FILE "HTTP_PORTS:\t\"[$line]\"\n"; | |
787 | ||
788 | # Close file handle. | |
789 | close(FILE); | |
790 | } | |
791 | ||
b02e30fd SS |
792 | # |
793 | ## Function to generate and write the file for used rulefiles. | |
794 | # | |
795 | sub write_used_rulefiles_file(@) { | |
796 | my @files = @_; | |
797 | ||
798 | # Open file for used rulefiles. | |
799 | open (FILE, ">$used_rulefiles_file") or die "Could not write to $used_rulefiles_file. $!\n"; | |
800 | ||
801 | # Write yaml header to the file. | |
802 | print FILE "%YAML 1.1\n"; | |
803 | print FILE "---\n\n"; | |
804 | ||
805 | # Write header to file. | |
806 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
807 | ||
808 | # Allways use the whitelist. | |
809 | print FILE " - whitelist.rules\n"; | |
810 | ||
811 | # Loop through the array of given files. | |
812 | foreach my $file (@files) { | |
813 | # Check if the given filename exists and write it to the file of used rulefiles. | |
814 | if(-f "$rulespath/$file") { | |
815 | print FILE " - $file\n"; | |
816 | } | |
817 | } | |
818 | ||
819 | # Close file after writing. | |
820 | close(FILE); | |
821 | } | |
822 | ||
74cc8f5a SS |
823 | # |
824 | ## Function to generate and write the file for modify the ruleset. | |
825 | # | |
81bae51f SS |
826 | sub write_modify_sids_file() { |
827 | # Get configured settings. | |
828 | my %idssettings=(); | |
829 | my %rulessettings=(); | |
830 | &General::readhash("$ids_settings_file", \%idssettings); | |
831 | &General::readhash("$rules_settings_file", \%rulessettings); | |
832 | ||
833 | # Gather the configured ruleset. | |
834 | my $ruleset = $rulessettings{'RULES'}; | |
74cc8f5a SS |
835 | |
836 | # Open modify sid's file for writing. | |
2ee51088 | 837 | open(FILE, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n"; |
74cc8f5a SS |
838 | |
839 | # Write file header. | |
840 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
841 | ||
842 | # Check if the traffic only should be monitored. | |
81bae51f | 843 | unless($idssettings{'MONITOR_TRAFFIC_ONLY'} eq 'on') { |
a5ba473c TF |
844 | # Suricata is in IPS mode, which means that the rule actions have to be changed |
845 | # from 'alert' to 'drop', however not all rules should be changed. Some rules | |
846 | # exist purely to set a flowbit which is used to convey other information, such | |
847 | # as a specific type of file being downloaded, to other rulewhich then check for | |
848 | # malware in that file. Rules which fall into the first category should stay as | |
849 | # alert since not all flows of that type contain malware. | |
850 | ||
81bae51f | 851 | if($ruleset eq 'registered' or $ruleset eq 'subscripted' or $ruleset eq 'community') { |
a5ba473c TF |
852 | # These types of rulesfiles contain meta-data which gives the action that should |
853 | # be used when in IPS mode. Do the following: | |
854 | # | |
855 | # 1. Disable all rules and set the action to 'drop' | |
856 | # 2. Set the action back to 'alert' if the rule contains 'flowbits:noalert;' | |
857 | # This should give rules not in the policy a reasonable default if the user | |
858 | # manually enables them. | |
859 | # 3. Enable rules and set actions according to the meta-data strings. | |
860 | ||
861 | my $policy = 'balanced'; # Placeholder to allow policy to be changed. | |
862 | ||
863 | print FILE <<END; | |
864 | modifysid * "^#?(?:alert|drop)" | "#drop" | |
865 | modifysid * "^#drop(.+flowbits:noalert;)" | "#alert\${1}" | |
866 | modifysid * "^#(?:alert|drop)(.+policy $policy-ips alert)" | "alert\${1}" | |
867 | modifysid * "^#(?:alert|drop)(.+policy $policy-ips drop)" | "drop\${1}" | |
868 | END | |
869 | } else { | |
870 | # These rulefiles don't have the metadata, so set rules to 'drop' unless they | |
871 | # contain the string 'flowbits:noalert;'. | |
872 | print FILE <<END; | |
873 | modifysid * "^(#?)(?:alert|drop)" | "\${1}drop" | |
874 | modifysid * "^(#?)drop(.+flowbits:noalert;)" | "\${1}alert\${2}" | |
875 | END | |
876 | } | |
74cc8f5a SS |
877 | } |
878 | ||
879 | # Close file handle. | |
880 | close(FILE); | |
881 | } | |
882 | ||
04a0d07c SS |
883 | # |
884 | ## Function to gather the version of suricata. | |
885 | # | |
886 | sub get_suricata_version($) { | |
887 | my ($format) = @_; | |
888 | ||
889 | # Execute piped suricata command and return the version information. | |
890 | open(SURICATA, "suricata -V |") or die "Couldn't execute program: $!"; | |
891 | ||
892 | # Grab and store the output of the piped program. | |
893 | my $version_string = <SURICATA>; | |
894 | ||
895 | # Close pipe. | |
896 | close(SURICATA); | |
897 | ||
898 | # Remove newlines. | |
899 | chomp($version_string); | |
900 | ||
fd2dccaa | 901 | # Grab the version from the version string. |
04a0d07c SS |
902 | $version_string =~ /([0-9]+([.][0-9]+)+)/; |
903 | ||
904 | # Splitt the version into single chunks. | |
905 | my ($major_ver, $minor_ver, $patchlevel) = split(/\./, $1); | |
906 | ||
907 | # Check and return the requested version sheme. | |
908 | if ($format eq "major") { | |
909 | # Return the full version. | |
910 | return "$major_ver"; | |
911 | } elsif ($format eq "minor") { | |
912 | # Return the major and minor part. | |
913 | return "$major_ver.$minor_ver"; | |
914 | } else { | |
915 | # Return the full version string. | |
916 | return "$major_ver.$minor_ver.$patchlevel"; | |
fd2dccaa | 917 | } |
04a0d07c SS |
918 | } |
919 | ||
9283e9b9 SS |
920 | # |
921 | ## Function to generate the rules file with whitelisted addresses. | |
922 | # | |
923 | sub generate_ignore_file() { | |
924 | my %ignored = (); | |
925 | ||
926 | # SID range 1000000-1999999 Reserved for Local Use | |
927 | # Put your custom rules in this range to avoid conflicts | |
928 | my $sid = 1500000; | |
929 | ||
930 | # Read-in ignoredfile. | |
931 | &General::readhasharray($IDS::ignored_file, \%ignored); | |
932 | ||
933 | # Open ignorefile for writing. | |
934 | open(FILE, ">$IDS::whitelist_file") or die "Could not write to $IDS::whitelist_file. $!\n"; | |
935 | ||
936 | # Config file header. | |
937 | print FILE "# Autogenerated file.\n"; | |
938 | print FILE "# All user modifications will be overwritten.\n\n"; | |
939 | ||
940 | # Add all user defined addresses to the whitelist. | |
941 | # | |
942 | # Check if the hash contains any elements. | |
943 | if (keys (%ignored)) { | |
944 | # Loop through the entire hash and write the host/network | |
945 | # and remark to the ignore file. | |
946 | while ( (my $key) = each %ignored) { | |
947 | my $address = $ignored{$key}[0]; | |
948 | my $remark = $ignored{$key}[1]; | |
949 | my $status = $ignored{$key}[2]; | |
950 | ||
951 | # Check if the status of the entry is "enabled". | |
952 | if ($status eq "enabled") { | |
953 | # Check if the address/network is valid. | |
954 | if ((&General::validip($address)) || (&General::validipandmask($address))) { | |
955 | # Write rule line to the file to pass any traffic from this IP | |
956 | print FILE "pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n"; | |
957 | ||
958 | # Increment sid. | |
959 | $sid++; | |
960 | } | |
961 | } | |
962 | } | |
963 | } | |
964 | ||
965 | close(FILE); | |
966 | } | |
967 | ||
1fedede6 SS |
968 | # |
969 | ## Function to set correct ownership for single files and directories. | |
970 | # | |
971 | ||
972 | sub set_ownership($) { | |
973 | my ($target) = @_; | |
974 | ||
975 | # User and group of the WUI. | |
976 | my $uname = "nobody"; | |
977 | my $grname = "nobody"; | |
978 | ||
979 | # The chown function implemented in perl requies the user and group as nummeric id's. | |
980 | my $uid = getpwnam($uname); | |
981 | my $gid = getgrnam($grname); | |
982 | ||
983 | # Check if the given target exists. | |
984 | unless ($target) { | |
985 | # Stop the script and print error message. | |
986 | die "The $target does not exist. Cannot change the ownership!\n"; | |
987 | } | |
988 | ||
989 | # Check weather the target is a file or directory. | |
990 | if (-f $target) { | |
991 | # Change ownership ot the single file. | |
992 | chown($uid, $gid, "$target"); | |
993 | } elsif (-d $target) { | |
994 | # Do a directory listing. | |
995 | opendir(DIR, $target) or die $!; | |
996 | # Loop through the direcory. | |
997 | while (my $file = readdir(DIR)) { | |
998 | ||
999 | # We only want files. | |
1000 | next unless (-f "$target/$file"); | |
1001 | ||
1002 | # Set correct ownership for the files. | |
1003 | chown($uid, $gid, "$target/$file"); | |
1004 | } | |
1005 | ||
1006 | closedir(DIR); | |
1007 | ||
1008 | # Change ownership of the directory. | |
1009 | chown($uid, $gid, "$target"); | |
1010 | } | |
1011 | } | |
77c31301 SS |
1012 | |
1013 | # | |
1014 | ## Function to read-in the aliases file and returns all configured and enabled aliases. | |
1015 | # | |
1016 | sub get_aliases() { | |
1017 | # Location of the aliases file. | |
1018 | my $aliases_file = "${General::swroot}/ethernet/aliases"; | |
1019 | ||
1020 | # Array to store the aliases. | |
1021 | my @aliases; | |
1022 | ||
1023 | # Check if the file is empty. | |
1024 | if (-z $aliases_file) { | |
1025 | # Abort nothing to do. | |
1026 | return; | |
1027 | } | |
1028 | ||
1029 | # Open the aliases file. | |
1030 | open(ALIASES, $aliases_file) or die "Could not open $aliases_file. $!\n"; | |
1031 | ||
1032 | # Loop through the file content. | |
1033 | while (my $line = <ALIASES>) { | |
1034 | # Remove newlines. | |
1035 | chomp($line); | |
1036 | ||
1037 | # Splitt line content into single chunks. | |
1038 | my ($address, $state, $remark) = split(/\,/, $line); | |
1039 | ||
1040 | # Check if the state of the current processed alias is "on". | |
1041 | if ($state eq "on") { | |
1042 | # Check if the address is valid. | |
1043 | if(&Network::check_ip_address($address)) { | |
1044 | # Add the alias to the array of aliases. | |
1045 | push(@aliases, $address); | |
1046 | } | |
1047 | } | |
1048 | } | |
1049 | ||
1050 | # Close file handle. | |
1051 | close(ALIASES); | |
1052 | ||
1053 | # Return the array. | |
1054 | return @aliases; | |
1055 | } | |
1056 | ||
de8e1e5b SS |
1057 | # |
1058 | ## Function to grab the current assigned IP-address on red. | |
1059 | # | |
1060 | sub get_red_address() { | |
1061 | # File, which contains the current IP-address of the red interface. | |
1062 | my $file = "${General::swroot}/red/local-ipaddress"; | |
1063 | ||
1064 | # Check if the file exists. | |
1065 | if (-e $file) { | |
1066 | # Open the given file. | |
1067 | open(FILE, "$file") or die "Could not open $file."; | |
1068 | ||
1069 | # Obtain the address from the first line of the file. | |
1070 | my $address = <FILE>; | |
1071 | ||
1072 | # Close filehandle | |
1073 | close(FILE); | |
1074 | ||
1075 | # Remove newlines. | |
1076 | chomp $address; | |
1077 | ||
1078 | # Check if the grabbed address is valid. | |
1079 | if (&General::validip($address)) { | |
1080 | # Return the address. | |
1081 | return $address; | |
1082 | } | |
1083 | } | |
1084 | ||
1085 | # Return nothing. | |
1086 | return; | |
1087 | } | |
77c31301 | 1088 | |
8076deba SS |
1089 | # |
1090 | ## Function to write the lock file for locking the WUI, while | |
1091 | ## the autoupdate script runs. | |
1092 | # | |
1093 | sub lock_ids_page() { | |
1094 | # Call subfunction to create the file. | |
1095 | &create_empty_file($ids_page_lock_file); | |
1096 | } | |
1097 | ||
1098 | # | |
1099 | ## Function to release the lock of the WUI, again. | |
1100 | # | |
1101 | sub unlock_ids_page() { | |
1102 | # Delete lock file. | |
1103 | unlink($ids_page_lock_file); | |
1104 | } | |
1105 | ||
8dcebe53 | 1106 | 1; |