Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 37 ยท Capturing Traffic

Capture Filters (BPF)

Learn Berkeley Packet Filter capture filter syntax: host, net, port, protocols, flags, and, or, not. See how capture filters differ from display filters.

A capture filter decides which packets are saved at all. It runs in the kernel before Wireshark sees anything, so packets that do not match are gone forever. That makes capture filters efficient and also dangerous: if your filter is wrong, the evidence you needed was never recorded. Use a capture filter when you must limit volume or privacy (a busy server, a long capture), and prefer capturing everything and filtering the display when you are unsure.

Capture filters use the BPF syntax shared with tcpdump and libpcap. This is not the display filter language: tcp port 80 is a capture filter, tcp.port == 80 is a display filter. Mixing them up is the most common beginner error.

Where to enter it

In the Capture Options dialog, type it in the Capture filter for selected interfaces box (it turns green when it compiles). On the command line: tshark -i eth0 -f "tcp port 80" -w web.pcapng or tcpdump -i eth0 'tcp port 80'. Always quote it in a shell.

Building blocks

A filter is made of primitives joined with and, or, not and parentheses (use quotes around parentheses in a shell).

Primitive Meaning
host 192.0.2.10 Source or destination is this host
src host 192.0.2.10 / dst host ... Only as source / destination
net 192.0.2.0/24 Any host in this network
port 53 Source or destination port is 53 (TCP or UDP)
src port 443 / dst port 443 Only one direction
portrange 6000-6010 A range of ports
tcp, udp, icmp, arp, ip, ip6 Protocol
ether host 00:00:5e:00:53:01 A MAC address
ether broadcast, ether multicast Link-layer broadcast / multicast
vlan 100 Frames tagged with VLAN 100
less 100, greater 1000 By length in bytes

A few useful combinations:

TEXT
host 192.0.2.10 and port 443          traffic of one host on HTTPS
tcp port 80 or tcp port 8080          web on two ports
not port 22                           everything except SSH (your own remote session!)
net 192.0.2.0/24 and not arp          a subnet, without ARP noise
udp portrange 5060-5061               SIP signalling
icmp or arp                           only "plumbing" protocols

If you capture on a server over SSH, add and not port 22 (or the real port). Otherwise your own session traffic is recorded, and the capture feeds on itself.

Looking inside headers: byte offsets

BPF can test any byte of a header with proto[offset:size]. Two classic examples:

TEXT
tcp[tcpflags] & tcp-syn != 0                       packets with the SYN flag set
tcp[tcpflags] & (tcp-syn|tcp-fin|tcp-rst) != 0     connection start and end packets
icmp[icmptype] == icmp-echo                        ping requests
ip[8] < 5                                          TTL below 5 (byte 8 of the IP header)

Capturing only the SYN packets gives you a very compact record of who connected to whom, which is perfect for a long-running connection log with almost no disk use.

Capture filters versus display filters

Capture filter Display filter
When it runs While capturing, in the kernel After capture, in Wireshark
Syntax BPF: tcp port 80, host 1.2.3.4 Wireshark: tcp.port == 80, ip.addr == 1.2.3.4
Understands Basic headers Thousands of protocol fields
Effect Packets are never saved Packets are only hidden; change the filter any time
Typical use Limit volume, disk and privacy Analysis

You can combine them: a broad capture filter to keep the file manageable, and precise display filters to analyze. See display filter basics.

A tiny BPF-style evaluator

Real BPF compiles to bytecode, but you can understand the logic with a small interpreter. This stand-in parses a simplified capture filter (host, port, tcp, udp, and, or, not, parentheses) and applies it to invented packets. It is a teaching model, not the real BPF engine.

Python A toy capture-filter evaluator (stand-in)

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.

Like real BPF, the toy treats two primitives next to each other (tcp port 443) as an implicit and.

Catch display syntax before you capture

A capture filter typed in display syntax fails or, worse, wastes a capture. This stand-in checks a filter string for the usual signs of display-filter syntax (dotted field names, ==, &&) and suggests the BPF form. It is a teaching helper with a few rules, not a validator.

Python Spot display-filter syntax in a capture filter (stand-in)

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.

Common mistakes

  • Using display syntax: ip.addr == 1.2.3.4 in the capture filter box is red. Write host 1.2.3.4.
  • Forgetting direction: port 80 matches both directions; add src or dst only when needed.
  • Filtering out your own proof: a too-narrow filter hides the cause of the problem (for example DNS failing before the HTTP request). When in doubt, capture broadly for a short time.
  • Assuming encapsulation: BPF port 80 does not see through a VLAN tag or VPN encapsulation unless you write vlan and port 80, and tunnels need their own filters.
  • IPv6: host name may match both IPv4 and IPv6; ip6 or ip narrow it down. Extension headers make byte-offset tests on IPv6 unreliable.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which one is a valid capture filter?

    Capture filters use BPF syntax like host and port. The others are display filter fields.
  2. 2. What happens to packets that do not match a capture filter?

    A capture filter drops non-matching packets before storage.
  3. 3. What does the filter 'tcp[tcpflags] & tcp-syn != 0' capture?

    It tests the flags byte of the TCP header for the SYN bit, so you get connection starts.
  4. 4. You capture on a remote server over SSH on port 22. What should you add to the capture filter?

    Excluding your own SSH session stops the capture from recording its own control traffic.

Summary

  • Capture filters use BPF syntax (host, net, port, tcp, and, or, not) and run in the kernel.
  • Non-matching packets are lost, so filter conservatively; filter more tightly when displaying.
  • Byte tests such as tcp[tcpflags] & tcp-syn != 0 capture specific header bits.
  • Capture and display filters have different syntaxes; do not mix them.
  • Exclude your own management traffic when capturing remotely.

Next: Wi-Fi, Monitor Mode and Special Captures