Skip to content

Cheatsheet · Infrastructure and AD pentesting

tcpdump and Wireshark: packet capture for diagnostics during a test

tcpdump and Wireshark to diagnose an authorised test: BPF capture filters, pcap files, rotation, display filters, streams and data protection.

By Steven Carrier Published

Official website: tcpdump

Capturing traffic helps you understand why a tool gets no reply, where a connection comes from, or what a firewall really lets through. tcpdump captures on the command line, Wireshark analyses (and can also capture). A capture contains everything that passes: record only the traffic of your scope, agreed using the penetration test scoping checklist, on the test workstation or on a device the client has authorised you to listen on. For the scans that generate the traffic, see the Nmap cheatsheet.

Data-handling precautions

  • A capture can contain credentials, cookies, e-mails and personal data. It falls under the same confidentiality rules as the rest of the engagement evidence.
  • Limit the volume: a targeted capture filter, -s to keep only headers if the content is not needed, a short duration.
  • Encrypt .pcap files at rest, avoid uploading them to online services, and delete them at the date agreed with the client.
  • Never listen to the traffic of a shared network that is outside the scope: unauthorised interception can be an offence.
  • In a report, redact any secrets present in excerpts.

Capture with tcpdump

# List the available interfaces
tcpdump -D

# Capture on an interface, without name resolution (-nn: neither hosts nor ports)
sudo tcpdump -i eth0 -nn

# Stop after 200 packets
sudo tcpdump -i eth0 -nn -c 200

# Show link-layer headers (MAC addresses)
sudo tcpdump -i eth0 -nn -e

# Show content in hexadecimal and ASCII (confidential: use with care)
sudo tcpdump -i eth0 -nn -X -c 20

Without -n, tcpdump queries DNS for every address, which slows the display and generates extra traffic. Depending on the system, the interface may be called eth0, ens160, en0, or any for all interfaces (Linux).

Capture filters (BPF)

The filter goes at the end of the command and applies before recording: it reduces the captured volume. Put it in single quotes.

# One host, in either direction, or only as source
sudo tcpdump -i eth0 -nn 'host 10.0.0.12'
sudo tcpdump -i eth0 -nn 'src host 10.0.0.12'

# A network, a port, a port range
sudo tcpdump -i eth0 -nn 'net 10.0.0.0/24'
sudo tcpdump -i eth0 -nn 'tcp port 443'
sudo tcpdump -i eth0 -nn 'portrange 8000-8100'

# Combinations with and, or, not
sudo tcpdump -i eth0 -nn 'host 10.0.0.12 and (tcp port 80 or tcp port 443)'
sudo tcpdump -i eth0 -nn 'not port 22'

# Common protocols
sudo tcpdump -i eth0 -nn 'udp port 53'
sudo tcpdump -i eth0 -nn 'icmp'
sudo tcpdump -i eth0 -nn 'arp'

# Start of a TCP connection: SYN packets without ACK
sudo tcpdump -i eth0 -nn 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn'

Always exclude your own administration session (not port 22): otherwise a remote capture captures its own connection and drowns in it.

Write, read and rotate pcap files

# Write to a file (no display), keeping whole packets
sudo tcpdump -i eth0 -nn -s 0 -w capture.pcap 'host 10.0.0.12'

# Read a file back, optionally with a filter
tcpdump -nn -r capture.pcap
tcpdump -nn -r capture.pcap 'tcp port 443'

# Rotation by size: 100 MB files (-C in millions of bytes), at most 10 files (-W)
sudo tcpdump -i eth0 -nn -w capture.pcap -C 100 -W 10

# Rotation by time: a new file every hour, named by date
sudo tcpdump -i eth0 -nn -w 'capture-%Y%m%d-%H%M%S.pcap' -G 3600

# Drop root privileges after opening the interface
sudo tcpdump -i eth0 -nn -Z nobody -w capture.pcap

Rotation avoids filling a disk and limits how much data you keep. Rotation and file-name options depend on the version: check with tcpdump -h or man tcpdump.

Wireshark display filters

Wireshark distinguishes two languages: capture filters (BPF syntax, above) and display filters, which are richer and typed in the bar above the packet list.

Need Display filter
One host ip.addr == 10.0.0.12
A TCP port tcp.port == 443
HTTP requests of one method http.request.method == "POST"
HTTP server-error responses http.response.code >= 500
DNS queries for a name dns.qry.name contains "target.example"
Start of a TLS negotiation (ClientHello) tls.handshake.type == 1
Name sent in the ClientHello (SNI) tls.handshake.extensions_server_name
Start of a TCP connection (SYN) tcp.flags.syn == 1 && tcp.flags.ack == 0
TCP retransmissions and anomalies tcp.analysis.retransmission or tcp.analysis.flags
Connection resets tcp.flags.reset == 1
ICMP or ARP icmp or arp
Exclude noise `!(arp

The filter bar turns green if the syntax is correct, red otherwise. Right-clicking a field, then "Apply as Filter", builds the filter without memorising it. In an HTTPS capture, only the TLS handshake is readable (with TLS 1.3, even the server certificate is encrypted): the application content stays encrypted.

Follow a stream and read the statistics

  • Follow a stream: right-click a packet, "Follow" then "TCP Stream" (or Ctrl+Alt+Shift+T). Wireshark rebuilds the conversation; the tcp.stream eq N filter is applied at the same time. Close the window to return to the filtered list.
  • Statistics: "Statistics" menu. "Conversations" lists exchanges by host pair, "Endpoints" by address, "Protocol Hierarchy" gives the protocol breakdown, "I/O Graphs" plots throughput over time.
  • Export objects: "File" then "Export Objects" then "HTTP" (or SMB, TFTP…) extracts files transferred in clear. Treat those files as client data: store them in an encrypted folder.

Command-line alternative: tshark

# Apply a display filter to a file
tshark -r capture.pcap -Y 'http.response.code >= 500'

# TCP conversation statistics, without printing packets
tshark -r capture.pcap -q -z conv,tcp

# Protocol breakdown
tshark -r capture.pcap -q -z io,phs

tshark ships with Wireshark and reuses its display filters: handy to sort a large capture on a server without a graphical interface. Then open only the useful excerpt (-w excerpt.pcap) in Wireshark rather than the full file.

Sources

Related resources

  • Methodology · Infrastructure and AD pentesting · Red teaming

    Active Directory config review

    Defensive Active Directory review checklist for audits: privileged accounts, krbtgt, delegation, LAPS, legacy protocols, GPOs, ADCS and logging.

    Published

  • Cheatsheet · Infrastructure and AD pentesting · Red teaming

    Active Directory enumeration

    Authenticated AD enumeration for an internal test: domain, accounts, groups, password policy, shares, LDAP queries and BloodHound collection.

    Published

  • AI prompt · Infrastructure and AD pentesting · OSINT · Cloud

    Attack surface prompt

    Prompt that analyses an asset inventory from the authorised scope (domains, IPs, services, cloud) to prioritise testing: exposure, criticality, checks.

    Published

BEAROPS

Need a penetration test?

BEAROPS experts test your applications, infrastructure and cloud, black box or grey box, following OWASP, NIST and ANSSI guidance.