Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Getting Started

How Capturing Works: libpcap, Npcap and Capture Files

See how packets travel from the network card to Wireshark: promiscuous mode, libpcap and Npcap, BPF filters, snapshot length, and the pcap and pcapng file formats.

Wireshark does not talk to the network card directly. A chain of components copies each packet from the hardware into the program, and knowing this chain explains many "why can't I see...?" questions. It also explains why capture files look the way they do, and how to read them with your own code.

From wire to screen

  1. The network card (NIC) receives frames. Normally it keeps only frames addressed to its own MAC address, to broadcast, or to multicast groups it joined.
  2. The driver hands accepted frames to the operating system.
  3. A capture library gets a copy: libpcap on Linux and macOS, Npcap on Windows. On Linux, the kernel uses AF_PACKET sockets; on BSD and macOS it uses BPF devices; on Windows, Npcap installs an NDIS filter driver.
  4. A small program, dumpcap, reads from the library and writes the packets to memory or a file. Wireshark and tshark start dumpcap as a child process, which is why only dumpcap needs special rights.
  5. The GUI reads the stream, dissects the packets and draws them.
flowchart LR
    W["Wire / Wi-Fi"] --> N["NIC + driver"]
    N --> K["Kernel"]
    K --> L["libpcap / Npcap<br/>BPF filter runs here"]
    L --> D["dumpcap"]
    D --> G["Wireshark GUI or tshark"]
    D --> FILE[".pcapng file"]

The capture filter runs inside the kernel path, before the packet is copied to user space, which is why capture filters are cheap.

Promiscuous mode

In promiscuous mode the NIC passes up every frame it sees on the medium, not just those addressed to it. Wireshark turns it on by default. It matters on shared media (old hubs, some wireless setups) and on mirrored ports. On a normal switch port it changes little, because the switch only sends you your own traffic. Virtual machines and some adapters ignore or restrict it. Wi-Fi is special: see Wi-Fi and monitor mode.

Snapshot length

The snaplen is the maximum number of bytes saved per packet. Modern defaults are 262144 (effectively everything). A small snaplen such as 96 keeps only headers, which saves disk and also avoids storing private payloads, but you can no longer read application data. If a packet is cut, Wireshark shows Frame ... captured length < frame length and the Packet Bytes pane ends early.

The BPF capture filter

The Berkeley Packet Filter is a tiny virtual machine in the kernel. A capture filter such as tcp port 80 compiles to BPF instructions, and packets that do not match never leave the kernel. Capture filters use a different, older syntax than display filters, covered in capture filters.

Capture file formats

Wireshark reads dozens of formats but saves two:

Format Extension Notes
pcap (libpcap) .pcap The classic format. One link type per file, fixed 24-byte global header, simple.
pcapng (next generation) .pcapng The default in recent Wireshark. Blocks with options: several interfaces, comments, interface names, packet comments, name resolution records, decryption secrets, capture statistics.

Use pcapng when you want comments and multiple interfaces, and pcap when a tool you rely on does not understand pcapng. You can convert in either direction with File > Save As or with editcap -F pcap in.pcapng out.pcap.

The pcap layout

A pcap file is a global header followed by records. Each record has a 16-byte record header (timestamp seconds, timestamp microseconds, captured length, original length) and the packet bytes.

The stand-in builds a tiny pcap file in memory, with one hand-made Ethernet frame, and then parses it back the way a tool would. The magic number 0xa1b2c3d4 also tells a reader the byte order of the file.

Python Write and read a pcap file in memory (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The pcapng layout

A pcapng file is a sequence of blocks. Every block starts with a type and a total length, and ends with the same length again. The important ones are the Section Header Block (SHB, byte-order magic and version), the Interface Description Block (IDB, link type and snaplen) and the Enhanced Packet Block (EPB, one packet with a 64-bit timestamp). Because everything is length-prefixed, a reader can skip block types it does not know.

Python Walk the blocks of a pcapng file (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Practical rules that follow from this

  • Save as pcapng, keep originals. Never overwrite the only copy of evidence; work on a copy.
  • Check the link type. A capture from Wi-Fi monitor mode (radiotap), from lo (null/loopback) or from a Linux "any" device (cooked capture) has a different first header than Ethernet.
  • Timestamps come from the capture host's clock. To compare two captures from two machines, their clocks must be synchronized (NTP), otherwise delays calculated across files are meaningless.
  • Dropped packets exist. A busy interface can overflow the kernel buffer; dumpcap reports "packets dropped by kernel". Use a capture filter, a bigger buffer, or a faster disk.
  • Offloading changes what you see. With TCP segmentation offload or checksum offload, a capture on the sending host may show huge frames (e.g. 64 KB) or "incorrect" checksums, because the NIC finishes the work after your capture point. Wireshark's checksum validation is therefore disabled by default.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which component runs the capture filter?

    Capture filters are compiled to BPF and run before packets are copied to user space, so unwanted packets are never stored.
  2. 2. What is the effect of a very small snaplen such as 96?

    Snaplen limits the bytes saved per packet. It saves space and privacy but removes application data.
  3. 3. Why does the classic pcap global header start with the magic number 0xa1b2c3d4?

    Readers compare the magic number with its byte-swapped form to know if the file was written in big or little endian.
  4. 4. A capture on the sending host shows a 'frame' of 60,000 bytes. What is the most likely cause?

    With offloading, the OS hands one large buffer to the NIC, which segments it later, so the capture sees the large buffer.

Summary

  • Packets flow from the NIC through the kernel and libpcap/Npcap to dumpcap, then to Wireshark or tshark.
  • Capture filters (BPF) run in the kernel; promiscuous mode and snaplen are per-capture settings.
  • pcap is simple and universal; pcapng adds multiple interfaces, comments and metadata.
  • A pcap file is a 24-byte header plus 16-byte record headers; pcapng is a stream of length-prefixed blocks.
  • Dropped packets, offloading and clock differences can mislead you, so check them early.

Next: Choosing Interfaces and Capture Options