Iptables Host absichern: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
| Zeile 57: | Zeile 57: | ||
=Firewall starten und aktivieren= | =Firewall starten und aktivieren= | ||
*firewall start | *firewall start | ||
| + | =Erweiterte Konfiguration mit Logging= | ||
| + | *vi /usr/local/sbin/firewall | ||
| + | |||
| + | #!/bin/bash | ||
| + | |||
| + | <span style="color:#004334">REMOTE_TCP_PORTS="22,25,53,80,465,443"</span> | ||
| + | <span style="color:#004334">REMOTE_UDP_PORTS="53"</span> | ||
| + | <span style="color:#8a2be2">LOCAL_TCP_PORTS="22,80,443"</span> | ||
| + | |||
| + | 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 | ||
| + | |||
| + | <span style="color:#202FF0"># Loopback | ||
| + | iptables -A INPUT -i lo -j ACCEPT | ||
| + | iptables -A OUTPUT -o lo -j ACCEPT</span> | ||
| + | |||
| + | <span style="color:#004334"># 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</span> | ||
| + | |||
| + | <span style="color:#8a2be2"># Input-Regeln | ||
| + | iptables -A INPUT -p tcp -m multiport --dports $LOCAL_TCP_PORTS -m state --state NEW -j ACCEPT</span> | ||
| + | |||
| + | <span style="color:#FF0000"># Logging | ||
| + | iptables -A INPUT -j LOG --log-prefix "--iptables-drop-input--" | ||
| + | iptables -A OUTPUT -j LOG --log-prefix "--iptables-drop-output--"</span> | ||
| + | ;; | ||
| + | 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 | ||
Version vom 12. April 2025, 20:12 Uhr
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