Skip to content
Tutorials Center
My learning

Topics

Lesson 68 of 79 ยท Tools and Troubleshooting

Packet Capture with tcpdump and Wireshark

Capture and read network packets with tcpdump and Wireshark: capture vs display filters, a TCP handshake, DNS and HTTP captures, follow stream, plus a Python pcap parser.

Tools like ping and curl tell you what the network did. A packet capture shows you exactly what was on the wire: every frame, header and byte. When two systems disagree about what they sent, the capture is the referee. It is how engineers find a missing reply, a retransmission storm, a malformed header or a firewall that eats one particular packet.

Two programs dominate: tcpdump, a command-line tool that is available on almost every server, and Wireshark, a graphical analyzer that decodes hundreds of protocols and lets you click through the layers you met in layered models. Both use the same capture library (libpcap or Npcap) and the same file format, pcap, so a capture taken with one opens in the other.

The captures in this lesson are sample text. The Python examples build and parse a real pcap byte stream, so you can see what these tools read from the file.

Where captures happen

A network card normally keeps only frames addressed to it. In promiscuous mode it hands up everything it sees. On a modern switched network (switches) you still only see your own traffic plus broadcasts, so to watch someone else's traffic you capture on the machine itself, on the router or firewall, or on a switch mirror (SPAN) port or a hardware tap.

Capturing needs administrator rights, and captures often contain passwords, cookies and personal data. Capture only where you are allowed to, store files securely and delete them when done. Encrypted traffic (TLS) shows headers and sizes but not content.

Capture filters versus display filters

This distinction trips up everyone at first.

  • A capture filter decides which packets are saved at all. It uses BPF (Berkeley Packet Filter) syntax, runs inside the kernel and is efficient. Packets that do not match are gone forever. Use it to keep files small on busy links.
  • A display filter (Wireshark) hides packets already captured. It has its own richer syntax with protocol fields, can be changed at any time and loses nothing.
Goal Capture filter (BPF) Wireshark display filter
One host host 203.0.113.25 ip.addr == 203.0.113.25
One port port 53 tcp.port == 80 or udp.port == 53
Only TCP SYN packets tcp[tcpflags] & tcp-syn != 0 tcp.flags.syn == 1
A subnet net 192.168.1.0/24 ip.addr == 192.168.1.0/24
HTTP status 404 not possible http.response.code == 404
Retransmissions not possible tcp.analysis.retransmission

Rule of thumb: use a capture filter to limit volume, a display filter to investigate. Filters on application fields such as HTTP status codes exist only in Wireshark, because they require the program to decode the packet.

tcpdump basics

TEXT
$ sudo tcpdump -i eth0 -n host 203.0.113.25 and tcp port 80
$ sudo tcpdump -i any -n -c 20 udp port 53          # 20 DNS packets, all interfaces
$ sudo tcpdump -i eth0 -w web.pcap port 443         # save to a file
$ tcpdump -r web.pcap -nn -A 'tcp port 80'          # read a file, show ASCII payload

The common flags: -i the interface, -n do not resolve names (faster and clearer), -nn also keep port numbers numeric, -c stop after N packets, -w write a pcap file, -r read one, -A or -X print the payload as ASCII or hex+ASCII, -s how many bytes of each packet to keep.

Reading a TCP handshake

Here is the opening of a web request as tcpdump prints it (client 192.168.1.20, server 203.0.113.25):

TEXT
12:00:00.000000 IP 192.168.1.20.51044 > 203.0.113.25.80: Flags [S],  seq 1000, win 64240, options [mss 1460,sackOK,TS,nop,wscale 7], length 0
12:00:00.018100 IP 203.0.113.25.80 > 192.168.1.20.51044: Flags [S.], seq 5000, ack 1001, win 65535, options [mss 1440,sackOK,TS,nop,wscale 8], length 0
12:00:00.018200 IP 192.168.1.20.51044 > 203.0.113.25.80: Flags [.],  ack 5001, win 502, length 0
12:00:00.018900 IP 192.168.1.20.51044 > 203.0.113.25.80: Flags [P.], seq 1001:1038, ack 5001, win 502, length 37: HTTP: GET / HTTP/1.1
12:00:00.037000 IP 203.0.113.25.80 > 192.168.1.20.51044: Flags [P.], seq 5001:5044, ack 1038, win 256, length 43: HTTP: HTTP/1.1 200 OK

How to read each line: the time, the protocol, source.port > destination.port, then the TCP flags in brackets: S is SYN, . is ACK, S. is SYN-ACK, P. is PSH+ACK (data being pushed), F is FIN, R is RST. Then the sequence numbers, the acknowledgment, the receive window and the payload length. seq 1001:1038 means bytes 1001 up to (not including) 1038. Notice that the ACK for the 37-byte request is 1038: it is the next byte expected, exactly as described in TCP reliability.

The first three lines are the three-way handshake: time between line 1 and 2 (18 ms) is one round trip to the server. The TCP options in the SYN packets show the negotiated MSS and window scaling.

Tcpdump prints sequence numbers relative to the start (like above) by default; Wireshark does the same with "relative sequence numbers" and can show the raw values if you disable it.

Reading DNS and HTTP captures

A DNS lookup is a single small UDP exchange:

TEXT
12:00:05.100000 IP 192.168.1.20.40211 > 192.168.1.1.53: 31337+ A? example.org. (29)
12:00:05.112000 IP 192.168.1.1.53 > 192.168.1.20.40211: 31337 1/0/0 A 203.0.113.25 (45)

31337 is the query ID that matches request to answer, + means recursion desired, A? is the question, and 1/0/0 counts the answers, authority records and additional records. If you see the first line repeated with no answer, the resolver is unreachable or filtered. If the answer has no addresses (0/0/0 with NXDomain), the name does not exist.

Plain HTTP is readable directly in the payload (-A flag): the request line, the headers and the body. With HTTPS you see the TLS handshake (a ClientHello that may contain the server name in the clear) followed by opaque encrypted records.

Wireshark in practice

Wireshark shows three panes: the packet list (one row per packet, color coded), the packet details (a tree with one expandable node per layer: Frame, Ethernet, IP, TCP, HTTP), and the raw bytes. Click a field in the tree and the matching bytes light up.

Useful features:

  • Follow TCP Stream (right-click a packet): reassembles the conversation and shows it as text, client data in one color and server data in another. This is the fastest way to read an HTTP or SMTP dialogue.
  • Statistics > Conversations / I/O Graph / Flow Graph: who talks the most, throughput over time, and a ladder diagram of the exchange.
  • Expert Information: Wireshark's own diagnosis such as retransmissions, duplicate ACKs, zero window or out-of-order segments.
  • Decode As and TLS keys: if you have the session keys (from a browser's key log file), Wireshark can decrypt your own HTTPS traffic for debugging.

Signs worth recognizing: repeated SYN packets without a SYN-ACK (blocked or host down), a RST straight after the SYN (nothing listening), repeated identical segments (retransmissions from loss), a window of zero (receiver overloaded), and a long gap before the first byte of the response (a slow server rather than a slow network).

flowchart LR
    NIC["Network interface"] --> CAP["Capture library<br/>(libpcap / Npcap)"]
    CAP -->|"capture filter (BPF)"| FILE["pcap file"]
    FILE --> DIS["Wireshark decoders"]
    DIS -->|"display filter"| VIEW["Packet list and details"]

A capture filter chooses what is saved; a display filter chooses what you look at.

The pcap file format

A classic pcap file is simple: a 24-byte global header (magic number, version, snapshot length and the link type, 1 for Ethernet), then for each packet a 16-byte record header (timestamp seconds, microseconds, captured length, original length) followed by the raw frame bytes. The timestamp and length fields are in the byte order of the machine that wrote the file; the magic number 0xa1b2c3d4 tells readers which. (The newer pcapng format adds blocks for interface descriptions and comments, but the idea is the same.)

Python Build a tiny pcap in memory and read it back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The checksums in this toy capture are left as zero. A real capture has valid ones, though capturing on the sending machine often shows "bad checksum" because the network card fills the field in after the capture point (checksum offload). That is a classic false alarm.

Following a stream and filtering

"Follow TCP Stream" works by ordering the segments of one direction by sequence number, dropping bytes seen before (retransmissions) and joining the payloads. A display filter is a boolean test per packet. The next example does both on parsed packets (here already decoded into dictionaries).

Python A mini display filter and Follow TCP Stream

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good capture habits

  • Capture as close to the problem as possible, and ideally on both ends so you can tell where a packet vanished.
  • Start the capture before reproducing the problem; stop soon after. Small, focused files are easier to read.
  • Filter at capture time on busy links, but remember you cannot get discarded packets back.
  • Note the clock: compare captures from two machines only if their clocks are synchronized (NTP).
  • Know the limits: captures are a view from one point. A packet missing here might have been lost before or after this point.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the key difference between a capture filter and a display filter?

    Packets rejected by a capture filter are never stored, while a display filter can be changed freely because every packet is still in the file.
  2. 2. In tcpdump output, what does Flags [S.] mean?

    S is SYN and the dot stands for the ACK flag, so [S.] is the second step of the three-way handshake.
  3. 3. A capture shows a client sending SYN packets again and again with no reply from the server. What is a likely cause?

    Repeated SYNs mean no SYN-ACK arrives. A firewall drop, a dead host or a routing problem are the usual explanations; a RST would indicate a closed port instead.
  4. 4. In the line 'seq 1001:1038, ack 5001, length 37', what will the receiver acknowledge once it has this data?

    An ACK number is the next byte expected. The segment covers bytes 1001 through 1037, so the receiver acknowledges 1038.
  5. 5. Why might you see 'bad checksum' warnings for packets captured on the sending machine?

    With checksum offload the network card computes the checksum after the packet has passed the capture point, so the captured copy still has an empty value.

Summary

  • A packet capture is the ground truth of what crossed the wire; tcpdump (command line) and Wireshark (graphical) share the pcap format.
  • Capture filters (BPF) decide what is saved; display filters decide what you see. Use capture filters for volume, display filters for investigation.
  • tcpdump lines show source.port > destination.port, TCP flags (S, S., ., P., F, R), sequence and ACK numbers, window and payload length.
  • Follow TCP Stream reorders segments by sequence number, discards retransmitted bytes and presents the conversation as text.
  • Capture legally and securely, near the problem and ideally at both ends, and remember that encrypted payloads stay encrypted.

Next, use these tools together in a methodical way in troubleshooting network problems.