Eine einfache stateful Firewall mit iptables

Mit iptables kann man unter Linux eine Firewall konfigurieren. In diesem Artikel zeige ich die wichtigsten Befehle und Arbeitsschritte um eine Firewall für IPv4 und IPv6 einzurichten.

Hilfsvariabeln und Hilfsfunktionen

Folgende Hilfsvariabeln vereinfachen einem die kontrolle über de einzelnen Befehle und Konfigurationsdateien:

IPTABLES="/sbin/iptables"
IPTABLES_SAVE="/sbin/iptables-save"
IPTABLESCONFIG="/etc/iptables/iptables.rules"
IP6TABLES="/sbin/ip6tables"
IP6TABLES_SAVE="/sbin/ip6tables-save"
IP6TABLESCONFIG="/etc/iptables/ip6tables.rules"
IP46TABLES="ip46tables"

Die Hilfsfunktion ip64tables verwende ich, um Befehle gleichzeitig mit iptables und ip6tables auszuführen. Somit verhindert man den Fehler, dass man Regeln nur für IPv4 und nicht für IPv6 erstellt:

ip46tables(){
$IPTABLES $@
$IP6TABLES $@
}

Firewall initialisieren

Zuerst werden alle bestehenden Regeln gelöscht:

$IP46TABLES -F INPUT
$IP46TABLES -F OUTPUT
$IP46TABLES -F FORWARD

Als restriktive Firewall sollen standarsmässig alle Pakete verworfen (Target DROP) werden:

$IP46TABLES -P INPUT DROP
$IP46TABLES -P OUTPUT DROP
$IP46TABLES -P FORWARD DROP

Der gesamte lokale Verkehr soll akzeptiert werden (z. B. für den X-Server oder lokale Sockets).

$IP46TABLES -A INPUT -i lo -j ACCEPT
$IP46TABLES -A OUTPUT -o lo -j ACCEPT

Stateful Firewall

Folgende zwei Regeln helfen einem eine stateful Firewall zu erstellen. Dabei werden mit dem Modul conntrack die Verbindungen beobachtet. Falls eine Verbindung zustande kommt, werden auch alle weiteren Pakete dieser Verbindung zugelassen.

$IP46TABLES -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Stateful_INPUT"
$IP46TABLES -A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Stateful_OUTPUT"

Verbindungen nach Aussen zulassen

Für jeden Service, den man nutzen will, erstellt man eine Regel, welche den ausgehenden Verkehr auf dem gewünschten Port zulässt. Hier am Beispiel whois, welches den Port 43 über TCP verwendet. Damit die stateful Regeln funktionieren, verwendet man das Modul conntrack um die Verbindung zu tracken. Die Antwortpakete auf die ausgehende Verbindung werden somit über die zuvor definierten Regeln der stateful Firewall zugelassen.

$IP46TABLES -A OUTPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT -m comment --comment "SSH"

Verbindungen nach Innen zulassen

Will man selber einen Service anbieten, muss man den gewünschten Port von aussen nach innen zulassen. Wichtig ist auch hier, dass man das Modul conntrack verwendet, damit die Antwortpakete auch gesendet werden können.

$IP46TABLES -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT -m comment --comment "Accept_SSH_IN"

Spezielle IPv4 Konfiguration

Diese Regel lässt eingehende ICMP Echo Requests (ping) zu. Dies Regel gilt nur für IPv4, da IPv6 die Option icmpv6 verwendet. Dabei ist zu beachten, dass jetzt $IPTABLES statt $IP46TABLES verwendet wird.

$IPTABLES -A INPUT -p icmp --icmp-type echo-request -m state --state NEW -j ACCEPT -m comment --comment "ICMP_echo-request"

Spezielle IPv6 Konfiguration

Damit man sich eine IPv6 Adresse generieren kann, muss man zuerst den Präfix vom Router erhalten. Dies geschieht über die Router Advertisments und Router Solicitations.

$IP6TABLES -A INPUT -p icmpv6 --icmpv6-type router-advertisement -j ACCEPT -m comment --comment "ICMPv6_router-advertisement"
$IP6TABLES -A INPUT -p icmpv6 --icmpv6-type router-solicitation -j ACCEPT -m comment --comment "ICMPv6_router-solicitation"
$IP6TABLES -A INPUT -p icmpv6 --icmpv6-type echo-request -j ACCEPT -m comment --comment "ICMPv6_echo-request"
$IP6TABLES -A INPUT -p icmpv6 --icmpv6-type echo-reply -j ACCEPT -m comment --comment "ICMPv6_echo-reply"

Die Namensauflösung der IPv6 Adressen in MAC-Adressen geschieht über die Neighbor Discovery mittels Neighbor Solicitations und Neighbor Advertisements.

$IP6TABLES -A INPUT -p icmpv6 --icmpv6-type neighbour-advertisement -j ACCEPT -m comment --comment "ICMPv6_neighbour-advertisement"
$IP6TABLES -A INPUT -p icmpv6 --icmpv6-type neighbour-solicitation -j ACCEPT -m comment --comment "ICMPv6_neighbour-solicitation"

Kernelmodule

Damit FTP funktioniert, muss das Kernelmodul nf_conntrack_ftp geladen sein. Das kann man manuell mit modprobe machen:

$ sudo modprobe nf_conntrack_ftp

Oder mit der Datei /etc/modules-load.d/iptables.conf:

$ cat /etc/modules-load.d/iptables.conf
nf_conntrack_ftp

Firewall Skript

Das ganze kann man jetzt schön in ein Skript packen, damit man iptables gut kontrollieren kann. Ich verwende diese Firewall unter Arch Linux. Mein Firewall-Skript sieht folgendermassen aus:

#!/bin/bash
#
# firewall - iptables firewall
#
<code lang="bash">

IPTABLES=”/sbin/iptables”
IPTABLES_SAVE=”/sbin/iptables-save”
IPTABLESCONFIG=”/etc/iptables/iptables.rules”
IP6TABLES=”/sbin/ip6tables”
IP6TABLES_SAVE=”/sbin/ip6tables-save”
IP6TABLESCONFIG=”/etc/iptables/ip6tables.rules”
IP46TABLES=”ip46tables”
SYSCTL=”/sbin/sysctl”

<code lang="bash">

help(){
cat << EOI Usage: firewall (start|stop|status|save|help) EOI } ip46tables(){ $IPTABLES $@ $IP6TABLES $@ } start(){ echo “[+] Starting firewall… ” $IP46TABLES -F INPUT $IP46TABLES -F OUTPUT $IP46TABLES -F FORWARD $IP46TABLES -P INPUT DROP $IP46TABLES -P OUTPUT DROP $IP46TABLES -P FORWARD DROP $IP46TABLES -A INPUT -i lo -j ACCEPT $IP46TABLES -A OUTPUT -o lo -j ACCEPT hardening output_chain input_chain echo “[+] Firewall started” } stop(){ echo -n “[+] Stopping firewall… ” $IP46TABLES -F INPUT $IP46TABLES -F OUTPUT $IP46TABLES -F FORWARD $IP46TABLES -P INPUT ACCEPT $IP46TABLES -P OUTPUT ACCEPT $IP46TABLES -P FORWARD ACCEPT echo “Done.” } status(){ $IP46TABLES –version $IP46TABLES -vnL –line-numbers } save(){ echo -n “[+] Saving iptables rules… ” $IPTABLES_SAVE > “$IPTABLESCONFIG”
echo “Done. (`wc -l $IPTABLESCONFIG | cut -d’ ‘ -f1` Lines saved.)”
echo -n “[+] Saving ip6tables rules… ”
$IP6TABLES_SAVE > “$IP6TABLESCONFIG”
echo “Done. (`wc -l $IP6TABLESCONFIG | cut -d’ ‘ -f1` Lines saved.)”
}

