Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 38 ยท Network and Data Protection

Firewalls, WAF and DDoS Protection

Understand network firewalls, web application firewalls, rate limiting and DDoS defenses in the cloud, plus a runnable token-bucket limiter and simple WAF rule engine.

Security groups decide which ports and sources may connect. Many attacks, though, arrive on a perfectly legitimate port, such as HTTPS on 443, and look like normal requests: an input field stuffed with malicious text, a client hammering a login form, or millions of requests meant to overwhelm a site. This lesson covers the layered defenses for those cases: network firewalls, web application firewalls, rate limiting and protection against distributed denial of service (DDoS).

Layers of filtering

Layers at the edge

DDoS and CDN Absorb floods, cache
WAF Inspect HTTP requests
Network firewall Ports, protocols, egress
Security groups Per-resource rules

Traffic passes through each layer in turn.

1 / 2
flowchart LR
    C["Clients"] --> D["DDoS protection<br/>and CDN"]
    D --> W["WAF<br/>HTTP rules"]
    W --> L["Load balancer"]
    L --> F["Network firewall /<br/>security groups"]
    F --> A["Application"]

Each layer sees something different. None of them replaces secure application code.

Network firewalls

A network firewall works at layers 3 and 4 (IP addresses, ports, protocols), and some add deeper inspection.

  • Packet filtering and stateful inspection allow or block by address, port and connection state.
  • Next-generation firewalls add application awareness, intrusion detection and prevention (IDS/IPS) and TLS inspection.
  • Egress filtering lists the domains or addresses your workloads may call, which stops many malware callbacks and data exfiltration attempts.
  • Central inspection VPC. Large organizations route traffic between networks through a shared inspection point.

Web application firewalls (WAF)

A WAF understands HTTP. It sits in front of a web application or API and inspects requests (URL, headers, query string, body) against rules before they reach your code.

Typical protections:

  • Managed rule sets for common attack classes such as SQL injection, cross-site scripting and path traversal.
  • Rate-based rules that block clients sending too many requests.
  • IP reputation lists and geographic rules.
  • Bot control and CAPTCHA challenges for login and signup pages.
  • Custom rules, for example "block requests to /admin unless from the office network".
  • Request size limits and header checks.

A WAF is a compensating control, not a fix. Roll out new rules in count (log-only) mode first to see false positives, then block. Keep fixing the underlying code, because attackers can often bypass signatures.

Rate limiting: the token bucket

A common algorithm is the token bucket: a bucket holds up to N tokens and refills at a fixed rate. Each request takes a token. If the bucket is empty the request is refused. It allows short bursts but caps the sustained rate. The program below simulates it with a fake clock so the output is deterministic.

Python A token-bucket 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.

The first five requests use the burst allowance, the next five are blocked, and by t=4.0 enough tokens have refilled for a new request. In production, limits are tracked per client key (IP address, API key or user) and shared between servers.

A tiny WAF rule engine

The example below shows the concept of signature rules on request parameters. These patterns are deliberately simple; real managed rule sets are much larger and carefully tuned. It only inspects made-up strings and sends nothing anywhere.

Python Match requests against WAF rules

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.

Switch MODE to "block" once you are confident the rules do not catch legitimate traffic. Notice how a user searching for "union select tickets" could trigger a false positive, which is exactly why a log-only trial matters.

Not a substitute for secure code. Parameterized queries, output encoding and input validation in the application are the real fixes. A WAF buys time and reduces noise.

DDoS protection

A distributed denial-of-service attack tries to exhaust bandwidth, connection tables or application resources using traffic from many sources. Categories:

  • Volumetric: floods of traffic that fill the pipe (UDP floods, amplification).
  • Protocol: abuse of connection handling (SYN floods).
  • Application layer: many expensive but valid-looking requests (login, search, report generation).

Defenses:

  1. Use a CDN and managed DDoS protection. Their global capacity absorbs floods far larger than your own links.
  2. Hide your origin. Allow only the CDN or load balancer to reach your servers, so attackers cannot bypass the protection.
  3. Autoscaling with limits. Scale to absorb load, but set a maximum so an attack does not produce an unlimited bill.
  4. Rate limits and caching for expensive endpoints.
  5. Graceful degradation. Serve static or cached pages when the back end is stressed.
  6. A runbook and contacts, and a tested way to turn on stricter modes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a WAF inspect that a basic network firewall does not?

    A WAF understands the HTTP application layer and applies rules to request content.
  2. 2. Why roll out a new WAF rule in count mode first?

    Log-only mode shows what would be blocked so you can tune rules.
  3. 3. How does a token bucket limiter behave?

    The bucket's capacity allows bursts and the refill rate sets the long-term limit.
  4. 4. Why should the origin server accept traffic only from the CDN or load balancer?

    If the origin is reachable directly, attackers can ignore the protective layers.

Summary

  • Layer defenses: DDoS protection and CDN, WAF, network firewall and security groups.
  • A WAF inspects HTTP for attack signatures and rate abuse; trial rules in count mode and keep fixing application code.
  • Token-bucket rate limiting allows bursts but caps the sustained rate.
  • Resist DDoS with managed protection, a hidden origin, capped autoscaling, caching and a runbook.

Next: Encryption at Rest and in Transit