Order of NAT & policy

Know what the policy sees

Lesson 14 of 20 · 12 min · Module 4: Address translation

A rule can look correct and still never match, because by the time the policy reads the packet, the address in it has already changed.

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…

  • State the order of destination NAT, policy and source NAT
  • Write a policy rule for traffic that was translated
  • Diagnose a rule that never matches because of NAT

The idea

Firewalls work in a fixed order: destination NAT first, then the policy, then source NAT. So the policy sees the translated destination (the real server and its real port) and the original source. A rule written for the public address of a forwarded service matches nothing.

dnat tcp from untrust to public port 2222 use bastion port 22
# the policy sees destination bastion, port 22
allow tcp from untrust to bastion port 22

01Follow the packet through the three stages

A partner connects to 203.0.113.10 port 8443. Stage one, destination NAT, rewrites it to 10.0.2.10 port 443. Stage two, the policy, now reads a packet to 10.0.2.10 port 443. Stage three, source NAT, would apply only if a snat line matched.

02Write the rule for what arrives at the policy

The rule must name the server web and port 443. A rule naming public and port 8443 describes a packet that no longer exists at that stage. In the Output tab, a denied packet that was translated shows what the policy saw.

03The same order on real devices

nftables translates the destination in prerouting, before the forward chain. Huawei USG applies the NAT server mapping first, and its security policy names the private address. FortiGate hides the step: the policy names the virtual IP object, which stands for the translated server.

# Huawei USG: the policy uses the inside address
nat server partner protocol tcp global 203.0.113.10 8443 inside 10.0.2.10 443
security-policy
 rule name partner_portal
  source-zone untrust
  destination-zone dmz
  destination-address 10.0.2.10 mask 255.255.255.255
  service https
  action permit

# FortiGate: the policy destination is the VIP object
config firewall policy
    edit 12
        set dstaddr "PARTNER_VIP"
    next
end

Your turn

Partners should reach the portal on public port 8443, which is forwarded to web port 443. The translation is correct, but the allow rule was written for the address partners type, and it never matches. Fix the allow rule so it describes the packet the policy sees.

What the checks look for

  • Partners reach the portal
  • The allow rule names the real server and port
  • Port 443 on the public address stays closed
  • Every rule is used
Try it in the editor →

Take this with you

Destination NAT, then policy, then source NAT. Write policy rules for the translated destination and the original source.

Go a little further

Think the outgoing direction through as well: with "snat from trust to untrust", which source address does a rule for office web browsing have to name?

Learn Firewall Foundations for free

20 short lessons with real code and instant checks.

See the whole course →