<code lang="bash">

input_chain(){
echo -n “[+] Applying input rules… ”

<code lang="bash">

$IP46TABLES -A INPUT -m conntrack –ctstate RELATED,ESTABLISHED -j ACCEPT -m comment –comment “Stateful_INPUT”

<code lang="bash">

$IPTABLES -A INPUT -p icmp –icmp-type echo-request -m state –state NEW -j ACCEPT -m comment –comment “ICMP_echo-request”
$IP6TABLES -A INPUT -p icmpv6 –icmpv6-type router-advertisement -j ACCEPT -m comment –comment “ICMPv6_router-advertisement”
$IP6TABLES -A INPUT -p icmpv6 –icmpv6-type router-solicitation -j ACCEPT -m comment –comment “ICMPv6_router-solicitation”
$IP6TABLES -A INPUT -p icmpv6 –icmpv6-type neighbour-advertisement -j ACCEPT -m comment –comment “ICMPv6_neighbour-advertisement”
$IP6TABLES -A INPUT -p icmpv6 –icmpv6-type neighbour-solicitation -j ACCEPT -m comment –comment “ICMPv6_neighbour-solicitation”
$IP6TABLES -A INPUT -p icmpv6 –icmpv6-type echo-request -j ACCEPT -m comment –comment “ICMPv6_echo-request”
$IP6TABLES -A INPUT -p icmpv6 –icmpv6-type echo-reply -j ACCEPT -m comment –comment “ICMPv6_echo-reply”

<code lang="bash">

$IP46TABLES -A INPUT -p tcp –dport 22 -m conntrack –ctstate NEW -m recent –set –name SSH –rsource -m comment –comment “SSH”
$IP46TABLES -A INPUT -p tcp –dport 22 -m recent –rcheck –seconds 60 –hitcount 4 –rttl –name SSH –rsource -m limit –limit 1/min –limit-burst 1 -j LOG –log-prefix “SSH_Brute-Force”
$IP46TABLES -A INPUT -p tcp –dport 22 -m recent –update –seconds 60 –hitcount 4 –rttl –name SSH –rsource -j REJECT –reject-with tcp-reset -m comment –comment “Accept_SSH_IN”
$IP46TABLES -A INPUT -p tcp –dport 22 -m conntrack –ctstate NEW -j LOG –log-prefix “Accept_SSH_IN”
$IP46TABLES -A INPUT -p tcp –dport 22 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Accept_SSH_IN”

<code lang="bash">

PHONE=”5C:0A:5B:93:3B:CB”
$IP46TABLES -A INPUT -p tcp –dport 6600 -m conntrack –ctstate NEW -m mac –mac-source $PHONE -m limit –limit 1/min –limit-burst 1 -j LOG –log-prefix “Accept_MPD_IN” -m comment –comment “MPD_Phone”
$IP46TABLES -A INPUT -p tcp –dport 6600 -m conntrack –ctstate NEW -m mac –mac-source $PHONE -j ACCEPT -m comment –comment “MPD_Phone”

<code lang="bash">

# Drop
$IP46TABLES -A INPUT -p tcp –dport 23 -m conntrack –ctstate NEW -m limit –limit 1/min –limit-burst 1 -j LOG –log-prefix “Drop_Telnet_IN ” -m comment –comment “Telnet”
$IP46TABLES -A INPUT -p tcp –dport 23 -m conntrack –ctstate NEW -j DROP
$IPTABLES -A INPUT -m addrtype –dst-type BROADCAST -j DROP
$IPTABLES -A INPUT -m addrtype –dst-type MULTICAST -j DROP

<code lang="bash">

# Log
$IP46TABLES -A INPUT ! -i lo -m limit –limit 1/min –limit-burst 1 -j LOG –log-prefix “DROP_IN ” –log-ip-options –log-tcp-options # Log all other packages

<code lang="bash">

echo “Done.”
}

