linux:iptables

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
Letzte ÜberarbeitungBeide Seiten der Revision
linux:iptables [2012/06/26 16:36] – [Simples Firewallscript] stlinux:iptables [2022/04/12 10:42] – [Iptables-Regeln dauerhaft sichern] st
Zeile 1: Zeile 1:
 +====== Netfilter / Iptables ======
 +Netfilter ist eine [[security:Firewall]] innerhalb des [[wpde>Linux-Kernel|Linux-Kernels]].
  
 +Einstellungen ausgeben:
 +  /sbin/iptables -L -n -v
 +
 +===== Konfigurationswerkzeuge =====
 +[[http://www.fwbuilder.org/|Firewallbuilder]] | [[http://kris.koehntopp.de/artikel/fwbuilder/|Anleitung]]
 +
 +[[software:bastille]]
 +
 +===== Links =====
 +  * [[http://www.netadmintools.com/art216.html|Block IP Addresses With IPtables]]
 +  * [[http://www.hermann-uwe.de/files/fw_laptop|A firewall script intended to be used on workstations / laptops]]
 +  * [[http://www.linuxguruz.com/iptables/|IPTABLES Firewall]]: Regeln und Skripts für Iptables
 +  * [[http://www.netfilter.org/documentation/HOWTO/de/packet-filtering-HOWTO-7.html]]
 +  * [[http://www.linux-magazin.de/Artikel/ausgabe/2000/06/IPTables/iptables.html]]
 +  * [[http://www.fz-juelich.de/zam/docs/printable/tki/tki-0402.pdf]]
 +  * [[http://www.linuxfaqs.de/howto/iptableshowto.php]]
 +  * [[http://cipherdyne.org/blog/2007/07/oscon-2007-talk-slides-iptables-attack-visualization.html|Iptables Attack Visualization]]
 +  * [[http://www.64-bit.de/dokumentationen/netzwerk/e/002/DE-IPTABLES-HOWTO-3.html|Linux IPTABLES HOWTO: iptables-Grundlagen]]
 +
 +===== Tools =====
 +
 +Wenn das Modul ip_conntrack geladen ist (wenn nicht: nachholen mit ''sudo modprobe ip_conntrack'' ) kann man die aktuelle Verbindungstabelle mit [[http://www.phildev.net/iptstate/|iptstate]] ausgeben:
 +  sudo iptstate
 +
 +(benötigt CONFIG_NF_CT_NETLINK in [[linux:Kernel]] Config).
 +
 +
 +===== Portumleitung auf Standardports =====
 +
 +Regeln erstellen, z.B. Port 80 -> 8080 und 443 -> 8443.
 +
 +<code bash>
 +sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-port 8080
 +sudo iptables -A PREROUTING -t nat -p tcp --dport 443 -j REDIRECT --to-port 8443
 +</code>
 +
 +===== Iptables-Regeln dauerhaft sichern =====
 +
 +Damit die aktuell gültigen Regeln einen reboot überleben gibt es unter Debian/Ubuntu das Paket ''iptables-persistent''.
 +
 +<code bash>apt install iptables-persistent</code>
 +
 +Regel permanent speichern:
 +<code bash>
 +iptables-save > /etc/iptables/rules.v4
 +ip6tables-save > /etc/iptables/rules.v6
 +</code>
 +
 +Restore sofort:
 +<code bash>
 +iptables-restore < /etc/iptables/rules.v4
 +ip6tables-restore  < /etc/iptables/rules.v6
 +</code>
 +
 +Der folgende systemd-service sorgt für einen restore beim reboot:
 +<code bash>systemctl status netfilter-persistent</code>
 +
 +[[https://www.thomas-krenn.com/de/wiki/Iptables_Firewall_Regeln_dauerhaft_speichern|Alternative Doku und Anleitung für Redhat-basiertes Systeme]]
 +===== Iptables-Regeln auflisten und löschen =====
 +
 +Auflistung aktuell gültiger Regeln (v4): <code bash>iptables -L -n --line-numbers</code> bzw v6: <code bash>ip6tables -L -n --line-numbers</code>
 +
 +löschen: <code bash>iptables -D $CHAIN 1</code>
 +===== Simples Firewallscript =====
 +
 +Blockieren einer einzigen IP-Adresse (hier im Beispiel "1.2.3.4"):
 +
 +<code bash>iptables -A INPUT -p all -s 1.2.3.4 -j REJECT</code>
 +
 +
 +Als Ausgangsbasis für eine umfangreichere Firewall:
 +
 +<code bash>
 +#!/bin/bash
 +
 +IPTABLES="/sbin/iptables"
 +
 +function firewall_start()
 +{
 +# echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
 +
 +# default policy = Drop
 +# uncomment
 +#$IPTABLES -P INPUT DROP
 +#$IPTABLES -P OUTPUT DROP
 +#$IPTABLES -P FORWARD DROP
 +
 +# example: Drop all packets from host 120.166.10.211
 +$IPTABLES -I INPUT -s 120.166.10.211 -j DROP
 +$IPTABLES -I INPUT -s 120.166.10.211 -m limit --limit 3/minute --limit-burst 3 -j LOG --log-level debug --log-prefix "IPT 120.166.10.211 blocked: "
 +
 +}
 +
 +
 +function firewall_stop()
 +{
 +
 +# forwarding off?
 +# echo "0" > /proc/sys/net/ipv4/ip_forward
 +#
 +# reset the default policies in the filter table.
 +#
 +$IPTABLES -P INPUT ACCEPT
 +$IPTABLES -P FORWARD ACCEPT
 +$IPTABLES -P OUTPUT ACCEPT
 +
 +#
 +# reset the default policies in the nat table.
 +#
 +$IPTABLES -t nat -P PREROUTING ACCEPT
 +$IPTABLES -t nat -P POSTROUTING ACCEPT
 +$IPTABLES -t nat -P OUTPUT ACCEPT
 +
 +#
 +# reset the default policies in the mangle table.
 +#
 +$IPTABLES -t mangle -P PREROUTING ACCEPT
 +$IPTABLES -t mangle -P OUTPUT ACCEPT
 +
 +#
 +# flush all the rules in the filter and nat tables.
 +#
 +$IPTABLES -F
 +$IPTABLES -t nat -F
 +$IPTABLES -t mangle -F
 +#
 +# erase all chains that's not default in filter and nat table.
 +#
 +$IPTABLES -X
 +$IPTABLES -t nat -X
 +$IPTABLES -t mangle -X
 +}
 +
 +
 +case "$1" in
 +start)
 +      echo "starting firewall..."
 +      firewall_start
 +      ;;
 +stop)
 +      echo "stopping firewall..."
 +      firewall_stop
 +      ;;
 +restart)
 +      echo "restarting firewall..."
 +      firewall_stop
 +      firewall_start
 +      ;;
 +*)
 +      echo "Usage: $0 {start|stop|restart}"
 +      exit 1
 +      ;;
 +esac
 +</code>