Catalog My learning Blog
Computer Networking for Developers: TCP, DNS, HTTP, TLS · Phase 3 · Lesson 18 · 3 min read

Firewalls and Network Segmentation Explained

On this page
  1. Why a flat network is dangerous
  2. Stateful vs stateless firewalls
  3. Default-deny rules with iptables
  4. Network segmentation into tiers
  5. Security groups and segmentation in the cloud
  6. Check yourself

Why a flat network is dangerous

In a flat network, every machine can reach every other. An attacker who compromises one web server can then try the database, the build server and every admin interface. Firewalls control which traffic is allowed; segmentation divides the network into zones with firewalls between them, so a breach in one zone stays contained.

Stateful vs stateless firewalls

Rules match on fields like source and destination IP, protocol, and port, and then allow or deny. Two kinds:

  • Stateless filters judge each packet alone. To allow replies to your outbound connections, you must open the reply ports too, which tends to open too much. Cloud network ACLs (such as AWS NACLs) work this way.
  • Stateful firewalls track connections. Allow an outbound connection, and its replies are allowed automatically. Linux netfilter (iptables, nftables), cloud security groups and most hardware firewalls are stateful.

Layer 7 firewalls and web application firewalls go further and inspect HTTP itself (paths, headers, request bodies).

Default-deny rules with iptables

Start from "nothing is allowed" and open only what's needed. On a Linux web server with iptables:

Code
# Inbound: drop everything not allowed below
iptables -P INPUT DROP
iptables -P FORWARD DROP

# Local traffic and replies to connections this host started
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

# Public HTTPS from anywhere
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# SSH only from the admin VPN subnet
iptables -A INPUT -p tcp -s 10.0.99.0/24 --dport 22 -j ACCEPT

The ESTABLISHED,RELATED rule is the stateful part: without it, replies to the server's own outbound calls (DNS, package downloads, API calls) would be dropped by the default policy. Rules are checked in order, and the first match wins. Newer distributions use nftables underneath, but the logic is the same.

Network segmentation into tiers

A classic web application splits into zones, each allowed to talk only to its neighbor:

ZoneAccepts fromOn ports
Public (load balancers, the "DMZ")The internet443
Application tierLoad balancers onlyThe app port, for example 8080
Data tierApplication tier only5432 (PostgreSQL), 6379 (Redis)
ManagementAdmin VPN only22, monitoring ports
Firewalls between the internet, a DMZ, an application tier and a data tier, each layer allowing a narrower set of traffic.
Firewalls between the internet, a DMZ, an application tier and a data tier, each layer allowing a narrower set of traffic.

The data tier has no route from the internet at all. Even if an attacker takes over a load balancer, they can't reach the database directly.

Security groups and segmentation in the cloud

  • Security groups are stateful firewalls attached to each instance or network interface. A rule can reference another security group instead of an IP range: "allow 5432 from the app-servers group". That keeps working as instances come and go.
  • Subnets provide the zones: public subnets have a route to an internet gateway; private subnets don't.
  • Kubernetes NetworkPolicies do the same between pods, but only if the cluster's network plugin enforces them.
  • Zero trust takes segmentation further: don't trust the network location at all; authenticate and authorize every service call (for example with mutual TLS in a service mesh).

Check yourself

It tracks each connection and automatically allows its reply traffic, so you only write rules for who may start connections. A stateless firewall judges each packet alone, so you must also open the broad ephemeral port ranges replies come back on.

DNS replies come back as inbound packets, and no rule accepts them, so they're dropped. Add a rule that accepts ESTABLISHED and RELATED traffic, usually as the first rule, so replies to connections the server started get through.

App instances change IPs as they scale and get replaced; a group reference always matches whichever instances belong to the app tier.

Lateral movement. If an attacker compromises a web server, segmentation stops it from reaching databases or admin systems it has no reason to talk to.

Network ACLs are stateless and apply to whole subnets, with numbered allow and deny rules. Security groups are stateful, attach to instances, and only allow.