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…“)
 
Zeile 9: Zeile 9:
  
 
=Die erste sinnvolle Konfiguration=
 
=Die erste sinnvolle Konfiguration=
*vi /usr/local/sbin/firewall
+
*vi /usr/local/sbin/firewall  
<pre>
+
#!/bin/bash
#!/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>
  
<span style="color:#004334">REMOTE_TCP_PORTS="22,25,53,80,465,443"</span>
+
case $1 in
<span style="color:#004334">REMOTE_UDP_PORTS="53"</span>
+
  start)
<span style="color:#8a2be2">LOCAL_TCP_PORTS="22,80,443"</span>
+
    echo "starte firewall"
 
+
    iptables -F
case $1 in
+
    iptables -F -t nat
  start)
+
    echo "starte firewall"
+
    iptables -P INPUT DROP
    iptables -F
+
    iptables -P OUTPUT DROP
    iptables -F -t nat
+
    iptables -P FORWARD DROP
 
+
    iptables -P INPUT DROP
+
    <span style="color:#1100FF"># Connection Tracking
    iptables -P OUTPUT DROP
+
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    iptables -P FORWARD DROP
+
    iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT</span>
 
+
    <span style="color:#1100FF"># Connection Tracking
+
    <span style="color:#202FF0"># Loopback-Verkehr
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
+
    iptables -A INPUT -i lo -j ACCEPT
    iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT</span>
+
    iptables -A OUTPUT -o lo -j ACCEPT</span>
 
+
    <span style="color:#202FF0"># Loopback-Verkehr
+
    <span style="color:#004334"># Nach außen erlauben
    iptables -A INPUT -i lo -j ACCEPT
+
    iptables -A OUTPUT -p tcp -m multiport --dports $REMOTE_TCP_PORTS -m state --state NEW -j ACCEPT
    iptables -A OUTPUT -o lo -j ACCEPT</span>
+
    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:#004334"># Nach außen erlauben
+
    iptables -A OUTPUT -p tcp -m multiport --dports $REMOTE_TCP_PORTS -m state --state NEW -j ACCEPT
+
    <span style="color:#8a2be2"># Zugriffe auf den Rechner erlauben
    iptables -A OUTPUT -p udp -m multiport --dports $REMOTE_UDP_PORTS -m state --state NEW -j ACCEPT
+
    iptables -A INPUT -p tcp -m multiport --dports $LOCAL_TCP_PORTS -m state --state NEW -j ACCEPT</span>
    iptables -A OUTPUT -p icmp --icmp-type echo-request -m state --state NEW -j ACCEPT</span>
+
    ;;
 
+
  stop)
    <span style="color:#8a2be2"># Zugriffe auf den Rechner erlauben
+
    echo "stoppe firewall"
    iptables -A INPUT -p tcp -m multiport --dports $LOCAL_TCP_PORTS -m state --state NEW -j ACCEPT</span>
+
    iptables -F
    ;;
+
    iptables -F -t nat
  stop)
+
    iptables -P INPUT ACCEPT
    echo "stoppe firewall"
+
    iptables -P OUTPUT ACCEPT
    iptables -F
+
    iptables -P FORWARD ACCEPT
    iptables -F -t nat
+
    ;;
    iptables -P INPUT ACCEPT
+
  *)
    iptables -P OUTPUT ACCEPT
+
    echo "usage: $0 start|stop"
    iptables -P FORWARD ACCEPT
+
    ;;
    ;;
+
esac
  *)
 
    echo "usage: $0 start|stop"
 
    ;;
 
esac
 
</pre>
 
  
 
=Firewall starten und aktivieren=
 
=Firewall starten und aktivieren=
 
*firewall start
 
*firewall start

Version vom 12. April 2025, 20:11 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