PDA

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



barton4
21.04.05, 14:08
ich bin gerad dabei fuer meinen linux rechner ein firewallscript zu schreiben,dieser rechner dient auch noch als dsl router fuer mein
anderen rechner.



#!/sbin/runscript

start()
{
$LOCAL_HOST="lo";
$LOCAL_LAN1="eth0";
$WWW_INFACE="ppp0";

iptables -P INPUT DROP;
iptables -P FORWARD DROP;
iptables -P OUTPUT DROP;

#localhost
iptables -A INPUT -i $LOCAL_HOST -j ACCEPT;
iptables -A OUTPUT -o $LOCAL_HOST -j ACCEPT;

#locales LAN
iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT;
iptables -A OUTPUT -d 192.168.0.0/24 -j ACCEPT;

#speziell verboten fuer WWW
iptables -A INPUT -i $WWW_INFACE -p tcp --dport 22 -j DROP #port_ssh
iptables -A INPUT -i $WWW_INFACE -p tcp --dport 23 -j DROP #port_telnet
iptables -A INPUT -i $WWW_INFACE -p tcp --dport 137:139 -j DROP #port_netbios
iptables -A INPUT -i $WWW_INFACE -p udp --dport 137:139 -j DROP #port_netbios
iptables -A INPUT -i $WWW_INFACE -p udp --dport 445 -j DROP #port_microsoft-ds
iptables -A INPUT -i $WWW_INFACE -p udp --dport 4000 -j DROP #port_mldonkey-telnet_port
iptables -A INPUT -i $WWW_INFACE -p udp --dport 4080 -j DROP #port_mldonkey-http_port

iptables -A INPUT -i -i $WWW_INFACE -s 192.168.0.0/24 -j DROP #keine inernen adressen aus dem inet iptables -A INPUT -i $WWW_INFACE -p icmp -m limit -limit 26/sec -j DROP



#speziell erlaubt


#alles darf raus,syn flags darf nicht rein
iptables -A INPUT -i $WWW_INFACE -p tcp ! --tcp-flags syn -j ACCEPT;
iptables -A OUTPUT -o $WWW_INFACE -p tcp --tcp-flags syn,ack,fin,urg,rst -j ACCEPT;


iptables -A INPUT -i $WWW_INFACE -p udp --state ESTABLISHED,RELATED -j ACCEPT;
iptables -A OUTPUT -i $WWW_INFACE -p udp --state NEW,ESTABLISHED,RELATED -j ACCEPT;


#icmp
iptables -A OUTPUT -o $WWW_INFACE -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT; #again PING of DEATH
iptables -A INPUT -i $WWW_INFACE -p icmp --icmp-type echo-reply -j ACCEPT;
iptables -A INPUT -i $WWW_INFACE -p icmp --icmp-type echo-request -j ACCEPT;
iptables -A OUTPUT -o $WWW_INFACE -p icmp --icmp-type echo-reply -j ACCEPT;
iptables -A OUTPUT -o $WWW_INFACE -p icmp --icmp-type destination-unreachable -j ACCEPT
iptables -A INPUT -i $WWW_INFACE -p icmp --icmp-type destination-unreachable -j ACCEPT
iptables -A INPUT -i $WWW_INFACE -p icmp --icmp-type source-quench -j ACCEPT;
iptables -A INPUT -i $WWW_INFACE -p icmp --icmp-type time-exceeded -j ACCEPT;
iptables -A INPUT -i $WWW_INFACE -p icmp --icmp-type parameter-problem -j ACCEPT;

#routing


#nat
iptables -A POSTROUTING -t nat -o $WWW_INFACE -j MASQUERADE; #masquerading









was meint ihr macht das script mein pc sicher oder findet ihr auch fehler bzw
sicherheits luechen.

gruss !

Harry
21.04.05, 14:24
Hi,

Du hast da schon eine recht abenteuerliche Mischung von statischen und dynamischen (stateful inspection)-Regeln. Beim groben überfliegen würde ich mal schätzen, dass knapp die Hälft der Regeln überflüssig sein könnten.

Schau Dir nochmal ein paar gute Tutorials zum Connection-Tracking an und dann kannst Du Deinen Regelsatz mal so richtig ausmisten :D

PS: Bei einer Regel müsste es ohnenin prasseln:
iptables -A OUTPUT -i $WWW_INFACE..
Die Option müsste dort "-o" anstelle von "-i" heissen.

Harry

barton4
21.04.05, 21:37
ich weis scho mit den connect tracking is vieleciht nicht die feinste methode aber beim tcp protkoll wollt ich mal mit den flags ausprobieren,führt doch am ende aufs gleiche raus wie beim state modul.
Bei udp muss ich ja state nehmen weil da gibst ja soweit ich weis keien flags

welche regeln sind da genau ueberflueeesig.?

ich würd sagen die zeilen wären ueberflüssig weil ja eh alle verbindungsversuche
von ausen gekillt werden.

#speziell verboten fuer WWW
iptables -A INPUT -i $WWW_INFACE -p tcp --dport 22 -j DROP #port_ssh
iptables -A INPUT -i $WWW_INFACE -p tcp --dport 23 -j DROP #port_telnet
iptables -A INPUT -i $WWW_INFACE -p tcp --dport 137:139 -j DROP #port_netbios
iptables -A INPUT -i $WWW_INFACE -p udp --dport 137:139 -j DROP #port_netbios
iptables -A INPUT -i $WWW_INFACE -p udp --dport 445 -j DROP #port_microsoft-ds
iptables -A INPUT -i $WWW_INFACE -p udp --dport 4000 -j DROP #port_mldonkey-telnet_port
iptables -A INPUT -i $WWW_INFACE -p udp --dport 4080 -j DROP #port_mldonkey-http_port

aber ich habs nur deshalb gemacht,weil wenn ich im abschnitt speziell erlaubt noch hab in den ich zb apache server freigeben will und vieleicht irgendwann nochmal andere sachen und da wollt ich ein paar ports und protokolle festlegen die dann nie freigegeben werden können,sozusageb gegen unbedacht fehler bei freigeben.

naja aber was sagsten sonst dazu würde die firewall was taugen
und was meinst du zu den icmp freigaben,wär es besser sie ganz zu blocken fürs www?

Harry
21.04.05, 21:49
Hi barton4,

die Regeln zum blockieren des Traffics aus dem WWW zu Dir sind an sich ok. Ich würde für das Target dort nur REJECT anstelle von DROP nehmen. Das ist ein wenig RFC-Konformer und macht Dich auch nach aussen nicht auf den ersten Blick interessant.

Die ICMP-Regeln könntest Du fast alle durch ein vernünftiges Connection-Tracking bei den TCP- und UDP-Regeln erschlagen. Lediglich echo-request und -reply brauchst Du dann noch explizit.

In den TCP-Regeln, in denen Du von der Option "--tcp-flags" Gebrauch machst ... also entweder bin ich jetzt zu müde, um das zu sehen, oder Du setzt da nur den Ausdruck für die Maske der zu betrachtenden Flags und hast den Vergleichsausdruck da einfach weggelassen.
Die Manpage sagt ja dazu (in Kurzform)

--tcp-flags [!] mask comp, wobei "mask" und "comp" jeweils eine komma-separierte Liste von Flags beinhalten kann, aber dazwischen _muss_ ein Leerzeichen stehen.

Du machst ein Masquerading auf dem WWW-Interface, aber hast die FORWARD-Queue komplett auf DROP. Dann kannst Du das Masquerading auch weglassen - da geht ja eh nichts durch.

Also die Ansätze sind gut, aber wie schon gesagt: Du solltest da mal ausmisten ;)

