LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 04-07-2024, 03:14 PM   #1
princessgentoo
LQ Newbie
 
Registered: Apr 2024
Location: Mexico
Distribution: Debian, Gentoo, Ubuntu
Posts: 4

Rep: Reputation: 0
Help with iptables for a DNS server.


Hello everyone, I hope all of you are having a great day!
I have a dnsmasq server open to public internet in a VPS because I like to make custom domain modifications there. But many ips connected to my server is reducing my network speed. That is why I want' to limit the use of my dnsmasq server to specific Ips. Since i use many vpns, specifying ips will filter my traffic neatly.
So I added some rules to the INPUT openvpn ip rules I used for my VPN to block all traffic except SSH, VPN and port 53.

In the code I replaced some ips for privacy, IP1 is the ip of my other VPN (which is not the current server), IP2 is the Ip of a server. And SERVERIP is the current server IP. I'm using Ubuntu Jammy Jellyfish with the DNSMasq.

I used some code I found on internet, because by simple logic, if "/usr/sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT" it just working for SSH, something similar must work for DNS just replacing the port. But then tried another code that also included sport and state. I added the 8.8.8.8 which is the Google DNS nameserver, i don't think it goes here but it won't harm the rule adding it.

Code:
[Unit]
Before=network.target
[Service]
Type=oneshot
ExecStart=/usr/sbin/iptables -t nat -A POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to SERVERIP
ExecStart=/usr/sbin/iptables -P INPUT DROP
ExecStart=/usr/sbin/iptables -A INPUT -p udp --dport 1194 -j ACCEPT
ExecStart=/usr/sbin/iptables -A INPUT -p tcp --dport 22  -j ACCEPT
ExectStart=/usr/sbin/iptables -A INPUT -p udp --dport 53 -s IP1,IP2,8.8.8.8,8.8.4.4 --sport 1024:65535 -d SERVERIP -m state --state NEW,ESTABLISHED -j ACCEPT
ExectStart=/usr/sbin/iptables -A INPUT -p udp --dport 53 -s IP1,IP2,8.8.8.8,8.8.4.4 --sport 53 -d SERVERIP -m state --state NEW,ESTABLISHED -j ACCEPT

ExecStart=/usr/sbin/iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT
ExecStart=/usr/sbin/iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
ExecStop=/usr/sbin/iptables -t nat -D POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to 212.227.145.229
ExecStop=/usr/sbin/iptables -D INPUT -p udp --dport 1194 -j ACCEPT
ExecStop=/usr/sbin/iptables -D FORWARD -s 10.8.0.0/24 -j ACCEPT
ExecStop=/usr/sbin/iptables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
When I run dig in both the server and the computer connected to VPN IP1.
Code:
dig @SERVERIP google.com
I get:
Code:
;; communications error to SERVERIP#53: timed out
;; communications error to SERVERIP#53: timed out
;; communications error to SERVERIP#53: timed out
The default policies on iptables is accepting except for INPUT which the default policy is Drop.

What I'm doing wrong?
 
Old 04-08-2024, 07:21 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,341

Rep: Reputation: Disabled
Not exactly sure what's wrong with your setup (other than the misspelling of "ExecStart" in two places), as you seem to have gotten the port numbers correct. But your ruleset is very strangely ordered and lacks at least one vital component, which may or may not affect the operations of the DNS server:
  • You absolutely need a loopback interface rule in the INPUT chain, as many processes communicate using lo.

  • You should add general rules at the top to allow all ESTABLISHED and RELATED traffic in the INPUT and FORWARD chains respectively. Think about it: For traffic to match these criteria, there would have to have been initial packets that were allowed by other rules.

My suggestions:
Code:
[Unit]
Before=network.target
[Service]
Type=oneshot
ExecStart=/usr/sbin/iptables -F INPUT
ExecStart=/usr/sbin/iptables -A INPUT -i lo -j ACCEPT
ExecStart=/usr/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
ExecStart=/usr/sbin/iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
ExecStart=/usr/sbin/iptables -A INPUT -p udp --dport 53 -s IP1,IP2,8.8.8.8,8.8.4.4 --sport 53 -d SERVERIP -m state --state NEW -j ACCEPT
ExecStart=/usr/sbin/iptables -A INPUT -p udp --dport 53 -s IP1,IP2,8.8.8.8,8.8.4.4 --sport 1024:65535 -d SERVERIP -m state --state NEW -j ACCEPT
ExecStart=/usr/sbin/iptables -A INPUT -p udp --dport 1194 -m state --state NEW -j ACCEPT
ExecStart=/usr/sbin/iptables -P INPUT DROP
ExecStart=/usr/sbin/iptables -P OUTPUT ACCEPT
ExecStart=/usr/sbin/iptables -F FORWARD
ExecStart=/usr/sbin/iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
ExecStart=/usr/sbin/iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT
ExecStart=/usr/sbin/iptables -P FORWARD DROP
ExecStart=/usr/sbin/iptables -t nat -A POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to SERVERIP

ExecStop=/usr/sbin/iptables -t nat -D POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to 212.227.145.229
ExecStop=/usr/sbin/iptables -D FORWARD -s 10.8.0.0/24 -j ACCEPT
ExecStop=/usr/sbin/iptables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
I added a DROP policy for the FORWARD chain and an ACCEPT policy for the OUTPUT chain, as well as commands to flush the existing ruleset. You may also want to add an interface condition to the FORWARD rule, as your existing ruleset contains a theoretical vulnerability to IP spoofing. I say theoretical, as most (if not all) ISPs will typically drop packets with RFC 1918 source addresses, but I wouldn't want to rely on that.

Also, bear in mind that the server will return an error if the DNS reply doesn't fit inside a single UDP packet, and the client will then switch to TCP/53 which your current ruleset doesn't allow.

Last edited by Ser Olmy; 04-10-2024 at 10:02 PM.
 
Old 04-10-2024, 12:34 AM   #3
princessgentoo
LQ Newbie
 
Registered: Apr 2024
Location: Mexico
Distribution: Debian, Gentoo, Ubuntu
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Ser Olmy View Post
Not exactly sure what's wrong with your setup (other than the misspelling of "ExecStart" in two places), as you seem to have gotten the port numbers correct. But your ruleset is very strangely ordered and lacks at least one vital component, which may or may not affect the operations of the DNS server:
  • You absolutely need a loopback interface rule in the INPUT chain, as many processes communicate using lo.

  • You should add general rules at the top to allow all ESTABLISHED and RELATED traffic in the INPUT and FORWARD chains respectively. Think about it: For traffic to match these criteria, there would have to have been initial packets that were allowed by other rules.

My suggestions:
Code:
[Unit]
Before=network.target
[Service]
Type=oneshot
ExecStart=/usr/sbin/iptables -F INPUT
ExecStart=/usr/sbin/iptables -A INPUT -i lo -j ACCEPT
ExecStart=/usr/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
ExecStart=/usr/sbin/iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
ExecStart=/usr/sbin/iptables -A INPUT -p udp --dport 53 -s IP1,IP2,8.8.8.8,8.8.4.4 --sport 53 -d SERVERIP -m state --state NEW -j ACCEPT
ExecStart=/usr/sbin/iptables -A INPUT -p udp --dport 53 -s IP1,IP2,8.8.8.8,8.8.4.4 --sport 1024:65535 -d SERVERIP -m state --state NEW -j ACCEPT
ExecStart=/usr/sbin/iptables -A INPUT -p udp --dport 1194 -m state --state NEW -j ACCEPT
ExecStart=/usr/sbin/iptables -P INPUT DROP
ExecStart=/usr/sbin/iptables -P OUTPUT ACCEPT
ExecStart=/usr/sbin/iptables -I FORWARD -F
ExecStart=/usr/sbin/iptables -I FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
ExecStart=/usr/sbin/iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT
ExecStart=/usr/sbin/iptables -P FORWARD DROP
ExecStart=/usr/sbin/iptables -t nat -A POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to SERVERIP

ExecStop=/usr/sbin/iptables -t nat -D POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to 212.227.145.229
ExecStop=/usr/sbin/iptables -D FORWARD -s 10.8.0.0/24 -j ACCEPT
ExecStop=/usr/sbin/iptables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
I added a DROP policy for the FORWARD chain and an ACCEPT policy for the OUTPUT chain, as well as commands to flush the existing ruleset. You may also want to add an interface condition to the FORWARD rule, as your existing ruleset contains a theoretical vulnerability to IP spoofing. I say theoretical, as most (if not all) ISPs will typically drop packets with RFC 1918 source addresses, but I wouldn't want to rely on that.

Also, bear in mind that the server will return an error if the DNS reply doesn't fit inside a single UDP packet, and the client will then switch to TCP/53 which your current ruleset doesn't allow.
thank you a lot.
The first thing I did was to correct the ExecStart entries I had wrong, and moved the lines like your suggested code, and added the rule for lo interface as well, it was very important the Accept policy in Output. Everything is well with your code, but then I had issues and solved adding this additional line before the Input drop:
Code:
ExecStart=/usr/sbin/iptables -I  INPUT  -i ens6 -m state --state RELATED,ESTABLISHED -j ACCEPT
The issue I was having I wasn't able to connect to websites or query dns from the server itself, therefore, also the DNS service was failing too, however the VPN server was working perfect.

Last edited by princessgentoo; 04-10-2024 at 12:36 AM.
 
  


Reply

Tags
dnsmasq, iptables



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Doggo Is A DNS Lookup Utility With Colorful Output, DNS-overTLS and DNS-over-HTTPS Support LXer Syndicated Linux News 0 04-07-2021 02:30 AM
Help Setting Dynamic DNS Server as DNS Server des_a Linux - Server 4 02-18-2019 06:05 PM
help needed to setup a DNS server can anyone say how to configure a DNS server subha Linux - Networking 4 04-27-2012 11:50 PM
Win2k3 DNS + PFsense DNS Forwarder = No internal DNS resolution Panopticon Linux - Networking 1 11-19-2007 09:59 PM
iptables v1.2.9: Unknown arg `/sbin/iptables' Try `iptables -h' or 'iptables --help' Niceman2005 Linux - Security 4 12-29-2005 08:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 03:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration