Skip to content
Sign inGet all access
Chapter 1·8 minFree

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:

terminal
docker run -it --rm --cap-add=NET_ADMIN --cap-add=NET_RAW \
    nicolaka/netshoot
Output$ docker run -it --rm --cap-add=NET_ADMIN --cap-add=NET_RAW \ nicolaka/netshootbash-5.2#

Every 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:

cap
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:

terminal
nc -l -p 9000 -k &
Output$ nc -l -p 9000 -k &$ cap "-i lo -n -c 1 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000"
cap "-i lo -n -c 1 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000"
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [S], seq X, win W, length 0

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"
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [S], seq X, win W, length 0IP 127.0.0.1.9000 > 127.0.0.1.CLIENT: Flags [S.], seq X, ack X, win W, length 0IP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [.], ack 1, win W, length 0

[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 -1
OutputIP (tos 0x0, ttl 64, id N, offset 0, flags [DF], proto TCP (6), length 60)

proto 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:

terminal
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 &
Output$ 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/"
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9100: Flags [S], seq X, win W, length 0IP 127.0.0.1.9100 > 127.0.0.1.CLIENT: Flags [S.], seq X, ack X, win W, length 0IP 127.0.0.1.CLIENT > 127.0.0.1.9100: Flags [.], ack 1, win W, length 0IP 127.0.0.1.CLIENT > 127.0.0.1.9100: Flags [P.], seq 1:79, ack 1, win W, length 78

Three packets of handshake, then length 78 — the whole HTTP request as data. cap prints summaries, so seeing those bytes needs tcpdump -A directly:

terminal
grep -E 'Host:|User-Agent' /tmp/b.txt
Output$ tcpdump -i lo -n -A -c 4 'tcp port 9100' > /tmp/b.txt 2>/dev/null &$ curl -s -m 2 127.0.0.1:9100/$ grep -E 'Host:|User-Agent' /tmp/b.txtHost: 127.0.0.1:9100User-Agent: curl/8.23.0