Sub topic 12
Seeing the layers in a packet capture
This chapter assumes you know the four TCP/IP layers, how encapsulation works, and the names frame, packet, segment, and datagram. It also assumes you can run commands with sudo in a terminal. Here you will capture traffic and label each header by layer.
What a capture shows
A capture tool such as tcpdump or Wireshark records frames as they cross a network interface. Each frame holds the headers added during encapsulation, in order. Reading a capture from left to right is the same as reading the stack from the bottom up.
Capturing a few packets
This command captures five TCP packets on port 80. The -nn flag turns off name and port lookups. The -e flag prints link-layer addresses. The -v flag prints more IP and TCP fields. The -c 5 flag stops after five packets.
sudo tcpdump -nn -e -v -c 5 'tcp port 80'Generate some traffic in another terminal, for example with curl http://192.0.2.10/. A typical first packet looks like this:
12:00:00.000000 02:00:00:aa:bb:01 > 02:00:00:cc:dd:02, ethertype IPv4 (0x0800), length 74: (tos 0x0, ttl 64, id 4242, offset 0, flags [DF], proto TCP (6), length 60)
192.0.2.20.51514 > 192.0.2.10.80: Flags [S], seq 1000, win 64240, options [mss 1460], length 0Labeling the headers
| Part of the output | Layer | What it tells you |
|---|---|---|
| `02:00:00:aa:bb:01 > 02:00:00:cc:dd:02`, `ethertype IPv4` | Link (OSI 2) | Source and destination MAC addresses for this hop. The EtherType says an IPv4 packet follows. |
| `ttl 64`, `proto TCP (6)`, `192.0.2.20 > 192.0.2.10` | Internet (OSI 3) | End-to-end addresses, the hop limit, and the protocol number of the next header. |
| `.51514 > .80`, `Flags [S]`, `seq 1000` | Transport (OSI 4) | Source and destination ports, plus the TCP flags and sequence number. `S` is a SYN, the first step of the handshake. |
| Payload after the TCP header | Application | Here the first packet carries none (`length 0`). Later packets carry HTTP text. |
Each header names the next one. The EtherType selects IPv4, and the IP protocol field selects TCP. The destination port tells the host which program should receive the data. This is how decapsulation knows what to do at each step.
Building the same stack yourself
Scapy lets you build a packet with the same layers. The / operator stacks one header on top of the next.
from scapy.all import Ether, IP, TCP
pkt = Ether() / IP(dst="192.0.2.10") / TCP(dport=80, flags="S")
pkt.show()show() prints one section for each layer: ###[ Ethernet ]###, ###[ IP ]###, and ###[ TCP ]###. Compare these sections with the tcpdump output. The field names differ a little, but the layers are the same. Building a packet does not send it, so this runs without sudo.
Letting the operating system add the headers
A normal program supplies only data, an address, and a port:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(b"hello", ("127.0.0.1", 9999))To watch the kernel add the lower layers, run a capture on Linux first, then run the script:
sudo tcpdump -nn -e -i lo 'udp port 9999'You should see a UDP datagram of 5 bytes, wrapped in an IP packet and a link-layer header. The loopback interface has no real MAC addresses, so the link layer looks plain there.
Common mistakes when reading captures
- Expecting TLS traffic to be readable. On port 443 you can still see Ethernet, IP, and TCP headers. The application data is encrypted, so you will not see HTTP text.
- Reading MAC addresses as end-to-end. MAC addresses change at every router. IP addresses stay the same unless NAT rewrites them.
- Forgetting where the capture ran. A capture on your own machine may show frames before checksum offload fills in the checksums. These can look wrong and are usually harmless.
- Capturing too much. Use a filter such as
'tcp port 80'or'host 192.0.2.10'to keep the output small.
Exercise
In a captured frame you see ethertype IPv4, proto TCP (6), and destination port 80. Name the layer that each of these three fields helps to select, and say what each one selects.
Show answer
The EtherType is in the link-layer header and selects the internet-layer protocol, here IPv4. The protocol field is in the IP header and selects the transport protocol, here TCP. The destination port is in the TCP header and selects the application that receives the data, here a web server on port 80.