From patchwork Fri Apr 26 03:31:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 2213 Return-Path: Received: from mail01.ipfire.org (mail01.i.ipfire.org [172.28.1.200]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mail01.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by web07.i.ipfire.org (Postfix) with ESMTPS id 6BE7285BD81 for ; Thu, 25 Apr 2019 18:31:56 +0100 (BST) Received: from mail01.i.ipfire.org (localhost [IPv6:::1]) by mail01.ipfire.org (Postfix) with ESMTP id 44qkkz0Sp9z55JnP; Thu, 25 Apr 2019 18:31:55 +0100 (BST) Received: from tuxedo.stevee (212095005098.public.telering.at [212.95.5.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mail01.ipfire.org (Postfix) with ESMTPSA id 44qkkw10qRz5161y; Thu, 25 Apr 2019 18:31:52 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=201904rsa; t=1556213512; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:openpgp:autocrypt; bh=a+cct9SDhU45W9BMsI07B9eQ56FfQsdo5pHFD4ED870=; b=USVicrjd6810sTAL4BFBgQPh9TRZvalu1iYkg8pRNhpXJB4PIIwhpmPdPPbUPBNTgn4+Tx 24td8DtDKvoyK3MGz+mx1nc0PsJd9jr0wzXuaPepy1KqwZgw7GweKIdD2w4p10YgAMFRQ7 LIfF++Cs+rAjnAU5VGcX9CNy/qFmEGqktI8A/Cl89TYedCPj456PpHJRu7r2eDlCOVDOT2 v7ERHW1IPSn+Y+UZTx6gG9i0BbCDhC+UbVKxNktANrQXSW4eNDNyx2+qEXd2bEnl7ruExn fRgBEEjpeHgfwrAWZWwNwou2bWqcMWT5guTIfAFrCFFEahDVMAr0/XzTDtvrZQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=201904ed25519; t=1556213512; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:openpgp:autocrypt; bh=a+cct9SDhU45W9BMsI07B9eQ56FfQsdo5pHFD4ED870=; b=RC0aBKzZBo7Fx+JaIMIwP+ARc7zGWTQUsMoSF3cEkH1NzgHy+Qe0BSNqaf/J9fxwfX4FrX 4y9t9Oh1YA4PJpCQ== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH 1/3] firewall: Use seperate firewall chains for passing traffic to the IPS Date: Thu, 25 Apr 2019 19:31:46 +0200 Message-Id: <20190425173148.3868-1-stefan.schantl@ipfire.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Authentication-Results: mail01.ipfire.org; auth=pass smtp.auth=stevee smtp.mailfrom=stefan.schantl@ipfire.org X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.15 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" Create and use seperate iptables chain called IPS_INPUT, IPS_FORWARD and IPS_OUTPUT to be more flexible which kind of traffic should be passed to suricata. Reference #12062 Signed-off-by: Stefan Schantl --- src/initscripts/system/firewall | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/initscripts/system/firewall b/src/initscripts/system/firewall index be6c9169f..da89857d8 100644 --- a/src/initscripts/system/firewall +++ b/src/initscripts/system/firewall @@ -186,10 +186,12 @@ iptables_init() { iptables -A FORWARD -j GUARDIAN # IPS (suricata) chains - iptables -N IPS - iptables -A INPUT -j IPS - iptables -A FORWARD -j IPS - iptables -A OUTPUT -j IPS + iptables -N IPS_INPUT + iptables -N IPS_FORWARD + iptables -N IPS_OUTPUT + iptables -A INPUT -j IPS_INPUT + iptables -A FORWARD -j IPS_FORWARD + iptables -A OUTPUT -j IPS_OUTPUT # Block non-established IPsec networks iptables -N IPSECBLOCK From patchwork Fri Apr 26 03:31:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 2214 Return-Path: Received: from mail01.ipfire.org (mail01.i.ipfire.org [172.28.1.200]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mail01.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by web07.i.ipfire.org (Postfix) with ESMTPS id 0824285BD81 for ; Thu, 25 Apr 2019 18:32:00 +0100 (BST) Received: from mail01.i.ipfire.org (localhost [IPv6:::1]) by mail01.ipfire.org (Postfix) with ESMTP id 44qkl33ffCz5GpB6; Thu, 25 Apr 2019 18:31:59 +0100 (BST) Received: from tuxedo.stevee (212095005098.public.telering.at [212.95.5.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mail01.ipfire.org (Postfix) with ESMTPSA id 44qkkw5P6yz5161R; Thu, 25 Apr 2019 18:31:52 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=201904rsa; t=1556213512; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:openpgp:autocrypt; bh=a7f9J0Yr5azzEms6XaWVZFNzFNnudeaJN+XmdUHpaLw=; b=a3AAAASdrvbF6LkSP/sVKZ15cCyi5v3qr+IufaRXoE8oJD9RYP5bpru4LRCIBOdhQ1Lib8 F0ZhPApH3ufP2mv+FTHJm8G2UHqJ+txcn5hU3cKOgmkjY5ivA3FJliKzHN6pzB9SmRSKbF Z66w06BN0dMFQ98xFMWPeU5VHrEPRXpPzQJtwbQLw8DArOyME3Da/8/GGej2dc+3nqD07d IB9fuqGct4hFYaLhDZeaAklr/4roIHvimV90nUMVfAcWL+48r81WxDbzIdxAtsaROcZZCS TRTa43k4jPxH+786jl/r1zIRtRYi7myFNxVAouKe5fFStZcU1hidPzspPvgWEQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=201904ed25519; t=1556213512; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:openpgp:autocrypt; bh=a7f9J0Yr5azzEms6XaWVZFNzFNnudeaJN+XmdUHpaLw=; b=Wp/Oo8qCbp6HAA/hZEGPWYvlVTJSWZWjIB8OWRyO5VAcb4vLdDNID01Qsg6dpBEniCheIZ hgE3TB6Mv6xTY5Aw== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH 2/3] initscripts/suricata: Move functions order and always use flush_fw_chain function Date: Thu, 25 Apr 2019 19:31:47 +0200 Message-Id: <20190425173148.3868-2-stefan.schantl@ipfire.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190425173148.3868-1-stefan.schantl@ipfire.org> References: <20190425173148.3868-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 Authentication-Results: mail01.ipfire.org; auth=pass smtp.auth=stevee smtp.mailfrom=stefan.schantl@ipfire.org X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.15 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 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/initscripts/system/suricata b/src/initscripts/system/suricata index ecd693054..233cded53 100644 --- a/src/initscripts/system/suricata +++ b/src/initscripts/system/suricata @@ -48,12 +48,18 @@ function get_cpu_count { echo $CPUCOUNT } +# Function to flush the firewall chain. +function flush_fw_chain { + # Call iptables and flush the chain + iptables -F "$FW_CHAIN" +} + # Function to create the firewall rules to pass the traffic to suricata. function generate_fw_rules { cpu_count=$(get_cpu_count) # Flush the firewall chain. - iptables -F "$FW_CHAIN" + flush_fw_chain # Loop through the array of network zones. for zone in "${network_zones[@]}"; do @@ -103,12 +109,6 @@ function generate_fw_rules { iptables -A "${FW_CHAIN}" -j MARK --set-xmark "0x0/${MASK}" } -# Function to flush the firewall chain. -function flush_fw_chain { - # Call iptables and flush the chain - iptables -F "$FW_CHAIN" -} - case "$1" in start) # Get amount of CPU cores. From patchwork Fri Apr 26 03:31:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 2215 Return-Path: Received: from mail01.ipfire.org (mail01.i.ipfire.org [172.28.1.200]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mail01.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by web07.i.ipfire.org (Postfix) with ESMTPS id 013C885BD81 for ; Thu, 25 Apr 2019 18:32:04 +0100 (BST) Received: from mail01.i.ipfire.org (localhost [IPv6:::1]) by mail01.ipfire.org (Postfix) with ESMTP id 44qkl73ZHlz5KY1f; Thu, 25 Apr 2019 18:32:03 +0100 (BST) Received: from tuxedo.stevee (212095005098.public.telering.at [212.95.5.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mail01.ipfire.org (Postfix) with ESMTPSA id 44qkkx2H5dz55Jnd; Thu, 25 Apr 2019 18:31:53 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=201904rsa; t=1556213513; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:openpgp:autocrypt; bh=pBrBisGOLrElAxSoeih7QjK/VOuvqHMsAsHtAtM/c3o=; b=efGiu8iE5A+ExJiuyuR10TLBPiyKJ85pyn1kQao8KCv/v2nqAms3tdLIYQ1zp9YWSnt/Nm 7i2Ne9aRDzLQWCiLIf87VEZ+XJ9mb806AZTrilxWSt3P9b0T87opm0QYxjkPmKUpFQWyra rqmcm3x6O279rah6ngCDPfNB9OFGsFjxLTo2f0D71XEWNqTC9eteslt7BQqzVLJGn7AR4D bIazAL1VqxMUSyJcL5BQaD+bvzclKbpi10z1tERd+dR5S37FC40GYnrffw0OaBV6vNaBr3 oDLoSkYMechC6I3wlLiDdICRxYtz79KGPHskWf1K+tYjh+91Z8cYz10ENiddzQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=201904ed25519; t=1556213513; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:openpgp:autocrypt; bh=pBrBisGOLrElAxSoeih7QjK/VOuvqHMsAsHtAtM/c3o=; b=F5o1+EmfQ8nE0rFtBa0up4R6YoszW83QGH0m98MWwhiQByPtICLNqORWfkgFZM4vrvZkxJ lHHN1OPH1SB7clBA== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH 3/3] initscripts/suricata: Rework creation of firewall rules. Date: Thu, 25 Apr 2019 19:31:48 +0200 Message-Id: <20190425173148.3868-3-stefan.schantl@ipfire.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190425173148.3868-1-stefan.schantl@ipfire.org> References: <20190425173148.3868-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 Authentication-Results: mail01.ipfire.org; auth=pass smtp.auth=stevee smtp.mailfrom=stefan.schantl@ipfire.org X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.15 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" The script now will use the previously introduced seperate firewall chains called IPS_INPUT, IPS_FORWARD and IPS_OUTPUT. The commit also creates an AND connection between the choosen network zones in the UI and the final firwall rules. Fixes #12062. Signed-off-by: Stefan Schantl --- src/initscripts/system/suricata | 78 +++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/src/initscripts/system/suricata b/src/initscripts/system/suricata index 233cded53..c9f131fca 100644 --- a/src/initscripts/system/suricata +++ b/src/initscripts/system/suricata @@ -6,7 +6,7 @@ # # Author : Stefan Schantl # -# Version : 01.00 +# Version : 01.01 # # Notes : # @@ -20,8 +20,10 @@ PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin; export PATH eval $(/usr/local/bin/readhash /var/ipfire/suricata/settings) eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings) -# Name of the firewall chain. -FW_CHAIN="IPS" +# Name of the firewall chains. +IPS_INPUT_CHAIN="IPS_INPUT" +IPS_FORWARD_CHAIN="IPS_FORWARD" +IPS_OUTPUT_CHAIN="IPS_OUTPUT" # Optional options for the Netfilter queue. NFQ_OPTS="--queue-bypass " @@ -29,6 +31,9 @@ NFQ_OPTS="--queue-bypass " # Array containing the 4 possible network zones. network_zones=( red green blue orange ) +# Array to store the network zones weather the IPS is enabled for. +enabled_ips_zones=() + # Mark and Mask options. MARK="0x70000000" MASK="0x70000000" @@ -48,19 +53,18 @@ function get_cpu_count { echo $CPUCOUNT } -# Function to flush the firewall chain. +# Function to flush the firewall chains. function flush_fw_chain { - # Call iptables and flush the chain - iptables -F "$FW_CHAIN" + # Call iptables and flush the chains + iptables -F "$IPS_INPUT_CHAIN" + iptables -F "$IPS_FORWARD_CHAIN" + iptables -F "$IPS_OUTPUT_CHAIN" } # Function to create the firewall rules to pass the traffic to suricata. function generate_fw_rules { cpu_count=$(get_cpu_count) - # Flush the firewall chain. - flush_fw_chain - # Loop through the array of network zones. for zone in "${network_zones[@]}"; do # Convert zone into upper case. @@ -85,28 +89,46 @@ function generate_fw_rules { network_device=${!zone_name} fi - # Assign NFQ_OPTS - NFQ_OPTIONS=$NFQ_OPTS - - # Check if there are multiple cpu cores available. - if [ "$cpu_count" -gt "1" ]; then - # Balance beetween all queues. - NFQ_OPTIONS+="--queue-balance 0:$(($cpu_count-1))" - NFQ_OPTIONS+=" --queue-cpu-fanout" - else - # Send all packets to queue 0. - NFQ_OPTIONS+="--queue-num 0" - fi - - # Create firewall rules to queue the traffic and pass to - # the IDS. - iptables -I "$FW_CHAIN" -i "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS - iptables -I "$FW_CHAIN" -o "$network_device" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS + # Add the network device to the array of enabled zones. + enabled_ips_zones+=( "$network_device" ) fi done - # Clear repeat bit, so that it does not confuse IPsec or QoS - iptables -A "${FW_CHAIN}" -j MARK --set-xmark "0x0/${MASK}" + # Assign NFQ_OPTS + NFQ_OPTIONS=$NFQ_OPTS + + # Check if there are multiple cpu cores available. + if [ "$cpu_count" -gt "1" ]; then + # Balance beetween all queues. + NFQ_OPTIONS+="--queue-balance 0:$(($cpu_count-1))" + NFQ_OPTIONS+=" --queue-cpu-fanout" + else + # Send all packets to queue 0. + NFQ_OPTIONS+="--queue-num 0" + fi + + # Flush the firewall chains. + flush_fw_chain + + # 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 + # Create rules queue input and output related traffic and pass it to the IPS. + iptables -I "$IPS_INPUT_CHAIN" -i "$enabled_ips_zone" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS + iptables -I "$IPS_OUTPUT_CHAIN" -o "$enabled_ips_zone" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS + + # Create rules which are required to handle forwarded traffic. + for enabled_ips_zone_forward in "${enabled_ips_zones[@]}"; do + iptables -I "$IPS_FORWARD_CHAIN" -i "$enabled_ips_zone" -o "$enabled_ips_zone_forward" -m mark ! --mark "$MARK"/"$MASK" -j NFQUEUE $NFQ_OPTIONS + done + done + + # Clear repeat bit, so that it does not confuse IPsec or QoS + iptables -A "${IPS_INPUT_CHAIN}" -j MARK --set-xmark "0x0/${MASK}" + iptables -A "${IPS_FORWARD_CHAIN}" -j MARK --set-xmark "0x0/${MASK}" + iptables -A "${IPS_OUTPUT_CHAIN}" -j MARK --set-xmark "0x0/${MASK}" + fi } case "$1" in