Skip to content
subuilds.dev

← logbuild #20 passedc04a049

OPNsense: Block Websites and Force DNS Through Your Firewall

How to block chosen websites with firewall aliases and stop DNS bypass with a NAT redirect. Also covers LAN hostnames in logs and category blocking for gambling, malware, and torrent sites.

· 18 min read

You installed OPNsense and traffic flows. Now you want to control what goes through it. You want to block specific sites and stop clients that bypass (go around) your DNS. You want to see hostnames in the logs, not raw IPs. And you want to block whole categories such as gambling, malware, and torrent sites.

DNS is the service that turns a name like reddit.com into an IP address. DNS enforcement means every device must use your DNS server. This post treats DNS enforcement the way a production network does: several defense layers, no single way around them, and a test after each step. Enterprise networks (the networks of large companies) use the same ideas. Here they are sized for a homelab.

Environment: OPNsense 26.1.2, Unbound DNS Resolver, LAN subnet 10.10.1.0/24.


In Simple Words#

The problem
You block a website on your firewall, but people can still open it. A device only has to ask another DNS server for the address.
The root cause
DNS blocking only works when clients use OPNsense as their DNS server. A client with 8.8.8.8 or 1.1.1.1 set by hand never asks OPNsense. Encrypted DNS (DoT and DoH) is one more way around.
The solution
Block the site by name in Unbound and by IP in the firewall. Then close the ways around: drop DNS on port 53 to other servers, drop DoT on port 853, and drop DoH to known providers. Last, add blocklists for whole categories.
Think of it like this
The guard at the door of a building gives people the address of every place they want to visit. For a blocked place, the guard gives no address. Some people try to ask a stranger outside, so the guard also stops every question that goes to someone else.

How Blocking Works on OPNsense#

Normal Traffic Flow (No Blocking)#

Before you add any rules, look at what happens when you open a browser and type reddit.com:

  1. The browser asks Unbound, the DNS resolver in OPNsense, for the IP address of reddit.com.
  2. Unbound answers with the real IP: 151.101.1.140.
  3. The browser opens a TCP connection to that IP on port 443 (HTTPS).
  4. The firewall allows the packet, applies NAT and sends it out the WAN.
  5. Reddit answers and the page loads.
5 / 5
  1. Your browser asks Unbound to resolve reddit.com. Unbound is the DNS resolver in OPNsense. To resolve a name is to find its IP address
  2. Unbound returns the real IP (151.101.1.140)
  3. Your browser opens a TCP connection to that IP on port 443 (HTTPS)
  4. OPNsense’s firewall allows the packet, applies NAT, and sends it out the WAN
  5. Reddit answers and the page loads

Every step works because nothing blocks it. Now look at where each defense layer stops this flow.

Six Defense Layers#

Each layer acts on a different step of the flow above. The layers form three groups:

  1. A site that is not on any list passes. OPNsense resolves the name and lets the traffic out to the internet.
  2. Layers 1 and 6: the client asks Unbound for reddit.com. Unbound finds a DNS override or a DNSBL match and answers 0.0.0.0.
  3. Layer 2: the client already knows a Reddit IP and connects to it. The firewall alias matches the IP and drops the packet.
  4. Layer 3: the client asks an external DNS server such as 8.8.8.8 on port 53. The firewall drops the query.
  5. Layer 4: the client tries DNS-over-TLS (DoT) on port 853. The firewall drops it.
  6. Layer 5: the client tries DNS-over-HTTPS (DoH) on port 443 to a known provider. The firewall drops it too.
6 / 6

Layers 1–3: Blocking — what to block and how

Layer What it does When it acts
Layer 1: DNS Override Unbound returns 0.0.0.0 for blocked domains When the name is resolved, before any connection
Layer 2: Firewall Alias Blocks packets to specific IPs (resolved from domain aliases) After the name is resolved, when the client connects
Layer 3: Block External DNS Drops all port 53 traffic to anything except OPNsense When a client tries to bypass Unbound

Layers 4–5: Anti-Bypass — close the ways around

Layer What it does When it acts
Layer 4: Block DoT Drops all port 853 traffic (DNS-over-TLS) When a client tries encrypted DNS bypass
Layer 5: Block DoH Drops port 443 to known DoH providers (Cloudflare, Google) When a client tries DNS-over-HTTPS bypass

Layer 6: Automation — block many domains with feeds (lists that other people keep up to date)

Layer What it does When it acts
Layer 6: DNSBL Auto-blocks thousands of domains from maintained feeds (ads, malware, gambling) When the name is resolved, same as Layer 1

Normal DNS flow (through OPNsense):

  1. Client asks Unbound to resolve reddit.com
  2. Layer 1 + 6 check: Unbound looks for a DNS override or a DNSBL match. If it finds one, it returns 0.0.0.0 → blocked
    • If no match, Unbound resolves the name as usual and returns the real IP → client connects → packet passes through the firewall
  3. Layer 2 check: Firewall compares the destination IP with the blocked aliases:
    • If match → drops packet
    • If no match → traffic reaches the internet

Bypass attempts:

  1. Client sends a DNS query directly to 8.8.8.8 on port 53
  2. The packet must pass through OPNsense’s firewall to leave the LAN
  3. Layer 3 check: Firewall sees port 53 to a destination that is not 10.10.1.3 → drops packet
  4. Layer 4 check: Client tries DoT on port 853 → blocked
  5. Layer 5 check: Client tries DoH on port 443 to known providers → blocked

The six layers complete each other. Use them together. This is called defense in depth: when one layer misses something, another layer catches it.


1. Block a Specific Website#

This section sets up Layer 1 (DNS Override) and Layer 2 (Firewall Alias) from the diagram above. The two layers support each other. The DNS override blocks by name. The firewall alias blocks by IP.

Layer 2 — Block via Firewall Alias#

Step 1 — Create the Alias#

An alias is a named list of hosts or IPs that a firewall rule can use. Go to Firewall → Aliases → click +:

Field Value
Name BLOCK_REDDIT
Type Host(s)
Description Block Reddit on LAN

In the Content field, add all domains the site needs. The main domain alone is not enough for sites that load their files from a CDN (content delivery network):

text
reddit.com
www.reddit.com
old.reddit.com
redd.it
redditstatic.com
redditmedia.com

Click Save → Apply changes.

OPNsense Firewall → Aliases list showing BLOCK_REDDIT entry with all reddit domains

Step 2 — Create the Block Rule#

Go to Firewall → Rules → LAN → click +:

Set Action to Block, Interface to LAN, Direction to in, TCP/IP Version to IPv4+IPv6, and Protocol to any.

For Source, select LAN net. For Destination, select the BLOCK_REDDIT alias.

Description: Block Reddit from LAN

Move this rule above your default allow rules. OPNsense reads rules from top to bottom and stops at the first match.

Click Save → Apply changes.

Firewall → Rules → LAN showing BLOCK_REDDIT rule at the top of the list with Action: Block

Layer 1 — Block via DNS Override#

This stops the domain when the name is resolved, before the client tries to connect. Use it together with Layer 2 for a deeper block.

  • Go to Services → Unbound DNS → Overrides → Host Overrides → click +:
  • Set Host to *, Domain to reddit.com, Type to A, and IP to 0.0.0.0
  • Description: DNS block — reddit.com
  • Repeat for each domain in the alias. Click Apply once at the end

When a client resolves any of these domains, Unbound returns 0.0.0.0. The browser gets an address that leads nowhere, before it even tries to connect.

Verify that it works:

bash
nslookup reddit.com
Server:		10.10.1.3
Address:	10.10.1.3#53

Name:	reddit.com
Address: 0.0.0.0

Unbound on OPNsense answered with 0.0.0.0. The block works. Now test what happens when a client bypasses OPNsense:

bash
nslookup reddit.com 1.1.1.1
Server:		1.1.1.1
Address:	1.1.1.1#53

Non-authoritative answer:
Name:	reddit.com
Address: 151.101.1.140
Name:	reddit.com
Address: 151.101.65.140
Name:	reddit.com
Address: 151.101.193.140
Name:	reddit.com
Address: 151.101.129.140

The real IP comes back. Any client with a DNS server set by hand (1.1.1.1, 8.8.8.8) fully avoids Unbound and your LAN block rules.

The next section fixes this. It forces all DNS queries through OPNsense, no matter what the client has configured.


2. Block External DNS (Layer 3)#