<code lang="bash">

output_chain(){
echo -n “[+] Applying output rules… ”

<code lang="bash">

$IP46TABLES -A OUTPUT -m conntrack –ctstate RELATED,ESTABLISHED -j ACCEPT -m comment –comment “Stateful_OUTPUT”

<code lang="bash">

$IPTABLES -A OUTPUT -p icmp –icmp-type echo-request -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “ICMP_echo-request”
$IP6TABLES -A OUTPUT -p icmpv6 –icmpv6-type router-advertisement -j ACCEPT -m comment –comment “ICMPv6_router-advertisement”
$IP6TABLES -A OUTPUT -p icmpv6 –icmpv6-type router-solicitation -j ACCEPT -m comment –comment “ICMPv6_router-solicitation”
$IP6TABLES -A OUTPUT -p icmpv6 –icmpv6-type neighbour-advertisement -j ACCEPT -m comment –comment “ICMPv6_neighbour-advertisement”
$IP6TABLES -A OUTPUT -p icmpv6 –icmpv6-type neighbour-solicitation -j ACCEPT -m comment –comment “ICMPv6_neighbour-solicitation”
$IP6TABLES -A OUTPUT -p icmpv6 –icmpv6-type echo-request -j ACCEPT -m comment –comment “ICMPv6_echo-request”
$IP6TABLES -A OUTPUT -p icmpv6 –icmpv6-type echo-reply -j ACCEPT -m comment –comment “ICMPv6_echo-reply”

<code lang="bash">

$IP46TABLES -A OUTPUT -p tcp –dport 21 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “FTP” # (nf_conntrack_ftp muss geladen sein)
$IP46TABLES -A OUTPUT -p tcp –dport 22 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “SSH”
$IP46TABLES -A OUTPUT -p tcp –dport 23 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Telnet”
$IP46TABLES -A OUTPUT -p tcp –dport 43 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Whois”
$IP46TABLES -A OUTPUT -p udp –dport 53 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “DNS_TCP”
$IP46TABLES -A OUTPUT -p tcp –dport 53 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “DNS_UDP”
$IP46TABLES -I OUTPUT -p udp –dport 67:68 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “DHCP”
$IP46TABLES -A OUTPUT -p tcp –dport 80 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “HTTP”
$IP46TABLES -A OUTPUT -p udp –dport 123 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “NTP”
$IP46TABLES -A OUTPUT -p udp –dport 137 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Samba_NETBIOS_Name_Service”
$IP46TABLES -A OUTPUT -p udp –dport 138 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Samba_NETBIOS_Datagram_Service”
$IP46TABLES -A OUTPUT -p tcp –dport 139 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Samba_NETBIOS_Session_Service”
$IP46TABLES -A OUTPUT -p tcp –dport 143 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “IMAP”
$IP46TABLES -A OUTPUT -p tcp –dport 443 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “HTTPS”
$IP46TABLES -A OUTPUT -p tcp –dport 445 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Samba_Microsoft-DS”
$IP46TABLES -A OUTPUT -p tcp –dport 465 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “SMTP_Gmail”
$IP46TABLES -A OUTPUT -p tcp –dport 587 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “SMTP_submission”
$IP46TABLES -A OUTPUT -p udp –dport 500 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “VPN_ISAKMP”
$IP46TABLES -A OUTPUT -p tcp –dport 993 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “IMAPS”
$IP46TABLES -A OUTPUT -p udp –dport 1194 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “OpenVPN”
$IP46TABLES -A OUTPUT -p tcp –dport 2305 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “SSH_2305”
$IP46TABLES -A OUTPUT -p tcp –dport 3690 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “SVN”
$IP46TABLES -A OUTPUT -p udp –dport 4500 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “VPN_IPSEC-NAT”
$IP46TABLES -A OUTPUT -p tcp –dport 5222 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “XMPP_TCP”
$IP46TABLES -A OUTPUT -p udp –dport 5222 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “XMPP_UDP”
$IP46TABLES -A OUTPUT -p tcp –dport 6667 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “IRC”
$IP46TABLES -A OUTPUT -p tcp –dport 6697 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “IRC”
$IP46TABLES -A OUTPUT -p udp –dport 80 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Bittorrent_Tracker”
$IP46TABLES -A OUTPUT -p udp –dport 1337 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Bittorrent_Tracker”
$IP46TABLES -A OUTPUT -p tcp –dport 6969 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Bittorrent_Tracker”
$IP46TABLES -A OUTPUT -p udp –dport 6969 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Bittorrent_Tracker”
$IP46TABLES -A OUTPUT -p tcp –dport 1337 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Bittorrent_Tracker”
$IP46TABLES -A OUTPUT -p udp –dport 1337 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Bittorrent_Tracker”
$IP46TABLES -A OUTPUT -p tcp –dport 6881:6889 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Bittorrent_Clients”
$IP46TABLES -A OUTPUT -p udp –dport 6881:6889 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “Bittorrent_Clients”
$IP46TABLES -A OUTPUT -p tcp –dport 8080 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “HTTP-Alt”
$IP46TABLES -A OUTPUT -p tcp –dport 9418 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “GIT”
$IP46TABLES -A OUTPUT -p tcp –dport 9418 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “GIT”
$IP46TABLES -A OUTPUT -p tcp –dport 11371 -m conntrack –ctstate NEW -j ACCEPT -m comment –comment “PGP_Keyserver_Communication”

<code lang="bash">

echo “Done.”
}

<code lang="bash">

hardening(){
echo -n “[+] Hardening…”

<code lang="bash">

$SYSCTL -q -w net.ipv4.tcp_syncookies=1 # SYN-Flood Attack
$SYSCTL -q -w net.ipv4.icmp_echo_ignore_broadcasts=1 # Smurf-Attack

<code lang="bash">

# (D)DOS
# $IP46TABLES -A INPUT -p tcp –syn -m limit –limit 1/s -j ACCEPT # SYN-Flood
# $IP46TABLES -A INPUT -p tcp –syn -m limit –limit 1/s -j LOG –log-level warning –log-tcp-options –log-ip-options –log-prefix “SYNFLOOD” # SYN

<code lang="bash">

# Portscans
$IP46TABLES -A INPUT -p tcp –tcp-flags ALL NONE -m limit –limit 1/min –limit-burst 1 -j LOG –log-level warning –log-tcp-options –log-ip-options –log-prefix “NULL-Scan” -m comment –comment “NULL-Scan”
$IP46TABLES -A INPUT -p tcp –tcp-flags ALL NONE -j DROP -m comment –comment “NULL-Scan”

<code lang="bash">

$IP46TABLES -A INPUT -p tcp –tcp-flags ALL FIN -m limit –limit 1/min –limit-burst 1 -j LOG –log-level warning –log-tcp-options –log-ip-options –log-prefix “FIN-Scan” -m comment –comment “FIN-Scan”
$IP46TABLES -A INPUT -p tcp –tcp-flags ALL FIN -j DROP -m comment –comment “FIN-Scan”

<code lang="bash">

$IP46TABLES -A INPUT -p tcp –tcp-flags ALL FIN,PSH,URG -m limit –limit 1/min –limit-burst 1 -j LOG –log-level warning –log-tcp-options –log-ip-options –log-prefix “XMAS-SCAN” -m comment –comment “XMAS-Scan”
$IP46TABLES -A INPUT -p tcp –tcp-flags ALL FIN,PSH,URG -j DROP -m comment –comment “XMAS-Scan”
$IP46TABLES -A INPUT -p tcp –tcp-flags ALL SYN,RST,ACK,FIN,URG -m limit –limit 1/min –limit-burst 1 -j LOG –log-level warning –log-tcp-options –log-ip-options –log-prefix “XMAS-PSH-Scan” -m comment –comment “XMAS-PSH-Scan”
$IP46TABLES -A INPUT -p tcp –tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP -m comment –comment “XMAS-PSH-Scan”
$IP46TABLES -A INPUT -p tcp –tcp-flags ALL ALL -m limit –limit 1/min –limit-burst 1 -j LOG –log-level warning –log-tcp-options –log-ip-options –log-prefix “XMAS-All-Scan” -m comment –comment “XMAS-All-Scan”
$IP46TABLES -A INPUT -p tcp –tcp-flags ALL ALL -j DROP -m comment –comment “XMAS-All-Scan”

<code lang="bash">

$IP46TABLES -A INPUT -p udp -m limit –limit 1/min –limit-burst 1 -m length –length 0:28 -j LOG –log-prefix “UDP-Scan_Empty_Package” -m comment –comment “UDP-Scan_Empty_UDP_Package”
$IP46TABLES -A INPUT -p udp -m length –length 0:28 -j DROP -m comment –comment “UDP-Scan_Empty_UDP_Package”

<code lang="bash">

$IP46TABLES -A INPUT -p tcp ! –syn -m conntrack –ctstate NEW -m limit –limit 1/min –limit-burst 1 -j LOG –log-ip-options –log-tcp-options –log-prefix “New_connection_not_SYN” -m comment –comment “New_connection_not_SYN ”
$IP46TABLES -A INPUT -p tcp ! –syn -m conntrack –ctstate NEW -j DROP -m comment –comment “New_connection_not_SYN ”

<code lang="bash">

$IP46TABLES -A INPUT -p tcp -m multiport –dports 23,5900 –tcp-flags ALL SYN -m limit –limit 1/m –limit-burst 1 -j LOG –log-prefix “SYN-Scan_Trap” -m comment –comment “SYN-Scan_Trap”
$IP46TABLES -A INPUT -p tcp -m multiport –dports 23,5900 –tcp-flags ALL SYN -j DROP -m comment –comment “SYN-Scan_Trap”

<code lang="bash">

# $IP46TABLES -A INPUT -m conntrack –ctstate INVALID -m limit –limit 1/h –limit-burst 1 -j LOG –log-ip-options –log-tcp-options –log-prefix “DROP_INVALID ”
$IP46TABLES -A INPUT -m conntrack –ctstate INVALID -j DROP -m comment –comment “Invalid_Packets ”

<code lang="bash">

# DOS Attacks (Denial of service)

<code lang="bash">

echo “Done.”
}

