Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 80 ยท Network Security

Networking Essentials for Defenders

Learn the networking basics every defender needs: TCP/IP layers, ports, DNS, subnets and CIDR with Python's ipaddress module, and where attacks happen at each layer.

You cannot defend a network you do not understand. Firewalls, intrusion detection and incident response all assume you can read an address, name a port, and explain what happens between typing a URL and seeing a page. This lesson is a defender's tour of the essentials, not a full networking course: just the parts that decide what can be attacked and how it can be watched.

For each piece, we point out where attackers operate. Later lessons then build on these facts: Firewalls, Segmentation and Zero Trust uses addresses and ports in rules, and Common Network Attacks and Defenses names the attacks that abuse each layer.

Layers: how data travels

Networking is organized in layers. Each layer wraps the data of the layer above with its own header, like envelopes inside envelopes. The practical TCP/IP model has four layers:

Layer Job Examples Typical attacks
Application The conversation itself HTTP, DNS, SMTP, SSH Injection, phishing, abuse of protocol features
Transport End-to-end delivery between programs TCP, UDP (ports) Port scans, SYN floods
Internet Addressing and routing between networks IP, ICMP IP spoofing, route hijacking
Link Delivery on the local network Ethernet, Wi-Fi, ARP ARP spoofing, sniffing, rogue access points
flowchart TB
    A["Application<br/>HTTP request"] --> T["Transport<br/>TCP segment: ports"]
    T --> I["Internet<br/>IP packet: addresses"]
    I --> L["Link<br/>Ethernet frame: MAC addresses"]
    L --> W(("Wire / Wi-Fi"))

Each layer adds a header on the way down and removes it on the way up.

A defender's rule of thumb: a control placed at one layer only sees that layer and the ones above. A basic packet filter sees addresses and ports but not the HTTP request inside. Only an application-aware control can judge the request itself. Encryption such as TLS hides the application layer from the network, which is good for privacy and bad for simple monitoring.

IP addresses, subnets and CIDR

Every device on a network has an IP address. IPv4 addresses are 32 bits, written as four numbers, such as 192.168.1.10. IPv6 uses 128 bits and looks like 2001:db8::1. An address is split into a network part and a host part. The split is written in CIDR notation: 192.168.1.0/24 means the first 24 bits identify the network, which leaves 8 bits for hosts, so 256 addresses (254 usable, because the first is the network address and the last the broadcast address).

Some ranges are private and are never routed on the public internet: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Home and office networks use them together with NAT. Others are special: 127.0.0.0/8 is loopback (the machine itself) and 169.254.0.0/16 is link-local, used when no address was assigned. Knowing these ranges matters for security. A server-side request that tries to reach 169.254.169.254 or 127.0.0.1 is a typical SSRF attempt (SSRF, Unsafe Deserialization and XXE).

Python's standard ipaddress module understands all of this, so you do not have to do bit arithmetic yourself:

Python Subnets with the ipaddress module

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.

Addresses are grouped into subnets for a reason. Devices on the same subnet talk directly; traffic between subnets passes a router, and that is a natural place to put filtering rules. Splitting a flat network into smaller ones is the idea behind segmentation, which you'll meet in the next lesson.

Ports and TCP

An IP address finds the machine; a port (0 to 65535) finds the program on it. A web server listens on 443, SSH on 22, a database on 5432 or 3306. Ports below 1024 are traditionally reserved for well-known services. When you browse, your computer picks a random high ephemeral port as the source of each connection.

TCP gives a reliable, ordered stream. Before any data moves, the two sides perform the three-way handshake:

