Blocking Facebook Web Trackers At The Firewall For Extra Privacy

If you've spent any time examining the network traffic to and from your web browser, you will notice the prolific number of trackers embedded in the pages. These "call home", tracking your movements across unrelated web pages. Big money is being made by turning you and your web activities in to commodities all while slowing down your net experience. This information is sold onwards, even to Redmond. How do you keep that information to yourself? After all, you're not the customer, you are the product being sold. You lose your privacy and gain nothing in return.

There are some add-ons to the web browsers that aid in privacy, but it is simple enough to block the rogue sites at the firewall. In this case we block Facebook, which seems to have tracking everywhere, but the same methods can apply to any trackers. First thing, we use whois to use the ip addresses of the offending sites to look up the network ranges in use by Facebook, which will be our example. The result turns out to be four subnets:

Edit 2012-11-15: As JR below points out, there is an easy way to get all the subnets:

/usr/bin/whois -h '!gAS32934' | tr ' ' '\n'

Next we add these addresses to the firewall. The method varies depending on the type of packet filter your system uses. For all of these, we REJECT the packet rather than DROP it. There are, of course, plenty of reasons to favor REJECT over DROP. In this case, the most important one is that we want to get immediate feedback that the site is blocked rather than waste time waiting for a timeout.

Below are three examples for the major types of filter. All of these examples will require root access, either via sudo or directly.


Linux with UFW

Some distros come with UFW or Uncomplicated Firewall. UFW is a text-based interface to iptables, consisting of a few simple commands and is designed to be easy to use. Several distros have it available. Fortunately, UFW can accept a network range as input.

ufw reject out to 
ufw reject out to
ufw reject out to
ufw reject out to

The way to restore the factory defaults for UFW is to reset:

ufw reset


Plain Old Iptables

If your distro does not come with UFW or you are not accustomed to using it, you can work directly with iptables. Again, iptables can take a network range as input, like UFW does.

iptables -A OUTPUT -d -j REJECT 
iptables -A OUTPUT -d -j REJECT
iptables -A OUTPUT -d -j REJECT
iptables -A OUTPUT -d -j REJECT

Clearing iptables takes three steps:

iptables -Z; iptables -F; iptables -X

More about iptables can be found at Oskar Andreasson's Iptables-tutorial.


BSD, including OS X

PF works for OS X, FreeBSD, NetBSD, DragonflyBSD and OpenBSD. Recent versions of OS X have started using OpenBSD's Packet Filter (PF), OS X being based on BSD. The regular BSDs all use this filter since way back.

Tables are the easy way to hold the list of networks to be blocked. The following two lines get added to the configuration file pf.conf in their appropriate places.

table <trackers> persist {,,, } 
block quick to <trackers>

Next, load the ruleset into PF.

pfctl -f pf.conf

Be sure to keep a backup copy of your filter rules. More about PF can be found in Peter Hansteen's Firewalling with PF or his book, The Book of PF, 2nd edition.



Blocking at the firewall will stop every form of tracking from the sites blocked. In some cases it will even speed up loading and rendering, as fewer calls to remote servers are made. These tracking requests can consume bandwidth. If you are on a slow connection or one with bandwidth caps, that is also something to think about. Facebook was used as the example in the above material, but the same method can be applied to protect from other trackers.

Share this page:

12 Comment(s)