Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Firewalls and Segmentation

Firewall Types and How They Work

Compare packet-filtering, stateful, proxy, next-generation and web application firewalls, host versus network firewalls, and see a packet filter work in Python.

A firewall is a control point that decides which traffic may pass between two networks (or into and out of a host) according to rules. The name comes from the fire-resistant walls that stop a blaze spreading between parts of a building. A network firewall does the equivalent for attacks: if one zone is compromised, the firewall limits what can reach the rest.

"Firewall" covers many different devices and programs. Some inspect only IP addresses and ports; others understand web requests. Some are dedicated appliances, others a feature of your operating system. This lesson sorts them out so you can choose the right type for each job, and ends with a runnable packet filter so you see what a rule actually does.

Where firewalls sit

flowchart LR
    I["Internet"] --> F1["Perimeter firewall"]
    F1 --> D["DMZ: public servers"]
    F1 --> F2["Internal firewall"]
    F2 --> L["Internal LAN"]
    L --> H["Host firewall on each machine"]

Network firewalls guard the borders between zones; host firewalls protect each machine, even from other machines on the same LAN.

  • A network firewall sits on the path between networks and filters traffic that crosses it.
  • A host-based firewall runs on a single computer (Windows Defender Firewall, nftables or ufw on Linux, the macOS application firewall). It protects that machine even if the network firewall is bypassed, and it can see which program owns a connection.
  • In the cloud, firewalls appear as security groups and network ACLs, which follow the same ideas with a different interface.

Types of firewall

1. Packet-filtering firewalls

The oldest and simplest type. For each packet, it checks header fields only: source and destination IP address, protocol, source and destination port (and sometimes flags). The decision uses a list of rules, evaluated in order, with a default action at the end. Packet filters are fast and cheap, but each packet is judged alone, with no memory of earlier packets. That makes it hard to tell a reply from an unsolicited packet. Router access control lists (ACLs) are packet filters.

2. Stateful inspection firewalls

A stateful firewall remembers connections in a state table. When an inside host opens a connection, the firewall records it and automatically allows the matching replies, so you do not have to open high ports for return traffic. Unsolicited packets that match no entry are dropped. This is the baseline for almost every modern firewall. The next lesson, stateful inspection, builds one.

3. Application-layer gateways and proxy firewalls

A proxy firewall terminates the connection from the client and opens a second, separate one to the server. Because it speaks the application protocol (HTTP, SMTP, FTP, DNS), it can check commands, block file types, enforce authentication and hide internal addresses. The cost is speed and the need for protocol-specific support. A forward proxy serves internal users going out; a reverse proxy sits in front of servers and receives requests for them.

4. Next-generation firewalls (NGFW)

An NGFW combines stateful filtering with deep packet inspection, application identification (recognising Skype or BitTorrent regardless of port), user identity from the directory, an intrusion prevention engine, URL filtering and sometimes TLS inspection. Rules can be written as "marketing group may use social media, nobody may use file-sharing apps" instead of "allow TCP 443". The price is complexity, cost and the need to keep signatures current.

5. Web application firewalls (WAF)

A WAF understands HTTP and protects one web application from attacks such as SQL injection and cross-site scripting by inspecting requests and responses. It complements, but does not replace, secure coding. A WAF belongs in front of a website; it is the wrong tool for protecting a database port.

6. Others you will meet

  • Circuit-level gateways check the TCP handshake and then relay without looking inside (SOCKS-style).
  • Unified threat management (UTM) bundles firewall, antivirus, VPN and filtering in one box for small sites.
  • Cloud and firewall-as-a-service deliver filtering from a provider's network.
  • Distributed / microsegmentation firewalls enforce policy at each workload in virtualised environments (see the segmentation lessons).

Firewall types by depth

Packet filter Addresses and ports
Stateful Remembers connections
NGFW / proxy Reads the application
WAF Protects one web app

Deeper inspection costs more speed

1 / 2

Choosing a type

Need Good fit
Fast, simple border filtering Packet filter or stateful firewall
Allow only replies to inside-initiated traffic Stateful firewall
Control which web sites staff visit; hide internal hosts Forward proxy or NGFW
Protect a public web application WAF plus secure code
Protect each server from its neighbours Host firewall / microsegmentation
Filter by application and user NGFW

In practice you combine them: a stateful perimeter firewall, a WAF in front of web apps, host firewalls everywhere.

Firewall actions and rule order

A rule has a match (source, destination, protocol, ports, maybe zone and user) and an action:

  • Accept / allow: let the packet through.
  • Drop: discard silently. The sender gets no response.
  • Reject: discard and send an error (TCP reset or ICMP unreachable).
  • Log: record the match (often combined with another action).

Rules are normally evaluated top to bottom, first match wins, and the last rule is the default policy. Order matters: a broad "allow all" above a specific "deny" makes the deny useless. This is the most common firewall mistake, and the next program lets you see it.

A tiny packet filter

The program models the first-match logic of a packet filter on fabricated packets. Change the order of the rules and run it again to see what changes.

Python First match wins: a packet filter in Python

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.

Notice that 203.0.113.50 to port 443 is denied only because the "known bad network" rule sits above the "public HTTPS" rule. Move it below and that address gets in.

What firewalls cannot do

  • They cannot stop traffic that does not cross them: a laptop on public Wi-Fi, a USB stick, an insider on the same segment.
  • They cannot inspect encrypted traffic unless they terminate TLS, and they cannot stop a user who is allowed to visit a site from downloading something harmful from it.
  • They cannot fix a vulnerable service you have chosen to expose; they can only decide who reaches it.
  • Rules rot. A firewall with thousands of unreviewed rules gives a false sense of safety.

Treat a firewall as one layer of defence in depth, configured with default deny and reviewed on a schedule.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does a stateful firewall remember that a plain packet filter does not?

    A state table lets the firewall recognise reply traffic for connections opened from the inside.
  2. 2. Which firewall type is the right fit for blocking SQL injection against a public website?

    A WAF inspects HTTP requests and recognises attack patterns such as injection, which header-only filters cannot see.
  3. 3. A rule 'allow all from 0.0.0.0/0' is placed above a rule 'deny 203.0.113.0/24'. What happens to traffic from that network?

    Rules are evaluated top to bottom and the first match decides, so the broad allow makes the later deny irrelevant.
  4. 4. What is the difference between drop and reject?

    Drop gives no response, which reveals less and slows scanners. Reject sends an error such as a TCP reset.
  5. 5. Why use host-based firewalls in addition to the perimeter firewall?

    Attackers who get inside the perimeter can still reach unprotected hosts. Host firewalls add a layer at each machine.

Summary

  • Firewalls control traffic between zones or into hosts, as network appliances, host software or cloud rules.
  • Types: packet filter (fast, stateless), stateful (tracks connections), proxy (terminates and relays), NGFW (deep inspection, identity, IPS) and WAF (web applications).
  • Actions are allow, drop, reject and log; rules run top to bottom with first match wins and a default policy at the end.
  • Combine types for layered protection; match the type to the job.
  • Firewalls do not see traffic that bypasses them, and rule sets need regular review.

Next: Writing Firewall Rules: iptables, nftables and Cloud Security Groups