LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   iptables rules for inbound and outbound FTP connections (https://www.linuxquestions.org/questions/linux-networking-3/iptables-rules-for-inbound-and-outbound-ftp-connections-4175438187/)

superkikim 11-21-2012 04:16 PM

iptables rules for inbound and outbound FTP connections
 
Hi,

I know iptables is a lot discussed here around, but I couldn't quite find the global answer to my problem yet.

First:

Can anyone explain to me how does FTP request get out from FTP client, and get in to FTP Server, and then how they get back out from server back in to client ? On what ports and why ?

Second:

That explained will help a lot I'm sure. Because to define the correct rules, it seems essential to understand how the protocol works.

These are the rules I set for my FTP server, and for this server to get out with its ftp client for backup purpose:

Let's say this rules are on SERVER A, and my backup server is SERVER B:
Code:

# Allow incoming FTP connections
iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d $SERVER_IP --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -s $SERVER_IP --sport 21 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d $SERVER_IP --dport 1024:65535 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp -s $SERVER_IP --sport 1024:65535 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

iptables -A OUTPUT -p tcp -s $SERVER_IP --sport 20 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d $SERVER_IP --dport 20 -m state --state ESTABLISHED -j ACCEPT

# Allow outgoing FTP connections
iptables -A OUTPUT -p tcp -s $SERVER_IP --dport 21 -d 0/0 --sport 1024:65535 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -s 0/0 --dport 1024:65535 -d $SERVER_IP --sport 21 -m state --state ESTABLISHED -j ACCEPT

iptables -A OUTPUT -p tcp -s $SERVER_IP --dport 20 -d 0/0 --sport 1024:65535 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -s 0/0 --dport 1024:65535 -d $SERVER_IP --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT

My problem is that I can connect from SERVER A to SERVER B with FTP, and I can login successfully.

But then, if I do an "ls" for example, the reply does not get in. If I remove all rules and accept all, it works. Therefore, these rules are wrong somewhere.... But I can't figure out where. If anyone can help ? and most of all, explain, for me to learn.

Note that my default policy is DROP for INPUT, OUTPUT and FORWARD.

acid_kewpie 11-22-2012 09:03 AM

you really want to look at using connection tracking proeprly here, it makes it pretty simple:

http://rackerhacker.com/2007/07/01/a...ough-iptables/


All times are GMT -5. The time now is 06:11 AM.