I have a PC on which I have a FTP server installed. I want to set the iptables rules to allow both active and passive FTP. I've tried the following code that people report is working, but it seems to block all traffic for me (pages won't load anymore etc)
#!/bin/bash
IPT=/sbin/iptables
$IPT -F
$IPT -X
$IPT -t nat -F
$IPT -t nat -X
$IPT -t mangle -F
$IPT -t mangle -X
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
# Setting default filter policy
$IPT -P INPUT DROP
$IPT -P OUTPUT ACCEPT
# Allow FTP connections @ port 21
$IPT -A INPUT -p tcp --sport 21 -m state --state ESTABLISHED -j ACCEPT
$IPT -A OUTPUT -p tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
# Allow Active FTP Connections
$IPT -A INPUT -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A OUTPUT -p tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
# Allow Passive FTP Connections
$IPT -A INPUT -p tcp --sport 1024: --dport 1024: -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A OUTPUT -p tcp --sport 1024: --dport 1024: -m state --state ESTABLISHED,RELATED -j ACCEPT
From Your question I suppose you have some trivial host with common set of apps such as web-browser, mail client, may be telnet and|or ssh-client, may be ftp-client too, may be some IM etc. And having all these apps working, You want additionally allow an FTP server on this host to work both in active and passive modes for clients which will connect. Here are 3 blocks of rules applicable in this case. Block of common rules is the minimalistic set of rules applicable for most client hosts. Next is block of rules for ftp-client, if You have such on Your host. The rules for ftp-client are slightly different from rules for others clients: there are always two connections to enable data transfer: ftp-control (port 21) and ftp-data (port 20 in Active mode or random port in Passive mode). You most probably will never need client rules for Active mode because Passive mode is single choice for NATed networks.
The rules for FTP server are in the last block.
Please check You have ip_conntrack_ftp ( may be named nf_conntrack_ftp ) in the kernel:
If You do not have this kernel module, the 'RELATED' rules will not work and, most probably, separate ftp-data connection will not start while primary ftp-control connection will hang somewhere after 'PORT' command. You still can enforce ftp-data connection in this case, but at the spent of degrading security provided by the tweaked rules. The tweaks are in comments preceeding the rules.
Pro
That code ONLY allows incoming and outgoing FTP connections. It doesn't allow anything else in/out.
Drops all incoming traffic. So if you start with that, you'll want to enable traffic into any other services you have running that you'd like to allow in. .
This rule would allow incoming FTP traffic.
An explanation of what this script is/does is it deletes all of your existing IP Tables chains, then it adds rules to allow all outgoing traffic and block all incoming traffic except for FTP.
Refer this site for Explanation: http://slacksite.com/other/ftp.html
FTP Client:
FTP SERVER:
To toggle between passive and active mode on the client side
The arguments for the INPUT and OUTPUT lines need to be flipped in the # Allow FTP connections @ port 21 section otherwise new (active) FTP connections will be blocked.
I have found a big mistake in the above script!
The rules are misstyped, it should be like that:
Dport and Sport change places! You are going to a destination, if you connect to a server, the sourceport is dynamic and clientside spefific and is not known nevertheless a connection is established!
Imho the second line is ambigious at all, cause you don't know which ports a server-side client is going to use to establish a ftp-connection. Better would be a rule like this, if outbound traffic is blocked by defalut:
But this is only needed if the rule
is on top of the rule-set.
Greetings
Marcus