Iptables firewall port forwarding and ftpnat settings
Source: Internet
Author: User
FTP is characterized by the separation of data flow ports and control flow ports, which requires administrators to consider more factors when setting firewall rules. at the same time, because of the importance and security of the FTP server itself, many administrators want to place it on the firewall machine, which makes it more troublesome. normally, the client can log on to the server, but cannot list the directories. the FTP protocol cannot transmit data. The data flow port is separated from the control flow port, which allows the administrator to set firewall rules, consider more factors. at the same time, because of the importance and security of the FTP server itself, many administrators want to place it on the firewall machine, which makes it more troublesome. it is usually because the client can log on to the server, but the directory cannot be listed, and data cannot be transmitted. This makes it hard for many novice administrators to find out the solution. in fact, this is not as difficult as you think. after reading this article, you will understand it all.
Part 1. understanding FTP active and passive modes
In fact, there have been a lot of explanations on the active/passive mode of FTP transmission on the internet. to smoothly transmit data between the client and the server, you must first understand their transmission principles, in this way, we can remedy the problem in case of problems. we use packet capture analysis to understand the transfer process. active Mode (Port mode)
The data transmission process in active mode is as follows (server ip: 192.168.200.1, client ip: 192.168.200.8 ):
1. 192.168.200.8.55722> 192.168.200.1.ftp: PORT192, 168,200, 8,178,210
2. 192.168.200.1.ftp> 192.168.200.8.55722: 200 PORT commandsuccessful. Consider using PASV.
3. 192.168.200.8.55722> 192.168.200.1.ftp: ...... ACK
4. 192.168.200.8.55722> 192.168.200.1.ftp: LIST
5. 192.168.200.1.ftp-data> 192.168.200.8.45778: ...... SYN
6. 192.168.200.8.45778> 192.168.200.1.ftp-data:... SYN. ACK
7. 192.168.200.1.ftp-data> 192.168.200.8.45778: ...... ACK
8 .. 192.168.200.1.ftp> 192.168.200.8.55722: 150 Here comes thedirectory listing.
9. 192.168.200.1.ftp-data> 192.168.200.8.45778:
-Rw-r -- 10 0 6 Mar 23 08:53 filetest
Drwxr-xr-x 20 0 4096 Dec 13 2007 pub
The transmission process of the active mode obtained from the above data is as follows:
A. (row 1st) the client applies for PORT-mode data transmission from the server and uses the PORT command to inform the server that "my IP address is 192.168.200.8. I will open PORT 45778 for you ".
(45778 is calculated by the last two digits after the PORT command. 178*256 + 210 = 45778)
B. After checking the syntax and security of the received PORT command, the server informs the client that the PORT command is successful. It also recommends that the client consider using PASV.
(PASV is the passive mode we will talk about next)
C. (row 3) the client confirms that it receives the information from the server and responds.
D. (row 3) the client requests the server to list the directory information.
E. (lines 5, 6, and 7) after receiving the request from the client, the server uses its own port 20 to connect to Port 45778 of the client for data transmission.
(Note: This is important. the active mode means that the server actively connects to the client from its own data port)
F. (row 8th) when the data stream port of the server and the client is successfully connected, the server sends a message to the customer through control port 21: The Directory list you need is sent.
G. (row 9th) the server sends the directory data required by the user to the client through the data stream port.
During the entire communication process, the server only uses two fixed low-end ports, namely control port 21 (ftp) and data port 20 (ftp-data). The firewall rules are well set, just open the two ports. the client is not so lucky. it dynamically opens a high-end port to wait for the connection, which makes the users in the Lan basically have nothing to do with internet access by sharing the same IP address. but is there any other solution? Yes! This is the passive mode we will talk about next.
Passive mode)
The data transmission process in passive mode is as follows (server ip: 192.168.200.1, client ip: 192.168.200.8 ):
1. 192.168.200.8.40.pdf> 192.168.200.1.ftp: PASV
2. 192.168.200.1.ftp> 192.168.200.8.40133: Entering PassiveMode (192,168,200, 187)
3. 192.168.200.8.40.pdf> 192.168.200.1.ftp: ...... ACK
4. 192.168.200.8.43422> 192.168.200.1.3003: ...... SYN
5. 192.168.200.1.3003> 192.168.200.8.43422: ...... SYN. ACK
6. 192.168.200.8.43422> 192.168.200.1.3003: ...... ACK
7. 192.168.200.8.40.pdf> 192.168.200.1.ftp: LIST
8 .. 192.168.200.1.ftp> 192.168.200.8.40.pdf: 150 Here comes thedirectory listing.
9. 192.168.200.1.3003> 192.168.200.8.43422:
-Rw-r -- 10 0 6 Mar 23 08:53 filetest
Drwxr-xr-x 20 0 4096 Dec 13 2007 pub
The following is a transmission tutorial on the active mode obtained from the above data:
A. (row 3) the client requests PASV data transmission from the server.
B. (row 2nd) the server opens a new data stream port and enters the listening mode. then, tell the customer, "Are you connected to 192.168.200.1? I have opened Port 3003 for you ".
(Port 3003 is calculated based on the last two digits of line 2nd, 11*256 + 187 = 3003)
C. (row 3) the client confirms that it receives the information from the server and responds.
D. (lines 4, 5, and 6) when the client learns that the server has opened a new data stream port for self-connection, it randomly selects a local port to connect to the port specified by the server, to transfer data.
(Note: This is important. Unlike the active mode, the server does not actively connect to the client, but opens a new port and passively waits for the client to connect. this is the so-called passive mode)
E. (row 7th) when the data stream port of the client and the server is successfully connected, the client requests the server to list the directory information data.
F. (row 8th) the server sends a message to the customer through control port 21: The Directory list you need is sent.
G. (row 9th) the server sends the directory data required by the user to the client through the data stream port.
Throughout the communication process, the client actively connects to the server, and no open port is connected. This does reduce the firewall of the client. but does the trouble disappear? No! In passive mode, the server only needs to dynamically open a high-end port to transmit data. it is difficult for us to handle this port. but don't worry. talented programmers think about everything for us. we just need to set parameters as required. next, let's analyze various possible situations.
Part 2. server-related settings
(Note: All examples in this article use RHEL5 + VSFTPD servers, and the default client does not have a firewall .)
The machine layout is as follows:
| Client | :::::::::::::::::::| LINUX gateway | ------ | VSFTPD server |
Client IP address: 10.10.10.10/8
LINUX gateway IP: 10.10.10.20/8, 192.168.1.1
VSFTPD server IP address: 192.168.1.8/24
1. configure the VSFTPD server. the configuration file is as follows: Listen = yes
Anonymous_enable = yes
Port_enable = yes
Connect_from_port_20 = yes
Pasv_enable = yes
Pasv_min_port = 10000
Pasvanderbilt max_port = 10100
This is a simple VSFTPD configuration. The first part allows access by anonymous users, the second part declares that the active mode is allowed, and the third part declares that the passive mode is allowed, the port opened by the server is limited to, so that we can set the corresponding firewall rules. I tested on the intranet, can be used normally, more detailed VSFTPD configuration can view man documentation or access http://blog.chinaunix.net/u3/94705/showart_1917230.html.
2. Gateway settings
First, enable the forwarding function.
# Echo 1>/proc/sys/net/ipv4/ip_forward
Then set the relevant NAT rules.
# Iptables-T nat-a postrouting-p tcp-s 192.168.1.8 -- sport 20:21-j SNAT -- to 10.10.10.20
# Iptables-t nat-a postrouting-p tcp-s 192.168.1.8 -- sport10000: 10100-j SNAT -- to 10.10.10.20
# Iptables-t nat-a prerouting-p tcp-d 10.10.10.20 -- dport 21-jDNAT -- to-destination 192.168.1.8
# Iptables-t nat-a prerouting-p tcp-d 10.10.10.20 -- dport10000: 10100-j DNAT -- to-destination 192.168.1.8
The first two rules forward the data on the port of the intranet FTP server.
The last two rules forward data from the Internet to the Gateway port to the FTP server.
3. Client Test
Log on to 10.10.10.10. the test result is as follows:
[Root @ X ~] # Ftp 10.10.10.20
Connected to 10.10.10.20 (10.10.10.20 ).
220 (vsFTPd 2.0.5)
Name (10.10.10.20: root): anonymous
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
Ftp> passive // disable the passive mode. test the active mode first.
Passive mode off.
Ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
-Rw-r -- 10 0 13 May 06 20:24 filetest
Drwxr-xr-x 20 0 4096 Dec 13 2007 pub
226 Directory send OK.
(At this point, we found that the active mode is configured correctly and can be connected successfully. However, as we mentioned in the previous section, the active mode is not highly applicable, and then we will test the passive mode)
Ftp> passive
Passive mode on.
Ftp> ls
227 Entering Passive Mode (192,168, 39,109)
Ftp: connect: Connection timed out
When I enter the ls command, the system returns a message "227 Entering Passive Mode (192,168, 39,109 )". then I waited for a message. The system reminded me that the connection timed out.
What's the problem? Let's look back at the first part of the passive mode transmission process and we will know that we failed in step D!
Of course it will fail! The server tells us that we can connect to Port 39*256 + 109 = 10093 of 192.168.1.8 for data transmission, but 192.168.1.8 is invisible to the client 10.10.10.10. it is behind 10.10.10.20.
There are two solutions. 1. intercept the data transmitted from the server to the client (Step B of the passive mode transmission process) and modify the data to spoof the client and connect it to the server's Gateway 10.10.10.20, because 192.168.1.8 is behind 10.10.10.20, it is invisible to the client. second, the server publishes "fake" messages to spoof the client to achieve the same effect.
Both solutions are well implemented. for the first solution, we load two modules on the Gateway Server to intercept and modify data. log on to 10.10.10.20 as follows:
# Modprobe ip_nat_ftp
# Modprobe ip_conntrack_ftp
Then we can test it on the client.
Ftp> ls
227 Entering Passive Mode (10, 10, 10, 20, 39,108)
150 Here comes the directory listing.
-Rw-r -- 10 0 13 May 06 20:24 filetest
Drwxr-xr-x 20 0 4096 Dec 13 2007 pub
226 Directory send OK.
How can this problem be solved. we can see the second line of information. the ip address has been changed to 10.10.10.20. after receiving this information, the client will connect to 10.10.10.20 and then forward it through 10.10.10.20. Therefore, we have succeeded.
Let's look at the second solution. how can we let the server publish "fake" messages to spoof the client? The author of vsftp has come up with this. we only need to add the following two lines to the vsftpd configuration file.
Pasv_addr_resolve = yes // you can use vsftpd to spoof customers.
Pasv_address = 10.10.10.20 // let vsftpd use this address to spoof the customer
After vsftpd is restarted, we can test it from the client and find that it can be used normally.
The security parameter is indeed worth the candle. if you still insist on using it after reading my suggestions, good luck to you.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service