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