2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2019 IPFire Development Team <info@ipfire.org> #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
24 require '/var/ipfire/general-functions.pl';
25 require "${General::swroot}/ids-functions.pl";
27 # Snort settings file, which contains the settings from the WUI.
28 my $snort_settings_file = "${General::swroot}/snort/settings";
30 # Main snort config file.
31 my $snort_config_file = "/etc/snort/snort.conf";
33 # Snort rules tarball.
34 my $snort_rules_tarball = "/var/tmp/snortrules.tar.gz";
37 ## Step 1: Convert snort user and group to suricata if exist.
40 # Check if the snort user exists.
41 if (getpwnam("snort")) {
45 '-l', 'suricata', 'snort'
48 system(@command) == 0 or die "Could not change username: @command failed: $?\n";
50 # Adjust home directory.
53 '-d', "/var/log/suricata",
57 system(@command) == 0 or die "Failed to adjust home directory: @command failed: $?\n";
60 # Check if the snort group exists.
61 if (getgrnam("snort")) {
65 '-n', 'suricata', 'snort'
68 system(@command) == 0 or die "Could not rename groupname: @command failed: $?\n";
72 ## Step 2: Setup directory and file layout, if not present and set correct
73 ## ownership. The converter runs as a privileged user, but the files
74 ## needs to be full access-able by the WUI user and group (nobody:nobody).
77 # Check if the settings directory exists.
78 unless (-d
$IDS::settingsdir
) {
79 # Create the directory.
80 mkdir($IDS::settingsdir
);
83 # Check if the rules directory exists.
84 unless (-d
$IDS::rulespath
) {
85 # Create the directory.
86 mkdir($IDS::rulespath
);
89 # Create file layout, if not exists yet.
90 &IDS
::check_and_create_filelayout
();
92 # Set correct ownership for settingsdir and rulespath.
93 &IDS
::set_ownership
("$IDS::settingsdir");
94 &IDS
::set_ownership
("$IDS::rulespath");
96 # Check if a snort settings file exists.
97 unless( -f
"$snort_settings_file") {
98 print "$snort_settings_file not found - Nothing to do. Exiting!\n";
102 # Check if the snort settings file is empty.
103 if (-z
"$snort_settings_file") {
104 print "$snort_settings_file is empty - Nothing to do. Exiting!\n";
109 ## Step 3: Import snort settings and convert to the required format for the new IDS
113 # Hash which contains the "old" snort settings.
116 # Hash which contains the IDS (suricata) settings.
118 # Add default value for MONITOR_TRAFFIC_ONLY which will be "on"
119 # when migrating from snort to the new IDS.
121 "MONITOR_TRAFFIC_ONLY" => "on",
124 # Hash which contains the RULES settings.
126 # Set default value for UPDATE_INTERVAL to weekly.
127 my %rulessettings = (
128 "AUTOUPDATE_INTERVAL" => "weekly",
131 # Get all available network zones.
132 my @network_zones = &IDS
::get_available_network_zones
();
134 # Read-in snort settings file.
135 &General
::readhash
("$snort_settings_file", \
%snortsettings);
137 # Loop through the array of network zones.
138 foreach my $zone (@network_zones) {
139 # Convert current zone into upper case.
140 my $zone_upper = uc($zone);
142 # Check if the current network zone is "red".
144 # Check if snort was enabled and enabled on red.
145 if ($snortsettings{"ENABLE_SNORT"} eq "on") {
147 $idssettings{"ENABLE_IDS"} = "on";
149 # Enable the IDS on RED.
150 $idssettings{"ENABLE_IDS_$zone_upper"} = "on";
153 # Check if snort was enabled on the current zone.
154 if ($snortsettings{"ENABLE_SNORT_$zone_upper"} eq "on") {
155 # Enable the IDS on this zone too.
156 $idssettings{"ENABLE_IDS_$zone_upper"} = "on";
161 # Grab the choosen ruleset from snort settings hash and store it in the rules
163 $rulessettings{"RULES"} = $snortsettings{"RULES"};
165 # Check if an oinkcode has been provided.
166 if($snortsettings{"OINKCODE"}) {
167 # Take the oinkcode from snort settings hash and store it in the rules
169 $rulessettings{"OINKCODE"} = $snortsettings{"OINKCODE"};
173 ## Step 4: Import guardian settings and whitelist if the addon is installed.
176 # Pakfire meta file for owncloud.
177 # (File exists when the addon is installed.)
178 my $guardian_meta = "/opt/pakfire/db/installed/meta-guardian";
180 # Check if the guardian addon is installed.
181 if (-f
$guardian_meta) {
182 # File which contains the taken setting for guardian.
183 my $guardian_settings_file = "${General::swroot}/guardian/settings";
185 # File which contains the white-listed hosts.
186 my $guardian_ignored_file = "${General::swroot}/guardian/ignored";
188 # Hash which will contain the settings of guardian.
189 my %guardiansettings;
191 # Check if the settings file of guardian is empty.
192 unless (-z
$guardian_settings_file) {
194 &General
::readhash
("$guardian_settings_file", \
%guardiansettings);
197 # Check if guardian is not configured to take actions on snort events.
198 if ($guardiansettings{"GUARDIAN_MONITOR_SNORT"} eq "on") {
199 # Change the IDS into MONITOR_TRAFFIC_ONLY mode.
200 $idssettings{"MONITOR_TRAFFIC_ONLY"} = "off";
203 # Check if guardian has any white-listed hosts configured.
204 unless (-z
$guardian_ignored_file) {
205 # Temporary hash to store the ignored hosts.
208 # Read-in white-listed hosts and store them in the hash.
209 &General
::readhasharray
($guardian_ignored_file, \
%ignored_hosts);
211 # Write-out the white-listed hosts for the IDS system.
212 &General
::writehasharray
($IDS::ignored_file
, \
%ignored_hosts);
214 # Call subfunction to generate the file for white-listing the hosts.
215 &IDS
::generate_ignored_file
();
221 ## Step 5: Save IDS and rules settings.
224 # Write IDS settings.
225 &General
::writehash
("$IDS::ids_settings_file", \
%idssettings);
227 # Write rules settings.
228 &General
::writehash
("$IDS::rules_settings_file", \
%rulessettings);
231 ## Step 6: Generate and write the file to modify the ruleset.
234 # Call subfunction and pass the desired IDS action.
235 &IDS
::write_modify_sids_file
();
237 # Set correct ownership.
238 &IDS
::set_ownership
("$IDS::modify_sids_file");
241 ## Step 7: Move rulestarball to its new location.
244 # Check if a rulestarball has been downloaded yet.
245 if (-f
$snort_rules_tarball) {
246 # Load perl module which contains the move command.
249 # Move the rulestarball to the new location.
250 move
($snort_rules_tarball, $IDS::rulestarball
);
252 # Set correct ownership.
253 &IDS
::set_ownership
("$IDS::rulestarball");
255 # In case no tarball is present, try to download the ruleset.
257 # Check if enought disk space is available.
258 if(&IDS
::checkdiskspace
()) {
259 # Print error message.
260 print "Could not download ruleset - Not enough free diskspace available.\n";
262 # Call the download function and grab the new ruleset.
263 &IDS
::downloadruleset
();
268 ## Step 8: Call oinkmaster to extract and setup the rules structures.
271 # Check if a rulestarball is present.
272 if (-f
$IDS::rulestarball
) {
273 # Launch oinkmaster by calling the subfunction.
276 # Set correct ownership for the rulesdir and files.
277 &IDS
::set_ownership
("$IDS::rulespath");
281 ## Step 9: Generate file for the HOME Net.
284 # Call subfunction to generate the file.
285 &IDS
::generate_home_net_file
();
287 # Set correct ownership for the homenet file.
288 &IDS
::set_ownership
("$IDS::homenet_file");
291 ## Step 10: Generate file for the DNS servers.
294 # Call subfunction to generate the file.
295 &IDS
::generate_dns_servers_file
();
297 # Set correct ownership for the dns_servers_file.
298 &IDS
::set_ownership
("$IDS::dns_servers_file");
301 ## Step 11: Setup automatic ruleset updates.
304 # Check if a ruleset is configured.
305 if($rulessettings{"RULES"}) {
306 # Call suricatactrl and setup the periodic update mechanism.
307 &IDS
::call_suricatactrl
("cron", $rulessettings{'AUTOUPDATE_INTERVAL'});
311 ## Step 12: Grab used ruleset files from snort config file and convert
312 ## them into the new format.
315 # Check if the snort config file exists.
316 unless (-f
$snort_config_file) {
317 print "$snort_config_file does not exist - Nothing to do. Exiting!\n";
321 # Array to store the enabled rules files.
322 my @enabled_rule_files;
324 # Open snort config file.
325 open(SNORTCONF
, $snort_config_file) or die "Could not open $snort_config_file. $!\n";
327 # Loop through the file content.
328 while (my $line = <SNORTCONF
>) {
330 next if ($line =~ /\#/);
333 next if ($line =~ /^\s*$/);
338 # Check for a line with .rules
339 if ($line =~ /\.rules$/) {
340 # Parse out rule file name
341 my $rulefile = $line;
342 $rulefile =~ s/\$RULE_PATH\///i
;
343 $rulefile =~ s/ ?include ?//i;
345 # Add the enabled rulefile to the array of enabled rule files.
346 push(@enabled_rule_files, $rulefile);
353 # Pass the array of enabled rule files to the subfunction and write the file.
354 &IDS
::write_used_rulefiles_file
(@enabled_rule_files);
357 ## Step 13: Start the IDS if enabled.
360 # Check if the IDS should be started.
361 if($idssettings{"ENABLE_IDS"} eq "on") {
362 # Call suricatactrl and launch the IDS.
363 &IDS
::call_suricatactrl
("start");