Linux - SecurityThis forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I am trying to build Iptables based firewall to limit allowed outgoing traffic only to specified hostnames. Unfortunately for some reason all outgoing traffic to any hostname is allowed. What is wrong here?
Code:
#!/bin/sh
set -x
iptables -F
# Allow unlimited traffic on the loopback interface
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# Set default policies
iptables --policy INPUT DROP
iptables --policy OUTPUT DROP
iptables --policy FORWARD DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
# Allow outgoing DNS query
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
# Allow incoming
iptables -A INPUT -p tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 21 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --dport 873 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 873 -m state --state ESTABLISHED -j ACCEPT
# Allow outgoing
iptables -A OUTPUT -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p udp -d 0.pool.ntp.org --dport 123 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p udp -d 0.pool.ntp.org --sport 123 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p udp -d 1.pool.ntp.org --dport 123 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p udp -d 1.pool.ntp.org --sport 123 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -d hostname1 --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -d hostname1 --sport 443 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -d hostname2 --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -d hostname2 --sport 443 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -d hostname3 --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -d hostname3 --sport 443 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -d hostname4 --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -d hostname4 --sport 443 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -d hostname5 --dport 8883 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -d hostname5 --sport 8883 -m state --state ESTABLISHED -j ACCEPT
# Drop all other traffic
iptables -A INPUT -j DROP
iptables -A OUTPUT -j DROP
HI. It's true about domains in iptables. And second, when you ask to a server Web (80 or 443) it doesn't have to answer trough same ports (80 or 443) it's more probably that answer any ramdom port(1024-65xxx)
HI. It's true about domains in iptables. And second, when you ask to a server Web (80 or 443) it doesn't have to answer trough same ports (80 or 443) it's more probably that answer any ramdom port(1024-65xxx)
This is wrong. The server has to answer on the port that the request came in on, it cannot randomly switch to another port as the client will not accept the response.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.