Harry

barton4
22.04.05, 07:13
erstmal danke das du nachgeschaut,ich wär dass mal ausprobieren

cane
22.04.05, 09:23
Ich empfehle Dir mal in Harry's Signatur zu scheuen und den Iptables-Generator auszuprobieren :)

Der erstellt ein Super-Scrict das Du so verwenden oder aber anpassen kannst!


mfg
cane

barton4
25.04.05, 20:06
ne ich bleib dabei und versuch es selber ein firewallscript auf die beine zu stellen hier meien ueberarbeitete version was sagt ihr dazu,was gibts noch
so zu verbessern:

#!/sbin/runscript

start()
{
$LOCAL_HOST="lo";
$LOCAL_LAN1="eth0";
$WWW_INFACE="ppp0";

iptables -P INPUT REJECT;
iptables -P FORWARD REJECT;
iptables -P OUTPUT REJECT;

#localhost
iptables -A INPUT -i $LOCAL_HOST -j ACCEPT;
iptables -A OUTPUT -o $LOCAL_HOST -j ACCEPT;

#locales LAN
iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT;
iptables -A OUTPUT -d 192.168.0.0/24 -j ACCEPT;


#logging $WWW_INFACE
iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j LOG --log-prefix "iptables_http" --log-level notice #http loggen
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j LOG --log-prefix "iptables_ssh" --log-level warn #ssh loggen
iptables -A INPUT -p tcp --dport 23 -m state --state NEW -j LOG --log-prefix "iptables_telnet" --log-level warn #telnet loggen
iptables -A INPUT -p tcp --dport 21 -m state --state NEW -j LOG --log-prefix "iptables_ftp" --log-level warn #ftp loggen


#speziell verboten fuer WWW
iptables -A INPUT -i $WWW_INFACE -p tcp --dport 22 -j REJECT #port_ssh
iptables -A INPUT -i $WWW_INFACE -p tcp --dport 23 -j REJECT #port_telnet
iptables -A INPUT -i $WWW_INFACE -p tcp --dport 137:139 -j REJECT #port_netbios
iptables -A INPUT -i $WWW_INFACE -p udp --dport 137:139 -j REJECT #port_netbios
iptables -A INPUT -i $WWW_INFACE -p udp --dport 445 -j REJECT #port_microsoft-ds
iptables -A INPUT -i $WWW_INFACE -p udp --dport 4000 -j REJECT #port_mldonkey-telnet_port
iptables -A INPUT -i $WWW_INFACE -p udp --dport 4080 -j REJECT #port_mldonkey-http_port

iptables -A INPUT -i $WWW_INFACE -s 192.168.0.0/24 -j REJECT #keine inernen adressen aus dem inet iptables -A INPUT -i $WWW_INFACE -p icmp -m limit -limit 26/sec -j DROP



#speziell erlaubt
#iptables -A INPUT -i $WWW_INFACE -p tcp --dport 80 -j ACCEPT #webserver aber erst spaeter

#alles darf raus,syn flags darf nicht rein
iptables -A INPUT -i $WWW_INFACE -p tcp --state ESTABLISHED,RELATED -j ACCEPT;
iptables -A OUTPUT -o $WWW_INFACE -p tcp --state NEW,ESTABLISHED,RELATED -j ACCEPT;

iptables -A INPUT -i $WWW_INFACE -p udp --state ESTABLISHED,RELATED -j ACCEPT;
iptables -A OUTPUT -o $WWW_INFACE -p udp --state NEW,ESTABLISHED,RELATED -j ACCEPT;


#icmp
iptables -A OUTPUT -o $WWW_INFACE -p icmp --icmp-type echo-request -j ACCEPT; #again PING of DEATH
iptables -A INPUT -i $WWW_INFACE -p icmp --icmp-type echo-reply -m limit --limit 1/s -j ACCEPT;
iptables -A INPUT -i $WWW_INFACE -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT;
iptables -A OUTPUT -o $WWW_INFACE -p icmp --icmp-type echo-reply -j ACCEPT;

#routing
iptables -A FORWARD -i $LOCAL_LAN1 -o $WWW_INFACE -s 192.168.0.0/24 -j ACCEPT #nur adressen mit 192.168... duerfen ins web
iptables -A FORWARD -i $WWW_INFACE -o $LOCAL_LAN1 -d 192.168.0.0/24 -j ACCEPT #nur adressen 192.168... duerfen ins LAN

#nat
iptables -A POSTROUTING -t nat -o $WWW_INFACE -j MASQUERADE; #masquerading