Einführung

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.

$ $ I I P P 6 6 T T A A B B L L E E S S - - A A I I N N P P U U T T - - p p i i c c m m p p v v 6 6 - - i i c c m m p p v v 6 6 - - t t y y p p e e n n e e i i g g h h b b o o u u r r - - a s d o v l e i r c t i i t s a e t m i e o n n t - - j j A A C C C C E E P P T T - - m m c c o o m m m m e e n n t t - - c c o o m m m m e e n n t t " " I I C C M M P P v v 6 6 _ _ n n e e i i g g h h b b o o u u r r - - s a o d l v i e c r i t t i a s t e i m o e n n " t "

Kernelmodule

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

$ s u d o m o d p r o b e n f _ c o n n t r a c k _ f t p

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

$ n f c _ a c t o n / n e t t r c a / c m k o _ d f u t l p e s - l o a d . d / i p t a b l e s . c o n f

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:

#!/usr/bin/env bash
#
# firewall - iptables firewall
#

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"

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.)"
}

input_chain(){
  echo -n "[+] Applying input rules... "

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

  $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"

  $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"

  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"

  # 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

  # 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

  echo  "Done."
}


output_chain(){
  echo -n "[+] Applying output rules... "

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

  $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"

  $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 tcp --dport 995 -m conntrack --ctstate NEW -j ACCEPT -m comment --comment "POP3S_Thunderbird"
  $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"

  echo  "Done."
}

hardening(){
  echo -n "[+] Hardening..."

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

  # (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

  # 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"

  $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"

  $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"

  $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"

  $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 "

  $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"

  # $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 "

  # DOS Attacks (Denial of service)

  echo  "Done."
}

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

Die aktuellste Version davon gibt es auf GitHub in meinem Scripts Repository: firewall.

Firewall kontrollieren

So startet man die Firewall:

$ [ [ [ [ + + + + s ] ] ] ] u d S A A F o t p p i a p p r r l l e / t y y w f i i i a i n n n l r g g g l e w f o i s a i u n t l r t p a l e p u r w u t t s a t e t l r d a l r u r . u l t . l e . e s s . . . . . . D D o o n n e e . .

Und wieder stoppen:

$ [ + s ] u d S o t o f p i p r i e n w g a l f l i r s e t w o a p l l . . . D o n e .

Firewall als Service aktivieren

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

$ $ s s u u d d o o s s y y s s t t e e m m c c t t l l e s n t a a b r l t e i i p p t t a a b b l l e e s s

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:

$ [ [ + + s ] ] u d S S o a a v v i i / n n f g g i r i i e p p w t 6 a a t l b a l l b e l s s e a s v r e u r l u e l s e . s . . . . . D o D n o e n . e . ( 5 ( 5 1 4 L i L n i e n s e s s a s v a e v d e . d ) . )

Regeln anschauen

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

