Skip to content
Network debugging for platform engineers · Chapter 12 of 21 Day 10: Capturing

Day 10: Capturing

Goal: You capture with tcpdump in a targeted way, read the handshake, data flow and teardown from the flags, see both sides of a connection including NAT at the router, analyze captures with tshark (conversations, retransmissions, TLS server name) and prove with two captures who really sent a packet · Duration: approx. 90 min · Prerequisite: Day 9 including clean-up, nginx with TLS from Day 8

Briefing

  • All tools of the last days ask the kernel or the application: “What do you think is happening?” A capture asks the wire: “What happened?” It is the proof when two teams blame each other.
  • The most important principle: capture at the right place, and if needed at two places. A packet that arrives on one side and not on the other locates the fault more precisely than any guess.
  • tcpdump captures and displays right away; tshark (the command line of Wireshark) analyzes. Both understand the same capture filters; tshark additionally knows display filters for every protocol field.

Drill 1: Reading a Conversation

# on netz
# Capture in the background, then trigger a request
#   timeout 5            stop the capture after 5 s at the latest
#   tcpdump -n           no name resolution (faster, unambiguous addresses)
#   -i eth0              on this interface
#   -c 10                stop after 10 packets
#   "host 10.2.0.10 and tcp port 80"   capture filter (BPF): only this host, only TCP port 80
#   &                    in the background
sudo ip netns exec client timeout 5 tcpdump -ni eth0 -c 10 "host 10.2.0.10 and tcp port 80" &
sleep 1
sudo ip netns exec client curl -s -o /dev/null http://web.firma.lab/
# wait for the capture to finish
wait

Expected (times, ports and sequence numbers differ; the options are shortened, and the status lines of tcpdump – listening on …, 10 packets captured – are left out):

16:23:52.858448 IP 10.1.0.10.51910 > 10.2.0.10.80: Flags [S], seq 978236933, win 64240, options [mss 1460,…], length 0
16:23:52.858707 IP 10.2.0.10.80 > 10.1.0.10.51910: Flags [S.], seq 508799158, ack 978236934, win 65160, options […], length 0
16:23:52.858751 IP 10.1.0.10.51910 > 10.2.0.10.80: Flags [.], ack 1, win 502, options […], length 0
16:23:52.859067 IP 10.1.0.10.51910 > 10.2.0.10.80: Flags [P.], seq 1:77, ack 1, win 502, options […], length 76: HTTP: GET / HTTP/1.1
16:23:52.859106 IP 10.2.0.10.80 > 10.1.0.10.51910: Flags [.], ack 77, win 509, options […], length 0
16:23:52.866696 IP 10.2.0.10.80 > 10.1.0.10.51910: Flags [P.], seq 1:186, ack 77, win 509, options […], length 185: HTTP: HTTP/1.0 200 OK
16:23:52.866735 IP 10.1.0.10.51910 > 10.2.0.10.80: Flags [.], ack 186, win 501, options […], length 0
16:23:52.866893 IP 10.2.0.10.80 > 10.1.0.10.51910: Flags [P.], seq 186:200, ack 77, win 509, options […], length 14: HTTP
16:23:52.866900 IP 10.1.0.10.51910 > 10.2.0.10.80: Flags [.], ack 200, win 501, options […], length 0
16:23:52.867091 IP 10.2.0.10.80 > 10.1.0.10.51910: Flags [F.], seq 200, ack 77, win 509, options […], length 0
Flags Meaning
[S] SYN: connection request
[S.] SYN + ACK: the server agrees
[.] ACK only: acknowledgment
[P.] PUSH + ACK: data (length > 0)
[F.] FIN: end of this direction
[R] / [R.] RESET: abort – “nobody listens here” or “I don’t want to”

Read the first three lines as the handshake (Day 5), then GET and the reply HTTP/1.0 200 OK, and at the end the [F.] – from the server, as seen on Day 5 with TIME-WAIT. From the third packet on, tcpdump counts seq and ack relatively so that the numbers stay readable: seq 1:77 are the first 76 bytes of the connection.

Filter Meaning
host 10.2.0.10 sender or receiver
src host …, dst host … one direction only
tcp port 443, udp port 53 protocol and port
net 10.2.0.0/24 a whole network
and, or, not, parentheses in "…" combine
"tcp[tcpflags] & tcp-syn != 0" only packets with the SYN flag

Save your progress? Sign up for free

Drill 2: Both Sides of Forwarding

At the router, -i any shows you every packet twice: once coming in, once going out – and in between, what NAT turns it into.

# on netz
#   -i any     all interfaces; each line names the interface and direction (In/Out)
#   "tcp port 443 and tcp[tcpflags] & tcp-syn != 0"   only SYN and SYN+ACK to/from port 443
#   -c 4       four packets: SYN in, SYN out, SYN+ACK in, SYN+ACK out
sudo ip netns exec router timeout 6 tcpdump -ni any -c 4 "tcp port 443 and tcp[tcpflags] & tcp-syn != 0" &
sleep 1
sudo ip netns exec client curl -s -o /dev/null https://example.com/
wait

Expected (addresses and ports differ, options shortened):

16:23:53.988389 eth-a In  IP 10.1.0.10.44962 > 172.66.147.243.443: Flags [S], seq 1100886518, …
16:23:53.988430 wan0  Out IP 192.168.100.2.44962 > 172.66.147.243.443: Flags [S], seq 1100886518, …
16:23:53.992862 wan0  In  IP 172.66.147.243.443 > 192.168.100.2.44962: Flags [S.], seq 3318481836, ack 1100886519, …
16:23:53.992892 eth-a Out IP 172.66.147.243.443 > 10.1.0.10.44962: Flags [S.], seq 3318481836, ack 1100886519, …

The same SYN (same seq) comes in on eth-a with the sender 10.1.0.10 and goes out on wan0 with 192.168.100.2: masquerade from Day 9, live. The reply is translated back on the return path. If the second line is missing, the router drops the packet (firewall, route); if the third is missing, no reply comes back. That is exactly the value of two measuring points.

Save your progress? Sign up for free

Drill 3: Capture to a File, Analysis with tshark

Whatever you don’t understand right away, you write to a file. You can analyze it as often as you like, send it to colleagues or open it in Wireshark.

# on netz
mkdir -p ~/ndbg/pcap && cd ~/ndbg/pcap
# Capture a TLS request to the nginx from Day 8
#   -w tls.pcap   raw packets to this file instead of the screen
sudo ip netns exec client timeout 6 tcpdump -ni eth0 -w tls.pcap "tcp port 8443" &
sleep 1
sudo ip netns exec client curl -s -o /dev/null --cacert ~/ndbg/pki/root.crt --resolve shop.firma.lab:8443:10.2.0.10 https://shop.firma.lab:8443/
wait
# Which server name did the client send in the TLS handshake (SNI, Day 8)?
#   tshark -r tls.pcap             read the file
#   -Y tls.handshake.type==1       display filter: only ClientHello
#   -T fields -e …                 output only these fields
tshark -r tls.pcap -Y tls.handshake.type==1 -T fields -e ip.src -e tls.handshake.extensions_server_name
# Overview: which conversations, how many packets and bytes, how long?
#   -q              no packet list
#   -z conv,tcp     statistics: TCP conversations
tshark -r tls.pcap -q -z conv,tcp

Expected (ports, bytes and duration differ; tshark may warn because it runs as root):

10.1.0.10   shop.firma.lab
================================================================================
TCP Conversations
Filter:<No Filter>
                                                           |       <-      | |       ->      | |     Total     |    Relative    |   Duration   |
                                                           | Frames  Bytes | | Frames  Bytes | | Frames  Bytes |      Start     |              |
10.1.0.10:46846            <-> 10.2.0.10:8443                   7 2508 bytes      10 1393 bytes      17 3901 bytes     0.000000000         0.0133
================================================================================

Nobody sees the content – it is encrypted. The server name, however, is visible: SNI crosses the wire in plain text (except with Encrypted Client Hello, which is only just spreading). That is exactly why firewalls and proxies can filter by domain without breaking TLS open.

Save your progress? Sign up for free

Drill 4: When Nobody Answers

On Day 4 you saw: DROP looks like a timeout. In the capture you see what the client does during that time.

# on netz
# Firewall in web drops port 8080 (as on Day 4)
sudo ip netns exec web nft add table inet fw
sudo ip netns exec web nft add chain inet fw in '{ type filter hook input priority 0; policy accept; }'
sudo ip netns exec web nft add rule inet fw in tcp dport 8080 drop
#   -ttt   instead of the clock time, the gap to the previous packet
sudo ip netns exec client timeout 9 tcpdump -ni eth0 -ttt "tcp port 8080" &
sleep 1
#   -m 7   curl gives up after 7 seconds
sudo ip netns exec client curl -s -m 7 http://10.2.0.10:8080/
wait
sudo ip netns exec web nft delete table inet fw

Expected (options shortened):

 00:00:00.000000 IP 10.1.0.10.56282 > 10.2.0.10.8080: Flags [S], seq 3866380414, win 64240, …
 00:00:01.004504 IP 10.1.0.10.56282 > 10.2.0.10.8080: Flags [S], seq 3866380414, win 64240, …
 00:00:01.024038 IP 10.1.0.10.56282 > 10.2.0.10.8080: Flags [S], seq 3866380414, win 64240, …
 00:00:01.024040 IP 10.1.0.10.56282 > 10.2.0.10.8080: Flags [S], seq 3866380414, win 64240, …
 00:00:01.023979 IP 10.1.0.10.56282 > 10.2.0.10.8080: Flags [S], seq 3866380414, win 64240, …
 00:00:01.023970 IP 10.1.0.10.56282 > 10.2.0.10.8080: Flags [S], seq 3866380414, win 64240, …

The same SYN (same seq), six times, once per second. That is the lab’s kernel (6.8): the first four retransmissions come after one second each (net.ipv4.tcp_syn_linear_timeouts = 4); only after that does the gap double. Older kernels double right away (1, 2, 4, 8 s). Without -m, Linux would give up after net.ipv4.tcp_syn_retries = 6 retransmissions. A SYN without any reply always means: the packet or the reply gets lost on the way – not: “The service is broken”.

Save your progress? Sign up for free

Exercise

Add packet loss (as on Day 4) and measure what it does to TCP:

# on netz
cd ~/ndbg/pcap
sudo ip netns exec router tc qdisc add dev eth-b root netem loss 20%
# capture for 20 seconds, with ten requests meanwhile
sudo ip netns exec client timeout 20 tcpdump -ni eth0 -w verlust.pcap "host 10.2.0.10" &
sleep 1
for i in 1 2 3 4 5 6 7 8 9 10; do sudo ip netns exec client curl -s -o /dev/null -m 10 http://web.firma.lab/; done
wait
sudo ip netns exec router tc qdisc del dev eth-b root

All ten requests succeed – but is everything fine? Count the retransmissions with tshark and show which packets were affected.

Solution

# on netz
#   -Y tcp.analysis.retransmission   tshark detects retransmissions itself (same seq, sent again)
tshark -r verlust.pcap -Y tcp.analysis.retransmission | wc -l
#   -e frame.time_relative -e ip.src -e tcp.flags.str   time since start, sender, flags as text
tshark -r verlust.pcap -Y tcp.analysis.retransmission -T fields -e frame.time_relative -e ip.src -e tcp.flags.str | head -5

Expected (the number varies, with 20 % loss usually between 4 and 15):

7
0.319372000 10.1.0.10   ·······AP···
0.575374000 10.1.0.10   ·······AP···
0.783373000 10.1.0.10   ·······AP···
0.837446000 10.2.0.10   ·······A···F
1.039367000 10.1.0.10   ·······A···F

Each retransmission costs at least 200 ms (TCP’s smallest timeout on Linux), and a lost SYN costs a whole second. The application notices nothing of this except “sometimes slow” – and that is exactly the typical ticket. curl -w (Day 7) shows the time, the capture shows the reason. On a server without a capture, nstat -az TcpRetransSegs (Day 5) gives a first hint.

Save your progress? Sign up for free

Challenge

Inject the fault:

# on netz
sudo ip netns exec router nft add table inet fw
sudo ip netns exec router nft add chain inet fw forward '{ type filter hook forward priority 0; policy accept; }'
sudo ip netns exec router nft add rule inet fw forward tcp dport 8080 reject with tcp reset

The ticket: “curl http://10.2.0.10:8080/ immediately reports ‘Couldn’t connect’. The web team says: our nginx listens on 8080, and we don’t see a single connection attempt.” Both are right. Prove with two captures who sends the RST, without looking at a firewall.

Solution

# on netz
cd ~/ndbg/pcap
# capture at both ends simultaneously: at client and at web
sudo ip netns exec web timeout 5 tcpdump -ni eth0 -w web.pcap "tcp port 8080" &
sudo ip netns exec client timeout 5 tcpdump -ni eth0 -w client.pcap "tcp port 8080" &
sleep 1
sudo ip netns exec client curl -sS http://10.2.0.10:8080/
wait
# What did web see?
#   -r web.pcap   read a file instead of capturing
sudo tcpdump -nr web.pcap | wc -l
# What did the client see – with the IP header (-v), which contains the TTL
sudo tcpdump -vnr client.pcap

Expected (shortened; cksum incorrect is harmless, see below):

curl: (7) Failed to connect to 10.2.0.10 port 8080 after 1 ms: Couldn't connect to server
0
… IP (tos 0x0, ttl 64, id 62892, offset 0, flags [DF], proto TCP (6), length 60)
    10.1.0.10.51286 > 10.2.0.10.8080: Flags [S], cksum 0x1445 (incorrect -> 0x0a03), seq 357552059, …
… IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 40)
    10.2.0.10.8080 > 10.1.0.10.51286: Flags [R.], cksum 0x1431 (incorrect -> 0xcec7), seq 0, ack 357552060, …

Not a single packet arrived at web. Still, the client receives an RST with the sender 10.2.0.10. The proof of who really sent it comes from the TTL: every router subtracts 1 when forwarding. An RST from web would arrive with 63 (64 minus the router). This one has 64 – it was created at the last hop before the client, the router, which pretends to be web. For comparison, a real RST from web, for a port on which nothing listens:

# on netz
sudo ip netns exec client timeout 4 tcpdump -vni eth0 "tcp port 82" &
sleep 1
sudo ip netns exec client curl -s http://10.2.0.10:82/
wait

Expected (shortened):

… IP (tos 0x0, ttl 64, …) 10.1.0.10.44154 > 10.2.0.10.82: Flags [S], …
… IP (tos 0x0, ttl 63, …) 10.2.0.10.82 > 10.1.0.10.44154: Flags [R.], cksum 0x2f99 (correct), …

TTL 63: this RST passed the router. Now looking into the router’s firewall is justified:

# on netz
sudo ip netns exec router nft list chain inet fw forward
sudo ip netns exec router nft delete table inet fw

About cksum incorrect: on the sending host, the network card (here the veth) computes the checksum only after the capture (“checksum offload”). tcpdump sees the packet before that. On the receiving side it is correct. For captured outgoing packets the message is therefore almost always harmless.

Save your progress? Sign up for free

Checkpoint

# on netz
# no firewall tables in router and web, no impairment on eth-b
sudo ip netns exec router nft list tables
sudo ip netns exec web nft list tables | wc -l
sudo ip netns exec router tc qdisc show dev eth-b | grep -c netem
# captures are present
ls ~/ndbg/pcap

Expected:

table ip nat
0
0
client.pcap
tls.pcap
verlust.pcap
web.pcap

Clean up

Nothing to do. You can keep the captures in ~/ndbg/pcap; they belong to tcpdump (the service drops its privileges after starting), so deleting works with sudo rm.

Interview note

“Two teams argue about whether a packet arrives. How do you settle it?” Capture at both ends simultaneously, with a tight filter (host, port), to files (-w). If it arrives on one side and not on the other, the fault lies in between. Bonus points: recognizing from the TTL whether a packet was forwarded or created at the last hop; tshark -Y tcp.analysis.retransmission for retransmissions; SNI is readable in the capture, the content is not.

Enterprise note

Captures contain data – with unencrypted traffic also passwords, tokens and personal data. In many companies they therefore require approval, are recorded only briefly and filtered (-s 128 cuts off after 128 bytes and usually keeps only the headers) and deleted afterwards. In Kubernetes you capture in a pod’s network namespace (Day 11) or with kubectl debug and a tools image; Cilium shows connections with Hubble without recording packets (Day 14).

Command Drills

Practise commands from Day 10

3 drills · approx. 9 minutes · in your browser, no VM

Task

Capture an HTTP conversation

With an account your progress is saved and drills that are due come back in your daily round. Without an account it stays in this browser only. Sign up for free

Contents

More