Iptables Host absichern
Version vom 12. April 2025, 20:13 Uhr von Thomas.will (Diskussion | Beiträge) (→Erweiterte Konfiguration mit Logging)
Die ersten wirklichen Regeln die etwas bewirken
- Momentan wollen wir nur den Host absichern.
- Darum können wir die FORWARD-Kette erstmal außen vor lassen.
- Wir beziehen uns also nur auf den Host selbst.
- Wir wollen nun folgendes tun:
- Der Rechner soll mit sich selbst über das Loopback Interface kommunizieren können.
- Vom Rechner selbst nach außen soll zugelassen werden tcp 22,25,53,80,465,443, udp 53 und icmp
- Auf den Rechner soll per "ssh, http und https" zugegriffen werden können.
Die erste sinnvolle Konfiguration
- vi /usr/local/sbin/firewall
#!/bin/bash REMOTE_TCP_PORTS="22,25,53,80,465,443" REMOTE_UDP_PORTS="53" LOCAL_TCP_PORTS="22,80,443"
case $1 in
start)
echo "starte firewall"
iptables -F
iptables -F -t nat
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
# Connection Tracking
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Loopback-Verkehr
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# Nach außen erlauben
iptables -A OUTPUT -p tcp -m multiport --dports $REMOTE_TCP_PORTS -m state --state NEW -j ACCEPT
iptables -A OUTPUT -p udp -m multiport --dports $REMOTE_UDP_PORTS -m state --state NEW -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-request -m state --state NEW -j ACCEPT
# Zugriffe auf den Rechner erlauben
iptables -A INPUT -p tcp -m multiport --dports $LOCAL_TCP_PORTS -m state --state NEW -j ACCEPT
;;
stop)
echo "stoppe firewall"
iptables -F
iptables -F -t nat
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
;;
*)
echo "usage: $0 start|stop"
;;
esac
Firewall starten und aktivieren
- firewall start
Erweiterte Konfiguration mit Logging
- vi /usr/local/sbin/firewall
#!/bin/bash
REMOTE_TCP_PORTS="22,25,53,80,465,443"
REMOTE_UDP_PORTS="53"
LOCAL_TCP_PORTS="22,80,443"
case $1 in
start)
echo "starte firewall"
iptables -F
iptables -F -t nat
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
# Connection Tracking
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Loopback
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# Output-Regeln
iptables -A OUTPUT -p tcp -m multiport --dports $REMOTE_TCP_PORTS -m state --state NEW -j ACCEPT
iptables -A OUTPUT -p udp -m multiport --dports $REMOTE_UDP_PORTS -m state --state NEW -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-request -m state --state NEW -j ACCEPT
# Input-Regeln
iptables -A INPUT -p tcp -m multiport --dports $LOCAL_TCP_PORTS -m state --state NEW -j ACCEPT
# Logging
iptables -A INPUT -j LOG --log-prefix "--iptables-drop-input--"
iptables -A OUTPUT -j LOG --log-prefix "--iptables-drop-output--"
;;
stop)
echo "stoppe firewall"
iptables -F
iptables -F -t nat
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
;;
*)
echo "usage: $0 start|stop"
;;
esac