Pages

Showing posts with label wireless router. Show all posts
Showing posts with label wireless router. Show all posts

Sunday, April 19, 2015

Full Home Ad-Block

One of the first things I do on installing a new browser, is to install the Ad block extension, for a cleaner, stress free browsing experience. It is also the primary reason I prefer Firefox on my mobile devices (Chrome still does not support extensions on mobile). One of the most popular and most efficient extension in uBlock. More than ads, they can also be configured to block other web annoyances like social buttons (which have been proven to be a significant drain on your bandwidth, and slow down your browser), and sites that are known to inject malware. Multiple lists are being maintained for known ad/malware servers and they can be chosen in the add-on setup screen.

But if you want to block these annoyances on all devices in your home (unsupported browsers, Apple devices etc), it is now possible to have this functionality in the home gateway (this is usually the wireless router, connected to your DSL/Cable modem) itself, with a little bit of hacking. This has unintended (but welcome) consequence of blocking interstitial ads in games and YouTube videos too! For this we will need a hacker-friendly routers than can run open-source firmwares like tomato, openWRT, or DD-WRT. Even though the steps needed to accomplish this might be different among these distributions, the basic mechanism is the same - the gateway advertises itself as the DNS server to all the clients in the home network, and for every DNS query made by the clients, it matches them against its block-list and returns an invalid reply if there is a match. So whenever a web page is loaded on the client, all requests to load ads, malware or social buttons simply fails.

The only downside to this approach is that blocking is universal among everyone in the network, and sometimes even legitimate sites get shot down due to false positives or over aggressive rules. We'll see couple of ways of bypassing the gateway blocking. The browser add-ons can be configured to be disabled on certain domains, or even just few select pages, but with gateway method, it is an all or nothing proposition.

Now let us see the steps to accomplish this in detail:

DNS Configuration


The first step in this process would be to enable DNS server in the gateway. On Tomato this is done by enabling internal server under DHCP/DNS, and on dd-wrt by enabling dnsmasq under 'Services' tab. You should also configure your clients to get their DNS config from the gateway (which is usually the default, unless otherwise changed) We'll see that this also helps in bypassing the gateway adblock if need be (like when some legitimate pages fails to load).

Block List


Multiple lists are being maintained for ad and malware servers. You can checkout some of them at AdAway site. You can choose one or multiples of them to your router, remove duplicates and convert it to a format dnsmasq understands (one host per line, with destination IP as itself). You should strike a balance because having too big a hosts file will make the router unstable, depending on how much memory it has. Then you have to edit the dnsmasq configuration file to pint to your blocklist. You might have to restart the service for new configuration to be read.
Since they are based on squashfs they do not have any persistent filesystem that spans reboots. So even though all of this can be on the telnet/ssh console of the routers, all of this is lost when gateway restarts. So it is a good idea to package the all the above steps in to a shell script, and type it out in the web admin page (Tomato: under admin->scripts->Wan up, DD-WRT: admin->commands). Anything saved in the web admin is saved into NVRAM of the gateway which is persistent.
Here is one such script. It is customized for dd-wrt but with little changes it will work fine in tomato too.
#!/bin/sh
logger WAN up script executing
logger Downloading http://www.mvps.org/winhelp2002/hosts.txt
wget -O - http://www.mvps.org/winhelp2002/hosts.txt |
 grep 0.0.0.0 | tr -s ' ' | tr ' ' '\t' | tr -d '\015' |
 sed 's/[[:space:]]*#.*$//g;' > /tmp/hosts
wget -O - http://www.malwaredomainlist.com/hostslist/hosts.txt
 | grep 127.0.0.1 | grep -v localhost | tr -s ' ' |
 tr ' ' '\t' | tr -d '\015' |
 sed 's/127.0.0.1/0.0.0.0/g; s/[[:space:]]*#.*$//g;' >> /tmp/hosts

if test -s /tmp/blocklist
then
        rm /tmp/blocklist
fi
uniq -u /tmp/hosts /tmp/blocklist
rm /tmp/hosts

logger Restarting dnsmasq
killall dnsmasq
grep addn-hosts /tmp/dnsmasq.conf ||
 echo "addn-hosts=/tmp/blocklist" >> /tmp/dnsmasq.conf
dnsmasq --conf-file=/tmp/dnsmasq.conf

DNS Target

You can spoof the target of the blocked hosts in multiple ways:
  • It can be the address of the gateway itself, in which case client tries to fetch the 'ad' from the gateway, and just times out. This is not favoured because it slows down the browser waiting for requests to timeout
  • It can be 0.0.0.0, in which case the borwser (at least Firefox) times out immediately
  • In both cases above, the block containing the 'ad' will have the standard error screen of the browser, making the page ugly. A better alternative would be to host a super simple webserver on the gateway, which responds with a single pixel image for any request it receives. PixelServ is just one such example.

