Sub topic 12
Seeing the layers in a packet capture
This chapter assumes you know the four TCP/IP layers (link, internet, transport, application), the idea of encapsulation, and the unit names frame, packet, segment, and datagram. You should also be comfortable running commands in a terminal and have permission to capture traffic on the machine you use. Capture only on networks you own or are authorized to inspect.
A packet capture is a recording of the bytes that cross a network interface. Reading one turns the layer model into something you can point at. Each header you see belongs to one layer, and the order of the headers matches the order of encapsulation.
What a capture actually contains
A capture tool asks the operating system for a copy of each frame as it is sent or received on an interface. The copy starts at the link-layer header, so the outermost header you see is almost always Ethernet. The tool then decodes the bytes from the outside in:
- Read the Ethernet header. Its type field says which protocol comes next.
- Read the IP header. Its protocol field says whether TCP, UDP, or ICMP follows.
- Read the TCP or UDP header. The port numbers hint at the application protocol.
- Treat what remains as application data.
This is decapsulation done by a tool instead of by a host. Each header names the next one, so a decoder never has to guess.
Building a packet to see the stacking
Before capturing real traffic, build a packet yourself. Scapy is a Python library for creating and decoding packets. Its / operator stacks one layer on top of another, which mirrors encapsulation from the link layer up to the transport layer.
from scapy.all import Ether, IP, TCP
pkt = Ether() / IP(dst="192.0.2.10") / TCP(dport=80, flags="S")
pkt.show()Running this prints three sections, one per header: ###[ Ethernet ]###, ###[ IP ]###, and ###[ TCP ]###. Scapy fills in defaults you did not set, such as the source addresses and the Ethernet type field. The address 192.0.2.10 comes from a range reserved for documentation, so the packet is never meant to reach a real host. Building a packet does not send it. Sending needs a call such as sendp() and usually administrator rights, and you do not need it here.
Look at the flags="S" argument. It sets the SYN flag, which makes this the first segment of a TCP connection. You will see the same flag in a real capture.
Capturing with tcpdump
tcpdump is a command-line capture tool. The following command captures five TCP packets on port 80 and prints the link-layer addresses (-e) and the IP and TCP header fields (-v), so each header can be matched to its layer.
sudo tcpdump -nn -e -v -c 5 'tcp port 80'The options mean:
-nnturns off name lookups. Addresses stay numeric and ports stay numbers, so DNS lookups do not add extra packets to the capture.-eprints the Ethernet header, with source and destination MAC addresses.-vprints more IP and TCP detail, such as TTL and header length.-c 5stops after five packets.'tcp port 80'is a capture filter. It is applied while capturing, and it keeps only matching packets.
Capturing needs elevated privileges, which is why the command uses sudo. To generate traffic, run curl http://example.com/ in a second terminal while the capture waits.
Reading one captured line by layer
A typical line from the capture looks like this. It is wrapped for readability and its values are illustrative, so your addresses and numbers will differ:
11:53:20.101 aa:bb:cc:00:00:01 > aa:bb:cc:00:00:02, ethertype IPv4 (0x0800), length 74:
(tos 0x0, ttl 64, id 1234, offset 0, flags [DF], proto TCP (6), length 60)
192.168.1.20.51512 > 93.184.216.34.80: Flags [S], seq 1000, win 64240, length 0Match each part to a layer.
| Part of the line | Layer | What it tells you |
|---|---|---|
| `aa:bb:cc:00:00:01 > aa:bb:cc:00:00:02` | Link | Source and destination MAC addresses for this one hop |
| `ethertype IPv4 (0x0800)` | Link | The next header is IPv4 |
| `length 74` | Link | Size of the whole frame as captured |
| `ttl 64`, `flags [DF]` | Internet | Hop limit and the do-not-fragment bit |
| `proto TCP (6)` | Internet | The next header is TCP |
| `192.168.1.20 > 93.184.216.34` | Internet | End-to-end source and destination IP addresses |
| `.51512 > .80` | Transport | Source port (chosen by the client) and destination port (the service) |
| `Flags [S]`, `seq`, `win` | Transport | TCP SYN flag, sequence number, and receive window |
The line has no application layer part, because a SYN segment carries no data. The application layer appears later in the capture, when the client sends an HTTP request. Plain HTTP shows readable text such as GET / HTTP/1.1. HTTPS shows only encrypted bytes after the TLS handshake.
What changes from hop to hop
If you capture on two machines along a path, you can see which fields stay fixed and which change. This follows from the layering:
- The IP addresses stay the same from the sender to the receiver, apart from changes made by address translation.
- The MAC addresses change at every router, because the link layer only covers one hop.
- The TTL drops by one at each router.
- The TCP ports stay the same, again unless a NAT device rewrites them.
Capturing on your own machine therefore shows your MAC address and your gateway's MAC address. The MAC address of the remote server never appears, because the server is not on your local link.
Capture filters and display filters
Two kinds of filter exist, and mixing them up is a common error.
- A capture filter limits what is recorded. It uses Berkeley Packet Filter (BPF) syntax, as in
'tcp port 80'. Packets that do not match are discarded and cannot be recovered. - A display filter limits what is shown from a file that is already recorded. Wireshark has its own display filter language, for example
tcp.port == 80. Nothing is lost.
For a first investigation, a broad capture saved to a file gives you more room to explore. Use tcpdump to write a file and Wireshark to read it:
sudo tcpdump -nn -i any -w sample.pcap 'host 192.0.2.10'The -w option writes raw packets to sample.pcap. The -i any option captures on all interfaces, but on Linux it records a different link header (Linux cooked capture) instead of Ethernet, so the MAC addresses are not shown. Pick a single interface, such as -i eth0, if you need Ethernet headers.
The application layer from a program's point of view
A program that sends data does not build any of these headers. The following program supplies only the application data, an address, and a UDP port. The operating system adds the UDP, IP, and link-layer headers below it.
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(b"hello", (“127.0.0.1”, 9999))If you capture on the loopback interface while this runs (sudo tcpdump -nn -i lo udp port 9999 on Linux), you see a UDP datagram with a 5-byte payload. The IP header is 20 bytes and the UDP header is 8 bytes, so the IP packet is 33 bytes long. Nobody needs to be listening for the datagram to appear in the capture, although the operating system may answer with an ICMP "port unreachable" message.
Common errors when reading captures
- Seeing nothing. The capture filter may be wrong, or you chose the wrong interface. Check with
tcpdump -D, which lists interfaces. - Capturing on the wrong side of a NAT. Addresses differ before and after translation, so match traffic using ports and timing.
- Checksum errors on outgoing packets. Many network cards compute checksums after the capture point (checksum offload). Your own outgoing packets can look corrupt in the capture even though they are fine on the wire.
- Frames larger than the MTU. Segmentation offload lets the capture show frames bigger than 1500 bytes, since the card splits them later.
- Unreadable application data. TLS encrypts the application layer. You can still see the lower layers, the sizes, and the timing.
- Switched networks. A switch sends unicast frames only to the port that needs them. A capture on your machine shows only your own traffic plus broadcasts.
Exercises
Exercise
In the captured line 192.168.1.20.51512 > 93.184.216.34.80: Flags [S], which layers do the address 93.184.216.34 and the number 80 belong to, and why does the MAC address of the server not appear in a capture taken on your own machine?
Show answer
The address 93.184.216.34 is an IP address, so it belongs to the internet layer. The number 80 is a TCP port, so it belongs to the transport layer. MAC addresses only identify devices on one link. On your machine the destination MAC is your gateway's, because the server is on a different network and the router rewrites the link header for each hop.
Exercise
A colleague captures with the filter 'tcp port 443' and later wants to look at DNS traffic from the same session. Why will this fail, and what should they have done?
Show answer
A capture filter discards packets that do not match, and DNS normally uses UDP port 53, so no DNS packets were recorded. They should capture with a broader filter, or none, and write to a file with -w, then use a display filter in Wireshark to narrow the view afterward.
Exercise
The program in this chapter sends the 5-byte payload "hello" over UDP on the loopback interface. What is the size of the IP packet, and which header contributes how many bytes?
Show answer
The IP packet is 33 bytes: a 20-byte IPv4 header (without options), an 8-byte UDP header, and 5 bytes of application data.