☼
⌃
☰

PCAP

Table of Contents

Related: Wireshark Tshark Network Packet DFIR

PCAP (Packet CAPture) is a common file format used to store network packet data captured from a network interface. It's the standard format used by many network analysis tools like Wireshark and tcpdump. Understanding PCAP is fundamental for network analysis, security investigations, and troubleshooting.

Core Concepts

  • Packet: The fundamental unit of data transmitted over a network. It consists of a header containing source and destination information, protocol details, and the actual data (payload).
  • Packet Capture: The process of intercepting and recording network packets as they travel across a network interface. This can be done using software tools.
  • PCAP File: A file that stores a sequence of captured network packets, along with timestamps for each packet.
  • Libpcap/WinPcap: Cross-platform libraries that provide an API for capturing network traffic. Most packet capture tools rely on these libraries.
  • Header Information: Each packet in a PCAP file typically includes:
    • Timestamp: The time when the packet was captured.
    • Capture Length: The number of bytes of the packet that were captured.
    • Original Length: The total size of the packet on the network (may be truncated during capture).
    • Packet Data: The raw bytes of the network packet, including headers (e.g., IP, TCP, UDP) and the application data.

How PCAP Capture Works

  1. Interface Selection: The user specifies the network interface to capture traffic from (e.g., eth0, wlan0, en0).
  2. Promiscuous Mode (Often): The network interface is often put into promiscuous mode, which allows it to capture all traffic passing through the network segment, not just traffic addressed to or from the capturing host.
  3. Packet Interception: The Operating System's network stack, through libpcap/WinPcap, intercepts packets at the data link layer.
  4. Buffering: Captured packets are often buffered in memory before being written to the PCAP file.
  5. File Storage: The buffered packets are written to a file with a .pcap or .cap extension.

Common Tools for Working with PCAP Files

  • Wireshark: A powerful and widely used GUI-based network protocol analyzer that can capture and analyze PCAP files in detail.
  • tcpdump: A command-line packet analyzer that can capture and display packet details. Often used for live capture and filtering.
  • Tshark: The command-line version of Wireshark, useful for scripting and automation.
  • NetworkMiner: A network forensic analysis tool that parses PCAP files (or live traffic) to extract higher-level artifacts like files, images, and credentials.
  • Scapy: A Python library for packet manipulation and analysis, capable of reading and writing PCAP files.

Capturing and Reading PCAP Files

SHELL
# tcpdump — capture live traffic to a file
tcpdump -i eth0 -w capture.pcap

# tcpdump — capture with a filter (only port 80 traffic)
tcpdump -i eth0 -w capture.pcap port 80

# tcpdump — read back a saved capture
tcpdump -r capture.pcap

# tshark — capture live traffic
tshark -i eth0 -w capture.pcap

# tshark — read a file with a display filter
tshark -r capture.pcap -Y "http.request"

# tshark — follow a TCP stream (useful for reconstructing a session)
tshark -r capture.pcap -q -z follow,tcp,ascii,0

Capture Filters vs Display Filters

A common point of confusion, worth being explicit about:

  • Capture filters (tcpdump/tshark -i with a filter, or Wireshark's capture filter field) use BPF syntax and are applied while capturing — traffic that doesn't match is never written to the file at all, so filtering here is permanent and can't be undone later.
  • Display filters (Wireshark's filter bar, tshark's -Y flag) are applied after capture, against a file that already contains everything — filtering here is non-destructive and can be changed or removed at any time.

When in doubt during an active investigation, capture broadly with minimal capture filters and narrow down with display filters afterward — a too-aggressive capture filter can silently discard the packet you actually needed.

Use Cases

  • Network Troubleshooting: Analyzing network communication to identify connectivity issues, latency problems, or errors.
  • Security Analysis: Investigating security incidents, detecting malicious traffic, and analyzing attack patterns.
  • Malware Analysis: Examining the network behavior of malware.
  • Protocol Analysis: Studying the implementation and behavior of network protocols.
  • Performance Monitoring: Understanding network traffic patterns and bandwidth utilization.
  • DFIR: Collecting and analyzing network evidence for legal or investigative purposes.

Key Considerations

  • Capture Size and Duration: Capturing too much traffic for too long can result in very large PCAP files, making analysis difficult. Use filters during capture when possible.
  • Privacy Concerns: PCAP files can contain sensitive information. Handle them with appropriate security measures and respect privacy regulations.
  • Capture Location: The location of the capture (e.g., on a host, at a network tap, on a Switch SPAN port) can significantly impact the traffic observed.
  • Filtering: Using capture filters (e.g., in tcpdump or Wireshark) can help focus on specific traffic of interest and reduce the size of the PCAP file.
  • Truncation: During capture, packets might be truncated to limit file size. Be aware of the "capture length" and "original length" fields.
  • Analysis Techniques: Analyzing PCAP files often involves filtering by IP Address, port number, protocol, or specific content to identify relevant traffic. Following TCP streams is crucial for understanding conversations.

Tips

  • Always capture broadly and filter for display, not the other way around — a capture filter that's too narrow can't be un-applied after the fact, since the discarded packets are simply gone
  • Following a TCP stream (Wireshark: right-click → Follow → TCP Stream; tshark: -z follow,tcp,ascii,STREAM_INDEX) is usually faster than reading packet-by-packet when trying to reconstruct what actually happened in a single conversation
  • For large PCAP files, tshark's scriptability makes it the better choice over Wireshark's GUI when you need to extract something specific (e.g. all HTTP requests, all DNS queries) rather than browse interactively