Image

Image

Search This Blog

Monday, May 04, 2015

dns_adblock

#!/bin/bash 
#dns_adblock.sh - poor man's adblock 
# (c)2015 sorin@toXX.guru                                                                                       
#replace add servers with a local blank page 
#this script works faster/better/cleaner if you have apache httpd configured to serve a null page in case of page not found 
#just create an empty file 'touch /var/www/html/null.html', then in /etc/httpd/httpd.conf add/modify the line 'ErrorDocument 404 /null.html'  


#define local variables 
target=/var/named/data/blacklist

nullzone=/var/named/null.zone.file 
namedconf=/etc/named.conf  
whitelisted=/var/named/data/whitelisted.personal 
blacklisted=/var/named/data/blacklisted.personal 
namesrv=192.168.18.100                                                                           
# if it's the first time we run this, add the right info into bind's configuration   
if [ ! -f $nullzone ]; then  
echo "\$TTL    86400   ; one day  
@       IN      SOA     ads.nodomain. root. (  
             2015021505 ; serial              
                  28800 ; refresh                  
                   7200 ; retry                        
                 864000 ; expiry                        
                86400 ) ; minimum                          
            NS      nameserver     
               A       $namesrv              
@       IN      A       $namesrv                    
*       IN      A       $namesrv" > $nullzone    
fi                                                                                
if ! grep -q $target $namedconf ; then                                             
echo "conf not found"  

echo " include \"$target\";" >> $namedconf 
fi           
                                               

# get the list of known adservers from yoyo.org, sanitize it and make it compatible with bind9   
wget -q -O - 'http://pgl.yoyo.org/as/serverlist.php?hostformat=bindconfig&showintro=0' | tail -n+29 | head -n -6 | sed -e 's/{/IN {/g' -e '/_/d' > $target-new 

                                  
# get a second list from mvps.org, sanitize it and add only the hostnames that were not given by yoyo.org 
while read adhost; do                                                              
if ! grep -q $adhost $target-new ; then  
echo "zone \"$adhost\" IN { type master; notify no; file \"null.zone.file\"; };" >> $target-new   
fi          
done < <(wget -q -O - http://www.mvps.org/winhelp2002/hosts.txt | sed -e '/^ *#/d;s/#.*//' -e '/^[[:space:]]*$/d' -e '/localhost/d' -e '/_/d' -e '/[\r\n]/d' | cut -d" " -f2 )          
             
# add personal blacklist    
blhost="nothing"  
sed '/^ *#/d;s/#.*//' $blacklisted | while read blhost; do  
if ! grep -q $blhost $target-new ; then                    
echo "zone \"$blhost\" IN { type master; notify no; file \"null.zone.file\"; };" >> $target-new  
fi        
done   

       
# remove whitelisted domains. ($wlisted variable have to be defined, otherwise sed might remove everything) 
wlisted="nothingatall"               
sed '/^ *#/d;s/#.*//' $whitelisted | while read wlisted; do 
sed -i /"$wlisted"'/d' $target-new                                                  done  

       
#remove duplicates, named is intolerant to multiple definitions for the same host  
cat  $target-new | sort -u > $target 


#cleanup line containing "empty" domain                                            
sed -i '/\"\"/d' $target 


# cleanup the last remanents of bad lines reported by named-checkconf  
if [ ! $(/usr/sbin/named-checkconf -t /var/lib/named -z /etc/named.conf >/dev/null 2>&1; echo $?) = 0 ]; then 
badlines=$(/usr/sbin/named-checkconf -t /var/lib/named -z /etc/named.conf | cut -d: -f2 | sed -e 's/$/d;/' | tr -d '\n')  
sed -i "$badlines" $target  
fi                                                                                    
rm -f $target-new                                                                                     
# reload bind  
/bin/systemctl reload named.service 




Blog Archive