Compare commits

...

24 Commits

Author SHA1 Message Date
Tristan PILAT a432511b04 Add per host output autorisation capability 2020-11-18 18:10:27 +01:00
Tristan PILAT c59e63d44d fixup! Update/Add section titles 2020-11-18 18:01:35 +01:00
Tristan PILAT 86ffdfc916 Accept any ICMPv6 input traffic 2020-11-18 18:01:09 +01:00
Tristan PILAT 36634a705f We have to accepted output ICMP and IGMP since we drop output traffic by default 2020-11-18 18:00:28 +01:00
Tristan PILAT ba865faf0a Add IPv6 compatibility 2020-11-18 17:56:11 +01:00
Tristan PILAT ab2a7e9eb0 Let's use the new ip_type function 2020-11-18 17:54:11 +01:00
Tristan PILAT 519a0f9c60 Add a function to tell whether an IP is a v4 or v6 one 2020-11-18 17:54:11 +01:00
Tristan PILAT 520b8893f0 Delete drop rules for output since it is the default policy now 2020-11-18 17:54:11 +01:00
Tristan PILAT 550af6e21f Change output default policy to drop 2020-11-18 17:54:10 +01:00
Tristan PILAT 7a1adbdf39 Update/Add section titles 2020-11-18 17:54:10 +01:00
Tristan PILAT 6bc1b75cd2 Update blacklist-countries.sh script to be used with nftables 2020-10-14 17:21:54 +02:00
Tristan PILAT 1b19f7084b We need flags interval to be able to use CIDR notation in minifirewall_privileged_ips and minifirewall_trusted_ips sets 2020-10-14 17:21:00 +02:00
Tristan PILAT 948a3aeeb2 We want to drop traffic coming to protected TCP/UDP ports 2020-10-14 17:18:03 +02:00
Tristan PILAT 1c1d5480bc Add rules to redirsct traffic from blocked IPs to protected_tcp_pots and protected_udp_ports chains 2020-10-14 17:16:17 +02:00
Tristan PILAT 6a46ca716b Add a set for the blocked IP addresses 2020-10-14 17:14:23 +02:00
Tristan PILAT 5af8fad976 It's easier to just accept all icmp 2020-10-14 16:49:23 +02:00
Tristan PILAT 79f6d47a6c Remove commented and useless rules 2020-10-14 16:48:39 +02:00
Tristan PILAT 4781ef509c Don't prevent ICMP replies to go out and only drop TCP and UDP 2020-09-07 11:18:52 +02:00
Tristan PILAT 5f4787d3fd Until we get a nftables version of the Docker rules present for iptables, remove iptables commented out part for Docker. 2020-09-07 11:17:34 +02:00
Tristan PILAT c7d0d6820b Simplification of the input ICMP et IGMP rules 2020-09-07 11:14:41 +02:00
Tristan PILAT 9169a9f0b0 Include rules in the if statements + add comments for every output rules 2020-08-31 17:08:30 +02:00
Tristan PILAT 585c16c92e minifirewall script has been renamed to minifirewall-{start,stop}.sh 2020-08-31 09:48:48 +02:00
Tristan PILAT 286fe62de5 Add initial work for output filtering 2020-08-31 09:47:35 +02:00
Tristan PILAT 129b323f80 First nftables version of minifirewall 2020-08-24 16:59:15 +02:00
7 changed files with 464 additions and 530 deletions

View File

@ -2,13 +2,15 @@ Minifirewall
=========
Minifirewall is shellscripts for easy firewalling on a standalone server
we used netfilter/iptables http://netfilter.org/ designed for recent Linux kernel
we used nftables https://wiki.nftables.org/ designed for recent Linux kernel
See https://gitea.evolix.org/evolix/minifirewall
## Install
~~~
install -m 0700 minifirewall /etc/init.d/minifirewall
install -m 0700 minifirewall.service /etc/systemd/system/minifirewall.service
install -m 0700 minifirewall-start.sh /usr/local/sbin/minifirewall-start.sh
install -m 0700 minifirewall-stop.sh /usr/local/sbin/minifirewall-stop.sh
install -m 0600 minifirewall.conf /etc/default/minifirewall
~~~
@ -17,19 +19,17 @@ install -m 0600 minifirewall.conf /etc/default/minifirewall
Edit /etc/default/minifirewall file:
* If your interface is not _eth0_, change *INT* variable
* If you don't IPv6 : *IPv6=off*
* Modify *INTLAN* variable, probably with your *IP/32* or your local network if you trust it
* Set your trusted and privilegied IP addresses in *TRUSTEDIPS* and *PRIVILEGIEDIPS* variables
* Authorize your +public+ services with *SERVICESTCP1* and *SERVICESUDP1* variables
* Authorize your +semi-public+ services (only for *TRUSTEDIPS* and *PRIVILEGIEDIPS* ) with *SERVICESTCP2* and *SERVICESUDP2* variables
* Authorize your +private+ services (only for *TRUSTEDIPS* ) with *SERVICESTCP3* and *SERVICESUDP3* variables
* Configure your authorizations for external services : DNS, HTTP, HTTPS, SMTP, SSH, NTP
* Add your specific rules
## Usage
~~~
/etc/init.d/minifirewall start/stop/restart
systemctl start/stop/restart minifirewall.service
~~~
If you want to add minifirewall in boot sequence:

View File

@ -1,5 +1,6 @@
#!/bin/sh
NFT=/usr/sbin/nft
ripedeny_file=/var/tmp/ripe_deny
cd /var/tmp
@ -10,14 +11,10 @@ GET http://antispam00.evolix.org/spam/ripe.cidr.md5 > ripe.cidr.md5
GET http://antispam00.evolix.org/spam/ripe.cidr > ripe.cidr
for i in CN KR RU; do
grep "^$i|" ripe.cidr >> $ripedeny_file
done
/sbin/iptables -F NEEDRESTRICT
for i in $(cat $ripedeny_file); do
BLOCK=$(echo $i | cut -d"|" -f2)
/sbin/iptables -I NEEDRESTRICT -s $BLOCK -j DROP
$NFT add element inet minifirewall minifirewall_blocked_ips {$BLOCK}
done

View File

@ -1,492 +0,0 @@
#!/bin/sh
# minifirewall is shellscripts for easy firewalling on a standalone server
# we used netfilter/iptables http://netfilter.org/ designed for recent Linux kernel
# See https://gitea.evolix.org/evolix/minifirewall
# Copyright (c) 2007-2015 Evolix
# 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.
# Description
# script for standalone server
# Start or stop minifirewall
#
### BEGIN INIT INFO
# Provides: minfirewall
# Required-Start:
# Required-Stop:
# Should-Start: $network $syslog $named
# Should-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop the firewall
# Description: Firewall designed for standalone server
### END INIT INFO
DESC="minifirewall"
NAME="minifirewall"
# Variables configuration
#########################
# iptables paths
IPT=/sbin/iptables
IPT6=/sbin/ip6tables
# TCP/IP variables
LOOPBACK='127.0.0.0/8'
CLASSA='10.0.0.0/8'
CLASSB='172.16.0.0/12'
CLASSC='192.168.0.0/16'
CLASSD='224.0.0.0/4'
CLASSE='240.0.0.0/5'
ALL='0.0.0.0'
BROAD='255.255.255.255'
PORTSROOT='0:1023'
PORTSUSER='1024:65535'
chain_exists()
{
local chain_name="$1" ; shift
[ $# -eq 1 ] && local intable="--table $1"
iptables $intable -nL "$chain_name" >/dev/null 2>&1
}
# Configuration
oldconfigfile="/etc/firewall.rc"
configfile="/etc/default/minifirewall"
IPV6=$(grep "IPV6=" /etc/default/minifirewall | awk -F '=' -F "'" '{print $2}')
DOCKER=$(grep "DOCKER=" /etc/default/minifirewall | awk -F '=' -F "'" '{print $2}')
INT=$(grep "INT=" /etc/default/minifirewall | awk -F '=' -F "'" '{print $2}')
case "$1" in
start)
echo "Start IPTables rules..."
# Stop and warn if error!
set -e
trap 'echo "ERROR in minifirewall configuration (fix it now!) or script manipulation (fix yourself)." ' INT TERM EXIT
# sysctl network security settings
##################################
# Don't answer to broadcast pings
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
# Ignore bogus ICMP responses
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
# Disable Source Routing
for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do
echo 0 > $i
done
# Enable TCP SYN cookies to avoid TCP-SYN-FLOOD attacks
# cf http://cr.yp.to/syncookies.html
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
# Disable ICMP redirects
for i in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo 0 > $i
done
for i in /proc/sys/net/ipv4/conf/*/send_redirects; do
echo 0 > $i
done
# Enable Reverse Path filtering : verify if responses use same network interface
for i in /proc/sys/net/ipv4/conf/*/rp_filter; do
echo 1 > $i
done
# log des paquets avec adresse incoherente
for i in /proc/sys/net/ipv4/conf/*/log_martians; do
echo 1 > $i
done
# IPTables configuration
########################
$IPT -N LOG_DROP
$IPT -A LOG_DROP -j LOG --log-prefix '[IPTABLES DROP] : '
$IPT -A LOG_DROP -j DROP
$IPT -N LOG_ACCEPT
$IPT -A LOG_ACCEPT -j LOG --log-prefix '[IPTABLES ACCEPT] : '
$IPT -A LOG_ACCEPT -j ACCEPT
if test -f $oldconfigfile; then
echo "$oldconfigfile is deprecated, rename to $configfile" >&2
exit 1
fi
if ! test -f $configfile; then
echo "$configfile does not exist" >&2
exit 1
fi
tmpfile=`mktemp`
. $configfile 2>$tmpfile >&2
if [ -s $tmpfile ]; then
echo "$configfile returns standard or error output (see below). Stopping." >&2
cat $tmpfile
exit 1
fi
rm $tmpfile
# Trusted ip addresses
$IPT -N ONLYTRUSTED
$IPT -A ONLYTRUSTED -j LOG_DROP
for x in $TRUSTEDIPS
do
$IPT -I ONLYTRUSTED -s $x -j ACCEPT
done
# Privilegied ip addresses
# (trusted ip addresses *are* privilegied)
$IPT -N ONLYPRIVILEGIED
$IPT -A ONLYPRIVILEGIED -j ONLYTRUSTED
for x in $PRIVILEGIEDIPS
do
$IPT -I ONLYPRIVILEGIED -s $x -j ACCEPT
done
# Chain for restrictions (blacklist IPs/ranges)
$IPT -N NEEDRESTRICT
# We allow all on loopback interface
$IPT -A INPUT -i lo -j ACCEPT
[ "$IPV6" != "off" ] && $IPT6 -A INPUT -i lo -j ACCEPT
# if OUTPUTDROP
$IPT -A OUTPUT -o lo -j ACCEPT
[ "$IPV6" != "off" ] && $IPT6 -A OUTPUT -o lo -j ACCEPT
# We avoid "martians" packets, typical when W32/Blaster virus
# attacked windowsupdate.com and DNS was changed to 127.0.0.1
# $IPT -t NAT -I PREROUTING -s $LOOPBACK -i ! lo -j DROP
$IPT -A INPUT -s $LOOPBACK ! -i lo -j DROP
if [ "$DOCKER" = "on" ]; then
$IPT -N MINIFW-DOCKER-TRUSTED
$IPT -A MINIFW-DOCKER-TRUSTED -j DROP
$IPT -N MINIFW-DOCKER-PRIVILEGED
$IPT -A MINIFW-DOCKER-PRIVILEGED -j MINIFW-DOCKER-TRUSTED
$IPT -A MINIFW-DOCKER-PRIVILEGED -j RETURN
$IPT -N MINIFW-DOCKER-PUB
$IPT -A MINIFW-DOCKER-PUB -j MINIFW-DOCKER-PRIVILEGED
$IPT -A MINIFW-DOCKER-PUB -j RETURN
# Flush DOCKER-USER if exist, create it if absent
if chain_exists 'DOCKER-USER'; then
$IPT -F DOCKER-USER
else
$IPT -N DOCKER-USER
fi;
# Pipe new connection through MINIFW-DOCKER-PUB
$IPT -A DOCKER-USER -i $INT -m state --state NEW -j MINIFW-DOCKER-PUB
$IPT -A DOCKER-USER -j RETURN
fi
# Local services restrictions
#############################
# Allow services for $INTLAN (local server or local network)
$IPT -A INPUT -s $INTLAN -j ACCEPT
# Enable protection chain for sensible services
for x in $SERVICESTCP1p
do
$IPT -A INPUT -p tcp --dport $x -j NEEDRESTRICT
done
for x in $SERVICESUDP1p
do
$IPT -A INPUT -p udp --dport $x -j NEEDRESTRICT
done
# Public service
for x in $SERVICESTCP1
do
$IPT -A INPUT -p tcp --dport $x -j ACCEPT
[ "$IPV6" != "off" ] && $IPT6 -A INPUT -p tcp --dport $x -j ACCEPT
done
for x in $SERVICESUDP1
do
$IPT -A INPUT -p udp --dport $x -j ACCEPT
[ "$IPV6" != "off" ] && $IPT6 -A INPUT -p udp --dport $x -j ACCEPT
done
# Privilegied services
for x in $SERVICESTCP2
do
$IPT -A INPUT -p tcp --dport $x -j ONLYPRIVILEGIED
done
for x in $SERVICESUDP2
do
$IPT -A INPUT -p udp --dport $x -j ONLYPRIVILEGIED
done
# Private services
for x in $SERVICESTCP3
do
$IPT -A INPUT -p tcp --dport $x -j ONLYTRUSTED
done
for x in $SERVICESUDP3
do
$IPT -A INPUT -p udp --dport $x -j ONLYTRUSTED
done
if [ "$DOCKER" = "on" ]; then
# Public services defined in SERVICESTCP1 & SERVICESUDP1
for dstport in $SERVICESTCP1
do
$IPT -I MINIFW-DOCKER-PUB -p tcp --dport "$dstport" -j RETURN
done
for dstport in $SERVICESUDP1
do
$IPT -I MINIFW-DOCKER-PUB -p udp --dport "$dstport" -j RETURN
done
# Privileged services (accessible from privileged & trusted IPs)
for dstport in $SERVICESTCP2
do
for srcip in $PRIVILEGIEDIPS
do
$IPT -I MINIFW-DOCKER-PRIVILEGED -p tcp -s "$srcip" --dport "$dstport" -j RETURN
done
for srcip in $TRUSTEDIPS
do
$IPT -I MINIFW-DOCKER-PRIVILEGED -p tcp -s "$srcip" --dport "$dstport" -j RETURN
done
done
for dstport in $SERVICESUDP2
do
for srcip in $PRIVILEGIEDIPS
do
$IPT -I MINIFW-DOCKER-PRIVILEGED -p udp -s "$srcip" --dport "$dstport" -j RETURN
done
for srcip in $TRUSTEDIPS
do
$IPT -I MINIFW-DOCKER-PRIVILEGED -p udp -s "$srcip" --dport "$dstport" -j RETURN
done
done
# Trusted services (accessible from trusted IPs)
for dstport in $SERVICESTCP3
do
for srcip in $TRUSTEDIPS
do
$IPT -I MINIFW-DOCKER-TRUSTED -p tcp -s "$srcip" --dport "$dstport" -j RETURN
done
done
for dstport in $SERVICESUDP3
do
for srcip in $TRUSTEDIPS
do
$IPT -I MINIFW-DOCKER-TRUSTED -p udp -s "$srcip" --dport "$dstport" -j RETURN
done
done
fi
# External services
###################
# DNS authorizations
for x in $DNSSERVEURS
do
$IPT -A INPUT -p tcp ! --syn --sport 53 --dport $PORTSUSER -s $x -j ACCEPT
$IPT -A INPUT -p udp --sport 53 --dport $PORTSUSER -s $x -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A OUTPUT -o $INT -p udp -d $x --dport 53 --match state --state NEW -j ACCEPT
done
# HTTP (TCP/80) authorizations
for x in $HTTPSITES
do
$IPT -A INPUT -p tcp ! --syn --sport 80 --dport $PORTSUSER -s $x -j ACCEPT
done
# HTTPS (TCP/443) authorizations
for x in $HTTPSSITES
do
$IPT -A INPUT -p tcp ! --syn --sport 443 --dport $PORTSUSER -s $x -j ACCEPT
done
# FTP (so complex protocol...) authorizations
for x in $FTPSITES
do
# requests on Control connection
$IPT -A INPUT -p tcp ! --syn --sport 21 --dport $PORTSUSER -s $x -j ACCEPT
# FTP port-mode on Data Connection
$IPT -A INPUT -p tcp --sport 20 --dport $PORTSUSER -s $x -j ACCEPT
# FTP passive-mode on Data Connection
# WARNING, this allow all connections on TCP ports > 1024
$IPT -A INPUT -p tcp ! --syn --sport $PORTSUSER --dport $PORTSUSER -s $x -j ACCEPT
done
# SSH authorizations
for x in $SSHOK
do
$IPT -A INPUT -p tcp ! --syn --sport 22 -s $x -j ACCEPT
done
# SMTP authorizations
for x in $SMTPOK
do
$IPT -A INPUT -p tcp ! --syn --sport 25 --dport $PORTSUSER -s $x -j ACCEPT
done
# secure SMTP (TCP/465 et TCP/587) authorizations
for x in $SMTPSECUREOK
do
$IPT -A INPUT -p tcp ! --syn --sport 465 --dport $PORTSUSER -s $x -j ACCEPT
$IPT -A INPUT -p tcp ! --syn --sport 587 --dport $PORTSUSER -s $x -j ACCEPT
done
# NTP authorizations
for x in $NTPOK
do
$IPT -A INPUT -p udp --sport 123 -s $x -j ACCEPT
$IPT -A OUTPUT -o $INT -p udp -d $x --dport 123 --match state --state NEW -j ACCEPT
done
# Always allow ICMP
$IPT -A INPUT -p icmp -j ACCEPT
[ "$IPV6" != "off" ] && $IPT6 -A INPUT -p icmpv6 -j ACCEPT
# IPTables policy
#################
# by default DROP INPUT packets
$IPT -P INPUT DROP
[ "$IPV6" != "off" ] && $IPT6 -P INPUT DROP
# by default, no FORWARING (deprecated for Virtual Machines)
#echo 0 > /proc/sys/net/ipv4/ip_forward
#$IPT -P FORWARD DROP
#$IPT6 -P FORWARD DROP
# by default allow OUTPUT packets... but drop UDP packets (see OUTPUTDROP to drop OUTPUT packets)
$IPT -P OUTPUT ACCEPT
[ "$IPV6" != "off" ] && $IPT6 -P OUTPUT ACCEPT
$IPT -A OUTPUT -o $INT -p udp --dport 33434:33523 --match state --state NEW -j ACCEPT
$IPT -A OUTPUT -p udp --match state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A OUTPUT -p udp -j DROP
[ "$IPV6" != "off" ] && $IPT6 -A OUTPUT -o $INT -p udp --dport 33434:33523 --match state --state NEW -j ACCEPT
[ "$IPV6" != "off" ] && $IPT6 -A OUTPUT -p udp --match state --state ESTABLISHED,RELATED -j ACCEPT
[ "$IPV6" != "off" ] && $IPT6 -A OUTPUT -p udp -j DROP
trap - INT TERM EXIT
echo "...starting IPTables rules is now finish : OK"
;;
stop)
echo "Flush all rules and accept everything..."
# Delete all rules
$IPT -F INPUT
$IPT -F OUTPUT
$IPT -F LOG_DROP
$IPT -F LOG_ACCEPT
$IPT -F ONLYTRUSTED
$IPT -F ONLYPRIVILEGIED
$IPT -F NEEDRESTRICT
[ "$DOCKER" = "off" ] && $IPT -t nat -F
$IPT -t mangle -F
[ "$IPV6" != "off" ] && $IPT6 -F INPUT
[ "$IPV6" != "off" ] && $IPT6 -F OUTPUT
if [ "$DOCKER" = "on" ]; then
$IPT -F DOCKER-USER
$IPT -A DOCKER-USER -j RETURN
$IPT -F MINIFW-DOCKER-PUB
$IPT -X MINIFW-DOCKER-PUB
$IPT -F MINIFW-DOCKER-PRIVILEGED
$IPT -X MINIFW-DOCKER-PRIVILEGED
$IPT -F MINIFW-DOCKER-TRUSTED
$IPT -X MINIFW-DOCKER-TRUSTED
fi
# Accept all
$IPT -P INPUT ACCEPT
$IPT -P OUTPUT ACCEPT
[ "$IPV6" != "off" ] && $IPT6 -P INPUT ACCEPT
[ "$IPV6" != "off" ] && $IPT6 -P OUTPUT ACCEPT
#$IPT -P FORWARD ACCEPT
#$IPT -t nat -P PREROUTING ACCEPT
#$IPT -t nat -P POSTROUTING ACCEPT
# Delete non-standard chains
$IPT -X LOG_DROP
$IPT -X LOG_ACCEPT
$IPT -X ONLYPRIVILEGIED
$IPT -X ONLYTRUSTED
$IPT -X NEEDRESTRICT
echo "...flushing IPTables rules is now finish : OK"
;;
status)
$IPT -L -n -v --line-numbers
$IPT -t nat -L -n -v --line-numbers
$IPT -t mangle -L -n -v --line-numbers
$IPT6 -L -n -v --line-numbers
$IPT6 -t mangle -L -n -v --line-numbers
;;
reset)
echo "Reset all IPTables counters..."
$IPT -Z
$IPT -t nat -Z
$IPT -t mangle -Z
[ "$IPV6" != "off" ] && $IPT6 -Z
[ "$IPV6" != "off" ] && $IPT6 -t mangle -Z
echo "...reseting IPTables counters is now finish : OK"
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart|status|reset|squid}"
exit 1
esac
exit 0

407
minifirewall-start.sh Executable file
View File

@ -0,0 +1,407 @@
#!/bin/sh
# minifirewall
# See https://gitea.evolix.org/evolix/minifirewall
# Copyright (c) 2007-2020 Evolix
# 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.
# Description
# script for standalone server
# Start minifirewall
#
# Variables configuration
#########################
# nft path
NFT=/usr/sbin/nft
# Configuration
configfile="/etc/default/minifirewall"
DOCKER=$(grep "DOCKER=" /etc/default/minifirewall | awk -F '=' -F "'" '{print $2}')
INT=$(grep "INT=" /etc/default/minifirewall | awk -F '=' -F "'" '{print $2}')
# sysctl network security settings
##################################
# Don't answer to broadcast pings
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
# Ignore bogus ICMP responses
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
# Disable Source Routing
for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do
echo 0 > $i
done
# Enable TCP SYN cookies to avoid TCP-SYN-FLOOD attacks
# cf http://cr.yp.to/syncookies.html
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
# Disable ICMP redirects
for i in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo 0 > $i
done
for i in /proc/sys/net/ipv4/conf/*/send_redirects; do
echo 0 > $i
done
# Enable Reverse Path filtering : verify if responses use same network interface
for i in /proc/sys/net/ipv4/conf/*/rp_filter; do
echo 1 > $i
done
# log des paquets avec adresse incoherente
for i in /proc/sys/net/ipv4/conf/*/log_martians; do
echo 1 > $i
done
############
## FUNCTIONS
############
# Determine IP type
ip_type() {
v4_ips=""
v6_ips=""
for ip in $(echo $1 | sed 's/ /, /g'); do
if [ "$ip" != "${ip#*[0-9].[0-9]}" ]; then
v4_ips="$v4_ips$ip "
elif [ "$ip" != "${ip#*:[0-9a-fA-F]}" ]; then
v6_ips="$v6_ips$ip "
else
echo "Unrecognized IP format '$ip'"
exit 1
fi
done
}
#########################
## NFTables configuration
#########################
if ! test -f $configfile; then
echo "$configfile does not exist" >&2
exit 1
fi
# Parse configuration file
. $configfile
# Flush everything first
$NFT flush ruleset
# Add a filter table
$NFT add table inet minifirewall
# Add the input, forward, and output base chains. The default policy will be to drop the traffic.
$NFT add chain inet minifirewall minifirewall_input '{ type filter hook input priority 0 ; policy drop ; }'
$NFT add chain inet minifirewall minifirewall_forward '{ type filter hook forward priority 0 ; policy drop ; }'
$NFT add chain inet minifirewall minifirewall_output '{ type filter hook output priority 0 ; policy drop ; }'
# Add set with trusted IP addresses
$NFT add set inet minifirewall minifirewall_trusted_v4_ips '{ type ipv4_addr ; flags interval ;}'
$NFT add set inet minifirewall minifirewall_trusted_v6_ips '{ type ipv6_addr ; flags interval ;}'
ip_type "$TRUSTEDIPS"
if [ -n "$v4_ips" ] ; then
$NFT add element inet minifirewall minifirewall_trusted_v4_ips {$v4_ips}
fi
if [ -n "$v6_ips" ] ; then
$NFT add element inet minifirewall minifirewall_trusted_v6_ips {$v6_ips}
fi
# Add set with privileged IP addresses
$NFT add set inet minifirewall minifirewall_privileged_v4_ips '{ type ipv4_addr ; flags interval ;}'
$NFT add set inet minifirewall minifirewall_privileged_v6_ips '{ type ipv6_addr ; flags interval ;}'
ip_type "$PRIVILEGIEDIPS"
if [ -n "$v4_ips" ] ; then
$NFT add element inet minifirewall minifirewall_privileged_v4_ips {$v4_ips}
fi
if [ -n "$v6_ips" ] ; then
$NFT add element inet minifirewall minifirewall_privileged_v6_ips {$v6_ips}
fi
# Add set for blocked IP addresses
$NFT add set inet minifirewall minifirewall_blocked_v4_ips '{ type ipv4_addr ; flags interval ;}'
$NFT add set inet minifirewall minifirewall_blocked_v6_ips '{ type ipv6_addr ; flags interval ;}'
# Add TCP/UDP chains for protected, public, semi-public and private ports
$NFT add chain inet minifirewall protected_tcp_ports
$NFT add chain inet minifirewall protected_udp_ports
$NFT add chain inet minifirewall public_tcp_ports
$NFT add chain inet minifirewall public_udp_ports
$NFT add chain inet minifirewall semipublic_tcp_ports
$NFT add chain inet minifirewall semipublic_udp_ports
$NFT add chain inet minifirewall private_tcp_ports
$NFT add chain inet minifirewall private_udp_ports
################
## Input traffic
################
# Related and established traffic is accepted
$NFT add rule inet minifirewall minifirewall_input ct state related,established accept
# All loopback interface traffic is accepted
$NFT add rule inet minifirewall minifirewall_input iif lo accept
# Allow services for $INTLAN (local server or local network) is accepted
$NFT add rule inet minifirewall minifirewall_input ip saddr $INTLAN accept
# Any invalid traffic is dropped
$NFT add rule inet minifirewall minifirewall_input ct state invalid drop
# ICMP and IGMP traffic is accepted
$NFT add rule inet minifirewall minifirewall_input ip protocol icmp accept
$NFT add rule inet minifirewall minifirewall_input meta l4proto ipv6-icmp accept
$NFT add rule inet minifirewall minifirewall_input ip protocol igmp accept
# New UDP traffic from blocked IPs jumps to the private_udp_ports chain
$NFT add rule inet minifirewall minifirewall_input 'ip saddr @minifirewall_blocked_v4_ips meta l4proto udp ct state new jump protected_udp_ports'
$NFT add rule inet minifirewall minifirewall_input 'ip6 saddr @minifirewall_blocked_v6_ips meta l4proto udp ct state new jump protected_udp_ports'
# New TCP traffic from blocked IPs jumps to the private_tcp_ports chain
$NFT add rule inet minifirewall minifirewall_input 'ip saddr @minifirewall_blocked_v4_ips meta l4proto tcp tcp flags & (fin|syn|rst|ack) == syn ct state new jump protected_tcp_ports'
$NFT add rule inet minifirewall minifirewall_input 'ip6 saddr @minifirewall_blocked_v6_ips meta l4proto tcp tcp flags & (fin|syn|rst|ack) == syn ct state new jump protected_tcp_ports'
# New UDP traffic from trusted IPs jumps to the private_udp_ports chain
$NFT add rule inet minifirewall minifirewall_input 'ip saddr @minifirewall_trusted_v4_ips meta l4proto udp ct state new jump private_udp_ports'
$NFT add rule inet minifirewall minifirewall_input 'ip6 saddr @minifirewall_trusted_v6_ips meta l4proto udp ct state new jump private_udp_ports'
# New TCP traffic from trusted IPs jumps to the private_tcp_ports chain
$NFT add rule inet minifirewall minifirewall_input 'ip saddr @minifirewall_trusted_v4_ips meta l4proto tcp tcp flags & (fin|syn|rst|ack) == syn ct state new jump private_tcp_ports'
$NFT add rule inet minifirewall minifirewall_input 'ip6 saddr @minifirewall_trusted_v6_ips meta l4proto tcp tcp flags & (fin|syn|rst|ack) == syn ct state new jump private_tcp_ports'
# New UDP traffic from trusted IPs and privileged IPs jumps to the semipublic_udp_ports chain
$NFT add rule inet minifirewall minifirewall_input 'ip saddr @minifirewall_privileged_v4_ips meta l4proto udp ct state new jump semipublic_udp_ports'
$NFT add rule inet minifirewall minifirewall_input 'ip6 saddr @minifirewall_privileged_v6_ips meta l4proto udp ct state new jump semipublic_udp_ports'
$NFT add rule inet minifirewall minifirewall_input 'ip saddr @minifirewall_trusted_v4_ips meta l4proto udp ct state new jump semipublic_udp_ports'
$NFT add rule inet minifirewall minifirewall_input 'ip6 saddr @minifirewall_trusted_v6_ips meta l4proto udp ct state new jump semipublic_udp_ports'
# New TCP traffic from trusted IPs and privileged IPs jumps to the semipublic_tcp_ports chain
$NFT add rule inet minifirewall minifirewall_input 'ip saddr @minifirewall_privileged_v4_ips meta l4proto tcp tcp flags & (fin|syn|rst|ack) == syn ct state new jump semipublic_tcp_ports'
$NFT add rule inet minifirewall minifirewall_input 'ip6 saddr @minifirewall_privileged_v6_ips meta l4proto tcp tcp flags & (fin|syn|rst|ack) == syn ct state new jump semipublic_tcp_ports'
$NFT add rule inet minifirewall minifirewall_input 'ip saddr @minifirewall_trusted_v4_ips meta l4proto tcp tcp flags & (fin|syn|rst|ack) == syn ct state new jump semipublic_tcp_ports'
$NFT add rule inet minifirewall minifirewall_input 'ip6 saddr @minifirewall_trusted_v6_ips meta l4proto tcp tcp flags & (fin|syn|rst|ack) == syn ct state new jump semipublic_tcp_ports'
# New UDP traffic from any other IP jumps to the public_udp_ports chain
$NFT add rule inet minifirewall minifirewall_input 'meta l4proto udp ct state new jump public_udp_ports'
# New TCP traffic from any other IP jumps to the public_tcp_ports chain
$NFT add rule inet minifirewall minifirewall_input 'meta l4proto tcp tcp flags & (fin|syn|rst|ack) == syn ct state new jump public_tcp_ports'
# Reject all traffic that was not processed by other rules
$NFT add rule inet minifirewall minifirewall_input meta l4proto udp reject
$NFT add rule inet minifirewall minifirewall_input meta l4proto tcp reject with tcp reset
# Feed public_tcp_ports chain with public TCP ports
for x in $SERVICESTCP1
do
$NFT add rule inet minifirewall public_tcp_ports tcp dport $x accept
done
# Feed public_udp_ports chain with public UDP ports
for x in $SERVICESUDP1
do
$NFT add rule inet minifirewall public_tcp_ports udp dport $x accept
done
# Feed semipublic_tcp_ports chain with semi-public TCP ports
for x in $SERVICESTCP2
do
$NFT add rule inet minifirewall semipublic_tcp_ports tcp dport $x accept
done
# Feed semipublic_udp_ports chain with semi-public UDP ports
for x in $SERVICESUDP2
do
$NFT add rule inet minifirewall semipublic_udp_ports udp dport $x accept
done
# Feed private_tcp_ports chain with private TCP ports
for x in $SERVICESTCP3
do
$NFT add rule inet minifirewall private_tcp_ports tcp dport $x accept
done
# Feed private_udp_ports chain with private UDP ports
for x in $SERVICESUDP3
do
$NFT add rule inet minifirewall private_udp_ports udp dport $x accept
done
# Feed protected_tcp_ports chain with protected TCP ports
for x in $SERVICESTCP1p
do
$NFT add rule inet minifirewall protected_tcp_ports tcp dport $x drop
done
# Feed protected_udp_ports chain with protected UDP ports
for x in $SERVICESUDP1p
do
$NFT add rule inet minifirewall protected_udp_ports udp dport $x drop
done
#####################################
## Output traffic / external services
#####################################
# Add set with $DNSSERVERS elements
$NFT add set inet minifirewall minifirewall_dnsservers { type ipv4_addr\;}
if [ ! -z $DNSSERVEURS ]
then
if echo $DNSSERVEURS | grep -q "0.0.0.0/0"
then
# If 0.0.0.0/0 is present we allow any output on TCP/UDP port 53
$NFT add rule inet minifirewall minifirewall_output udp dport 53 counter accept
$NFT add rule inet minifirewall minifirewall_output tcp dport 53 counter accept
else
# Else we add each element to the minifirewall_dnsservers set and allow this set to be reached on TCP/UDP port 53
$NFT add element inet minifirewall minifirewall_dnsservers {$(echo $DNSSERVEURS | sed 's/ /, /g')}
$NFT add rule inet minifirewall minifirewall_output ip daddr @minifirewall_dnsservers udp dport 53 counter accept
$NFT add rule inet minifirewall minifirewall_output ip daddr @minifirewall_dnsservers tcp dport 53 counter accept
fi
fi
# Add set with $HTTPSITES elements
$NFT add set inet minifirewall minifirewall_httpsites { type ipv4_addr\;}
if [ ! -z $HTTPSITES ]
then
if echo $HTTPSITES | grep -q "0.0.0.0/0"
then
# If 0.0.0.0/0 is present we allow any output on TCP port 80
$NFT add rule inet minifirewall minifirewall_output tcp dport 80 counter accept
else
# Else we add each element to the minifirewall_httpsites set and allow this set to be reach on TCP port 80
$NFT add element inet minifirewall minifirewall_httpsites {$(echo $HTTPSITES | sed 's/ /, /g')}
$NFT add rule inet minifirewall minifirewall_output ip daddr @minifirewall_httpsites tcp dport 80 counter accept
fi
fi
# Add set with $HTTPSSITES elements
$NFT add set inet minifirewall minifirewall_httpssites { type ipv4_addr\;}
if [ ! -z $HTTPSSITES ]
then
if echo $HTTPSSITES | grep -q "0.0.0.0/0"
then
# If 0.0.0.0/0 is present we allow any output on TCP port 443
$NFT add rule inet minifirewall minifirewall_output tcp dport 443 counter accept
else
# Else we add each element to the minifirewall_httpssites set and allow this set to be reach on TCP port 443
$NFT add element inet minifirewall minifirewall_httpssites {$(echo $HTTPSSITES | sed 's/ /, /g')}
$NFT add rule inet minifirewall minifirewall_output ip daddr @minifirewall_httpssites tcp dport 443 counter accept
fi
fi
# Add set with $FTPSITES elements
$NFT add set inet minifirewall minifirewall_ftpsites { type ipv4_addr\;}
if [ ! -z $FTPSITES ]
then
if echo $FTPSITES | grep -q "0.0.0.0/0"
then
# If 0.0.0.0/0 is present we allow any output on TCP ports 20, 21, 1024-65535
$NFT add rule inet minifirewall minifirewall_output tcp dport {20, 21, 1024-65535} counter accept
else
# Else we add each element to the minifirewall_ftpsites set and allow this set to be reach on TCP ports 20, 21, 1024-65535
$NFT add element inet minifirewall minifirewall_ftpsites {$(echo $FTPSITES | sed 's/ /, /g')}
$NFT add rule inet minifirewall minifirewall_output ip daddr @minifirewall_ftpsites tcp dport {20, 21, 1024-65535} counter accept
fi
fi
# Add set with $SSHOK elements
$NFT add set inet minifirewall minifirewall_sshok { type ipv4_addr\;}
if [ ! -z $SSHOK ]
then
if echo $SSHOK | grep -q "0.0.0.0/0"
then
# If 0.0.0.0/0 is present we allow any output on TCP port 22
$NFT add rule inet minifirewall minifirewall_output tcp dport 22 counter accept
else
# Else we add each element to the minifirewall_sshok set and allow this set to be reach on TCP port 22
$NFT add element inet minifirewall minifirewall_sshok {$(echo $SSHOK | sed 's/ /, /g')}
$NFT add rule inet minifirewall minifirewall_output ip daddr @minifirewall_sshok tcp dport 22 counter accept
fi
fi
# Add set with $SMTPOK elements
$NFT add set inet minifirewall minifirewall_smtpok { type ipv4_addr\;}
if [ ! -z $SMTPOK ]
then
if echo $SMTPOK | grep -q "0.0.0.0/0"
then
# If 0.0.0.0/0 is present we allow any output on TCP port 25
$NFT add rule inet minifirewall minifirewall_output tcp dport 25 counter accept
else
# Else we add each element to the minifirewall_smtpok set and allow this set to be reach on TCP port 25
$NFT add element inet minifirewall minifirewall_smtpok {$(echo $SMTPOK | sed 's/ /, /g')}
$NFT add rule inet minifirewall minifirewall_output ip daddr @minifirewall_smtpok tcp dport 25 counter accept
fi
fi
# Add set with $SMTPSECUREOK elements
$NFT add set inet minifirewall minifirewall_smtpsecureok { type ipv4_addr\;}
if [ ! -z $SMTPSECUREOK ]
then
if echo $SMTPSECUREOK | grep -q "0.0.0.0/0"
then
# If 0.0.0.0/0 is present we allow any output on TCP ports 465 and 587
$NFT add rule inet minifirewall minifirewall_output tcp dport {465, 587} counter accept
else
# Else we add each element to the minifirewall_smtpsecureok set and allow this set to be reach on TCP ports 465 and 587
$NFT add element inet minifirewall minifirewall_smtpsecureok {$(echo $SMTPSECUREOK | sed 's/ /, /g')}
$NFT add rule inet minifirewall minifirewall_output ip daddr @minifirewall_smtpsecureok tcp dport {465, 587} counter accept
fi
fi
# Add set with $NTPOK elements
$NFT add set inet minifirewall minifirewall_ntpok { type ipv4_addr\;}
if [ ! -z $NTPOK ]
then
if echo $NTPOK | grep -q "0.0.0.0/0"
then
# If 0.0.0.0/0 is present we allow any output on TCP ports 123
$NFT add rule inet minifirewall minifirewall_output tcp dport 123 counter accept
else
# Else we add each element to the minifirewall_smtpsecureok set and allow this set to be reach on TCP port 123
$NFT add element inet minifirewall minifirewall_ntpok {$(echo $NTPOK | sed 's/ /, /g')}
$NFT add rule inet minifirewall minifirewall_output ip daddr @minifirewall_ntpok tcp dport 123 counter accept
fi
fi
# If specified, we add per host output autorisation
if [ -n $OUTPUTOK ]
then
for item in $(echo $OUTPUTOK)
do
ip=$(echo $item | awk -F'!' '{print $1}')
port=$(echo $item | awk -F'!' '{print $2}')
$NFT add rule inet minifirewall minifirewall_output ip daddr $ip tcp dport $port counter accept
done
fi
# Related and established traffic is accepted
$NFT add rule inet minifirewall minifirewall_output ct state established,related accept
# ICMP and IGMP traffic is accepted
$NFT add rule inet minifirewall minifirewall_output ip protocol icmp accept
$NFT add rule inet minifirewall minifirewall_output meta l4proto ipv6-icmp accept
$NFT add rule inet minifirewall minifirewall_output ip protocol igmp accept
trap - INT TERM EXIT
echo "...starting NFTables rules is now finish : OK"
exit 0

31
minifirewall-stop.sh Executable file
View File

@ -0,0 +1,31 @@
#!/bin/sh
# minifirewall
# See https://gitea.evolix.org/evolix/minifirewall
# Copyright (c) 2007-2020 Evolix
# 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.
# Description
# script for standalone server
# Stop minifirewall
#
# Variables configuration
#########################
# nft path
NFT=/usr/sbin/nft
echo "Flush all rules and accept everything..."
# Flush everything
$NFT flush ruleset
echo "...flushing IPTables rules is now finish : OK"
exit 0

View File

@ -1,6 +1,4 @@
# Configuration for minifirewall : https://forge.evolix.org/projects/minifirewall
# For fun, we keep last change from first CVS repository:
# version 0.1 - 12 juillet 2007 $Id: firewall.rc,v 1.2 2007/07/12 19:08:59 reg Exp $
# Main interface
INT='eth0'
@ -21,9 +19,9 @@ INTLAN='192.168.0.2/32'
# Trusted IPv4 addresses for private and semi-public services
TRUSTEDIPS=''
# Privilegied IPv4 addresses for semi-public services
# Privileged IPv4 addresses for semi-public services
# (no need to add again TRUSTEDIPS)
PRIVILEGIEDIPS=''
PRIVILEGEDIPS=''
# Local services IPv4/IPv6 restrictions
@ -77,27 +75,6 @@ SMTPSECUREOK=''
# NTP authorizations
NTPOK='0.0.0.0/0'
# IPv6 Specific rules
#####################
# Example: allow input HTTP/HTTPS/SMTP/DNS traffic
/sbin/ip6tables -A INPUT -i $INT -p tcp --sport 80 --match state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/ip6tables -A INPUT -i $INT -p tcp --sport 443 --match state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/ip6tables -A INPUT -i $INT -p tcp --sport 25 --match state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/ip6tables -A INPUT -i $INT -p udp --sport 53 --match state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/ip6tables -A INPUT -i $INT -p tcp --sport 53 --match state --state ESTABLISHED,RELATED -j ACCEPT
# Example: allow output DNS, NTP and traceroute traffic
/sbin/ip6tables -A OUTPUT -o $INT -p udp --dport 53 --match state --state NEW -j ACCEPT
/sbin/ip6tables -A OUTPUT -o $INT -p udp --dport 123 --match state --state NEW -j ACCEPT
#/sbin/ip6tables -A OUTPUT -o $INT -p udp --dport 33434:33523 --match state --state NEW -j ACCEPT
# Example: allow DHCPv6
/sbin/ip6tables -A INPUT -i $INT -p udp --dport 546 -d fe80::/64 -j ACCEPT
/sbin/ip6tables -A OUTPUT -o $INT -p udp --dport 547 -j ACCEPT
# IPv4 Specific rules
#####################
# /sbin/iptables ...
# Per host output autorisations (IP!Port)
# OUTPUTOK='203.0.113.1!42 203.0.113.2!43'
OUTPUTOK=''

14
minifirewall.service Normal file
View File

@ -0,0 +1,14 @@
[Unit]
Description=minifirewall
Documentation=https://gitea.evolix.org/evolix/minifirewall
After=default.target
[Service]
Type=oneshot
ExecStart=/usr/sbin/minifirewall-start.sh
RemainAfterExit=yes
ExecStop=/usr/sbin/minifirewall-stop.sh
StandardOutput=journal
[Install]
WantedBy=multi-user.target