From patchwork Wed Apr 6 19:12:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 5454 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4KYZ0V143mz3x1b for ; Wed, 6 Apr 2022 19:12:58 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4KYZ0R3Dlyz2cK; Wed, 6 Apr 2022 19:12:55 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4KYZ0R2FBMz2yw8; Wed, 6 Apr 2022 19:12:55 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4KYZ0P65tYz2xKQ for ; Wed, 6 Apr 2022 19:12:53 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4KYZ0P01mwzdN; Wed, 6 Apr 2022 19:12:52 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1649272373; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=C0711cqn8FYv5zxkMSEL4JaGVgMmJOtpJHb3ISbFuqk=; b=GCa4BqAQCDOech5w3Djtx2AEPL+TJclbKtl9T3DY5CjDXU49gCeK5WgQ8Nisvt2VC71KQv tUmbCjUuwvBsfFAA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1649272373; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=C0711cqn8FYv5zxkMSEL4JaGVgMmJOtpJHb3ISbFuqk=; b=BK8A5Xat78D55WAYEE60jodc8V9Jr49OlnAXj5Ob4s3j5NOptOM7OL/e5Gd/EMwluvCjnO A79JLnqWlnSzeWGP0gK7s/lrrMTixrtu7udj0yCcvawK/Kee8yeud5zXbuNrtrYf916QBG 8fMJ2fIVZIN7zh+8RUkEIabABLCFb5NW3A+GaDzAuIvlPm2cBvxtzmRAF+b60Zy58MTlqE MHLhmU3iw+ccbp0uMAOTqcdYh4JmIHFm8oQBxWgsZWZIjrMzZb8xeGxJtX6QIAlNL76ppi y73shM5BOckoLsuPsNWJbzOJAgv3w8EjCJx+jnHIMBb3DDpbQswXeV8AIegmCg== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH 1/3] ids-functions.pl: Generate ipset based whitelist. Date: Wed, 6 Apr 2022 21:12:43 +0200 Message-Id: <20220406191245.4218-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: development-bounces@lists.ipfire.org Sender: "Development" Signed-off-by: Stefan Schantl --- config/cfgroot/ids-functions.pl | 68 ++++++++++++++++----------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/config/cfgroot/ids-functions.pl b/config/cfgroot/ids-functions.pl index 94dccc8ae..d8ce5d0a0 100644 --- a/config/cfgroot/ids-functions.pl +++ b/config/cfgroot/ids-functions.pl @@ -90,7 +90,7 @@ our $sid_msg_file = "$rulespath/sid-msg.map"; our $local_rules_file = "$rulespath/local.rules"; # File which contains the rules to whitelist addresses on suricata. -our $whitelist_file = "$rulespath/whitelist.rules"; +our $whitelist_file = "$settingsdir/whitelist.conf"; # File which contains a list of all supported ruleset sources. # (Sourcefire, Emergingthreads, etc..) @@ -125,7 +125,7 @@ my @cron_intervals = ('off', 'daily', 'weekly' ); my @http_ports = ('80', '81'); # Array which contains a list of rulefiles which always will be included if they exist. -my @static_included_rulefiles = ('local.rules', 'whitelist.rules'); +my @static_included_rulefiles = ('local.rules'); # Array which contains a list of allways enabled application layer protocols. my @static_enabled_app_layer_protos = ('app-layer', 'decoder', 'files', 'stream'); @@ -1199,9 +1199,6 @@ sub _cleanup_rulesdir() { # We only want files. next unless (-f "$rulespath/$file"); - # Skip rules file for whitelisted hosts. - next if ("$rulespath/$file" eq $whitelist_file); - # Skip rules file with local rules. next if ("$rulespath/$file" eq $local_rules_file); @@ -1707,46 +1704,47 @@ sub get_suricata_enabled_app_layer_protos() { # sub generate_ignore_file() { my %ignored = (); + my @ignored_addresses = (); - # SID range 1000000-1999999 Reserved for Local Use - # Put your custom rules in this range to avoid conflicts - my $sid = 1500000; + # Name of the ipset. + my $list = "IPSWHITELIST"; # Read-in ignoredfile. &General::readhasharray($IDS::ignored_file, \%ignored); - # Open ignorefile for writing. - open(FILE, ">$IDS::whitelist_file") or die "Could not write to $IDS::whitelist_file. $!\n"; + # Loop through the entire hash and add the enabled addresses to + # the array of ignored addresses.. + while ( (my $key) = each %ignored) { + my $address = $ignored{$key}[0]; + my $remark = $ignored{$key}[1]; + my $status = $ignored{$key}[2]; + + # Check if the status of the entry is "enabled". + if ($status eq "enabled") { + # Check if the address/network is valid. + if ((&General::validip($address)) || (&General::validipandmask($address))) { + # Add the address to the array of ignored addresses. + push(@ignored_addresses, $address); + } + } + } - # Config file header. - print FILE "# Autogenerated file.\n"; - print FILE "# All user modifications will be overwritten.\n\n"; + # Open the the whitelist file for writing. + open(FILE, ">", "$whitelist_file") or die "Could not write to $whitelist_file. $!\n"; - # Add all user defined addresses to the whitelist. - # - # Check if the hash contains any elements. - if (keys (%ignored)) { - # Loop through the entire hash and write the host/network - # and remark to the ignore file. - while ( (my $key) = each %ignored) { - my $address = $ignored{$key}[0]; - my $remark = $ignored{$key}[1]; - my $status = $ignored{$key}[2]; - - # Check if the status of the entry is "enabled". - if ($status eq "enabled") { - # Check if the address/network is valid. - if ((&General::validip($address)) || (&General::validipandmask($address))) { - # Write rule line to the file to pass any traffic from this IP - print FILE "pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; bypass; sid:$sid\;)\n"; - - # Increment sid. - $sid++; - } - } + # Check if the array of ignored addresses contains any elements. + if(@ignored_addresses) { + # Write file header. + print FILE "create $list hash:net family inet -exist\n"; + print FILE "flush $list\n"; + + # Loop through the array of ignored addresses. + foreach my $address (@ignored_addresses) { + print FILE "add $list $address\n"; } } + # Close filehandle. close(FILE); }