sequenceDiagram
    participant C as Client
    participant S as Server (port 443)
    C->>S: SYN (let's talk)
    S->>C: SYN-ACK (ok, let's talk)
    C->>S: ACK (connected)
    Note over C,S: Data flows until FIN or RST closes the connection

A scanner only needs the first reply: SYN-ACK means the port is open, RST means it is closed, silence often means a firewall is dropping packets.

UDP has no handshake and no delivery guarantee. It is faster and is used for DNS, video calls and VPNs. Because there is no handshake, UDP source addresses are easy to forge, which is why UDP services are popular in amplification attacks.

For defenders, the list of open ports is the attack surface. Every listening service is code that can have bugs. The rule is to expose only what you need and to know exactly what that is. This small model of a port inventory shows the idea:

Python Auditing a port inventory

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.

DNS: the internet's phone book

People use names; networks use addresses. The Domain Name System translates between them. When your laptop asks for shop.example, a recursive resolver (usually run by your ISP or a public provider) walks the hierarchy: root servers, then the .example servers, then the authoritative server for the domain, and caches the answer for the record's TTL (time to live). Important record types:

  • A / AAAA: name to IPv4 / IPv6 address.
  • CNAME: alias to another name.
  • MX: where to deliver mail for the domain.
  • TXT: free text, used for email authentication (SPF, DKIM, DMARC) and domain ownership checks.
  • NS: which servers are authoritative.

DNS was designed without authentication, so a forged answer looks the same as a real one. Attackers abuse it in several ways: poisoning caches, hijacking a domain's registration, or using it as a covert channel to sneak data out. Defenses include DNSSEC (signed records), encrypted transport (DNS over HTTPS or TLS), registrar locks and monitoring of unusual lookups. DNS logs are a defender's goldmine: nearly every piece of malware must look up a name.

Other pieces you will meet

  • DHCP hands out addresses automatically; a rogue DHCP server can point clients at an attacker's gateway or resolver.
  • ARP maps IP addresses to hardware (MAC) addresses on a local network and has no authentication, which enables ARP spoofing.
  • NAT lets many private devices share one public address. It is not a firewall, but it does mean inbound connections are not possible unless configured.
  • ICMP carries diagnostics such as ping; blocking it entirely breaks path discovery, so filter selectively.

Where attacks happen

flowchart LR
    subgraph L["Local network"]
        A1["ARP spoofing<br/>sniffing"]
    end
    subgraph R["Routing and internet"]
        A2["IP spoofing<br/>route hijack<br/>DDoS floods"]
    end
    subgraph P["Ports and services"]
        A3["Port scans<br/>exploits<br/>brute force"]
    end
    subgraph N["Names"]
        A4["DNS poisoning<br/>domain hijack<br/>tunneling"]
    end
    L --> R --> P
    N -.-> P

Different layers, different attacks. Defense in depth means having a control at each.

The following lessons answer each of these in turn. First, though, the next lesson shows how to decide, with rules, which traffic is allowed at all.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How many usable host addresses does a /24 IPv4 network have?

    A /24 has 8 host bits, so 256 addresses, but the network address and the broadcast address cannot be assigned to hosts, leaving 254.
  2. 2. A TCP scanner receives a SYN-ACK in reply to its SYN on port 22. What does that mean?

    SYN-ACK is the second step of the handshake: a service accepted the connection attempt. A closed port answers with RST.
  3. 3. Why is the list of open ports called the attack surface?

    Reducing exposed services reduces the code an attacker can probe, which is why unused ports should be closed.
  4. 4. Which of these addresses is NOT routable on the public internet?

    172.16.0.0/12 is private address space. The other addresses belong to public ranges (203.0.113.0/24 is reserved for documentation but is not private space).
  5. 5. Why is DNS interesting to defenders?

    Malware and users alike need names resolved, so unusual domains in DNS logs are a strong detection signal.

Summary

  • Networks work in layers; a control sees only its layer and above, and each layer has typical attacks.
  • CIDR splits addresses into network and host parts; the ipaddress module checks membership, privacy and subnetting.
  • Ports identify services; TCP uses a three-way handshake, UDP does not. Open ports are your attack surface.
  • DNS maps names to addresses without built-in authentication, so it is both an attack target and a rich source of logs.
  • Know your baseline of expected addresses and ports, so that deviations stand out.

Next: Firewalls, Segmentation and Zero Trust, where you'll write the rules that decide which traffic is allowed.