From patchwork Wed Apr 6 19:12:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 5455 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) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4KYZ0W1wZ1z3x1x for ; Wed, 6 Apr 2022 19:12:59 +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) client-signature ECDSA (P-384)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4KYZ0S0W57z4S6; Wed, 6 Apr 2022 19:12:56 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4KYZ0R2vr2z3019; 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) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4KYZ0Q0L1Cz2yCy for ; Wed, 6 Apr 2022 19:12:54 +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 4KYZ0P4nwxz1xJ; Wed, 6 Apr 2022 19:12:53 +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: in-reply-to:in-reply-to:references:references; bh=0OJBUgbbSPbRFJo4dJt6Ei9q4D/ywJA13tHibAgI0qs=; b=ftN85Rn8xRmC7SLbNa1PiJMSom1kp5TIAQDAUTm3qp+s1V/wOqB+f/3G7Ggfx0/5QxXY9M 0Py7RpK0ccdk1dAg== 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: in-reply-to:in-reply-to:references:references; bh=0OJBUgbbSPbRFJo4dJt6Ei9q4D/ywJA13tHibAgI0qs=; b=lrVKtP47K7lt2P1NqrNinQ1ySYa6SM/DXbTKhqpG5YCjkUBYhVdbxi8fraDip8zkTKtp7e KODSJ5WXmFYjO9e//Xh2WIIv6xe345zujTXIjkQTHZU1qDRI1PD5yFV02OcewUnH9u/vef XysSeMc4FiBSvrJadUbtZ2cnyQft6uYBHaJGYfSc0OpSyrN6l6W/SOQVGj8pDRAmNwn+gf rbRJdjP+pXQ0y8F3exD8m+hZgoyumm9nZ31ltO0bJpNqiZCjjKMZDIHBT5edzES7VSMzgB FL4lu6otXDu/ppOyeCB40ot94cZ42vI7GExMHHoVRkUCK8vAz2V0T1oDg1fmuA== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH 2/3] suricata: Handle ipset based whitelist in initscript. Date: Wed, 6 Apr 2022 21:12:44 +0200 Message-Id: <20220406191245.4218-2-stefan.schantl@ipfire.org> In-Reply-To: <20220406191245.4218-1-stefan.schantl@ipfire.org> References: <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 --- src/initscripts/system/suricata | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/initscripts/system/suricata b/src/initscripts/system/suricata index 938ea66de..5ede405ce 100644 --- a/src/initscripts/system/suricata +++ b/src/initscripts/system/suricata @@ -44,6 +44,15 @@ enabled_ips_zones=() # PID file of suricata. PID_FILE="/var/run/suricata.pid" +# File which contains the ipset of whitelisted hosts. +WHITELIST_FILE="/var/ipfire/suricata/whitelist.conf" + +# Name of the ipset set +IPSET_SET="IPSWHITELIST" + +IPS_BYPASS_MARK="0x40000000" +IPS_BYPASS_MASK="0x40000000" + # Function to get the amount of CPU cores of the system. function get_cpu_count { CPUCOUNT=0 @@ -135,16 +144,44 @@ function generate_fw_rules { # Flush the firewall chains. flush_fw_chain + if [ -s "$WHITELIST_FILE" ]; then + # Load the whitelist file. + ipset restore -f "$WHITELIST_FILE" + fi + # Check if the array of enabled_ips_zones contains any elements. if [[ ${enabled_ips_zones[@]} ]]; then # Loop through the array and create firewall rules. for enabled_ips_zone in "${enabled_ips_zones[@]}"; do + # Check if the whitelist file is not empty. + if [ -s "$WHITELIST_FILE" ]; then + # Create rules to handle whitelisted hosts. + iptables -w -A "$IPS_INPUT_CHAIN" -i "$enabled_ips_zone" -m set --match-set $IPSET_SET src -j CONNMARK --set-xmark $IPS_BYPASS_MARK/$IPS_BYPASS_MASK + iptables -w -A "$IPS_INPUT_CHAIN" -i "$enabled_ips_zone" -m set --match-set $IPSET_SET dst -j CONNMARK --set-xmark $IPS_BYPASS_MARK/$IPS_BYPASS_MASK + iptables -w -A "$IPS_INPUT_CHAIN" -i "$enabled_ips_zone" -m connmark --mark $IPS_BYPASS_MARK/$IPS_BYPASS_MASK -j RETURN + + iptables -w -A "$IPS_OUTPUT_CHAIN" -o "$enabled_ips_zone" -m set --match-set $IPSET_SET src -j CONNMARK --set-xmark $IPS_BYPASS_MARK/$IPS_BYPASS_MASK + iptables -w -A "$IPS_OUTPUT_CHAIN" -o "$enabled_ips_zone" -m set --match-set $IPSET_SET src -j CONNMARK --set-xmark $IPS_BYPASS_MARK/$IPS_BYPASS_MASK + iptables -w -A "$IPS_OUTPUT_CHAIN" -o "$enabled_ips_zone" -m connmark --mark $IPS_BYPASS_MARK/$IPS_BYPASS_MASK -j RETURN + fi + # Create rules queue input and output related traffic and pass it to the IPS. iptables -w -A "$IPS_INPUT_CHAIN" -i "$enabled_ips_zone" -j NFQUEUE $NFQ_OPTIONS iptables -w -A "$IPS_OUTPUT_CHAIN" -o "$enabled_ips_zone" -j NFQUEUE $NFQ_OPTIONS # Create rules which are required to handle forwarded traffic. for enabled_ips_zone_forward in "${enabled_ips_zones[@]}"; do + # Check if the whetelist file is not empty. + if [ -s "$WHITELIST_FILE" ]; then + # Create rules to handle whitelisted hosts. + iptables -w -A "$IPS_FORWARD_CHAIN" -i "$enabled_ips_zone" -o "$enabled_ips_zone_forward" \ + -m set --match-set $IPSET_SET src -j CONNMARK --set-xmark $IPS_BYPASS_MARK/$IPS_BYPASS_MASK + iptables -w -A "$IPS_FORWARD_CHAIN" -i "$enabled_ips_zone" -o "$enabled_ips_zone_forward" \ + -m set --match-set $IPSET_SET dst -j CONNMARK --set-xmark $IPS_BYPASS_MARK/$IPS_BYPASS_MASK + iptables -w -A "$IPS_FORWARD_CHAIN" -i "$enabled_ips_zone" -o "$enabled_ips_zone_forward" \ + -m connmark --mark $IPS_BYPASS_MARK/$IPS_BYPASS_MASK + fi + iptables -w -A "$IPS_FORWARD_CHAIN" -i "$enabled_ips_zone" -o "$enabled_ips_zone_forward" -j NFQUEUE $NFQ_OPTIONS done done @@ -188,6 +225,11 @@ case "$1" in # Flush firewall chain. flush_fw_chain + # Unload the ipset set. + if [ -s "$WHITELIST_FILE" ]; then + ipset destroy $IPSET_SET 2>/dev/null + fi + # Sometimes suricata not correct shutdown. So killall. killall -KILL /usr/bin/suricata 2>/dev/null