task 2.2 · chapter 2 of 8: filters · 20 min
Capture filters
objective
Capture with a capture filter, so only DNS is ever saved, and write two capture filters of your own in BPF, the language capture filters use.
answers/2.2.ymlopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Capture | make capture AT=laptop:eth1 NAME=2.2 X=mixed F="udp port 53" |
|---|---|
| Try a capture filter on a file | tcpdump -r pcaps/mixed.pcapng 'udp port 53' |
| Your answers | answers/2.2.yml |
new to this? step by stepbeginner
make capture AT=laptop:eth1 NAME=2.2 X=mixed F="udp port 53"; open it; count the frames.- Challenge: write two BPF filters; try each with
tcpdump -r pcaps/mixed.pcapng '<filter>'. - Fill in answers/2.2.yml, then
make check T=2.2.
more in meet the lab
Deciding what gets saved at all
Two kinds of filter
A display filter (task 2.1) works on a capture you already have: it hides frames, and you can always clear it. A capture filter works while you capture: it decides which frames get written down in the first place. Whatever it rejects is never saved. There’s no clearing it afterwards.
Why would you ever want that? Because real networks are busy. Capture everything on a server’s uplink for an hour and you may have gigabytes, most of it irrelevant, and some of it data you’d rather not keep (other people’s traffic). A capture filter keeps the file small and keeps only what you’re allowed and need to see.
in one sentence A capture filter decides what’s saved at all; a display filter decides what’s shown. One is final, the other isn’t.
A different language
Capture filters aren’t written in Wireshark’s display filter language. They use BPF (the Berkeley Packet
Filter), the language of tcpdump and of the operating system’s capture code, which is simpler and much older:
| BPF capture filter | Display filter that means the same |
|---|---|
host 192.168.2.10 | ip.addr == 192.168.2.10 |
src host 192.168.1.20 | ip.src == 192.168.1.20 |
udp port 53 | udp.port == 53 |
tcp port 443 | tcp.port == 443 |
ether host aa:aa:aa:11:11:11 | eth.addr == aa:aa:aa:11:11:11 |
arp · not arp | arp · !arp |
host 192.168.2.10 and tcp port 80 | ip.addr == 192.168.2.10 && tcp.port == 80 |
BPF only knows about headers: addresses, ports, protocol numbers. It can’t say DNS; it can only say port 53, and trust that whatever uses port 53 is DNS. That’s the trade: fast and simple, but blind to what’s inside.
in one sentence Capture filters are BPF, tcpdump’s language: addresses, ports and protocols, and the words host, port, and, or, not.
Capture wide, filter narrow
The usual advice: unless you have to, capture with no filter, or a loose one, and narrow it down with display filters afterwards. You can’t add back what a capture filter threw away, and the clue you didn’t expect is usually in the traffic you’d have filtered out. In this lab files are tiny, so you’ll capture everything from here on. The capture filter is for when you can’t.
F= and tcpdump -r
make capture takes a capture filter as F=. The lab applies it with tcpdump, exactly as a filter on the
wire would, and only what passes is written to the file:
$ make capture AT=laptop:eth1 NAME=2.2 X=mixed F="udp port 53"
capturing on laptop:eth1, keeping only: udp port 53 → captures/2.2.pcapng
...
saved 10 frames to captures/2.2.pcapng
To test a capture filter without capturing, run tcpdump on a file you already have: -r reads it, and the
filter goes last, in quotes. It prints one line per packet that passes:
$ tcpdump -nn -r pcaps/mixed.pcapng 'host 192.168.2.10 and tcp port 80'
reading from file pcaps/mixed.pcapng, link-type EN10MB (Ethernet), snapshot length 262144
01:46:14.809114 IP 192.168.1.175.50124 > 192.168.2.10.80: Flags [S], seq 2744843428, win 64240, options [...], length 0
01:46:14.809350 IP 192.168.2.10.80 > 192.168.1.175.50124: Flags [S.], seq 3313393487, ack 2744843429, win 65160, ...
...
-nn keeps addresses and ports as numbers instead of looking up names. tcpdump comes with the dev container.
Keep only DNS
Capture the laptop’s mixed traffic, keeping only port 53:
run in the terminalmake capture AT=laptop:eth1 NAME=2.2 X=mixed F="udp port 53"The traffic had pings, ARP, HTTP and HTTPS in it too. None of it was saved.
Open
/captures/2.2.pcapngin Wireshark (ortshark -r captures/2.2.pcapng). Every frame is DNS. Count them.Fill in the follow-along line:
editanswers/2.2.ymlCtrl+P → 2.2.ymldns_frames: 10(Yours may differ by a frame or two. The checker counts the frames in your file.)
Two capture filters
Write two capture filters in BPF. Test each on the reference capture with tcpdump -nn -r pcaps/mixed.pcapng '...' until it keeps exactly what’s asked, then put them in answers/2.2.yml:
bpf_https_web: HTTPS (TCP port 443) to or from web,192.168.2.10;bpf_laptop_not_arp: every frame to or from the laptop’s network card, except ARP.
hint
Join conditions with and. A host match is host 192.168.2.10; a port is tcp port 443.
hint
The laptop’s IP address isn’t in ARP frames or DHCP’s first messages; its MAC address is in every frame it
sends or receives: ether host ....
show the solutionanswers/2.2.yml
or pull it into your lab: make solve T=2.2, then git diff --staged shows what changed
bpf_https_web: host 192.168.2.10 and tcp port 443
bpf_laptop_not_arp: ether host aa:aa:aa:11:11:11 and not arp
The checker runs both through tcpdump on pcaps/mixed.pcapng and compares the packets they keep with ours,
so tcp port 443 and host 192.168.2.10 passes as well. Order doesn’t matter in an and.
5 check: run it, then prove it
$ make check T=2.2when it's right, you'll see
╭─ task 2.2 · capture filters
│ ✓ captures/2.2.pcapng: taken on laptop:eth1, 10 frames
│ ✓ captures/2.2.pcapng holds only DNS: the capture filter kept nothing else (follow along)
│ ✓ how many frames the capture filter kept (follow along): ...
│ ✓ a capture filter: HTTPS to or from web (challenge): ...
│ ✓ a capture filter: the laptop's frames, but no ARP (challenge): ...
╰─ 5/5 checks passed verified