Ad blocking

From DD-WRT Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 10:32, 2 August 2012 (edit)
Sash (Talk | contribs)
(shorten the script. keep it more simple. download at startup and once a day.)
← Previous diff
Revision as of 18:42, 29 August 2012 (edit) (undo)
Oldschool2012 (Talk | contribs)
(added section on ad blocking with DD-WRT micro build)
Next diff →
Line 35: Line 35:
Ensure cron is enabled. Ensure cron is enabled.
 +
 +== Ad Blocking with the DD-WRT Micro build ==
 +
 +Due to design limitations of the Micro build, the router can be instructed to block ads (though a hosts file) only on a temporary basis. The hosts file can only be written to temporary memory that is lost with each reboot.
 +
 +As soon as the router is rebooted, loses electrical power, or is restarted with new settings, you must complete the steps below again, in order to again begin ad blocking.
 +
 +Unfortunately, the Micro build currently does not include features that would allow the router to automatically download an updated hosts file itself, so you must do it manually. Also, the Micro build does not include SSH capability, so you must transfer the content of the hosts file by manually pasting it into the router's hosts file through a telnet window, instead of SSH uploading it.
 +
 +===Steps===
 +
 +# Download the newest update of a trusted hosts file, such as from MVPS.org at:
 +http://winhelp2002.mvps.org/hosts.txt
 +
 +# Open the hosts text file in Notepad, TextEdit, or Notepad++, or another text editor. Highlight all the text in the file and copy it.
 +
 +# Go to your router's web interface through your web browser, typically by entering the URL http://192.168.1.1. If you assigned your router a different local IP address, use that instead.
 +
 +# In the web interface, click on the Services tab. Enter your DD-WRT router username and password, if prompted.
 +
 +# Scroll down to the 'DNSMasq' section. Make sure that DNSMasq bullet is set to 'Enable' and that no other DNSMasq options are set.
 +
 +# Use Putty or another Telnet client to connect to the router's Telnet interface. (That is, connect to the router's local IP address on port 23 - telnet.)
 +
 +# Enter your login and password, which are the same as what you assigned for your web interface.
 +
 +# At the DD-WRT command prompt, type <code>killall DNSMasq</code> and hit enter.
 +
 +# Type <code>pwd</code> and hit enter. You should get back "/tmp/root". Type <code>cd ..</code> and hit enter.
 +
 +# At the next prompt, type <code>cat > hosts</code> and hit enter.
 +
 +# Paste all the text you copied earlier from the hosts.txt file. (For example, in Putty, bring your mouse cursor to the cursor location at the bottom of the telnet window and click your right mouse button to paste.)
 +
 +# After a minute or so of the text pasting/transferring, the screen will stop scrolling. When that happens, finish the file with a Ctrl-D key combination.
 +
 +# Type <code>dnsmasq --conf-file=/tmp/dnsmasq.con --addn-hosts=/tmp/hosts</code> and hit enter.
 +
 +# You are now finished. Type <code>exit</code> and hit enter.
 +
 +# Test out your configuration. Use a computer or connected device to use the local commands "ping" or "tracert" on known good sites (like google.com) vs. sites on the hosts list. The known good sites should have much higher ping times than the sites on the hosts list (for example, 12 ms versus less than 1 ms). The "tracert" (traceroute) comamnd should show a much lengthier Internet path to the good site than the bad site (which should only be one or two hops).
 +
 +# Again, repeat all these steps if your router goes down for some reason.
 +
[[Category:Traffic moderation]] [[Category:Traffic moderation]]

Revision as of 18:42, 29 August 2012

This script, courtesy of several people on the forums, who probably should be named, will enable host-based ad blocking via dns.

Maybe this script will not work on every device or it might crash your router when ther is not enough free ram.

Just put this script into "Startup":

#!/bin/sh
logger WAN up script executing
if test -s /tmp/hosts0
then
        rm /tmp/hosts0
fi

logger Downloading http://www.mvps.org/winhelp2002/hosts.txt
wget -O - http://www.mvps.org/winhelp2002/hosts.txt | grep 127.0.0.1 |
	sed '2,$s/127.0.0.1/0.0.0.0/g; 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 "addn-hosts=/tmp/hosts0" >>/tmp/dnsmasq.conf
logger Restarting dnsmasq
killall dnsmasq
dnsmasq --conf-file=/tmp/dnsmasq.conf

...and this into cron:

* 1 * * * root /tmp/.rc_startup

Enable DNSMasq and Local DNS in Services tab; no Additional DNSMasq options necessary.

Ensure cron is enabled.

Ad Blocking with the DD-WRT Micro build

Due to design limitations of the Micro build, the router can be instructed to block ads (though a hosts file) only on a temporary basis. The hosts file can only be written to temporary memory that is lost with each reboot.

As soon as the router is rebooted, loses electrical power, or is restarted with new settings, you must complete the steps below again, in order to again begin ad blocking.

Unfortunately, the Micro build currently does not include features that would allow the router to automatically download an updated hosts file itself, so you must do it manually. Also, the Micro build does not include SSH capability, so you must transfer the content of the hosts file by manually pasting it into the router's hosts file through a telnet window, instead of SSH uploading it.

Steps

  1. Download the newest update of a trusted hosts file, such as from MVPS.org at:

http://winhelp2002.mvps.org/hosts.txt

  1. Open the hosts text file in Notepad, TextEdit, or Notepad++, or another text editor. Highlight all the text in the file and copy it.
  1. Go to your router's web interface through your web browser, typically by entering the URL http://192.168.1.1. If you assigned your router a different local IP address, use that instead.
  1. In the web interface, click on the Services tab. Enter your DD-WRT router username and password, if prompted.
  1. Scroll down to the 'DNSMasq' section. Make sure that DNSMasq bullet is set to 'Enable' and that no other DNSMasq options are set.
  1. Use Putty or another Telnet client to connect to the router's Telnet interface. (That is, connect to the router's local IP address on port 23 - telnet.)
  1. Enter your login and password, which are the same as what you assigned for your web interface.
  1. At the DD-WRT command prompt, type killall DNSMasq and hit enter.
  1. Type pwd and hit enter. You should get back "/tmp/root". Type cd .. and hit enter.
  1. At the next prompt, type cat > hosts and hit enter.
  1. Paste all the text you copied earlier from the hosts.txt file. (For example, in Putty, bring your mouse cursor to the cursor location at the bottom of the telnet window and click your right mouse button to paste.)
  1. After a minute or so of the text pasting/transferring, the screen will stop scrolling. When that happens, finish the file with a Ctrl-D key combination.
  1. Type dnsmasq --conf-file=/tmp/dnsmasq.con --addn-hosts=/tmp/hosts and hit enter.
  1. You are now finished. Type exit and hit enter.
  1. Test out your configuration. Use a computer or connected device to use the local commands "ping" or "tracert" on known good sites (like google.com) vs. sites on the hosts list. The known good sites should have much higher ping times than the sites on the hosts list (for example, 12 ms versus less than 1 ms). The "tracert" (traceroute) comamnd should show a much lengthier Internet path to the good site than the bad site (which should only be one or two hops).
  1. Again, repeat all these steps if your router goes down for some reason.