# iptables-init # für RH72+up: iptables initialisieren, dann /etc/rc.d/init.d/iptables save # $Id$ PATH=/usr/sbin:/sbin:/usr/bin:/bin export PATH # Adresse des Nameservers automatisch feststellen: #nameserver=`grep '^nameserver' /etc/resolv.conf | head -1 | awk '{print $2}'` # Alternative: Manuelle Festlegung vertrauenswürdiger Nameserver nslist="" #nslist="$nslist 145.253.2.11 145.253.2.75" # arcor #nslist="$nslist 195.212.44.225" # callino #nslist="$nslist 195.222.195.222 195.222.195.223" # callpop #nslist="$nslist 212.82.225.7 212.82.225.12" # claranet #nslist="$nslist 193.227.195.130" # drillisch #nslist="$nslist 192.76.144.66" # knuut #nslist="$nslist 212.125.36.1 212.125.37.1" # ln #nslist="$nslist 129.187.10.25 129.187.16.1" # lrz #nslist="$nslist 213.21.51.250 213.21.0.218" # nochmal lrz, auch mds #nslist="$nslist 194.231.255.1 194.231.164.4" # mirado RO #nslist="$nslist 194.77.97.10 194.77.97.12" # mirado SB #nslist="$nslist 62.104.196.134" # mobilcom #nslist="$nslist 193.159.187.130" # ngi #nslist="$nslist 194.25.2.129 212.185.252.201" # ngi AUTO #nslist="$nslist 195.71.233.3 193.189.224.4" # ngi neu AUTO #nslist="$nslist 212.122.128.10 212.122.129.10" # nikoma #nslist="$nslist 195.50.149.33 195.50.140.6" # otelo #nslist="$nslist 195.252.128.53" # talkline #nslist="$nslist 195.182.96.28 195.182.96.126" # viag #nslist="$nslist 195.226.96.131 195.226.96.132" # yello nslist="$nslist 172.16.45.13" # nathan.not-for-mail ##### Kernel-Tuning ########################################################### for i in /proc/sys/net/ipv4/conf/*/{accept_source_route,accept_redirects,send_redirects}; do echo 0 >$i done echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_broadcasts echo 1 >/proc/sys/net/ipv4/tcp_syncookies echo 1 >/proc/sys/net/ipv4/ip_forward ##### POLICIES ################################################################ # Bereits existierende Regeln löschen iptables -F # Voreinstellung: Unbekannte Pakete dürfen nicht passieren iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT DROP ##### INPUT-Chain ############################################################# # NUR ZUM TESTEN: Nameserver-Antworten von überall erlauben # iptables -A INPUT -p udp --sport 53 -j ACCEPT # NUR ZUM TESTEN: Alle Pakete annehmen # iptables -A INPUT -j ACCEPT # NUR ZUM TESTEN: Alle Pakete protokollieren # iptables -A INPUT -j LOG # vom Loopback-Interface ist alles erlaubt iptables -A INPUT -i lo -j ACCEPT # Falls -m state gewünscht ist: erkannte Verbindungen erlauben #iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # X11 sperren iptables -A INPUT -p tcp --dport 6000:6020 --syn -j LOG iptables -A INPUT -p tcp --dport 6000:6020 --syn -j DROP # NFS und SOCKS sperren iptables -A INPUT -p tcp -m multiport --dport 1080,2049 --syn -j LOG iptables -A INPUT -p tcp -m multiport --dport 1080,2049 --syn -j DROP iptables -A INPUT -p udp -m multiport --dport 2049,4045 -j LOG iptables -A INPUT -p udp -m multiport --dport 2049,4045 -j DROP # Bestehende Verbindungen zu oberen Ports sind erlaubt iptables -A INPUT -p tcp --dport 1024: ! --syn -j ACCEPT # Einzelne Server-Ports sind ebenfalls erlaubt iptables -A INPUT -p tcp -s 172.16.45.0/24 --dport ssh -j ACCEPT # ssh vom LAN iptables -A INPUT -p tcp --dport 80 -j ACCEPT # http #iptables -A INPUT -p tcp --dport auth -j ACCEPT # auth/identd iptables -A INPUT -p udp -s 172.16.45.0/24 --dport 514 -j ACCEPT # syslog v.LAN #iptables -A INPUT -p udp --dport 6970 -j ACCEPT # RealPlayer / nautilus #iptables -A INPUT -p tcp --dport 6346 -j ACCEPT # Gnutella # auth-Anfragen werden mit einer Fehlermeldung an den Absender verweigert. # Das beschleunigt den Aufbau zu Servern, die eine ident-Anfrage durchführen. iptables -A INPUT -p tcp --dport auth -j REJECT --reject-with tcp-reset # Drei Varianten für DNS: # (1) DNS-Pakete nur von einem einzigen Nameserver #iptables -A INPUT -p tcp -s $nameserver --sport domain -j ACCEPT #iptables -A INPUT -p udp -s $nameserver --sport domain -j ACCEPT # (2) DNS-Pakete von den Nameservern aus der obigen Liste for i in $nslist; do # TCP (für große Pakete): nur abgehende Verbindungen iptables -A INPUT -p tcp -s $i --sport domain ! --syn -j ACCEPT iptables -A INPUT -p udp -s $i --sport domain -j ACCEPT done # (3) UDP-DNS-Pakete von überall zu unserem DNS-Cache. Hier brauchen wir # keine eigene TCP-Regel, weil BIND für TCP-Anfragen einen unprivilegierten # Port benutzt. #iptables -A INPUT -p udp --sport domain --dport 7531 -j ACCEPT # Fragmentierte ICMP-Pakete blockieren iptables -A INPUT -p icmp --fragment -j LOG iptables -A INPUT -p icmp --fragment -j DROP # Bestimmte ICMP-Pakete erlauben iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT iptables -A INPUT -p icmp --icmp-type source-quench -j ACCEPT iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT iptables -A INPUT -p icmp --icmp-type parameter-problem -j ACCEPT # Manche Ereignisse kommen so häufig vor, dass ich sie im Protokoll nicht # sehen will. iptables -A INPUT -p udp --dport netbios-ns -j DROP #iptables -A INPUT -p udp --dport netbios-dgm -j DROP #iptables -A INPUT -p tcp --dport netbios-ssn -j DROP # Alles andere wird zunächst protokolliert und dann gesperrt. iptables -A INPUT -j LOG iptables -A INPUT -j DROP ##### OUTPUT-Chain ############################################################ # NUR ZUM TESTEN: Alle Pakete senden # iptables -A OUTPUT -j ACCEPT # Pakete nach Loopback iptables -A OUTPUT -o lo -j ACCEPT # Bestimmte UDP-Pakete iptables -A OUTPUT -p udp --sport 1024: --dport 53 -j ACCEPT # domain/udp #iptables -A OUTPUT -p udp --dport 7091 -j ACCEPT # nautilus # Wir betreiben ein paar Server iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT # our sshd iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT # our httpd #iptables -A OUTPUT -p tcp --sport 113 -j ACCEPT # our authd # TCP-Pakete von lokalen Client-Programmen iptables -A OUTPUT -p tcp --sport 1024: --dport 21 -j ACCEPT # ftp iptables -A OUTPUT -p tcp --sport 1024: --dport 22 -j ACCEPT # ssh iptables -A OUTPUT -p tcp --sport 1024: --dport 23 -j ACCEPT # telnet iptables -A OUTPUT -p tcp --dport 25 -j ACCEPT # smtp iptables -A OUTPUT -p tcp --sport 1024: --dport 37 -j ACCEPT # time iptables -A OUTPUT -p tcp --sport 1024: --dport 43 -j ACCEPT # whois iptables -A OUTPUT -p tcp --sport 1024: --dport 53 -j ACCEPT # domain/tcp iptables -A OUTPUT -p tcp --sport 1024: --dport 79 -j ACCEPT # finger iptables -A OUTPUT -p tcp --sport 1024: --dport 80 -j ACCEPT # www iptables -A OUTPUT -p tcp --sport 1024: --dport 110 -j ACCEPT # pop-3 iptables -A OUTPUT -p tcp --sport 1024: --dport 119 -j ACCEPT # nntp iptables -A OUTPUT -p tcp --sport 1024: --dport 143 -j ACCEPT # imap2 iptables -A OUTPUT -p tcp --sport 1024: --dport 443 -j ACCEPT # https iptables -A OUTPUT -p tcp --sport 1024: --dport 554 -j ACCEPT # Real G2 # Abgehende TCP-Verbindungen sind erlaubt, wenn auf beiden Enden der # Verbindung ein unsicherer Port benutzt wird. Unsicher, aber für # passives FTP notwendig, sofern -m state nicht benutzt wird. iptables -A OUTPUT -p tcp --sport 1024: --dport 1024: -j ACCEPT # Alternative: Das Zustandsmodul erlaubt aktives und passives FTP # (beachten Sie auch die entsprechende Regel in der INPUT-Chain!) #iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Bestimmte ICMP-Pakete erlauben iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT iptables -A OUTPUT -p icmp --icmp-type destination-unreachable -j ACCEPT iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT # Der Rest wird protokolliert und gesperrt. Bei TCP-Verbindungen senden # wir eine Fehlermeldung an unser eigenes Programm, damit wir nicht auf # den langwierigen Timeout warten müssen. iptables -A OUTPUT -j LOG iptables -A OUTPUT -p tcp -j REJECT --reject-with tcp-reset iptables -A OUTPUT -j DROP ##### FORWARD-Chain ########################################################### # NUR ZUM TESTEN: Alle Pakete protokollieren # iptables -A FORWARD -j LOG # Abgehende Pakete iptables -A FORWARD -o ppp0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT # Ankommende Pakete iptables -A FORWARD -o eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT # Adressübersetzung: nat-Tabelle! iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE