Use iptables to set NAT for FTP

Source: Internet
Author: User
In the past, the company's FTP was on the public network, and it was not a big problem to use it. now I feel that a gateway may be safer, so I put the FTP in the intranet, therefore, port forwarding must be performed on the Gateway. I configured 21 to switch to intranet FTP just like forwarding other ports. However, the connection is always unsuccessful, and the problem is found only after searching. this involves that FTP in the company before the FTP Association is on the public network, and it is not a big problem to use it, now I feel that a gateway may be safer, so I put FTP in the intranet, so of course port forwarding is required on the Gateway. I configured 21 to switch to intranet FTP just like forwarding other ports. However, the connection is always unsuccessful. you can find the problem after searching it. this involves the content of the FTP protocol:

LinuxIptablesConfigure Active and Passive FTP mode

Active/passive connection mode:

The FTP protocol can work in two ways: PORT and PASV. the Chinese meaning is active and passive.

Port mode: ftp server: tcp 21 <------ client: dynamic ftp server: tcp 20 ------> client: dynamic

Pasv mode: ftp server: tcp 21 <---- client: dynamic ftp server: tcpDynamic<---- Client: dynamic

The active connection process is that the client sends a connection request to the ftp port of the server (21 by default). The server accepts the connection and establishes a command link. When data needs to be transmitted, the client uses the PORT command on the command link to tell the server: "I opened PORT XXXX and you came to connect to me ". The server sends a connection request from Port 20 to Port XXXX of the client and establishes a data link to transmit data.

PASV (passive) connection process: the client sends a connection request to the FTP port of the server (21 by default). The server accepts the connection and establishes a command link. When data needs to be transmitted, the server uses the PASV command on the command link to tell the client: "I opened Port XXXX, and you came to connect to me ". Therefore, the client sends a connection request to Port XXXX of the server and establishes a data link to transmit data.

The reason for PASV mode is that the default port 21 is the control port and 20 is the data port.

The FTP port can only occupy one data port at the same time, that is, this can have one user, so when PASV mode is enabled, any port can be opened by the server, to allow other users to transmit data from non-20 ports.

Bold and red fonts:DynamicThis is the reason. when Pasv mode is used for connection, the FTP server uses a random port. However, the random port is not opened on the firewall, and the FTP data port cannot be connected.

So I need to continue looking for the reason. I am a little worried about this command introduced on the internet. will it open all ports after 1024?

# Iptables-a input-p tcp --- dport 1024:-m state -- state ESTABLISHED, RELATED-j ACCEPT

For this purpose, I carefully studied iptabels firewall rules. Sure enough, I think of the previous company's router products. it is actually configuring iptables rules.

The above rule is interpreted as: add a rule in the INPUT chain. The rule protocol is TCP, and the data after the target Port is 1024. when the data connection status is ESTABLISHED or RELATED, all the data is allowed to enter the local machine.

Because NAT is required, the above rules are not suitable.

Iptables mainly manages three tables to control data packets in and out of the host [forwarding]:

About netfilter table and netfilter chain

This figure clearly shows the netfilter table and netfilter chain.
The Filter table contains three links:
The INPUT destination is the data packet accessing the firewall.
The OUTPUT source address is the data packet sent out by the firewall.
Both the source address and destination address of the FORWARD packet are not firewall data packets.
There are also three links in the Nat table:
The OUTPUT packet is thrown to the proxy server before it is sent to the Gateway.
PREROUTING performs destination NAT translation (before route judgment)
POSTROUTING for Source NAT translation (after route judgment)
There are five links in Mangle talbe:
INPUT
OUTPUT
FORWARD
PREROUTING
POSTROUTING

Process of iptables data packets

This figure shows the packet flow of the entire iptables firewall at work.
Iptables access control rules
When a data packet passes through the iptables firewall, the first policy is read first. if the first policy matches, the policy is executed and not read.

If the first policy does not match, the second policy is read. if the second policy is matched, the policy is executed and not read.

If the data packet does not match any policy, the default policy is matched. The default policy is allow or deny, which can be defined by the user.
Iptables matching rules
-S 192.168.0.0/24
CIDR block whose source address is 192.168.0.0/24
-D 192.168.0.10
Host whose destination address is 192.168.0.10
-I eth0
Packets from eht0
-O eth0
Packets sent from eth0
'! '192.168.0.10
Reverse selection
-P tcp -- dport 80
Protocol Type. the target Port is 80.
-P udp -- sport 53
Protocol Type. the Source port is 53.
Rules target of iptables
DROP reject
ACCEPT allowed
LOG. after this policy is matched, the policy will continue to be read below.
The REJECT rejects the request, but returns the response to the user.
Iptables basic chain operations
Iptables-L
View iptables table (filter table is displayed by default)
Iptables-nL
Display as IP address
Iptables-L -- line-numbers
Display the row number of a policy entry
Iptables-vL
View iptables table (display more details)
Iptables-
Append an iptables policy entry (the last part of the policy will be appended by default)
Iptables-I
Insert iptables policy entries (insert to the beginning of the policy by default)
Iptables-I INPUT 2
Insert it into the second policy of the iptables policy entry
Iptables-d input 2
Delete the second policy of the iptables policy entry
Iptables-F
Clear all policies, but do not clear the default chain policies.
Iptables-Z
Clear counter
Iptables-N
Add custom links
Iptables-X
Clear custom links
About the default chain policy of iptables
Iptables-P INPUT DROP
Change the default policy of the INPUT chain. DROP and REJECT can be used only for rejection. (This policy is finally executed)
Iptables-P OUTPUT DROP
Change the default OUTPUT chain policy
Iptables-P FORWARD DROP
Change the default FORWARD chain policy

