Careers Post-AI
Security: all chapters

SecurityPart 1: Foundations

Chapter 2Networking through an attacker's eyes

35 min read, 2 labs, Beginner

In April 2018 attackers hijacked internet routes to Amazon's DNS servers for about two hours, answered queries for a cryptocurrency wallet site with their own IP address, and took roughly $150,000 from users who clicked through a certificate warning. Routing, DNS and TLS each did exactly what they were designed to do, and the attack still worked. This chapter walks the network stack layer by layer and shows, at each one, what an attacker can see, forge or take over.

After this chapter you can

  • Explain encapsulation and identify which addresses and fields each layer exposes
  • Describe how ARP spoofing, IP spoofing and DNS cache poisoning work and what stops them
  • Trace a TCP connection through its states and explain how port scanners interpret responses
  • Explain what TLS protects and what it still leaks to an observer
  • Capture and read real traffic with tcpdump and Wireshark
  • Scan your own containers with nmap and interpret open, closed and filtered results

Layers and what each one exposes

Network protocols are stacked. Your application hands bytes to TCP, TCP adds a header and hands a segment to IP, IP adds a header and hands a packet to Ethernet or Wi-Fi, which adds a header and puts a frame on the wire. Each layer only reads its own header, which is why each layer can be attacked separately.

LayerExample protocolsAddressWhat an attacker on the path can do
Link (L2)Ethernet, Wi-Fi, ARPMAC addressSpoof MACs, poison ARP caches, sniff a shared segment
Network (L3)IP, ICMP, BGPIP addressForge source IPs, hijack routes, map hosts
Transport (L4)TCP, UDPPortScan ports, inject resets, flood half-open connections
Application (L7)DNS, HTTP, TLS, SSHNames, URLsPoison caches, read plaintext, strip or downgrade encryption

The key question at every layer is whether the protocol authenticates its data. Ethernet, ARP, IP, UDP and classic DNS do not. Anyone who can put bytes on the right wire can claim to be anyone. Authentication only appears higher up, in TLS, SSH and signed DNS.

Ethernet and ARP spoofing

On a local network, machines deliver frames by MAC address. To send an IP packet to the router at 192.168.1.1, your laptop must learn the router's MAC, so it broadcasts an ARP (Address Resolution Protocol) request: who has 192.168.1.1? The owner replies, and your laptop caches the answer.

ARP has no authentication, and hosts accept replies they never asked for. An attacker on the same segment sends forged replies to the victim saying "192.168.1.1 is at my MAC" and to the router saying "the victim is at my MAC." Both now send their traffic through the attacker, who forwards it so nothing looks broken.

This is an on-path (formerly man-in-the-middle) position. From there the attacker can read any plaintext, rewrite responses, or block traffic. On public Wi-Fi the same position is reached even more easily by running a rogue access point with a familiar network name.

DefenseHow it works
Dynamic ARP inspectionManaged switches drop ARP replies that do not match the DHCP lease table
Port security and 802.1XOnly authenticated devices get a switch port at all
Client isolation on Wi-FiClients cannot talk to each other directly
Encryption above L2TLS or a VPN makes the on-path position far less valuable

The last row is the important one. You will not control every network your users join, so the durable defense is assuming the local network is hostile and encrypting everything above it.

IP, routing and spoofing

IP delivers packets between networks, hop by hop, using routing tables. The source address in an IP header is just a field the sender fills in. Nothing on the path checks it by default, so an attacker can send packets with any source address they like.

Spoofing is useful for one-way attacks. The attacker never sees replies, because those go to the forged address. That makes it ideal for reflection and amplification DDoS: send a small UDP query to a DNS or NTP server with the victim's address as the source, and the server sends a much larger response to the victim. Ingress filtering at ISPs (described in BCP 38) blocks packets whose source address could not legitimately come from that customer, but it is not universal.

At internet scale, routing itself runs on trust. BGP (Border Gateway Protocol) lets networks announce which IP ranges they can reach, and neighbors largely believe them. The 2018 attack in the opening announced a more specific route for Amazon's DNS address space, and more specific routes win. RPKI (Resource Public Key Infrastructure) lets networks cryptographically sign which provider may announce their ranges, and adoption has grown steadily, but route hijacks and leaks still happen.

TCP: the handshake, state and port scanning

TCP gives applications a reliable, ordered byte stream on top of unreliable IP. It starts every connection with a three-way handshake. Each side picks a random 32-bit initial sequence number (ISN), and every later byte is numbered from it.

The random ISN is a security feature. An attacker who is not on the path cannot see the numbers, so they cannot inject data into a connection or complete a handshake with a spoofed source IP. In the 1990s ISNs were predictable, and attackers used that to hijack sessions blindly. Today, injecting into a TCP stream practically requires an on-path position.

Two classic TCP attacks still matter:

  • SYN flood: send masses of SYNs from spoofed addresses so the server fills its table of half-open connections. SYN cookies defend by encoding the connection state into the server's ISN, so nothing is stored until the final ACK arrives.
  • RST injection: an on-path device sends a forged reset to kill a connection. Some national censorship systems do exactly this.

Port scanning uses the handshake's rules to learn what a host runs. A closed port answers a SYN with a RST. An open port answers with SYN-ACK. A firewall that silently drops packets produces no answer at all.

Scan typeWhat nmap sendsOpenClosedFiltered
SYN scan, -sS (root)SYN, then RST instead of completingSYN-ACKRSTNo reply or ICMP unreachable
Connect scan, -sTFull handshake via the OSConnection succeedsConnection refusedTimeout
UDP scan, -sUEmpty or protocol-specific UDP datagramA UDP replyICMP port unreachableNo reply (ambiguous with open)

UDP and DNS

UDP is connectionless: one datagram, no handshake, no sequence numbers. That makes it fast and simple, and also trivially spoofable, because a reply only has to match the right IP addresses and ports to be accepted.

DNS runs mostly over UDP port 53. When you visit a site, a stub resolver on your machine asks a recursive resolver (your ISP, your company, or a public one), which walks the hierarchy from the root servers down to the domain's authoritative server and caches the answer for its TTL.

Cache poisoning means getting a forged answer into the recursive resolver's cache, so every user of that resolver goes to the attacker. The resolver accepts the first reply that matches its query's 16-bit transaction ID and source port. In 2008 Dan Kaminsky showed attackers could trigger endless queries for random subdomains and race forged replies against each one, which made guessing 16 bits practical. Resolvers responded by randomizing the source port too, adding roughly 16 more bits of guesswork.

ProblemMitigationWhat it does and does not do
Off-path forged repliesSource port randomizationRaises the guessing cost, does not authenticate
Any forged recordDNSSECSigns records so resolvers can verify them, adoption is uneven
Snooping and tampering between you and the resolverDNS over HTTPS or TLSEncrypts the last hop, the resolver still sees everything
Hijacked registrar or DNS accountMFA, registry lockMost real DNS takeovers happen here, not on the wire

DNS is also an attacker's tool. Malware uses it for command and control and data exfiltration by encoding data into subdomain queries, because DNS is allowed out of almost every network. Watching DNS logs is one of the cheapest detections you will ever deploy.

HTTP, TLS and what still leaks

Plain HTTP sends everything in cleartext: URLs, headers, cookies, form bodies. Anyone on the path reads it and can modify it. TLS wraps HTTP in an encrypted, authenticated channel, and the certificate proves the server owns the name you typed. The mechanics are covered in detail in the chapter on applied cryptography.

TLS protects content, not everything. An observer still sees:

  • The IP addresses and ports of both ends.
  • The server name in the TLS ClientHello (SNI), unless Encrypted Client Hello is used, which is still being rolled out.
  • The DNS lookup that preceded the connection, unless it used encrypted DNS.
  • Timing and sizes of each record, which can reveal which page you loaded.

Two failure modes matter more than broken crypto. SSL stripping works when a user types a bare domain, the first request goes over HTTP, and an on-path attacker keeps the victim on HTTP while talking HTTPS to the real server. HSTS (HTTP Strict Transport Security) tells browsers to only ever use HTTPS for a domain, and the preload list bakes that in before the first visit.

The second failure mode is users clicking through certificate warnings, exactly as in the 2018 attack. Browsers have made warnings harder to bypass for this reason, and HSTS removes the bypass button entirely.

NAT and firewalls

A firewall decides which packets pass based on rules. Early firewalls were stateless and matched each packet in isolation. Modern ones are stateful: they track connections, so a reply is allowed only if it belongs to a connection someone inside started. Linux implements this with netfilter, configured through nftables or iptables, using the connection tracking table.

NAT (Network Address Translation) lets many private addresses share one public address by rewriting source addresses and ports on the way out and keeping a table to reverse it on the way back. It blocks unsolicited inbound connections as a side effect, which people mistake for security.

What NAT and stateful firewalls stop

  • Unsolicited inbound connections to internal hosts
  • Direct scanning of private address space from outside

What they do not stop

  • Malware inside calling out to its controller
  • Phishing, drive-by downloads and malicious updates
  • Lateral movement between hosts on the same inside network

This is why mature networks filter egress (outbound) traffic too and segment internal networks, as covered in the chapter on network defense and zero trust. Most intrusions involve an inside machine connecting out, and a firewall that allows all outbound traffic will not even slow that down.

Key terms

Encapsulation
Wrapping data in a header for each layer, so each layer reads only its own header.
ARP spoofing
Sending forged ARP replies so that traffic for one IP address goes to the attacker's MAC address.
On-path attacker
An attacker positioned between two parties who can read, modify or drop their traffic.
IP spoofing
Sending packets with a forged source IP address, useful for one-way attacks such as reflection DDoS.
Initial sequence number
The random starting value for TCP byte numbering, which prevents blind injection by off-path attackers.
SYN scan
A port scan that sends SYN packets and classifies ports by SYN-ACK, RST or silence without completing the handshake.
DNS cache poisoning
Planting a forged DNS answer in a resolver's cache so all its users are sent to the wrong address.
HSTS
A response header that tells browsers to use only HTTPS for a domain, defeating SSL stripping.
Stateful firewall
A firewall that tracks connections and allows replies only for connections that were legitimately started.
Lab

Watch TCP, HTTP and DNS on the wire

Beginner, 45 min

Goal: capture a real TCP handshake, a plaintext HTTP request and a DNS lookup between containers you own, and read them in Wireshark.

You need: Docker, and Wireshark (brew install --cask wireshark on macOS, sudo apt install wireshark on Debian or Ubuntu, or the installer from wireshark.org on Windows). Everything runs on a private Docker network on your machine.

  1. Create a lab network and a web server:

    docker network create seclab
    docker run -d --name web --network seclab nginx:alpine
  2. Start a capture inside the web server's network namespace, using the netshoot toolbox image. Sharing the namespace means you see exactly what web sees:

    mkdir -p captures
    docker run --rm -d --name sniff --network container:web \
      --cap-add NET_ADMIN --cap-add NET_RAW \
      -v "$PWD/captures":/cap nicolaka/netshoot \
      tcpdump -i eth0 -w /cap/web.pcap
  3. From a separate client container, make a DNS lookup and an HTTP request with a fake password in it:

    docker run --rm --network seclab nicolaka/netshoot \
      sh -c 'dig web +short; curl -si "http://web/login?user=alice&password=hunter2" | head -1'

    You should see the container's IP address and then HTTP/1.1 404 Not Found (nginx has no login page, which is fine).

  4. Stop the capture and open it:

    docker stop sniff
    open captures/web.pcap        # macOS; on Linux run: wireshark captures/web.pcap
  5. In Wireshark, apply the display filter tcp.flags.syn == 1 and find the SYN and SYN-ACK. Note the raw sequence numbers (Edit, Preferences, Protocols, TCP, untick relative sequence numbers to see the real random values).

  6. Clear the filter, right-click any HTTP packet and choose Follow, TCP Stream. You should see GET /login?user=alice&password=hunter2 in readable text.

  7. Repeat a quick capture on the client side with tcpdump -n port 53 in the client container to see the DNS query to Docker's embedded resolver at 127.0.0.11. Docker answers names inside its own network namespace, so you see the query and answer leave and return locally.

