Securus Journal 43.65°N, 79.38°W

Watching the wire with tcpdump

Using tcpdump to capture, filter and read live network traffic from the command line, and where the tool and its filter language came from.

Contents

Where it came from

In October 1986 the link between the Lawrence Berkeley Laboratory and the University of California at Berkeley, sites some 400 yards apart, slowed from 32 kilobits a second to 40 bits a second. Van Jacobson, at LBL, went looking for the cause, and in 1988 he and Mike Karels published it: TCP had no way to back off when the network filled up. Their fixes are still in every TCP stack. Finding the answer meant watching packets, and the tool for that came out of the same LBL group in 1988. Its manual page credits Jacobson, Craig Leres and Steven McCanne. They called it tcpdump. In 1993 McCanne and Jacobson published the BSD Packet Filter, the in-kernel machine that the filter syntax still compiles down to.

This exercise used tcpdump to capture live traffic on a local interface and see what was actually moving across the wire. It is easy to assume a network is quiet until someone looks. The output looks dense at first, but once each field makes sense the picture is remarkably detailed.


Capturing traffic

To start a basic capture on the primary interface:

sudo tcpdump -i eth0

The -i flag selects the interface. Replace eth0 with whatever ip a shows for the active interface (ens3, wlan0, and so on). Without filters this floods the terminal within seconds, so in practice a capture is almost always narrowed down.

To capture to a file for later analysis in Wireshark or tshark:

sudo tcpdump -i eth0 -w capture.pcap

Capturing needs root, or the CAP_NET_RAW and CAP_NET_ADMIN capabilities, because it reads raw frames from the interface rather than data handed up through a socket. That is also why a capture file deserves the care of any other sensitive data: whatever crossed the wire unencrypted is now sitting in it.


Filtering

Filters are where tcpdump becomes useful. The Berkeley Packet Filter (BPF) syntax can be very specific:

# Only HTTP and HTTPS traffic
sudo tcpdump -i eth0 port 80 or port 443

# Traffic to or from a specific host
sudo tcpdump -i eth0 host 192.168.1.1

# Only TCP SYN packets (connection attempts)
sudo tcpdump -i eth0 'tcp[tcpflags] == tcp-syn'

# DNS queries only
sudo tcpdump -i eth0 udp port 53

Combining filters with and, or and not slices traffic precisely without capturing everything and sorting through it afterwards. The SYN filter shows what BPF can do: tcp[tcpflags] reads the flags byte straight out of the TCP header, and == tcp-syn keeps only packets where SYN is the one flag set, so the SYN-ACK replies are left out. The quotes keep the shell from interpreting the brackets.

One more flag earns a place in nearly every command: -n. By default tcpdump turns addresses into hostnames and port numbers into service names, and to do that it sends reverse DNS lookups of its own, which then appear in the capture. -n turns that off:

sudo tcpdump -n -i eth0 udp port 53

Reading the output

A DNS query, captured with -n, looks like this:

12:34:56.789012 IP 192.168.1.42.52318 > 8.8.8.8.53: 41325+ A? example.com. (29)

Breaking it down:

  • timestamp: microsecond precision
  • network protocol: IP for IPv4, IP6 for IPv6, ARP for address resolution
  • source.port > destination.port: the direction of the packet; tcpdump joins the port to the address with a dot
  • protocol details: here tcpdump has decoded DNS, so it prints the query ID (41325), a + for “recursion desired”, and the question, an A record for example.com
  • length: for DNS, the size of the DNS message in bytes, in brackets at the end

Without -n, the same line would show dns.google.domain in place of 8.8.8.8.53. For UDP traffic tcpdump has no decoder for, the details shrink to the generic UDP, length 33, where the number is the UDP payload in bytes.

For TCP, you’ll see flags like [S] (SYN), [S.] (SYN-ACK), [.] (ACK), [P.] (PSH-ACK, data), [F.] (FIN) and [R] (RST). Watching a full three-way handshake followed by data transfer followed by FIN shows how TCP actually behaves in the wild, and it is the same exchange Jacobson was trying to see in 1986.


Security relevance

Mapped to the CompTIA Security+ exam outline (SY0-701), the work touches three domains.

Domain 2, Threats, Vulnerabilities, and Mitigations: identifying unusual traffic patterns, unexpected protocols, or connections to suspicious destinations.

Domain 3, Security Architecture: understanding how protocols behave at the packet level informs better firewall rules and network segmentation.

Domain 4, Security Operations: tcpdump is a staple of incident response. When something goes wrong, a packet capture is often the first thing a responder reaches for.


The habit

tcpdump has low overhead, ships on almost every Unix-like system, and needs nothing but a terminal and the right permissions. There is no substitute for watching traffic. Knowing how TCP works in theory is useful; watching a handshake, a retransmission and a reset in real time is something else.

With Wireshark for visual analysis or tshark for scripting, it makes a capable packet-analysis workflow that costs nothing and installs almost nothing. It serves for baselining traffic, spotting anomalies, debugging protocols and incident response on any Linux or BSD system.

Jacobson found the cause of an eight-hundredfold slowdown by looking at packets. The tool that came out of that looking is already installed.

Sources

  • Jacobson, Van, and Michael J. Karels. “Congestion Avoidance and Control.” Proceedings of SIGCOMM ‘88, 1988.
  • McCanne, Steven, and Van Jacobson. “The BSD Packet Filter: A New Architecture for User-level Packet Capture.” Proceedings of the USENIX Winter 1993 Conference, 1993.
  • The Tcpdump Group. tcpdump(1) and pcap-filter(7) manual pages.
  • CompTIA. Security+ (SY0-701) Exam Objectives.