Advertisement
Guest User

Modded Tablspn Script

a guest
Aug 31st, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.98 KB | None | 0 0
  1. #!/bin/bash
  2. ## Adblocker.sh - Based on Adblocker.sh by Todd Stein ([email protected]), Saturday, October 25, 2014
  3. ##                Modifiyed by Mathew Evans ([email protected]), Monday, August 31, 2015
  4. ##                for use with Bind9 on *Nix setup
  5. ##
  6. #  
  7. #  Requirements:
  8. #  *nix & Bind9
  9. #
  10. #  Add to named.conf.local:
  11. #  include "/etc/bind/blacklist.zones";
  12. #
  13. #
  14. #  ToDo:
  15. #  - Cronjob support.
  16.  
  17.  
  18.  
  19. readBlkList()
  20. {
  21.         while read p; do
  22.                 set -- "$p"
  23.                 IFS=" "; declare -a Array=($*)
  24.                 hostname=`echo "${Array[1]}" | sed 's/\\r//g'`
  25.                 appendBlackList $hostname
  26.         done </tmp/adblocker_hostlist
  27. }
  28.  
  29. appendBlackList()
  30. {
  31.         if [ $(hostExists $1) -le 0 ]; then
  32. #               echo ">>New blocked host added: $1";
  33.                 echo "zone \"$1\" {type master; file \"/etc/bind/db.blocked\";};" >> /etc/bind/blacklist.zones
  34.         fi
  35. }
  36.  
  37. hostExists()
  38. {
  39.         res=$(find /etc/bind -type f -name "blacklist.zones" | xargs egrep -i '('$1')');
  40.         if [ ${#res} -ge 1 ]; then echo 1  # Found
  41.         else echo 0                        # Not Found
  42.         fi
  43. }
  44.  
  45. BuildBlockList()
  46. {
  47. HOST_LISTS="
  48.        http://adaway.org/hosts.txt
  49.        http://www.malwaredomainlist.com/hostslist/hosts.txt
  50.        http://www.mvps.org/winhelp2002/hosts.txt
  51.        http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate%5Bday%5D=&startdate%5Bmonth%5D=&star
  52. "
  53. BLOCKLIST=/tmp/adblocker_hostlist
  54. BLACKLIST=/etc/adblocker_blacklist
  55. WHITELIST=/etc/adblocker_whitelist
  56.  
  57. # await internet connectivity before proceeding (in case rc.local executes this script before connectivity is achieved)
  58. until ping -c1 -w3 google.com || ping -c1 -w3 yahoo.com; do
  59.         sleep 5
  60. done &>/dev/null
  61.  
  62. # initialize block list
  63. >"$BLOCKLIST"
  64.  
  65. # grab blacklisted domains if any have been specified
  66. [ -s "$BLACKLIST" ] && awk '/^[^#]/ { print "0.0.0.0",$1 }' "$BLACKLIST" >>"$BLOCKLIST"
  67.  
  68. # grab host lists from the internet
  69. IP_REGEX='([0-9]{1,3}\.){3}[0-9]{1,3}'
  70. wget -qO- $HOST_LISTS | sed -rn "s/^$IP_REGEX\W/0.0.0.0 /p" | awk '{ print $1,$2 }' | sort -uk2 >>"$BLOCKLIST"
  71.  
  72. if [ -s "$WHITELIST" ]; then
  73.         white_listed_regex=`echo \`grep -o '^[^#]\+' "$WHITELIST"\` | tr ' ' '|'`
  74.         sed -ri "/$white_listed_regex/d" "$BLOCKLIST"
  75. fi
  76. }
  77.  
  78. AddToCron()
  79. {
  80. echo "todo: CronJobs support";
  81. #SCRIPT_NAME="/etc/bind/adblocker.sh";
  82. #DELAY='$(head /dev/urandom | wc -c | /usr/bin/awk "{ print \$0 % 30 }")m'
  83. #grep -Fq "$SCRIPT_NAME" /etc/crontabs/root 2>/dev/null || cat >>/etc/crontabs/root <<-:EOF:
  84.         # Download updated ad and malware server lists every Tuesday between 3:00 and 3:30 AM
  85. #       0 3 * * 2 /bin/sleep $DELAY && $SCRIPT_NAME
  86. #:EOF:
  87. }
  88.  
  89.  
  90. ## make sure added to cronjobs to autorun
  91. AddToCron
  92.  
  93. ## Download updated blocklist
  94. BuildBlockList
  95.  
  96. ## Read downloaded blocklist and add new to bind list
  97. readBlkList
  98.  
  99. ##Retart Bind to update to new blocked list
  100. service bind9 restart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement