Day 7: Capturing with tcpdump and tshark
Goal: Capture packets where they originate, pass through and arrive. Read the TCP handshake line by line, filter captures, save them and analyze them with tshark. And see in real faults what packet loss, a full window and a forged reset look like in a capture · Duration: approx. 90 min · Prerequisite: Days 2, 4 and 5
Briefing
- tcpdump captures packets on an interface and shows them as text, or writes them to a file (pcap). It runs everywhere, needs no GUI and is the first tool to reach for when “the network” is supposedly to blame.
- tshark is Wireshark without a window. It reads the same files, understands hundreds of protocols and computes what tcpdump only shows: retransmits, windows, conversations, HTTP responses.
- The most important question when capturing is not how but where. A packet missing at the client may still be there at the router. That is why today you capture in three places: in the client, in the router and in the server.
┌─────┐ 10.70.1.0/24 ┌──────────────┐ 10.70.2.0/24 ┌──────────────────────┐
│ cli ├───────────────┤ eth1 r eth2├───────────────┤ srv: 8080 http.server │
│ .10 │ └──────────────┘ │ .20 │
└─────┘ └──────────────────────┘
▲ tcpdump ▲ tcpdump -i any ▲ tcpdump
| Term | Meaning |
|---|---|
| pcap | File format for captures, readable by tcpdump, tshark and Wireshark |
| BPF filter | Capture filter (tcp port 8080), already applied in the
kernel, discarded packets cost nothing |
| Display filter | tshark filter (-Y http), only applied during analysis,
understands protocol fields |
| Retransmit | a segment that TCP sends again because no acknowledgment arrived |
| Window | the number of bytes the sender may send before the receiver acknowledges |
Drill 1: Lab and web server
The lab from Day 5, this time with a web server that serves a small and a large file:
# on netz
sudo ip netns add r
sudo ip netns add cli
sudo ip netns add srv
sudo ip -n r link set lo up
sudo ip -n cli link set lo up
sudo ip -n srv link set lo up
sudo ip -n r link add eth1 type veth peer name eth0 netns cli
sudo ip -n r link add eth2 type veth peer name eth0 netns srv
sudo ip -n r addr add 10.70.1.1/24 dev eth1
sudo ip -n r addr add 10.70.2.1/24 dev eth2
sudo ip -n cli addr add 10.70.1.10/24 dev eth0
sudo ip -n srv addr add 10.70.2.20/24 dev eth0
sudo ip -n r link set eth1 up
sudo ip -n r link set eth2 up
sudo ip -n cli link set eth0 up
sudo ip -n srv link set eth0 up
sudo ip -n cli route add default via 10.70.1.1
sudo ip -n srv route add default via 10.70.2.1
sudo ip netns exec r sysctl -qw net.ipv4.ip_forward=1
sudo ip netns exec cli ping -c1 -W1 10.70.2.20 | grep -o '1 received'Expected: 1 received
# on netz
mkdir -p ~/mitschnitt/web && cd ~/mitschnitt
echo "Hallo Mitschnitt" > web/index.html
head -c 3000000 /dev/urandom > web/gross.bin
sudo ip netns exec srv sudo -u ubuntu python3 -m http.server 8080 --directory /home/ubuntu/mitschnitt/web >/dev/null 2>&1 &
sleep 1
sudo ip netns exec cli curl -s http://10.70.2.20:8080/Expected: Hallo Mitschnitt
Save your progress? Sign up for free
Drill 2: The first packets
tcpdump in the router, on the interface facing the client. First
cli forgets its ARP entry so you also see the address
resolution:
# on netz
sudo ip netns exec r tcpdump -i eth1 -n -c 6 &
sleep 1
sudo ip netns exec cli ip neigh flush dev eth0
sudo ip netns exec cli ping -c2 -q 10.70.2.20 >/dev/null
waitExpected:
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), snapshot length 262144 bytes
00:15:30.172608 ARP, Request who-has 10.70.1.1 tell 10.70.1.10, length 28
00:15:30.172633 ARP, Reply 10.70.1.1 is-at 2e:eb:0c:c0:7a:a8, length 28
00:15:30.172640 IP 10.70.1.10 > 10.70.2.20: ICMP echo request, id 11481, seq 1, length 64
00:15:30.172693 IP 10.70.2.20 > 10.70.1.10: ICMP echo reply, id 11481, seq 1, length 64
00:15:31.216385 IP 10.70.1.10 > 10.70.2.20: ICMP echo request, id 11481, seq 2, length 64
00:15:31.216454 IP 10.70.2.20 > 10.70.1.10: ICMP echo reply, id 11481, seq 2, length 64
6 packets captured
6 packets received by filter
0 packets dropped by kernel
First cli asks who has 10.70.1.1 (its
gateway, not the server!), then the pings go out. The options you always
need:
| Option | Effect |
|---|---|
-i eth1 |
capture on this interface, -i any on all of them |
-n |
do not resolve names. Without -n, tcpdump queries DNS
for every address and gets slow |
-c 6 |
stop after 6 packets |
-v, -vv |
more details: TTL, IP ID, checksums |
-e |
show MAC addresses too |
-A |
payload as text |
0 packets dropped by kernel is the line to watch under
heavy traffic. If tcpdump cannot keep up, the kernel discards packets
before tcpdump sees them. A narrower filter or a larger buffer
(-B) helps then.
Save your progress? Sign up for free
Drill 3: The handshake
Now in the server, port 8080 only, while cli fetches the
small page. The capture goes into a file and is read afterwards:
# on netz
cd ~/mitschnitt
sudo ip netns exec srv tcpdump -i eth0 -n -c 12 -w abruf.pcap tcp port 8080 2>/dev/null &
sleep 1
sudo ip netns exec cli curl -s -o /dev/null http://10.70.2.20:8080/
wait
tcpdump -r abruf.pcap -n 2>/dev/null | cut -c1-110Expected (truncated to 110 characters):
00:15:49.702927 IP 10.70.1.10.49110 > 10.70.2.20.8080: Flags [S], seq 892736632, win 64240, options [mss 1460,
00:15:49.702982 IP 10.70.2.20.8080 > 10.70.1.10.49110: Flags [S.], seq 3118129683, ack 892736633, win 65160, o
00:15:49.703025 IP 10.70.1.10.49110 > 10.70.2.20.8080: Flags [.], ack 1, win 502, options [nop,nop,TS val 2514
00:15:49.703247 IP 10.70.1.10.49110 > 10.70.2.20.8080: Flags [P.], seq 1:79, ack 1, win 502, options [nop,nop,
00:15:49.703259 IP 10.70.2.20.8080 > 10.70.1.10.49110: Flags [.], ack 79, win 509, options [nop,nop,TS val 623
00:15:49.704957 IP 10.70.2.20.8080 > 10.70.1.10.49110: Flags [P.], seq 1:186, ack 79, win 509, options [nop,no
00:15:49.705007 IP 10.70.1.10.49110 > 10.70.2.20.8080: Flags [.], ack 186, win 501, options [nop,nop,TS val 25
00:15:49.705079 IP 10.70.2.20.8080 > 10.70.1.10.49110: Flags [P.], seq 186:203, ack 79, win 509, options [nop,
00:15:49.705110 IP 10.70.1.10.49110 > 10.70.2.20.8080: Flags [.], ack 203, win 501, options [nop,nop,TS val 25
00:15:49.705244 IP 10.70.1.10.49110 > 10.70.2.20.8080: Flags [F.], seq 79, ack 203, win 501, options [nop,nop,
00:15:49.705249 IP 10.70.2.20.8080 > 10.70.1.10.49110: Flags [F.], seq 203, ack 79, win 509, options [nop,nop,
00:15:49.705271 IP 10.70.1.10.49110 > 10.70.2.20.8080: Flags [.], ack 204, win 501, options [nop,nop,TS val 25
Twelve packets for 17 bytes of content. The flags:
| Flag | Meaning | Packets |
|---|---|---|
[S] |
SYN: I want to connect, my initial number is … | 1 |
[S.] |
SYN + ACK: agreed, my initial number is … | 2 |
[.] |
ACK only: received up to here | 3, 5, 7, 9, 12 |
[P.] |
PUSH + ACK: data, please hand it to the application right away | 4, 6, 8 |
[F.] |
FIN + ACK: I am done | 10, 11 |
[R] |
RST: connection refused or aborted | Drill 4 |
From packet 3 on, tcpdump shows relative numbers:
seq 1:79 means bytes 1 to 78 of this connection, the 78
bytes of the HTTP request. The server acknowledges with
ack 79: everything up to byte 78 received, next I expect
79. -S shows the real numbers:
# on netz
tcpdump -r abruf.pcap -n -S -c 4 2>/dev/null | cut -c1-110Expected:
00:15:49.702927 IP 10.70.1.10.49110 > 10.70.2.20.8080: Flags [S], seq 892736632, win 64240, options [mss 1460,
00:15:49.702982 IP 10.70.2.20.8080 > 10.70.1.10.49110: Flags [S.], seq 3118129683, ack 892736633, win 65160, o
00:15:49.703025 IP 10.70.1.10.49110 > 10.70.2.20.8080: Flags [.], ack 3118129684, win 502, options [nop,nop,TS
00:15:49.703247 IP 10.70.1.10.49110 > 10.70.2.20.8080: Flags [P.], seq 892736633:892736711, ack 3118129684, wi
Each side picks a random initial number, and the other acknowledges
it plus one. The SYN counts as one byte, hence
ack 892736633 for seq 892736632.
Tried it: Who sends
FINfirst is a race with this server. In this run it was the client (packet 10), in an earlier run the server. HTTP/1.0 closes after the response, curl closes as soon as the response is complete, both in almost the same microsecond. On Day 5,TIME-WAITwas on the server because the server was faster there.
Save your progress? Sign up for free
Drill 4: Filtering, in two places
A BPF filter may also reach into the TCP header. This one only lets
through packets that set up or tear down a connection.
-i any captures on all of the router’s interfaces:
# on netz
sudo ip netns exec r tcpdump -i any -n -c 8 'tcp[tcpflags] & (tcp-syn|tcp-fin|tcp-rst) != 0' 2>/dev/null &
sleep 1
sudo ip netns exec cli curl -s -o /dev/null http://10.70.2.20:8080/
sleep 1; sudo pkill -INT -x tcpdump; waitExpected (truncated):
00:17:39.631094 eth1 In IP 10.70.1.10.53148 > 10.70.2.20.8080: Flags [S], seq 2891393417, win 64240, options [mss 1460,sackOK,TS
00:17:39.631113 eth2 Out IP 10.70.1.10.53148 > 10.70.2.20.8080: Flags [S], seq 2891393417, win 64240, options [mss 1460,sackOK,TS
00:17:39.631152 eth2 In IP 10.70.2.20.8080 > 10.70.1.10.53148: Flags [S.], seq 2162532257, ack 2891393418, win 65160, options [m
00:17:39.631185 eth1 Out IP 10.70.2.20.8080 > 10.70.1.10.53148: Flags [S.], seq 2162532257, ack 2891393418, win 65160, options [m
00:17:39.633584 eth2 In IP 10.70.2.20.8080 > 10.70.1.10.53148: Flags [F.], seq 203, ack 79, win 509, options [nop,nop,TS val 623
00:17:39.633593 eth1 Out IP 10.70.2.20.8080 > 10.70.1.10.53148: Flags [F.], seq 203, ack 79, win 509, options [nop,nop,TS val 623
00:17:39.633972 eth1 In IP 10.70.1.10.53148 > 10.70.2.20.8080: Flags [F.], seq 79, ack 204, win 501, options [nop,nop,TS val 251
00:17:39.633980 eth2 Out IP 10.70.1.10.53148 > 10.70.2.20.8080: Flags [F.], seq 79, ack 204, win 501, options [nop,nop,TS val 251
Every packet appears twice: In on one side of the
router, Out on the other, 10 to 30 microseconds later. This
is exactly how you prove that a router forwards. If the Out
line is missing, it dropped the packet (route, firewall,
rp_filter from Day 4).
A port where nothing is listening:
# on netz
sudo ip netns exec r tcpdump -i any -n -c 4 tcp port 8081 2>/dev/null &
sleep 1
sudo ip netns exec cli curl -sS http://10.70.2.20:8081/; echo "rc=$?"
waitExpected:
curl: (7) Failed to connect to 10.70.2.20 port 8081 after 0 ms: Couldn't connect to server
rc=7
00:18:08.356911 eth1 In IP 10.70.1.10.55972 > 10.70.2.20.8081: Flags [S], seq 1778869639, win 64240, options [mss 1460
00:18:08.356929 eth2 Out IP 10.70.1.10.55972 > 10.70.2.20.8081: Flags [S], seq 1778869639, win 64240, options [mss 1460
00:18:08.356955 eth2 In IP 10.70.2.20.8081 > 10.70.1.10.55972: Flags [R.], seq 0, ack 1778869640, win 0, length 0
00:18:08.356961 eth1 Out IP 10.70.2.20.8081 > 10.70.1.10.55972: Flags [R.], seq 0, ack 1, win 0, length 0
SYN in, RST back, after 0 milliseconds. The kernel of
srv answers itself when no program is listening. That is
“Connection refused”. A firewall that drops instead of rejecting would
look different: SYN in, nothing back, and curl waits until the
timeout.
The BPF filters you need most often:
| Filter | Matches |
|---|---|
host 10.70.2.20 |
everything from or to this address |
src net 10.70.1.0/24 |
everything from this network |
tcp port 8080 |
TCP with source or destination port 8080 |
icmp or arp |
pings, error messages and address resolution |
not port 22 |
everything except SSH, important on a server you are connected to via SSH |
tcp[tcpflags] & tcp-rst != 0 |
resets only |
Save your progress? Sign up for free
Drill 5: tshark analyzes
tshark reads abruf.pcap and explains every packet:
# on netz
cd ~/mitschnitt
tshark -r abruf.pcap 2>/dev/nullExpected:
1 0.000000 10.70.1.10 → 10.70.2.20 TCP 74 49110 → 8080 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM TSval=2514278254 TSecr=0 WS=128
2 0.000055 10.70.2.20 → 10.70.1.10 TCP 74 8080 → 49110 [SYN, ACK] Seq=0 Ack=1 Win=65160 Len=0 MSS=1460 SACK_PERM TSval=623318249 TSecr=2514278254 WS=128
3 0.000098 10.70.1.10 → 10.70.2.20 TCP 66 49110 → 8080 [ACK] Seq=1 Ack=1 Win=64256 Len=0 TSval=2514278254 TSecr=623318249
4 0.000320 10.70.1.10 → 10.70.2.20 HTTP 144 GET / HTTP/1.1
5 0.000332 10.70.2.20 → 10.70.1.10 TCP 66 8080 → 49110 [ACK] Seq=1 Ack=79 Win=65152 Len=0 TSval=623318250 TSecr=2514278255
6 0.002030 10.70.2.20 → 10.70.1.10 TCP 251 HTTP/1.0 200 OK [TCP segment of a reassembled PDU]
7 0.002080 10.70.1.10 → 10.70.2.20 TCP 66 49110 → 8080 [ACK] Seq=79 Ack=186 Win=64128 Len=0 TSval=2514278256 TSecr=623318251
8 0.002152 10.70.2.20 → 10.70.1.10 HTTP 83 HTTP/1.0 200 OK (text/html)
9 0.002183 10.70.1.10 → 10.70.2.20 TCP 66 49110 → 8080 [ACK] Seq=79 Ack=203 Win=64128 Len=0 TSval=2514278256 TSecr=623318251
10 0.002317 10.70.1.10 → 10.70.2.20 TCP 66 49110 → 8080 [FIN, ACK] Seq=79 Ack=203 Win=64128 Len=0 TSval=2514278257 TSecr=623318251
11 0.002322 10.70.2.20 → 10.70.1.10 TCP 66 8080 → 49110 [FIN, ACK] Seq=203 Ack=79 Win=65152 Len=0 TSval=623318252 TSecr=2514278256
12 0.002344 10.70.1.10 → 10.70.2.20 TCP 66 49110 → 8080 [ACK] Seq=80 Ack=204 Win=64128 Len=0 TSval=2514278257 TSecr=623318252
Two differences from tcpdump: the time counts from the first packet
(the whole request took 2.3 milliseconds), and Win is
already multiplied by the scaling factor: win 502 times
WS=128 gives Win=64256. tcpdump shows the raw
value from the header.
With -Y, tshark filters by protocol fields, with
-T fields it outputs only the fields you want.
-z computes statistics:
# on netz
tshark -r abruf.pcap -Y http -T fields -e frame.number -e ip.src -e http.request.method -e http.request.uri -e http.response.code -e http.server 2>/dev/null
tshark -r abruf.pcap -Y 'tcp.flags.syn==1' -T fields -e ip.src -e tcp.options.mss_val -e tcp.window_size_value -e tcp.options.wscale.multiplier 2>/dev/null
tshark -r abruf.pcap -q -z conv,tcp 2>/dev/nullExpected:
4 10.70.1.10 GET /
8 10.70.2.20 200 SimpleHTTP/0.6 Python/3.12.3
10.70.1.10 1460 64240 128
10.70.2.20 1460 65160 128
================================================================================
TCP Conversations
Filter:<No Filter>
| <- | | -> | | Total | Relative | Duration |
| Frames Bytes | | Frames Bytes | | Frames Bytes | Start | |
10.70.1.10:49110 <-> 10.70.2.20:8080 5 540 bytes 7 548 bytes 12 1088 bytes 0.000000000 0.0023
================================================================================
The second query shows what both sides negotiate during the handshake: maximum segment size 1460 bytes (MTU 1500 minus 40 bytes of IP and TCP header, Day 8) and window scaling times 128. The whole conversation as text, like “Follow TCP Stream” in Wireshark:
# on netz
tshark -r abruf.pcap -q -z follow,tcp,ascii,0 2>/dev/null | sed -n 7,20pExpected:
GET / HTTP/1.1
Host: 10.70.2.20:8080
User-Agent: curl/8.5.0
Accept: */*
185
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.12.3
Date: Tue, 24 Nov 2026 00:15:49 GMT
Content-type: text/html
Content-Length: 17
Last-Modified: Tue, 24 Nov 2026 00:15:21 GMT
Everything in plain text, as with any unencrypted protocol. With HTTPS you would only see the handshake and encrypted data here.
Save your progress? Sign up for free
Drill 6: Packet loss and retransmits
First the large file without interference, captured in the client.
-s 128 stores only the first 128 bytes of each packet, the
headers are enough for the analysis, and the file stays small:
# on netz
cd ~/mitschnitt
sudo ip netns exec cli tcpdump -i eth0 -n -s 128 -w gross-ok.pcap tcp port 8080 2>/dev/null &
sleep 1
sudo ip netns exec cli curl -s -o /dev/null -w '%{size_download} Byte in %{time_total} s\n' http://10.70.2.20:8080/gross.bin
sleep 1; sudo pkill -INT -x tcpdump; waitExpected:
3000000 Byte in 0.009588 s
Now the server loses 5% of its outgoing packets. You will get to know
tc netem in more detail on Day 9, one line is enough
here:
# on netz
sudo ip netns exec srv tc qdisc add dev eth0 root netem loss 5%
sudo ip netns exec cli tcpdump -i eth0 -n -s 128 -w gross-verlust.pcap tcp port 8080 2>/dev/null &
sleep 1
sudo ip netns exec cli curl -s -o /dev/null -w '%{size_download} Byte in %{time_total} s\n' http://10.70.2.20:8080/gross.bin
sleep 1; sudo pkill -INT -x tcpdump; wait
sudo ip netns exec srv tc qdisc del dev eth0 rootExpected:
3000000 Byte in 1.102355 s
5% loss, and the transfer takes 115 times as long. tshark counts what happened:
# on netz
for f in gross-ok.pcap gross-verlust.pcap; do echo "$f: $(tshark -r $f -Y tcp.analysis.retransmission 2>/dev/null | wc -l) Retransmits, $(tshark -r $f -Y tcp.analysis.duplicate_ack 2>/dev/null | wc -l) doppelte ACKs, $(tshark -r $f -Y tcp.analysis.lost_segment 2>/dev/null | wc -l) Lücken"; done
tshark -r gross-verlust.pcap -Y tcp.analysis.rto -T fields -e frame.number -e tcp.analysis.rto 2>/dev/null | head -3Expected:
gross-ok.pcap: 0 Retransmits, 0 doppelte ACKs, 0 Lücken
gross-verlust.pcap: 31 Retransmits, 60 doppelte ACKs, 57 Lücken
245 0.203288000
247 0.203432000
249 0.203499000
How to read this:
- Gaps (
Lücken;lost_segment, in expert mode “Previous segment(s) not captured”): tshark sees a sequence number higher than expected. The client never received the segments in between, so it did not capture them either. They would be in the server’s capture, because they were only lost on the way. - Duplicate ACKs (
doppelte ACKs): the client acknowledges the same number again and again to tell the server: something is missing. After three duplicate ACKs, the server resends immediately (fast retransmit). - RTO (retransmission timeout): the server only
notices some gaps after no acknowledgment has arrived for 200
milliseconds. That is the minimum value of
rtofromss -i(Day 5), and on a 10-millisecond transfer each of these pauses costs twenty times as much.
Tried it: In the undisturbed capture, the TCP segments are up to 65,160 bytes in size, far above the MTU of 1500. The server leaves splitting into 1500-byte packets to the network card (TSO/GSO, segmentation offload), and veth never splits but simply passes the large segments on. On real hardware, GRO (generic receive offload) on the receiver merges packets again before tcpdump sees them. A capture shows what the kernel sees, not necessarily what was on the wire. On Day 9 you turn this off and measure what it costs.
# on netz
tshark -r gross-ok.pcap -T fields -e tcp.len 2>/dev/null | sort -n | tail -1Expected: 65160
Save your progress? Sign up for free
Drill 7: The full window
A receiver that reads nothing for five seconds, like the faulty service from the Day 5 challenge. The client sends it 5 MB of zeros:
# on netz
cd ~/mitschnitt
sudo ip netns exec srv sudo -u ubuntu python3 -c 'import socket,time; s=socket.create_server(("0.0.0.0", 7100)); c=s.accept()[0]; time.sleep(5); [c.recv(65536) for _ in range(200)]; time.sleep(30)' &
sleep 1
sudo ip netns exec cli tcpdump -i eth0 -n -s 128 -w fenster.pcap tcp port 7100 2>/dev/null &
sleep 1
head -c 5000000 /dev/zero | sudo ip netns exec cli timeout 3 nc 10.70.2.20 7100 &
sleep 2
sudo ip netns exec srv ss -tn 'sport = :7100'
sudo ip netns exec cli ss -tn 'dport = :7100'Expected:
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
ESTAB 125976 0 10.70.2.20:7100 10.70.1.10:46010
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
ESTAB 0 365544 10.70.1.10:46010 10.70.2.20:7100
On the server, 126 KB sit in the receive buffer (Recv-Q)
that the program does not pick up. On the client, 366 KB wait in the
send buffer (Send-Q) that it cannot get rid of. In the
capture:
# on netz
sleep 2; sudo pkill -INT -x tcpdump; sleep 1
tshark -r fenster.pcap -Y 'tcp.analysis.zero_window || tcp.analysis.window_full' -T fields -e frame.number -e frame.time_relative -e ip.src -e tcp.window_size -e _ws.expert.message 2>/dev/nullExpected:
24 0.296786000 10.70.1.10 64256 TCP window specified by the receiver is now completely full
25 0.296949000 10.70.2.20 0 TCP Zero Window segment
27 0.512957000 10.70.2.20 0 TCP Zero Window segment
30 1.824911000 10.70.2.20 0 TCP Zero Window segment
After 0.3 seconds, the client has exhausted the window, and the
server reports Win=0: stop, my buffer is full. After that,
the client asks at growing intervals (0.5 s, 1.8 s …) whether there is
room again (zero window probe), and the server answers 0 every time.
This is the most important pattern when capturing slow applications:
zero window means the receiver cannot keep up. The
network is innocent, the culprit is the program on the side that reports
Win=0. Stop the hanging receiver:
# on netz
for p in $(pgrep -f '[c]reate_server'); do sudo kill $p; done
pgrep -f '[c]reate_server' || echo "beendet"Expected: beendet
Caution:
pkill -f create_serverin an SSH command line likessh netz 'pkill -f …'also hits your own shell, whose command line contains the pattern. The square brackets in[c]reate_serverprevent that: the pattern matches “create_server” but not itself.
Save your progress? Sign up for free
Drill 8: Ring buffer for long captures
A fault that occurs once a night needs a capture that runs for hours
without filling the disk. -C 1 starts a new file after 1
MB, -W 3 keeps at most three and then overwrites the
oldest:
# on netz
cd ~/mitschnitt
sudo ip netns exec srv tcpdump -i eth0 -n -Z ubuntu -w ring.pcap -C 1 -W 3 tcp port 8080 &
sleep 1
for i in 1 2 3; do sudo ip netns exec cli curl -s -o /dev/null http://10.70.2.20:8080/gross.bin; done
sleep 1; sudo pkill -INT -x tcpdump; wait
ls -l ring.pcap*Expected:
tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
526 packets captured
531 packets received by filter
5 packets dropped by kernel
-rw-r--r-- 1 ubuntu ubuntu 1053308 Nov 24 00:18 ring.pcap0
-rw-r--r-- 1 ubuntu ubuntu 1053848 Nov 24 00:18 ring.pcap1
-rw-r--r-- 1 ubuntu ubuntu 513572 Nov 24 00:18 ring.pcap2
9 MB transferred, 2.6 MB on disk: the last minutes before the stop.
-Z ubuntu is necessary because tcpdump drops its privileges
after starting and continues as the user tcpdump. That user
may not create new files in ~/mitschnitt, and the second
file would never come into being.
Tried it:
5 packets dropped by kernel: with three fast downloads over veth, tcpdump briefly could not keep up. For captures under load, add-B 8192(8 MB buffer) and a filter that records only what you need.
Save your progress? Sign up for free
Exercise: Speed drills
- In the router, capture exactly two ICMP packets on
eth2whileclipings once. Target time: 30 s - How many connection setups (SYN without ACK) does
abruf.pcapcontain? Target time: 30 s - Which HTTP status did the server send in
abruf.pcap, just the number? Target time: 30 s
Solution
# on netz
sudo ip netns exec r tcpdump -i eth2 -n -c 2 icmp 2>/dev/null &
sleep 1; sudo ip netns exec cli ping -c1 -q 10.70.2.20 >/dev/null; wait
tshark -r ~/mitschnitt/abruf.pcap -Y 'tcp.flags.syn==1 && tcp.flags.ack==0' 2>/dev/null | wc -l
tshark -r ~/mitschnitt/abruf.pcap -Y http.response -T fields -e http.response.code 2>/dev/nullExpected:
00:18:53.900568 IP 10.70.1.10 > 10.70.2.20: ICMP echo request, id 12572, seq 1, length 64
00:18:53.900639 IP 10.70.2.20 > 10.70.1.10: ICMP echo reply, id 12572, seq 1, length 64
1
200
Save your progress? Sign up for free
Challenge
A colleague has created a rule in the router:
# on netz
sudo ip netns exec r nft add table inet wand
sudo ip netns exec r nft add chain inet wand weiter '{ type filter hook forward priority 0; }'
sudo ip netns exec r nft add rule inet wand weiter tcp dport 8080 ip saddr 10.70.1.10 reject with tcp reset
sudo ip netns exec cli curl -sS -m 3 http://10.70.2.20:8080/; echo "rc=$?"Expected:
curl: (7) Failed to connect to 10.70.2.20 port 8080 after 0 ms: Couldn't connect to server
rc=7
The web server operator swears their service is running. Prove with tcpdump that the reset does not come from the server, even though it carries the server’s source address. Also find a feature by which the client alone can already recognize the forged reset.
Hint: Capture in the client and in the server at the
same time. For the feature: compare -v and the resets from
port 8080 and 8081.
Solution
# on netz
sudo ip netns exec srv tcpdump -i eth0 -n -c 3 tcp port 8080 &
sudo ip netns exec cli tcpdump -i eth0 -n -c 2 tcp port 8080 2>/dev/null &
sleep 1; sudo ip netns exec cli curl -s -m 3 http://10.70.2.20:8080/; sleep 2; sudo pkill -INT -x tcpdump; waitExpected (truncated):
tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
00:18:10.370812 IP 10.70.1.10.45208 > 10.70.2.20.8080: Flags [S], seq 1367850177, win 64240, options [mss 1460,sackOK,TS val 2514418922 ecr
00:18:10.370858 IP 10.70.2.20.8080 > 10.70.1.10.45208: Flags [R.], seq 0, ack 1367850178, win 0, length 0
0 packets captured
0 packets received by filter
0 packets dropped by kernel
The client gets a reset from 10.70.2.20, while not even
the SYN arrives at the server. The router answered, with the server’s
address as the source. reject with tcp reset does exactly
that: to the client it looks as if nothing were listening there. The
feature:
# on netz
sudo ip netns exec cli tcpdump -i eth0 -n -v -c 4 'tcp[tcpflags] & tcp-rst != 0' 2>/dev/null &
sleep 1
sudo ip netns exec cli curl -s http://10.70.2.20:8080/
sudo ip netns exec cli curl -s http://10.70.2.20:8081/
sleep 1; sudo pkill -INT -x tcpdump; waitExpected (truncated):
00:18:20.711651 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 40)
10.70.2.20.8080 > 10.70.1.10.39650: Flags [R.], cksum 0x17c4 (incorrect -> 0x513f), seq 0, ack 1067732177,
00:18:20.762364 IP (tos 0x0, ttl 63, id 0, offset 0, flags [DF], proto TCP (6), length 40)
10.70.2.20.8081 > 10.70.1.10.44196: Flags [R.], cksum 0xc267 (correct), seq 0, ack 2478536142, win 0, leng
The TTL: the real reset from srv starts
at 64 and loses one in the router, so it arrives with
ttl 63. The forged one originates in the router itself and
arrives with ttl 64. A packet that has one hop less behind
it than its alleged sender comes from somewhere along the way.
By the way, the cksum … incorrect on the forged packet
is not an error: the router leaves computing the checksum to the network
card (checksum offload), and veth never recalculates it. Wrong checksums
in the sender’s capture are almost always offload, not corruption.
Remove the rule:
# on netz
sudo ip netns exec r nft delete table inet wand
sudo ip netns exec cli curl -s http://10.70.2.20:8080/Expected: Hallo Mitschnitt
Save your progress? Sign up for free
Checkpoint
# on netz
ls ~/mitschnitt/*.pcap | wc -lExpected: 4 (abruf,
gross-ok, gross-verlust,
fenster)
# on netz
tshark -r ~/mitschnitt/gross-verlust.pcap -Y tcp.analysis.retransmission 2>/dev/null | wc -lExpected: a number greater than 0, in
my run 31
# on netz
sudo ip netns exec srv tc qdisc show dev eth0
sudo ip netns exec r nft list ruleset | wc -lExpected:
qdisc noqueue 0: root refcnt 2
0
Clean-up
# on netz
pkill -u ubuntu -f '[h]ttp.server'
sudo ip netns del r
sudo ip netns del cli
sudo ip netns del srv
rm -rf ~/mitschnitt
ip netns list | wc -l
pgrep -u ubuntu -af '[h]ttp.server|[t]cpdump' || echo "keine Labor-Prozesse"
sysctl net.ipv4.ip_forwardExpected:
0
keine Labor-Prozesse
net.ipv4.ip_forward = 0
If you want to keep captures, copy them away beforehand. In Wireshark on your own machine they show the same as tshark, just with colors.
Practice note
- First think about where, then capture. A capture at both ends and, if possible, in the middle answers the question “Where does the packet go missing?” in minutes. A capture only at the client simply leaves lost packets out.
- Zero window belongs to the application, retransmits to the
network. If the receiver reports
Win=0, its program cannot keep up. If retransmits and duplicate ACKs pile up, packets are being lost on the way. - Always filter and limit on servers.
not port 22,-cor-C/-W,-sfor headers only. An unfiltered capture on a busy server costs CPU, fills the disk and contains data that does not belong in a file. - Wrong checksums and giant packets are offload. On
the sender, the network card computes and splits (checksum offload,
TSO/GSO), on the receiver GRO merges. If you need a true picture of the
wire, capture on a mirror port or turn offloads off with
ethtool -K(Day 9).