Skip to content

Intermediate · Identity and network defence · topic 17 of 43 · 30 min

Firewalls

Explanation

A firewall decides which network traffic is allowed, using an ordered list of rules. The safest setup is default deny: block everything, then allow only what is needed. Firewalls come as network appliances, cloud security groups, host firewalls on each machine, and web application firewalls (WAF) that understand HTTP.

Why firewalls

Without a firewall, every open port on every machine can be reached by anyone who can reach the network. A firewall is a gatekeeper between networks (or in front of one machine) that checks each connection against rules and allows or blocks it.

How rules work

A rule usually says: from this source, to this destination, on this protocol and port, do this action (allow or deny). Rules are checked from top to bottom and the first match wins. If nothing matches, a default action applies.

output
#  Source            Destination     Port      Action
1  10.20.0.0/16      10.50.0.10      TCP 443   ALLOW   (staff to intranet)
2  10.20.5.0/24      10.50.0.20      TCP 22    ALLOW   (IT team SSH to server)
3  any               10.50.0.0/24    any       DENY    (nothing else to servers)
*  default                                     DENY
A small rule table. Order matters: if rule 3 were first, rules 1 and 2 would never be reached.

Default deny

Default deny (also called "deny by default" or an allow-list) means everything is blocked unless a rule allows it. The opposite, default allow, blocks only what you remembered to block, and attackers find what you forgot. Default deny is the professional standard for incoming traffic and increasingly for outgoing traffic too.

Types of firewalls

TypeWhat it looks atExample
Packet filter (stateless)Each packet alone: IPs, ports, protocolRouter access control lists (ACLs)
Stateful firewallTracks connections, so replies to allowed traffic are let back in automaticallyMost office firewalls, Linux nftables/iptables
Next-generation firewall (NGFW)Also identifies applications and users, and can include IPS and URL filteringEnterprise firewall appliances
Web application firewall (WAF)HTTP requests: blocks patterns like SQL injectionIn front of websites and APIs, often as a cloud service
Host firewallTraffic to and from one machineWindows Defender Firewall, ufw, firewalld
Cloud security groupsVirtual firewall rules around cloud machinesAWS security groups, Azure network security groups

Ingress and egress

Ingress filtering controls traffic coming in. Egress filtering controls traffic going out. Egress rules are often forgotten, but they matter: malware needs to call out to its controller and copy data out. A server that only needs to talk to a database should not be able to connect to any address on the internet.

Host firewall examples

bash
$ sudo ufw default deny incoming
$ sudo ufw default allow outgoing
$ sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcp
$ sudo ufw allow 443/tcp
$ sudo ufw enable
$ sudo ufw status verbose
Status: active
Default: deny (incoming), allow (outgoing), disabled (routed)

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    192.168.1.0/24
443/tcp                    ALLOW IN    Anywhere
ufw on Ubuntu: deny incoming by default, allow SSH only from the local network, and HTTPS from anywhere.
powershell
PS> New-NetFirewallRule -DisplayName "Block inbound Telnet" -Direction Inbound -Protocol TCP -LocalPort 23 -Action Block
PS> Get-NetFirewallProfile | Select-Object Name, Enabled, DefaultInboundAction

Name    Enabled DefaultInboundAction
----    ------- --------------------
Domain     True                Block
Private    True                Block
Public     True                Block
Windows: add a block rule and check that every profile blocks inbound traffic by default.

Common mistakes

  • "Any any allow" rules added during troubleshooting and never removed.
  • Wrong order: a broad allow above a specific deny makes the deny useless.
  • Only filtering incoming traffic. Add egress rules for servers.
  • Opening management ports (SSH, RDP, admin panels) to 0.0.0.0/0, the whole internet.
  • Never reviewing rules. Old rules for retired projects pile up; review them on a schedule.
Tip: Every rule should have an owner, a reason and a date in its comment. When nobody can explain a rule, plan to remove it.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Firewalls · 1/7

Firewall = gatekeeper

A firewall decides which traffic is allowed, using rules.

Prefer a video by a teacher? Find free videos on “firewall rules explained stateful vs stateless default deny” on YouTube ↗

In real life: Cloud database open to the world

A developer at a Bengaluru start-up opened port 5432 to 0.0.0.0/0 in a cloud security group "for ten minutes" to debug from home. Two weeks later, automated scanners found the database, which still had a weak password. Data was copied out. The fix: only the app servers' security group may reach the database, admin access goes through a VPN or bastion, and a weekly script flags any rule open to 0.0.0.0/0 on sensitive ports.

Practice (0/3 done)

Task 1 · Question

Rule 1: ALLOW any to web server port 443. Rule 2: DENY 203.0.113.9 to web server port 443. Will 203.0.113.9 be blocked?

Task 2 · Code · Python

Write decide(rules, src_ip, port) for a first-match firewall. Each rule is a tuple (source_cidr, port, action) where port may be "any" and action is "allow" or "deny". Return the action of the FIRST rule whose source network contains src_ip and whose port matches. If none matches, return "deny" (default deny).

Task 3 · Code · Python

Write open_to_world(rules, sensitive) where rules is a list of dicts with keys "name", "source" and "port". Return a sorted list of rule names whose source is "0.0.0.0/0" and whose port is in the set sensitive.

Quick quiz

Question 1

1.What does "default deny" mean?

Question 2

2.Which firewall understands HTTP and can block SQL injection patterns?

Question 3

3.What does a stateful firewall add over a stateless packet filter?

Question 4

4.Why use egress filtering on servers?

Question 5

5.A cloud rule allows port 3389 from 0.0.0.0/0. What is the risk?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What does "default deny" mean?

Answer: Block everything unless a rule allows it

An allow-list approach: only explicitly allowed traffic passes.

Which firewall understands HTTP and can block SQL injection patterns?

Answer: Web application firewall (WAF)

A WAF inspects web requests at the application layer.

What does a stateful firewall add over a stateless packet filter?

Answer: It tracks connections, allowing replies to permitted traffic automatically

It remembers connection state, so you do not need separate rules for replies.

Why use egress filtering on servers?

Answer: To stop malware calling out and data being copied out

Outgoing rules limit command-and-control and exfiltration.

A cloud rule allows port 3389 from 0.0.0.0/0. What is the risk?

Answer: Remote Desktop is open to the whole internet

0.0.0.0/0 is every IPv4 address; exposed RDP is a top ransomware entry point.

Which statement about network ACLs is true?Related

Answer: They are stateless and evaluated in rule-number order

NACLs are stateless subnet firewalls; the lowest numbered matching rule decides.

From VPC security and connectivity

What is the default zone on a fresh RHEL 9 server?Related

Answer: public

public is the default.

From firewalld: zones, services and ports

What lets a stateful firewall allow a reply from a web server back to an inside user?Related

Answer: Its state table of sessions started from the inside

The firewall remembers the outgoing session and allows only matching replies.

From Firewalls, IPS and VPNs

You created an Ingress, but nothing happens and no address appears. What is the most likely cause?Related

Answer: No ingress controller is installed, or the ingressClassName is wrong

An Ingress is only rules. A controller must be running to act on them.

From Services, Ingress and the Gateway API

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 2 more topics in “Identity and network defence” before its exam: Intrusion detection and prevention (IDS/IPS), Network segmentation, zero trust and VPNs.
  2. Module 4 exam: Identity and network defence · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Logs, SIEM and incident response. 43 topics left in the course before the final exam.

← Previous: Identity and access management, least privilege and RBAC