tcpdump (2/2)
Part 2 of tcpdump is all about the capture filter language (pcap-filter, BPF): protocol primitives, broadcast and direction, VLAN/tunnels, link-specific filters (Wi-Fi, pflog, ATM), byte accessors like tcp[tcpflags], and combining with and/or/not. The same syntax works in tshark -f and Wireshark capture filters. Lab: sniffer 192.168.143.10, web 192.168.143.20 (ports 80, 443, 8005).
123.1 Why is it used?
- Capture only what you need: smaller files, less CPU, less private data.
- Find specific events directly: only new connection attempts, only resets, only low-TTL packets.
- Work on busy links where an unfiltered capture would drop packets.
- Same filter string is reused in tcpdump, tshark -f, dumpcap and Wireshark.
123.2 How it works
- Step 1
1.ICMP reply from web goes through the Docker bridge
- Step 2
2.Frame reaches the sniffer's eth0
- Step 3
3.Kernel BPF program checks it against the filter 'icmp'
- Step 4
4.Match: a copy is handed to tcpdump in user space
- Step 5
5.TCP packet to port 8005 does not match: dropped right there
- Step 6
6.Only matching packets cost CPU, memory and disk
libpcap compiles the text filter into BPF bytecode; tcpdump -d shows it
- Step 1
1.web does not know the sniffer's MAC
- Step 2
2.web sends ARP request to ff:ff:ff:ff:ff:ff (ether broadcast)
- Step 3
3.Bridge forwards the broadcast to every device
- Step 4
4.web pings the sniffer: inbound packet for the sniffer
- Step 5
5.Sniffer's echo reply: outbound packet (inbound/outbound need -i any on Linux)
123.3 Protocol primitives
- icmp, tcp, udp, sctp, icmp6 are shortcuts; ip proto 17 matches by number (17 = UDP).
- ether proto \arp needs the backslash because arp is also a keyword; ether proto 0x88cc takes any EtherType.
- proto \tcp matches TCP in both IPv4 and IPv6; ip6 protochain follows the whole IPv6 header chain (slower).
- ah, esp (IPsec), pim; decnet, ipx, iso, isis are for legacy/OSI networks.
tcpdump -i eth0 -nn -t -c 4 icmptcpdump -i eth0 -nn -t -c 2 'ip proto 17'tcpdump -i eth0 -nn -t -c 2 'ether proto \arp'tcpdump -i eth0 -nn -t -c 4 'proto \tcp'$ tcpdump -i eth0 -nn -t -c 4 icmpIP 192.168.143.10 > 192.168.143.20: ICMP echo requestIP 192.168.143.20 > 192.168.143.10: ICMP echo replyIP 192.168.143.10 > 192.168.143.20: ICMP echo requestIP 192.168.143.20 > 192.168.143.10: ICMP echo reply4 packets captured$ tcpdump -i eth0 -nn -t -c 2 'ip proto 17'IP 192.168.143.10.38418 > 192.168.143.20.53: 52478+ [1au] A? example.com. (52)1 packet captured$ tcpdump -i eth0 -nn -t -c 2 'ether proto \arp'ARP, Request who-has 192.168.143.10 (ff:ff:ff:ff:ff:ff) tell 192.168.143.20, length 28ARP, Reply 192.168.143.10 is-at de:7e:b8:57:a8:fb2 packets captured$ tcpdump -i eth0 -nn -t -c 4 'proto \tcp'IP 192.168.143.10.58708 > 192.168.143.20.80: Flags [S]IP 192.168.143.20.80 > 192.168.143.10.58708: Flags [S.]IP 192.168.143.10.58708 > 192.168.143.20.80: Flags [.]IP 192.168.143.10.58708 > 192.168.143.20.80: Flags [P.] ... length 77: HTTP: GET / HTTP/1.1123.4 Broadcast, multicast and direction
- ether broadcast = destination MAC all ff; ip broadcast uses the netmask.
- ip multicast / ether multicast: the ping to 224.0.0.1 used MAC 01:00:5e:00:00:01.
- inbound / outbound work on the Linux cooked capture (-i any).
tcpdump -i eth0 -nn -t -e -c 1 ether broadcasttcpdump -i eth0 -nn -t -c 1 'ip multicast'tcpdump -i eth0 -nn -t -e -c 1 'ether multicast'tcpdump -i any -nn -t -c 2 'inbound and icmp'tcpdump -i any -nn -t -c 2 'outbound and icmp'$ tcpdump -i eth0 -nn -t -e -c 1 ether broadcast6e:5c:61:bb:93:24 > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42: Request who-has 192.168.143.10 (ff:ff:ff:ff:ff:ff) tell 192.168.143.20, length 281 packet captured$ tcpdump ... -c 1 'ip multicast'IP 192.168.143.10 > 224.0.0.1: ICMP echo request$ tcpdump ... -e -c 1 'ether multicast'de:7e:b8:57:a8:fb > 01:00:5e:00:00:01, ethertype IPv4 (0x0800), length 98: 192.168.143.10 > 224.0.0.1: ICMP echo request$ tcpdump -i any -nn -t -c 2 'inbound and icmp'listening on any, link-type LINUX_SLL2eth0 In IP 192.168.143.20 > 192.168.143.10: ICMP echo request, id 21546, seq 1, length 64eth0 In IP 192.168.143.20 > 192.168.143.10: ICMP echo request, id 21546, seq 2, length 64$ tcpdump -i any -nn -t -c 2 'outbound and icmp'eth0 Out IP 192.168.143.10 > 192.168.143.20: ICMP echo reply, id 21547, seq 1, length 64eth0 Out IP 192.168.143.10 > 192.168.143.20: ICMP echo reply, id 21547, seq 2, length 642 packets captured123.5 VLAN, MPLS and tunnels
- The Linux 8021q module was not loaded in the VM, so scapy wrote a pcap with VLAN 10 and VLAN 20 frames instead (nothing sent).
- 'vlan 10' matched only the VLAN 10 frame; 'vlan and icmp' matched both tagged frames.
- Plain 'icmp' matched only the untagged frame: the vlan keyword shifts offsets by 4 bytes, so on a trunk always start with vlan.
- vlan 100 && vlan 200 = QinQ inner tag; mpls labels, pppoed/pppoes, geneve (UDP 6081) work the same way.
# vlan.pcap written by scapy: Dot1Q(vlan=10), Dot1Q(vlan=20), 1 untagged frametcpdump -r vlan.pcap -nn -t -e 'vlan 10'tcpdump -r vlan.pcap -nn -t 'vlan and icmp'tcpdump -r vlan.pcap -nn -t 'icmp'tcpdump -i eth0 -d 'vlan 10'$ tcpdump -r vlan.pcap -nn -t -e 'vlan 10'02:42:c0:a8:8f:0a > 02:42:c0:a8:8f:14, ethertype 802.1Q (0x8100), length 46: vlan 10, p 0, ethertype IPv4 (0x0800), 192.168.10.5 > 192.168.10.1: ICMP echo request, id 0, seq 0$ tcpdump -r vlan.pcap -nn -t 'vlan and icmp'IP 192.168.10.5 > 192.168.10.1: ICMP echo requestIP 192.168.20.5 > 192.168.20.1: ICMP echo request$ tcpdump -r vlan.pcap -nn -t 'icmp'IP 192.168.143.10 > 192.168.143.20: ICMP echo request$ tcpdump -i eth0 -d 'vlan 10'(002) ldh [12](003) jeq #0x8100 jt 6 jf 4(004) jeq #0x88a8 jt 6 jf 5(005) jeq #0x9100 jt 6 jf 14(010) ldh [14](011) and #0xfff(012) jeq #0xa jt 13 jf 14(013) ret #262144123.6 Link-specific filters (need the right link type)
| Family | Examples | Needs |
|---|---|---|
| IEEE 802.11 (Wi-Fi) | type mgt subtype beacon, wlan addr2 MAC, dir tods | -I monitor mode + 802.11 link type |
| pf log | ifname em0, rulenum 5, action block, reason match | pflog0 on OpenBSD / FreeBSD |
| ATM | vpi 0, vci 5, oam, ilmic, lane | SunATM on Solaris (raw ATM) |
| LLC | llc, llc i|s|u, llc ui, llc xid | 802.2 LLC frames |
| link / radio accessors | radio[0], link[], ppp[] | Matching link header in the capture |
On the Ethernet eth0 these filters fail with real, clear errors:
$ tcpdump -i eth0 -d 'type mgt subtype beacon'!tcpdump: 802.11 link-layer types supported only on 802.11$ tcpdump -i eth0 -d 'ifname em0'!tcpdump: ifname supported only on PF linktype$ tcpdump -i eth0 -d 'vpi 0'!tcpdump: 'vpi' supported only on raw ATM$ tcpdump -i eth0 -d 'radio[0] = 0'!tcpdump: radio information not present in capture123.7 Packet data accessors: TCP flags
- proto[offset:size] reads bytes inside the packet; tcp[tcpflags] is byte 13 of the TCP header.
- 'tcp-syn != 0' matches SYN and SYN-ACK (both have the SYN bit).
- '(tcp-syn|tcp-ack) == tcp-syn' matches only new connection attempts.
- 'tcp[13] = 2' does the same by byte number; 'tcp-rst != 0' caught resets from closed ports 9999 and 9998.
tcpdump -i eth0 -nn -t -c 2 \ 'tcp[tcpflags] & tcp-syn != 0'tcpdump -i eth0 -nn -t -c 1 \ 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn'tcpdump -i eth0 -nn -t -c 1 'tcp[13] = 2'tcpdump -i eth0 -nn -t -c 2 \ 'tcp[tcpflags] & tcp-rst != 0'$ tcpdump ... 'tcp[tcpflags] & tcp-syn != 0'IP 192.168.143.10.58186 > 192.168.143.20.80: Flags [S], seq 2602840860, win 64240, ...IP 192.168.143.20.80 > 192.168.143.10.58186: Flags [S.], seq 2636841429, ack 2602840861, ...2 packets captured$ tcpdump ... '... (tcp-syn|tcp-ack) == tcp-syn'IP 192.168.143.10.36156 > 192.168.143.20.80: Flags [S], seq 1445275408, win 64240, ...1 packet captured$ tcpdump ... 'tcp[13] = 2'IP 192.168.143.10.36172 > 192.168.143.20.80: Flags [S], seq 3645014963, win 64240, ...1 packet captured$ tcpdump ... 'tcp[tcpflags] & tcp-rst != 0'IP 192.168.143.20.9999 > 192.168.143.10.40620: Flags [R.], seq 0, ack 309719883, win 0, length 0IP 192.168.143.20.9998 > 192.168.143.10.47200: Flags [R.], seq 0, ack 1326620323, win 0, length 02 packets captured123.8 Arithmetic expressions
- ip[8] is the TTL byte: 'ip[8] < 5' caught only the ping -t 3 packets.
- len and ip[2:2] (IP total length) both caught only the 1200-byte ping.
- icmp[icmptype] = icmp-echo / icmp-unreach separates request from unreachable.
- udp[2:2] = 53 reads the destination port; tcp[] / udp[] accessors work on IPv4 only.
- Operators: + - * / % & | ^ << >>; e.g. ip[0] & 0xf != 5 = IPv4 with options, ip[6:2] & 0x1fff = 0 = first fragment.
tcpdump -i eth0 -nn -t -v -c 2 'ip[8] < 5'tcpdump -i eth0 -nn -t -c 2 'len > 1000'tcpdump -i eth0 -nn -t -c 2 'ip[2:2] > 576'tcpdump -i eth0 -nn -t -c 1 \ 'icmp[icmptype] = icmp-unreach'tcpdump -i eth0 -nn -t -c 2 'udp[2:2] = 53'$ tcpdump -i eth0 -nn -t -v -c 2 'ip[8] < 5'(traffic) ping -c 1 192.168.143.20; ping -c 2 -i 0.3 -t 3 192.168.143.20IP (tos 0x0, ttl 3, id 53285, offset 0, flags [DF], proto ICMP (1), length 84) 192.168.143.10 > 192.168.143.20: ICMP echo requestIP (tos 0x0, ttl 3, id 53311, offset 0, flags [DF], proto ICMP (1), length 84)2 packets captured(traffic) ping -c 1 ...; ping -c 1 -s 1200 ...$ tcpdump ... -c 2 'len > 1000'IP 192.168.143.10 > 192.168.143.20: ICMP echo request, id 22302, seq 1, length 1208IP 192.168.143.20 > 192.168.143.10: ICMP echo reply, id 22302, seq 1, length 1208$ tcpdump ... -c 2 'ip[2:2] > 576'... ICMP echo request, id 22304, seq 1, length 1208... ICMP echo reply, id 22304, seq 1, length 1208$ tcpdump ... 'icmp[icmptype] = icmp-unreach'IP 192.168.143.20 > 192.168.143.10: ICMP 192.168.143.20 udp port 53 unreachable, length 88$ tcpdump ... 'udp[2:2] = 53'IP 192.168.143.10.46225 > 192.168.143.20.53: 22520+ [1au] A? example.com. (52)1 packet captured123.9 Combining primitives
- and (&&), or (||), not (!) with brackets; always wrap the filter in single quotes so the shell leaves the brackets alone.
- Only the SYNs for 80 and 443 matched, not 8005; portrange 8000-8010 matched only the 8005 SYN.
- 'not arp and not icmp and not port 80' left only the DNS query.
tcpdump -i eth0 -nn -t -c 2 \ 'host 192.168.143.20 and (port 80 or port 443) and tcp[tcpflags] == tcp-syn'tcpdump -i eth0 -nn -t -c 1 'tcp portrange 8000-8010 and tcp[tcpflags] == tcp-syn'tcpdump -i eth0 -nn -t -c 2 \ 'not arp and not icmp and not port 80'(traffic) curl http://192.168.143.20:8005/; curl http://192.168.143.20/; curl http://192.168.143.20:443/IP 192.168.143.10.35468 > 192.168.143.20.80: Flags [S], seq 3356351492, win 64240, ...IP 192.168.143.10.57188 > 192.168.143.20.443: Flags [S], seq 1482271425, win 64240, ...2 packets captured(traffic) curl :80, then curl :8005IP 192.168.143.10.45778 > 192.168.143.20.8005: Flags [S], seq 564571105, win 64240, ...1 packet captured(traffic) ping, curl :80, dig @192.168.143.20IP 192.168.143.10.57988 > 192.168.143.20.53: 53225+ [1au] A? example.com. (52)1 packet captured123.10 Compiled BPF: tcpdump -d
- icmp compiles to 6 instructions: check EtherType 0x800 at byte 12, then protocol 1 at byte 23.
- The SYN filter adds a fragment check (jset 0x1fff), computes the IP header length, then tests the SYN bit with jset #0x2.
tcpdump -i eth0 -d 'icmp'tcpdump -i eth0 -d \ 'tcp[tcpflags] & tcp-syn != 0'$ tcpdump -i eth0 -d 'icmp'(000) ldh [12](001) jeq #0x800 jt 2 jf 5(002) ldb [23](003) jeq #0x1 jt 4 jf 5(004) ret #262144(005) ret #0$ tcpdump -i eth0 -d 'tcp[tcpflags] & tcp-syn != 0'(000) ldh [12](001) jeq #0x800 jt 2 jf 10(002) ldb [23](003) jeq #0x6 jt 4 jf 10(004) ldh [20](005) jset #0x1fff jt 10 jf 6(006) ldxb 4*([14]&0xf)(007) ldb [x + 27](008) jset #0x2 jt 9 jf 10(009) ret #262144(010) ret #0123.11 Common mistakes
- Using 'tcp-syn != 0' for new connections: it also matches SYN-ACK.
- 'not host A and B' means 'not host A and host B'; to skip both write 'not (host A or host B)'.
- Plain 'icmp' on a VLAN trunk misses tagged frames; start with 'vlan and ...'.
- Forgetting quotes: the shell eats brackets and | characters.
- Using Wi-Fi / pflog / ATM filters on an Ethernet interface.
123.12 Interview tips
- Capture filters are fast because libpcap compiles them to BPF bytecode that runs in the kernel; non-matching packets are never copied to user space.
- Only new TCP connections: 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn'.
- 'not' has the highest precedence; use brackets.
- tcpdump -d is the way to debug and prove what a filter actually checks.