Check your work: you can point to the three handshake packets with their sequence and acknowledgement numbers, and you can show the password in cleartext in the followed TCP stream.

Stretch: run a second nginx container configured for HTTPS with a self-signed certificate, repeat the capture, and confirm you can see the SNI in the ClientHello but not the URL or password. Then, on this lab network only, try arpspoof (from the dsniff package in a container with NET_ADMIN) between two containers and watch the ARP cache change with ip neigh.

Lab

Scan your own containers with nmap

Beginner, 40 min

Goal: discover hosts and services on a network you built, and connect each nmap result to the packets that produced it.

You need: Docker and the seclab network from the previous lab. All targets are containers you start yourself; never point these commands at anything else.

  1. Start a few target services:

    docker run -d --name cache --network seclab redis:7-alpine
    docker run -d --name db --network seclab -e POSTGRES_PASSWORD=lab postgres:16-alpine
  2. Open a shell in a scanner container:

    docker run --rm -it --network seclab nicolaka/netshoot
  3. Find live hosts. Get your subnet with ip -4 addr show eth0 (often 172.18.0.0/16 or similar), then:

    nmap -sn 172.18.0.0/24

    Expect a list of hosts up, including web, cache and db by name.

  4. Run a SYN scan with service detection on all ports:

    nmap -sS -sV -p- --reason web cache db

    Expect 80/tcp open http nginx, 6379/tcp open redis and 5432/tcp open postgresql, each with reason syn-ack. Every other port shows as closed with reason reset.

  5. Watch the mechanics. In a second terminal, capture inside the cache container's namespace while you scan two ports:

    docker run --rm --network container:cache --cap-add NET_RAW nicolaka/netshoot \
      tcpdump -n -c 10 'tcp port 6379 or tcp port 6380'

    Then in the scanner run nmap -sS -p 6379,6380 cache. You should see SYN, SYN-ACK, RST for 6379 and SYN, RST-ACK for 6380.

  6. Compare with a connect scan, nmap -sT -p 6379 cache, and note that the handshake now completes with a final ACK before the reset.

  7. Ask redis what it reveals to an unauthenticated client: redis-cli -h cache INFO server | head. If netshoot lacks redis-cli, use docker exec cache redis-cli INFO server. Note the version and OS details, the reconnaissance an attacker would collect next.

  8. Clean up: docker rm -f web cache db; docker network rm seclab.

Check your work: you have a scan report listing three open services with versions, and a tcpdump excerpt showing the different packet sequences for an open and a closed port.

Stretch: add a firewall rule inside a target (docker run --cap-add NET_ADMIN and iptables -A INPUT -p tcp --dport 8080 -j DROP), scan it again, and explain why that port now shows as filtered and why the scan of it took longer.

Check yourself

An attacker on the same coffee-shop Wi-Fi poisons your ARP cache. You browse a site that uses HTTPS with HSTS. What can the attacker most realistically learn?

Why can an off-path attacker easily spoof UDP-based DNS queries for a reflection attack, but not inject data into an existing TCP session?

nmap reports a TCP port as filtered rather than closed. What did it observe?

After the Kaminsky disclosure in 2008, resolvers randomized their source ports. What did that change achieve?

A company says its servers are safe because they sit behind NAT with no inbound port forwards. Which threat does this leave fully open?

Recap

  • Each layer has its own header and its own attacks, and the lower layers authenticate nothing.
  • ARP spoofing gives a local attacker an on-path position; encryption above it is the durable defense.
  • Random TCP sequence numbers stop blind injection, and scanners classify ports by SYN-ACK, RST or silence.
  • DNS answers are only as trustworthy as the resolver and the account that controls the domain.
  • TLS protects content but leaks metadata, and HSTS closes the downgrade and click-through gaps.
  • NAT is not a security control; filter egress and segment the inside.

Go deeper