Advertisement
rockdrilla

[MikroTik scripting] DNS blacklisting script

Oct 31st, 2013
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. # DNS blacklist file
  2. :global dns_blacklist "dns_blacklist.txt";
  3. # if you're using static DNS records with '127.0.0.1':
  4. # change this value to something else (must be 'invalid' too :) )
  5. :global dns_blackhole "127.0.0.1";
  6.  
  7. # http://pastebin.com/YXYrQVsH
  8. #strreplace
  9. # 1 - where
  10. # 2 - what
  11. # 3 - with
  12. :global strreplace do={
  13. :local l1 [:len $1];
  14. :local l2 [:len $2];
  15. :local l3 [:len $3];
  16. :local lr 0;
  17. :local i 0;
  18. :local k -1;
  19. :local r $1;
  20. :do {
  21. :set lr [:len $r];
  22. :set i [:find $r $2 $k];
  23. :if ($i != nil) do={
  24. :set r ([:pick $r -1 $i] . $3 . [:pick $r ($i + $l2) $lr]) ;
  25. :set k ($i + $l3 - 1);
  26. }
  27. } while=($i < $l1)
  28. :return $r;
  29. }
  30.  
  31. #dnsrenew
  32. # reads file "$dns_blacklist" (list of DNS names, one per line)
  33. # and bans out DNS requests to them;
  34. :global dnsrenew do={
  35. :local cC [/file get [/file find name=$dns_blacklist ] contents];
  36. :local cL [ :len $cC ];
  37. :local le0 0;
  38. :local le1 -1;
  39. :local nameF "";
  40. :local nameRE "";
  41. /ip dns static remove [/ip dns static find where address=$dns_blackhole];
  42. :delay;
  43. :do {
  44. :set le0 [:find $cC "\n" $le1];
  45. :set nameF [:pick $cC $le1 $le0];
  46. :set le1 ($le0 + 1);
  47. :if ($nameF != nil) do={
  48. :set nameRE (".*\\." . [ $strreplace $nameF "." ("\\".".")]);
  49. /ip dns static add address=$dns_blackhole name=$nameF;
  50. /ip dns static add address=$dns_blackhole name=$nameRE;
  51. :put ($nameF . "\t" . $nameRE);
  52. }
  53. } while=($le0 < $cL)
  54. :delay;
  55. /ip dns cache flush;
  56. }
  57.  
  58. # call dnsrenew()
  59. $dnsrenew
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement