The concept of packet capture
Packet capture saves an exact copy of every frame an interface sends or receives, so you can see what really happened on the wire when logs show no clear error. This note covers promiscuous mode, why a switch hides other people's traffic, SPAN/TAP, pcap vs pcapng, capture vs display filters and the privacy rules.
121.1 Why is it used?
- A web app does not open and the logs have no clear error: the packets themselves show where it fails (ARP, handshake, HTTP).
- See who is talking to whom, on which port, and at what time.
- Prove whether a request even left your machine or a reply came back.
- Save a file (pcap) that a colleague can open later in Wireshark/tshark.
- The original packet is not changed or delayed; the tool only keeps a copy.
121.2 How it works
- A capture tool (tcpdump, tshark, Wireshark) asks the kernel, via libpcap, for a copy of each frame on an interface.
- Normal mode: the NIC checks the destination MAC and silently drops frames that are not for it (or broadcast).
- Promiscuous mode: the NIC skips that check and accepts every frame that physically reaches its port.
- On an old hub every frame went to every port, so promiscuous mode showed everyone's traffic.
- A switch learns MACs and forwards unicast only to the destination port, so promiscuous mode is NOT enough: you see only broadcasts and your own traffic.
- SPAN (Switched Port Analyzer) / mirror port: the admin tells the switch to copy a port's frames to a monitor port.
- Network TAP: a device inside the cable that passes traffic through and hands out a copy.
- Step 1
1.Wire / NIC: all frames that reach the port
- Step 2
2.Capture filter (BPF) runs in the kernel: host, port
- Step 3
3.Non-matching packets are dropped and never saved
- Step 4
4.pcap / pcapng file: only matching packets
- Step 5
5.Display filter (tshark -Y, Wireshark): http.request
- Step 6
6.You read the result; change the display filter any time
BPF drops are gone for good; a display filter only hides packets from the view
- Step 1
1.Client 192.168.141.10 pings server 192.168.141.20
- Step 2
2.ARP request is a broadcast: the switch floods it to every port
- Step 3
3.Spy 192.168.141.30 (promiscuous) captures the ARP request
- Step 4
4.Switch learns the server's port from the MAC table
- Step 5
5.ICMP unicast goes only to the server's port, never to the spy
- Step 6
6.Fix: SPAN / mirror port or a TAP gives the spy a copy
121.3 pcap vs pcapng
| Point | pcap | pcapng |
|---|---|---|
| Age | Old, simple format | pcap next generation, newer |
| Default of | tcpdump | Wireshark, tshark, dumpcap |
| Extra data | One link type, no comments | Several interfaces, comments, metadata in one file |
| Magic number (first 4 bytes) | d4 c3 b2 a1 | 0a 0d 0d 0a |
| Opens in Wireshark | Yes | Yes |
121.4 Capture filter (BPF) vs display filter
| Point | Capture filter | Display filter |
|---|---|---|
| Language | BPF (Berkeley Packet Filter) | Wireshark's own field syntax |
| Example | host 192.168.141.20, port 80 | http.request, ip.src == 10.0.0.1 |
| Where it runs | In the kernel, while capturing | On already saved packets |
| Effect | Non-matching packets never reach the file | Only hides packets from the view |
| Used by | tcpdump, tshark -f, dumpcap | tshark -Y, Wireshark filter bar |
| Can you undo it? | No, dropped data is gone | Yes, change it any time |
121.5 Interfaces and permission
- tcpdump -D lists the interfaces you can capture on (eth0, any, lo).
- Capturing needs root or the CAP_NET_RAW / CAP_NET_ADMIN capability; a normal user gets a permission error.
tcpdump -Dsu -s /bin/sh nobody \ -c 'tcpdump -i eth0 -c 1'$ tcpdump -D1.eth0 [Up, Running, Connected]2.any (Pseudo-device that captures on all interfaces) [Up, Running]3.lo [Up, Running, Loopback]$ su -s /bin/sh nobody \ -c 'tcpdump -i eth0 -c 1'tcpdump: eth0: You don't have permission to perform this capture on that device(socket: Operation not permitted)121.6 Promiscuous mode on and off
- ip -d link show prints the promiscuity counter of the interface.
- tcpdump turns promiscuous mode on by default (0 -> 1).
- tcpdump -p does not turn it on, so the counter stays 0.
ip -d link show eth0 | grep -o promiscuity..tcpdump -i eth0 -w /dev/null &sleep 1ip -d link show eth0 | grep -o promiscuity..pkill tcpdumptcpdump -p -i eth0 -w /dev/null &sleep 1ip -d link show eth0 | grep -o promiscuity..pkill tcpdump$ ip -d link show eth0 | grep -o promiscuity..promiscuity 0$ tcpdump -i eth0 -w /dev/null &tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes$ ip -d link show eth0 | grep -o promiscuity..promiscuity 1$ pkill tcpdump$ tcpdump -p -i eth0 -w /dev/null &$ ip -d link show eth0 | grep -o promiscuity..promiscuity 0121.7 First capture with tcpdump
- -i eth0 picks the interface, -w first.pcap writes raw packets to a file.
- host 192.168.141.20 is the BPF capture filter, so the ping to 1.1.1.1 never reaches the file.
- pkill -INT stops tcpdump cleanly, and it prints the captured / dropped counters.
cd /tmptcpdump -i eth0 -w first.pcap \ host 192.168.141.20 &sleep 1curl -s http://192.168.141.20/ping -c 2 -q 1.1.1.1 > /dev/nullsleep 1; pkill -INT tcpdump; sleep 1ls -l first.pcaptcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes$ curl -s http://192.168.141.20/hello from server$ ping -c 2 -q 1.1.1.1 > /dev/null$ sleep 1; pkill -INT tcpdump; sleep 115 packets captured15 packets received by filter0 packets dropped by kernel$ ls -l first.pcap-rw-r--r-- 1 root root 1478 Nov 9 02:37 first.pcap121.8 Reading the file with tcpdump and tshark
- tcpdump -r reads a file; -nn no name/port lookup, -t no timestamp, -q short lines.
- First ARP, then the TCP three-way handshake to port 80.
- tshark -Y http.request applies a display filter: only 1 GET out of 15 packets is left.
- -T fields -e ... prints only chosen fields, which is ideal for scripts.
tcpdump -nn -t -q -r first.pcap | head -6tshark -r first.pcap -Y http.requesttshark -r first.pcap -Y http -T fields \ -e ip.src -e http.request.uri \ -e http.response.code$ tcpdump -nn -t -q -r first.pcap | head -6reading from file first.pcap, link-type EN10MB (Ethernet), snapshot length 262144ARP, Request who-has 192.168.141.20 tell 192.168.141.10ARP, Reply 192.168.141.20 is-at 06:31:87:6a:cd:b2IP 192.168.141.10.54536 > 192.168.141.20.80: tcp 0IP 192.168.141.20.80 > 192.168.141.10.54536: tcp 0IP 192.168.141.10.54536 > 192.168.141.20.80: tcp 0$ tshark -r first.pcap -Y http.request 7 0.002178 192.168.141.10 → 192.168.141.20 HTTP 143 GET / HTTP/1.1$ tshark -r first.pcap -Y http -T fields \ -e ip.src -e http.request.uri \ -e http.response.code192.168.141.10 / 192.168.141.20 200121.9 Two different filter languages
- tcpdump -d shows the compiled BPF code that the kernel runs on every packet.
- Putting BPF into tshark -Y fails, and tshark hints that it looks like a capture filter.
- Putting a display filter into tcpdump fails with a syntax error.
tcpdump -d 'host 192.168.141.20' | head -4tshark -r first.pcap \ -Y 'host 192.168.141.20'tcpdump -nn -r first.pcap 'http.request'$ tcpdump -d 'host 192.168.141.20' | head -4(000) ldh [12](001) jeq #0x800 jt 2 jf 6(002) ld [26](003) jeq #0xc0a88d14 jt 12 jf 4$ tshark -r first.pcap \ -Y 'host 192.168.141.20'tshark: "192.168.141.20" was unexpected in this context. Note: That read filter code looks like a valid capture filter; maybe you mixed them up?$ tcpdump -nn -r first.pcap 'http.request'reading from file first.pcap, link-type EN10MB (Ethernet), snapshot length 262144tcpdump: can't parse filter expression: syntax error121.10 Checking the file format
capinfos -t -c first.pcapeditcap -F pcapng first.pcap first.pcapngcapinfos -t first.pcapngod -An -tx1 -N4 first.pcapod -An -tx1 -N4 first.pcapng$ capinfos -t -c first.pcapFile name: first.pcapFile type: Wireshark/tcpdump/... - pcapNumber of packets: 15$ editcap -F pcapng first.pcap first.pcapng$ capinfos -t first.pcapngFile name: first.pcapngFile type: Wireshark/... - pcapng$ od -An -tx1 -N4 first.pcap d4 c3 b2 a1$ od -An -tx1 -N4 first.pcapng 0a 0d 0d 0a121.11 What a promiscuous spy sees on a switch
- The client flushes its ARP cache and sends 3 pings; all 3 are answered.
- The spy captured only 1 packet: the ARP broadcast. Not a single ICMP packet.
- The Docker bridge behaves like a learning switch, exactly like a real office switch.
# spy 192.168.141.30 (promiscuous)timeout 8 tcpdump -i eth0 -nn# client 192.168.141.10ip neigh flush dev eth0ping -c 3 192.168.141.20 | tail -2client$ ping -c 3 192.168.141.20 | tail -23 packets transmitted, 3 received, 0% packet loss, time 2030msrtt min/avg/max/mdev = 0.134/0.272/0.510/0.168 msspy$ timeout 8 tcpdump -i eth0 -nnlistening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes02:38:02.714444 ARP, Request who-has 192.168.141.20 tell 192.168.141.10, length 281 packet captured1 packet received by filter0 packets dropped by kernel121.12 Privacy and permission
- A capture can contain passwords, cookies and personal messages.
- Capture only on your own network or with written permission.
- Use a small snap length (-s 96) to save only headers and cut off the payload.
- Delete pcap files when done, or keep them encrypted.
tcpdump -i eth0 -s 96 -w small.pcap \ host 192.168.141.20 &sleep 1curl -s -o /dev/null http://192.168.141.20/sleep 1; pkill -INT tcpdump; sleep 1capinfos -l small.pcap | tail -112 packets captured0 packets dropped by kernel$ capinfos -l small.pcap | tail -1Packet size limit: inferred: 96 bytes121.13 Common mistakes
- Thinking promiscuous mode on a switch port shows everyone's traffic: you only get broadcasts and your own frames.
- Thinking capture lets you read everything: HTTPS/TLS content looks like random bytes; you still see IP, port, timing and the handshake.
- Mixing the two filter languages (BPF in -Y, or http.request in tcpdump).
- Using a too-narrow capture filter and losing the packet you actually needed.
- Capturing on someone else's network without permission.
121.14 Interview tips
- Capture filter = BPF, applied in the kernel while capturing, keeps the file small but dropped data is gone; display filter = Wireshark syntax on saved packets, changeable any time.
- On a switch, promiscuous mode is not enough: you need a SPAN/mirror port or a TAP.
- pcap is tcpdump's simple default; pcapng (Wireshark default) holds many interfaces and comments; the magic number tells them apart.
- Encrypted traffic still shows who, when, which port and where the connection fails, which is what troubleshooting needs.