My friends,
I’m trying to configure iptables on my Docker Server with Debian Bookworm and kernel 6.12.
I’ve been studying a lot and I’ve “worked a lot” with AI, because I didn’t even know how to work with iptables, but it’s difficult and I needed to go a little further. That’s why I’m asking for your help on a very basic matter.
I want to block full access to the server with the following exceptions:
- General access to ports 53 (DNS: UDP and TCP), 80 (http: TCP), 443 (https: TCP);
- Access to port 22 (SSH) via local IP and loopback;
- Access to the loopback;
- Two-way communications:
- Access to a specific port by a specific PC from the local IP (TCP and UDP).
Note: the port number “22” for SSH is just an example because I’m using another port as a security measure
This is the configuration I created:
#!/usr/bin/bash
#Script to configure server iptables
# Flush existing rules:
#iptables -F
#iptables -X
# Set default rules:
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# Allow port 53 (DNS: UDP and TCP):
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
# Allow port 80 and 443 (TCP):
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# Allow port 22 (SSH: TCP) only from localhost and local IPs:
iptables -A INPUT -p tcp --dport 22 -s 127.0.0.1 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -s 192.168.0.0/24 -j ACCEPT
# Allow loopback interface:
iptables -A INPUT -i lo -j ACCEPT
# Allow established and related connections:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allows access to a specific port of a specific PC from the local IP (TCP and UDP):
iptables -A INPUT -s 192.168.0.20 -p tcp --dport 10000 -j ACCEPT
iptables -A INPUT -s 192.168.0.20 -p udp --dport 10000 -j ACCEPT
# Save the rules:
netfilter-persistent save
# List the rules:
iptables -nvL
This is the list of rules that have already been implemented:
Chain INPUT (policy DROP 39 packets, 8272 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT 0 -- lo * 0.0.0.0/0 0.0.0.0/0
735 57814 ACCEPT 0 -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT 17 -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:53
0 0 ACCEPT 6 -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
0 0 ACCEPT 6 -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
0 0 ACCEPT 6 -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
0 0 ACCEPT 6 -- * * 127.0.0.1 0.0.0.0/0 tcp dpt:22
1 88 ACCEPT 6 -- * * 192.168.0.0/24 0.0.0.0/0 tcp dpt:22
0 0 ACCEPT 6 -- * * 192.168.0.20 0.0.0.0/0 tcp dpt:10000
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 715 packets, 113K bytes)
pkts bytes target prot opt in out source destination
For now, my initial doubt is whether what I've done is really right
I’m not sure if I should use conntrack
in connections. From everything I’ve read, it’s recommended for those who use Docker. Do you think otherwise?
I know that the rules I’ve made can be an aberration for those who work with iptables.
What would you change or how would you do it if it were for you?
Thank you in advance for any help you can give me
Jorge