Here's my iptables script:
#!/bin/sh
service iptables stop
iptables -F
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp -m multiport --dports 22,80,443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p udp --sport 53 --dport 1024:65535 -j ACCEPT
service iptables save
service iptables restart
It works fine on Centos 6.3, but on Centos 6.0 I can't establish outbound HTTP connections. (I probably can't establish any outbound TCP/IP connections.) DNS lookup, however, works just fine, as does apparently anything else based on UDP.
I'm guessing this has something to do with TCP's three-way handshake, of which I know almost nothing. That being the case, there must be a difference in the versions of one of the modules where the earlier version requires explicit specification of INPUT
rules to allow the handshake. So, what rule(s) would I need to create to allow the TCP handshake and therefore receive data?
Your system will initiate connections from ephemeral ports.
Generally speaking, don't try and be smarter than netfilter's conntrack.
the first rule in any decent ruleset should be
iptables -m conntrack --ctstate ESTABLISHED -j ACCEPT
(only use-m state
if your distro lacks-m conntrack
) - doing this will enable your system initiate outbound connections and successfully receive the replies.Additionally, to save resources,
iptables -t raw -A PREROUTING -i lo -j CT --notrack
(or-j NOTRACK
) andiptables -t raw -A OUTPUT -o lo -j CT --notrack
along with appropriate rules in the INPUT and OUTPUT chains to allow--ctstate UNTRACKED
will save unnecessarily consuming conntrack resources on loopback connections.This works (see comments):
I tested this quite thoroughly with netcat.