Skip to content

Instantly share code, notes, and snippets.

@AdaFrame
Created November 19, 2016 08:47
Show Gist options
  • Save AdaFrame/f67a48a896ceeb60c51cfe7a5086fc80 to your computer and use it in GitHub Desktop.
Save AdaFrame/f67a48a896ceeb60c51cfe7a5086fc80 to your computer and use it in GitHub Desktop.
Simple shell script that updates the /etc/hosts file with the newest version from someonewhocares.org/hosts, a hosts file configuration that redirects the URLs of many unwanted sites back to the localhost interface.
#!/bin/bash
## CoffeeFrame
## Coffeeframe(a)linux.com
## Update /etc/hosts file with version from someonewhocares.org/hosts/hosts and backs up old hosts file
## WARNING -- As of 2016-11-19 someonewhocares.org does not provide a valid SSL/TLS certificate, meaning the hosts file is retrieved over an unencrypted connection. This method is vulnerable to Man-in-the-Middle attacks, allowing an intermediary attacker to modify the hosts file as they see fit. For example, an attacker modify or add a line redirecting www.bankofamerica.com to a site designed to harvest bank credentials. Use at your own risk.
## I recommend backing up your current /etc/hosts file before running this script for the first time.
TEMPHOSTS=`mktemp -p /var/ tempHosts.XXXXXXX`
curl -s http://someonewhocares.org/hosts/hosts > '$TEMPHOSTS'
diff /etc/hosts '$TEMPHOSTS' > /dev/null ## Compare current hosts file to new hosts file.
if [ $? -eq 1 ] ## If new hosts file is different than current hosts file
then
cat /etc/hosts > /etc/hosts.backup ## Backup old hosts file
cat '$TEMPHOSTS' > /etc/hosts
fi
rm '$TEMPHOSTS' ## Deletes temporary file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment