Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Network and Log Forensics

Packet Capture and PCAP Parsing

Understand the PCAP file format, Ethernet, IP and TCP headers, and parse a synthetic capture in Python to list conversations, with read-only Wireshark and tcpdump examples.

A packet capture (PCAP) is a file holding copies of packets seen on a network interface, each with a timestamp. It is the highest-fidelity network evidence: if the traffic was not encrypted, the capture contains the actual content. Analysts open captures in tools such as Wireshark or tcpdump, but understanding the format helps you trust (and verify) what those tools show.

You cannot run Wireshark in a browser, so this lesson shows tool commands as read-only examples and then builds a tiny working parser in Python that reads a synthetic capture created in memory.

How a classic PCAP file is laid out

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

flowchart TB
  G["Global header (24 bytes): magic, version, snaplen, link type"] --> R1["Record header (16 bytes)"]
  R1 --> P1["Packet bytes"]
  P1 --> R2["Record header"]
  R2 --> P2["Packet bytes"]

Magic number 0xA1B2C3D4 tells you the byte order; link type 1 means Ethernet.

Newer pcapng files use blocks instead and can store interface names and comments, but the packet contents are the same.

Layers inside a packet

Inside each Ethernet frame you find nested headers:

Layer Header size Key fields
Ethernet 14 bytes destination MAC, source MAC, EtherType (0x0800 = IPv4)
IPv4 20+ bytes version/length, protocol (6 = TCP, 17 = UDP), source and destination address
TCP 20+ bytes source port, destination port, sequence numbers, flags
Payload variable application data (HTTP, DNS, ...)

Read-only: what the standard tools look like

These commands work on a capture you are authorized to read. The output is an illustrative example.

Terminal
# Show the first 5 packets without resolving names
tcpdump -nn -r case42.pcap -c 5

# Typical output (example):
# 14:03:01.120344 IP 10.0.0.7.51512 > 198.51.100.9.4444: Flags [S], seq 1001, length 0
# 14:03:01.140901 IP 198.51.100.9.4444 > 10.0.0.7.51512: Flags [S.], seq 7001, ack 1002, length 0

# Hash first, then analyze a copy
sha256sum case42.pcap

In Wireshark a display filter such as ip.addr == 10.0.0.7 && tcp.port == 4444 narrows the view, and Follow TCP Stream reassembles the conversation.

A working parser

First we build a synthetic capture in memory (so the lesson needs no files), then parse it with Python's struct module.

Python Build and parse a tiny PCAP

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 three packets form the start of a TCP handshake (SYN, SYN-ACK) followed by data. A real parser also checks bounds: malformed or truncated packets are common in evidence.

Conversations and streams

Analysts group packets into conversations keyed by the 5-tuple (protocol, source IP, source port, destination IP, destination port). Normalize the key so both directions fall in one bucket.

Python Group packets into conversations

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.

Tips for sound captures

  • Capture with the least possible filtering when authorized; filters can discard the key packet.
  • Record the capture point, tool, version, interface, start and end time.
  • Hash the file at collection and again after copying.
  • Remember snaplen: a small snapshot length truncates payloads.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many bytes is the classic pcap global header?

    The global header is 24 bytes; each packet record then has its own 16-byte header.
  2. 2. Which value in the IPv4 header says the payload is TCP?

    Protocol 6 is TCP and 17 is UDP; 0x0800 in the Ethernet header means IPv4.
  3. 3. Why normalize the conversation key?

    Sorting the two endpoints gives the same key for requests and replies.
  4. 4. What can a too-small snaplen cause?

    Only the first snaplen bytes of each packet are saved, so payload data may be cut off.

Summary

  • A pcap is a global header plus timestamped packet records.
  • Packets nest Ethernet, IP, TCP/UDP and payload headers.
  • Tools like tcpdump and Wireshark show the same fields you can parse yourself.
  • Group packets by a direction-independent 5-tuple to read conversations.
  • Document the capture point and hash the file.

Next: Web Server Log Analysis