PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : iptables script - Fehler?



einherjer
07.06.02, 13:11
Ich hab mir mal für meinen Fall (Rechner mit fixer IP, Zugang über SSH, kein FTP Server -> SCP, IMAP Server, DNS, Webmin auf anderem Port) ein Script zusammengebastelt. Ist aus verschiedenen anderen zusammengeklaut weil ich nicht DIE Ahnung von Firewalls hab :rolleyes:
Wäre nett wenn ihr Euch das mal ansehen könntet ob ich da noch schwerwiegende Fehler drin hab...


>>>>-------
# !/bin/sh
#
# ----- IP TABLES / FIREWALL ----
#

iptables=/usr/sbin/iptables

# Benoetigte Module hinzufuegen
/sbin/modprobe ip_conntrack

# Zuruecksetzen aller Regeln
$iptables -F
$iptables -P INPUT DROP
$iptables -P FORWARD DROP
$iptables -P OUTPUT DROP

# Alle bereits bestehenden Verbindungen erlauben
$iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$iptables -I OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Erlauben von PING
$iptables -A INPUT -m limit -p ICMP --limit 1 --limit-burst 2 -j ACCEPT
$iptables -A OUTPUT -m limit -p ICMP --limit 1 --limit-burst 2 -j ACCEPT

# Erlauben von Loopback-Verbindungen
$iptables -A INPUT -i lo -j ACCEPT
$iptables -A OUTPUT -o lo -j ACCEPT

#Erlauben von FTP (Client)
$iptables -A OUTPUT -p tcp --dport 21 -j ACCEPT

#Erlauben von SSH (Server und Client)
$iptables -A INPUT -p tcp --dport 22 -j ACCEPT
$iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT

# Erlauben von DNS
$iptables -A OUTPUT -p udp --sport 1024: --dport 53 -j ACCEPT
$iptables -A OUTPUT -p tcp --sport 1024: --dport 53 -j ACCEPT
$iptables -A INPUT -p udp -s 195.20.224.97 --sport 53 -j ACCEPT
$iptables -A INPUT -p tcp -s 195.20.224.97 --sport 53 -j ACCEPT
$iptables -A INPUT -p udp --sport 53 --dport 7531 -j ACCEPT
$iptables -A INPUT -p tcp --sport 53 --dport 7531 -j ACCEPT

#Erlauben von HTTP (Server und Client)
$iptables -A INPUT -p tcp --dport 80 -j ACCEPT
$iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT

#Erlauben von ident
$iptables -A INPUT -p tcp --dport 113 -j ACCEPT -m limit --limit 2 --limit-burst 4
$iptables -A OUTPUT -p tcp --dport 113 -j ACCEPT -m limit --limit 2 --limit-burst 4

#Erlauben von IMAP (Server und Client)
$iptables -A INPUT -p tcp --dport 143 -j ACCEPT
$iptables -A OUTPUT -p tcp --dport 143 -j ACCEPT

#Erlauben von HTTPS (Server und Client)
$iptables -A INPUT -p tcp --dport 443 -j ACCEPT
$iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT

#Erlauben von Webmin (auf port 7575)
$iptables -A INPUT -p tcp --dport 7575 -j ACCEPT
$iptables -A OUTPUT -p tcp --dport 7575 -j ACCEPT


Thx,

boxa
07.06.02, 13:25
für DNS reicht udp völlig aus. TCP brauchts du da nicht. Protocoll udp auf Port 53 muss geöffnet werden. das wars.

einherjer
07.06.02, 13:42
also für DNS nur:

$iptables -A INPUT -p udp --dport 53 -j ACCEPT
$iptables -A OUTPUT -p udp --dport 53 -j ACCEPT

?

würde logisch klingen ;-)

boxa
07.06.02, 13:49
Du verwendest für INPUT und OUTPUT immer dport. dport steht für Destination Port. sport für source Port. D.h Du musst dir überlegen was rein und raus geht. So wie es bei dir jetzt aussieht dürfte nicht viel funktionieren.

Beispiel Webmin auf Server Zugriff von Clients

iptables -A INPUT -i eth0 -p tcp --dport 10000 -j ACCEPT
iptables -A OUTPUT -i eth0 -p tcp --sport 10000 -j ACCEPT

Also Verkehr von Port 10000 (sport) nach außen erlauben. Verkehr nach innen auf Port 10000 erlauben (dport)

Ich glaube Du musst dein script nochmal überarbeiten

boxa
07.06.02, 13:54
Der FTP Bereich ist völlig Falsch. FTP benutzt zwei Ports und arbeitet auf TCP ebene. Port 21 und Port 20 muss für aktives FTP offen sein.

Passives FTP benötigt noch weitere Ports aus dem Bereich 1024:65535

Ich hab das so gemacht:

#FTP
iptables -A INPUT -p tcp --dport 21 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 21 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
#aktives FTP
iptables -A INPUT -p tcp --dport 20 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 20 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
#passives FTP
iptables -A INPUT -p tcp --dport $UP_PORTS --sport $UP_PORTS -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --dport $UP_PORTS --sport $UP_PORTS -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

ACHTUNG: Das Script läuft so auf einem Server. Ich glaube Du brauchst es für einen Client?

Weiß aber auch nicht 1000%ig ob das alles so stimmt. Funktioniert aber bestens!

einherjer
07.06.02, 14:03
ok, mit dport und sport hast Du natürlich recht! Muß mich wohl wirklich nochmal dransetzen wenn ich *wirklich* wach bin und den 3. Kaffee hab ;-)

FTP brauch ich nur als Client, d.h. es läuft kein FTP Server auf der Dose. Datentransfer mach ich eigentlich nur über SCP. Nur zu FTP Servern soll ich mich natürlich schon connecten können, vor allem wegen updates etc.

Thomas
08.06.02, 13:35
@ boxa:
Nach deinen Angaben kann ja jeder auf jeden UpPort deines Systems connecten!
Das kann wohl kaum im Sinne des Erfinders sein...

Im Übrigen ist UDP ebenfalls für ftp-data eingetragen, wie du in /etc/services nachlesen kannst.


@ einherjer:
Schau in diesem Post (http://linuxforen.de/forums/showthread.php?s=&threadid=33784) mal nach, das bringt dich sicher weiter! (Zumindest in Hinsicht auf FTP... ;- )

Thomas.

boxa
08.06.02, 14:32
@tthomas

das soll ja auch so sein. Es ist ein öffentlicher FTP Server!

einherjer
08.06.02, 17:49
Soda, nach rumprobieren und nachlesen (in genau dieser Reihenfolge) sieht es jetzt aus wie unten. Ist zwar sicher noch nicht ideal, aber es scheinen zumindest alle Dienste zu laufen und ein Portscan sieht ok aus.

#! /bin/sh
#
# ----- IP TABLES / FIREWALL ----
#
echo "Starting Firewall!"
#
#
iptables=/usr/sbin/iptables
#
# Benoetigte Module hinzufuegen
/sbin/modprobe ip_conntrack
#
# Zuruecksetzen aller Regeln
$iptables -F
$iptables -X
$iptables -Z
$iptables -P INPUT DROP
$iptables -P FORWARD DROP
$iptables -P OUTPUT DROP
#
# Alle bereits bestehenden Verbindungen erlauben
$iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$iptables -I OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#
# Erlauben von PING
$iptables -A INPUT -m limit -p ICMP --limit 1 --limit-burst 2 -j ACCEPT
$iptables -A OUTPUT -m limit -p ICMP --limit 1 --limit-burst 2 -j ACCEPT
#
# Erlauben von Loopback-Verbindungen
$iptables -A INPUT -i lo -j ACCEPT
$iptables -A OUTPUT -o lo -j ACCEPT
#
#Erlauben von FTP (Client)
$iptables -A OUTPUT -p tcp --dport 21 -j ACCEPT
$iptables -A OUTPUT -p tcp --sport 1024: --dport 1024: -j ACCEPT
#
#Erlauben von SSH (Server und Client)
$iptables -A INPUT -p tcp --dport 22 -j ACCEPT
$iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
#
#Erlauben von SMTP (Server und Client)
$iptables -A INPUT -p tcp --dport 25 -j ACCEPT
$iptables -A OUTPUT -p tcp --sport 25 -j ACCEPT
#
# Erlauben von DNS
$iptables -A INPUT -p udp --sport 53 --dport 7531 -j ACCEPT
$iptables -A OUTPUT -p udp --sport 1024: --dport 53 -j ACCEPT
#
#Erlauben von HTTP (Server und Client)
$iptables -A INPUT -p tcp --dport 80 -j ACCEPT
$iptables -A OUTPUT -p tcp --sport 1024: --dport 80 -j ACCEPT
#
#Erlauben von ident
$iptables -A INPUT -p tcp --dport 113 -j ACCEPT -m limit --limit 2 --limit-burst 4
$iptables -A OUTPUT -p tcp --sport 113 -j ACCEPT -m limit --limit 2 --limit-burst 4
#
#Erlauben von IMAP (Server und Client)
$iptables -A INPUT -p tcp --dport 143 -j ACCEPT
$iptables -A OUTPUT -p tcp --sport 143 -j ACCEPT
#
#Erlauben von HTTPS (Server und Client)
$iptables -A INPUT -p tcp --dport 443 -j ACCEPT
$iptables -A OUTPUT -p tcp --sport 443 -j ACCEPT
#
#Erlauben von Webmin (auf port 10000)
$iptables -A INPUT -p tcp --dport 10000 -j ACCEPT
$iptables -A OUTPUT -p tcp --sport 10000 -j ACCEPT
#
# Schluss und Logging
$iptables -A INPUT -j LOG
$iptables -A OUTPUT -j LOG
$iptables -A INPUT -p tcp --dport auth -j REJECT --reject-with tcp-reset
$iptables -A INPUT -j DROP
$iptables -A OUTPUT -p tcp -j REJECT --reject-with tcp-reset
$iptables -A INPUT -p udp -j REJECT
$iptables -A INPUT -j DROP
#
$iptables -L

bernie
09.06.02, 10:50
Original geschrieben von boxa
für DNS reicht udp völlig aus. TCP brauchts du da nicht. Protocoll udp auf Port 53 muss geöffnet werden. das wars.

Hi,

das stimmt nicht ganz, alle DNS-Queries über 512Byte verwenden TCP. Das sollte im normalen Betrieb nicht auftreten. Allerdings wird es gebraucht wenn Zone-Transfers gemacht werden. Aber das kommt wiederum drauf an ob du die grundsätlich erlaubst oder nicht.

Ciao, bernie