The procedure is as follows:

1. load modules on the firewall

Modprobe ip_nat_ftp
Modprobe ip_conntrack
Modprobe ip_conntrack_ftp

2. configure firewall rules:

Sudo iptables-t nat-a prerouting-p tcp-d 211.6.38.3 -- dport 21-j DNAT -- to 192.168.0.241
Sudo iptables-t nat-a prerouting-p tcp-d 211.6.38.3 -- dport 20-j DNAT -- to 192.168.0.241
Sudo iptables-t nat-a prerouting-p tcp-d 211.6.38.3 -- dport 50000: 60000-j DNAT -- to 192.168.0.241
Sudo iptables-t nat-a postrouting-d 192.168.0.241-p tcp -- dport 21-j SNAT -- to 192.168.0.8
Sudo iptables-t nat-a postrouting-d 192.168.0.241-p tcp -- dport 20-j SNAT -- to 192.168.0.8
Sudo iptables-t nat-a postrouting-d 192.168.0.241-p tcp -- dport 50000: 60000-j SNAT -- to 192.168.0.8
Sudo iptables-a forward-o eth1-p tcp-d 192.168.0.241 -- dport 21-m state -- state NEW, ESTABLISHED, RELATED-j ACCEPT
Sudo iptables-a forward-o eth1-p tcp-d 192.168.0.241 -- dport 20-m state -- state NEW, ESTABLISHED, RELATED-j ACCEPT
Sudo iptables-a forward-o eth1-p tcp-d 192.168.0.241 -- dport 50000: 60000-m state -- state NEW, ESTABLISHED, RELATED-j ACCEPT
Sudo iptables-a forward-I eth1-p tcp -- sport 21-s 192.168.0.241-m state -- state ESTABLISHED, RELATED-j ACCEPT
Sudo iptables-a forward-I eth1-p tcp -- sport 20-s 192.168.0.241-m state -- state ESTABLISHED, RELATED-j ACCEPT
Sudo iptables-a forward-I eth1-p tcp -- sport 50000: 60000-s 192.168.0.241-m state -- state ESTABLISHED, RELATED-j ACCEPT

Explanation of the above rules:

The first rule is to change the destination IP address to 192.168.0.241 (FTP intranet IP address) when 211.6.38.3 receives a request with an access port of 21/20/50000-60000)

The second rule is to change the source IP address in the packet to 192.168.0.241 to 192.168.0.8 (Firewall intranet IP address) when there is a packet sent to 192.168.0.241 port 21/20/50000-60000)

The third group of rules is sent from the firewall eth1 (Firewall intranet interface) port, the destination address is 192.168.0.241, the destination port is 21/20/50000-60000 data packets, when the status is NEW, ESTABLISHED, RELATED is allowed

The fourth rule is that when the port enters from the firewall eth1, the source IP address is 192.168.0.241, and the source port is 21/20/50000-60000 packets, when the status is ESTABLISHED, RELATED is allowed to pass

Note:

The above 12 rules. Three groups of ports 21/20/50000-60000 are enabled on the firewall:

In active mode, Port 21 is the control port and Port 20 is the data port.

In passive mode, the data port is random, but the range of the passive Port can also be configured on the FTP server. in this case, the value is 50000-60000.

Here are some configuration rules for Vsftpd:

Anonymous_enable = NO
Local_enable = YES
Local_max_rate = 10000000
Anon_max_rate = 10000000
Write_enable = NO
Local_umask = 022
Dirmessage_enable = YES
Xferlog_enable = YES
Connect_from_port_20 = YES
Xferlog_file =/var/log/vsftpd. log
Xferlog_std_format = YES
Dual_log_enable = YES
Vsftpd_log_file =/var/log/vsftp. log
Ascii_upload_enable = YES
Ascii_download_enable = YES
Chroot_list_enable = YES
Chroot_list_file =/etc/vsftpd/chroot_list
Listen = YES
Pam_service_name = vsftpd
Pasv_addr_resolve = yes
Pasv_address = 211.6.38.3
Pasv_min_port = 50000
Pasvanderbilt max_port = 60000
User_config_dir =/etc/vsftpd/vuser_conf
Userlist_enable = YES
Tcp_wrappers = YES
Listen_port = 21
Idle_session_timeout = 300
Data_connection_timeout = 5
Accept_timeout = 10
Connect_timeout = 10
Use_localtime = YES
Max_clients = 20
Max_per_ip = 10

Now that Vsftpd is configured, restart the FTP server:

Sudo service vsftpd restart

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.