How to Audit a Firewall Ruleset
Last updated:
A firewall ruleset is the ordered list of rules that decides which packets reach your server and which are turned away. Every rule is a condition plus a verdict — accept, drop, reject — and the firewall walks the list from the top until a rule matches. That is the whole mechanism, and it is also the whole failure mode: rulesets are written once, edited in emergencies, and almost never re-read. A temporary hole opened for a migration in 2023 is still open. A deny added to block one abusive host sits below the accept that already let it in. A database port opened to the world for a ten-minute test never got closed. You can paste any ruleset into the Firewall Ruleset Analyser and get a score and a list of findings in seconds — but this article teaches you to read the file yourself, so that when the analyser flags a rule, you understand what it is actually saying.
What a firewall ruleset is — and why it drifts
A ruleset is not a policy document; it is a program. It has tables, chains, and an order of evaluation, and like any program it accumulates dead code, contradictions, and leftovers from people who no longer work there. The most common drift is the temporary exception that became permanent: someone opens a port to debug a deployment, the deployment succeeds, and nobody reverts the rule because reverting it feels riskier than leaving it. The second most common is the copy-paste rule — the same accept written twice, or a rule copied from a tutorial with the source address left as 0.0.0.0/0. The third is the defensive rule that does nothing: a deny placed after an accept that already matches the same traffic, so the deny can never fire. None of these show up in a quick glance at the file, because each line looks reasonable on its own. The problem is never one line; it is the interaction between lines.
Drift is also a matter of scale. A fresh server ships with a handful of rules and a clear intent: allow SSH, allow the web, drop everything else. Six months later the file has forty rules, three of them contradictory, and nobody remembers which service the rule on port 8080 was for. The audit habit that prevents this is cheap: export the ruleset, read it top to bottom once a quarter, and ask of every rule, "who needs this, and from where?" If nobody can answer, the rule is a candidate for removal. An automated analyser does the same thing faster — it checks every rule against every other rule for shadowing and duplication, and it checks every open port against a list of ports that should never face the internet — but the judgement about whether a rule belongs is still yours. The analyser finds the candidates; you decide what to keep.
Reading iptables-save and nftables by hand
An iptables-save export starts with a table line such as *filter, then one chain-policy line per chain — :INPUT ACCEPT [0:0] — and then one -A line per rule. Read the chain policies first: they are the default verdict for any packet that matches no rule, and an ACCEPT policy on INPUT means the firewall is open by default and the rules are only carving exceptions out of that openness, which is the wrong way round. Then read each rule left to right: -A INPUT says which chain, -i lo or -o eth0 restricts the interface, -s and -d restrict source and destination, -p tcp --dport 22 restricts protocol and port, -m conntrack --ctstate ESTABLISHED,RELATED restricts to reply traffic, and -j ACCEPT is the verdict. A rule with no -s, no -p and no --dport matches everything — that is the shape of a catch-all, and it is the most dangerous line in the file.
An nftables export from nft list ruleset reads differently but says the same things. There are no built-in chains to memorise: every table and chain is user-defined, and each chain declares its own hook (input, forward, output), its type, and its policy. A rule looks like tcp dport 22 accept or ip saddr 203.0.113.0/24 tcp dport 80 accept, and a chain may end with a bare drop as its final verdict. The new shapes to watch for are named sets — tcp dport @allowed_ports accept — where the actual port list lives in a separate set definition elsewhere in the file, so a rule that looks narrow may be wide once you expand the set. The same applies to verdict maps and to ct state established,related accept, which is the nftables spelling of the established-traffic rule. When you read an nftables ruleset, expand every set before you judge a rule; the rule text alone does not tell you what it allows.
Reading pf.conf and Windows Firewall exports by hand
A pf.conf file from OpenBSD or FreeBSD has one rule per line, in the shape pass in on em0 proto tcp from any to any port 22 or block in quick on egress from <martians> to any. The trap for readers coming from iptables is the matching rule: PF is last-match-wins by default, so the final matching rule decides — the exact opposite of iptables. The keyword quick flips a rule back to first-match behaviour, which is why most real pf.conf files are full of it; a ruleset written without quick has to be read bottom-up, and a deny at the top of such a file is only a suggestion. Other shapes to recognise: table <name> definitions that hold address lists, macros such as ext_if = "em0" that stand in for interface names, and set skip on lo0, which exempts loopback traffic from filtering entirely. If you see block all with no matching pass for loopback, the firewall is dropping its own local traffic — a classic self-inflicted outage.
A Windows Firewall export looks nothing like the Unix formats, and that is the point of knowing it exists. netsh advfirewall firewall show rule name=all prints one block per rule, with fields for the rule name, whether it is enabled, its direction (In or Out), its action (Allow or Block), the profile it applies to (Domain, Private, Public), the program or service it covers, and the local and remote ports. The audit questions are the same, only the spelling changes: an enabled inbound Allow rule with remote address Any and a local port of 3389 is the same world-open RDP hole as the iptables line that opens the same port. Two Windows-specific traps are worth knowing: a rule that is disabled still shows up in the export and must not be counted as live, and a rule scoped to the Public profile is the one that faces untrusted networks, so a loose Public rule is worse than the same rule scoped to Domain only.
The demo ruleset, walked rule by rule
The analyser ships with a ten-rule demo ruleset, and it is deliberately ugly: it packs the most common real-world mistakes into one short file. The chain policies set the tone — INPUT ACCEPT means the firewall is open by default, so every packet that matches no rule is let in. The first rule, -i lo -j ACCEPT, is correct and necessary: loopback traffic should always be allowed. The next two rules open SSH on port 22 and RDP on port 3389 to the whole world; SSH is a calculated risk that should at least be restricted by source, and RDP to the world is never acceptable — it is the port brute-force bots scan first. The fourth rule opens MySQL on port 3306 to the world, which is worse still: a database listener should never be reachable from the internet at all. Then comes the pair that teaches the most: an accept for port 80 from 203.0.113.0/24, immediately followed by a drop for port 80 from the single host 203.0.113.5 — a host inside that same /24. The drop is dead on arrival, as the next section explains.
The remaining rules finish the picture. Port 80 is accepted twice in identical lines — a duplicate, which is harmless on its own but a sign that two people edited the file without reading it. Then comes -A INPUT -j ACCEPT with no match at all: a catch-all that accepts every packet reaching it, which makes every rule below it unreachable — including the final --dport 443 -j DROP, a rule that was presumably meant to block HTTPS but will never see a packet. The analyser scores this file 24 out of 100 — Critical — with eleven findings: six high, three medium, one low and one informational. The fix order is the severity order: change the INPUT policy to DROP, close RDP and MySQL to the world, delete the catch-all, and then sort out the shadowed deny and the duplicate. Four changes, and the score moves from Critical to Good.
Order is the firewall: shadowing and accept-before-deny
In iptables and nftables, the first matching rule wins and evaluation stops. That single fact is the source of the most dangerous class of firewall bug: the shadowed rule. Take the demo pair — an accept for port 80 from 203.0.113.0/24, then a drop for port 80 from 203.0.113.5. The host 203.0.113.5 is inside the /24, so every packet from it matches the accept first, is accepted, and never reaches the drop. The drop rule is still in the file, still visible in every listing, and still doing nothing. Whoever added it believed they had blocked a hostile host; the firewall disagrees. The fix is mechanical: move the narrower rule above the broader one, or narrow the accept so it no longer covers the blocked host — -s 203.0.113.0/24 ! -s 203.0.113.5 — and then re-export and re-check, because the only way to be sure a deny works is to see it above every accept that could match the same traffic.
The same logic runs in the other direction. An accept placed above a deny is not a bug when the accept is narrower than the deny — that is the normal shape of a firewall: allow the one office network, then drop everything else. The pattern to distrust is the broad accept above a narrow deny, the duplicate rule, and the wide port range (a rule opening ports 1–65535 is a catch-all wearing a disguise). A healthy ruleset has a visible shape: loopback first, established and related traffic second, the specific allows next, and a final drop — or a DROP chain policy — at the bottom. If your ruleset does not have a final deny anywhere, every packet that falls through the list is decided by the chain policy, and if that policy is ACCEPT, the firewall is a list of suggestions rather than a barrier. Logging matters here too: a rule that drops without logging leaves no trace of what it blocked, so when something stops working you have no evidence to work from.
Dangerous ports, default policy, and what the analyser does — and does not do
Some ports should never be open to the internet, and the list is short enough to memorise. Remote desktop (3389) and VNC (5900) are the first thing automated scanners probe, because a guessed password there is a full desktop. Database ports — MySQL 3306, PostgreSQL 5432, Redis 6379, MongoDB 27017, Elasticsearch 9200 — are worse: they hold the data itself, and several of them have shipped with no authentication by default, so an open port has repeatedly meant a public database. SMB (445) and NFS (2049) expose file shares. SSH (22) is the exception that proves the rule: it is meant to be reachable, but only from known sources, with key authentication, and ideally behind a rate limit — world-open SSH is a finding, not a catastrophe, and the analyser scores it that way. The other half of the port question is the default policy: a chain policy of DROP means the ruleset only has to justify what it allows, while a policy of ACCEPT means it has to justify what it blocks, and no real ruleset ever justifies all of it.
The analyser checks all of this — it parses iptables-save, nftables, pf.conf and Windows Firewall exports, walks every rule in order, flags wide-open accepts, dangerous and risky ports, shadowed and duplicate rules, missing established-traffic handling, missing loopback handling, missing final deny, and missing logging, and scores the result from 0 to 100 with a CSV export of every finding and every rule. What it cannot do is see the network: it cannot tell whether a port that is open in the ruleset is actually listening on the host, it cannot see NAT rules that redirect traffic before the filter chains run, and it cannot expand a named nftables set or a pf table whose contents live in another file. It also reads pf.conf as first-match — the safe assumption for rulesets that use quick heavily — so a last-match ruleset without quick can shadow differently than the analyser reports. Use the score as a triage signal, not a certificate: a clean 100 means the file reads well, and the next step is still to test the live firewall with a port scan from outside.
Try it yourself: the demo ruleset, exactly as the analyser sees it
# Demo ruleset (iptables-save) — paste it into the analyser.
*filter
:INPUT ACCEPT [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp --dport 22 -j ACCEPT
-A INPUT -p tcp --dport 3389 -j ACCEPT
-A INPUT -p tcp --dport 3306 -j ACCEPT
-A INPUT -s 203.0.113.0/24 -p tcp --dport 80 -j ACCEPT
-A INPUT -s 203.0.113.5 -p tcp --dport 80 -j DROP
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -j ACCEPT
-A INPUT -p tcp --dport 443 -j DROP
COMMIT