SECURITY ENGINEERING · PART 2 · NETWORKING FOR SECURITY · DEFENSE · 29 OF 229

Firewalls Explained: Stateful, Next-Gen and WAF

What each kind of firewall can and cannot see: packet filters, stateful firewalls, next-generation firewalls and web application firewalls. How rules are evaluated, default deny, inbound versus outbound (egress) filtering, host firewalls and cloud security groups, writing rules that survive review, and why a firewall is one layer and never the whole defense.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 13

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Stateful, default deny, egress too
  • NGFW knows apps and users
  • WAF reads HTTP, misses logic
  • firewalls on every host

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.

Stateless packet filtering
  • 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.

Stateful firewalls
  • 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.

Rule evaluation
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.

Outbound filtering
  • 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.

NGFW
  • 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.

Web application firewall
  • 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.

WAF limits
  • 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.

Close to the workload
# 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.

Rule hygiene
  • 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.

Avoid these
  • "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.

In the interview
  • "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."

Interview questions

Q1What is the difference between a stateless and a stateful firewall?

A stateless packet filter judges each packet alone by addresses, protocol and port. A stateful firewall tracks connections in a state table, so replies to connections you initiated are allowed automatically while new inbound connections must match a rule.

Q2What does default deny mean?

Only explicitly allowed traffic passes; everything else is dropped, usually by a final deny-all rule. It is safer than blocking known-bad traffic, because anything new or unexpected is denied by default.

Q3Why is egress filtering important?

Compromised hosts need outbound access to download tools, reach command-and-control servers and exfiltrate data. Restricting outbound traffic to required destinations breaks those steps, and denied outbound attempts are high-quality alerts.

Q4What is a next-generation firewall?

A firewall that goes beyond ports and addresses: it identifies applications regardless of port, applies rules by user or group, and integrates intrusion prevention, malware detection and often TLS inspection.

Q5What is a WAF and how does it differ from a network firewall?

A web application firewall inspects HTTP requests and responses and blocks patterns such as SQL injection and XSS, with rate limiting and bot rules. A network firewall controls which connections are allowed by address and port, and does not understand HTTP content.

Q6What can a WAF not protect against?

Business logic flaws and broken access control, where requests are well formed but should not be authorised, plus attacks crafted to evade its patterns. It also produces false positives, so it must be tuned, and it complements rather than replaces secure code.

Q7What is virtual patching?

Adding a WAF or IPS rule that blocks exploitation of a specific known vulnerability, so the system is protected immediately while the real code or software patch is tested and deployed.

Q8How are cloud security groups best configured?

As stateful, default-deny rules attached to each workload, referencing other security groups rather than IP ranges (for example the database group allowing its port only from the web group), with no sensitive ports open to 0.0.0.0/0.