GuidesTech

AdBlocking with TomatoUSB Router – Ad free Internet for all your devices

Left: Before implementing AdBlock script. Right: After Adblock scripts on TomatoUSB router
Left: Before implementing AdBlock script. Right: After Adblock scripts on TomatoUSB router

The Internet is flooded populated with advertisements today. There are AdBlock plugins for browsers. But what about mobile devices like non-rooted or non-jailbroken Androids and iOS devices?

Recently, I chanced upon a Kickstarter project called AdTrap. It introduces a small zero configuration device that removes advertisements from your Internet connection before they reach any of your home devices. This device is connected between your modem and home router. Sounds cool? Yes if you are willing to fork out US$132 (inclusive of US$12 shipping if outside US / Canada).

 

Do I need the AdTrap project to achieve that? No!

So I gave it a thought – with dnsmasq on the TomatoUSB-enabled router (e.g. ASUS RT-N66U, Linksys E4200), I could probably achieve ad blocking with DNS Cache Poisoning on the TomatoUSB-enabled router. Too geek? In simpler terms, I can make the TomatoUSB-enabled router resolve known advertisement domain names / hostnames to invalid addresses like 0.0.0.0.

As a result, I will be able to filter advertisements from the Internet before they reach any of my devices at home.

I started to google around to see if anybody has used the DNS Cache Poisoning  technique (using dnsmasq) and found that somebody actually did the scripts (http://goo.gl/mhykQ)! Did a little modification to it and here it is.

 

Updates

29 Apr 2014

Added Pixelserv for ASUS RT-AC68U. Download Pixelserv (ARM) v43-2.

28 Apr 2014

Pixelserv does not work on ASUS RT-AC68U as it was originally compiled for Broadcom architecture. The ASUS RT-AC68U is based on ARM architecture. Instead of using Pixelserv, I decided to use NGINX web server to perform the role of pixelserv. Check out AdBlocking with NGINX: Serving 1 pixel GIF and 204 No Content.

7 Dec 2013

Updated  the script to use pixelserv V31. The changes are in the base64 encoded block. Previous version of pixelserv requires a parameter “-n br0” which is no longer required in V31. Updated the base64 block to reflect that.

 

Pre-requisites

  1. TomatoUSB-enabled router like ASUS RT-N66URT-N16, Linksys E4200ASUS RT-AC66U
  2. Flashed to TomatoUSB firmware (I am using TomatoUSB on my RT-N66U and RT-AC66U)
  3. Geeky mind to do some troubleshooting

 

Inserting the AdBlocking script

  1. Using a web browser, login to the TomatoUSB web administration page
  2. Navigate to Administration -> Scripts -> WAN Up tab
  3. Copy and paste the contents below. Then save and reboot the router

 

Verifying that the script is running

  1. After the TomatoUSB router has rebooted, ssh into the router
  2. Verify that script_wanup.sh is created by typing “ls /tmp” without the quotes
  3. Verify that the adblock script is running by typing “cat /var/log/messages | grep ADBLOCK” without the quotes
    It should show that it has N amount of entries for hosts and dnsmasq is running
  4.  Congratulations! Ad block is running!

 

How do I remove the script?

To remove the script. Simply delete the content of the WAN Up tab, save and reboot. The /tmp/script_wanup.sh should go away.

 

What is in the base64 text block

In TomatoUSB, all scripts are limited to 4096 characters hence some scripts are gziped and converted to base64. The contents of the base64 block is based on the following script. You can also verify by checking the contents of the file /tmp/script_wanup.sh. You do not have to copy and paste the below script anywhere. I am showing here for illustration purposes only. If you are geeky enough, you can also use the command

 

The AdBlock Sources

I have actually updated the AdBlock sources to one that is more suitable for me. The codes that are responsible for the AdBlock sources are:

You can choose which hosts files will be downloaded to block ads, update the variable GETS. I am downloading all of them.

A little explanation about TRIM_BEGIN=2:

S1 contains hostname only, it will be copied without formatting. While S2 to S6 are file formatted to replace hosts file, these files will be formatted to works with AdBlock. TRIM_BEGIN tells AdBlock which sources need formatting (from S2 to the end). Well usually you don’t need to touch this. Yes, you can add new blacklists to it.

If OPTIMISE=”Y”, AdBlock will remove duplicated entries.

 

Custom Whitelist and Blacklist

You can add inline whitelist and blacklist to the script by updating this chunk of the codes:

 

Auto Updating of AdBlock sources

By setting UPDATE=”Y”, AdBlock will update the AdBlock entries at 4am daily according to the time specified in the cron rule.

 

Getting Pixelserv to work

Pixelserv is a super minimal web server whose sole purpose is to serve a 1 x 1 pixel transparent gif file regardless of request. When USEPIXELSERV is enabled (set to “Y” instead of “N”). The ad blocked hostnames are resolved to PXL_IP (which actually points to the router itself). This result in your web browser requesting resource from Pixelserv.

To get Pixelserv to work, simply do the following:

  1. Download Pixelserv V31 or Pixelserv (ARM) V34-2 (for RT-AC68U).
    For non-ARM routers (e.g. RT-AC66U, RT-N66U) use the Pixelserv V31. For ARM-based routers (e.g. RT-AC68U, RT-N56U), use the Pixelserv (ARM) V34-2.
  2. Unpack the package and upload it to your webhost or the public folder of your Dropbox
  3. Update PXL_URL to point to the location of  pixelserv on your webhost or Dropbox without the https (e.g. http://dl.dropbox.com/u/pixelserv)
  4. Update PXL_IP to a unused IP address on your network (e.g. 192.168.1.10)
  5. Update USEPIXELSERV to “Y”
  6. Change TomatoUSB Web Admin page to run on port 8080 (TomatoUSB –> Administration –> Admin Access –> HTTP Port)
  7. Save the changes and restart the router

Do note that you will need to append :8080 to your router IP if you intend to access the Web Admin in the future.

 

Some useful hosts to whitelist

I will update the below list whenever I find some desktop / web applications breaks or do not work as they should. Do share your findings too.

 

Treat shadowandy!

If these step-by-step guides have been very helpful to you and saved you a lot of time, please consider treating shadowandy to a cup of Starbucks.  

This site contains links to Amazon. These links will take you to some of the products mentioned in today’s article. As an Amazon Associate, I earn from qualifying purchases.
85 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments