Sub topic 13
Seeing the layers in a packet capture
A packet capture is a recording of frames as they pass a network interface. Every chapter so far described headers in the abstract. A capture lets you check them against real bytes.
This chapter assumes you know the layers (application, transport, network, link), what a header and a payload are, and how to read hexadecimal bytes. You also need a terminal where you can run tcpdump with administrator rights. Wireshark shows the same information in a graphical view.
What a capture contains
A capture tool asks the operating system for a copy of each frame the interface sends or receives. It sees the frame after the network stack has built it, so every layer's header is present, outermost first.
A capture does not show everything:
- The Ethernet frame check sequence (FCS), the 4-byte trailer, is normally stripped by the network card before the capture tool sees it.
- On the sending machine, a capture may be taken before the card finishes its work, so some checksums look wrong.
- If the payload is encrypted, as in HTTPS, you see the TLS records but not the HTTP request inside them.
Capturing and reading one packet
This command captures five TCP packets on port 80. Replace eth0 with your interface name.
sudo tcpdump -i eth0 -e -nn -v -c 5 tcp port 80Each option has a job:
-eprints the Ethernet header.-nnshows numeric addresses and ports instead of looking up names.-vprints extra IP header fields.-c 5stops after five packets.tcp port 80is a capture filter. It decides which frames are kept.
Here is output for one HTTP request. The addresses match the example used earlier in this tutorial, and the layout is typical for tcpdump.
12:12:15.347120 11:22:33:44:55:66 > aa:bb:cc:dd:ee:ff, ethertype IPv4 (0x0800), length 91: (tos 0x0, ttl 64, id 4242, offset 0, flags [DF], proto TCP (6), length 77)
192.168.1.10.50000 > 203.0.113.5.80: Flags [P.], cksum 0x1a2b (correct), seq 1:38, ack 1, win 65535, length 37: HTTP: GET / HTTP/1.1The first line covers the Ethernet and IP headers. The second line covers TCP and the start of the payload.
| Output fragment | Layer | Header field |
|---|---|---|
| `11:22:33:44:55:66 > aa:bb:cc:dd:ee:ff` | Link | Source and destination MAC |
| `ethertype IPv4 (0x0800)` | Link | Type field, which names the next protocol |
| `length 91` | Link | Whole frame without FCS: 14 + 77 |
| `ttl 64`, `proto TCP (6)` | Network | Time to live, protocol number |
| `length 77` (inside the parentheses) | Network | IP total length: 20 + 20 + 37 |
| `192.168.1.10 > 203.0.113.5` | Network | Source and destination IP |
| `.50000 > .80` | Transport | Source and destination port |
| `Flags [P.]`, `seq 1:38`, `ack 1`, `win 65535` | Transport | TCP flags (PSH and ACK), sequence range, acknowledgment, window |
| `length 37` (after the colon) | Application | Payload size in bytes |
| `GET / HTTP/1.1` | Application | The data being carried |
Two lines both say length. The frame length of 91 contains the IP length of 77, which contains the TCP payload length of 37. The sizes nest the same way the headers do.
Reading the raw bytes
Adding -XX prints each frame in hexadecimal, starting at the Ethernet header. This is the same frame as above, split into its headers.
| Bytes (hex) | Meaning |
|---|---|
| `aa bb cc dd ee ff` | Destination MAC |
| `11 22 33 44 55 66` | Source MAC |
| `08 00` | EtherType: IPv4 |
| `45` | IP version 4, header length 5 words (20 bytes) |
| `00 4d` | IP total length: 77 |
| `40 06` | TTL 64, protocol 6 (TCP) |
| `c0 a8 01 0a` | Source IP 192.168.1.10 |
| `cb 00 71 05` | Destination IP 203.0.113.5 |
| `c3 50 00 50` | Source port 50000, destination port 80 |
| `50 18` | TCP header length 5 words (20 bytes), flags PSH and ACK |
| `47 45 54 20 2f ...` | ASCII `GET /` and the rest of the request |
The IP header begins at byte 14 and the TCP header at byte 34, which is 14 + 20. The payload begins at byte 54. You can find any layer's start by adding up the header lengths. Those lengths come from the Ethernet header (always 14 bytes here), the IP header length field, and the TCP data offset field.
Making a capture file yourself
You can write a frame into a capture file and open it with tcpdump -r or Wireshark. This script builds the frame from the earlier example and saves it in the pcap format.
import struct
import time
http = b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
tcp = struct.pack("!HHIIHHHH", 50000, 80, 1, 0, (5 << 12) | 0x018, 65535, 0, 0) + http
src_ip = bytes([192, 168, 1, 10])
dst_ip = bytes([203, 0, 113, 5])
ip = struct.pack("!BBHHHBBH4s4s", 0x45, 0, 20 + len(tcp), 0, 0, 64, 6, 0, src_ip, dst_ip) + tcp
frame = struct.pack("!6s6sH", bytes.fromhex("aabbccddeeff"), bytes.fromhex("112233445566"), 0x0800) + ip
with open("demo.pcap", "wb") as f:
# Global header: magic, version 2.4, timezone, accuracy, snap length, link type 1 (Ethernet)
f.write(struct.pack("<IHHiIII", 0xA1B2C3D4, 2, 4, 0, 0, 65535, 1))
# Record header: seconds, microseconds, captured length, original length
f.write(struct.pack("<IIII", int(time.time()), 0, len(frame), len(frame)))
f.write(frame)Then read it back:
tcpdump -nn -e -v -r demo.pcapBecause the script leaves the checksums at 0, tcpdump reports them as incorrect. That is expected. Checksums are the one field in this example that a real network stack or card fills in.
Seeing what changes at each hop
Capture on both sides of a router and compare the same packet. Here is what you will find:
- The IP source and destination are the same on both sides.
- The TTL is one lower after the router.
- The source and destination MAC addresses are different. The router rewrites them to describe the next link.
- The IP header checksum is different, because the TTL changed.
- The TCP header and the payload are unchanged.
Run tcpdump -e on a client and look at the destination MAC of a packet going to a remote server. It is the MAC of your default gateway, not the server. This is why the ARP exchange from the previous chapters happens for the gateway's IP address and not for the server's.
Capturing ARP
ARP frames have no IP header. They sit directly inside Ethernet, with EtherType 0x0806.
sudo tcpdump -i eth0 -e -nn arpYou will see a broadcast request, "who has 192.168.1.1, tell 192.168.1.10", sent to ff:ff:ff:ff:ff:ff. The reply is sent directly back to the requester's MAC address. If the request does not appear before your first IP packet to a new neighbor, the neighbor's entry was already in the ARP cache.
Common surprises
- Frames larger than the MTU. On the sending machine you may see a 6000-byte packet on a 1500-byte link. The card splits it afterward (segmentation offload). A capture on a different machine will show normal-sized frames.
- Wrong checksums on outgoing packets. Checksum offload makes the card fill in the checksum after the capture point. If incoming packets show correct checksums, the network is fine.
- Truncated payloads. Some tools cut each frame at a snap length. Use
-s 0with older tcpdump versions if you need full frames. - VLAN tags. A tagged frame has 4 extra bytes after the MAC addresses, and the EtherType shifts four bytes later.
- Tunnels. A tunneled packet shows two IP headers. The outer one is used for forwarding, and the inner one is carried as payload. Wireshark lists both in its layer tree.
- Two kinds of filter. A capture filter such as
tcp port 80limits what is recorded, and it cannot be changed after the fact. A Wireshark display filter such astcp.port == 80only hides frames that are already recorded.
Checking your understanding
Exercise
A capture line shows length 1514 on the Ethernet line and length 1500 inside the IP parentheses. How many bytes of TCP payload does the packet carry if the TCP header is 20 bytes and the IP header is 20 bytes?
Show answer
The Ethernet header takes 1514 - 1500 = 14 bytes. The IP total length of 1500 includes the IP header (20) and the TCP header (20), so the payload is 1500 - 20 - 20 = 1460 bytes. This is the usual MSS on an Ethernet link with a 1500-byte MTU.
Exercise
You capture the same packet on the client's interface and on the server's interface, with a router between them. Which of these fields are the same in both captures: source IP, destination MAC, TTL, TCP source port?
Show answer
The source IP and the TCP source port are the same. The destination MAC differs, because each link has its own MAC addresses, and the TTL is lower at the server by the number of routers crossed.