mirror of
https://github.com/vincentmli/bpfire.git
synced 2026-04-28 11:43:25 +02:00
convert-ids-modifications-files converter. This converter also will convert the used rulesfiles file for the providers. Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
131 lines
4.3 KiB
Perl
131 lines
4.3 KiB
Perl
#!/usr/bin/perl
|
|
###############################################################################
|
|
# #
|
|
# IPFire.org - A linux based firewall #
|
|
# Copyright (C) 2021 IPFire Development Team <info@ipfire.org> #
|
|
# #
|
|
# This program is free software: you can redistribute it and/or modify #
|
|
# it under the terms of the GNU General Public License as published by #
|
|
# the Free Software Foundation, either version 3 of the License, or #
|
|
# (at your option) any later version. #
|
|
# #
|
|
# This program is distributed in the hope that it will be useful, #
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
|
# GNU General Public License for more details. #
|
|
# #
|
|
# You should have received a copy of the GNU General Public License #
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
|
# #
|
|
###############################################################################
|
|
|
|
use strict;
|
|
|
|
require '/var/ipfire/general-functions.pl';
|
|
require '/var/ipfire/ids-functions.pl';
|
|
|
|
# Exit if there is no main oinkmaster config file anymore.
|
|
exit 0 unless (-f "$IDS::settingsdir/oinkmaster.conf");
|
|
|
|
# Get all supported providers.
|
|
my @providers = &IDS::get_ruleset_providers();
|
|
|
|
#
|
|
## Step 1: Convert used rules files.
|
|
#
|
|
|
|
# Loop through the array of known providers.
|
|
foreach my $provider (@providers) {
|
|
my %used_rulesfiles = ();
|
|
|
|
# Generate old filename which contained the used rulesfile.
|
|
my $old_used_rulesfiles_file = "$IDS::settingsdir/suricata-$provider\-used-rulefiles.yaml";
|
|
|
|
# Skip the provider if there is no used rulesfiles file available.
|
|
next unless (-f $old_used_rulesfiles_file);
|
|
|
|
# Open the used rulesfiles file.
|
|
open(FILE, "$old_used_rulesfiles_file");
|
|
|
|
# Read-in the file content.
|
|
my @file = <FILE>;
|
|
|
|
# Close file handle.
|
|
close(FILE);
|
|
|
|
# Loop through the file content.
|
|
foreach my $line(@file) {
|
|
chomp($line);
|
|
|
|
# Grab the used rulesfile name from the line.
|
|
if ($line =~ /^\s-\s(.*)/) {
|
|
my $rulesfile = $1;
|
|
|
|
# Add the used rulesfile to the has of used rulesfile for this provider.
|
|
$used_rulesfiles{$rulesfile} = "enabled";
|
|
}
|
|
}
|
|
|
|
# Get the filename for the new used rulesfiles file.
|
|
my $used_rulesfiles_file = &IDS::get_provider_used_rulesfiles_file($provider);
|
|
|
|
# Write the file.
|
|
&General::writehash("$used_rulesfiles_file", \%used_rulesfiles);
|
|
|
|
# Set the correct ownership for the new file.
|
|
&IDS::set_ownership("$used_rulesfiles_file");
|
|
}
|
|
|
|
#
|
|
## Step 2: Convert ruleset modifictaion files.
|
|
#
|
|
|
|
# Loop through the array of providers.
|
|
foreach my $provider (@providers) {
|
|
my %modifications = ();
|
|
|
|
# Generate old filename which hold the ruleset modifications.
|
|
my $old_modifications_file = "$IDS::settingsdir/oinkmaster\-$provider\-modified-sids.conf";
|
|
|
|
# Skip provider if there is no modifications file.
|
|
next unless (-f $old_modifications_file);
|
|
|
|
# Open modifications file.
|
|
open(FILE, "$old_modifications_file");
|
|
|
|
# Read-in file content.
|
|
my @file = <FILE>;
|
|
|
|
# Close file handle.
|
|
close(FILE);
|
|
|
|
# Loop through the file content.
|
|
foreach my $line (@file) {
|
|
chomp($line);
|
|
|
|
# Split line and assign to an temporary array.
|
|
my @tmp = split(/ /, $line);
|
|
|
|
# Assign nice human-readable variables.
|
|
my $action = $tmp[0];
|
|
my $sid = $tmp[1];
|
|
|
|
# Process stored rule action and assign to the modifications hash.
|
|
if ($action eq "enablesid") {
|
|
$modifications{$sid} = "enabled";
|
|
|
|
} elsif ($action eq "disablesid") {
|
|
$modifications{$sid} = "disabled";
|
|
}
|
|
}
|
|
|
|
# Get new filename which will hold the ruleset modifications for this provider.
|
|
my $new_modifications_file = &IDS::get_provider_ruleset_modifications_file($provider);
|
|
|
|
# Write new modifications file.
|
|
&General::writehash("$new_modifications_file", \%modifications);
|
|
|
|
# Set correct ownership for the new modifications file.
|
|
&IDS::set_ownership("$new_modifications_file");
|
|
}
|