08. Juni 2006
Aus Labor für Echtzeitsysteme
Inhaltsverzeichnis |
Aufgabe
Aufgabe jeder Gruppe war es ein Netzwerk mit verschiedenene Diensten entsprechend der Anforderungen in Firewall.png aufzubauen. Dazu wurden mehrere Kabel und ein Switch pro Gruppe bereitgestellt.
Lösungsweg Gruppe 1 (FireFuckers):
Um die Übung mit Firewall-Systemen zu vertiefen wird folgendes Fallbeispiel behandelt:
1.) Baue Netzstruktur auf:
2.) Konfiguriere die Systeme mit den folgenden Adressen:
Folgende UNIX-Befehle können für die Konfiguration benutzt werden:
root@A4# ifconfig eth0 172.16.10.5 netmask 255.255.0.0 up root@A4# route add default gw 172.16.10.1
Danach kann die Konfiguration folgende kontrollieren:
root@A4# ifconfig eth0 Protokoll:Ethernet Hardware Adresse 00:D0:59:C6:BE:AF inet Adresse:172.16.10.5 Bcast:172.16.255.255 Maske:255.255.0.0 inet6 Adresse: fe80::2d0:59ff:fec6:beaf/64 Gültigkeitsbereich:Verbindung UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 Kollisionen:0 Sendewarteschlangenlänge:1000 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) lo Protokoll:Lokale Schleife inet Adresse:127.0.0.1 Maske:255.0.0.0 inet6 Adresse: ::1/128 Gültigkeitsbereich:Maschine UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:3 errors:0 dropped:0 overruns:0 frame:0 TX packets:3 errors:0 dropped:0 overruns:0 carrier:0 Kollisionen:0 Sendewarteschlangenlänge:0 RX bytes:172 (172.0 b) TX bytes:172 (172.0 b)
root@A4# route -n Kernel IP Routentabelle Ziel Router Genmask Flags Metric Ref Use Iface 172.16.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 172.16.10.1 0.0.0.0 UG 0 0 0 eth0
3.) Aufsetzen der Firewall-Regeln
Es wurden zwei Shell-Scripte ( firewall_up.sh , firewall_down.sh ) angefertigt, welche die Firewall-Regeln aufsetzen/zurücksetzen. Die Aufsetzung der Firewall-Regeln basiert auf dem Netfilter-Prinzip, welcher im Linux-Kernel implementiert ist ( Im Kernel? @Lars oder Herr Quade ?!??)
!! Die Regeln sollten natürlich nur auf dem Firewall-System angewendet werden (A3) !!
Netfilter-Prinzip
(" Unvollständig " - Noch nicht getestet!!! )
1.) firewall_up.sh
#!/bin/bash MODPROBE="/sbin/modprobe" A4=172.16.10.5 A3_0=192.168.50.1 A3_1=172.16.10.1 B3=192.168.50.2 B4=192.168.50.3 # --- Start ---- echo echo "Setting up Firewall......." $MODPROBE ip_tables # Benutze iptables $MODPROBE ip_conntrack # Um einzelne Verbindungen zu zuordnen $MODPROBE iptable_nat # NAT Unterstützdung $MODPROBE ipt_MASQUERADE # MASQUERADE echo "Modules loaded!" echo echo "Applying rules....." echo echo "1" > /proc/sys/net/ipv4/ip_forward # Damit Paketweiterleitung zwischen ETH's funzt # Gehe von restriktiver Firewall aus! Verbiete alles und lasse einzelne Dinge zu iptables -P INPUT DROP iptables -P FORWARD DROP iptables -t nat -P POSTROUTING DROP iptables -t nat -P PREROUTING DROP # -- Damit bestende oder aufgebaute Verbinungen zugelassen werden iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A PREROUTING -t nat -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A POSTROUTING -t nat -m state --state ESTABLISHED,RELATED -j ACCEPT # -- SSH von A4 auf A3 iptables -A INPUT -s $A4 -d $A3_1 -p TCP --dport 22 -j ACCEPT # -- ssh A4 auf B3/B4 iptables -A FORWARD -t filter -p TCP -s $A4 -m iprange --src-range $B3-$B4 --dport 22 -j ACCEPT # -- ssh B3/B4 auf A4 iptables -A FORWARD -t filter -p TCP -m iprange --src-range $B3-$B4 -d $A4 --dport 22 -j ACCEPT # -- PORT-FORWARDING (DNAT) von extern (alle) auf intern (B3) iptables -A PREROUTING -t nat -p tcp -d $A3_1 --dport 80 -j DNAT --to $B3 iptables -A FORWARD -t filter -d $B3 -p tcp --dport 80 -j ACCEPT # -- telnet/finger > Mirror ?? # -- Ping Pakete > 32 Byte DROP iptables -A PREROUTING -t nat -p ICMP -m length --length 33: -j DROP # -- Alle übrigen Pakete DROP + LOG iptables -A PREROUTING -t nat -j LOG -m limit --limit 5/s --limit-burst 20 iptables -A PREROUTING -t nat -j DROP exit 0;
2.) firewall_down.sh
#/bin/bash iptables -F INPUT iptables -F OUTPUT iptables -F FORWARD iptables -t nat -F PREROUTING iptables -t nat -F POSTROUTING iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT iptables -t nat -P PREROUTING ACCEPT iptables -t nat -P POSTROUTING ACCEPT exit 0;
ggf. müssen die Rechte der Scripts in der SHELL angepasst werden:
root@A4# chmod 700 firewall_up
root@A4# chmod 700 firewall_down
Die Firewall-Regeln kann man sich folgend ansehen:
root@A3# iptables -L Chain INPUT (policy DROP) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT tcp -- 172.16.10.5 172.16.10.1 tcp dpt:ssh Chain FORWARD (policy DROP) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT tcp -- 172.16.10.5 anywhere source IP range 192.168.50.2-192.168.50.3 tcp dpt:ssh ACCEPT tcp -- anywhere 172.16.10.5 source IP range 192.168.50.2-192.168.50.3 tcp dpt:ssh ACCEPT tcp -- anywhere 192.168.50.2 tcp dpt:www Chain OUTPUT (policy ACCEPT) target prot opt source destination
root@A3# iptables -t nat -L Chain PREROUTING (policy DROP) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED DNAT tcp -- anywhere 172.16.10.1 tcp dpt:www to:192.168.50.2 DROP icmp -- anywhere anywhere length 33:65535 LOG all -- anywhere anywhere limit: avg 5/sec burst 20 LOG level warning DROP all -- anywhere anywhere Chain POSTROUTING (policy DROP) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED Chain OUTPUT (policy ACCEPT) target prot opt source destination
Lösungsweg Gruppe 2 (Gruppe ohne Namen):
Konfiguration von A1
Hier die Netzwerkkonfigurationsdatei editieren /etc/network/interface. IP-Adresse Netzmaske Broadcast etc. wie nachfolgend beschrieben anpassen. Als Gateway muss A2 eingerichtet sein.
# Konfig vom A-Rechner # The primary network interface auto eth0 iface eth0 inet static address 172.16.10.5 network 172.16.10.0 netmask 255.255.0.0 broadcast 172.16.255.255 gateway 172.16.10.1 # IP von A2 (Router)
Konfiguration der beiden B Rechner:
Auf beiden muss A2 als Default-Gateway eingerichtet sein.
Datei /etc/network/interfaces editieren und folgendes in die Konfig einfügen(Beispiel):
# Konfig von einem B-Rechner # The primary network interface auto eth0 iface eth0 inet static address 192.168.5.1 network 192.168.5.0 netmask 255.255.255.0 broadcast 192.168.5.255 gateway 192.168.5.3 # IP von A2 (Router)
Konfiguration des Firewall Rechners:
Netzwerkkonfiguration aus /etc/network/interfaces :
Die Datei entsprechend editieren und mittels ifdown <if>, ifup <if> die Interfaces aktivieren. Für NICHT-debianbasierte System ifconfig ethX [down|up], oder den Netzwerkdienst neustarten user@host:/etc/init.d/networking restart. Weicht von NICHT-debianbasierten Systemen ab.
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # This is a list of hotpluggable network interfaces. # They will be activated automatically by the hotplug subsystem. mapping hotplug script grep map eth1 # The primary network interface auto eth0 iface eth0 inet static address 172.16.10.1 network 172.16.10.0 netmask 255.255.0.0 broadcast 172.16.255.255 auto eth1 iface eth1 inet static address 192.168.5.3 network 192.168.5.0 netmask 255.255.255.0 broadcast 192.168.5.255
Ausgabe von ifconfig :
eth0 Protokoll:Ethernet Hardware Adresse 00:06:4F:0E:33:94 inet Adresse:172.16.10.1 Bcast:172.16.10.255 Maske:255.255.0.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:1189 errors:0 dropped:0 overruns:0 frame:0 TX packets:950 errors:0 dropped:0 overruns:2 carrier:0 Kollisionen:0 Sendewarteschlangenlänge:1000 RX bytes:117090 (114.3 KiB) TX bytes:213067 (208.0 KiB) Interrupt:11 Basisadresse:0xd400 eth1 Protokoll:Ethernet Hardware Adresse 00:04:76:95:C9:83 inet Adresse:192.168.5.3 Bcast:192.168.5.255 Maske:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1898 errors:0 dropped:0 overruns:0 frame:0 TX packets:1830 errors:0 dropped:0 overruns:0 carrier:0 Kollisionen:0 Sendewarteschlangenlänge:1000 RX bytes:333857 (326.0 KiB) TX bytes:279720 (273.1 KiB) Interrupt:10 Basisadresse:0x2000
Ausgabe von route -n :
Kernel IP Routentabelle Ziel Router Genmask Flags Metric Ref Use Iface 192.168.5.0 * 255.255.255.0 U 0 0 0 eth1 172.16.0.0 * 255.255.0.0 U 0 0 0 eth0
Script zur Konfiguration von iptables :
Da das Target MIRROR nicht mehr im aktuellen Kernel verfügbar ist, wäre REJECT eine mögliche Alternative.
#! /bin/sh #Forwarding aktivieren echo 1 > /proc/sys/net/ipv4/ip_forward ############################################################################### #vars iptbin="/sbin/iptables" a1_ip="172.16.10.5" a2_ip="172.16.10.1" b1_ip="192.168.5.1" b2_ip="192.168.5.2" ############################################################################### # Flush all tables ${iptbin} -F ${iptbin} --table nat --flush # Delete the optional user-defined chain specified ${iptbin} --delete-chain # a policy to block all input and forwarding ${iptbin} -P INPUT DROP ${iptbin} -P OUTPUT DROP ${iptbin} -P FORWARD DROP # Create a LOGDROP chain to log and drop packets ${iptbin} -N LOGDROP ${iptbin} -A LOGDROP -j LOG --log-prefix "DROP: " ${iptbin} -A LOGDROP -j DROP ############################################################################### # Allow unlimited traffic on the loopback interface ${iptbin} -A INPUT -i lo -j ACCEPT ${iptbin} -A OUTPUT -o lo -j ACCEPT # Allow unlimited outbound traffic ${iptbin} -A OUTPUT -j ACCEPT ############################################################################### # allow SSH from A1 to A2, B1, B2 ${iptbin} -A INPUT --source $a1_ip -p tcp --dport 22 --destination $a2_ip -j ACCEPT ${iptbin} -A FORWARD --source $a1_ip -p tcp --dport 22 --destination $b1_ip -j ACCEPT ${iptbin} -A FORWARD --source $b1_ip -p tcp --sport 22 --destination $a1_ip -j ACCEPT ${iptbin} -A FORWARD --source $a1_ip -p tcp --dport 22 --destination $b2_ip -j ACCEPT ${iptbin} -A FORWARD --source $b2_ip -p tcp --sport 22 --destination $a1_ip -j ACCEPT # allow SSH from B1 auf A1 ${iptbin} -A FORWARD --source $b1_ip -p tcp --dport 22 --destination $a1_ip -j ACCEPT ${iptbin} -A FORWARD --source $a1_ip -p tcp --sport 22 --destination $b1_ip -j ACCEPT # allow SSH from B2 auf A1 ${iptbin} -A FORWARD --source $b2_ip -p tcp --dport 22 --destination $a1_ip -j ACCEPT ${iptbin} -A FORWARD --source $a1_ip -p tcp --sport 22 --destination $b2_ip -j ACCEPT #http von A1/A2 auf B1 ${iptbin} -A FORWARD --source $a1_ip -p tcp --destination $b1_ip --dport 80 -j ACCEPT ${iptbin} -A FORWARD --source $a2_ip -p tcp --destination $b1_ip --dport 80 -j ACCEPT ${iptbin} -A FORWARD --source $b1_ip -p tcp --destination $a1_ip --sport 80 -j ACCEPT ${iptbin} -A FORWARD --source $b1_ip -p tcp --destination $a2_ip --sport 80 -j ACCEPT #telnet/finger --> Mirror # nur kernel 2.3 und 2.4 #${iptbin} -A. INPUT -p tcp --dport 23 -j MIRROR #${iptbin} -A INPUT -p tcp --dport 79 -j MIRROR #Ping-Pakete > 32 Byte --> Drop ${iptbin} -A INPUT -p icmp --icmp-type 8 -m length --length :32 -j ACCEPT ${iptbin} -A INPUT -p icmp --icmp-type 0 -m length --length :32 -j ACCEPT ${iptbin} -A FORWARD -p icmp --icmp-type 8 -m length --length :32 -j ACCEPT ${iptbin} -A FORWARD -p icmp --icmp-type 0 -m length --length :32 -j ACCEPT ${iptbin} -A OUTPUT -p icmp --icmp-type 8 -m length --length :32 -j ACCEPT ${iptbin} -A OUTPUT -p icmp --icmp-type 0 -m length --length :32 -j ACCEPT ############################################################################### # !!! Letzte Regel !!! # Drop all other traffic ${iptbin} -A INPUT -j LOGDROP ${iptbin} -A OUTPUT -j LOGDROP ${iptbin} -A FORWARD -j LOGDROP
Resultierende Firewall-Regeln :
Chain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- lo any anywhere anywhere 0 0 ACCEPT tcp -- any any 172.16.10.5 172.16.10.1 tcp dpt:ssh 0 0 ACCEPT icmp -- any any anywhere anywhere icmp echo-request length 0:32 0 0 ACCEPT icmp -- any any anywhere anywhere icmp echo-reply length 0:32 15 4920 LOGDROP all -- any any anywhere anywhere Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- any any 172.16.10.5 192.168.5.1 tcp dpt:ssh 0 0 ACCEPT tcp -- any any 192.168.5.1 172.16.10.5 tcp spt:ssh 0 0 ACCEPT tcp -- any any 172.16.10.5 192.168.5.2 tcp dpt:ssh 0 0 ACCEPT tcp -- any any 192.168.5.2 172.16.10.5 tcp spt:ssh 0 0 ACCEPT tcp -- any any 192.168.5.1 172.16.10.5 tcp dpt:ssh 0 0 ACCEPT tcp -- any any 172.16.10.5 192.168.5.1 tcp spt:ssh 0 0 ACCEPT tcp -- any any 192.168.5.2 172.16.10.5 tcp dpt:ssh 0 0 ACCEPT tcp -- any any 172.16.10.5 192.168.5.2 tcp spt:ssh 0 0 ACCEPT tcp -- any any 172.16.10.5 192.168.5.1 tcp dpt:www 0 0 ACCEPT tcp -- any any 172.16.10.1 192.168.5.1 tcp dpt:www 0 0 ACCEPT tcp -- any any 192.168.5.1 172.16.10.5 tcp spt:www 0 0 ACCEPT tcp -- any any 192.168.5.1 172.16.10.1 tcp spt:www 12 361 ACCEPT icmp -- any any anywhere anywhere icmp echo-request length 0:32 12 361 ACCEPT icmp -- any any anywhere anywhere icmp echo-reply length 0:32 9 469 LOGDROP all -- any any anywhere anywhere Chain LOGDROP (3 references) pkts bytes target prot opt in out source destination 24 5389 LOG all -- any any anywhere anywhere LOG level warning prefix `DROP: ' 24 5389 DROP all -- any any anywhere anywhere Chain OUTPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- any lo anywhere anywhere 185 20648 ACCEPT all -- any any anywhere anywhere 0 0 ACCEPT icmp -- any any anywhere anywhere icmp echo-request length 0:32 0 0 ACCEPT icmp -- any any anywhere anywhere icmp echo-reply length 0:32 0 0 LOGDROP all -- any any anywhere anywhere
Hinweise:
- Bei der Ping-Paketgrösse ist zu beachten das beim erzeugen nicht 32 Byte angegebn werden, da durch die Pakete der anderen Schichten noch Bytes hinzukommen und iptables die gesamte Paketgrösse beachtet.
- ...