Need an option - if it isn't there ...

Started by BenKenobi, October 19, 2026, 01:07:42 AM

Previous topic - Next topic
Really need an option to force firewall rules before NAT - I need to keep scumbags and scammers out but OpnSense lets them through - as I have found here NAT is applied before Firewall rules - I want an option not to - if I block a country I expect it blocked even from NAT traversal or I need to put a firewall on every internal box - this isn't efficient.

If I have missed something and there is a setting could somebody expose it please.

October 19, 2026, 06:42:06 AM #1 Last Edit: October 19, 2026, 06:46:13 AM by Monviech (Cedrik)
I mean you can use a GeoIP alias in your NAT rules as well, then it won't even be translated.

In a NAT rule in advanced mode its "Do not NAT".

BTW:

NAT is not allowing traffic, its only translation
Firewall rules are allowing traffic
Hardware:
DEC740

Don't use destination NAT with "pass", then.
Deciso DEC750
People who think they know everything are a great annoyance to those of us who do. (Isaac Asimov)

October 19, 2026, 11:59:18 PM #3 Last Edit: October 20, 2026, 12:00:51 AM by BenKenobi
Well despite the responses - I mean NAT without a 'pass' is pretty pointless -

I take issue with "Firewall rules are allowing traffic" no they aren't - the NAT bypasses firewall rules completely.

I found the solution here, it is still wrong IMHO that firewall rules are not enforced before NAT - should only be presented for NAT if it does not violate any block list which is exactly what the thread linked below does.

https://forum.opnsense.org/index.php?topic=46122.0

Quote from: BenKenobi on October 19, 2026, 11:59:18 PMWell despite the responses - I mean NAT without a 'pass' is pretty pointless -

I take issue with "Firewall rules are allowing traffic" no they aren't - the NAT bypasses firewall rules completely.

It isn't. Without pass the incoming packet is first put through destination NAT and then firewall rules apply. That's a perfectly fine way to implement granular control. Only issue is that your firewall rule needs to have the source address as is (no change here) and the destination address *after NAT*. What's the problem with that?

HTH,
Patrick
Deciso DEC750
People who think they know everything are a great annoyance to those of us who do. (Isaac Asimov)

The problem is one of precedence - NAT should NOT be executed before any blocks I set up - this defeats rules specifically configured to protect internal servers from known bad actors whether they be government sponsored or otherwise. The firewall rules are NOT evaluated after NAT - they're completely ignored.

By not enforcing rules that I set up the firewall is in effect providing an open door via any port I have open for legitimate means.

The solution I found seems to work but configuring the firewall to process block rules before NAT would be a much better option.

Quote from: BenKenobi on October 23, 2026, 01:59:20 PMThe firewall rules are NOT evaluated after NAT - they're completely ignored.

This is only true IF you explicitly set "Firewall rule" to "Pass" in the NAT rule in question.
This setting is not the default. You chose to do so.
Deciso DEC750
People who think they know everything are a great annoyance to those of us who do. (Isaac Asimov)

Also I feel like what I said was also misunderstood.

You can limit what is translated by only NATing from certain sources, and you can limit what is passed by controlling the firewall rules the same way.

I don't understand the issue.
Hardware:
DEC740

You can't NAT from certain sources if you don't know what they are - and to web servers and mail servers they can not be known in advance.

Which part of NAT bypasses firewall rules isn't being understood, I don't think the misunderstanding here is mine.

Any firewall 'blocks' should be evaluated BEFORE any packets are offered up for NAT, as it stands those rules are completely ignored, if the firewall executes a destination NAT the firewall rules are not enforced so anything set in them is irrelevant.

I only managed to make this work by tricking the NAT - by stating that the 'source' must not be in any of these lists using a network group i.e. a network group rule inverted as the NAT source.

For the umpteenth time: NAT does not bypass firewall rules if you do not set "Firewall rule" to "Pass" in the NAT rule.
Deciso DEC750
People who think they know everything are a great annoyance to those of us who do. (Isaac Asimov)

Well I have evidence that it does from logs on the servers so we will leave it at that, IP's that are specifically and deliberately blocked are still appearing.

Not debating it any more

Show your NAT rule and the firewall rule and I'll explain.
Deciso DEC750
People who think they know everything are a great annoyance to those of us who do. (Isaac Asimov)

