]> git.ipfire.org Git - ipfire-2.x.git/blob - config/cfgroot/ids-functions.pl
ids-functions.pl: Add function to check if the IDS is running
[ipfire-2.x.git] / config / cfgroot / ids-functions.pl
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';
27
28 # Location where all config and settings files are stored.
29 our $settingsdir = "${General::swroot}/suricata";
30
31 # Location and name of the tarball which contains the ruleset.
32 our $rulestarball = "/var/tmp/idsrules.tar.gz";
33
34 # File to store any errors, which also will be read and displayed by the wui.
35 our $storederrorfile = "/tmp/ids_storederror";
36
37 # Location where the rulefiles are stored.
38 our $rulespath = "/etc/suricata/rules";
39
40 # File which contains a list of all supported ruleset sources.
41 # (Sourcefire, Emergingthreads, etc..)
42 our $rulesetsourcesfile = "$settingsdir/ruleset-sources";
43
44 # The pidfile of the IDS.
45 our $idspidfile = "/var/run/suricata.pid";
46
47 #
48 ## Function for checking if at least 300MB of free disk space are available
49 ## on the "/var" partition.
50 #
51 sub checkdiskspace () {
52 # Call diskfree to gather the free disk space of /var.
53 my @df = `/bin/df -B M /var`;
54
55 # Loop through the output.
56 foreach my $line (@df) {
57 # Ignore header line.
58 next if $line =~ m/^Filesystem/;
59
60 # Search for a line with the device information.
61 if ($line =~ m/dev/ ) {
62 # Split the line into single pieces.
63 my @values = split(' ', $line);
64 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
65
66 # Check if the available disk space is more than 300MB.
67 if ($available < 300) {
68 # Log error to syslog.
69 &_log_to_syslog("Not enough free disk space on /var. Only $available MB from 300 MB available.");
70
71 # Exit function and return "1" - False.
72 return 1;
73 }
74 }
75 }
76
77 # Everything okay, return nothing.
78 return;
79 }
80
81 #
82 ## This function is responsible for downloading the configured snort ruleset.
83 ##
84 ## * At first it obtains from the stored snortsettings which ruleset should be downloaded.
85 ## * The next step is to get the download locations for all available rulesets.
86 ## * After that, the function will check if an upstream proxy should be used and grab the settings.
87 ## * The last step will be to generate the final download url, by obtaining the URL for the desired
88 ## ruleset, add the settings for the upstream proxy and final grab the rules tarball from the server.
89 #
90 sub downloadruleset {
91 # Get snort settings.
92 my %snortsettings=();
93 &General::readhash("$settingsdir/settings", \%snortsettings);
94
95 # Get all available ruleset locations.
96 my %rulesetsources=();
97 &General::readhash($rulesetsourcesfile, \%rulesetsources);
98
99 # Read proxysettings.
100 my %proxysettings=();
101 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
102
103 # Load required perl module to handle the download.
104 use LWP::UserAgent;
105
106 # Init the download module.
107 my $downloader = LWP::UserAgent->new;
108
109 # Set timeout to 10 seconds.
110 $downloader->timeout(10);
111
112 # Check if an upstream proxy is configured.
113 if ($proxysettings{'UPSTREAM_PROXY'}) {
114 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
115 my $proxy_url;
116
117 # Check if we got a peer.
118 if ($peer) {
119 $proxy_url = "http://";
120
121 # Check if the proxy requires authentication.
122 if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) {
123 $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@";
124 }
125
126 # Add proxy server address and port.
127 $proxy_url .= "$peer\:$peerport";
128 } else {
129 # Log error message and break.
130 &_log_to_syslog("Could not proper configure the proxy server access.");
131
132 # Return "1" - false.
133 return 1;
134 }
135
136 # Setup proxy settings.
137 $downloader->proxy('http', $proxy_url);
138 }
139
140 # Grab the right url based on the configured vendor.
141 my $url = $rulesetsources{$snortsettings{'RULES'}};
142
143 # Check if the vendor requires an oinkcode and add it if needed.
144 $url =~ s/\<oinkcode\>/$snortsettings{'OINKCODE'}/g;
145
146 # Abort if no url could be determined for the vendor.
147 unless ($url) {
148 # Log error and abort.
149 &_log_to_syslog("Unable to gather a download URL for the selected ruleset.");
150 return 1;
151 }
152
153 # Pass the requested url to the downloader.
154 my $request = HTTP::Request->new(GET => $url);
155
156 # Perform the request and save the output into the "$rulestarball" file.
157 my $response = $downloader->request($request, $rulestarball);
158
159 # Check if there was any error.
160 unless ($response->is_success) {
161 # Log error message.
162 &_log_to_syslog("Unable to download the ruleset. $response->status_line");
163
164 # Return "1" - false.
165 return 1;
166 }
167
168 # If we got here, everything worked fine. Return nothing.
169 return;
170 }
171
172 #
173 ## A tiny wrapper function to call the oinkmaster script.
174 #
175 sub oinkmaster () {
176 # Load perl module to talk to the kernel syslog.
177 use Sys::Syslog qw(:DEFAULT setlogsock);
178
179 # Establish the connection to the syslog service.
180 openlog('oinkmaster', 'cons,pid', 'user');
181
182 # Call oinkmaster to generate ruleset.
183 open(OINKMASTER, "/usr/local/bin/oinkmaster.pl -v -s -u file://$rulestarball -C $settingsdir/oinkmaster.conf -o $rulespath|");
184
185 # Log output of oinkmaster to syslog.
186 while(<OINKMASTER>) {
187 # The syslog function works best with an array based input,
188 # so generate one before passing the message details to syslog.
189 my @syslog = ("INFO", "$_");
190
191 # Send the log message.
192 syslog(@syslog);
193 }
194
195 # Close the pipe to oinkmaster process.
196 close(OINKMASTER);
197
198 # Close the log handle.
199 closelog();
200 }
201
202 #
203 ## Function to do all the logging stuff if the downloading or updating of the ruleset fails.
204 #
205 sub log_error ($) {
206 my ($error) = @_;
207
208 # Remove any newline.
209 chomp($error);
210
211 # Call private function to log the error message to syslog.
212 &_log_to_syslog($error);
213
214 # Call private function to write/store the error message in the storederrorfile.
215 &_store_error_message($error);
216 }
217
218 #
219 ## Function to log a given error message to the kernel syslog.
220 #
221 sub _log_to_syslog ($) {
222 my ($message) = @_;
223
224 # Load perl module to talk to the kernel syslog.
225 use Sys::Syslog qw(:DEFAULT setlogsock);
226
227 # The syslog function works best with an array based input,
228 # so generate one before passing the message details to syslog.
229 my @syslog = ("ERR", "<ERROR> $message");
230
231 # Establish the connection to the syslog service.
232 openlog('oinkmaster', 'cons,pid', 'user');
233
234 # Send the log message.
235 syslog(@syslog);
236
237 # Close the log handle.
238 closelog();
239 }
240
241 #
242 ## Private function to write a given error message to the storederror file.
243 #
244 sub _store_error_message ($) {
245 my ($message) = @_;
246
247 # Remove any newline.
248 chomp($message);
249
250 # Open file for writing.
251 open (ERRORFILE, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n";
252
253 # Write error to file.
254 print ERRORFILE "$message\n";
255
256 # Close file.
257 close (ERRORFILE);
258 }
259
260 #
261 ## Function to get a list of all available network zones.
262 #
263 sub get_available_network_zones () {
264 # Get netsettings.
265 my %netsettings = ();
266 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
267
268 # Obtain the configuration type from the netsettings hash.
269 my $config_type = $netsettings{'CONFIG_TYPE'};
270
271 # Hash which contains the conversation from the config mode
272 # to the existing network interface names. They are stored like
273 # an array.
274 #
275 # Mode "0" red is a modem and green
276 # Mode "1" red is a netdev and green
277 # Mode "2" red, green and orange
278 # Mode "3" red, green and blue
279 # Mode "4" red, green, blue, orange
280 my %config_type_to_interfaces = (
281 "0" => [ "red", "green" ],
282 "1" => [ "red", "green" ],
283 "2" => [ "red", "green", "orange" ],
284 "3" => [ "red", "green", "blue" ],
285 "4" => [ "red", "green", "blue", "orange" ]
286 );
287
288 # Obtain and dereference the corresponding network interaces based on the read
289 # network config type.
290 my @network_zones = @{ $config_type_to_interfaces{$config_type} };
291
292 # Return them.
293 return @network_zones;
294 }
295
296 #
297 ## Function to check if the IDS is running.
298 #
299 sub ids_is_running () {
300 if(-f $idspidfile) {
301 # Open PID file for reading.
302 open(PIDFILE, "$idspidfile") or die "Could not open $idspidfile. $!\n";
303
304 # Grab the process-id.
305 my $pid = <PIDFILE>;
306
307 # Close filehandle.
308 close(PIDFILE);
309
310 # Remove any newline.
311 chomp($pid);
312
313 # Check if a directory for the process-id exists in proc.
314 if(-d "/proc/$pid") {
315 # The IDS daemon is running return the process id.
316 return $pid;
317 }
318 }
319
320 # Return nothing - IDS is not running.
321 return;
322 }
323
324 1;