Notes
Packet filter
The simplest firewall is a packet filter. It looks at each packet on its own and matches on source address, destination address, protocol and port, then allows or drops it. It is fast and simple. Its weakness is that it has no memory. It cannot tell a reply to a connection you started from a brand new connection coming in, so the rules end up wider than you want. Cloud network access control lists work this way.
- looks at each packet alone
- matches: source IP, dest IP, protocol, port
- allow or drop
- fast and simple
- problem: it cannot tell a reply from a new connection, so rules get loose
- cloud network ACLs work this way
Stateful
A stateful firewall remembers. It keeps a table of every connection, so replies to a conversation you started are let back in automatically, while a new inbound connection must match an explicit rule. That allows the rule everyone wants: let my machines talk out, and let nothing new in. The Linux firewall and cloud security groups are stateful. This is the standard network firewall and the one you will configure most.
- tracks every connection in a state table
- a reply to a connection you opened is allowed back
- a new inbound connection must match a rule
- so you can say: allow out, deny everything in iptables/nftables, cloud security groups
How rules work
Rules are read from top to bottom and the first match wins. The most important rule is the last one: deny everything else. That is default deny, and it is fail-safe from Part zero applied to networks. You list the few conversations that should exist, and everything unlisted is dropped and logged. The opposite approach, allowing everything and blocking the known bad, fails the moment something new appears.
1 allow tcp any -> web:443 2 allow tcp office -> bastion:22 3 allow tcp web -> db:5432 4 deny all (log) first match wins default deny: anything not listed is dropped specific rules above broad ones
Egress
Here is the half most teams forget. Firewalls are usually strict about what comes in and wide open about what goes out. But an attacker who compromises a server needs outbound access for almost everything: to download tools, to connect back to their own server, to send stolen data away. Restricting each server to the few destinations it truly needs breaks all of that. A blocked outbound attempt from a database is also a superb alert.
- most firewalls: strict in, wide open out attackers need outbound: to download tools, call their server, send data away
- restrict servers to the destinations they need
- a database has no reason to reach the internet
- log the denied attempts
Next-generation
Everything runs over port 443 now, so port numbers say less than they used to. A next-generation firewall looks deeper. It identifies the actual application whatever port it uses, writes rules by user and group rather than address, and includes intrusion prevention and malware checks. Many can decrypt TLS to inspect it, which is powerful and also a privacy and trust decision that needs care. These are the large appliances at company network edges.
- identifies the application, whatever the port
- rules by user and group, not just IP
- built-in intrusion prevention and malware checks
- TLS inspection: decrypts to look inside (with care)
- Palo Alto, Fortinet, Check Point
WAF
A web application firewall is a different thing with a similar name. It sits in front of a web application and reads the HTTP request itself: the path, the parameters, the headers and the body. It blocks requests that match known attack patterns such as SQL injection and cross-site scripting, applies rate limits and bot rules, and can block a newly announced vulnerability within minutes, which is called a virtual patch, while the real fix is deployed.
- sits in front of a web app
- reads the HTTP request: path, parameters, headers, body
- blocks known attack patterns: SQL injection, XSS, path traversal
- rate limits, bot rules, virtual patches
- OWASP Core Rule Set, ModSecurity, cloud WAFs
What a WAF misses
Be honest about what a WAF cannot do. A request that asks for another customer's order is perfectly well formed. No pattern flags it, because the flaw is a missing permission check in your code. Logic flaws and broken access control pass straight through. Pattern rules can be evaded with clever encoding, and they block real users until tuned, so always start in log-only mode. A WAF buys time. Only fixing the code removes the bug.
- a request for someone else's order looks perfectly valid
- logic flaws and broken access control pass straight through
- patterns can be evaded with encoding tricks
- false positives block real users: start in log-only mode, then tune
- it buys time; code fixes it
Host and cloud
The edge firewall is not enough, because attackers who get inside find a soft interior. Put a firewall on every machine. On a server that is the host firewall. In the cloud it is the security group, and the best practice is to reference other groups rather than addresses: the database group accepts its port only from the web group. And look hard at any rule that allows the whole internet, written as zero dot zero dot zero dot zero slash zero.
# host firewall (Ubuntu) ufw default deny incoming ufw allow from 10.0.1.0/24 \ to any port 5432 # cloud security group db-sg: allow 5432 from web-sg (reference the group, not IPs) nothing from 0.0.0.0/0
Rules that last
Firewalls rot. Rules are added in emergencies and never removed, until nobody knows why half of them exist. So give every rule an owner, a reason and a ticket. Keep rules as code and review changes like any other code. Give temporary rules an expiry date. Review regularly and delete what is unused or too broad. Send denied traffic to your log system. And verify from the outside with a port scan, because the scan shows the truth.
- every rule: an owner, a reason, a ticket
- rules as code, reviewed in pull requests
- temporary rules get an expiry
- review regularly: delete unused and "any-any" rules
- log denies and send them to the SIEM
- test from outside with Nmap
Common mistakes
Mistakes. Believing a firewall makes you safe. An allow-everything rule added temporarily and never removed. No outbound filtering at all. SSH or a database open to the entire internet. A hard edge around a flat interior where everything can reach everything. Leaving a known bug unfixed because the WAF blocks it today. And hundreds of rules that nobody owns and nobody dares delete.
- "we have a firewall, we are safe"
- allow any-any "temporarily"
- no egress filtering
- SSH or databases open to 0.0.0.0/0
- a hard edge, a flat inside
- a WAF instead of fixing code
- rules nobody owns
Say it like this
In the interview: a network firewall controls which connections may exist, by address, port, protocol and connection state, with default deny, and it should filter outbound traffic as well as inbound. A WAF works at the application layer, inspecting HTTP requests and blocking known attack patterns, with rate limiting and virtual patching. Neither can stop logic flaws such as broken access control, so both are layers on top of secure code.
- "A network firewall controls
- which connections may exist, by address, port and state, default deny, in both directions. A WAF inspects
- HTTP content and blocks known
- attack patterns. Neither stops
- logic flaws, so both are layers on top of secure code."