<code lang="bash">

if [ “$UID” -ne “0” ]
then
echo “[!] Only root can run this script.”
exit 1
fi

 
case "$1"
in
"start") start ;;
"stop") stop ;;
"restart") stop; start ;;
"status") status ;;
"save") save ;;
"help") help ;;
*) help; exit 1 ;;
esac

Firewall kontrollieren

So startet man die Firewall:

$ sudo ./firewall start
[+] Starting firewall...
[+] Applying output rules... Done.
[+] Applying input rules... Done.
[+] Firewall started

Und wieder stoppen:

$ sudo firewall stop
[+] Stopping firewall... Done.

Firewall als Service aktivieren

Nach einem Reboot sind die Regeln wieder weg. Deshalb kann man den iptables Service aktivieren und gleich starten:

$ sudo systemctl enable iptables
$ sudo systemctl start iptables

Regeln speichern

Dieser Service liest die iptables Regeln von der Datei /etc/iptables/iptables.rules bzw. /etc/iptables/ip6tables.rules. Damit diese Dateien die iptables Regeln erhalten, speichert man diese so:

$ sudo ./firewall save
[+] Saving iptables rules... Done. (55 Lines saved.)
[+] Saving ip6tables rules... Done. (14 Lines saved.)

Regeln anschauen

