Skip to content

Instantly share code, notes, and snippets.

@jawi
Last active August 11, 2016 08:52
Show Gist options
  • Save jawi/a417562a90e03141f16bcaf10c0d85c9 to your computer and use it in GitHub Desktop.
Save jawi/a417562a90e03141f16bcaf10c0d85c9 to your computer and use it in GitHub Desktop.
Creates a black hole of hosts, based on the work of pi-hole.
#!/usr/bin/env bash
#
# bhole.sh - creates a black hole of hosts, based on the work of pi-hole, <https://pi-hole.net>.
#
# Licensed under Apache License v2.
#
# (C) Copyright 2016 - JaWi - j.w.janssen@lxtreme.nl
#
# Usage: change WORK_DIR and DEST_FILE variables to your liking and run the script. If you want, you can create and
# change the WHITELIST_FILE as well to whitelist certain hosts.
WORK_DIR="$HOME/bhole.work"
DEST_FILE="$HOME/bhole.list"
WHITELIST_FILE="/etc/bhole/bhole.whitelist"
RAW_SOURCE_LIST="/etc/bhole/bhole.sources"
# No need to change these variables...
SOURCE_URL_FILE="bholes.list"
DEST_IP="0.0.0.0"
# Ensure our working directory exists...
if [[ ! -d "$WORK_DIR" ]]; then
mkdir -p "$WORK_DIR"
fi
# Make sure if can colorize our output...
if [[ -t 1 ]]; then
ncolors=$(tput colors)
if [[ -n "$ncolors" ]] && [[ $ncolors -ge 8 ]]; then
bold="$(tput bold)"
normal="$(tput sgr0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
tab="$(tput ht)"
fi
fi
pushd $WORK_DIR >/dev/null
# Abort on first error
set -e
echo -n "${bold}${blue}:::${normal} updating ad list sources ... ${tab}"
if [[ "$RAW_SOURCE_LIST" == http* ]]; then
if [[ -f "$SOURCE_URL_FILE" ]]; then
curl -s -R -o "$SOURCE_URL_FILE.tmp" -z "$SOURCE_URL_FILE" $RAW_SOURCE_LIST
else
curl -s -R -o "$SOURCE_URL_FILE.tmp" $RAW_SOURCE_LIST
fi
elif [[ -f "$RAW_SOURCE_LIST" ]]; then
# It a source list that is present locally...
cp -u "$RAW_SOURCE_LIST" "$SOURCE_URL_FILE.tmp"
else
echo "${bold}${red}failed${normal}!"
echo "No such source file: $RAW_SOURCE_LIST!"
exit 1
fi
if [[ -f "$SOURCE_URL_FILE.tmp" ]]; then
grep -v -e '^#' -e '^\s*$' "$SOURCE_URL_FILE.tmp" > "$SOURCE_URL_FILE"
rm "$SOURCE_URL_FILE.tmp"
fi
echo "${bold}${green}done${normal}!"
changes=0
echo -n "${bold}${blue}:::${normal} sourcing ad lists for updates ... ${tab}"
while read -r url; do
name="lst-$(echo "$url" | shasum - | awk '{print $1}')"
if [[ -f "$name" ]]; then
curl -s -R -o "$name.tmp" -z "$name" $url
else
curl -s -R -o "$name.tmp" $url
fi
if [[ -f "$name.tmp" ]]; then
awk -v ipAddr="$DEST_IP" '{sub(/#.*$/,""); sub(/\r$/,""); if (NF<1) {next} else if (NF>1) {print ipAddr" "$2} else {print ipAddr" "$1}}' "$name.tmp" > "$name"
rm "$name.tmp"
# the 2nd assignment style is used to always yield a return code of `0`...
((changes++)) || :
fi
done < $SOURCE_URL_FILE
echo "${bold}${green}done${normal} (detected $changes list change(s))!"
popd >/dev/null
# we're in control again...
set +e
if [ $changes -eq 0 ]; then
echo "${bold}${yellow}Nothing changed, not creating new hosts file ...${normal}"
exit 1
fi
echo -n "${bold}${blue}:::${normal} filtering out duplicates ... ${tab}"
sort -u $WORK_DIR/lst-* | egrep -v -f $WHITELIST_FILE -e "\slocalhost$" -e "\slocalhost.localdomain$" -e "\sbroadcasthost$" -e "\slocal$" > "$DEST_FILE.tmp"
if [ $? -eq 0 ]; then
mv "$DEST_FILE.tmp" $DEST_FILE
else
rm "$DEST_FILE.tmp"
fi
echo "${bold}${green}done${normal}!"
###EOF###
https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
http://adblock.gjtech.net/?format=unix-hosts
http://mirror1.malwaredomains.com/files/justdomains
http://sysctl.org/cameleon/hosts
https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist
https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt
https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt
https://hosts-file.net/ad_servers.txt
https://raw.githubusercontent.com/quidsup/notrack/master/trackers.txt
https://ransomwaretracker.abuse.ch/downloads/RW_DOMBL.txt
https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext
https://hosts-file.net/ad_servers.txt
https://hosts-file.net/emd.txt
https://hosts-file.net/exp.txt
https://hosts-file.net/fsa.txt
https://hosts-file.net/grm.txt
https://hosts-file.net/hfs.txt
https://hosts-file.net/hjk.txt
https://hosts-file.net/mmt.txt
https://hosts-file.net/pha.txt
https://hosts-file.net/psh.txt
http://winhelp2002.mvps.org/hosts.txt
http://someonewhocares.org/hosts/hosts
localhost.localdomain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment