LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 11-12-2008, 03:37 AM   #1
mr51m0n
LQ Newbie
 
Registered: Nov 2008
Posts: 17

Rep: Reputation: 0
iptables & FTP


Hello

I want to have a firewall on my server, thats why i mad a iptables script. It seems that everything runs fine, except FTP. When i want to connect to my server via FTP after applying the iptables rules, this happens:

Status: Resolving address of www.domain.ch
Status: Connecting to 123.123.123.123:21...
Status: Connection established, waiting for welcome message...
Response: 220 FTP Server Ready
Command: USER simon@domain.ch
Response: 331 Password required for simon@domain.ch.
Command: PASS ******
Response: 230 User simon@domain.ch logged in.
Status: Connected
Status: Retrieving directory listing...
Command: PWD
Response: 257 "/" is current directory.
Command: TYPE I
Response: 200 Type set to I
Command: PASV
Response: 227 Entering Passive Mode (123,123,123,123,153,158).
Command: LIST
Error: Connection timed out
Error: Failed to retrieve directory listing

my iptables look like this:

[...]

# FTP out (active & passive)
iptables -A OUTPUT -p tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 21 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 1024: --dport 1024: -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 1024: --dport 1024: -m state --state ESTABLISHED,RELATED -j ACCEPT

# FTP in (active & passive)
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 OUTPUT -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 1024: --dport 1024: -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 1024: --dport 1024: -m state --state ESTABLISHED -j ACCEPT

[...]


what else is needed?

thanks, simon
 
Old 11-12-2008, 11:21 AM   #2
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
You have the FTP connection tracking module loaded? Check with:
Code:
lsmod | grep ftp
Load it with:
Code:
modprobe ip_conntrack_ftp

Last edited by win32sux; 11-12-2008 at 11:22 AM.
 
Old 11-13-2008, 10:00 AM   #3
mr51m0n
LQ Newbie
 
Registered: Nov 2008
Posts: 17

Original Poster
Rep: Reputation: 0
Found the problem

Hello, the problem was: the client was not able to change the port, because NEW was missing in two rules:

# FTP out (active & passive)
iptables -A OUTPUT -p tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 21 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 1024: --dport 1024: -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 1024: --dport 1024: -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

# FTP in (active & passive)
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 OUTPUT -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 1024: --dport 1024: -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 1024: --dport 1024: -m state --state ESTABLISHED -j ACCEPT


Everithing fine now thank you
 
Old 11-13-2008, 01:51 PM   #4
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by mr51m0n View Post
Hello, the problem was: the client was not able to change the port, because NEW was missing in two rules:

# FTP out (active & passive)
iptables -A OUTPUT -p tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 21 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 1024: --dport 1024: -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 1024: --dport 1024: -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

# FTP in (active & passive)
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 OUTPUT -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 1024: --dport 1024: -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 1024: --dport 1024: -m state --state ESTABLISHED -j ACCEPT
Even though these rules work, they are at best totally unnecessary, and at worst completely insane. Why are you going through all this trouble? For FTP (regardless of whether it is passive or active) you don't need to make rules for packets in state NEW to ports other than 21. Everything else will be picked-up by the FTP connection tracking. With the rules you currently have you might as well just disable the firewall completely as you are allowing all packets to enter and exit, with the only requirement being that they have a high destination port. This kind of stuff should be unheard of today (it was the norm WAY back in the ipchains days). You really should get rid of all those dangerous rules.

This example provides the bare essentials needed to serve FTP (both active and passive):
Code:
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p TCP --dport 21 -m state --state NEW -j ACCEPT
Notice how there is no need to match state NEW in the OUTPUT chain.

Last edited by win32sux; 11-13-2008 at 08:10 PM. Reason: Added example.
 
Old 11-14-2008, 02:55 AM   #5
mr51m0n
LQ Newbie
 
Registered: Nov 2008
Posts: 17

Original Poster
Rep: Reputation: 0
Heh, looks way better, but it doesn't work for me, same problem as before.

My server says this:

Nov 14 09:36:00 server kernel: IN=eth0 OUT= MAC=00:19:db:f9:74:59:00:02:85:18:ac:a0:08:00 SRC=131.152.195.21 DST=123.123.123.123 LEN=60 TOS=0x00 PREC=0x00 TTL=53 ID=60470 DF PROTO=TCP SPT=46228 DPT=44227 WINDOW=5840 RES=0x00 SYN URGP=0

But i have the RELATED,ESTABLISHED rule!?!
 
Old 11-14-2008, 10:52 AM   #6
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by mr51m0n View Post
Heh, looks way better, but it doesn't work for me, same problem as before.

My server says this:

Nov 14 09:36:00 server kernel: IN=eth0 OUT= MAC=00:19:db:f9:74:59:00:02:85:18:ac:a0:08:00 SRC=131.152.195.21 DST=123.123.123.123 LEN=60 TOS=0x00 PREC=0x00 TTL=53 ID=60470 DF PROTO=TCP SPT=46228 DPT=44227 WINDOW=5840 RES=0x00 SYN URGP=0

But i have the RELATED,ESTABLISHED rule!?!
See post #2.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
FTP & iptables firewall hct224 Linux - Newbie 9 05-04-2012 01:43 PM
ftp and pop&smtp with squid proxy and iptables ssilayaraja Linux - Networking 3 04-06-2008 05:32 PM
[SOLVED] Upload Ftp & IpTables. AleLinuxBSD Linux - Security 11 03-30-2007 02:53 AM
iptables masquerading & active ftp connections PowerMatt Linux - Networking 2 10-20-2005 05:02 PM
iptables & inbound ftp, ssh bddwyer Linux - Networking 3 02-06-2003 09:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 10:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration