Firewall rulesets from firewalld, SuSEfirewall2 or ufw are not strict enough. Here a simple solution for firewall ruleset “tuning”. Example for SLED15 SP1 with firewalld and Netfilter/iptables:
/usr/local/sbin/firewallFineTuning.sh
[CODE]#!/bin/bash
########################################################
/usr/local/sbin/firewallFineTuning.sh
Andreas Meyer, 29.02.2020
Firewall-Einstellungen korrigieren
########################################################
echo “Firewall-Einstellungen korrigieren…”
Spoof-Schutz für IPv4 aktivieren => wegen NetworkManager < v1.14.6
/usr/sbin/iptables -w 20 -t raw -A PREROUTING -m rpfilter --invert -j DROP
ICMPv6-Regeln entfernen
/usr/sbin/ip6tables -w 20 -t raw -D PREROUTING -p icmpv6 --icmpv6-type 135 -j ACCEPT
/usr/sbin/ip6tables -w 20 -t raw -D PREROUTING -p icmpv6 --icmpv6-type 134 -j ACCEPT
Zugelassene Dienste ausgehend für IPv4 vorbereiten
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -m conntrack --ctstate INVALID -j DROP
Zugelassene Dienste ausgehend für IPv6 vorbereiten
/usr/sbin/ip6tables -w 20 -t filter -A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
/usr/sbin/ip6tables -w 20 -t filter -A OUTPUT -m conntrack --ctstate INVALID -j DROP
Lasche Loopback-Regeln von IPv4 entfernen
/usr/sbin/iptables -w 20 -t filter -D INPUT -i lo -j ACCEPT
/usr/sbin/iptables -w 20 -t filter -D FORWARD -i lo -j ACCEPT
Strengere Loopback-Regeln für IPv4 einsetzen
/usr/sbin/iptables -w 20 -t filter -A INPUT -i lo -p tcp -s 127.0.0.0/8 -d 127.0.0.0/8 -m conntrack --ctstate NEW -j ACCEPT
/usr/sbin/iptables -w 20 -t filter -A INPUT -i lo -p udp -s 127.0.0.0/8 -d 127.0.0.0/8 -m conntrack --ctstate NEW -j ACCEPT
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o lo -p tcp -s 127.0.0.0/8 -d 127.0.0.0/8 -m conntrack --ctstate NEW -j ACCEPT
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o lo -p udp -s 127.0.0.0/8 -d 127.0.0.0/8 -m conntrack --ctstate NEW -j ACCEPT
Lasche Loopback-Regeln von IPv6 entfernen
/usr/sbin/ip6tables -w 20 -t filter -D INPUT -i lo -j ACCEPT
/usr/sbin/ip6tables -w 20 -t filter -D FORWARD -i lo -j ACCEPT
Strengere Loopback-Regeln für IPv6 einsetzen
/usr/sbin/ip6tables -w 20 -t filter -A INPUT -i lo -p tcp -s ::1/128 -d ::1/128 -m conntrack --ctstate NEW -j ACCEPT
/usr/sbin/ip6tables -w 20 -t filter -A INPUT -i lo -p udp -s ::1/128 -d ::1/128 -m conntrack --ctstate NEW -j ACCEPT
/usr/sbin/ip6tables -w 20 -t filter -A OUTPUT -o lo -p tcp -s ::1/128 -d ::1/128 -m conntrack --ctstate NEW -j ACCEPT
/usr/sbin/ip6tables -w 20 -t filter -A OUTPUT -o lo -p udp -s ::1/128 -d ::1/128 -m conntrack --ctstate NEW -j ACCEPT
Zugelassene Dienste ausgehend
DNS (ausgehend)
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o eth0 -p tcp --dport=53 -m conntrack --ctstate NEW -j ACCEPT
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o eth0 -p udp --dport=53 -m conntrack --ctstate NEW -j ACCEPT
DHCP (ausgehend)
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o eth0 -p udp --dport=67 -m conntrack --ctstate NEW -j ACCEPT
NTP (ausgehend)
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o eth0 -p udp --dport=123 -m conntrack --ctstate NEW -j ACCEPT
ICMP Echo (Ping) (ausgehend)
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o eth0 -p icmp --icmp-type echo-request -m conntrack --ctstate NEW -j ACCEPT
HTTP (ausgehend)
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o eth0 -p tcp --dport=80 -m conntrack --ctstate NEW -j ACCEPT
HTTPS (ausgehend)
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o eth0 -p tcp --dport=443 -m conntrack --ctstate NEW -j ACCEPT
POP3S (ausgehend)
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o eth0 -p tcp --dport=995 -m conntrack --ctstate NEW -j ACCEPT
SMTPS (ausgehend)
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o eth0 -p tcp --dport=465 -m conntrack --ctstate NEW -j ACCEPT
SSH (ausgehend)
/usr/sbin/iptables -w 20 -t filter -A OUTPUT -o eth0 -p tcp --dport=22 -m conntrack --ctstate NEW -j ACCEPT
Policies der Default-Chains von IPv4 anpassen
/usr/sbin/iptables -w 20 -t filter --policy INPUT DROP
/usr/sbin/iptables -w 20 -t filter --policy OUTPUT DROP
/usr/sbin/iptables -w 20 -t filter --policy FORWARD DROP
Policies der Default-Chains von IPv6 anpassen
/usr/sbin/ip6tables -w 20 -t filter --policy INPUT DROP
/usr/sbin/ip6tables -w 20 -t filter --policy OUTPUT DROP
/usr/sbin/ip6tables -w 20 -t filter --policy FORWARD DROP
Drop-Regeln in IPv4 entfernen
/usr/sbin/iptables -w 20 -t filter -D IN_public -j DROP
/usr/sbin/iptables -w 20 -t filter -D FWDI_public -j DROP
/usr/sbin/iptables -w 20 -t filter -D FWDO_public -j DROP
Drop-Regeln in IPv6 entfernen
/usr/sbin/ip6tables -w 20 -t filter -D IN_public -j DROP
/usr/sbin/ip6tables -w 20 -t filter -D FWDI_public -j DROP
/usr/sbin/ip6tables -w 20 -t filter -D FWDO_public -j DROP
Reject-Regeln in IPv4 entfernen
/usr/sbin/iptables -w 20 -t filter -D INPUT -j REJECT --reject-with icmp-host-prohibited
/usr/sbin/iptables -w 20 -t filter -D FORWARD -j REJECT --reject-with icmp-host-prohibited
Reject-Regeln in IPv6 entfernen
/usr/sbin/ip6tables -w 20 -t filter -D INPUT -j REJECT --reject-with icmp6-adm-prohibited
/usr/sbin/ip6tables -w 20 -t filter -D FORWARD -j REJECT --reject-with icmp6-adm-prohibited
Strikte Paketkontrollen in der SPI-Firewall aktivieren
/usr/sbin/sysctl -w net.netfilter.nf_conntrack_tcp_loose=0[/CODE]
/etc/systemd/system/firewallFineTuning.service
[CODE][Unit]
Description=firewallFineTuning
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/firewallFineTuning.sh[/CODE]
/etc/systemd/system/firewallFineTuning.timer
[CODE][Unit]
Description=firewallFineTuning.timer
[Timer]
OnStartupSec=1min
AccuracySec=1s
[Install]
WantedBy=multi-user.target[/CODE]
Set file permissions:
[CODE]# su
chmod u=rwx,g=,o= /usr/local/sbin/firewallFineTuning.sh
chmod u=rw,g=r,o=r /etc/systemd/system/firewallFineTuning.service
chmod u=rw,g=r,o=r /etc/systemd/system/firewallFineTuning.timer[/CODE]
Enable this new systemd unit:
[CODE]# su
systemctl enable firewallFineTuning.timer
systemctl start firewallFineTuning.timer
systemctl status firewallFineTuning.timer
systemctl status firewallFineTuning.service[/CODE]
Reboot and check the new systemd unit:
[CODE]# su
systemctl status firewallFineTuning.timer
systemctl status firewallFineTuning.service
iptables -t raw -L -n -v|more
ip6tables -t raw -L -n -v|more
iptables -t filter -L -n -v|more
ip6tables -t filter -L -n -v|more
sysctl net.netfilter.nf_conntrack_tcp_loose[/CODE]