Blocking Facebook Web Trackers At The Firewall For Extra Privacy

Want to support HowtoForge? Become a subscriber!
 
Submitted by orange (Contact Author) (Forums) on Fri, 2012-11-09 17:26. :: Security

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:

31.13.64.0/18 
66.220.144.0/20
69.171.224.0/19
69.63.176.0/20

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

/usr/bin/whois -h whois.radb.net '!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 31.13.64.0/18
ufw reject out to 66.220.144.0/20
ufw reject out to 69.171.224.0/19
ufw reject out to 69.63.176.0/20

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 31.13.64.0/18 -j REJECT
iptables -A OUTPUT -d 66.220.144.0/20 -j REJECT
iptables -A OUTPUT -d 69.171.224.0/19 -j REJECT
iptables -A OUTPUT -d 69.63.176.0/20 -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 { 31.13.64.0/18, 66.220.144.0/20, 69.171.224.0/19, 69.63.176.0/20 } 
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.

 

Postscript

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.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Thu, 2014-01-16 06:28.
Thank you very much...It have very useful for block facebook in my school computer network
Submitted by XfceEvangelist (not registered) on Tue, 2013-05-21 13:35.

This is what exactly I was looking for.

A very big thank you from Italy.

Submitted by Scanman (not registered) on Thu, 2012-11-15 07:29.
The ghostery addon in firefox gives me insight in which url adress is actually a tracker. Then a whois reveals their IP. Thanks for this article, my iptables are up to date now.
Submitted by Anonymous (not registered) on Tue, 2012-11-13 20:01.

I guess that blocking the facebook trackers also blocks facebook?

Submitted by Anonymous (not registered) on Tue, 2012-11-13 02:52.

There is a typo in two places

66.220.144.0/2

should be 66.220.144.0/20

Two-bit netmask seems to be excessive :-)

Submitted by orange (registered user) on Thu, 2012-11-15 15:26.
Thanks. I've updated the table.
Submitted by jr (not registered) on Mon, 2012-11-12 18:48.
For Linux: to block all IP addresses currently owned by Facebook use:
/usr/bin/whois -h whois.radb.net '!gAS32934' | head -n -1 | tail -n -1 | /usr/bin/xargs --max-args=1 | /usr/bin/xargs -I {} --max-args=1 /sbin/iptables -t mangle -I POSTROUTING -d {} -j DROP
Submitted by daniel (not registered) on Sun, 2012-12-23 19:15.

Thank you for this great HowTo!

The above command leads to the following (additional) IP-Ranges:
31.13.24.0/21
31.13.64.0/18
66.220.144.0/20
69.171.224.0/19
69.63.176.0/20
74.119.76.0/22
103.4.96.0/22
173.252.64.0/18
204.15.20.0/22

Submitted by orange (registered user) on Thu, 2012-11-15 15:51.
It would be important to use REJECT instead of DROP so that you get a prompt response rather than a sluggish timeout.
Submitted by orange (registered user) on Thu, 2012-11-15 15:31.
Thanks, that's much more thorough.
Submitted by gbi (registered user) on Mon, 2012-11-12 17:28.
have immidiately integrated your rules into my wlan router's firewall rules