Regular Updates

The blocklists are regularly updated to keep up with new ad and malware servers by the maintainers. So it is imperative to keep the gateway also updated with the latest blocklist. So you can make this download/convert script a regularly scheduled cron job. Both Tomato and DD-WRT have provisions for this in their web admin.

Gateway Bypassing

If for any reason you would like to avoid this gateway ad-blocking, you could do that in couple of ways:
  • Since this works because the gateways injects itself in the middle as the DNS server, the first method would be to manually reconfigure the client to bypass the gateway and direct all its DNS queries to the ISP or better yet, Google's servers.
  • Almost all routers have a button on the outside for WDS connections. Almost all third-party firmwares allow us to reconfigure this button to execute any command. We can make it to stop or start the dnsmasq service. This method affects all the hosts in the network, not just one client, as in the previous method.
With this basic understanding of the gateway level ad blocking, you can get into the specifics of configuring your flavor of firmware

Friday, January 4, 2008

Lights out :-(

How ever strong the signal was with the new WHR-G125, being placed in the corner office, it still would not reach bedrooms at the other end of the house. So plan was to augment it with another router acting as a repeater.
As luck would have it, one fine day as I plugged in my router in the wall socket after an upgrade, it just died. No lights, no signs of life. If there are issues with firmware, atleast the power LED lights up. So I suspect the power adapter got fried. Luckily I was able to dig out a universal adapter, set it to the power requirements of the Buffalo (3v) and fired it up. This time around power LED and the lights for all the ports come and stay on. Not a good sign either :-(
I am wondering if the universal adapter does not have enough juice to bring the router fully to life. Supplied adapter supplied 3.3v at 1.2A, while the uiversal one is set to 3v and is only rated at 500mA.
So think it is time for another router, and it is urgent, since we are fully cut-off from the Interwebs. This time around I am specifically looking for routers that are supported by one of popular alternative firmwares, Tomato, DD-WRT, or OpenWRT, almost in that order :-)

Friday, October 26, 2007

Alternative firmwares for WHR-G125

After encountering a few problems in configuration of the wireless routers, I was searching the Intenet for solutions. The Buffalo site itself was down due to some ongoing litigation. Scourging more, I found that DD-WRT has released an alternate firmware for it.
Installing it on the WHR-G125 was fairly easy. Although wireless routers come with a web gui, where one can upgrade firmware, this one rejected the alterative. Apparently Buffalo routers only accept signed updates released officially.
But all is not lost. A minor feature in this router came in handy. The bootloader (CFE) waits for incoming TFTP connections for about 5 seconds before loading the firmware from flash. So, I had to connect the router to my PC, give it static IP addresses, and push the DD-WRT firmware within that window. It took a couple of tries to get the timing right, but in the end it went through. Once downloaded, CFE overwrites the new firmware to the flash, and brings the router up wth the new image.
I think this will be a life-saver if ever we screw up with new images. If the router does not come up fully enough to respond to network connections ('bricked'), we can always force download a known-to-work image thorugh the bootloader. It should be a default. OpenWrt (another popular alternative firmware) sets boot_wait variable by default. Apparently they too are working on porting thier image to WHR-G125.
The advantages of alternative firmwares are enhanced signal strength, telnet access (they run linux underneath, afterall), lot more features, nice graphical bandwidth, and wireless site monitor), frequent updates with bug fixes, customizations (you can pick and choose what features you want and build your image, like in FreeWRT). DD-WRT even offers different pre-packaged images like standard, VPN, mini, micro (for routers with as little as 2MB flash). The feature I like the most in DD-WRT is the ability to have multiple WLANS. I can now give the password to a different wireless to my guest, so they can access the Intenet, but not my home PCs.
Update: Apparently Tomato has been ported to this router too. I downloaded and tried it out. Much simpler, slick UI, and I hear a better optimised code too.

Tuesday, October 2, 2007

Time for a new Router

Ok, only a geek will get excited about this, and working in a Networking company doesn't help much either.
Had to give away my trusty old D-Link DI-624 to my Dad (who is a proud owner of a laptop now, and cutting all cords). It has served its purpose well, albeit with some limitations: it used to crash/reboot every time I setup URL filtering and domain blocking. Guess its meager processor was just bogged down by the demands of deep packet inspection. So, it is time to retire it, and look for a new powerful, shiny wireless router.
Along came Buffalo WHR-G125 in a really small form factor. I have heard good things about Buffalo before, and this router fit all requirements of mine. Except for one little detail - It had a fixed antenna (I used to have an external antenna for the DLink, to extend its range upstairs). But my fears were unfounded - this little router exceeded my expectations with all excellent signal strength all over the house. So far so good. Will keep this space updated.