Format:pcap (libpcap savefile)
| Magic | D4 C3 B2 A1 (little-endian, microseconds) / A1 B2 C3 D4 (big-endian) |
|---|---|
| Extensions | .pcap, .cap |
| Endianness | writer's native order, detected from the magic |
| License | BSD-3-Clause (libpcap) |
| On this wiki | Facts · Code · Notes · Topics |
The classic packet capture file of libpcap/tcpdump: a 24-byte file header (magic 0xA1B2C3D4, or 0xA1B23C4D for nanosecond timestamps) followed by 16-byte record headers and raw packet bytes, with the byte order of the writing machine.
Overview
The pcap savefile is what tcpdump, libpcap-based tools and Wireshark write by default. It is the usual input when reversing an application's network protocol from captures of its own traffic (see Technique:Reversing network protocols). Its successor is Format:pcapng. The IETF is standardising both (draft-ietf-opsawg-pcap, draft-ietf-opsawg-pcaplinktype).
File header (24 bytes)
libpcap declares it as struct pcap_file_header in pcap/pcap.h: u32 magic; u16 version_major; u16 version_minor; i32 thiszone; u32 sigfigs; u32 snaplen; u32 linktype;. thiszone and sigfigs are unused and should be 0. libpcap defines PCAP_VERSION_MAJOR 2 and PCAP_VERSION_MINOR 4. The upper bits of linktype carry extra fields (see pcap.h), so mask them before comparing with a LINKTYPE value.
Magic numbers and byte order
The reader decides the byte order by comparing the first four bytes with the magic; if only the byte-swapped value matches, every integer in the file is swapped.
| Magic as u32 | Meaning |
|---|---|
| 0xA1B2C3D4 | standard, timestamps in seconds + microseconds |
| 0xA1B23C4D | timestamps in seconds + nanoseconds |
| 0xA1B2CD34 | Alexey Kuznetsov's modified format (extra fields per record header; libpcap reads it) |
| 0xA1B234CD | FMesquita variant, defined in sf-pcap.c as FMESQUITA_TCPDUMP_MAGIC but referenced nowhere else in that file at ba925f7be2 (checked by reading the file): libpcap neither reads nor writes it; its layout is not documented there |
| 0xA12B3C4D | Navtel variant, defined in sf-pcap.c as NAVTEL_TCPDUMP_MAGIC; checked: the only use in sf-pcap.c is the append path, which answers "not a pcap file to which we can append", and the read path has no case for it, so libpcap does not read this layout |
| 0xA1B2C3CB | not pcap: libpcap's cbpf-savefile (compiled BPF program) |
On disk a little-endian microsecond file begins D4 C3 B2 A1, a little-endian nanosecond file 4D 3C B2 A1; a big-endian writer produces A1 B2 C3 D4 / A1 B2 3C 4D.
Records
Each packet is a 16-byte header followed by caplen bytes: u32 ts_sec; u32 ts_subsec (usec or nsec by magic); u32 caplen; u32 len (len is the original length before slicing to snaplen). The timestamp fields are unsigned in the file format. There is no index, no end marker and no checksum: read until end of file, and treat a record whose caplen exceeds snaplen as corruption.
Link types
The linktype field says how to parse the first bytes of each record. It uses the LINKTYPE_ numbering (a registry kept in the pcaplinktype draft), which is not always the same as the in-memory DLT_ numbers of libpcap (DLT_RAW is 12 or 14 depending on the OS, LINKTYPE_RAW is 101; DLT_LOOP is 108 in the registry). Common values: 0 NULL (BSD loopback, 4-byte address family in host order), 1 ETHERNET, 101 RAW (bare IPv4 or IPv6, version nibble decides), 108 LOOP, 113 LINUX_SLL (Linux cooked capture), 276 LINUX_SLL2, 228 IPV4, 229 IPV6, 201 BLUETOOTH_HCI_H4_WITH_PHDR, 220 USB_LINUX_MMAPPED, 9 PPP. Checked in libpcap pcap-linux.c (ba925f7be2): ARPHRD_LOOPBACK sets linktype DLT_EN10MB, so a Linux loopback capture is LINKTYPE_ETHERNET (1) with zero MAC addresses. On BSD-family systems pcap-bpf.c (ba925f7be2, line 2328-2329) maps the interface type IFT_LOOP to DLT_NULL, so a loopback capture there is LINKTYPE_NULL (0), unlike Linux. pcap-linux.c sets DLT_LINUX_SLL for cooked captures (it also supports DLT_LINUX_SLL2; which one the "any" device gets by default was not traced), with the interface information in the cooked header.
More values from the registry (linktypes.csv in draft-ietf-opsawg-pcap @ 207d00185e, read): 105 IEEE802_11, 127 IEEE802_11_RADIOTAP (802.11 preceded by a Radiotap header), 163 IEEE802_11_AVS, 104 C_HDLC, 50 PPP_HDLC, and 147 to 162 (16 values, named LINKTYPE_RESERVED_01 to _16 in the csv) for private use, which a private-format capture may use with any layout, so never trust them without knowing the writer. The registry names 228 IPV4 and 229 IPV6 as bare IP without link header, like 101 RAW but with the version fixed.
Settled: on the Linux any device libpcap (pcap-linux.c @ ba925f7be2, read) sets DLT_LINUX_SLL by default and lists DLT_LINUX_SLL and DLT_LINUX_SLL2 as selectable; the header layouts are on Format:Linux cooked capture (SLL and SLL2).
Tools and sources
- Library:libpcap (reader and writer:
sf-pcap.c). - Wireshark, tcpdump, editcap, tshark can all read it; scapy and dpkt are common script readers (Unverified: not read for this page).
- Specification drafts: https://github.com/ietf-opsawg-wg/draft-ietf-opsawg-pcap.
Code & repositories
- https://github.com/the-tcpdump-group/libpcap @
ba925f7be2ad4cc90ff00ea249310679094221a5· license: BSD-3-Clause (declared by the contributor for the files used; GitHub reports BSD-2-Clause (read from LICENSE; GitHub's detector did not classify it) for the repository)
libpcap: sf-pcap.c savefile reader/writer with the magic numbers, pcap/pcap.h file and record header structs, pcap/dlt.h link types.
Tested on: source read, not run: source only · checked 2026-10-18; last activity 2026-10-17 - https://github.com/ietf-opsawg-wg/draft-ietf-opsawg-pcap @
207d00185e028e13fc22b9f0b051da8767768fe5· license: BSD-3-Clause (declared by the contributor; GitHub's detector did not recognize LICENSE.md)
IETF drafts for pcap, pcapng and the LINKTYPE registry (markdown sources, linktypes.csv).
Tested on: source read, not run: source only · checked 2026-10-18; last activity 2026-10-13
Pitfalls
- Check the magic first, not the extension: pcapng files are often named .pcap (their first bytes are
0A 0D 0D 0A, see Format:pcapng). - A nanosecond file read as microseconds shows timestamps 1000 times too large.
- Snaplen truncation (
caplen < len) cuts payloads: a protocol that looks like it has bad length fields may simply be cut.