LLI Wiki — My Digital Knowledge Base

tcpdump cheat sheet: filters and remote capture to Wireshark

tcpdump options, filters and examples, capture to file, and streaming a remote SSH capture into Wireshark.

A practical tcpdump cheat sheet: common options, filters, capture-to-file examples, and how to stream a remote capture into Wireshark. Based on the well-known tcpdump primer by Daniel Miessler, rewritten and extended with field examples.

Common options

Option Effect
-i any Listen on all interfaces (useful to see whether there is any traffic)
-n Do not resolve hostnames
-nn Do not resolve hostnames or port names
-X Show packet contents in hex and ASCII
-XX Same as -X, plus the Ethernet header
-v, -vv, -vvv Increase the amount of packet information
-c <n> Stop after n packets
-s <n> Snapshot length in bytes; -s0 captures whole packets
-S Print absolute sequence numbers
-e Print the Ethernet header
-q Show less protocol information
-E Decrypt IPsec traffic with a given key
-w <file> Write the capture to a file
-r <file> Read a capture file

TCP flags in the output

Flag Meaning
[S] SYN (start connection)
[.] no flag set (ACK only)
[S.] SYN-ACK
[P] PSH (push data)
[F] FIN (finish connection)
[R] RST (reset connection)

Filters

Traffic to and from a host:

tcpdump -nvvv -i any -c 3 host 192.0.2.10

Only traffic whose source is a host:

tcpdump -nvvv -i any -c 3 src host 192.0.2.10

Source and destination ports:

tcpdump -nvvv -i any -c 3 port 22 and port 60738

One port or another:

tcpdump -nvvv -i any -c 20 'port 80 or port 443'

Two ports and a specific host:

tcpdump -nvvv -i any -c 20 '(port 80 or port 443) and host 192.0.2.11'

Other building blocks:

tcpdump src 192.0.2.17                 # source only
tcpdump dst 192.0.2.18                 # destination only
tcpdump net 192.0.2.0/24               # a whole network (CIDR)
tcpdump icmp                           # protocol: tcp, udp, icmp
tcpdump port 3389                      # one port
tcpdump src port 1025                  # source port
tcpdump dst port 389                   # destination port
tcpdump src port 1025 and tcp          # combine port and protocol
tcpdump udp and src port 53
tcpdump portrange 21-23                # port range
tcpdump less 32                        # packets smaller than 32 bytes
tcpdump greater 128                    # packets larger than 128 bytes
tcpdump '> 32'                         # same, with symbols
tcpdump '<= 128'

Logical operators: and or &&, or or ||, not or !.

Verbosity presets:

tcpdump -nS                 # basic communication
tcpdump -nnvvS              # very verbose, no name resolution
tcpdump -nnvvXS             # adds the payload
tcpdump -nnvvXSs 1514       # whole packet (snaplength 1514)

Combined examples:

# TCP traffic from one host to port 3389
tcpdump -nnvvS tcp and src 192.0.2.24 and dst port 3389

# Everything from a host except SSH, resets only
tcpdump "host 192.0.2.14 and not (port 22 or port 80) and tcp[tcpflags] == tcp-rst"

# Everything from a host except SSH
tcpdump "host 192.0.2.14 and not port 22"

# Kerberos
tcpdump -i eth0 '(tcp port 88 or udp port 88)'

# DHCP (with Ethernet header)
sudo tcpdump -i ens192 port 67 or port 68 -e -n -vv

# ARP
tcpdump -nei eth2 arp -s0

# SNMP to or from a host
sudo tcpdump -i ens192 "host 192.0.2.15"

The service name to port mapping is in /etc/services:

grep domain /etc/services

Capture to a file and read it back

# Capture traffic for a host and port into a file
tcpdump -lnni eth0 -w dump -s 65535 host web01 and port 80

# Capture everything except SSH (keeps your own session out of the capture)
sudo tcpdump -i eth0 -s 65535 not port 22 -w capture.out

# Capture a host and port
sudo tcpdump -i eth0 -w capture.out -s 65535 host 192.0.2.12 and port 80

# Capture HTTP traffic with a large snaplength
tcpdump -s 1514 port 80 -w capture_file

# Read a capture
sudo tcpdump -i eth0 -r capture.out
tcpdump -r capture.out -nnvvvSe
tcpdump -r capture.out -nnvvvSeXX

To watch a capture while saving it:

tcpdump -i eth0 port 389 -w - -U | tee ldapQuery.dump | tcpdump -r -

Stream a remote capture into Wireshark

From a Linux or macOS workstation, using SSH and a named pipe:

mkfifo /tmp/board
wireshark -k -i /tmp/board &
ssh user@sqlserver01 "tcpdump -s 0 -U -n -w - -i lo not port 22" > /tmp/board

Or directly through a pipe:

ssh user@sqlserver01 "sudo /usr/sbin/tcpdump -s0 -w - 'not port 22'" | wireshark -k -i -

From Windows, using PuTTY's plink:

plink.exe -ssh user@sqlserver01 -pw <password> -no-antispoof "sudo tcpdump -i ens192 -U -s 0 -w - 'not port 22'" | "C:\Program Files\Wireshark\Wireshark.exe" -k -i -

With dumpcap (no sudo needed when the user may capture), with and without a capture filter:

plink -batch -ssh -pw <password> user@sqlserver01 "dumpcap -i ens192 -P -w -" | "C:\Program Files\Wireshark\Wireshark.exe" -k -i -
plink -batch -ssh -pw <password> user@sqlserver01 "dumpcap -i ens192 -f \"not port 22\" -P -w -" | "C:\Program Files\Wireshark\Wireshark.exe" -k -i -

Useful Wireshark display filter to hide your own remote-desktop session and a few known hosts:

not tcp.port in {3389} and not udp.port in {3389} and (not ip.addr == 192.0.2.20 and not ip.addr == 192.0.2.21)

Notes

Mastodon LLIMastodon SocialFosstodon