Rule order & shadowed rules

The first matching rule wins

Lesson 2 of 20 · 11 min · Module 1: How a firewall thinks

A firewall reads its rules from the top and stops at the first one that fits the packet. The same rules in a different order are a different policy.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Predict which rule decides a packet
  • Recognise a shadowed rule
  • Order an exception before the general rule

The idea

Because evaluation stops at the first match, a broad rule placed early can hide a narrower rule below it. The hidden rule is called shadowed: it is in the configuration, but the traffic it was written for never reaches it.

default deny
deny any from 10.0.5.9 to 10.0.6.0/24
allow tcp from 10.0.5.0/24 to 10.0.6.0/24 port 80

01Follow one packet down the list

Take the packet and test it against rule 1. If it fits, the action of rule 1 is the answer and the firewall looks no further. Only if it does not fit does rule 2 get a turn. The Output tab of this lab names the rule that decided every test packet.

02Put the exception first

The kiosk in the lobby has the address 10.0.1.99, which is inside lan. If "allow tcp from lan to web port 443" comes first, the kiosk matches it and is allowed, and a deny for the kiosk further down is never consulted. An exception is narrower than the rule it limits, so it goes above that rule. Most rule sets are ordered this way, from specific to general.

03Where order lives on real devices

nftables evaluates the rules of a chain from top to bottom. FortiGate and Huawei USG evaluate policies in their displayed sequence, which you change with a move command. The policy ID or name does not decide the order.

# FortiGate: place policy 7 above policy 3
config firewall policy
    move 7 before 3
end

# Huawei USG: place a rule above another
security-policy
 rule move block_kiosk before staff_to_web

# nftables: insert at the top of the chain instead of appending
nft insert rule inet filter forward ip saddr 10.0.1.99 drop

Your turn

The kiosk in the lobby (kiosk, 10.0.1.99) must not reach anything in servers. The rest of lan still needs the website on web, tcp port 443. Both rules are already written, but the kiosk gets through. Fix the policy.

What the checks look for

  • The kiosk cannot reach the website
  • A deny rule stops the kiosk, not only the default
  • Staff still reach the website
  • Every rule decides at least one test packet
Try it in the editor →

Take this with you

Order is part of the policy. Put narrow exceptions above the broad rules they limit, and check which rule actually matched.

Go a little further

Add the word log to the end of the deny rule. On a real firewall that is how you get a record of every blocked attempt from the kiosk.

Learn Firewall Foundations for free

20 short lessons with real code and instant checks.

See the whole course →