Skip to content

Instantly share code, notes, and snippets.

@Cybso
Forked from teffalump/README.md
Last active September 3, 2021 18:07
Show Gist options
  • Save Cybso/bf9b69c6a638ffd68281 to your computer and use it in GitHub Desktop.
Save Cybso/bf9b69c6a638ffd68281 to your computer and use it in GitHub Desktop.
#!/bin/sh
#Put in /etc/adblock.sh
#Script to grab and sort a list of adservers and malware
#Check proper DHCP config and, if necessary, update it
uci get dhcp.@dnsmasq[0].addnhosts > /dev/null 2>&1 || uci add_list dhcp.@dnsmasq[0].addnhosts=/etc/block.hosts && uci commit
#Leave crontab alone, or add to it
grep -q "/etc/adblock.sh" /etc/crontabs/root || echo "0 4 * * 0,3 sh /etc/adblock.sh" >> /etc/crontabs/root
#Delete the old block.hosts to make room for the updates
rm -f /etc/block.hosts
#Download and process the files needed to make the lists (add more, if you want)
wget -qO- http://www.mvps.org/winhelp2002/hosts.txt| sed 's/0.0.0.0/127.0.0.1/g' |grep "^127.0.0.1" > /tmp/block.build.list
wget -qO- http://www.malwaredomainlist.com/hostslist/hosts.txt|grep "^127.0.0.1" >> /tmp/block.build.list
wget -qO- "http://hosts-file.net/.\ad_servers.txt"|grep "^127.0.0.1" >> /tmp/block.build.list
wget -qO- --no-check-certificate "https://adaway.org/hosts.txt"|grep "^127.0.0.1" >> /tmp/block.build.list
#Add black list, if non-empty
[ -s "/etc/black.list" ] && awk '/^[^#]/ { print "127.0.0.1",$1 }' /etc/black.list >> /tmp/block.build.list
#Sort the download/black lists
awk '{sub(/\r$/,"");print $1,$2}' /tmp/block.build.list|sort|uniq > /tmp/block.build.before
#Use network address 0.0.0.0 instead of localhost to
#avoid conflicts with local webservers.
#Add IPv6 with :: (not specified) entry
sed -ire 's/^(127\.0\.0\.1)(.*)$/0.0.0.0\2\n:: \2/g' /tmp/block.build.before
if [ -s "/etc/white.list" ]
then
#Filter the blacklist, supressing whitelist matches
awk '/^[^#]/ {sub(/\r$/,"");print $1}' /etc/white.list | grep -vf - /tmp/block.build.before > /etc/block.hosts
else
cat /tmp/block.build.before > /etc/block.hosts
fi
#Delete files used to build list to free up the limited space
rm -f /tmp/block.build.before
rm -f /tmp/block.build.list
#Restart dnsmasq
/etc/init.d/dnsmasq restart
exit 0
#/etc/black.list
#add some server that the list doesn't block
example1.block.com
#/etc/firewall.user
#Add these two lines
iptables -t nat -I PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 53
iptables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 53
#/etc/sysupgrade.conf
#This file is a list of files that should be preserved through upgrades
#OPTIONAL!!!!!
/etc/passwd
/etc/shadow
...
...
/etc/adblock.sh #ADD THIS LINE
/etc/white.list #AND THIS ONE
/etc/block.hosts #AND THIS ONE
/etc/black.list #AND THIS ONE
#/etc/white.list
#Add whitelisted addresses, when appropriate, etc.
a248.e.akamai.net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment