Kein Port Forwarding mit iptables

Einrichten des lokalen Netzes, Verbindung zu anderen Computern und Diensten.
Antworten
wonder75
Beiträge: 18
Registriert: 04.11.2006 06:03:18

Kein Port Forwarding mit iptables

Beitrag von wonder75 » 04.11.2006 20:27:20

Hallo,
ich bekomme einfach kein Port-Forwarding hin.
Alle Clients im LAN haben Internet ohne Probleme,
die Input-Rules funktionieren, man kann nur auf SSH zugreifen aber
Anfragen von aussen auf 192.168.0.22:80 kommen einfach nicht an....

etho ist die externe karte, bekommt direkt die ip vom provider,
an eth1 hängt das lan (192.168.0.0/24)

Code: Alles auswählen

#!/bin/sh

#
# Dieses Script
# macht
# NAT
# PORTMAPPING
# BLOCKT ALLES AUSSER den INPUT-PORTS

ifconfig eth0 down
ifconfig eth1 down

ifconfig eth0 up
dhclient eth0
ifconfig eth1 192.168.0.2 up


echo 0 > /proc/sys/net/ipv4/ip_forward

# Module laden
modprobe ip_nat_ftp
modprobe ip_conntrack_ftp

# Flush alte Tabellen
iptables -t nat -F POSTROUTING
iptables -t nat -F PREROUTING
iptables -t nat -F OUTPUT
iptables -F


#Input rules
INPUT(){
 iptables -A INPUT -j ACCEPT -p tcp --dport $1 -s ! 192.168.0.0/24
}
INPUT 22

# Input von Lan erlauben
iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT


# NAT aktivieren
/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward

#PORTMAPPING
MAP(){
/sbin/iptables -t nat -A PREROUTING -p $3 -i eth0 --dport $1 -j DNAT --to-destination $2
}

MAP 80 192.168.0.22:80 tcp


iptables -P INPUT DROP
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

iptables -A INPUT -i lo -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT



Danke für Eure Hilfe

wonder75
Beiträge: 18
Registriert: 04.11.2006 06:03:18

Gelöst

Beitrag von wonder75 » 05.11.2006 23:40:23

Ok, ich habs herausgefunden.
Das Hinzufügen der folgenden Zeile in der MAP-Funktion hat das Problem gelöst:

Code: Alles auswählen

iptables -t nat -A POSTROUTING -d $3 -j SNAT --to $lan_nic_ip
Die Pakete haben wohl den Weg aus dem Netzwerk nicht mehr zurück zum Quellhost gefunden. Weiss nicht warum das nötig ist, habe ca. 20 Howtos / Tutorials gelesen, die das alle nicht machen, aber bei mir geht es nur so.

Anbei noch mal ne funktionierende Version des Firewallscripts, viellicht brauchts mal jemand...

Code: Alles auswählen

#!/bin/sh
echo "Starting firewall"

# Lokale Config
wan_nic=eth0
lan_nic=eth1
lan_nic_ip=192.168.0.2
lan_network=192.168.0.0/24

# Alte Rules löschen
iptables -t nat -F POSTROUTING
iptables -t nat -F PREROUTING
iptables -t nat -F OUTPUT
iptables -F

# NAT für die Clients im LAN
/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward


# INPUT
INPUT(){
iptables -A INPUT -j ACCEPT -p tcp --dport $1 -s ! $lan_network
echo "Allowing connections to this machine from the internet to port $1"
}
 
# Hier alle Dienste hinzufügen, die auf dem Server von aussen erreichbar sein sollen
INPUT 22
  
# FORWARDING
MAP(){
iptables -A PREROUTING -t nat -i $wan_nic -p $1 --dport $2 -j DNAT --to $3:$4
iptables -t nat -A POSTROUTING -d $3 -j SNAT --to $lan_nic_ip
echo "Mapped a port. localhost:$2 ($1) -> $3:$4"
}


# Hier alle Portforwards hinzufügen
# MAP [protokoll] [port auf server] [zieladresse] [zielport]
#MAP tcp 80 192.168.0.22 80
   
   
# Input von Lan erlauben
iptables -A INPUT -s $lan_network -j ACCEPT

# PING erlauben
iptables -A INPUT -p icmp -j ACCEPT
echo "This machine will answer icmp requests (ping)"
   
# DEFAULT CHAINS
iptables -P INPUT DROP
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
   
# Alles von localhost ok
iptables -A INPUT -i lo -j ACCEPT
   
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
   
echo "Firewall active"
   
Gruss
wonder

Antworten