Die Regeln kann man in diesen Dateien oder direkt mit dem firewall Script anschauen:

$ sudo ./firewall status
[+] iptables:
iptables v1.4.20
Chain INPUT (policy DROP 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
2        3   120 LOG        all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID LOG flags 6 level 4 prefix "DROP INVALID "
3        3   120 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
4       69  9510 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
5        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8 state NEW
6        0     0 LOG        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW LOG flags 0 level 4 prefix "ALLOW SSH IN "
7        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW
8        0     0 LOG        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:6600 ctstate NEW MAC 5C:0A:5B:93:3B:CB LOG flags 0 level 4 prefix "ALLOW MPD IN "
9        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:6600 ctstate NEW MAC 5C:0A:5B:93:3B:CB
10       0     0 LOG        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:23 ctstate NEW LOG flags 0 level 4 prefix "DROP Telnet IN"
11       0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:23 ctstate NEW
12       2   144 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type BROADCAST
13       0     0 LOG        all  --  !lo    *       0.0.0.0/0            0.0.0.0/0            LOG flags 6 level 4 prefix "DROP IN "
14       0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp flags:0x17/0x02 limit: avg 1/sec burst 5
15       0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8 limit: avg 1/sec burst 5
16       0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp flags:!0x17/0x02 state NEW
17       0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp flags:0x3F/0x3F
18       0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp flags:0x3F/0x00
19       0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 state NEW recent: SET name: SSH side: source mask: 255.255.255.255
20       0     0 LOG        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 recent: UPDATE seconds: 60 hit_count: 4 TTL-Match name: SSH side: source mask: 255.255.255.255 LOG flags 0 level 4 prefix "SSH_brute_force "
21       0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 recent: UPDATE seconds: 60 hit_count: 4 TTL-Match name: SSH side: source mask: 255.255.255.255
<code>

Chain FORWARD (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination

 
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpts:67:68 ctstate NEW
2 0 0 ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0
3 77 27355 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
4 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 ctstate NEW
5 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:53 ctstate NEW
6 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53 ctstate NEW
7 2 120 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 ctstate NEW
8 2 120 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 ctstate NEW
9 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:21 ctstate NEW
10 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ctstate NEW
11 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:43 ctstate NEW
12 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:123 ctstate NEW
13 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:5222 ctstate NEW
14 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:5222 ctstate NEW
15 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:6667 ctstate NEW
16 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:6697 ctstate NEW
17 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:465 ctstate NEW
18 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:993 ctstate NEW
19 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:1194 ctstate NEW
20 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:9418 ctstate NEW
21 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:9418 ctstate NEW
22 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:137 ctstate NEW
23 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:138 ctstate NEW
24 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:139 ctstate NEW
25 1 60 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:445 ctstate NEW
26 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:6969 ctstate NEW
27 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpts:6881:6889 ctstate NEW

Log betrachten

Die Logs kann man normal mit journalctl verfolgen:

$ sudo journalctl -f

Download

Die Firewall gibt es auch auf GitHub in meinem Scripts Repository. Die Firewall wurde noch etwas erweitert. Link: firewall.

Links und weitere Informationen

4 thoughts on “Eine einfache stateful Firewall mit iptables”

  1. Ein schöner Artikel, Respekt.
    Im ersten Abschnitt beschreiben Sie eine Hilsfunktion um Regeln für IPv4 und IPv6 zu erstellen.
    Im späteren Script benutzen Sie diese Hilfsfunktion aber nicht. Ist das Script älter und noch nicht geändert oder gibt es einen anderen Grund.

    Reply
  2. Hallo Emanuel,
    Danke für die schnelle Antwort, eine kleine Frage habe ich noch.
    Wo lege ich das Firewall Script am besten ab?

    Gruß
    Dirk

    Reply

Leave a Reply to Dirk Pyplowski Cancel reply