Code: Select all
/ip firewall nat add
chain=dstnat action=redirect to-ports=53 protocol=udp dst-port=53 to-address=192.168.88.5
Such as:
Client --> Mikrotik-->pihole-->Mikrotik as a DNS--> wan
/ip firewall nat add
chain=dstnat action=redirect to-ports=53 protocol=udp dst-port=53 to-address=192.168.88.5
/ip firewall nat
add action=dst-nat chain=dstnat comment=dns_redirect dst-port=53 in-interface-list=LAN protocol=udp src-address=!192.168.88.5 to-addresses=192.168.88.5 to-ports=53
add action=masquerade chain=srcnat comment=dns_redirect dst-address=192.168.88.5 dst-port=53 protocol=udp src-address=192.168.88.0/24
add action=dst-nat chain=dstnat comment=dns_redirect dst-port=53 in-interface-list=LAN protocol=tcp src-address=!192.168.88.5 to-addresses=192.168.88.5 to-ports=53
add action=masquerade chain=srcnat comment=dns_redirect dst-address=192.168.88.5 dst-port=53 protocol=tcp src-address=192.168.88.0/24
/ip dhcp-server network
add address=192.168.88.0/24 dns-server=192.168.88.5 gateway=192.168.88.1 netmask=24
add address=192.168.88.5/32 dns-server=192.168.88.1 gateway=192.168.88.1 netmask=24
:local currentDNS [/ip dns get server]
:local piholeDNS "192.168.88.5"
:local backupDNS "8.8.8.8,8.8.4.4"
:local testDomain "www.google.com"
:if ($currentDNS = $piholeDNS) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dns set servers=$backupDNS
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dns set servers=$piholeDNS
} on-error={}
}
/ip route
add dst-address=192.168.188.2 gateway=bridge
/ip dhcp-server network
add address=192.168.88.0/24 dns-server=192.168.188.2 gateway=192.168.88.1
add address=192.168.188.2/32 dns-server=192.168.88.1 gateway=192.168.88.1
/ip firewall nat
add action=dst-nat chain=dstnat dst-address=192.168.188.2 dst-port=53 protocol=tcp to-address=192.168.88.1 comment=pihole_bypass disabled=yes
add action=dst-nat chain=dstnat dst-address=192.168.188.2 dst-port=53 protocol=udp to-address=192.168.88.1 comment=pihole_bypass disabled=yes
:if ([/ip firewall nat get [find where comment=pihole_bypass] disabled]=yes) do={:do {resolve google.com server=192.168.188.2} on-error={/ip firewall nat enable [find where comment=pihole_bypass]}} else={:do {resolve google.com server=192.168.188.2; /ip firewall nat disable [find where comment=pihole_bypass]}}
/ip dhcp-server network
add address=192.168.88.0/24 dns-server=192.168.88.5 gateway=192.168.88.1
add address=192.168.88.5/32 dns-server=192.168.88.1 gateway=192.168.88.1
/ip firewall nat
add action=dst-nat chain=dstnat dst-address=192.168.88.5 dst-port=53 protocol=tcp to-address=192.168.88.1 comment=pihole_bypass disabled=yes
add action=dst-nat chain=dstnat dst-address=192.168.88.5 dst-port=53 protocol=udp to-address=192.168.88.1 comment=pihole_bypass disabled=yes
:if ([/ip firewall nat get [find where comment=pihole_bypass] disabled]=yes) do={:do {resolve google.com server=192.168.88.5} on-error={/ip firewall nat enable [find where comment=pihole_bypass]}} else={:do {resolve google.com server=192.168.88.5; /ip firewall nat disable [find where comment=pihole_bypass]}}
/ip route
add dst-address=192.168.88.5 gateway=bridge
/ip dhcp-server network
add address=192.168.88.0/24 dns-server=192.168.88.5 gateway=192.168.88.1
add address=192.168.88.5/32 dns-server=192.168.88.1 gateway=192.168.88.1
:local currentDNS "192.168.88.5"
:local gateway "192.168.88.1"
:local piholeDNS "192.168.88.5"
:local testDomain "www.google.com"
:if ([/ip dhcp-server network get [find dns-server=$piholeDNS]]) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dhcp-server network remove [find comment="defconf"]
/ip dhcp-server network add address=192.168.88.0/24 comment=defconf gateway=$gateway netmask=24
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dhcp-server network set 0 address=192.168.88.0/24 dns-server=$piholeDNS comment=defconf gateway=$gateway netmask=24
} on-error={}
}
:local currentDNS "192.168.88.5"
[/ip dns get servers]
[/ip dhcp-server network address=192.168.88.0/24 comment=defconf get dns-servers]
:local currentDHCPDNS [/ip dhcp-server network print count-only where dns-server=$piholeDNS]
:if ($currentDNS = $piholeDNS) do={
::if (/ip dhcp-server network get [find dns-server=$piholeDNS]) do={
/ip dhcp-server network set 0 address=192.168.88.0/24 dns-server=$piholeDNS comment=defconf dns-none=yes gateway=$gateway netmask=24
/ip dhcp-server network set 0 address=192.168.88.0/24 dns-server=$piholeDNS comment=defconf dns-none=no gateway=$gateway netmask=24
} else={
/ip dhcp-server network remove [find comment="defconf"]
/ip dhcp-server network add address=192.168.88.0/24 comment=defconf gateway=$gateway netmask=24
} else={
/ip dhcp-server network set 0 address=192.168.88.0/24 dns-server=$piholeDNS comment=defconf gateway=$gateway netmask=24
:local gateway "192.168.88.1"
:local currentDNS [/ip dhcp-server network get [find comment=defconf] dns-server]
:local piholeDNS "192.168.88.5"
:local backupDNS ""
:local testDomain "www.google.com"
:if ($currentDNS = $piholeDNS) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dhcp-server network set 0 address=192.168.88.0/24 dns-server=$backupDNS comment=defconf gateway=$gateway netmask=24;
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dhcp-server network set 0 address=192.168.88.0/24 dns-server=$piholeDNS comment=defconf gateway=$gateway netmask=24;
} on-error={}
}
:local gateway "192.168.88.1"
:local currentDNS [/ip dhcp-server network get [find comment=defconf] dns-server]
:local piholeDNS "192.168.88.5"
:local backupDNS ""
:local testDomain "www.google.com"
:if ($currentDNS = $piholeDNS) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dhcp-server network set [find comment=defconf] dns-server=$backupDNS;
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dhcp-server network set [find comment=defconf] dns-server=$piholeDNS;
} on-error={}
}
:local IPsubnet "192.168.88.0/24"
:local currentDNS
:local piholeDNS "192.168.88.5"
:local alternateDNS "192.168.88.1"
:local testDomain "www.google.com"
:set $currentDNS [/ip dhcp-server network get [find address=$IPsubnet] dns-server]
:if ($currentDNS=$piholeDNS) do={
:do {
:resolve $testDomain server=$piholeDNS
} on-error={
/ip dhcp-server network set [find address=$IPsubnet] dns-server=$alternateDNS
}
} else={
:do {
:resolve $testDomain server=$piholeDNS
/ip dhcp-server network set [find address=$IPsubnet] dns-server=$piholeDNS
} on-error={
}
}
/ip route
add dst-address=192.168.188.2 gateway=bridge
/ip dhcp-server network
add address=192.168.88.0/24 dns-server=192.168.188.2 gateway=192.168.88.1
add address=192.168.188.2/32 dns-server=192.168.88.1 gateway=192.168.88.1
/ip firewall nat add
chain=dstnat action=redirect to-ports=53 protocol=udp dst-port=53 to-address=192.168.88.5
/ip firewall nat
add chain=dstnat dst-address=192.168.88.1 src-address=!192.168.88.5 in-interface=bridge dst-port=53 protocol=tcp action=dst-nat to-address=192.168.88.5
add chain=dstnat dst-address=192.168.88.1 src-address=!192.168.88.1 in-interface=bridge dst-port=53 protocol=udp action=dst-nat to-address=192.168.88.5
add chain=srcnat dst-address=192.168.88.5 src-address=192.168.88.0/24 action=masquerade
/Ip firewall filter
add action=accept chain=forward comment="ENABLE LAN to WAN" in-interface=\
bridge out-interface-list=WAN
add action=drop chain=forward comment="Drop All Else"
add action=drop chain=forward dst-port=53 in-interface-list=WAN protocol=tcp
add action=drop chain=forward dst-port=53 in-interface-list=WAN protocol=udp
/ip firewall nat
add action=masquerade chain=srcnat comment="defconf: masquerade" \
ipsec-policy=out,none out-interface-list=WAN
/ip route
add dst-address=192.168.188.2 gateway=bridge
/interface bridge port remove [find interface="ether5"]
/interface list member
add interface=ether5 list=LAN
/ip address
add address=192.168.188.1/24 comment=pinet interface=ether5 network=192.168.188.0
/ip pool
add name=pinet ranges=192.168.188.20-192.168.188.40
/ip dhcp-server
add address-pool=pinet disabled=no interface=ether5 name=pinetDHCP
/ip dhcp-server network
add address=192.168.188.0/24 comment=pinet gateway=192.168.88.1 netmask=24
/ip firewall address-list
add address=192.168.188.20-192.168.188.40 list=allowed_to_router
# may/13/2021 08:15:52 by RouterOS 6.48.2
# model = RouterBOARD 962UiGS-5HacT2HnT
/interface bridge
add admin-mac=xxxxxx auto-mac=no comment=defconf name=bridge
/interface ethernet
set [ find default-name=ether3 ] advertise=\
100M-half,100M-full,1000M-half,1000M-full
set [ find default-name=sfp1 ] disabled=yes
/interface pppoe-client
add add-default-route=yes disabled=no interface=ether1 name=pppoe-out1 \
password=xxxxx user=xxxxxxx
/interface wireless
/interface list
add comment=defconf name=WAN
add comment=defconf name=LAN
/interface wireless security-profiles
/ip pool
add name=dhcp ranges=192.168.88.10-192.168.88.254
add name=pinet ranges=192.168.188.20-192.168.188.40
/ip dhcp-server
add address-pool=dhcp disabled=no interface=bridge name=defconf
add address-pool=pinet disabled=no interface=ether5 name=pinetDHCP
/user group
set full policy="local,telnet,ssh,ftp,reboot,read,write,policy,test,winbox,pas\
sword,web,sniff,sensitive,api,romon,dude,tikapp"
/interface bridge port
add bridge=bridge comment=defconf interface=ether2
add bridge=bridge comment=defconf interface=ether3
add bridge=bridge comment=defconf interface=ether4
add bridge=bridge comment=defconf interface=sfp1
add bridge=bridge comment=defconf interface=wlan1
add bridge=bridge comment=defconf interface=wlan2
/ip neighbor discovery-settings
set discover-interface-list=none
/ip settings
set rp-filter=loose
/interface detect-internet
set detect-interface-list=all
/interface list member
add comment=defconf interface=bridge list=LAN
add comment=defconf interface=ether1 list=WAN
add interface=pppoe-out1 list=WAN
add interface=ether5 list=LAN
/ip address
add address=192.168.88.1/24 comment=defconf interface=bridge network=\
192.168.88.0
add address=192.168.188.1/24 comment=pinet interface=ether5 network=\
192.168.188.0
/ip cloud
set update-time=no
/ip dhcp-client
add comment=defconf interface=ether1
/ip dhcp-server lease
add address=192.168.88.245 client-id=1:bc:5f:f4:8a:a0:2 mac-address=\
xxxxxx server=defconf
add address=192.168.88.246 client-id=1:10:dd:b1:b0:32:3d mac-address=\
xxxxxx server=defconf
add address=192.168.88.247 client-id=1:0:26:bb:68:22:82 mac-address=\
xxxxxx server=defconf
add address=192.168.88.4 client-id=1:dc:a6:32:44:f:f8 mac-address=\
xxxxxx server=defconf
/ip dhcp-server network
add address=192.168.88.0/24 comment=defconf gateway=192.168.88.1
add address=192.168.188.0/24 comment=pinet gateway=192.168.88.1 netmask=24
/ip dns
set allow-remote-requests=yes cache-max-ttl=2d query-server-timeout=100ms \
query-total-timeout=5s servers=1.1.1.1,1.0.0.1 use-doh-server=\
https://cloudflare-dns.com/dns-query verify-doh-cert=yes
/ip dns static
add address=192.168.88.1 name=router.lan
add address=104.16.248.249 name=cloudflare-dns.com
add address=104.16.249.249 name=cloudflare-dns.com
/ip firewall address-list
add address=192.168.88.2-192.168.88.254 list=allowed_to_router
add address=0.0.0.0/8 comment=RFC6890 list=not_in_internet
add address=172.16.0.0/12 comment=RFC6890 list=not_in_internet
add address=192.168.0.0/16 comment=RFC6890 list=not_in_internet
add address=10.0.0.0/8 comment=RFC6890 list=not_in_internet
add address=169.254.0.0/16 comment=RFC6890 list=not_in_internet
add address=127.0.0.0/8 comment=RFC6890 list=not_in_internet
add address=224.0.0.0/4 comment=Multicast list=not_in_internet
add address=198.18.0.0/15 comment=RFC6890 list=not_in_internet
add address=192.0.0.0/24 comment=RFC6890 list=not_in_internet
add address=192.0.2.0/24 comment=RFC6890 list=not_in_internet
add address=198.51.100.0/24 comment=RFC6890 list=not_in_internet
add address=203.0.113.0/24 comment=RFC6890 list=not_in_internet
add address=100.64.0.0/10 comment=RFC6890 list=not_in_internet
add address=240.0.0.0/4 comment=RFC6890 list=not_in_internet
add address=192.88.99.0/24 comment="6to4 relay Anycast [RFC 3068]" list=\
not_in_internet
add address=192.168.188.20-192.168.188.40 list=allowed_to_router
/ip firewall filter
add action=accept chain=input comment="defconf: accept established,related" \
connection-state=established,related
add action=drop chain=input comment="defconf: drop invalid" connection-state=\
invalid
add action=accept chain=input comment="defconf: accept ICMP" protocol=icmp
add action=accept chain=input comment="Allow ADMIN to Router" \
in-interface-list=LAN src-address-list=allowed_to_router
add action=accept chain=input in-interface-list=LAN port=53 protocol=tcp
add action=accept chain=input in-interface-list=LAN port=53 protocol=udp
add action=drop chain=input
add action=accept chain=forward comment="defconf: accept in ipsec policy" \
ipsec-policy=in,ipsec
add action=accept chain=forward comment="defconf: accept out ipsec policy" \
ipsec-policy=out,ipsec
add action=fasttrack-connection chain=forward comment="defconf: fasttrack" \
connection-state=established,related
add action=accept chain=forward comment="defconf: accept established,related" \
connection-state=established,related
add action=drop chain=forward comment="defconf: drop invalid" \
connection-state=invalid
add action=accept chain=forward comment=" Allow Port Forwarding - DSTNAT" \
connection-nat-state=dstnat connection-state=new in-interface-list=WAN
add action=accept chain=forward comment="ENABLE LAN to WAN" in-interface=\
bridge out-interface-list=WAN
add action=drop chain=forward comment="Drop All Else"
add action=drop chain=forward dst-port=53 in-interface-list=WAN protocol=tcp
add action=drop chain=forward dst-port=53 in-interface-list=WAN protocol=udp
/ip firewall mangle
/ip firewall nat
add action=masquerade chain=srcnat comment="defconf: masquerade" \
ipsec-policy=out,none out-interface-list=WAN
/ip route
add check-gateway=ping distance=1 gateway=104.16.248.249 routing-mark=to_ISP1
add check-gateway=ping distance=1 gateway=pppoe-out1 routing-mark=to_ISP1
add check-gateway=ping distance=2 gateway=104.16.249.249 routing-mark=to_ISP1
add distance=1 dst-address=104.16.248.249/32 gateway=pppoe-out1 scope=10
add distance=1 dst-address=104.16.249.249/32 gateway=pppoe-out1 scope=10
/ip service
set telnet disabled=yes
set ftp disabled=yes
set ssh port=
set api disabled=yes
set winbox address=192.168.88.0/24
set api-ssl disabled=yes
/ip ssh
set forwarding-enabled=remote strong-crypto=yes
/ip upnp
set enabled=yes
/ip upnp interfaces
add interface=bridge type=internal
add interface=ether1 type=external
/system clock
set time-zone-name=Asia/Dhaka
/system logging
add topics=wireless,debug
add disabled=yes topics=dns
/system ntp client
set enabled=yes server-dns-names=\
0.pool.ntp.org,1.pool.ntp.org,2.pool.ntp.org,3.pool.ntp.org
/system scheduler
add interval=1d name=reboot on-event="system reboot" policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
start-date=jun/07/2020 start-time=05:05:00
add interval=2w1d name="Package upgrade" on-event=\
"system package update install" policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
start-date=jun/23/2020 start-time=04:00:00
add interval=2w1d name="Routerboard Upgrade" on-event=":global Var1\r\
\n:global Var2\r\
\n:set Var1 \"\$[/system package get system version]\"\r\
\n:set Var2 \"\$[/system routerboard get current-firmware]\"\r\
\n:if (\$Var1>\$Var2) do={/system routerboard upgrade;\r\
\n/system reboot;\r\
\n}" policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
start-date=jun/23/2020 start-time=04:10:00
/tool bandwidth-server
set enabled=no
/tool mac-server
set allowed-interface-list=none
/tool mac-server mac-winbox
set allowed-interface-list=none
/tool mac-server ping
set enabled=no
/ip firewall filter
add action=accept chain=forward comment="ENABLE LAN to WAN" in-interface=\
bridge out-interface-list=WAN
add action=drop chain=forward comment="Drop All Else"
/ip firewall filter
add action=drop chain=forward comment="Drop in from WAN not DSTNAT" connection-nat-state=!dstnat in-interface-list=WAN
/ip firewall filter
add action=accept chain=input comment="defconf: accept established,related" \
connection-state=established,related
add action=drop chain=input comment="defconf: drop invalid" connection-state=\
invalid
add action=accept chain=input comment="defconf: accept ICMP" protocol=icmp
add action=accept chain=input comment="Allow ADMIN to Router" \
in-interface-list=LAN src-address-list=allowed_to_router
add action=accept chain=input in-interface-list=LAN port=53 protocol=tcp
add action=accept chain=input in-interface-list=LAN port=53 protocol=udp
add action=drop chain=input
add action=accept chain=forward comment="defconf: accept in ipsec policy" \
ipsec-policy=in,ipsec
add action=accept chain=forward comment="defconf: accept out ipsec policy" \
ipsec-policy=out,ipsec
add action=fasttrack-connection chain=forward comment="defconf: fasttrack" \
connection-state=established,related
add action=accept chain=forward comment="defconf: accept established,related" \
connection-state=established,related
add action=drop chain=forward comment="defconf: drop invalid" \
connection-state=invalid
add action=drop chain=forward comment="Drop in from WAN not DSTNAT" \
connection-nat-state=!dstnat in-interface-list=WAN
add action=drop chain=forward dst-port=53 in-interface-list=WAN protocol=tcp
add action=drop chain=forward dst-port=53 in-interface-list=WAN protocol=udp
/interface bridge port remove [find interface="ether5"]
/interface list member add interface=ether5 list=LAN
/ip address
add address=192.168.188.1/24 comment=pinet interface=ether5 network=192.168.188.0
/ip pool
add name=pinet ranges=192.168.188.20-192.168.188.40
/ip dhcp-server
add address-pool=pinet disabled=no interface=ether5 name=pinetDHCP
/ip dhcp-server network
add address=192.168.188.0/24 comment=pinet gateway=192.168.188.1 netmask=24
add address=192.168.188.25/32 comment=route dns-server=192.168.88.1 gateway=192.168.88.1 netmask=24
/ip firewall address-list
add address=192.168.188.20-192.168.188.40 list=allowed_to_router
/ip firewall nat
add action=dst-nat chain=dstnat dst-address=192.168.188.28 dst-port=53 protocol=tcp to-address=192.168.88.1 comment=pihole_bypass disabled=yes
add action=dst-nat chain=dstnat dst-address=192.168.188.28 dst-port=53 protocol=udp to-address=192.168.88.1 comment=pihole_bypass disabled=yes
/ip firewall filter
add action=accept chain=forward dst-address=192.168.188.0/24 src-address=\
192.168.88.0/24
add action=accept chain=forward dst-address=192.168.88.0/24 src-address=\
192.168.188.0/24
/ip upnp interfaces
add interface=ether5 type=internal
:local piholeDown [/ip firewall nat print count-only where comment~"pihole_bypass" && disabled]
:local piholeDNS "192.168.188.25"
:local testDomain "www.google.com"
:if ($piholeDown > 0 ) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip firewall nat enable [find comment=pihole_bypass];
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip firewall nat disable [find comment=pihole_bypass];
} on-error={}
}
/ip dns
set allow-remote-requests=yes query-server-timeout=100ms query-total-timeout=5s
add servers=1.1.1.1,1.0.0.1
set use-doh-server=https://cloudflare-dns.com/dns-query verify-doh-cert=yes
/ip dns static
add address=104.16.248.249 name=cloudflare-dns.com type=A
add address=104.16.249.249 name=cloudflare-dns.com type=A
/ip route
add dst-address=0.0.0.0/0 gateway=pppoe-out1 routing-mark=to_ISP1 check-gateway=ping
add dst-address=104.16.248.249 gateway=pppoe-out1 scope=10
add dst-address=104.16.249.249 gateway=pppoe-out1 scope=10
add distance=1 gateway=104.16.248.249 routing-mark=to_ISP1 check-gateway=ping
add distance=2 gateway=104.16.249.249 routing-mark=to_ISP1 check-gateway=ping