Iptables: How do I block TCP from port 53?

Post new topic   Reply to topic    DD-WRT Forum Index -> Advanced Networking
Author Message
danielwritesback
DD-WRT User


Joined: 29 Aug 2011
Posts: 240

PostPosted: Fri Mar 13, 2020 17:38    Post subject: Iptables: How do I block TCP from port 53? Reply with quote
I'd like to make sure that my DNS uses only UDP (no TCP on 53).

What is the relevant iptables command for that?
Sponsor
Per Yngve Berg
DD-WRT Guru


Joined: 13 Aug 2013
Posts: 6868
Location: Romerike, Norway

PostPosted: Fri Mar 13, 2020 17:56    Post subject: Reply with quote
iptables -I OUTPUT -p tcp -port 53 -j REJECT

Blocks a DNS server running on the router.
danielwritesback
DD-WRT User


Joined: 29 Aug 2011
Posts: 240

PostPosted: Fri Mar 13, 2020 18:42    Post subject: Reply with quote
Per Yngve Berg wrote:
iptables -I OUTPUT -p tcp -port 53 -j REJECT
Results: "iptables v1.3.7: multiple -p flags not allowed"

So, how to catch both the dnsmasq and also clients that don't use the router-specified dns?
Wildlion
DD-WRT Guru


Joined: 24 May 2016
Posts: 1415

PostPosted: Fri Mar 13, 2020 19:17    Post subject: Reply with quote
Code:

iptables -I INPUT 1 -p tcp -m tcp --dport 53 -j REJECT
iptables -I FORWARD 1 -p tcp -m tcp --dport 53 -j REJECT
iptables -I OUTPUT 1 -p tcp -m tcp --dport 53 -j REJECT


This will reject all of the port 53 tcp dns connections... I am putting it as the very first rule as well

INPUT -- is for connections TO the router
FORWARD -- is for connections THROUGH the router
OUTPUT -- is for connections FROM the router
danielwritesback
DD-WRT User


Joined: 29 Aug 2011
Posts: 240

PostPosted: Sun Mar 15, 2020 22:52    Post subject: Reply with quote
THANKS!
Wildlion wrote:
iptables -I INPUT 1 -p tcp -m tcp --dport 53 -j REJECT
is non-stealth, responds as closed. https://www.grc.com/shieldsup
It should use DROP.

Would this do?
iptables -A INPUT -p tcp --dport 53 -j DROP


Wildlion wrote:
iptables -I FORWARD 1 -p tcp -m tcp --dport 53 -j REJECT
iptables -I OUTPUT 1 -p tcp -m tcp --dport 53 -j REJECT
These seem fine, but I had questions:
Do they need to be the first rule?
Is the -m tcp needed with the protocol already specified as TCP?


Wildlion wrote:
INPUT -- is for connections TO the router
FORWARD -- is for connections THROUGH the router
OUTPUT -- is for connections FROM the router

What I need to do is narrow the scope of DNS from commercial-size overdo, down to what is appropriate for home use--DNS without TCP, is the correct scope for home use. So, which command(s) should be used for that?
Alozaros
DD-WRT Guru


Joined: 16 Nov 2015
Posts: 6440
Location: UK, London, just across the river..

PostPosted: Mon Mar 16, 2020 0:33    Post subject: Reply with quote
danielwritesback wrote:

What I need to do is narrow the scope of DNS from commercial-size overdo, down to what is appropriate for home use--DNS without TCP, is the correct scope for home use. So, which command(s) should be used for that?


for normal DNS operation both ports must be used although port 53 UDP is not widely supported...if you want to increase DNS security use either stubby or dnscrypt, if your router supports it...
stubby works on everything that has USB check my signature...you dont have to add those iptables rules to increase DNS security.....

for normal DNSmasq use, add those lines to advanced Dnsmasq rules

no-resolv
server=xxx.xxx.xxx.xxx

replace xxx with your dns server preferred

_________________
Atheros
TP-Link WR740Nv1 ---DD-WRT 55630 WAP
TP-Link WR1043NDv2 -DD-WRT 55723 Gateway/DoT,Forced DNS,Ad-Block,Firewall,x4VLAN,VPN
TP-Link WR1043NDv2 -Gargoyle OS 1.15.x AP,DNS,QoS,Quotas
Qualcomm-Atheros
Netgear XR500 --DD-WRT 55779 Gateway/DoH,Forced DNS,AP Isolation,4VLAN,Ad-Block,Firewall,Vanilla
Netgear R7800 --DD-WRT 55819 Gateway/DoT,AD-Block,Forced DNS,AP&Net Isolation,x3VLAN,Firewall,Vanilla
Netgear R9000 --DD-WRT 55779 Gateway/DoT,AD-Block,AP Isolation,Firewall,Forced DNS,x2VLAN,Vanilla
Broadcom
Netgear R7000 --DD-WRT 55460 Gateway/SmartDNS/DoH,AD-Block,Firewall,Forced DNS,x3VLAN,VPN
NOT USING 5Ghz ANYWHERE
------------------------------------------------------
Stubby DNS over TLS I DNSCrypt v2 by mac913
SurprisedItWorks
DD-WRT Guru


Joined: 04 Aug 2018
Posts: 1447
Location: Appalachian mountains, USA

PostPosted: Mon Mar 16, 2020 16:14    Post subject: Reply with quote
Per Yngve Berg wrote:
iptables -I OUTPUT -p tcp -port 53 -j REJECT

Blocks a DNS server running on the router.

There was a minor typo there... I'm sure Per meant this:

iptables -I OUTPUT -p tcp -dport 53 -j REJECT

But I agree with Alozaros that if this is about security, DNSCrypt is simpler and more secure, particularly if you are on a build that offers Encrypt DNS as an option on the GUI>Services>Services page. If that option is absent or if you want to use a DNSCrypt DNS provider not in the menu, see my post at https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=318094&start=6.

Do note that many DNSCrypt providers have moved on to a newer DNSCrypt protocol that the dd-wrt built-in dnscrypt-proxy code does not support. But I'm using the build-in version successfully with Quad9 DNS and Adguard DNS still, just as detailed in that post.

Using entware to install an upgraded dnscrypt-proxy to handle the new protocol is also an option. See thread https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320764.

_________________
2x Netgear XR500 and 3x Linksys WRT1900ACSv2 on 53544: VLANs, VAPs, NAS, station mode, OpenVPN client (AirVPN), wireguard server (AirVPN port forward) and clients (AzireVPN, AirVPN, private), 3 DNSCrypt providers via VPN.
tinkeruntilitworks
Guest





PostPosted: Mon Mar 16, 2020 16:25    Post subject: Reply with quote
if your router has unbound you can turn tcp off
Alozaros
DD-WRT Guru


Joined: 16 Nov 2015
Posts: 6440
Location: UK, London, just across the river..

PostPosted: Mon Mar 16, 2020 17:47    Post subject: Reply with quote
tinkeruntilitworks wrote:
if your router has unbound you can turn tcp off


DDWRT build in Unbound is not that versatile as the full Unbound installation via Entware....it does just recursive resolving and that's all...

Among all the 3 options for encrypting DNS mentioned above
Unbound is more picky and hard to set up for beginers...

while Stubby for DoT is the easiest and works even out of the box...

_________________
Atheros
TP-Link WR740Nv1 ---DD-WRT 55630 WAP
TP-Link WR1043NDv2 -DD-WRT 55723 Gateway/DoT,Forced DNS,Ad-Block,Firewall,x4VLAN,VPN
TP-Link WR1043NDv2 -Gargoyle OS 1.15.x AP,DNS,QoS,Quotas
Qualcomm-Atheros
Netgear XR500 --DD-WRT 55779 Gateway/DoH,Forced DNS,AP Isolation,4VLAN,Ad-Block,Firewall,Vanilla
Netgear R7800 --DD-WRT 55819 Gateway/DoT,AD-Block,Forced DNS,AP&Net Isolation,x3VLAN,Firewall,Vanilla
Netgear R9000 --DD-WRT 55779 Gateway/DoT,AD-Block,AP Isolation,Firewall,Forced DNS,x2VLAN,Vanilla
Broadcom
Netgear R7000 --DD-WRT 55460 Gateway/SmartDNS/DoH,AD-Block,Firewall,Forced DNS,x3VLAN,VPN
NOT USING 5Ghz ANYWHERE
------------------------------------------------------
Stubby DNS over TLS I DNSCrypt v2 by mac913
danielwritesback
DD-WRT User


Joined: 29 Aug 2011
Posts: 240

PostPosted: Mon Mar 16, 2020 19:29    Post subject: Reply with quote
Wildlion wrote:
Code:
iptables -I OUTPUT 1 -p tcp -m tcp --dport 53 -j REJECT
That command blocks an average of 4 'unnecessary' things per hour (according to iptables -nvL).
THANKS!
SurprisedItWorks wrote:
Code:
iptables -I OUTPUT -p tcp -dport 53  -j REJECT
THANKS!
Alozaros wrote:
...while Stubby for DoT is the easiest and works even out of the box
Does that have a fallback to ordinary DNS, just in case?
tinkeruntilitworks
Guest





PostPosted: Mon Mar 16, 2020 20:24    Post subject: Reply with quote
delete

Last edited by tinkeruntilitworks on Tue Apr 28, 2020 23:10; edited 1 time in total
Alozaros
DD-WRT Guru


Joined: 16 Nov 2015
Posts: 6440
Location: UK, London, just across the river..

PostPosted: Mon Mar 16, 2020 20:50    Post subject: Reply with quote
Alozaros wrote:
...while Stubby for DoT is the easiest and works even out of the box


danielwritesback wrote:
Does that have a fallback to ordinary DNS, just in case?


it does have a fallback DNS, in fact you can add as many as you want, you have to be careful and keep the initial format/syntax correct....

nope its not and ordinary DNS, its a stub recursive resolver..

much better and secure than ordinary DNS...and this is its purpose to encrypt DNS with TLS encryption...

_________________
Atheros
TP-Link WR740Nv1 ---DD-WRT 55630 WAP
TP-Link WR1043NDv2 -DD-WRT 55723 Gateway/DoT,Forced DNS,Ad-Block,Firewall,x4VLAN,VPN
TP-Link WR1043NDv2 -Gargoyle OS 1.15.x AP,DNS,QoS,Quotas
Qualcomm-Atheros
Netgear XR500 --DD-WRT 55779 Gateway/DoH,Forced DNS,AP Isolation,4VLAN,Ad-Block,Firewall,Vanilla
Netgear R7800 --DD-WRT 55819 Gateway/DoT,AD-Block,Forced DNS,AP&Net Isolation,x3VLAN,Firewall,Vanilla
Netgear R9000 --DD-WRT 55779 Gateway/DoT,AD-Block,AP Isolation,Firewall,Forced DNS,x2VLAN,Vanilla
Broadcom
Netgear R7000 --DD-WRT 55460 Gateway/SmartDNS/DoH,AD-Block,Firewall,Forced DNS,x3VLAN,VPN
NOT USING 5Ghz ANYWHERE
------------------------------------------------------
Stubby DNS over TLS I DNSCrypt v2 by mac913
Wildlion
DD-WRT Guru


Joined: 24 May 2016
Posts: 1415

PostPosted: Mon Mar 16, 2020 22:40    Post subject: Reply with quote
danielwritesback wrote:
THANKS!
Wildlion wrote:
iptables -I INPUT 1 -p tcp -m tcp --dport 53 -j REJECT
is non-stealth, responds as closed. https://www.grc.com/shieldsup
It should use DROP.

Would this do?
iptables -A INPUT -p tcp --dport 53 -j DROP


Wildlion wrote:
iptables -I FORWARD 1 -p tcp -m tcp --dport 53 -j REJECT
iptables -I OUTPUT 1 -p tcp -m tcp --dport 53 -j REJECT
These seem fine, but I had questions:
Do they need to be the first rule?
Is the -m tcp needed with the protocol already specified as TCP?


Wildlion wrote:
INPUT -- is for connections TO the router
FORWARD -- is for connections THROUGH the router
OUTPUT -- is for connections FROM the router

What I need to do is narrow the scope of DNS from commercial-size overdo, down to what is appropriate for home use--DNS without TCP, is the correct scope for home use. So, which command(s) should be used for that?


Yes DROP would be correct if you wanted full stealth or you put it as a later rule. I just did the first rule to ensure that it worked for you. The -m tcp is probably not needed I think i was just typing and it does not hurt.

For the scope, that is up to you, most systems never use tcp dns, but either shut it down on the router or block the ports.
danielwritesback
DD-WRT User


Joined: 29 Aug 2011
Posts: 240

PostPosted: Wed Mar 18, 2020 19:58    Post subject: Reply with quote
Wildlion wrote:
...I just did the first rule to ensure that it worked for you. The -m tcp is probably not needed I think i was just typing and it does not hurt.
For the scope, that is up to you, most systems never use tcp dns, but either shut it down on the router or block the ports.

Awesome. I am grateful for your help. That is effective.
It blocks command&control and binary-schlepping, which never were in scope for DNS in a house.

Bonus: Seems faster.
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> Advanced Networking All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum