Wireshark Display Filters with tshark
Wireshark is the analyst's microscope: it dissects more than three thousand protocols and lets you narrow millions of packets down to the handful that matter. The narrowing is done with display filters, a small language over the dissectors' field names (ip.addr, tcp.flags.syn, dns.qry.name, http.request.method). The same language works in tshark, Wireshark's command-line twin, which is how this lesson can run real filters in the sandbox. Everything you learn here applies one to one in the GUI's filter bar.
The window you will use
Wireshark's main window has three panes. A schematic (a lab session, not a screenshot):
Clicking a field in the details pane shows its filter name in the status bar; right-click, "Apply as Filter" writes the expression for you. That is the fastest way to learn field names.
The language
| Construct | Example | Meaning |
|---|---|---|
| protocol present | dns, arp, tls |
packets containing that protocol |
| field present | http.set_cookie, tcp.options.mss |
packets where the field exists |
| comparison | ip.ttl < 10, tcp.dstport == 443, ip.src != 10.10.0.5 |
== != < <= > >= |
| either-side fields | ip.addr == 10.10.0.10, tcp.port == 80 |
matches source or destination |
| sets and ranges | tcp.dstport in {80, 443}, tcp.port in {8000..8100} |
membership |
| text | http.host contains "lab", dns.qry.name matches "^n.*\\.lab\\.testquot; |
substring, regular expression (PCRE2) |
| bytes and slices | eth.src[0:3] == 02:00:00, frame contains "password" |
byte ranges of a field, raw bytes of the frame |
| logic | && / and, || / or, ! / not, parentheses |
combine |
A trap with the "either-side" fields, and one that changed: since Wireshark 3.6, ip.addr != 10.10.0.10 means "no address equals 10.10.0.10", the same as !(ip.addr == 10.10.0.10). In older versions (and in many old guides) it meant "some address differs", which is true for a packet from 10.10.0.10 to someone else, so it excluded almost nothing. That old "any not equal" test is now written ip.addr !== 10.10.0.10 (or any_ne). !(ip.addr == 10.10.0.10) works in every version.
Filters on the sample captures
The session merges the four sample captures into one file with mergecap (they share the same lab clock, so packets interleave by time), prints its summary, and then runs filters an analyst would use.
mergecap -w all.pcap $SAMPLES/pcap/arp.pcap $SAMPLES/pcap/dns.pcap $SAMPLES/pcap/http.pcap $SAMPLES/pcap/tls.pcap
capinfos -c -u all.pcap | tail -2
echo "== 1. everything to or from the server except web traffic:"
tshark -r all.pcap -Y 'ip.addr == 10.10.0.10 && !(tcp.port == 80)'
echo "== 2. the word 'password' anywhere in a frame:"
tshark -r all.pcap -Y 'frame contains "password"'
echo "== 3. connection attempts and their targets:"
tshark -r all.pcap -Y 'tcp.flags.syn == 1 && tcp.flags.ack == 0' -T fields -e frame.number -e ip.dst -e tcp.dstport
echo "== 4. DNS names matching a pattern:"
tshark -r all.pcap -Y 'dns.qry.name matches "^n.*\\.lab\\.testquot;'
echo "== 5. web destinations by port:"
tshark -r all.pcap -Y 'tcp.dstport in {80, 443} && tcp.flags.syn == 1' -T fields -e tcp.dstport | sort | uniq -cNumber of packets: 32
Capture duration: 5.000000 seconds
== 1. everything to or from the server except web traffic:
1 0.000000 10.10.0.5 → 10.10.0.10 TCP 58 50420 → 443 [SYN] Seq=0 Win=64240 Len=0 MSS=1460
5 0.000250 10.10.0.10 → 10.10.0.5 TCP 58 443 → 50420 [SYN, ACK] Seq=0 Ack=1 Win=65160 Len=0 MSS=1460
8 0.000500 10.10.0.5 → 10.10.0.10 TCP 54 50420 → 443 [ACK] Seq=1 Ack=1 Win=502 Len=0
10 0.000750 10.10.0.5 → 10.10.0.10 TLSv1 268 Client Hello (SNI=lab.test)
12 0.001000 10.10.0.10 → 10.10.0.5 TCP 54 443 → 50420 [ACK] Seq=1 Ack=215 Win=509 Len=0
== 2. the word 'password' anywhere in a frame:
25 1.000750 10.10.0.5 → 10.10.0.10 HTTP 227 POST /login HTTP/1.1 (application/x-www-form-urlencoded)
== 3. connection attempts and their targets:
1 10.10.0.10 443
2 10.10.0.10 80
22 10.10.0.10 80
== 4. DNS names matching a pattern:
20 0.500000 10.10.0.5 → 10.10.0.53 DNS 73 Standard query 0x1a2c A nope.lab.test
21 0.501100 10.10.0.53 → 10.10.0.5 DNS 147 Standard query response 0x1a2c No such name A nope.lab.test SOA ns.lab.test
== 5. web destinations by port:
1 443
2 80
Statistics: the first five minutes with an unknown capture
Before filtering, get the shape of the traffic. The protocol hierarchy shows what is in the file; endpoints and conversations show who talks; per-protocol trees summarise requests.
mergecap -w all.pcap $SAMPLES/pcap/*.pcap
tshark -r all.pcap -q -z io,phs | sed -n '5,17p'
tshark -r all.pcap -q -z endpoints,ip | sed -n '4,8p'
eth frames:32 bytes:2605
ip frames:29 bytes:2479
tcp frames:25 bytes:2087
tls frames:1 bytes:268
http frames:4 bytes:715
data-text-lines frames:1 bytes:187
urlencoded-form frames:1 bytes:227
udp frames:4 bytes:392
dns frames:4 bytes:392
arp frames:3 bytes:126
text frames:1 bytes:42
===================================================================
| Packets | | Bytes | | Tx Packets | | Tx Bytes | | Rx Packets | | Rx Bytes |
10.10.0.5 29 2479 17 1433 12 1046
10.10.0.10 25 2087 10 799 15 1288
10.10.0.53 4 392 2 247 2 145
================================================================================
Filters every analyst keeps at hand
| Question | Display filter |
|---|---|
| cleartext logins | http.request.method == "POST" && urlencoded-form, ftp.request.command == "PASS", telnet, pop.request.command == "PASS" |
| failed connections | tcp.flags.reset == 1, icmp.type == 3 |
| scans | tcp.flags.syn == 1 && tcp.flags.ack == 0 then Statistics → Conversations |
| DNS problems | dns.flags.rcode != 0, dns.qry.name.len > 50 |
| TLS inventory | tls.handshake.type == 1 with columns for tls.handshake.extensions_server_name, tls.handshake.ja4 |
| ARP spoofing | arp.duplicate-address-detected, arp.opcode == 2 |
| retransmissions and loss | tcp.analysis.retransmission, tcp.analysis.flags |
| expert warnings | _ws.expert.severity >= warning |
Capture filters are a different language
The filter you type before a capture (or give to tcpdump) is a BPF capture filter: tcp port 80, host 10.10.0.10. It decides what is recorded at all, is applied in the kernel, and knows only header offsets, not protocol fields. Display filters run afterwards on everything recorded. Wireshark rejects one syntax in the other's box. The next lesson, tcpdump and BPF Capture Filters, covers capture filters.
Using it safely and well
- Capture legally: on your own systems or with written authorisation; captures contain other people's data and credentials. Store them encrypted and delete them when the case is closed.
- Work on copies, and record the capture's hash (
sha256sum) before analysis so you can show it wasn't changed (chain of custody, Security Foundations). - Dissectors have bugs: Wireshark has had many vulnerabilities in protocol dissectors. Analyse untrusted captures in a VM, keep Wireshark updated, and never run it as root (capture with
dumpcapas a member of thewiresharkgroup instead).
Pitfalls
- Copying
ip.addr != xfilters between Wireshark versions: before 3.6 it kept nearly every packet;!(ip.addr == x)means the same everywhere. - Forgetting that
containsis case-sensitive;matches "password"is case-insensitive by default (add(?-i)to make it case-sensitive). - Relying on port-based dissection: HTTP on port 8081 may show up as plain TCP until you use "Decode As" (
-d tcp.port==8081,httpin tshark), as the RTP example in SIP and RTP did.
Practice
1. Write a filter for DNS responses that say the name doesn't exist.
dns.flags.response == 1 && dns.flags.rcode == 3. In the merged file it matches one packet (the answer for nope.lab.test).
2. Why did filter 1 show the TLS connection but not the HTTP packets?
All packets involve 10.10.0.10; the !(tcp.port == 80) part removes the two HTTP connections, leaving the TLS connection to port 443. ARP and DNS packets don't contain 10.10.0.10 in an IP header at all.
3. Find packets with a TTL below 64 in the merged capture. How many are there and why?
ip.ttl < 64 matches none: every lab packet was sent with TTL 64 on the same subnet. On a real network such a filter separates local from routed traffic.
Test yourself
Which display filter excludes every packet to or from 10.10.0.10 in every Wireshark version?
- ip.addr !== 10.10.0.10
- !(ip.addr == 10.10.0.10)
- not host 10.10.0.10
- ip.dst != 10.10.0.10
What is the difference between a capture filter and a display filter?
- None
- A capture filter (BPF) decides what is recorded; a display filter selects among recorded packets using dissector fields
- Display filters are faster
- Capture filters work only in tshark
Which filter finds connection attempts?
- tcp.flags.syn == 1 && tcp.flags.ack == 0
- tcp.flags.fin == 1
- tcp.len > 0
- tcp.analysis.retransmission
Why should Wireshark not run as root?
- It can't decode as root
- Dissector bugs in a privileged process could compromise the system; capture with dumpcap instead
- Root can't open pcap files
- It disables display filters