Command Palette

Search for a command to run...

Hectal
PHASE 6Advanced ~16 min· topic 4 of 8

Topic 6.4

Packet Capture: tcpdump & Wireshark

In one line

When logs and guesses disagree, packets tell the truth. tcpdump captures traffic on an interface with a filter (host, port, protocol), shows it live or saves it to a .pcap file, and Wireshark opens that file to decode every layer and follow whole conversations. Capture on the right host and interface, filter tightly, keep captures short, and treat them as sensitive data.

0/8 · 0%

Think of it like this

A CCTV camera at a shop's door. Instead of asking staff 'did the delivery arrive?', you rewind the recording and see exactly who came, when, and what they carried.

Words you'll meet

New words in this topic, in plain English. Come back here whenever one feels fuzzy.

Packet capture
Recording network packets as they pass an interface, for later analysis.
tcpdump
A command-line packet capture tool available on almost every Linux system.
Wireshark
A graphical tool that decodes captured packets layer by layer.
pcap
The standard file format for saved packet captures.
BPF filter
The capture filter language tcpdump uses (host, port, tcp, and, not).
Retransmission
TCP re-sending a packet that wasn't acknowledged in time, a sign of loss.

Step by step

01Is the request even arriving?

Some Tiffin orders fail with 'payment gateway timeout'. The app team says it sent the request, and the payment provider says it never arrived. Arjun captures on the API server, filtered to the provider's port, while retrying one payment.

terminal
$ sudo tcpdump -ni eth0 -c 8 'tcp port 443 and host 203.0.113.80'
── expected output ──
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
10:02:11.104221 IP 10.0.1.20.51344 > 203.0.113.80.443: Flags [S], seq 1840211, win 64240, options [mss 1460,sackOK,TS val 1 ecr 0,nop,wscale 7], length 0
10:02:12.129877 IP 10.0.1.20.51344 > 203.0.113.80.443: Flags [S], seq 1840211, win 64240, options [mss 1460,sackOK,TS val 1025 ecr 0,nop,wscale 7], length 0
10:02:14.145902 IP 10.0.1.20.51344 > 203.0.113.80.443: Flags [S], seq 1840211, win 64240, options [mss 1460,sackOK,TS val 3041 ecr 0,nop,wscale 7], length 0
Three SYNs at 1 s, then 2 s intervals, with no SYN-ACK. The packets leave the server but nothing comes back.
Is the request even arriving?diagram
Rendering diagram…

02Narrowing it down

The provider's capture shows no SYN from Tiffin's NAT gateway IP. The NAT gateway's metrics show 'port allocation errors': it ran out of source ports for that one destination (Topic 4.3). The packets were dropped at the NAT, which no application log could show.

03Saving and analysing in Wireshark

For a slow-response investigation, Priya saves a two-minute capture, copies it to her laptop, and opens it in Wireshark. The 'tcp.analysis.retransmission' filter instantly shows packet loss on one path, and 'Follow TCP Stream' shows the request and response together.

terminal
$ sudo tcpdump -ni eth0 -w /tmp/slow-api.pcap -c 20000 'tcp port 8080'
scp app1:/tmp/slow-api.pcap . && wireshark slow-api.pcap
── expected output ──
tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
20000 packets captured
20012 packets received by filter
0 packets dropped by kernel
In Wireshark: Statistics → Conversations shows the busiest pairs, and Analyze → Expert Information lists retransmissions and resets.

04Capturing inside containers and pods

A container has its own network namespace, so capturing on the host's eth0 may miss traffic between containers. Run tcpdump inside the namespace: kubectl debug with a netshoot image attaches to a pod's network.

terminal
$ kubectl debug -it pod/order-svc-7d9c -n tiffin --image=nicolaka/netshoot --target=order-svc -- tcpdump -ni eth0 port 53
── expected output ──
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
10:21:04.551 IP 10.10.33.17.44012 > 172.20.0.10.53: 4512+ A? api.razorpay.com.tiffin.svc.cluster.local. (59)
10:21:04.552 IP 172.20.0.10.53 > 10.10.33.17.44012: 4512 NXDomain 0/1/0 (152)

Break it on purpose

Errors are the best teachers. Make each change, read the error, guess what went wrong, then reveal the answer.

Break #1

Capturing on the wrong interface

Arjun runs tcpdump -n port 5432 on a server with several interfaces to debug a database connection.

terminal
$ sudo tcpdump -n port 5432
── what you'll see ──
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
^C
0 packets captured
Nothing, so he concludes the app isn't connecting at all.

Myth vs fact

Myth

HTTPS makes packet captures useless.

Fact

You can't read the encrypted payload, but you still see who connected, when, handshake success or failure, resets, retransmissions, and timings, which solves most network problems.

Pro corner

Extra depth for experienced readers. New to this? Skip it for now and come back later.

  • ▸

    To decrypt your own TLS traffic in a test environment, set SSLKEYLOGFILE=/tmp/keys.log for curl, browsers, or Node, and point Wireshark at the key log (Preferences → Protocols → TLS). Never do this with production user traffic.

Remember this

  1. 1

    tcpdump -ni <iface> '<filter>': -n skips DNS lookups (faster, no extra traffic), -i any captures all interfaces, and filters look like host 10.0.1.20 and port 5432.

  2. 2

    Read TCP flags: [S] SYN, [S.] SYN-ACK, [.] ACK, [P.] data, [F.] FIN, [R] reset. A SYN with no reply means a drop on the way. A SYN answered by [R] means refused.

  3. 3

    Save with -w file.pcap (add -s 0 for full packets on old versions, and -c 1000 to stop after 1,000 packets), then open it in Wireshark on your laptop.

  4. 4

    Wireshark's display filters (tcp.port == 443, dns, http.response.code >= 500, tcp.analysis.retransmission) and 'Follow TCP Stream' turn thousands of packets into one readable conversation.

  5. 5

    Capture at both ends when unsure: if the client sends a SYN and the server never sees it, the drop is between them.

  6. 6

    Captures contain real data (cookies, tokens, personal information in plaintext protocols). Capture only what you need, store them securely, and delete them after the investigation.

Explain it without notes

01

What does a capture showing repeated SYNs and no SYN-ACK tell you?

02

Why capture at both ends of a connection?

Practice

01

Capture a DNS lookup and identify the query and answer.

02

Save a capture of an HTTP request and open it in Wireshark.

Trade-offs

  • ↔

    Captures give ground truth but are heavy (disk, CPU at high rates) and sensitive. Tight filters and short durations keep both manageable. Flow logs (VPC Flow Logs) are lighter but only show connection summaries, not packets.

Done when you can

  • I can capture with a tight filter on the right interface.

  • I can read SYN, RST, and retransmissions.

  • I treat captures as sensitive data.