Skip to content
Tutorials Center
My learning

Topics

Lesson 66 of 79 ยท Network Security

Common Attacks and How to Defend

Understand ARP and DNS spoofing, SYN floods, DDoS amplification, port scans and session hijacking, and defend with rate limiting. Includes a live Python rate limiter.

Almost every network attack abuses a trust assumption built into a protocol: that an ARP reply is honest, that a SYN comes from a real client, that a UDP source address is not forged, that a cookie belongs to whoever presents it. Knowing the assumption makes the attack easy to understand, and makes the defense obvious.

This lesson tours the classic attacks, each with how it works and what stops it, and ends with rate limiting, a defense useful against many of them. It uses ideas from earlier lessons: ARP, DNS, the TCP handshake and firewalls. The goal is to understand and defend; only test systems you own or have written permission to test.

A map of attacks by layer

flowchart TB
    L2["Link layer<br/>ARP spoofing, MAC flooding"] --> L3["Network layer<br/>IP spoofing, ICMP floods"]
    L3 --> L4["Transport layer<br/>SYN flood, port scans, session hijacking"]
    L4 --> L7["Application layer<br/>DNS spoofing, HTTP floods, credential attacks"]

The same ideas repeat at every layer: forge an identity, exhaust a resource, or listen in.

ARP spoofing

You saw this in the VLANs and ARP lesson: the attacker sends forged ARP replies so that victims map the gateway's IP address to the attacker's MAC address. All of the victim's outgoing traffic goes through the attacker, who can read or alter it.

  • Needs: a position on the same LAN (or Wi-Fi network).
  • Defenses: dynamic ARP inspection and DHCP snooping on switches, port security, separating untrusted devices in their own VLAN, and end-to-end encryption (TLS) so that intercepted data is useless. Monitoring tools can alert when an IP address suddenly changes MAC address.

A related attack is MAC flooding: sending frames from thousands of fake source MACs to overflow a switch's learning table, so it falls back to flooding and the attacker sees traffic meant for others. Port security (a limit of MAC addresses per port) stops it.

DNS spoofing and cache poisoning

DNS answers were originally unauthenticated. If an attacker can get a resolver to accept a forged answer, every user of that resolver is sent to the attacker's address when they look up a bank or a software update server. Two common routes:

  • Cache poisoning: race the real answer with forged ones. The attacker must guess the 16-bit transaction ID and, in a well-implemented resolver, the random source port too; randomizing both makes the guess hopelessly large. A resolver that uses a fixed port is easy prey.
  • Local spoofing: an attacker on the LAN answers your DNS query faster than the real server, or changes the DNS server you are told to use (a rogue DHCP server does exactly that).

Defenses: randomized source ports and IDs, DNSSEC (signed records), encrypted DNS (DoT and DoH, see DNS in practice), and above all HTTPS with certificate validation, because a spoofed address cannot present a valid certificate for the real site name.

SYN flood

The TCP handshake makes the server allocate state after the first SYN. A SYN flood sends a huge number of SYNs, usually from forged source addresses, and never completes the handshake. The server's half-open connection queue fills up and real clients are refused.

sequenceDiagram
    participant A as Attacker (forged sources)
    participant S as Server
    A->>S: SYN (from fake address 1)
    S-->>A: SYN-ACK (goes nowhere)
    A->>S: SYN (from fake address 2)
    S-->>A: SYN-ACK (goes nowhere)
    Note over S: backlog full: real clients get no answer

The server waits for a final ACK that never comes while its queue of half-open connections fills.

Defense: SYN cookies. The server encodes the connection details into the initial sequence number of its SYN-ACK using a secret hash, and keeps no state at all. Only a client that sends back a valid ACK (acknowledging that number) gets a connection. Other measures: bigger queues, shorter timeouts, rate limits per source, and upstream filtering.

Python SYN flood against a backlog, with and without SYN cookies

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.

Port scans

Before attacking, a scanner finds which services answer. A TCP connect scan tries a full handshake to each port; a SYN scan sends a SYN and looks at the reply (SYN-ACK means open, RST means closed, silence means filtered) without finishing the handshake. A UDP scan is slower because closed ports answer with ICMP errors and open ports often say nothing. Tools such as nmap are legitimate for auditing your own systems (see network tools).

Defenses: expose only needed ports (default deny), keep services patched, detect patterns such as one source touching many ports in seconds (an IDS rule, or a rate limit), and hide management ports behind a VPN.

Session hijacking

Anyone who steals the token that proves your login can act as you. Attackers get it by sniffing unencrypted traffic, by cross-site scripting, or by guessing weak IDs. At the TCP level, a hijacker who can predict sequence numbers could inject packets into an existing connection, which is why initial sequence numbers are random today.

Defenses: HTTPS everywhere, cookies marked Secure, HttpOnly and SameSite (see caching, cookies and state), long random session IDs, short lifetimes with renewal after login, and binding sessions to additional signals.

DoS, DDoS and amplification

A denial of service (DoS) attack tries to make a service unavailable by exhausting bandwidth, connection state or CPU. When it comes from thousands of compromised machines (a botnet) it is a distributed DoS, hard to filter by source.

Attackers love reflection with amplification. They send a small request to an open server (DNS, NTP, memcached) with the victim's address forged as the source. The server sends its much larger reply to the victim. The attacker's cost is tiny, and the traffic comes from many innocent servers.

flowchart LR
    ATT["Attacker"] -->|"small request<br/>source forged = victim"| R["Open reflectors<br/>DNS, NTP, memcached"]
    R -->|"large replies"| V["Victim"]

The amplification factor is reply size divided by request size.

Python How much does amplification multiply an attacker's bandwidth?

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.

Defenses: ISPs filter forged sources (BCP 38 ingress filtering); operators close or restrict open resolvers and old UDP services; large sites use anycast CDNs and scrubbing services that absorb traffic across many locations; and servers use rate limiting and caching for application-layer floods.

Rate limiting

A rate limiter caps how many requests a client may make in a time window. It protects against floods, scans, brute-force logins and scraping, and is used in firewalls, load balancers, APIs and proxies. Common algorithms:

  • Fixed window: count requests per clock minute. Simple, but allows a burst of double the limit across a window boundary.
  • Sliding window: count requests in the last N seconds, which is smoother.
  • Token bucket: a bucket refills at a steady rate and each request spends a token. It allows short bursts yet enforces an average rate (see QoS).
Python A per-client sliding-window rate limiter

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.

Limiting by IP address has limits of its own: many users can share one address behind NAT, and a botnet spreads its requests across thousands of addresses. Real systems combine per-IP, per-account and global limits, and respond with HTTP status 429 Too Many Requests and a Retry-After header.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the purpose of SYN cookies?

    The server encodes the connection into its initial sequence number with a secret hash and creates state only when a valid final ACK returns.
  2. 2. Why is DNS amplification effective for an attacker?

    A small spoofed request produces a large reply to the victim, and the traffic arrives from many legitimate servers.
  3. 3. Which defense best protects a user's data even when ARP spoofing succeeds?

    Spoofing puts the attacker in the path, but encrypted and authenticated traffic cannot be read or silently altered.
  4. 4. A port scanner sends a SYN to a port and receives a RST. What does that usually mean?

    A closed port answers with RST. An open port answers SYN-ACK; a firewall that silently drops produces no reply at all.
  5. 5. What advantage does a token bucket have over a fixed window limiter?

    Tokens accumulate up to the bucket size and refill steadily, so bursts are allowed but the long-run rate is capped.

Summary

  • Attacks abuse trust assumptions: unauthenticated ARP and DNS answers, free half-open connection state, forged source addresses, and stolen session tokens.
  • Defenses stack: switch features (DAI, port security), DNSSEC and randomization, SYN cookies, ingress filtering, patched services with default-deny firewalls, and encryption with secure cookies.
  • DDoS amplification multiplies the attacker's bandwidth by the ratio of reply size to request size, using spoofed sources and open servers.
  • Rate limiting (fixed window, sliding window, token bucket) caps request rates per client; answer excess requests with 429.

Now learn the tools that let you observe and test a network yourself in essential network tools.