The tcpdump application is the frontend to Wireshark and other network sniffer programs. The GUI interface supports many of the options we'll describe shortly.
This application's default behavior is to display every packet seen on the primary Ethernet link. The format of a packet report is as follows:
TIMESTAMP SRC_IP:PORT > DEST_IP:PORT: NAME1 VALUE1, NAME2 VALUE2,...
The name-value pairs include:
- Flags: The flags associated with this packet are as follows:
-
- The term S stands for SYN (Start Connection)
- The term F stands for FIN (Finish Connection)
- The term P stands for PUSH (Push data)
- The term R stands for RST (Reset Connection)
- The period . means there are no flags
- seq: This refers to the sequence number of the packet. It will be echoed in an ACK to identify the packet being acknowledged.
- ack: This refers to the acknowledgement that indicates a packet is received. The value is the sequence number from a previous packet.
- win: This indicates the size of the buffer at the destination.
- options: This refers to the TCP options defined for this packet. It is reported as a comma-separated set of key-value pairs.
The following output shows requests from a Windows computer to the SAMBA server intermingled with a DNS request. The intermingling of different packets from different sources and applications makes it difficult to track a specific application or traffic on a given host. However, the tcpdump command has flags that make our life easier:
$ tcpdump 22:00:25.269277 IP 192.168.1.40.49182 > 192.168.1.2.microsoft-ds: Flags [P.], seq 3265172834:3265172954, ack 850195805, win 257, length 120SMB PACKET: SMBtrans2 (REQUEST) 22:00:25.269417 IP 192.168.1.44.33150 > 192.168.1.7.domain: 13394+ PTR? 2.1.168.192.in-addr.arpa. (42) 22:00:25.269917 IP 192.168.1.2.microsoft-ds > 192.168.1.40.49182: Flags [.], ack 120, win 1298, length 0 22:00:25.269927 IP 192.168.1.2.microsoft-ds > 192.168.1.40.49182: Flags [P.], seq 1:105, ack 120, win 1298, length 104SMB PACKET: SMBtrans2 (REPLY)
The -w flag sends the tcpdump output to a file instead of the terminal. The output format is in binary form, which can be read with the -r flag. Sniffing packets must be done with root privileges, but displaying the results from a previously saved file can be done as a normal user.
By default, tcpdump runs and collects data until it is killed using Ctrl-C or SIGTERM. The -c flag limits the number of packets:
# tcpdump -w /tmp/tcpdump.raw -c 50 tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 50 packets captured 50 packets received by filter 0 packets dropped by kernel
As a rule, we want to examine the activity on a single host, perhaps a single application.
The last values of the tcpdump command line form an expression that helps us filter packets. The expression is a set of key-value pairs with modifiers and Boolean operators. The next recipes demonstrate using filters.