The Layers
Four headers, wrapped — and your first packet capture.
Every connection you have opened looks like this underneath.
What TCP/IP is
Two protocols, always used together. IP moves a packet between machines; TCP turns those packets into an ordered, reliable stream.
Four headers
Your bytes travel wrapped in headers, each added by the layer below:
| Layer | Header says | Example |
|---|---|---|
| Link | which machine on this wire | Ethernet |
| IP | which machine on the internet | 172.17.0.4 |
| TCP | which program, and where in the stream | port 9000 |
| Application | whatever you decided | HTTP |
Every question — which machine, which program, which byte — is a field in one of them.
A container to work in
tcpdump needs root. One container gives a shell where it, nc, ss and ping all work:
docker run -it --rm --cap-add=NET_ADMIN --cap-add=NET_RAW \
nicolaka/netshootEvery capture below uses a helper that runs tcpdump while a command executes, then strips
the three things that differ per run — timestamp, client port, sequence numbers:
tcpdump $1 > /tmp/cap.out 2>/dev/null &
TD=$!
sleep 0.8
sh -c "$2" >/dev/null 2>&1
sleep 1.2
kill $TD 2>/dev/null; wait $TD 2>/dev/null
grep -E '^[0-9]' /tmp/cap.out | sed -E "
s/^[0-9:.]+ //
s/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\.[0-9]{5}/\1.CLIENT/g
s/seq [0-9]{4,}(:[0-9]+)?/seq X/
s/ack [0-9]{6,}/ack X/
s/, options \[[^]]*\]//
s/win [0-9]+/win W/
s/ id [0-9]+,/ id N,/"Your first capture
Start something listening, then connect and watch:
nc -l -p 9000 -k &cap "-i lo -n -c 1 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000"One packet — the first your machine sends on every connection it makes.
Reading one line
Six parts, left to right:
| Part | Here | Means |
|---|---|---|
IP |
IP |
an IPv4 packet |
| source | 127.0.0.1.CLIENT |
address, then port |
> |
> |
direction |
| destination | 127.0.0.1.9000 |
address, then port |
Flags |
[S] |
SYN — “I want to connect” |
length |
0 |
bytes of your data, none yet |
Three packets open a connection. Capture all three:
cap "-i lo -n -c 3 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000"[S] out, [S.] back, [.] out. Chapter 3 is about those three lines.
The IP header
-v prints the envelope that TCP travels inside:
cap "-i lo -n -v -c 1 tcp port 9000" \
"echo hi | nc -w 1 127.0.0.1 9000" | head -1proto TCP (6) says what is inside. Chapter 6 takes the rest apart.
Where HTTP sits
An HTTP request is bytes inside these packets. Serve a response, then capture a curl:
while true; do printf 'HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi' \
| nc -l -p 9100 -q 1 >/dev/null 2>&1; done &cap "-i lo -n -c 4 tcp port 9100" "curl -s -m 2 127.0.0.1:9100/"Three packets of handshake, then length 78 — the whole HTTP request as data. cap prints
summaries, so seeing those bytes needs tcpdump -A directly:
grep -E 'Host:|User-Agent' /tmp/b.txt