Blocking URLs/IPs

From DD-WRT Wiki

(Difference between revisions)
Jump to: navigation, search

Donny (Talk | contribs)
(New page: __TOC__ ==Block URLs with an Automatically Downloaded Host File== I have kept the original script, which is below this one. This one is much shorter, but does even more checking than the...)
Next diff →

Revision as of 11:48, 5 July 2011


Block URLs with an Automatically Downloaded Host File

I have kept the original script, which is below this one. This one is much shorter, but does even more checking than the other one. It tries to download the file at least 5 times after startup An even better solution is offered in my set of optware scripts where it will also run pixelserv on the router. I didn't like the cronjob approach because the list is quite static.


echo -e "n=1\nwhile ! wget -q -O /tmp/hsts ; do\n\t[ \$n -gt 5 ] && break\n\tlet n+=1\n\tsleep 60\ndone\ngrep \"^\" /tmp/hsts | grep \"^\" | grep -v localhost | awk '{print \"$_rogue\\\t\"\$2}' | tr -d '\\\015' >/tmp/dlhosts\nrm /tmp/hsts\nkillall -HUP dnsmasq" >/tmp/wh
sh /tmp/wh &

IMHO the following script is deprecated, but I left it for comparison.

This was originally taken from mraneri from the Linksys forum, but was heavily modified.


logger WAN up script executing
sleep 5
if test ! -s /tmp/dlhosts
	cat >/tmp/dlhosts <<"EOF"
logger Downloading
wget -O - |
	grep |
	sed '2,$s/; s/[[:space:]]*#.*$//g;' |
	grep -v localhost |
	tr ' ' '\t' |
	tr -s '\t' |
        tr -d '\015' |
	sort -u >/tmp/hosts0
grep addn-hosts /tmp/dnsmasq.conf ||
	echo " " >>/tmp/dnsmasq.conf
grep addn-hosts /tmp/dnsmasq.conf ||
	echo "addn-hosts=/tmp/hosts0" >>/tmp/dnsmasq.conf
logger Restarting dnsmasq
killall -HUP dnsmasq
dnsmasq --conf-file=/tmp/dnsmasq.conf
	chmod 777 /tmp/dlhosts
grep -q '/tmp/dlhosts' /tmp/crontab || 
	echo "45 23 * * 5 root /tmp/dlhosts" >>/tmp/crontab

This script automatically downloads a host file from: "" and redirects all the URLs in that file to All those URLs are common malware or advertisement sites so is better to block them. You can also download the file, modify it with new URLs that you want to block or delete the ones you don't want to block and then upload to a web site and change the URL in the code to your custom one. Be aware that the more URLs in the file the more RAM that you will be eating from your router. Check the file size and your free memory to see if it will suit you. If not just erase some URLs... If you want to block all URLs since the router boots then just placed in the startup scripts.

--Brueggmann 10.17.2010 - I've modified the "killall" line as sending a HUP didn't reread the configuration file for me. Please see the dnsmasq man page.

Update by Aviad (A.K.A. Hotfortech): While the above works just fine, there are two main disadvantages of the above method:

1. The blocked content will be shown as "page cannot be displayed" within the websites advertisement segments and will cause the page to not load until the browser gives up on the missing object.

2. If you want to add sites to the block list, you have to do it on a per host bases... (tedious)

Enter shameless self promotion -> I have created a script on my wiki that deals with the above and more by using the pixelserv method described in the forum. you can find the script and a detailed explanation on how it works on my wiki:

Hope this helps anyone.

BobLfoot - Edit Note : 2011-01-14 added line "dnsmasq --conf-file=/tmp/dnsmasq.conf" to the script as it was stopping dnsmasq, but not restarting it. Also found that adding " grep addn-hosts /tmp/dnsmasq.conf || echo " " >>/tmp/dnsmasq.conf made sure that the dnsmasq.conf addition went on a new line. Script might be made leaner through \n usage but that is untested.

Global Blacklisting per MAC

If you have a lot of DD-WRT routers, then denying of access for abusing users through the web interface of each router can be time consuming.

Here is a small firewall script to automatically download MAC-addresses of computers that should be denied access. The format of the file is Unix textfile one MAC address per line. The script assumes that you have a jffs partition. You can run it at startup by saving it as /jffs/etc/config/wifi_bl.wanup

cd /jffs
rm wifi_blacklist.txt
#Please modify the script to download the blacklist file from your web server
module_exists=`lsmod | grep ipt_mac`
if [ -z "$module_exists" ] ; then
    insmod ipt_mac
#Deleting the old table
old_mac=`iptables -L | egrep "..:..:..:..:..:.."  | sed "s/.*\(..:..:..:..:..:..\).*/\1/"`
for mac in $old_mac ; do
    iptables -D FORWARD -p tcp -m mac --mac-source $mac -j REJECT --reject-with tcp-reset
#Adding the table again
for mac in `cat /jffs/wifi_blacklist.txt` ; do
    iptables -I FORWARD -p tcp -m mac --mac-source $mac -j REJECT --reject-with tcp-reset

White Listing

If you want to create a white list to block access by default but allow certain traffic through, then you can use this script to do it. Remove any junk comment lines beginning with # to save nvram space. Discuss here.

# IP Tables White Listing script by phuzi0n -Tek @
# Version 4. Please increment version number with subsequent modifications. GeeTek.

# Set up the chain
iptables -N wanout
iptables -I INPUT -i `nvram get lan_ifname` -j wanout
iptables -I FORWARD -i `nvram get lan_ifname` -j wanout

# Create whitelist 'function' script
echo 'iptables -I wanout  -j ACCEPT' > $WOUT
chmod 777 $WOUT

# Exempt Machine MAC
# load xt_mac instead of ipt_mac on k2.6 builds
insmod ipt_mac
$WOUT '-m mac --mac-source 00:30:18:A9:A9:C6'

# Exempt Machine IP
$WOUT '-s'

# Allow everyone access to these sites  (DNS lookup only happens once when rule is inserted and stays that single IP)
$WOUT '-d'
$WOUT '-d'
$WOUT '-d'

# Allow everyone access to these IP addresses/netmask
$WOUT '-d'
$WOUT '-d'
$WOUT '-d'
$WOUT '-d'

#Allow everyone access to specific destination ports
$WOUT '-p udp --dport 8000'
$WOUT '-p tcp --dport 80'

# Everything else gets blocked
iptables -A wanout -j REJECT --reject-with icmp-proto-unreachable