Emulating Networks with Katharà
In questa pagina 9
See also: ARP - Address Resolution ProtocolTo place an IP datagram in a frame a node needs the MAC address of the next hop, obtained with ARP: a broadcast request (destination MAC FF:FF:FF:FF:FF:FF, carrying the sender's IP and MAC and the wanted IP) and a unicast reply with the wanted MAC, stored in the ARP cache; ARP always resolves the next hop (destination host or router), never the final destination, and since broadcasts do not leave the subnet a router can answer with its own MAC (proxy ARP).ARP - Address Resolution Protocol →, ICMP, Ping and TracerouteICMP adds to IP the error reporting and queries it lacks; its messages travel inside IP datagrams with Protocol 1 and are either errors (destination unreachable type 3, time exceeded 11, redirect 5, parameter problem 12), always sent to the original source with the original IP header plus 8 bytes of data, or queries (echo request 8 and reply 0, timestamp 13 and 14); ICMP only reports and never corrects errors, and it supports ping (RTT), path MTU discovery with the D bit (maximum payload 1500-20-8=1472 bytes) and traceroute (TTL increasing, n+1 probes for n routers), but also attacks such as smurf and fake redirects.ICMP, Ping and Traceroute →, Network Layer and IPv4 AddressingThe network layer delivers datagrams host to host through routers (encapsulation, routing, forwarding); an IPv4 address is 32 bits split into a network prefix and a host suffix, and modern classless addressing (CIDR) gives a block of 2^(32-n) addresses from a.b.c.d/n, with network and broadcast addresses computed through the netmask.Network Layer and IPv4 Addressing →.
Why an emulator
Real networks have many devices, interfaces and protocols, and experimenting on a company or university network is impossible; buying equipment is costly. The solution is a network emulator.
Definition (simulation and emulation). A simulator reproduces the performance of a real system (delays, losses) with a model. An emulator reproduces the functions (configurations, architectures, protocols): the programs that run are the real ones, with little attention to performance.
Example. A ping between two Katharà containers answers in under 1 ms because they are on the same machine: times are not those of a real link; in the lab one checks whether packets arrive and which way they go.
Katharà (Kathará, the successor of Netkit, built with Python and Docker) is the emulator used in the lab. Every emulated device (host, router, server) is a container, created from an image: Base (DNS, web server, network tools), Quagga and FRRouting (standard routing protocols; used for RIP with kathara/frr, see RIP and Advanced Routing in KatharàLab 3 configures static routes on a three-router triangle with /20 and /30 networks so that every interface answers every ping, and uses traceroute to discover routers through the TTL; Lab 5 replaces manual tables with RIPv2 run by FRRouting (zebra and ripd, vtysh, router rip, network, redistribute connected), on a five-router stub network with a default route injected into RIP.RIP and Advanced Routing in Katharà →), Open vSwitch, Behavioral Model (P4).
Definition (device and collision domain). Each device has a console (terminal window), memory, its own filesystem and zero or more network interfaces (
eth0,eth1, ...). Each interface connects to a virtual collision domain, named by a letter or a name (A, B, MYNET), which can connect several interfaces.
Example. If pc1.eth0 and pc2.eth0 are on domain A, the two hosts are on the same LAN as if attached to the same hub. So in Katharà no hubs or switches are added. A collision domain corresponds to an IP network; devices in different domains communicate only through a router (Static Routing and Diagnostic Tools in KatharàLab 2 builds by hand the routing tables of two LANs (195.11.14.0/24 and 200.1.1.0/24) joined by routers r1 and r2 over 100.0.0.8/30: an active interface adds only its directly connected network, hosts use a default route, routers need static routes in both directions, and ping, route -n, tcpdump and traceroute diagnose the result (ttl=62 after two routers).Static Routing and Diagnostic Tools in Katharà →).
Katharà commands
| family | command | what it does |
|---|---|---|
| v-commands (one machine) | vstart |
starts a new device with chosen interfaces |
vconfig |
attaches more interfaces to a running device | |
vclean |
stops a device | |
| l-commands (a whole lab) | lstart |
starts all devices of the lab |
lclean |
stops all devices | |
lrestart |
stops and restarts (resets e.g. ARP caches) | |
linfo |
lab information | |
lcrash |
stops devices abruptly | |
| global | check |
checks the environment |
connect |
opens the console of a running machine | |
list |
lists running machines | |
settings |
shows and changes settings | |
wipe |
deletes all machines and links |
kathara check # test the installation
kathara vstart -n pc1 --eth 0:A # start pc1 with eth0 on domain A
kathara vclean -n pc1 # stop pc1In the university lab the host terminal prepares the environment with source ./kathara.sh, then cd into the lab folder and lstart / lclean; lwipe removes every leftover (machines and virtual networks).
Sharing files with the real machine
The /shared folder inside each device points to the lab's shared folder (enabled by default, read and write). /hosthome points to the real user's home (disabled by default, enabled with kathara settings). Example. tcpdump -i eth0 -w /hosthome/capture.pcap puts the capture in the user's home, outside the container.
Structure of a lab
Definition (Katharà lab). A folder containing a
lab.conffile with the topology; a<name>.startupfile per device (commands run inside the device right after boot); optionally a subfolder<name>/per device, whose content is copied into the root/of that device's filesystem.
Example. pc1/foo/file.txt becomes /foo/file.txt in pc1; vpc/var/www/index.html puts a web page in server vpc; r1/etc/shadow sets the root password of r1.
lab.conf has lines machine[argument]=value: if the argument is a number, value is the collision domain of interface eth<number>; otherwise the argument is an option name.
pc1[0]=A
pc2[0]=A
pc2[1]=B
pc3[0]=Bpc1 has eth0 on A; pc2 has eth0 on A and eth1 on B (so it can act as a router); pc3 has eth0 on B.
| line | meaning |
|---|---|
r1[image]="kathara/frr" |
container image (FRR daemons) |
s1_client[num_terms]=1 |
number of terminal windows (0 = none) |
s2_attacker[bridged]=1 |
also connect to the physical network |
s2_client[sysctl]="net.ipv4.conf.all.arp_accept=1" |
set a kernel parameter in the container |
s1_client[ipv6]=0 |
disable IPv6 |
.startup files are shell scripts run inside the device at boot, typically configuring interfaces and starting services:
ifconfig eth0 10.0.0.1/24 up # assign the address and activate the interface
/etc/init.d/zebra start # start a service (here a routing daemon)Configuring an interface with ifconfig
Formula.
ifconfig <interface> <address> netmask <mask> broadcast <broadcast address> [mtu <bytes>] up, equivalent toifconfig eth0 <address>/<length> up.
Example. ifconfig eth0 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255 up equals ifconfig eth0 10.0.0.1/24 up.
netmask 255.255.255.0: first bits are the network part. Network address = address AND mask:10.0.0.1AND255.255.255.010.0.0.0. Broadcast = network OR (NOT mask)10.0.0.255. There are addresses, for hosts. Without the mask Linux uses the class mask, wrong for subnetted networks (e.g./30): always write it.upactivates the interface; Linux then automatically adds the directly connected network (10.0.0.0/24oneth0) to the routing table, which is why two hosts on the same network reach each other without further configuration.
The MTU can be changed: ifconfig eth0 10.0.0.5 mtu 4000 up (address 10.0.0.5, MTU bytes; the default mask for 10.x.x.x is class A, , so better to specify it).
The "Two Hosts" lab
pc1 10.0.0.1/24 and pc2 10.0.0.2/24 on domain A.
Step 1: create the machines.
kathara vstart -n pc1 --eth 0:A
kathara vstart -n pc2 --eth 0:AThe message network with name netkit_nt_A already exists is not a real error: domain A was created by pc1.
Step 2: configure the interfaces.
pc1:~# ifconfig eth0 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255 up
pc2:~# ifconfig eth0 10.0.0.2 netmask 255.255.255.0 broadcast 10.0.0.255 upStep 3: ping.
pc1:~# ping 10.0.0.2
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=2.65 ms
5 packets transmitted, 5 received, 0% packet lossping sends ICMP echo request messages and waits for echo reply. 56(84) bytes: bytes of data ICMP IP bytes. icmp_seq: request number. ttl=64: stays because there is no router between the hosts; each router subtracts . time: RTT; the first is higher because it includes ARP resolution. Options: -c N (N requests), -w s (maximum wait); stop with Ctrl+C.
Step 4: tcpdump is a packet sniffer:
pc2:~# tcpdump -i eth0
19:27:17.899782 arp who-has 10.0.0.2 tell 10.0.0.1
19:27:18.002578 arp reply 10.0.0.2 is-at fe:fd:0a:00:00:02
19:27:18.004384 IP 10.0.0.1 > 10.0.0.2: icmp 64: echo request seq 1
19:27:18.005806 IP 10.0.0.2 > 10.0.0.1: icmp 64: echo reply seq 1- ARP request (
who-has 10.0.0.2 tell 10.0.0.1):pc1knows only the IP ofpc2but needs its MAC for the Ethernet frame, so it asks in broadcast; - ARP reply (
is-at ...):pc2answers in unicast; - echo request and echo reply, one pair per
icmp_seq.
ARP appears only the first time (then the cache has the MAC).
| option | meaning |
|---|---|
-i eth0 |
interface to listen on |
-w file.pcap |
save packets to a file |
-r file.pcap |
read a saved file |
-n / -nn |
no name resolution / also no protocol and port names |
-t |
no timestamps (not meaningful in Katharà) |
-e |
show the link-layer header (MAC addresses) |
-c N |
stop after packets |
tcpdump -tenni eth0 combines -t -e -n -n -i.
Step 5: graphical view. Save with tcpdump -i eth0 -w /hosthome/capture.pcap and open the file on the real computer with Wireshark, a packet dissector showing every field of every header and the payload.
Katharà MAC addresses
MACs have the form fe:fd followed by the four bytes of the IP in hexadecimal: 10.0.0.2 fe:fd:0a:00:00:02 (); 200.1.1.7 FE:FD:C8:01:01:07 (); 195.11.14.5 FE:FD:C3:0B:0E:05. This is the emulator's choice; in a real network the MAC is assigned by the manufacturer.
Lab exercises
- Ping to a non-existing address. In the same network (e.g.
10.0.0.99): nobody answers the ARP who-has,pingprintsDestination Host Unreachable. Outside the network without route or default gateway (e.g.192.168.5.5):connect: Network is unreachableimmediately, nothing is sent. Reason: the only table entry is10.0.0.0/24;192.168.5.5AND255.255.255.0192.168.5.0, no match. A wrong mask causes the same: withpc1at10.0.0.1/25andpc2at10.0.0.200,10.0.0.200AND255.255.255.12810.0.0.128, not the network ofpc1. - ARP in Wireshark. The request has source MAC of
pc1and destinationff:ff:ff:ff:ff:ff; the reply carries the MAC ofpc2. - Different domains.
pc3andpc4on another domain B are unreachable frompc1: no router. In another IP networkpinggivesNetwork is unreachable; in the same IP network ARP would not cross the domain border.
Common mistakes
- Forgetting
up: the interface stays down and is absent from the routing table. - Wrong mask: different networks on the same domain, hence
Network is unreachable. - Machines on different domains without a router.
- Judging performance from
pingtimes: Katharà emulates functions, not delays. - Forgetting
lcleanandlwipe.
Versione ripasso
- Emulator vs simulator. Simulator: performance with a model; emulator: real functions and programs. Katharà
pingtimes (under 1 ms) are not real. - Architecture. Katharà (Python + Docker, ex Netkit): each device is a container from an image (base, Quagga, FRRouting
kathara/frr, Open vSwitch, Behavioral Model; RIP and Advanced Routing in KatharàLab 3 configures static routes on a three-router triangle with /20 and /30 networks so that every interface answers every ping, and uses traceroute to discover routers through the TTL; Lab 5 replaces manual tables with RIPv2 run by FRRouting (zebra and ripd, vtysh, router rip, network, redistribute connected), on a five-router stub network with a default route injected into RIP.RIP and Advanced Routing in Katharà →). - Collision domain. Each interface (
eth0,eth1) joins a named virtual domain (A, B, MYNET); interfaces on the same domain see each other like on a hub, so no hubs or switches. Domain = IP network; different domains need a router (Static Routing and Diagnostic Tools in KatharàLab 2 builds by hand the routing tables of two LANs (195.11.14.0/24 and 200.1.1.0/24) joined by routers r1 and r2 over 100.0.0.8/30: an active interface adds only its directly connected network, hosts use a default route, routers need static routes in both directions, and ping, route -n, tcpdump and traceroute diagnose the result (ttl=62 after two routers).Static Routing and Diagnostic Tools in Katharà →). - Commands. v-commands:
vstart,vconfig,vclean. l-commands:lstart,lclean,lrestart(resets ARP caches),linfo,lcrash. Global:check,connect,list,settings,wipe. Sequence:source ./kathara.sh,cdlab,lstart...lclean,lwipe.
kathara check
kathara vstart -n pc1 --eth 0:A # pc1 with eth0 on domain A
kathara vclean -n pc1- Files.
/sharedis the lab folder (default);/hosthomethe user's home (enable withkathara settings).tcpdump -i eth0 -w /hosthome/capture.pcap. - Lab.
lab.conf(topology),<name>.startup(boot script per machine),<name>/folder copied into/(pc1/foo/file.txtbecomes/foo/file.txt). lab.conf.machine[N]=domainputsethNon a domain; non-numeric arguments are options:image,num_terms,bridged,sysctl,ipv6. Example:pc1[0]=A,pc2[0]=A,pc2[1]=B,pc3[0]=B(pc2in the middle can route).ifconfig.ifconfig eth0 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255 [mtu N] upifconfig eth0 10.0.0.1/24 up. Always write the mask.upadds the directly connected network to the routing table. MTU:ifconfig eth0 10.0.0.5 mtu 4000 up.
pc1:~# ifconfig eth0 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255 up
pc1:~# ping 10.0.0.2
pc2:~# tcpdump -tenni eth0- Ping.
56(84) bytes: .ttl=64stays 64 without routers (62 after two). First RTT higher (ARP).-c N,-w s. tcpdumpshows. ARP requestwho-hasin broadcast, ARP replyis-atin unicast, then echo request and reply. ARP only the first time (ARP cache).- Options.
-i,-w,-r,-n,-nn,-t,-e(MACs),-c N. Open.pcapwith Wireshark. - Katharà MAC.
fe:fd+ IP bytes in hex:10.0.0.2fe:fd:0a:00:00:02;195.11.14.5FE:FD:C3:0B:0E:05. - Exercises. Same network, absent host:
Destination Host Unreachable. No route:Network is unreachable, nothing sent. ARP request destinationff:ff:ff:ff:ff:ff. Different domains without router: unreachable. - Mistakes: forgetting
up; wrong mask; different domains without router; judging delays; forgettinglcleanandlwipe. The ARP attack lab is in Man-in-the-Middle Attack with ARP PoisoningLab 7 shows that Telnet sends user name and password in clear, so with a hub a tcpdump on the attacker is enough, while with a switch the attacker needs ARP poisoning (false ARP replies repeated every second) and IP forwarding to sit in the middle and read the session in Wireshark with Follow TCP stream.Man-in-the-Middle Attack with ARP Poisoning →.