How to block ads with local DNS server

Before you proceed with this tutorial, there are few prerequisites you need to fulfil. First, you either local server or server on the Internet that you (can) use as your own DNS server, second, you need skills to install and configure Linux based DNS server – Bind, third, you need a second IP address if you already have web server running on your primary IP address. I will focus on Raspberry Pi running Ubuntu 16.04 LTS (Snappy Ubuntu Core).

Please note that I will NOT cover installation of Ubuntu OS and/or Bind9 server. I presume you have these up and running before proceeding.This is what you need:

  1. Raspbery Pi 2 Model B or Raspberry Pi 3 Model B
  2. Ubuntu 16.04 LTS
  3. Bind9 DNS server from Ubuntu repository
  4. Pixelserver
  5. Complete adserver list in Bind format

Once you have Bind9 installed and configured, create 2 files in Bind installation directory, /etc/bind/null-blacklist and null.zone.file

# vim /etc/bind/null-blacklist

Paste all lines from starting with zone, from the Complete adserver list. Note that you can add your own addresses using the same syntax. Be sure to add full path to null.zone.file in each line /etc/bind/null.zone.file as relative path will not work, so your syntax is as follows:

zone "24log.de" { type master; notify no; file "/etc/bind/null.zone.file"; };

instead of

zone "24log.de" { type master; notify no; file "null.zone.file"; };

Pixelserver is a minimal webserver. It solely purpose is serving 1×1 px transparent gif image. In our case, we will redirect all ad-server traffic to pixelserver listening address. Download single file pixelserver and make the script executable:

# wget http://proxytunnel.sourceforge.net/files/pixelserver.pl.txt
# mv pixelserver.pl.txt /usr/local/bin/pixelserver
# chmod +x pixelserver.pl

This step is necessary ONLY if you are running multiple IP addresses on your box: Make sure that the IP address where pixelserver will listen is NOT in use by any other server listening on port 80 and is reachable!

Edit pixelserver.pl and correct listening IP address:

# vim pixelserver.pl

and make sure to change LocalHost directive:

LocalHost => '0.0.0.0',

so it match your IP address dedicated to pixelserver.

Now we can create startup/shutdown script for your pixelserver:

# vim /etc/init.d/pixelserver

and paste the following lines:

#!/bin/sh
# /etc/init.d/pixelserver

# Starts pixelserver either during OS startup or when run manually
case "$1" in
      start )
         echo "Starting pixelserver"
         /usr/local/bin/pixelserver &
         ;;
      stop )
         echo "Stopping pixelserver"
         killall pixelserver
         ;;
       *)
        echo "Usage /etc/init.d/pixelserver {start|stop}"
        exit 1
        ;;
esac


exit 0

Make the startup/shutdown script executable:

# chmod 775 /etc/init.d/pixelserver

Now you can test if pixelserver startup/shutdown scripts works. Make sure to stop pixelserver. Now we will add pixelserver init script to startup/shutdown sequence:

# update-rc.d pixelserv defaults

Now you can start pixelserver using init script. If you are using adblock add-ons in your browser, now you can disable it and test your setup.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.