We can check your plugins and stuff

Saturday, February 12, 2011

Quick and easy way to block ad's and spam in Fedora Linux

How many time we all wished that the web site or web page you where visiting would load up faster. Now a days no matter what site you visit blogs, news, forums, portals or generic web sites. They all are littered with all sorts of java scripts,flash animations, java applets, active-x controls and cross feeds via dynamic html/scripting to one or more advertising servers. If you have ever noticed the bottom of the web browser while opening any standard site such as Facebook, Orkut, BBC news etc, you will see the web browser trying to connect to many different sites and many many different sub domains.

Most of big sites pull content from many different sites and sub domains. At times the main site that you are trying to reach would not even load completely. All because of a slow ad server or sub domain from where it is pulling up the info or advertisement. To get out of this rut we have to refresh the page many times in hopes of the entire content being loaded. There are a few ways to get around this, today we will look at one such very easy way. This method has the added advantage that it blocks out known bad/evil domains that server Malware, Spyware and Spam. This also provides an extra layer of defense against all those hackers and script kiddies so eager to steal your data, cookies, web logins and your peace of mind in general.

Under most *nix like system the web address is first check against the HOST file. If a match is not found a DNS lookup is done for the IP address as the web browsers works on IP internally over HTTP/FTP(s). What we will do is use the host file to block unwanted domains and IP addresses.... Aha you say old trick what's so great about it.. well for starters we will use some bash script magic to automate this for us.  The list(s) comes from very well know sources like WOT/mvps.... so lets get to it. You will need the following tools installed (these are pre installed in Fedora) wget unzip dos2unix grep. If for some reason your system does not have any of these do a quick 'yum install' to install the missing tool. Copy and paste the code below and save as hostup.sh

****************
!/bin/bash
# uphosts - Hosts File Updater
# README:
#original script found at http://guide.debianizzati.org/index.php/UpHosts
#Bad hosts are blocked putting them in the hosts file as 0.0.0.0
# To add other sources script must be manually modified
# Permanent entries must be added to the original file
# THIS SCRIPT HAS NO WARRANTY !
# Thanks to:
# http://ubuntuedintorni.wordpress.com/2009/06/29/di-script-dns-e-file-host/
# http://hostsfile.mine.nu/downloads/updatehosts.sh.txt
# 20101216 Paolo
# has been modified to work under Fedora/RHE /CentOS
#-----------------------------------------------------------------------

HOSTSPATH="/tmp/hosts-`date +%s`"               # Temp directory
HOSTSFILE="/etc/hosts"                          # Hosts file
ORIGFILE="$HOSTSFILE.original"                  # Backup file

CONFDIR="$(dirname $(readlink -f $0))"  # Parent directory of the script
BLACKLIST="$CONFDIR/uphosts-blacklist"  # Local Blacklist
WHITELIST="$CONFDIR/uphosts-whitelist"  # Whitelist

PROXYUSER="" #PROXYUSER="--proxy-user=user.name"
PROXYPASS="" #PROXYPASS="--proxy-password='password"

DAYS="2" # Update frequency
#-----------------------------------------------------------------------
# Checks for root privileges
if [ "$(whoami)" != 'root' ] ; then
        echo "You need to be root to execute uphosts. Exiting!"
        exit 1
fi

# Checks required packages
ABORT=0
builtin type -P wget     &>/dev/null || { echo -n "wget is missing."; ABORT=1; }
builtin type -P unzip    &>/dev/null || { echo -n "unzip is missing."; ABORT=1; }
builtin type -P dos2unix  &>/dev/null || { echo -n "dos2unix is missing."; ABORT=1; }
builtin type -P grep     &>/dev/null || { echo -n "grep is missing."; ABORT=1; }

if [ $ABORT != 0 ] ; then
        echo " Exiting!"
        exit 2
fi

# Limits updates if uphosts is run often (i.e. at every if-up)
# If there is no original hosts file this is the first run on a fresh system, and update runs anyway
if [ -f "$ORIGFILE" ] && [ `find $HOSTSFILE -mtime -$DAYS` ] ; then
        echo "$HOSTSFILE is less than $DAYS days old. Exiting!"
        exit 3
fi
************
This script need Root to execute. Make the script executable by doing a #chmod +x ./hostup.sh
now run the script #./hostup.sh [You should see something like this]
Retrieving hphosts from http://support.it-mate.co.uk/downloads ... OK
Retrieving hphosts-partial from http://www.hosts-file.net ... OK
Retrieving mvps from http://www.mvps.org/winhelp2002 ... OK
Merging lists ... OK
Writing hosts file /etc/hosts ... OK
Update process complete - 134929 hosts blocked!

That's it and we are done. Enjoy a safer and fasted web experience.

P.S - In a perfect world I should have unlimited bandwidth and money ;) .... But alas though this method is great and saves a lot of pain and manual effort. It sometimes does block some sites and forums from loading properly.. if that is the case just open the hosts file in vi
#vi /etc/hosts and add a "#" to comment the site to unblock it. Do not forget to save the hosts file. Refresh the page in your browser.

No comments:

Post a Comment