tcpdump and BPF Capture Filters
Wireshark is for looking; tcpdump is for capturing. It runs on any Unix server, needs no GUI, and writes standard pcap files that every other tool can read. Its filters are BPF (Berkeley Packet Filter) expressions, compiled into a tiny program that the kernel runs on every packet before it is copied to user space, so a good filter keeps a capture on a busy link small and cheap. This lesson covers the BPF language from simple primitives to byte offsets, runs filters on the sample captures, and shows what tcpdump does with them.
The sandbox can't capture live traffic (no root, no CAP_NET_RAW), which is itself a useful lesson:
tcpdump -i lo -c 1 2>&1
echo "exit code: $?"tcpdump: lo: You don't have permission to perform this capture on that device
(socket: Operation not permitted)
exit code: 1
On a real system, capturing needs root or the capabilities cap_net_raw and cap_net_admin (granted, for example, to dumpcap for the members of the wireshark group). All other examples read files with -r.
Options you use every day
| Option | Does |
|---|---|
-i eth0 / -i any |
interface to capture on |
-nn |
no name or port lookups (faster, and no DNS queries that leak your analysis) |
-r file / -w file |
read / write pcap |
-c N |
stop after N packets |
-e |
print link-layer headers (MACs, VLAN tags) |
-A / -X |
payload as ASCII / hex and ASCII |
-s 0 / -s 128 |
snap length: whole packets (default 262144) or only headers |
-tttt |
absolute timestamps with date |
-G 3600 -W 24 / -C 100 |
rotate files every hour, keep 24 / every 100 MB |
-Z user |
drop privileges after opening the interface |
The BPF language
| Kind | Examples |
|---|---|
| host / network | host 10.10.0.10, src net 10.10.0.0/24, dst host 192.0.2.25 |
| port | port 53, tcp dst port 80, portrange 8000-8100 |
| protocol | arp, icmp, tcp, udp, ip6, vlan 20, ether proto 0x888e |
| logic | and or not, parentheses (quote the whole expression for the shell) |
| byte offsets | proto[offset:size] with &, ==, > … : tcp[13] & 2 != 0, ip[8] < 5, udp[10] & 0x80 != 0 |
| named bits | tcp[tcpflags] & tcp-syn != 0, icmp[icmptype] == icmp-echo |
Offsets count from the start of the named header. For TCP the header length varies, so payload offsets are computed: tcp[((tcp[12] & 0xf0) >> 2):4] reads the first 4 payload bytes.
Filters on the sample captures
cd $SAMPLES/pcap
echo "== DNS responses (QR bit, byte 2 of the DNS header = udp[10]):"
tcpdump -nn -r dns.pcap 'udp port 53 and udp[10] & 0x80 != 0' 2>/dev/null
echo "== DNS answers with RCODE 3 (NXDOMAIN, low 4 bits of udp[11]):"
tcpdump -nn -r dns.pcap 'udp port 53 and udp[11] & 0x0f == 3' 2>/dev/null
echo "== ARP replies (operation field at arp[6:2]):"
tcpdump -nn -r arp.pcap 'arp[6:2] == 2' 2>/dev/null
echo "== TCP segments whose payload starts with 'POST':"
tcpdump -nn -r http.pcap 'tcp[((tcp[12] & 0xf0) >> 2):4] = 0x504f5354' 2>/dev/null
echo "== the second connection only, saved to its own file:"
tcpdump -nn -r http.pcap -w ~/login.pcap 'tcp port 50414' 2>/dev/null # the samples folder is read-only
capinfos -c ~/login.pcap | tail -1== DNS responses (QR bit, byte 2 of the DNS header = udp[10]):
10:00:00.001200 IP 10.10.0.53.53 > 10.10.0.5.40001: 6699* 1/0/0 A 10.10.0.10 (58)
10:00:00.501100 IP 10.10.0.53.53 > 10.10.0.5.40002: 6700 NXDomain* 0/1/0 (105)
== DNS answers with RCODE 3 (NXDOMAIN, low 4 bits of udp[11]):
10:00:00.501100 IP 10.10.0.53.53 > 10.10.0.5.40002: 6700 NXDomain* 0/1/0 (105)
== ARP replies (operation field at arp[6:2]):
10:00:00.000400 ARP, Reply 10.10.0.1 is-at 02:00:00:0a:0a:01, length 28
10:00:05.000000 ARP, Reply 10.10.0.1 is-at 02:00:00:0a:0a:66, length 28
== TCP segments whose payload starts with 'POST':
10:00:01.000750 IP 10.10.0.5.50414 > 10.10.0.10.80: Flags [P.], seq 2000001:2000174, ack 4000001, win 502, length 173: HTTP: POST /login HTTP/1.1
== the second connection only, saved to its own file:
Number of packets: 10
0x504f5354 is "POST" in ASCII. Matching application bytes in BPF only works when the data is at a fixed position in one segment; for anything more, capture broadly and use Wireshark's display filters afterwards.
What the kernel actually runs
-d prints the compiled BPF program. Reading it once shows what "efficient" means here: a handful of loads and comparisons per packet.
tcpdump -r $SAMPLES/pcap/http.pcap -d 'tcp dst port 80' 2>/dev/null(000) ldh [12]
(001) jeq #0x86dd jt 2 jf 6
(002) ldb [20]
(003) jeq #0x6 jt 4 jf 15
(004) ldh [56]
(005) jeq #0x50 jt 14 jf 15
(006) jeq #0x800 jt 7 jf 15
(007) ldb [23]
(008) jeq #0x6 jt 9 jf 15
(009) ldh [20]
(010) jset #0x1fff jt 15 jf 11
(011) ldxb 4*([14]&0xf)
(012) ldh [x + 16]
(013) jeq #0x50 jt 14 jf 15
(014) ret #65535
(015) ret #0
Line by line: load the EtherType at byte 12; if IPv6 (0x86dd), check the next-header byte and the destination port at a fixed offset; if IPv4 (0x800), check the protocol byte for TCP (6), skip fragments that aren't the first (jset #0x1fff), load the IP header length into x, and compare the port at x + 16 (14 Ethernet + 2) with 80. ret #65535 keeps the packet, ret #0 drops it.
Capturing as a defender
- Scope and authorisation first: capture only where you are allowed to, and capture as little as answers the question (headers only with
-s 128, one host, one port). - Rotate and limit:
-G/-C/-Wso a capture can't fill the disk. - Drop privileges:
-Z tcpdump(the default on many distributions) so the packet parser doesn't run as root. - Protect the output: pcaps contain credentials and personal data; restrict permissions, encrypt, hash, delete when done.
- Mind the vantage point: a capture on a server shows only its traffic; a SPAN/mirror port or a network tap is needed to see others', and tags and checksums may look different there.
Pitfalls
- Unquoted expressions: the shell interprets
&,(,)and!. Always put the filter in single quotes. port 80matches source or destination; usedst port 80for requests only.- Fragments: only the first fragment carries the TCP/UDP header, so port filters miss the rest (the compiled program above explicitly skips non-first fragments).
vlanchanges offsets for everything after it, andtcp port 80 or vlanbehaves differently fromvlan or tcp port 80.
Practice
1. Write a filter for TCP SYNs to port 22 from outside 10.10.0.0/24.
'tcp dst port 22 and tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn and not src net 10.10.0.0/24'.
2. Which filter matches ICMP time-exceeded messages, and why might you capture them?
'icmp[icmptype] == icmp-timxceed' (type 11). They reveal traceroutes towards or through your network, and routing loops.
3. Why does the compiled program load byte 23 for IPv4 but byte 20 for IPv6?
The IPv4 protocol field is at offset 9 of the IP header (14 + 9 = 23); IPv6's next-header field is at offset 6 (14 + 6 = 20).
Test yourself
What does the BPF expression udp[10] & 0x80 != 0 select on port 53?
- DNS queries
- DNS responses (QR bit set)
- Truncated responses
- Queries over TCP
Why should tcpdump use -nn during analysis?
- To capture more packets
- To avoid name and port lookups, which are slow and send DNS queries about the addresses you analyse
- To print hex
- To rotate files
Why does the sandbox's tcpdump -i lo fail?
- Loopback has no packets
- Live capture needs root or CAP_NET_RAW, which the sandbox doesn't have
- tcpdump can't capture on loopback
- BPF is disabled in Linux
What does tcp[((tcp[12] & 0xf0) >> 2):4] refer to?
- The TCP checksum
- The first 4 bytes of the TCP payload
- The sequence number
- The source port