Intermediate · Identity and network defence · topic 17 of 43 · 30 min
Firewalls
Explanation
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.
# 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 DENYDefault 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
| Type | What it looks at | Example |
|---|---|---|
| Packet filter (stateless) | Each packet alone: IPs, ports, protocol | Router access control lists (ACLs) |
| Stateful firewall | Tracks connections, so replies to allowed traffic are let back in automatically | Most office firewalls, Linux nftables/iptables |
| Next-generation firewall (NGFW) | Also identifies applications and users, and can include IPS and URL filtering | Enterprise firewall appliances |
| Web application firewall (WAF) | HTTP requests: blocks patterns like SQL injection | In front of websites and APIs, often as a cloud service |
| Host firewall | Traffic to and from one machine | Windows Defender Firewall, ufw, firewalld |
| Cloud security groups | Virtual firewall rules around cloud machines | AWS 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
$ 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 AnywherePS> 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 BlockCommon 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.
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
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)
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?
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).
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Intrusion detection and prevention (IDS/IPS)Identity and network defence · same module
- Network segmentation, zero trust and VPNsIdentity and network defence · same module
- VPC security and connectivityCloud Computing (AWS, Azure, Google Cloud) · Networking and databases: VPC and RDS
- firewalld: zones, services and portsLinux and Red Hat (RHCSA to RHCE) · Software and networking
- Firewalls, IPS and VPNsComputer Networking (CCNA level) · Network security
- Services, Ingress and the Gateway APIDevOps: Git, Docker, Kubernetes and CI/CD · Kubernetes fundamentals
Questions people ask about this
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.
What is the default zone on a fresh RHEL 9 server?Related
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.
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.
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
- 2 more topics in “Identity and network defence” before its exam: Intrusion detection and prevention (IDS/IPS), Network segmentation, zero trust and VPNs.
- Module 4 exam: Identity and network defence · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
- 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