Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
208 lines (196 sloc) 5.89 KB
#!/bin/bash
echo -n "Starting script at: "
date "+%a %d %b %Y - %r"
echo ""
echo "Checking for lockfile:"
checklock=$(cat lockfile.txt 2>&1 | awk '{ print $1 }')
if [ $checklock == "lock" ]; then
echo "- Lock file found. Exiting."
exit
else
echo "- Lock file not found. Continuing."
fi
echo ""
echo -n "Creating lockfile (sleep 3): "
echo "lock" > lockfile.txt
sleep 3
echo "DONE"
verifications=0
echo ""
echo "Checking file integrity:"
echo -n "- Verifying character whitelist: "
if egrep -q "[^][a-zA-Z0-9 #:|=?+%&\*()_;/ @~!źż'\"<>.,\\-]" current-hosts.txt; then
echo "FAILED"
echo -n "- Unwhitelisted character(s) found: \""
fold -w1 current-hosts.txt | sort -u | egrep "[^][a-zA-Z0-9 #:|=?+%&\*()_;/ @~!źż'\"<>.,\\-]" | tr -d "\n"
echo "\""
echo ""
echo -n "Removing lockfile (sleep 3): "
sleep 3
rm lockfile.txt
echo "DONE"
echo ""
echo -n "Script exiting (unwhitelisted characters found) at: "
date "+%a %d %b %Y - %r"
echo "- Exiting."
exit
else
echo "SUCCESS"
echo -n "- All whitelisted special characters: \""
fold -w1 current-hosts.txt | sort -u | sed 's/[a-zA-Z0-9]//' | tr -d "\n"
echo "\""
verifications=$((verifications+1))
fi
echo -n "- Grep-stripping allowed/safe content from file: "
#All 0.0.0.0 entries (including xn-- domains as well as erroneous entires)
grepstrip=$(egrep -v "^0.0.0.0 [a-zA-Z0-9._-]{,96}[.][a-z0-9-]{2,20}$" current-hosts.txt)
#All lines starting with a hash (comments)
grepstrip2=$(echo "$grepstrip" | egrep -v "^#.*$")
#Blank lines
grepstrip=$(echo "$grepstrip2" | egrep -v "^$")
#Lines starting with spaces/multiple tabs
grepstrip2=$(echo "$grepstrip" | egrep -v "^( | {32}| | {4}| ? {5})# ")
#Remove default/system hosts file entries
grepstrip=$(echo "$grepstrip2" | egrep -v "^(127[.]0[.]0[.]1 localhost|127[.]0[.]0[.]1 localhost[.]localdomain|127[.]0[.]0[.]1 local|255[.]255[.]255[.]255 broadcasthost|::1 localhost|fe80::1%lo0 localhost|0[.]0[.]0[.]0 0[.]0[.]0[.]0)$")
#Remove bits left behind
grepstrip2=$(echo "$grepstrip" | egrep -v "^(0[.]0[.]0[.]0 collectionofbestporn|0[.]0[.]0[.]0 sexdating|0[.]0[.]0[.]0 spankbang)$")
#Remove hostname entry
grephostname=$(hostname)
grepstrip=$(echo "$grepstrip2" | egrep -v "^127[.]0[.]0[.]1 $grephostname$")
if [ "$grepstrip" == "" ]; then
echo "SUCCESS"
echo -n "- All content successfully stripped: \""
echo "$grepstrip" | tr -d "\n"
echo "\""
verifications=$((verifications+1))
else
echo "FAILED"
echo ""
echo "- Lines left over after filtering (max 20 shown):"
echo "$grepstrip" | sed 's/\./(dot)/g' | head -n 20
echo ""
echo -n "Removing lockfile (sleep 3): "
sleep 3
rm lockfile.txt
echo "DONE"
echo ""
echo -n "Script exiting (grep strip failed) at: "
date "+%a %d %b %Y - %r"
echo "- Exiting."
exit
fi
echo -n "- Checking file byte count: "
bytecount=$(wc -c current-hosts.txt | awk '{ print $1 }')
if (( $bytecount > 1200000 )); then
echo "SUCCESS"
echo -n "- Byte count greater than 1200000: \""
echo "$bytecount" | tr -d "\n"
echo "\""
verifications=$((verifications+1))
else
echo "FAILED"
echo -n "- Byte count less than 1200000: \""
echo "$bytecount" | tr -d "\n"
echo "\""
echo ""
echo -n "Removing lockfile (sleep 3): "
sleep 3
rm lockfile.txt
echo "DONE"
echo ""
echo -n "Script exiting (not enough bytes) at: "
date "+%a %d %b %Y - %r"
echo "- Exiting."
exit
fi
echo -n "- Checking file newline count: "
newlinecount=$(wc -l current-hosts.txt | awk '{ print $1 }')
if (( $newlinecount > 50000 )); then
echo "SUCCESS"
echo -n "- Newline count greater than 50000: \""
echo "$newlinecount" | tr -d "\n"
echo "\""
verifications=$((verifications+1))
else
echo "FAILED"
echo -n "- Newline count less than 50000: \""
echo "$newlinecount" | tr -d "\n"
echo "\""
echo ""
echo -n "Removing lockfile (sleep 3): "
sleep 3
rm lockfile.txt
echo "DONE"
echo ""
echo -n "Script exiting (not enough lines) at: "
date "+%a %d %b %Y - %r"
echo "- Exiting."
exit
fi
if [ $verifications == "4" ]; then
echo ""
echo "***************************"
echo "File successfully verified!"
echo -n "- Verification count: \""
echo "$verifications" | tr -d "\n"
echo "\""
echo "***************************"
echo ""
echo "Old /etc/hosts file:"
echo -n "- "
head -n 1 /etc/hosts | tr -d "#"
echo -n "- SHA256: "
sha256sum /etc/hosts | awk '{ print $1 }'
echo -n "- SHA1: "
sha1sum /etc/hosts | awk '{ print $1 }'
echo -n "- MD5: "
md5sum /etc/hosts | awk '{print $1 }'
echo ""
echo "Diff between both files (max 40 lines shown):"
echo "<--- old - new --->"
diff /etc/hosts current-hosts.txt | sed 's/\./(dot)/g' | head -n 40
echo ""
echo "Implementing new hosts file:"
echo -n "- Copying current-hosts.txt to /etc/hosts: "
sleep 1
cp current-hosts.txt /etc/hosts
sleep 1
echo "DONE"
echo ""
echo "New /etc/hosts file:"
echo -n "- "
head -n 1 /etc/hosts | tr -d "#"
echo -n "- SHA256: "
sha256sum /etc/hosts | awk '{ print $1 }'
echo -n "- SHA1: "
sha1sum /etc/hosts | awk '{ print $1 }'
echo -n "- MD5: "
md5sum /etc/hosts | awk '{print $1 }'
else
#Script should never get here
echo ""
echo "File verification failed!"
echo -n "- Verification count: \""
echo "$verifications" | tr -d "\n"
echo "\""
echo ""
echo -n "Removing lockfile (sleep 3): "
sleep 3
rm lockfile.txt
echo "DONE"
echo ""
echo -n "Script exiting (unknown error) at: "
date "+%a %d %b %Y - %r"
echo "- Exiting."
exit
fi
echo ""
echo -n "Removing lockfile (sleep 3): "
sleep 3
rm lockfile.txt
echo "DONE"
echo ""
echo -n "Script finishing (implemented successfully) at: "
date "+%a %d %b %Y - %r"
echo "- See hosts file at \"/etc/hosts\"."
echo ""