Iptables Host absichern: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „=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 l…“)
 
 
(4 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 9: Zeile 9:
  
 
=Die erste sinnvolle Konfiguration=
 
=Die erste sinnvolle Konfiguration=
 +
*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
 +
 +
    <span style="color:#1100FF"># Connection Tracking
 +
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 +
    iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT</span>
 +
 +
    <span style="color:#202FF0"># Loopback-Verkehr
 +
    iptables -A INPUT -i lo -j ACCEPT
 +
    iptables -A OUTPUT -o lo -j ACCEPT</span>
 +
 +
    <span style="color:#004334"># 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</span>
 +
 +
    <span style="color:#8a2be2"># Zugriffe auf den Rechner erlauben
 +
    iptables -A INPUT -p tcp -m multiport --dports $LOCAL_TCP_PORTS -m state --state NEW -j ACCEPT</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
 +
 +
=Firewall starten und aktivieren=
 +
*firewall start
 +
=Erweiterte Konfiguration mit Logging=
 
*vi /usr/local/sbin/firewall
 
*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
 +
=Wir schauen uns die Logdatei an=
 +
*tail -f /var/log/syslog
 
<pre>
 
<pre>
#!/bin/bash
+
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
 
 
<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
 
 
 
    <span style="color:#1100FF"># Connection Tracking
 
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 
    iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT</span>
 
 
 
    <span style="color:#202FF0"># Loopback-Verkehr
 
    iptables -A INPUT -i lo -j ACCEPT
 
    iptables -A OUTPUT -o lo -j ACCEPT</span>
 
 
 
    <span style="color:#004334"># 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</span>
 
 
 
    <span style="color:#8a2be2"># Zugriffe auf den Rechner erlauben
 
    iptables -A INPUT -p tcp -m multiport --dports $LOCAL_TCP_PORTS -m state --state NEW -j ACCEPT</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
 
 
</pre>
 
</pre>
 
=Firewall starten und aktivieren=
 
*firewall start
 

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