This sets up Layer 3 (Block External DNS) from the layer diagram. DNS blocking only works if clients use OPNsense as their resolver. Any client with 8.8.8.8 or 1.1.1.1 set by hand fully bypasses your Unbound rules. The fix is a firewall rule. It drops all port 53 traffic that leaves your LAN for any place except OPNsense.

  • Go to Firewall → Rules → LAN → click +
  • Set Action to Block, Interface to LAN, Direction to in, TCP/IP Version to IPv4+IPv6, and Protocol to TCP/UDP
  • For Source, select LAN net
  • Destination — you want to block DNS to every place except OPNsense itself. Check Invert (the ! checkbox next to Destination). Then set Destination to Single host or Network → 10.10.1.3. With the invert, the rule reads: block port 53 to anything that is not 10.10.1.3
  • For Destination port, set From and To to 53
  • Description: Block external DNS
  • Click Save → Apply changes

OPNsense Firewall Rules LAN blocks external DNS

Verify the Intercept Works#

bash
# Query an external DNS server — should now time out
nslookup reddit.com 8.8.8.8
bash
nslookup reddit.com 8.8.8.8
;; connection timed out; no servers could be reached

nslookup reddit.com 1.1.1.1
;; connection timed out; no servers could be reached

External DNS is now blocked. Queries to OPNsense still resolve, and they return 0.0.0.0 for blocked domains:

bash
nslookup reddit.com
Server:		10.10.1.3
Address:	10.10.1.3#53

Name:	reddit.com
Address: 0.0.0.0

OPNsense replied with 0.0.0.0. The DNS block works, and a client cannot bypass it by pointing at an external resolver.

Now that everything is set up, let’s remove the rule that blocks reddit.com 🙂

3. Block DoT — Layer 4 (Port 853)#

DNS-over-TLS (DoT) is another way for clients to bypass your local DNS. It encrypts DNS queries and sends them over port 853, so Unbound cannot see them. Block it at the firewall.

  • Go to Firewall → Rules → LAN → click + (add to top)
  • Set Action to Block, Interface to LAN, Direction to in, TCP/IP Version to IPv4+IPv6, and Protocol to TCP
  • For Source, select LAN net. For Destination, select any
  • For Destination port, set From and To to 853
  • Description: Block DNS over TLS (DoT)
  • Click Save → Apply changes
  • Verify DoT is blocked:
bash
openssl s_client -connect 1.1.1.1:853

Expected output (the connection times out):

text
error:8000003C:system library:BIO_connect:Operation timed out

Check that normal DNS still works:

bash
nslookup google.com
Server:		10.10.1.3
Address:	10.10.1.3#53

Non-authoritative answer:
Name:	google.com
Address: 142.250.198.206

Your device uses OPNsense (10.10.1.3) as DNS. DNS enforcement works: port 53 is locked down, and DoT is blocked.

4. Block DoH — Layer 5 (Known Providers)#

DNS-over-HTTPS (DoH) sends DNS queries over port 443, the same port as normal HTTPS traffic. So you cannot block all DoH with one port rule. But if you block the IPs of known DoH providers, you still stop the most common bypass attempts.

Create the Alias#

  • Go to Firewall → Aliases → click +:
  • Set Name to DOH_SERVERS
  • Set Type to Host
  • Set Description to Known DoH providers
  • In the Content field, set it as text and paste:
text
1.1.1.1
1.0.0.1
8.8.8.8
8.8.4.4
  • Click Save

Create the Block Rule#

  • Go to Firewall → Rules → LAN → click + (add to top)
  • Set Action to Block and Protocol to TCP
  • For Source, select LAN net. For Destination, select the DOH_SERVERS alias
  • For Destination port, set From and To to 443
  • Description: Block DoH (known providers)
  • Click Save → Apply changes

OPNsense Firewall Rules LAN all blocking rules: external DNS, DoT, DoH servers


5. Category-Based DNS Blocking with DNSBL — Layer 6#

Blocking single sites by hand does not work when there are thousands of them. DNSBL (DNS Block List) solves this. You do not add one domain at a time. Unbound downloads large blocklists that a community keeps up to date. It then blocks ads, trackers, malware, and any custom domains you add. OPNsense has DNSBL built in, so you need no extra packages.

Step 1 — Enable DNSBL#

  • Go to Services → Unbound DNS → Blocklist and check Enable
  • Choose your blocklist sources. OPNsense comes with a large set of maintained lists. Pick the ones that match what you want to block:
List Purpose
[hagezi] Multi LIGHT Basic protection — good starting point
[hagezi] Multi NORMAL All-round protection, recommended for most setups
[hagezi] Multi PRO Extended protection, more aggressive
[hagezi] Multi ULTIMATE Maximum blocking — may break some sites
Steven Black List Ads, trackers, and malware combined
[hagezi] Threat Intelligence Feeds Known malware and phishing domains
[hagezi] Gambling Gambling sites
[hagezi] DoH/VPN/TOR/Proxy Bypass Blocks known bypass infrastructure
[hagezi] Anti Piracy Torrent and piracy sites
EasyList / EasyPrivacy Ad and tracker lists (the same kind that browser ad blockers use)

Set the other fields:

  • Allowlist Domains — domains that should never be blocked, even if they appear in a feed (supports regex)
  • Blocklist Domains — exact domain names to always block (the ones you add by hand)
  • Wildcard Domains — blocks a domain and all its subdomains (e.g. reddit.com blocks old.reddit.com, www.reddit.com, etc.)
  • Source Net(s) — apply the policy only to specific subnets, or leave empty to apply to all LAN traffic

Click Save → Apply.

Step 2 — Add Custom Block Domains#

Use Wildcard Domains to block a site and all its subdomains at once:

text
reddit.com
redd.it
redditstatic.com
redditmedia.com

Click Save → Apply.

Step 3 — Restart Unbound#

Go to Services → Unbound DNS → General and click Restart. This makes Unbound load the updated blocklists.

Verify Blocking Works#

bash
nslookup doubleclick.net
Server:		10.10.1.3
Address:	10.10.1.3#53

Name:	doubleclick.net
Address: 0.0.0.0

nslookup ads.google.com
Server:		10.10.1.3
Address:	10.10.1.3#53

Name:	ads.google.com
Address: 0.0.0.0

nslookup tracking.facebook.com
Server:		10.10.1.3
Address:	10.10.1.3#53

** server can't find tracking.facebook.com: NXDOMAIN

nslookup ad.doubleclick.net
Server:		10.10.1.3
Address:	10.10.1.3#53

Name:	ad.doubleclick.net
Address: 0.0.0.0

Ad and tracking domains resolve to 0.0.0.0 or return NXDOMAIN (no such domain). Both mean the connection is dropped before it reaches the internet.

Allowlist Exceptions#

If a blocklist catches a domain that is fine, go to Services → Unbound DNS → Blocklist → Allowlist → click +:

Field Value
Domain legitimate-site.com
Type Domain

What You Now Have#

Your OPNsense now enforces DNS at six layers. There is no single way around them:

  1. A site that is not on any list passes. OPNsense resolves the name and lets the traffic out to the internet.
  2. Layers 1 and 6: the client asks Unbound for reddit.com. Unbound finds a DNS override or a DNSBL match and answers 0.0.0.0.
  3. Layer 2: the client already knows a Reddit IP and connects to it. The firewall alias matches the IP and drops the packet.
  4. Layer 3: the client asks an external DNS server such as 8.8.8.8 on port 53. The firewall drops the query.
  5. Layer 4: the client tries DNS-over-TLS (DoT) on port 853. The firewall drops it.
  6. Layer 5: the client tries DNS-over-HTTPS (DoH) on port 443 to a known provider. The firewall drops it too.
6 / 6

Summary of what’s in place:

Blocking (Layers 1–3):

  • Layer 1: DNS Override — Unbound returns 0.0.0.0 for domains you blocked by hand
  • Layer 2: Firewall Alias — blocks by IP, catches traffic that bypasses DNS
  • Layer 3: Block External DNS — port 53 to any destination other than OPNsense is dropped

Anti-Bypass (Layers 4–5):

  • Layer 4: Block DoT — port 853 blocked, no DNS-over-TLS bypass
  • Layer 5: Block DoH — port 443 to known providers blocked

Automation (Layer 6):

  • Layer 6: DNSBL — category blocklists for ads, malware, gambling, and custom domains

Is this enterprise-grade?

The layers copy what enterprise networks do: defense in depth, DNS enforcement, and several bypass paths closed. For a homelab or small office, it is at production level.

What enterprise setups add on top:

Gap Enterprise solution
DoH on unknown servers (port 443) SSL inspection / TLS MITM proxy
Per-user policies LDAP / Active Directory integration
Centralized audit logs SIEM (Elasticsearch, Splunk)
Threat response IDS/IPS with Suricata (next post)

You have the foundation. The next layer is intrusion detection.


To lock down OPNsense itself, continue to OPNsense Hardening: Lock Down Your Firewall After Install. It covers limits on the admin interface, SSH hardening, automatic updates, and Suricata.