Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

A Defender's Map of the Network Layers

See which attacks and defences live at each network layer, how a packet is wrapped in headers, and parse a real packet layout with Python's struct module.

To defend a network you need a mental map of how data travels. The classic OSI model has seven layers and the practical TCP/IP model has four. You do not need to memorise every protocol; you need to know, for each layer, what can go wrong and what control belongs there. A firewall that only understands IP addresses cannot stop a malicious web request, and a web application firewall cannot stop someone plugging a rogue device into a switch. Matching the control to the layer is the skill.

This lesson walks up the stack, shows how a packet is built from nested headers, and then has you decode a packet by hand in Python. The headers are the data that firewalls, routers and intrusion detection systems actually look at.

The layers and what lives there

OSI layer Typical content Example attacks Example defences
7 Application HTTP, DNS, SMTP, SSH SQL injection, phishing links, DNS poisoning Web application firewall, input validation, DNSSEC
6 Presentation TLS, encoding Downgrade to weak ciphers, bad certificates Strong TLS configuration, certificate checks
5 Session Login sessions, tokens Session hijacking Short sessions, secure cookies
4 Transport TCP, UDP ports SYN floods, port scans Stateful firewall, SYN cookies, rate limits
3 Network IP addresses, routing IP spoofing, route hijacking Ingress filtering, ACLs, IPsec
2 Data link Ethernet, Wi-Fi, MAC addresses ARP spoofing, MAC flooding, rogue access points Port security, dynamic ARP inspection, WPA3
1 Physical Cables, radio Tapping, jamming, theft Locked rooms, shielded cables

In the TCP/IP model, layers 5 to 7 are merged into one application layer, layer 4 is transport, layer 3 is internet, and layers 1 and 2 form the link layer. Defenders usually still talk in OSI numbers, so "a layer 7 attack" means an application-level one.

flowchart TD
    A["Application: HTTP request"] --> B["Transport: TCP segment, port 443"]
    B --> C["Internet: IP packet, addresses"]
    C --> D["Link: Ethernet frame, MAC addresses"]
    D --> E["Physical: bits on the wire"]

Each layer wraps the data from the layer above in its own header. This wrapping is called encapsulation.

Encapsulation: headers inside headers

When your browser sends a request, the HTTP text is wrapped in a TCP header (ports, sequence numbers, flags), then in an IP header (source and destination addresses), then in an Ethernet header (MAC addresses). A router reads and rewrites only the layers it needs. A switch looks at the Ethernet header, a router at the IP header, a stateful firewall at IP and TCP, and a deep-inspection device at everything.

This is why encryption changes what defenders see. With TLS, the HTTP content is opaque, but the IP and TCP headers stay readable, because routers need them. A VPN goes further and hides even the inner IP header inside a new one.

Decoding a packet by hand

Network protocols define headers as fixed byte layouts. Python's struct module reads them. The next program builds a small IPv4 + TCP packet in memory (no network is used), then parses it back and prints the fields a firewall would care about. Study how each field maps to bytes; you will use the same idea in the packet analysis lesson.

Python Build and parse an IPv4 + TCP header

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A single SYN with no ACK is the first step of the TCP handshake. Remember that shape: many SYNs with no follow-up is how a scanner or a flood looks in logs.

Which layer does each control read?

Putting controls at the right layer avoids both gaps and waste. A packet filter is fast but only understands headers. A proxy or web application firewall understands application content but is slower. The next program shows the idea with a tiny model: the same request is inspected by three kinds of device, each seeing a different slice.

Python What each device can see

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

If traffic is encrypted end to end, even a layer 7 device sees only ciphertext unless it terminates TLS itself. That is a trade-off between visibility and privacy that comes back in the monitoring chapter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. ARP spoofing mainly abuses which layer?

    ARP maps IP addresses to MAC addresses on a local link, so it operates at layer 2 and is attacked there.
  2. 2. What is encapsulation?

    Each layer adds its own header around the data from the layer above, so an HTTP request ends up inside TCP, inside IP, inside Ethernet.
  3. 3. A packet-filter firewall that only reads IP and TCP headers can NOT directly detect which of these?

    The injection string is application data (layer 7). A header-only filter cannot see it, so you need a web application firewall or application-level checks.
  4. 4. A TCP segment with only the SYN flag set usually means

    SYN starts the three-way handshake. The reply is SYN+ACK, then the client sends ACK.

Summary

  • Know what can go wrong at each OSI layer and match the control to that layer.
  • Encapsulation nests application data inside TCP, IP and Ethernet headers.
  • Devices see only the layers they parse: switches see layer 2, routers layer 3, firewalls layers 3 to 4 (or 7 with deep inspection).
  • Encryption hides content but not the IP and TCP headers; a VPN hides more.
  • struct lets you decode headers by hand, which is how packet tools work.

Next: Defence in Depth and Core Principles