Mikrotik adblock script

I’ve been using a script to block ads directly form my Mikrotik router, but unfortunately the domain used by the script has expired, so I decided to generate the list myself.

I’m using the php code form here: https://github.com/tarampampam/mikrotik-hosts-parser/

First of all note that this script will use about 50 mb of ram.

Edit: This script works only for devices with at least 128 mb ram, for devices with 64 mb like RB751G-2HnD you can use the lite version with approximately 3000 entries from here. 

To setup ad blocking follow these steps:

  • Set the router as a DNS server:

  • force the DNS request to be directed to the router:
/ip firewall nat
 add action=redirect chain=dstnat dst-port=53 in-interface=bridge protocol=udp
  • Add the script to import the satic DNS list and the scheduler to update the list one a day:
/system script
 add name=adblock policy=\
 ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":\
 log warning \"starting adblock update\";\r\
 \n\
 \n\
 \n:delay 20;\r\
 \n\
 \n\
 \n:log warning \"downloading adblock\";\r\
 \n\
 \n\
 \n\
 \n\
 \n:local hostScriptUrl \"https://micu.eu/adblock/adblock.php\";\
 \n\
 \n\
 \n\
 \n\r\
 \n:local scriptName \"adblock\";\
 \n\
 \n\
 \n\
 \n\r\
 \ndo {\
 \n\
 \n /tool fetch mode=http url=\$hostScriptUrl dst-path=(\"./\".\$scriptName\
 );\
 \n\
 \n\r\
 \n:delay 20;\
 \n\
 \n\r\
 \n:if ([:len [/file find name=\$scriptName]] > 0) do={\r\
 \n\
 \n\
 \n :log warning \"removing old adblock list\";\
 \n\
 \n\r\
 \n /ip dns static remove [/ip dns static find address=127.0.0.1];\
 \n\
 \n\r\
 \n :log warning \"importing new adblock list\";\r\
 \n\
 \n\
 \n /import file-name=\$scriptName;\r\
 \n\
 \n\
 \n /file remove \$scriptName;\r\
 \n\
 \n\
 \n :log warning \"adblock list imported\";\r\
 \n\
 \n\
 \n } else={\
 \n\
 \n \r\
 \n:log warning \"adblock list not downloaded, script stopped\";\r\
 \n\
 \n\
 \n }\r\
 \n\
 \n\
 \n} on-error={\
 \n\
 \n \r\
 \n:log warning \"adblock list download FAILED\";\
 \n\
 \n\r\
 \n};"
 /system scheduler
 add interval=1d name=adblock on-event=adblock policy=\
 ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
 start-time=startup
 add name=adblock_startup on-event=\
 ":delay 30;\r\
 \n/system script run adblock" policy=\
 ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
 start-time=startup
19 Comments

Add a Comment