Saturday, February 10, 2007

Ad Filtering Using "Hosts" File

To block the Banner Ads and to stop malicious softwares from contacting Ad servers, I use windows hosts file [ %WINDIR%\system32\drivers\etc\hosts ]. Yes, it is hosts and not host.

A hosts file contains the mappings of IP addresses to host names. When you type a URL in the browser address bar... say deverastic.blogspot.com, windows [ even Unix/Linux ] checks if this URL is available in the hosts file. If NOT found, it will contact the DNS sever.

That means, if windows finds the URL (or hostname) mapped to an IP in the hosts file... it will NOT contact the DNS.

Lets do a small experiment.

[ Ping deverastic.blogspot.com ]
1. Start -> Run -> cmd
2. Ping www.deverastic.blogspot.com

Reply from 72.14.219.191: bytes=32 time=239ms TTL=245
Reply from 72.14.219.191: bytes=32 time=238ms TTL=245
Reply from 72.14.219.191: bytes=32 time=242ms TTL=245

[ Now I map the name www.deverastic.blogspot.com with my localhost ip 127.0.0.1 ]

1. Open the hosts file [ c:\windows\system32\drivers\etc\hosts]
2. You will find a default entry in hosts file :

127.0.0.1 localhost

lets add a new line

127.0.0.1 www.deverastic.blogspot.com


3. Save the file
4. Start -> Run -> cmd

Ping www.deverastic.blogspot.com


Reply from 127.0.0.1: bytes=32 time<1ms ttl="128">
Reply from 127.0.0.1: bytes=32 time<1ms ttl="128">
Reply from 127.0.0.1: bytes=32 time<1ms ttl="128">

Lesson- If you have the names of the Ad Servers, you can map it to the localhost IP and block the request from going outside your machine. Mvps.org has collected a list of hundreds of Ad servers and created a hosts file. [ download ]

A DEVERASTIC Hack !!

URLs :
Hosts_file (wikipedia)
mvps.org