$ [ i C n 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2 2 C n C n 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 + p h u 0 1 2 3 4 5 6 7 8 9 0 1 h u h u 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 s ] t a m a m a m 0 0 7 0 0 0 2 2 0 u a i i i 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 d i b n n p n p 0 0 0 0 0 1 1 0 o p l k k 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 t e I p F t O t A A 7 A A A 0 0 A 0 a s N k O s U s C C 3 C C C C A A A A A A A A A A A A A A A A A / b P t 6 R T C C 5 C C C A A C C C C C C C C C C C C C C C C A C C f l v U s 0 3 3 9 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 W b P b E E 5 E E E C C E C C C C C C C C C C C C C C C C C C i e 1 T A y U y P P P P P C C P E E E E E E E E E E E E E E E C E E r s . b R t T t T T A T T T E E T P P P P P P P P P P P P P P P E P P e : 4 ( y 9 D e e C P P T T T T T T T T T T T T T T T P T T w . p t 1 1 5 1 s ( s u a C i u t T T t T a 2 o e 2 2 1 4 ( p d l E c d c c t t u t u t t t t u t u u u t t t l 0 l s 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 p t o t p l P m p p t t p c c d c d c c c c d c d d d c t c c l i o a l a T p c c p p p p p p p p p p p p p p p c p p c t A L D A A L A L A L D D L A A D D D A L D l r i r p p p s y a C O R C C O C O C O R R O C C R R R C O R i g c g a t r C G O C C G C G C G O O G C C O O O C G O c e y e l a D g E P E E E E P P E E P P P E P y t t l t R e P P P P P P P P D u O t T T T T T T T T D p R p l s P R r O r o O o P o 0 0 0 0 0 P t t . 0 0 . . . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 . . 0 0 0 0 0 . . . . . . . . . . . . . . . 0 . . p 0 o o . 0 0 . . . . . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 . 0 0 a p a a a a i t t t t t t a a t i t t t t t t p p p 0 . . 0 0 0 0 0 . . . . . . . . . . . . . . . 0 . . c r l l l l c c c c c c c l l c c c c c c c c p t a t . 0 0 0 . . . . . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 . 0 0 k o l l l l m p p p p p p l l p m p p p p p p a c 0 . . . 0 0 0 0 0 . . . . . . . . . . . . . . . 0 . . e t p p c i k i 0 0 0 / . . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 . 0 0 t k n e n 0 / . 0 0 0 0 0 / / / / / / / / / / / / / / 0 / s o e t 0 0 0 / / 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 , p t o s o 0 . 0 0 0 0 0 0 t s u , u . 0 0 0 . . . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 , t t 0 . / . 0 0 0 0 0 . . . . . . . . . . . . . . . 0 . . i l ! 0 . 0 0 0 . . . . . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 . 0 0 b n o l 0 s s 0 . . 0 0 0 0 0 . . . . . . . . . . . . . . . 0 . . y o o b o . 0 0 0 . . . . . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 . 0 0 t b u y u 0 . . . 0 0 0 0 0 . . . . . . . . . . . . . . . 0 . . e y r t r 0 0 0 / . . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 . 0 0 s t c e c 0 / . 0 0 0 0 0 / / / / / / / / / / / / / / 0 / ) e e s e 0 0 0 / / 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o s ) u . u t 0 0 t 0 u ) d d d 0 i d c c t t u t u t t t t u t u u u t t t t e e p / c p p t t p c c d c d c c c c d c d d d c t c c s s 0 m c c p p p p p p p p p p p p p p p c p p t t d p d d p p d p i i p c t p p p d d d d d d d d d d d d d d d d d n n t t y t t d d t p p p p p p p p p p p p p p p d p p a a s s p : : p p : t t t t t t t t t t t t t t t p t t s 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 t t : t e 5 5 t t 2 : : : : : : : : : : : : : : : t : s o . . . . . . . . . . . . . . . . . . . . . i i 6 a 3 3 : : 1 2 4 1 5 5 6 6 4 9 1 9 9 1 1 1 : 6 : u 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o o 7 t 8 8 4 2 3 2 2 2 6 6 6 9 1 4 4 3 3 3 4 9 6 r . . . . . . . . . . . . . . . . . . . . . n n : e c c 0 4 c 3 2 2 6 9 5 3 9 1 1 7 8 9 4 6 8 c 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 c t t 3 t c c 2 2 7 7 4 8 8 5 9 8 e . . . . . . . . . . . . . . . . . . . . . 8 R t s s c s t t c c c c c c 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 E s t t t c t s s t c c c c t t c c c t t t c c : / / / / / / / / / / / / / / / / / / / / / c L t a a s t a t t s t t t t s s t t t s s s t t 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 t A a t t t s t a a t s s s s t t s s s t t t s s 8 s T t e e a t e t t a t t t t a a t t t a a a t t 8 t E e t a e e t a a a a t t a a a t t t a a 9 a D N N e t N e t t t t e e t t t e e e t t t , N E E e E N N e e e e e e e e e c e E E W W N W E E N N N N N N t S W E N W W E N N N N E E N N N E E E N N s N T W E W E E E E W W E E E W W W E E t E A W W W W W W W W W W a W B t L e I S N d 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H E e . . . . . . . . . . . . . . . . . . . . . E W s 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 D t . . . . . . . . . . . . . . . . . . . . . i 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 n . . . . . . . . . . . . . . . . . . . . . a 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 t / / / / / / / / / / / / / / / / / / / / / i 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o n c c c i t t t t t t A L t i t t t t t t t t t c c c c c c c D O c c c c c c c c s s s m p p p p p p D G p m p p p p p p t t t p R p a a a t d d d d d d T f f t f f f d d d t t t y p p p p p p Y l l y l l l p p p e e e p t t t t t t P a a p a a a t t t e : : : : : : E g g e g g g : : : I I R 2 2 6 6 2 2 s s s s s 2 2 2 N N E 8 2 2 6 6 3 3 m : 8 : : : 2 2 2 V V L 0 0 a 6 0 ! 0 0 A A A s c c 0 0 c c t x l 0 x x s r r L L T t t t t t c l 1 i x 3 3 t e e I I E a s s c c s s h e 7 m 1 F F a c c D D D t t t t t t t v / i 7 / t e e , e a a s s a a d e 0 t 0 0 e n n L E t t t t t t s l x : 0 x x t t O S N e e a a e e t 0 x 3 0 N : : G T E t t - 4 2 a 0 F 0 E A W N N e e N N t v 2 W U U f B E E E E y p l g P P l L W W N N W W p r i s r D D a I E E e e m 1 t e A A g S L W W L f i / a c T T s H O O B i t s t e E E E G M M G R x : e e n 6 D A A O c t s s f C C f A " a N : e e l l l D D v b E c c e a 5 5 a C R g u W S o o v g C C g A O r E n n e s : : s S P 1 s T d d l 0 0 T / t s s 0 A A 0 I s n : : 4 : : N e 5 a l 5 5 l c m 6 6 p e B B e " e 0 0 r v : : v b : e e 9 9 e u h h f l 3 3 l r S i i i : : s S t t x 4 3 3 4 t H _ _ B B c c " p : : p 5 s o o D r C C r i u u R e B B e d n n O f f e t t P i L i : : : x O x I G s 4 4 N " " o V A f D u T T A L l R r T T L L a O c L L I O g P e - - D W s M M T m a a " S 0 e a t t S l s c c H l n k h h e e : I v t n n N e 2 a a l I 5 m m " N 5 e e 4 " . : : 2 p 5 S S r 5 S S e . H H f 2 i 5 s s x 5 i i . d d " 2 e e A 5 : : L 5 L s s O o o W u u r r M c c P e e D m m I a a N s s k k " : : 2 2 5 5 5 5 . . 2 2 5 5 5 5 . . 2 2 5 5 5 5 . . 2 2 5 5 5 5 L O G f l a g s 0 l e v e l 4 p r e f i x " S S H _ b r u t e _ f o r c e "

Log betrachten

Die Logs kann man normal mit journalctl verfolgen:

$ s u d o j o u r n a l c t l - f