Skip to main content

TCPDump

Fundamentals

tcpdump is a CLI packet sniffer that can directly capture and interpret data frames from a file or network interface.

To capture traffic from "off the wire", it uses the libraries pcac and libpcap, paired with an interface in promiscuous mode to listen for data. This allows the program to see and capture packets sourcing from or destined for any device in the LAN.

Traffic captures

Basic capture options

Here are some basic Tcpdumps switches we can use to modify how our captures run and can be chained tohether:

Switch CommandResult
DWill display any interfaces available to capture from.
iSelects an interface to capture from. ex. -i eth0
nDo not resolve hostnames.
nnDo not resolve hostnames or well-known ports.
eWill grab the ethernet header along with upper-layer data.
XShow Contents of packets in hex and ASCII.
XXSame as X, but will also specify ethernet headers. (like using Xe)
v, vv, vvvIncrease the verbosity of output shown and saved.
cGrab a specific number of packets, then quit the program.
sDefines how much of a packet to grab.
Schange relative sequence numbers in the capture display to absolute sequence numbers. (13248765839 instead of 101)
qPrint less protocol information.
r file.pcapRead from a file.
w file.pcapWrite into a file.

Tcpdump output

Output example

  • Timestap: yellow.
  • Protocol: orange.
  • Source & destination IP: orange.
  • Flags: green.
  • Sequence & Acknowledgment numbers: red.
  • Protocol options: blue.
  • Notes / Next Header: white.

File I/O

Using -w will write our capture to a file: sudo tcpdump -i eth0 -w ~/output.pcap. Afterwards, we can read the output with: sudo tcpdump -r ~/output.pcap.

Packet filtering

FilterResult
hosthost will filter visible traffic to show anything involving the designated host. Bi-directional
src / destsrc and dest are modifiers. We can use them to designate a source or destination host or port.
netnet will show us any traffic sourcing from or destined to the network designated. It uses / notation.
protowill filter for a specific protocol type. (ether, TCP, UDP, and ICMP as examples)
portport is bi-directional. It will show any traffic with the specified port as the source or destination.
portrangeportrange allows us to specify a range of ports. (0-1024)
less / greater "< >"less and greater can be used to look for a packet or protocol option of a specific size.
and / &&and && can be used to concatenate two different filters together. for example, src host AND port.
oror allows for a match on either of two conditions. It does not have to meet both. It can be tricky.
notnot is a modifier saying anything but x. For example, not UDP.