]>
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 | |
eea2670b | 31 | # Location and name of the tarball which contains the ruleset. |
164eab66 | 32 | our $rulestarball = "/var/tmp/idsrules.tar.gz"; |
eea2670b | 33 | |
3983aebd | 34 | # File to store any errors, which also will be read and displayed by the wui. |
77910792 | 35 | our $storederrorfile = "/tmp/ids_storederror"; |
3983aebd | 36 | |
298ef5ba | 37 | # Location where the rulefiles are stored. |
164eab66 | 38 | our $rulespath = "/etc/suricata/rules"; |
298ef5ba | 39 | |
bce84f39 SS |
40 | # File which contains a list of all supported ruleset sources. |
41 | # (Sourcefire, Emergingthreads, etc..) | |
42 | our $rulesetsourcesfile = "$settingsdir/ruleset-sources"; | |
43 | ||
796eea21 SS |
44 | # The pidfile of the IDS. |
45 | our $idspidfile = "/var/run/suricata.pid"; | |
46 | ||
5240a809 SS |
47 | # Location of suricatactrl. |
48 | my $suricatactrl = "/usr/local/bin/suricatactrl"; | |
49 | ||
50 | # Array with allowed commands of suricatactrl. | |
330759d8 | 51 | my @suricatactrl_cmds = ( 'start', 'stop', 'restart', 'reload', 'fix-rules-dir' ); |
5240a809 | 52 | |
8dcebe53 SS |
53 | # |
54 | ## Function for checking if at least 300MB of free disk space are available | |
55 | ## on the "/var" partition. | |
56 | # | |
57 | sub checkdiskspace () { | |
58 | # Call diskfree to gather the free disk space of /var. | |
59 | my @df = `/bin/df -B M /var`; | |
60 | ||
61 | # Loop through the output. | |
62 | foreach my $line (@df) { | |
63 | # Ignore header line. | |
64 | next if $line =~ m/^Filesystem/; | |
65 | ||
66 | # Search for a line with the device information. | |
67 | if ($line =~ m/dev/ ) { | |
68 | # Split the line into single pieces. | |
69 | my @values = split(' ', $line); | |
70 | my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values; | |
71 | ||
72 | # Check if the available disk space is more than 300MB. | |
73 | if ($available < 300) { | |
434001d0 SS |
74 | # Log error to syslog. |
75 | &_log_to_syslog("Not enough free disk space on /var. Only $available MB from 300 MB available."); | |
8dcebe53 | 76 | |
434001d0 SS |
77 | # Exit function and return "1" - False. |
78 | return 1; | |
8dcebe53 SS |
79 | } |
80 | } | |
81 | } | |
82 | ||
83 | # Everything okay, return nothing. | |
84 | return; | |
85 | } | |
86 | ||
eea2670b SS |
87 | # |
88 | ## This function is responsible for downloading the configured snort ruleset. | |
89 | ## | |
90 | ## * At first it obtains from the stored snortsettings which ruleset should be downloaded. | |
91 | ## * The next step is to get the download locations for all available rulesets. | |
92 | ## * After that, the function will check if an upstream proxy should be used and grab the settings. | |
93 | ## * The last step will be to generate the final download url, by obtaining the URL for the desired | |
94 | ## ruleset, add the settings for the upstream proxy and final grab the rules tarball from the server. | |
95 | # | |
96 | sub downloadruleset { | |
97 | # Get snort settings. | |
98 | my %snortsettings=(); | |
02844177 | 99 | &General::readhash("$settingsdir/settings", \%snortsettings); |
eea2670b | 100 | |
be52c68a SS |
101 | # Check if a ruleset has been configured. |
102 | unless($snortsettings{'RULES'}) { | |
103 | # Log that no ruleset has been configured and abort. | |
104 | &_log_to_syslog("No ruleset source has been configured."); | |
105 | ||
106 | # Return "1". | |
107 | return 1; | |
108 | } | |
109 | ||
eea2670b SS |
110 | # Get all available ruleset locations. |
111 | my %rulesetsources=(); | |
bce84f39 | 112 | &General::readhash($rulesetsourcesfile, \%rulesetsources); |
eea2670b SS |
113 | |
114 | # Read proxysettings. | |
115 | my %proxysettings=(); | |
116 | &General::readhash("${General::swroot}/proxy/settings", \%proxysettings); | |
117 | ||
118 | # Load required perl module to handle the download. | |
119 | use LWP::UserAgent; | |
120 | ||
121 | # Init the download module. | |
122 | my $downloader = LWP::UserAgent->new; | |
123 | ||
124 | # Set timeout to 10 seconds. | |
125 | $downloader->timeout(10); | |
126 | ||
127 | # Check if an upstream proxy is configured. | |
128 | if ($proxysettings{'UPSTREAM_PROXY'}) { | |
129 | my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/); | |
130 | my $proxy_url; | |
131 | ||
132 | # Check if we got a peer. | |
133 | if ($peer) { | |
134 | $proxy_url = "http://"; | |
135 | ||
136 | # Check if the proxy requires authentication. | |
137 | if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) { | |
138 | $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@"; | |
139 | } | |
140 | ||
141 | # Add proxy server address and port. | |
142 | $proxy_url .= "$peer\:$peerport"; | |
143 | } else { | |
434001d0 SS |
144 | # Log error message and break. |
145 | &_log_to_syslog("Could not proper configure the proxy server access."); | |
146 | ||
147 | # Return "1" - false. | |
148 | return 1; | |
eea2670b SS |
149 | } |
150 | ||
151 | # Setup proxy settings. | |
152 | $downloader->proxy('http', $proxy_url); | |
153 | } | |
154 | ||
155 | # Grab the right url based on the configured vendor. | |
156 | my $url = $rulesetsources{$snortsettings{'RULES'}}; | |
157 | ||
158 | # Check if the vendor requires an oinkcode and add it if needed. | |
159 | $url =~ s/\<oinkcode\>/$snortsettings{'OINKCODE'}/g; | |
160 | ||
161 | # Abort if no url could be determined for the vendor. | |
162 | unless ($url) { | |
434001d0 SS |
163 | # Log error and abort. |
164 | &_log_to_syslog("Unable to gather a download URL for the selected ruleset."); | |
165 | return 1; | |
eea2670b SS |
166 | } |
167 | ||
168 | # Pass the requested url to the downloader. | |
169 | my $request = HTTP::Request->new(GET => $url); | |
170 | ||
171 | # Perform the request and save the output into the "$rulestarball" file. | |
172 | my $response = $downloader->request($request, $rulestarball); | |
173 | ||
174 | # Check if there was any error. | |
175 | unless ($response->is_success) { | |
88daf7eb SS |
176 | # Obtain error. |
177 | my $error = $response->content; | |
178 | ||
434001d0 | 179 | # Log error message. |
88daf7eb | 180 | &_log_to_syslog("Unable to download the ruleset. \($error\)"); |
434001d0 SS |
181 | |
182 | # Return "1" - false. | |
183 | return 1; | |
eea2670b SS |
184 | } |
185 | ||
186 | # If we got here, everything worked fine. Return nothing. | |
187 | return; | |
188 | } | |
8dcebe53 | 189 | |
25f5cb0d SS |
190 | # |
191 | ## A tiny wrapper function to call the oinkmaster script. | |
192 | # | |
193 | sub oinkmaster () { | |
330759d8 SS |
194 | # Check if the files in rulesdir have the correct permissions. |
195 | &_check_rulesdir_permissions(); | |
196 | ||
0e40e1e7 SS |
197 | # Load perl module to talk to the kernel syslog. |
198 | use Sys::Syslog qw(:DEFAULT setlogsock); | |
199 | ||
200 | # Establish the connection to the syslog service. | |
201 | openlog('oinkmaster', 'cons,pid', 'user'); | |
202 | ||
25f5cb0d | 203 | # Call oinkmaster to generate ruleset. |
d9711d91 | 204 | 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 |
205 | |
206 | # Log output of oinkmaster to syslog. | |
207 | while(<OINKMASTER>) { | |
208 | # The syslog function works best with an array based input, | |
209 | # so generate one before passing the message details to syslog. | |
210 | my @syslog = ("INFO", "$_"); | |
211 | ||
212 | # Send the log message. | |
213 | syslog(@syslog); | |
214 | } | |
215 | ||
216 | # Close the pipe to oinkmaster process. | |
217 | close(OINKMASTER); | |
218 | ||
219 | # Close the log handle. | |
220 | closelog(); | |
25f5cb0d SS |
221 | } |
222 | ||
3983aebd SS |
223 | # |
224 | ## Function to do all the logging stuff if the downloading or updating of the ruleset fails. | |
225 | # | |
226 | sub log_error ($) { | |
227 | my ($error) = @_; | |
228 | ||
229 | # Remove any newline. | |
230 | chomp($error); | |
231 | ||
eb5592c1 SS |
232 | # Call private function to log the error message to syslog. |
233 | &_log_to_syslog($error); | |
234 | ||
3983aebd SS |
235 | # Call private function to write/store the error message in the storederrorfile. |
236 | &_store_error_message($error); | |
237 | } | |
238 | ||
eb5592c1 SS |
239 | # |
240 | ## Function to log a given error message to the kernel syslog. | |
241 | # | |
242 | sub _log_to_syslog ($) { | |
243 | my ($message) = @_; | |
244 | ||
245 | # Load perl module to talk to the kernel syslog. | |
246 | use Sys::Syslog qw(:DEFAULT setlogsock); | |
247 | ||
248 | # The syslog function works best with an array based input, | |
249 | # so generate one before passing the message details to syslog. | |
250 | my @syslog = ("ERR", "<ERROR> $message"); | |
251 | ||
252 | # Establish the connection to the syslog service. | |
253 | openlog('oinkmaster', 'cons,pid', 'user'); | |
254 | ||
255 | # Send the log message. | |
256 | syslog(@syslog); | |
257 | ||
258 | # Close the log handle. | |
259 | closelog(); | |
260 | } | |
261 | ||
3983aebd SS |
262 | # |
263 | ## Private function to write a given error message to the storederror file. | |
264 | # | |
265 | sub _store_error_message ($) { | |
266 | my ($message) = @_; | |
267 | ||
268 | # Remove any newline. | |
269 | chomp($message); | |
270 | ||
271 | # Open file for writing. | |
272 | open (ERRORFILE, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n"; | |
273 | ||
274 | # Write error to file. | |
275 | print ERRORFILE "$message\n"; | |
276 | ||
277 | # Close file. | |
278 | close (ERRORFILE); | |
279 | } | |
280 | ||
1cae702c SS |
281 | # |
282 | ## Function to get a list of all available network zones. | |
283 | # | |
284 | sub get_available_network_zones () { | |
285 | # Get netsettings. | |
286 | my %netsettings = (); | |
287 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
288 | ||
289 | # Obtain the configuration type from the netsettings hash. | |
290 | my $config_type = $netsettings{'CONFIG_TYPE'}; | |
291 | ||
292 | # Hash which contains the conversation from the config mode | |
293 | # to the existing network interface names. They are stored like | |
294 | # an array. | |
295 | # | |
296 | # Mode "0" red is a modem and green | |
297 | # Mode "1" red is a netdev and green | |
298 | # Mode "2" red, green and orange | |
299 | # Mode "3" red, green and blue | |
300 | # Mode "4" red, green, blue, orange | |
301 | my %config_type_to_interfaces = ( | |
302 | "0" => [ "red", "green" ], | |
303 | "1" => [ "red", "green" ], | |
304 | "2" => [ "red", "green", "orange" ], | |
305 | "3" => [ "red", "green", "blue" ], | |
306 | "4" => [ "red", "green", "blue", "orange" ] | |
307 | ); | |
308 | ||
309 | # Obtain and dereference the corresponding network interaces based on the read | |
310 | # network config type. | |
311 | my @network_zones = @{ $config_type_to_interfaces{$config_type} }; | |
312 | ||
313 | # Return them. | |
314 | return @network_zones; | |
315 | } | |
316 | ||
796eea21 SS |
317 | # |
318 | ## Function to check if the IDS is running. | |
319 | # | |
320 | sub ids_is_running () { | |
321 | if(-f $idspidfile) { | |
322 | # Open PID file for reading. | |
323 | open(PIDFILE, "$idspidfile") or die "Could not open $idspidfile. $!\n"; | |
324 | ||
325 | # Grab the process-id. | |
326 | my $pid = <PIDFILE>; | |
327 | ||
328 | # Close filehandle. | |
329 | close(PIDFILE); | |
330 | ||
331 | # Remove any newline. | |
332 | chomp($pid); | |
333 | ||
334 | # Check if a directory for the process-id exists in proc. | |
335 | if(-d "/proc/$pid") { | |
336 | # The IDS daemon is running return the process id. | |
337 | return $pid; | |
338 | } | |
339 | } | |
340 | ||
341 | # Return nothing - IDS is not running. | |
342 | return; | |
343 | } | |
344 | ||
5240a809 SS |
345 | # |
346 | ## Function to call suricatactrl binary with a given command. | |
347 | # | |
348 | sub call_suricatactrl ($) { | |
349 | # Get called option. | |
350 | my ($option) = @_; | |
351 | ||
352 | # Loop through the array of supported commands and check if | |
353 | # the given one is part of it. | |
354 | foreach my $cmd (@suricatactrl_cmds) { | |
355 | # Skip current command unless the given one has been found. | |
356 | next unless($cmd eq $option); | |
357 | ||
358 | # Call the suricatactrl binary and pass the requrested | |
359 | # option to it. | |
360 | system("$suricatactrl $option &>/dev/null"); | |
361 | ||
362 | # Return "1" - True. | |
363 | return 1; | |
364 | } | |
365 | ||
366 | # Command not found - return nothing. | |
367 | return; | |
368 | } | |
369 | ||
308ba5e7 SS |
370 | # |
371 | ## Function to create a new empty file. | |
372 | # | |
373 | sub create_empty_file($) { | |
374 | my ($file) = @_; | |
375 | ||
376 | # Check if the given file exists. | |
377 | if(-e $file) { | |
378 | # Do nothing to prevent from overwriting existing files. | |
379 | return; | |
380 | } | |
381 | ||
382 | # Open the file for writing. | |
383 | open(FILE, ">$file") or die "Could not write to $file. $!\n"; | |
384 | ||
385 | # Close file handle. | |
386 | close(FILE); | |
387 | ||
388 | # Return true. | |
389 | return 1; | |
390 | } | |
391 | ||
330759d8 SS |
392 | # |
393 | ## Private function to check if the file permission of the rulespath are correct. | |
394 | ## If not, call suricatactrl to fix them. | |
395 | # | |
396 | sub _check_rulesdir_permissions() { | |
e568796b SS |
397 | # Check if the rulepath main directory is writable. |
398 | unless (-W $rulespath) { | |
399 | # If not call suricatctrl to fix it. | |
400 | &call_suricatactrl("fix-rules-dir"); | |
401 | } | |
402 | ||
330759d8 SS |
403 | # Open snort rules directory and do a directory listing. |
404 | opendir(DIR, $rulespath) or die $!; | |
405 | # Loop through the direcory. | |
406 | while (my $file = readdir(DIR)) { | |
407 | # We only want files. | |
408 | next unless (-f "$rulespath/$file"); | |
409 | ||
410 | # Check if the file is writable by the user. | |
411 | if (-W "$rulespath/$file") { | |
412 | # Everything is okay - go on to the next file. | |
413 | next; | |
414 | } else { | |
415 | # There are wrong permissions, call suricatactrl to fix it. | |
416 | &call_suricatactrl("fix-rules-dir"); | |
417 | } | |
418 | } | |
419 | } | |
420 | ||
8dcebe53 | 421 | 1; |