Do-It-Yourself Web Filter

Using a custom hosts file provided by a community and Mac OS X’s Apache web server, you can build your own free web filter. Remove undesirable content from your web surfing, such as tracking and advertisements.

A big red bar with a 404 is perhaps not the best thing to show as a replacement for and advertisement, but whilst testing, this clearly shows the filter working.

The hpHosts Community maintains a database of sites that fall foul of their standard. Various criteria, such as tracking a visitor, or providing advertisements, or serving malware, will have your site listed. Download their lists to add the offending FQDN’s to your /etc/hosts file.

First, I edited my /etc/hosts to add a marker to show where the banned list will be added.

sudo vi /etc/hosts

# Host Database
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
## localhost broadcasthost
::1 localhost
fe80::1%lo0 localhost

## begin block list

Save and quit.

Now, add the banned hosts list to the /etc/hosts file.

sudo sh -c "cat /users/johndoe/Desktop/bannedHosts.txt >> /etc/hosts"

If yo do this on your desktop, then you might notice the web slowing down, because pages that contain hosts that have been banned will try to contact your local machine in vain. Eventually the attempt will time-out, but you need to wait for it.

Alternatively, do this on your web proxy. The Apache server has mod_proxy enabled, and tries to serve all the pages from the local DocumentRoot. Naturally, it will fail to find the page on the banned site that was requested.

So set ErrorDocument 404 in your httpd.conf file, such as like this:

ErrorDocument 404 /404.html

Then create the 404.html file in your DocumentRoot directory with an empty page (I used the red “404” page). This ensures requests to banned sites are served from the localhost with an empty page.