Don't know how you want that shared - I can't post images here and I'm not setting them up in an external repository, I suppose I could extract the XML from a backup.

    <rule uuid="e3035897-2778-4205-ae56-aeb7ef3203a5">
      <sequence>1000</sequence>
      <disabled>0</disabled>
      <nordr>0</nordr>
      <interface>wan</interface>
      <ipprotocol>inet</ipprotocol>
      <protocol>tcp</protocol>
      <source>
        <network>BlockBeforeNAT</network>
        <port/>
        <not>1</not>
      </source>
      <destination>
        <network>82.68.34.122</network>
        <port>143</port>
        <not>0</not>
      </destination>
      <target>ATAMailServer</target>
      <local-port>143</local-port>
      <poolopts/>
      <log>0</log>
      <category/>
      <descr/>
      <tag/>
      <tagged/>
      <nosync>0</nosync>
      <natreflection/>
      <pass/>
      <audit>eyJjcmVhdGVkIjp7InVzZXJuYW1lIjoicm9vdCIsInRpbWUiOiIxNzgzNTg4NzIwLjkxIiwiZGVzY3JpcHRpb24iOiJcL2FwaVwvYXBpLnBocCBtYWRlIGNoYW5nZXMifSwidXBkYXRlZCI6eyJ1c2VybmFtZSI6InJvb3RAMTAuMTAwLjAuMTMiLCJ0aW1lIjoiMTc5MjQ0NjcxMC43MiIsImRlc2NyaXB0aW9uIjoiXC9hcGlcL2FwaS5waHAgbWFkZSBjaGFuZ2VzIn0sInVzZXJkYXRhIjp7Im5vdGUiOiIifX0=</audit>
      <associated-rule-id/>
    </rule>

This is the rule - note that it contains specific rules designed to block anyone that triggers a security issue on the internal servers - without the 'not' rule in the source the IP addresses that are blocked continue to trigger alerts so they are bypassing the rule below.

          <rule uuid="10814b62-534c-46a2-9b02-32e5f624bc4c">
            <enabled>1</enabled>
            <statetype>keep</statetype>
            <state-policy/>
            <sequence>51</sequence>
            <action>block</action>
            <quick>1</quick>
            <interfacenot>0</interfacenot>
            <interface>wan</interface>
            <received-on-not>0</received-on-not>
            <received-on/>
            <direction>in</direction>
            <ipprotocol>inet</ipprotocol>
            <protocol>any</protocol>
            <icmptype/>
            <icmp6type/>
            <source_net>Censys,EMailRangePerm,InetCens,InetCens2,Shodan,Shadow,VariousScumbags,BlacklistedSources,BlacklistJSON,TalosV4,GeoIP,range_bans</source_net>
            <source_not>0</source_not>
            <source_port/>
            <destination_net>any</destination_net>
            <destination_not>0</destination_not>
            <destination_port/>
            <divert-to/>
            <gateway/>
            <replyto/>
            <disablereplyto>0</disablereplyto>
            <log>0</log>
            <allowopts>0</allowopts>
            <nosync>0</nosync>
            <nopfsync>0</nopfsync>
            <statetimeout/>
            <udp-first/>
            <udp-multiple/>
            <udp-single/>
            <max-src-nodes/>
            <max-src-states/>
            <max-src-conn/>
            <max/>
            <max-src-conn-rate/>
            <max-src-conn-rates/>
            <max-pkt-rate-number/>
            <max-pkt-rate-seconds/>
            <overload/>
            <adaptivestart/>
            <adaptiveend/>
            <prio/>
            <set-prio/>
            <set-prio-low/>
            <tag/>
            <tagged/>
            <tcpflags1/>
            <tcpflags2/>
            <tcpflags_any>0</tcpflags_any>
            <categories/>
            <sched/>
            <tos/>
            <shaper1/>
            <shaper2/>
            <description>Scumbags</description>
            <audit>eyJjcmVhdGVkIjp7InVzZXJuYW1lIjoicm9vdEAxMC4xMDAuMC4xMyIsInRpbWUiOiIxNzg5NTYxODkyLjU3IiwiZGVzY3JpcHRpb24iOiJcL2FwaVwvYXBpLnBocCBtYWRlIGNoYW5nZXMifSwidXBkYXRlZCI6eyJ1c2VybmFtZSI6InJvb3RAMTAuMTAwLjAuMTMiLCJ0aW1lIjoiMTc4OTU2MTg5Mi41NyIsImRlc2NyaXB0aW9uIjoiXC9hcGlcL2FwaS5waHAgbWFkZSBjaGFuZ2VzIn0sInVzZXJkYXRhIjp7Im5vdGUiOiIifX0=</audit>
          </rule>

Some of the triggers on the mail server in particular are brute force attempts, these are caught by fail2ban, invalid <CR> characters and injection attempts, I regularly check the logs on the servers. When I added a new IP address I found that I had already blocked it, so I dug deeper ...

October 23, 2026, 07:50:51 PM #13 Last Edit: October 23, 2026, 07:53:26 PM by Bob.Dig
Quote from: BenKenobi on October 19, 2026, 01:07:42 AMif I block a country I expect it blocked even from NAT traversal or I need to put a firewall on every internal box - this isn't efficient.
You are wrong about it. While it is true, that NAT is before firewall rules, after the NAT, that traffic still must go through the firewall rules and therefore is blocked. No need for doing it again on some hosts.

You might have other problems with your configurations. Always interesting to see that people with ten years here still don't know, how this works.

You can attach images if you click on "Preview" before saving or use the full edit mode instead of the quick reply from the start.

I think I get what you are doing from the XML, though. The block rule is pretty unspecific in terms of protocol and destination, but should nonetheless work.

Are you sure you placed it above the corresponding allow rule for your inbound NAT? Quick firewall rules are executed in order.
Deciso DEC750
People who think they know everything are a great annoyance to those of us who do. (Isaac Asimov)