Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login
    Introducing Netgate Nexus: Multi-Instance Management at Your Fingertips.

    pfsense-rule-order: script to keep firewall rules in the right order automatically

    Scheduled Pinned Locked Moved Firewalling
    21 Posts 5 Posters 4.3k Views 6 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ngfN Offline
      ngf
      last edited by

      The actual issue I ran into was pfBlockerNG's cron job moving my "Block IoT" rule below some Allow rules every hour.
      it was already in the right place, pfBlockerNG just kept pushing it down.
      That's what the script is for.

      Regarding my version, yes, i know. i'm in the process of replacing the pfsense box.

      tinfoilmattT 1 Reply Last reply Reply Quote 0
      • tinfoilmattT Offline
        tinfoilmatt LAYER 8 @ngf
        last edited by

        There are package settings which address auto-added rule ordering. Do those not cover your situation?

        ngfN 1 Reply Last reply Reply Quote 0
        • ngfN Offline
          ngf @tinfoilmatt
          last edited by

          @tinfoilmatt said in pfsense-rule-order: script to keep firewall rules in the right order automatically:

          There are package settings which address auto-added rule ordering. Do those not cover your situation?

          I looked at the pfBlockerNG rule order settings but they control where pfBlockerNG places its own auto rules relative to manual ones, not the order of manual rules themselves.
          my issue was specifically manual rules getting reordered relative to each other, so those settings didn't help in my case.

          tinfoilmattT 1 Reply Last reply Reply Quote 0
          • tinfoilmattT Offline
            tinfoilmatt LAYER 8 @ngf
            last edited by tinfoilmatt

            my issue was specifically manual rules getting reordered relative to each other

            This wouldn't be caused by pfBlockerNG's cron job. The order of non-pfBlockerNG auto-added rules (i.e., 'manual rules') could only be changed manually.

            What do you have for that package setting—"Firewall > pfBlockerNG > IP > IP Interface/Rules Configuration > Firewall 'Auto' Rule Order"?

            ngfN 1 Reply Last reply Reply Quote 0
            • ngfN Offline
              ngf @tinfoilmatt
              last edited by

              @tinfoilmatt said in pfsense-rule-order: script to keep firewall rules in the right order automatically:

              Firewall 'Auto' Rule Order

              aa4b408f-ea7b-4cc8-a7c8-c0cffee10854-image.png

              tinfoilmattT 1 Reply Last reply Reply Quote 0
              • tinfoilmattT Offline
                tinfoilmatt LAYER 8 @ngf
                last edited by

                Interesting. So those last two settings in the drop-down can and do affect 'manual rule' ordering.

                So then why not use the default setting which leaves "All other rules" unaffected by pfBlockerNG?

                ngfN 1 Reply Last reply Reply Quote 0
                • ngfN Offline
                  ngf @tinfoilmatt
                  last edited by

                  @tinfoilmatt I was not aware that this setting also affects manual rule ordering.
                  i will change back to the default and disable the script temporarily to see if that alone solves the problem.
                  thank you very much.

                  tinfoilmattT 1 Reply Last reply Reply Quote 0
                  • tinfoilmattT Offline
                    tinfoilmatt LAYER 8 @ngf
                    last edited by tinfoilmatt

                    Additionally (and with the default setting there)—if for whatever reason you need a pass rule above pfBlockerNG's auto-added block rules, then the way to do that would be by way of the "Permit" feed group action options (i.e., actions "Permit Inbound" for WAN interface/s, "Permit Outbound" for LAN interface/s, or "Permit Both"). This would be the cleanest and most automated way to "whitelist" any pfBlockerNG false positives.

                    ngfN 1 Reply Last reply Reply Quote 0
                    • ngfN Offline
                      ngf @tinfoilmatt
                      last edited by

                      @tinfoilmatt said in pfsense-rule-order: script to keep firewall rules in the right order automatically:

                      Additionally (and with the default setting there)—if for whatever reason you need a pass rule above pfBlockerNG's auto-added block rules, then the way to do that would be by way of the "Permit" feed group action options (i.e., actions "Permit Inbound" for WAN interface/s, "Permit Outbound" for LAN interface/s, or "Permit Both"). This would be the cleanest and most automated way to "whitelist" any pfBlockerNG false positives.

                      Thanks, I'll check with the default setting.
                      If that solves the reordering problem, the script is still useful as a general safety net for rule ordering, but the main reason here was clearly misconfiguration on my part.

                      tinfoilmattT 1 Reply Last reply Reply Quote 0
                      • tinfoilmattT Offline
                        tinfoilmatt LAYER 8 @ngf
                        last edited by

                        At one point, I would've spent a lot of time parsing that blue infoblock in my own mind. I'm certain there are good use cases for the non-default options. But it either never occured to me, or I had forgotten that those last two do affect non-pfBlockerNG rule ordering. Thanks for the refresher.

                        By the way, I like what you're doing with your blocklist-manager tool.

                        ngfN 1 Reply Last reply Reply Quote 0
                        • ngfN Offline
                          ngf @tinfoilmatt
                          last edited by

                          @tinfoilmatt same here, i had no idea that setting affected manual rules until now, good to know.

                          🙏 thanks for the kind words on blocklist-manager, glad it's useful!

                          GertjanG JeGrJ 2 Replies Last reply Reply Quote 0
                          • GertjanG Offline
                            Gertjan @ngf
                            last edited by

                            @ngf

                            A newer pfSense will also give you access to the latest version pfBlockerng.
                            Your issue might auto solve itself.

                            No "help me" PM's please. Use the forum, the community will thank you.

                            1 Reply Last reply Reply Quote 0
                            • JeGrJ Offline
                              JeGr LAYER 8 Moderator @ngf
                              last edited by JeGr

                              @ngf I also wanted to add: Why rely on pfBlockerNG to push the rules at the location you want and not doing it yourself manually?

                              Just go into pfBlocker and change the type from "deny XY" to "alias deny". That way pfB created the aliases you already know (e.g. pfB_pri1_v4) and let's your rules alone. Then you do the rules you actually want/need, not some automatism, that can get it wrong. The auto-building is fine for simple use cases and homelabs often but when you're building a bit of a more complex policy ruleset, it's immensly easier to just use Alias deny/permit for deny/permit-lists and do the rules yourself.

                              Also you can do "outbound" blocking (e.g. block rules for your LANs/VLANs) much easier, when you create an interface group. E.g. create a group "Blocklist" and add all internal interfaces you want to protect. Then create rules like "reject from <grp_networks> to <pfB_pri1_v4" so you can easily turn on/off logging and reject stuff on the inside (faster drop on a client) and block (silent drop) it on WAN. That way you only manage the rules on WAN(s) and the blocklist group and as the targets in those blocklists are external IPs anyways, it's not that bad to block them in a group ahead of the rule processing on the interface as you can't accidentally block something on the inside with it. We do so in many cases even with customers with great success :)

                              Cheers :)

                              PS: Although great job on the blocklist manager, I don't really get the problem. The intro says IPs slipped through that FireHOL would've catched. Yeah, add their blocklist and be done. Or add Q-Feeds, too, etc. etc. Where's the problem on having duplicates? Even the pfBlocker feeds have duplicates, it's completely normal with blocklists depending on which source lists you build your lists about. I simply don't understand the need to deduplicate that hard on something as simple as a blocklist. Besides, pfB already ca do that, so perhaps there's something I don't understand here?
                              And if all you wanted was one big list to block anything, then you can already use pfBlockers Override in the Feeds to make every list PRI1 so everything is in PRI1 (or something else) and you have one big list with sublists that gets merged, deduplicated and loaded. Multiple tiers of lists is only really neccessary if you want to use them differently on various interfaces. E.g. use stuff like Firehol3 (with false positives) or others that can have overblocking on a separate tier list to enable on networks like guest wifi or kids or protecting special usergroups while e.g. only blocking relatively safe one on server subnets etc. so they won't have to deal with overblocking and somehow breaking a software update or stuff :)

                              Don't forget to upvote 👍 those who kindly offered their time and brainpower to help you!

                              If you're interested, I'm available to discuss details of German-speaking paid support (for companies) if needed.

                              ngfN 1 Reply Last reply Reply Quote 2
                              • ngfN Offline
                                ngf @JeGr
                                last edited by

                                @JeGr Thanks, really appreciate the detailed response. The alias deny approach is something I wasn't aware of, good to know. That said, my setup is pretty stable at this point, and I'm not looking to redo the rule set.

                                The script works, and honestly the numbered rules are something I'd want to keep regardless it just makes things much cleaner to look at and easier to manage.

                                Knowing that the order didn't change without me touching anything helps my peace of mind.

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post
                                Copyright 2026 Rubicon Communications LLC (Netgate). All rights reserved.