Blocking Ads using unbound(8) on OpenBSD

       416 words, 2 minutes

The Internet is full of Ads and Trackers. Some of them are useful to monetize free content. Some are used in a non-ethical manner. Savvy users will configure Ad-Blocker on their Web browser. Others won’t. Most Appliance and IoT modules won’t allow third-party blocking addons.

Here’s how to add an extra layer of privacy using OpenBSD and its unbound(8) DNS resolver.

Pi-Hole is an OpenSource project that enables blocking Ads at the network-level. Meaning users don’t even have to care about it. What happens is that is serves as a DNS server for your LAN which simply won’t allow IP resolutions for well-known Ad services. Read the Pi-Hole documentation to learn more about it.

After reading the sources, I decided that the core feature could be implemented using OpenBSD’s stock unbound(8) and a home-made script.

The Ad blocklist

The public blocklists used by Pi-Hole are the following:

I wrote a script that will fetch the blocklists content, parse it and create a local zone file for unbound(8). That file will contain all the blocked domains and use the redirect answer to resolve those as invalid. The final unbound(8) zone file looks like this:

local-zone: "" redirect
local-data: " A"
local-zone: "" redirect
local-data: " A"

The script is regularly run from cron and the zone file is included on every unbound(8) start/reload/restart.

Configure unbound(8)

Now that the local zone file is filled with unwanted domains, using it with unbound(8) is as simple as adding the following line to unbound.conf:

# grep include /var/unbound/etc/unbound.conf
include: /var/unbound/etc/unbound-adhosts.conf

That’s all. When you look at the logs, you’ll now see things like: redirect A IN redirect A IN redirect A IN redirect A IN redirect A IN redirect A IN redirect A IN redirect A IN redirect A IN redirect A IN

The Ad-blocker dashboard

Using syslog-ng, I parse my unbound(8) logs and store some metrics in InfluxDB. This way, it is possible to render the Ad-blocker activity using Grafana.

This is what happened when browsing sites like CNN, YouTube, TheVerge, LeMonde, LeParisien or Economie.Gouv.Fr without an AdBlocker enabled in Firefox-ESR.

I’ve been using the unbound blocklist for a month now and didn’t notice any drawbacks. Nor did I get any complains from the daughter and her smartphone…