What does a tcpdump filter builder do?
tcpdump shows the packets that cross a network interface. On a busy server that is far too much, so you give it a capture filter written in BPF, the Berkeley Packet Filter language of libpcap. A good filter keeps the output readable and the CPU load low, because packets that do not match are dropped early. The syntax is compact and easy to forget: src net, portrange, tcp[tcpflags], and the rule that and and or have the same precedence.
This tool builds the filter from a list of conditions and then wraps it in a complete command with the capture options. Each condition can be a host, a network, a port, a port range, a protocol, TCP flags, packet size, a MAC address, a VLAN, broadcast or multicast, or your own expression. Join them with and, or and not. The result is a command you can paste into a terminal, and the bare filter, which also works in other tools that take a libpcap capture filter.
Everything is built in your browser, so the addresses and ports you type are not uploaded. The syntax follows the pcap-filter manual, and the tool's tests compile its filters with a real tcpdump. To work out a CIDR block for a network condition, use the IPv4 subnet calculator; to look up a port, use the port numbers list.
How to build a tcpdump command and BPF filter
- In What to capture, start from a preset if one is close to what you need: Web (80 and 443), DNS, new connections (SYN), resets (RST), ping, DHCP, ARP, everything except SSH, or one host over HTTPS. The presets fill in the conditions, which you can then change.
- Add conditions with Add a condition. For each, pick what to Match: host, network, port, port range, protocol, TCP flags, packet size, MAC address, VLAN, broadcast or multicast, or your own expression.
- Set the direction (either, source or destination) and the transport (any, TCP, UDP or SCTP) where they apply. Tick not to negate a condition.
- From the second condition on, choose how to join it with the one before: and, or. The first one is just Capture.
- In How to capture, set the interface (the default is
any), whether to show names, the detail level, packet contents, timestamps, bytes per packet, a packet count, and an output file with optional rotation. - Copy the Command, or only the Filter. Run it with
sudo, since opening an interface needs privileges.
An empty list gives an empty filter, and tcpdump then captures everything. Conditions that are not valid are skipped and flagged with a message, so check the filter before relying on it.
Because and and or have the same precedence in a capture filter, the builder adds parentheses when the operator changes. A filter built as "port 80, or port 443, and host 10.0.0.1" is written (port 80 or port 443) and host 10.0.0.1, so it reads left to right the way you built it.
Examples
Web traffic on the default capture
The Web preset with the default options gives:
sudo tcpdump -i any -nn -s 0 'tcp port 80 or tcp port 443'-nn shows numbers instead of host and port names, and -s 0 keeps the whole packet. The filter is quoted so the shell does not touch it.
New connection attempts only
The SYN preset looks for packets with SYN set and ACK clear, which are the first packet of each TCP connection:
tcp[tcpflags] & (tcp-syn|tcp-ack) = tcp-synThe RST preset is tcp[tcpflags] & tcp-rst = tcp-rst. If you also tick IPv6, the builder adds a second test for IPv6 packets without extension headers.
Everything except SSH
Match port 22 over TCP and tick not:
not tcp port 22This is handy when you are connected by SSH and the capture would otherwise show your own session.
One host, with the order made explicit
Join host 10.0.0.1 with port 80, then with port 443 using or. Left to right, this is:
(host 10.0.0.1 and port 80) or port 443If you meant "this host, and port 80 or 443", build it as port 80, port 443, then host, and you get (port 80 or port 443) and host 10.0.0.1.
A rotating capture file
With interface eth0, names off with -n, very verbose, hex and ASCII dump, link-level headers, short lines, date timestamps, line buffered, not promiscuous, incoming only, 96 bytes per packet, 50 packets, file out.pcap rotated every 100 million bytes and every 3600 seconds, keeping 5 files, the command is:
tcpdump -i eth0 -n -vv -X -e -q -tttt -l -p -Q in -s 96 -c 50 -w out.pcap -C 100 -G 3600 -W 5The rotation flags are written only when a file is set.
Common errors and how to fix them
- Fill in a valid value: the condition is empty or wrong. Write an IP address or name, a CIDR block, a port number or name, or a protocol from the list.
- A port range looks like 8000-8100: write the lower port first, and keep both within 65535.
- A MAC address looks like aa:bb:cc:dd:ee:ff: six pairs separated by colons.
- Choose at least one flag: a TCP flags condition needs a flag that must be set or clear.
- The size is a whole number of bytes from 0 to 65535.
- Write a pcap-filter expression without special characters: own expressions cannot contain
;, a backtick,$,|,&,<or>. - The interface is a name like eth0 or any, up to 15 characters.
- The file name cannot contain spaces: use letters, digits and
@ % + = : , . / - _. - The numbers must be whole and zero or more.
Frequently asked questions
What is the difference between a capture filter and a display filter?
A capture filter (BPF) decides which packets are kept while capturing. A display filter, as used in Wireshark, hides packets after they are captured and has a different syntax. This tool writes capture filters.
Why does tcpdump need sudo?
Opening a network interface for capture needs elevated privileges. Run the command with sudo, or give the binary the capture capability.
How do I read the saved file?
Open the .pcap file with tcpdump -r out.pcap, or in a packet analyser.
Does the filter work on a DHCP or ARP exchange?
Yes. Use the DHCP preset (udp port 67 or udp port 68) or the ARP preset (arp). To check what an access point receives, see the DHCP option 43 generator.
Is anything uploaded?
No. The command is built in your browser.
How to run the result
Bash
sudo tcpdump -i any -nn -s 0 'tcp port 80 or tcp port 443'
sudo tcpdump -nn -r out.pcap 'host 10.0.0.1'
sudo tcpdump -i eth0 -c 100 -w sample.pcap 'udp port 53'