DNS wildcards in Alias (Hosts)

Started by ArminF, February 04, 2020, 09:01:14 PM

Previous topic - Next topic
Greetings,
took a search before i posted as it did not ended with a good result.

Want to migrate my XG and later my UTM to OPNSense and installed a VM to prepare and cleanup my "old mess" to start with something fresh.

Recognized to use as much Aliases in the Firewall as possible to set groups and services together.
As i do have a lot of external WAN connection into the company from home i wanted to create some DNS wildcards.
Like *.company.com and so on. But was not able to archive it.
Do i really have to enter all in a single address or DNS?
I hope that you Pro's have a better way to archive this..

Otherwise i could life with it but just wanted to ask before i type and resolve several IPs to DNS.

thanks!
A
English: Never try, never know!
Deutsch: Unversucht ist Unerfahren!

January 27, 2022, 06:43:26 PM #1 Last Edit: January 27, 2022, 07:17:40 PM by chropnsense
Hi,

I'm also wondering, if this is possible or not (in pfSense it is not it seems). I just got the task to evaluate OPNSense and this is more or less a show stopper, if not possible to e.g. allow only MS Updates based on wildcard DNS:
https://docs.microsoft.com/en-us/windows-server/administration/windows-server-update-services/deploy/2-configure-wsus#211-connection-from-the-wsus-server-to-the-internet

http://windowsupdate.microsoft.com
http://*.windowsupdate.microsoft.com
https://*.windowsupdate.microsoft.com
http://*.update.microsoft.com
https://*.update.microsoft.com
http://*.windowsupdate.com
http://download.windowsupdate.com
https://download.microsoft.com
http://*.download.windowsupdate.com
http://wustat.windows.com
http://ntservicepack.microsoft.com
http://go.microsoft.com
http://dl.delivery.mp.microsoft.com
https://dl.delivery.mp.microsoft.com

Windows update here only as an example (could use WSUS), but also other use cases where need to allow CDN type *.domain.com

Edit: if not out-of-the-box possible, would it be possible to use cron and dnsmasq to poll through a list say once an hour (if that wildcard doesn't need to be resolved real time) and then use that IP list in an alias or similar?

Also curious about this, i have a number of hosts to insert and wildcard support would be great

In the mid-term Unbound blocklists will be able to run pattern matching with the switch to Python backend, but to my knowledge it's impossible to derive all IP addresses from all (unknown) subdomains as an alias in the firewall.


Cheers,
Franco

December 19, 2024, 01:49:47 PM #4 Last Edit: December 19, 2024, 01:55:03 PM by Shayoo Reason: content spacing
Hi, there,

The post is old, but I'll take the liberty of replying to it as I've been faced with the same problem. I was able to solve it based on other forum user entries.
Unfortunately it requires some tweaking to use wildcard domains.

References
- https://github.com/opnsense/core/issues/4145
- https://gist.github.com/PiDroid-B/078198bc84c1e8451d5fd331b46b332d

1. Create Aliases for Each wildcard

For each domain that uses a wildcard, create two aliases "External (advanced)" (e.g., _a and _b).
Then create a "Host(s)" alias containing the two externals created above.

Example Aliases for Multiple Domains:

- Microsoft_IPs_a External (advanced)
- Microsoft_IPs_b External (advanced)
- Microsoft_IPs Host(s)

2. Configure DNSMASQ on Port 53530

  •   Navigate to Services > DNSMASQ in the OPNsense web interface.
  •   Set DNSMASQ to listen on port `53530`.

Edit the DNSMASQ Configuration File

Access the firewall via CLI/SSH and edit the DNSMASQ configuration:

vi /usr/local/etc/dnsmasq.conf.d/dnsmasq-ipset.conf
Example Configuration File:
Replace the domain names and alias names with those relevant to your environment.

# Add the response for certain A/AAAA lookups to an OPNsense alias
ipset=/microsoft.com/windowsupdate.com/windows.net/Microsoft_IPs_a,Microsoft_IPs_b

# Uncomment these if Unbound is still your primary DNS server; otherwise, it may cause a loop
no-resolv
server=1.1.1.2
server=1.0.0.2
server=9.9.9.9
server=149.112.112.112

3. Configure Unbound DNS to Use DNSMASQ for specific domain resolution

Unbound DNS will forward specific queries to DNSMASQ to handle the aliases.
Navigate to Services > Unbound DNS > Overrides and create an entry in "Domain Overrides".

Example Entry:

Domain: microsoft.com
IP: 127.0.0.1@53530

Domain: windowsupdate.com
IP: 127.0.0.1@53530

Domain: windows.net
IP: 127.0.0.1@53530

4. Create a Cron Job to Flush Alias Entries

To prevent aliases from growing indefinitely and containing obsolete data, set up a cron job to flush the alias entries periodically (e.g., every 48 hours).
Create the Action Configuration File

vi /usr/local/opnsense/service/conf/actions.d/actions_alias-flush.conf
Add the Following Content:**

[flush]
command:/usr/local/bin/flock -n -E 0 -o /tmp/filter_update_tables_blk.lock /usr/home/alias-flush.sh
parameters: %s
type:script_output
message:Alias IPs flushed
description:Flush IPs from alias

Reload the system to make the cron job task appear

service configd restart
5. Create the Flush Script

This script will handle flushing the IPs from the specified alias.

vi /usr/home/alias-flush.sh
Add the Following Content:

#!/bin/sh
# Check if the alias name is provided as a parameter
if [ -z "$1" ]; then
    echo "Error: No alias name provided."
    exit 1
fi

ALIAS_NAME="$1"

pfctl -t "$ALIAS_NAME" -T flush

if [ $? -eq 0 ]; then
    echo "Alias '$ALIAS_NAME' flushed successfully."
    exit 0
else
    echo "Error while flushing alias '$ALIAS_NAME'."
    exit 1
fi

Make the Script Executable

chmod 755 /usr/home/alias-flush.sh

6. Schedule the Cron Jobs for Each Alias

Create cron job for each alias to flush them alternately (e.g., *_a` and *_b`) every 48 hours.
This rotation ensures that the aliases do not accumulate obsolete data.

Example Cron jobs:
enabled: Check
Minutes: 4
Hours: 3
Days of the months: *
Months: *
Days of the week: 1,3,5,7
Command: Flush IPs from alias
Parameters: Microsoft_IPs_a (Must be the exact name of you external alias)
Description: Flush -  Microsoft_IPs_a


enabled: Check
Minutes: 5
Hours: 3
Days of the months: *
Months: *
Days of the week: 2,4,6
Command: Flush IPs from alias
Parameters: Microsoft_IPs_b (Must be the exact name of you external alias)
Description: Flush - Microsoft_IPs_b

7. Configure Firewall Rules Using the Aliases

Create firewall rules based on your requirements and use the combined aliases.
When users perform DNS resolutions, the aliases will dynamically populate with the relevant IPs.

If Unbound has a cached resolution, the request might not be forwarded to DNSMASQ, preventing the client from communicating with the desired IP.
To resolve this issue, restart the Unbound service to clear the cache.



Hi everyone,

good question @ArminF and a good solution @Shayoo. I also faced this problem with OPNSense in our business environment and I took AI to help me build it (especially conformant for OPNSense usage and logic). In the end it's mostly the dnsmasq ipset solution but way easier to use for average admins and a little more user-friendly.
Please check my writing about it here: https://www.sysorchestra.com/wildcard-aliases-for-opnsense/
and the actual plugin here: https://github.com/martinseener/os-wildcard-aliases

I am using it for all queries on my Homelab for couple days now working just fine. Please let me know (here or DM) if you encounter issues or would love to see some improvements.

Hope this helps!

Best, Martin

But why, it's already available in core?

https://docs.opnsense.org/manual/dnsmasq.html#firewall-alias-ipset
Hardware:
DEC740

Remember the firewall deals with ghosts at the IP level. The convenience of using a host alias to resolve into an IP address is courtesy of opnsense, not necessarily the PF system.

You can bulk load host names into the alias by turning the tags into text. The number of hosts is finite, correct? It is better to have an accountable list so you know who can access your network. If we trusted any host from a domain that could allow some exotic spoofing.

It's not that hard, and the framework already does the heavy lifting of resolving them for you. I do something similar, including a use of dynamic DNS for most clients. I still have to set each site up and add the host in the respective alias. I wish my sites were as numerous as yours to have this problem haha. Hire more people to manage the access of your sites.

You can alternately allow a whole ASN have access, especially if an ISP is common amongst several clients. You can confine the access to a time interval so it is not open when unnecessarily, if the use case permits.

October 19, 2026, 11:09:38 AM #8 Last Edit: October 19, 2026, 11:14:07 AM by yourfriendarmando
I use a combination of hagezi and the windowsspyblocker lists.  You can get lists that work at both DNS and IP level of blocking,  and only allow windows updates without windows spy and  telling on you.

I did manually add some additional hosts on top of these lists such as the unique ID blocking for good measure, just enough before other functions like the store, begin to break. It is good to know those hosts in case I do want to break the store purposely.


Quote from: chropnsense on January 27, 2022, 06:43:26 PMHi,

I'm also wondering, if this is possible or not (in pfSense it is not it seems). I just got the task to evaluate OPNSense and this is more or less a show stopper, if not possible to e.g. allow only MS Updates based on wildcard DNS:
https://docs.microsoft.com/en-us/windows-server/administration/windows-server-update-services/deploy/2-configure-wsus#211-connection-from-the-wsus-server-to-the-internet

http://windowsupdate.microsoft.com
http://*.windowsupdate.microsoft.com
https://*.windowsupdate.microsoft.com
http://*.update.microsoft.com
https://*.update.microsoft.com
http://*.windowsupdate.com
http://download.windowsupdate.com
https://download.microsoft.com
http://*.download.windowsupdate.com
http://wustat.windows.com
http://ntservicepack.microsoft.com
http://go.microsoft.com
http://dl.delivery.mp.microsoft.com
https://dl.delivery.mp.microsoft.com

Windows update here only as an example (could use WSUS), but also other use cases where need to allow CDN type *.domain.com

Edit: if not out-of-the-box possible, would it be possible to use cron and dnsmasq to poll through a list say once an hour (if that wildcard doesn't need to be resolved real time) and then use that IP list in an alias or similar?

Quote from: Monviech (Cedrik) on October 19, 2026, 09:49:43 AMBut why, it's already available in core?

https://docs.opnsense.org/manual/dnsmasq.html#firewall-alias-ipset
Hi Cedrik,

my bad, you're actually right! Somehow I overlooked this as I don't use dnsmasq and really haven't checked the recent docs for it.
I checked again why someone would rather use the new plugin than dnsmasq's option and to conclude first: Keep using dnsmasq and use it for this case. It will work perfectly fine! 100% agree.

Some things to mention though:

- For people (like me) using Adguard Home + Unbound DNS, this is a good option and easy to use
- My plugin uses the DNS TTLs instead of fixed windows and prunes
- dnsmasq's ipset code only adds A/AAAA addresses. The plugin also adds ipv4hint/ipv6hint from HTTPS and SVCB records
- The plugin can return a SERVFAIL if it can't update the ipset, dnsmasq would not do that, potentially resulting in a not-working allow rule
- And the plugin has some more GUI based Stats/Learned Addresses.

So I don't say the dnsmasq option is not good. It definitely is! And everyone where this works and is sufficient should use it, as it's also not only covered by community effort.
For everyone else, especially the occasional admin or people who use different DNS setups, the Plugin might be very handy (as it is for me).
So I would say, both are good options and have their use cases.

Have a nice day and thanks for replying :)

Hallo Martin Seener,

Thank you for putting the work into creating this plugin! I really hope it'll make it into the core sometime soon.

I do have a question though - on your website you are mentioning this model:

client ──DNS──▶ AdGuard Home / Unbound ──▶ Wildcard Aliases ──▶ upstream resolver

Which makes me wonder - would your plugin necessarily have to forward the lookup to an external upstream resolver? If currently that is  the case, is there any way to insert it before Unbound, and let Unbound be the actual resolver? Or could at least the plugin refer ONLY its own resolution requests to an upstream server, and let Unbound take care of all client resolution (most of which isn't in most case alias-based)?

Danke,
OC

Hi OC,

thanks for the kind words!

"Upstream resolver" in that diagram just means "whatever actually answers the daemon's queries". It doesn't have to be external. The daemon has no cache and doesn't resolve anything recursively on its own. It forwards each query, reads the answer, adds the addresses to the pf table, and passes the answer back. So Unbound can stay your real recursive resolver. There are two ways to do that:

1. Daemon in front of Unbound, Unbound does the resolving ("all queries" mode)
This is what you described.
- In Services → Unbound DNS → General, move Unbound to port 5353, on Localhost only if you can.
- Let the daemon listen on port 53: 127.0.0.1:53 plus your interface addresses, e.g. 127.0.0.1:53,192.168.1.1:53.
- Set the daemon's upstream to 127.0.0.1:5353.

Unbound still does the full recursion, caching, DNSSEC and blocklists. Queries that don't match a pattern pass through unchanged.

The one downside: Unbound now sees every query as coming from 127.0.0.1, so per-client statistics and blocklist reports stop being useful. With AdGuard Home in front, AdGuard keeps the client addresses, and the chain is AdGuard → daemon → Unbound. Again, nothing external.

2. Unbound handles everything, only watched domains go through the daemon ("watched domains only" mode)
This is your second idea. In Services → Unbound DNS → Query Forwarding, add the watched domain (e.g. blob.core.windows.net) with server 127.0.0.1, port 5354. All other client queries never touch the plugin.

The catch: the daemon's upstream can't be this same Unbound, because Unbound would forward the query straight back to the daemon and loop. So in this mode only the watched domains use an outside resolver: your ISP's, a public one, or another resolver on your network. Also, Unbound caches CNAME targets separately. If a watched name has a CNAME pointing outside the watched domain (common with *.trafficmanager.net), later lookups of that target can bypass the daemon. Add those target domains as patterns too, or use option 1.

For exact names (no *), the daemon also resolves them itself, on start and before they expire. Those queries go to the same configured upstream, so in option 1 they go to Unbound as well.

If you want everything resolved locally with no outside resolver involved, option 1 is the way to go.

Hope this helps,

Best, Martin

Hello Martin,

Thank you for getting back to me - I really appreciate it!

I would like to keep Unbound as the "default/basic" resolver for all my clients, both for its ability to resolve recursively, as well as for the stats dashboards, which I started liking and looking at quite frequently (and which led to interesting insights). I would not want to relinquish that.

Therefore I would find it OK to only forward 'special' domains that I want to wildcard (and access through different gateways, which reach different outbound VPNs through PBR).

It is thus assumed that for each 'special' wildcard URL I would have to set up a conditional forwarder in Unbound (Services: Unbound DNS: Query Forwarding), and also an entry in your plugin, assuming that I want to associate each special domain (or more than one, but not all of them) with a certain (different for each domain or group of domains sharing the similar intended gateway) firewall alias, which I then can associate (through PBR) with the designated gateway?

Also - would I have an option (in your plugin) to have a catch-all rule, where all the forwarded queries (to say port 127.0.0.1:5353) are automatically associated with a designated firewall alias? (meaning that if I only set a *.domain.tld in Unbound, but nothing related to it in your plugin, that all such forwarded queries automatically land in a designated firewall alias)

And the last question: I have never dealt with non-core plugins. What can I expect when updating OPNsense? Will your plugin still be there and work? Do I have to reinstall? Can it break and if so, how do I cleanly remove/reinstall it? Can it somehow break OPNsense? Sorry for the silly questions, but I am still a relatively new user of OPNsense (yet very excited and satisfied), and patches always still raise my heartbeat a bit :)

Notwithstanding the above - I salute the apparition of your plugin, and think it has its place and merit. Of course, it would be great if wildcard aliases were embedded into Unbound - but, as this isn't the case, we cannot add a feature request to either one of the teams (OPNsense/Unbound), and thus have to deal with it ourselves.

Danke sehr,
OC

Today at 03:58:57 PM #13 Last Edit: Today at 04:01:34 PM by mooh
Unbound can still be used as the main DNS resolver. Only redirect "Microsoft.com" and any other wildcard domain to your dnsmasq listening at 127.0.0.1:53053 for example. Don't use port 5353. It's reserved for mDNS. Queries not redirected will work as usual.

I have been running this solution for a while but don't do it anymore. The core of the problem is that the external alias populated via dnsmasq "ipset" doesn't keep the TTLs around. Dnsmasq itself doesn't clean up when a TTL expires. External tools don't know the time of expiry, so there is no way of deleting entries at the right time. If an entry is cleaned up before it expires, DNS still resolves the now deleted address from cache, not update the alias, and the rule will fail. Clean up too late and the alias may allow traffic to an address that provides a very different service. This is particularly relevant in times of massive cloud services.

In the end, there is no good solution for DNS level issues on the IP level. I'd also argue that a service provider that can't provide a list of specific DNS names for its service should reconsider the design. But in my experience they will just tell everyone to use a proper firewall ...

Quote from: mooh on <strong>Today</strong> at 03:58:57 PMUnbound can still be used as the main DNS resolver. Only redirect "Microsoft.com" and any other wildcard domain to your dnsmasq listening at 127.0.0.1:53053 for example. Don't use port 5353. It's reserved for mDNS. Queries not redirected will work as usual.
I have set that up, it works. But it's clunky, in a number of ways, including regarding adding a new entry, which needs to be done in at least two places (Unbound and Dnsmasq) - and potentially a new rule as well.

Quote from: mooh on <strong>Today</strong> at 03:58:57 PMI have been running this solution for a while but don't do it anymore. The core of the problem is that the external alias populated via dnsmasq "ipset" doesn't keep the TTLs around. Dnsmasq itself doesn't clean up when a TTL expires. External tools don't know the time of expiry, so there is no way of deleting entries at the right time. If an entry is cleaned up before it expires, DNS still resolves the now deleted address from cache, not update the alias, and the rule will fail. Clean up too late and the alias may allow traffic to an address that provides a very different service. This is particularly relevant in times of massive cloud services.
Indeed - and that's something Martin's plugin solves - his plugin is pruning the record after its own original TTL expires (rather than relying on a one-size-fits-all discretionary TTL).

Quote from: mooh on <strong>Today</strong> at 03:58:57 PMIn the end, there is no good solution for DNS level issues on the IP level. I'd also argue that a service provider that can't provide a list of specific DNS names for its service should reconsider the design. But in my experience they will just tell everyone to use a proper firewall ...
In my experience (including at least two of the top enterprise firewall vendors), the big guys are also not flawless when it comes to wildcard-based rules/policies. And I agree with you - the current CDN model, relying on Anycast IPs makes this very hard to implement reliably - but cybersecurity isn't a perfect science, yet rather (IMO) a way to continuously work on "making things safeR" (not entirely safe). And, for getting to "safer", an imperfect wildcard firewall alias is better than nothing.