Iptables Host absichern: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
| (2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
| Zeile 15: | Zeile 15: | ||
<span style="color:#004334">REMOTE_UDP_PORTS="53"</span> | <span style="color:#004334">REMOTE_UDP_PORTS="53"</span> | ||
<span style="color:#8a2be2">LOCAL_TCP_PORTS="22,80,443"</span> | <span style="color:#8a2be2">LOCAL_TCP_PORTS="22,80,443"</span> | ||
| − | + | ||
case $1 in | case $1 in | ||
start) | start) | ||
| Zeile 59: | Zeile 59: | ||
=Erweiterte Konfiguration mit Logging= | =Erweiterte Konfiguration mit Logging= | ||
*vi /usr/local/sbin/firewall | *vi /usr/local/sbin/firewall | ||
| − | |||
#!/bin/bash | #!/bin/bash | ||
| − | + | ||
<span style="color:#004334">REMOTE_TCP_PORTS="22,25,53,80,465,443"</span> | <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:#004334">REMOTE_UDP_PORTS="53"</span> | ||
<span style="color:#8a2be2">LOCAL_TCP_PORTS="22,80,443"</span> | <span style="color:#8a2be2">LOCAL_TCP_PORTS="22,80,443"</span> | ||
| − | + | ||
case $1 in | case $1 in | ||
start) | start) | ||
| Zeile 71: | Zeile 70: | ||
iptables -F | iptables -F | ||
iptables -F -t nat | iptables -F -t nat | ||
| − | + | ||
iptables -P INPUT DROP | iptables -P INPUT DROP | ||
iptables -P OUTPUT DROP | iptables -P OUTPUT DROP | ||
iptables -P FORWARD DROP | iptables -P FORWARD DROP | ||
| − | + | ||
# Connection Tracking | # Connection Tracking | ||
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT | iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT | ||
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT | iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT | ||
| − | + | ||
<span style="color:#202FF0"># Loopback | <span style="color:#202FF0"># Loopback | ||
iptables -A INPUT -i lo -j ACCEPT | iptables -A INPUT -i lo -j ACCEPT | ||
iptables -A OUTPUT -o lo -j ACCEPT</span> | iptables -A OUTPUT -o lo -j ACCEPT</span> | ||
| − | + | ||
<span style="color:#004334"># Output-Regeln | <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 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 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> | iptables -A OUTPUT -p icmp --icmp-type echo-request -m state --state NEW -j ACCEPT</span> | ||
| − | + | ||
<span style="color:#8a2be2"># Input-Regeln | <span style="color:#8a2be2"># Input-Regeln | ||
iptables -A INPUT -p tcp -m multiport --dports $LOCAL_TCP_PORTS -m state --state NEW -j ACCEPT</span> | iptables -A INPUT -p tcp -m multiport --dports $LOCAL_TCP_PORTS -m state --state NEW -j ACCEPT</span> | ||
| − | + | ||
<span style="color:#FF0000"># Logging | <span style="color:#FF0000"># Logging | ||
iptables -A INPUT -j LOG --log-prefix "--iptables-drop-input--" | iptables -A INPUT -j LOG --log-prefix "--iptables-drop-input--" | ||
| Zeile 108: | Zeile 107: | ||
;; | ;; | ||
esac | esac | ||
| + | =Wir schauen uns die Logdatei an= | ||
| + | *tail -f /var/log/syslog | ||
| + | <pre> | ||
| + | 2022-11-09T19:07:57.409090+01:00 hostname kernel: --iptables-drop-output--IN= OUT=ens18 SRC=10.0.10.115 DST=8.8.8.8 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=43885 DF PROTO=TCP SPT=55566 DPT=87 WINDOW=64240 RES=0x00 SYN URGP=0 | ||
| + | </pre> | ||
Aktuelle Version vom 12. April 2025, 20:14 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
Wir schauen uns die Logdatei an
- tail -f /var/log/syslog
2022-11-09T19:07:57.409090+01:00 hostname kernel: --iptables-drop-output--IN= OUT=ens18 SRC=10.0.10.115 DST=8.8.8.8 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=43885 DF PROTO=TCP SPT=55566 DPT=87 WINDOW=64240 RES=0x00 SYN URGP=0