| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>ProFTPD: Firewalls, Routers, and NAT</title> |
| 5 | </head> |
| 6 | |
| 7 | <body bgcolor=white> |
| 8 | |
| 9 | <hr> |
| 10 | <center><h2><b><i>ProFTPD: Firewalls, Routers, and NAT</i></b></h2></center> |
| 11 | <hr> |
| 12 | |
| 13 | <p> |
| 14 | <b>Basic NAT information</b><br> |
| 15 | NAT (<b>N</b>etwork <b>A</b>ddress <b>T</b>ranslation) is a system that acts |
| 16 | like a proxy, but on a "packet" level. When a computer on your |
| 17 | local network connects to a computer on the Internet, the NAT replaces the |
| 18 | "from" information of packets with its own address, making your |
| 19 | local network invisible to the Internet. Many firewalls perform NAT |
| 20 | duties as well, so the following information is valid in firewalled |
| 21 | environments as well. |
| 22 | |
| 23 | <p> |
| 24 | For server systems, NAT can improve security and enable multiple servers to be |
| 25 | accessed using a single IP address. This is done by allowing certain ports |
| 26 | forwarded "inward" to the local network. However, the part of the |
| 27 | FTP protocol known as "passive" data transfers is not by default |
| 28 | compatible with NAT solutions. But NAT functionality <i>is</i> possible with |
| 29 | ProFTPD versions 1.2rc2 and later. |
| 30 | |
| 31 | <p> |
| 32 | <b>Note</b>: for details on NAT configuration for Linux, read the Linux |
| 33 | IP-masq HOWTO at: |
| 34 | <pre> |
| 35 | <a href="http://tldp.org/HOWTO/IP-Masquerade-HOWTO/">tldp.org/HOWTO/IP-Masquerade-HOWTO/</a> |
| 36 | </pre> |
| 37 | or search for information concerning your OS of choice. |
| 38 | |
| 39 | <p> |
| 40 | <b>Configuring ProFTPD behind NAT</b><br> |
| 41 | First configure your installed <code>proftpd</code> so that it works correctly |
| 42 | from inside the NAT. There are example configuration files included with the |
| 43 | source. Then add the <a href="../modules/mod_core.html#MasqueradeAddress"><code>MasqueradeAddress</code></a> directive |
| 44 | to your <code>proftpd.conf</code> file to define the public name or IP address |
| 45 | of the NAT. For example: |
| 46 | <pre> |
| 47 | MasqueradeAddress ftp.mydomain.com # using a DNS name |
| 48 | MasqueradeAddress 123.45.67.89 # using an IP address |
| 49 | </pre> |
| 50 | Now your <code>proftpd</code> will hide its local address and instead use the |
| 51 | public address of your NAT. |
| 52 | |
| 53 | <p> |
| 54 | However, one <b>big</b> problem still exists. The passive FTP connections |
| 55 | will use ports from 1024 and up, which means that you must forward <i>all</i> |
| 56 | ports 1024-65535 from the NAT to the FTP server! And you have to allow many |
| 57 | (possibly) dangerous ports in your firewalling rules! Not a good situation. |
| 58 | For a good description of active versus passive FTP data transfers, see: |
| 59 | <pre> |
| 60 | <a href="http://slacksite.com/other/ftp.html">http://slacksite.com/other/ftp.html</a> |
| 61 | </pre> |
| 62 | To resolve this, simply use the <a href="../modules/mod_core.html#PassivePorts"><code>PassivePorts</code></a> directive |
| 63 | in your <code>proftpd.conf</code> to control what ports <code>proftpd</code> |
| 64 | will use for its passive data transfers: |
| 65 | <pre> |
| 66 | PassivePorts 60000 65535 # These ports should be safe... |
| 67 | </pre> |
| 68 | Note that if the configured range of ports is too small, connecting clients |
| 69 | may experience delays or be completely unable to operate when they |
| 70 | request passive data transfers. When the daemon cannot use one of the |
| 71 | ports in the configured range, it will fall back to using a kernel-assigned |
| 72 | port, and log a message reporting the issue. The clients' ability to |
| 73 | use this non-configured port will then depend on any NAT, router, or firewall |
| 74 | configuration. |
| 75 | <p> |
| 76 | Now start the FTP daemon and you should see something like: |
| 77 | <pre> |
| 78 | 123.45.67.89 - Masquerading as '123.45.67.89' (123.45.67.89) |
| 79 | </pre> |
| 80 | in the log files. |
| 81 | |
| 82 | <p> |
| 83 | <b>A Linux Example</b><br> |
| 84 | This example is for Linux kernel version 2.2.<i>x</i> with |
| 85 | <code>ipchains</code> and <code>ipmasqadm</code>. The examples below assume |
| 86 | that your FTP server has local address <code>192.168.1.2</code>. |
| 87 | |
| 88 | <p> |
| 89 | First we need to enable NAT for our FTP server. As <code>root</code>: |
| 90 | <pre> |
| 91 | $ echo "1">/proc/sys/net/ipv4/ip_forward |
| 92 | $ ipchains -P forward DENY |
| 93 | $ ipchains -I forward -s 192.168.1.2 -j MASQ |
| 94 | </pre> |
| 95 | Now we load the <code>autofw</code> kernel module and forward ports 20 and 21 |
| 96 | to the FTP server: |
| 97 | <pre> |
| 98 | $ insmod ip_masq_autofw |
| 99 | $ ipmasqadm autofw -A -r tcp 20 21 -h 192.168.1.2 |
| 100 | </pre> |
| 101 | Then we forward ports for passive FTP transfers. In our |
| 102 | <code>proftpd.conf</code> file we restricted passive transfers to ports |
| 103 | 60000-65535, so that is what we use here as well: |
| 104 | <pre> |
| 105 | $ ipmasqadm autofw -A -r tcp 60000 65535 -h 192.168.1.2 |
| 106 | </pre> |
| 107 | |
| 108 | <p> |
| 109 | If instead your Linux system uses IP Filters, then you might do something |
| 110 | like the following. First, update your <code>ipf.conf</code> with: |
| 111 | <pre> |
| 112 | # Allow passive FTP transfers from ports 49152 to 65534, the IANA-registered |
| 113 | # ephemeral port range. |
| 114 | pass in quick proto tcp from any to any port 49151 >< 65535 flags S keep state |
| 115 | </pre> |
| 116 | Then make sure that the changes take effect by using: |
| 117 | <pre> |
| 118 | $ ipf -Fa -f /path/to/ipf.conf |
| 119 | </pre> |
| 120 | |
| 121 | <p> |
| 122 | <b>Double Checking</b><br> |
| 123 | Setting up <code>proftpd</code> that allows passive data transfers srequires |
| 124 | that a range of ports be forwarded from the NAT to the local network. This |
| 125 | could be a security hazard, but since you can specify what port range to use, |
| 126 | you are still able to setup relatively tight firewalling rules. To be sure |
| 127 | that you have no other processes listening on the ports you have specified |
| 128 | for passive transfers, use a port scanner such as <code>nmap</code>: |
| 129 | <pre> |
| 130 | $ nmap -sT -I -p 60000-65535 localhost |
| 131 | </pre> |
| 132 | If the result says something like: |
| 133 | <pre> |
| 134 | All 5536 scanned ports on localhost (127.0.0.1) are: closed |
| 135 | </pre> |
| 136 | then you should be safe. |
| 137 | |
| 138 | <p><a name="FAQ"> |
| 139 | <b>Frequently Asked Questions</b><br> |
| 140 | |
| 141 | <p> |
| 142 | <font color=red>Question</font>: How do I know if my |
| 143 | <code>MasqueradeAddress</code> and <code>PassivePorts</code> configuration is |
| 144 | working?<br> |
| 145 | <font color=blue>Answer</font>: When performing a passive data transfer, an |
| 146 | FTP client sends the <code>PASV</code> command to the FTP server. The server |
| 147 | responds with the address and port to which the client should connect. For |
| 148 | example: |
| 149 | <pre> |
| 150 | 227 Entering Passive Mode (127,0,0,1,19,6). |
| 151 | </pre> |
| 152 | The address and port are contained in the parentheses, formatted as |
| 153 | <i>a1,a2,a3,a4,p1,p2</i>, where the IP address is: |
| 154 | <pre> |
| 155 | a1.a2.a3.a4 |
| 156 | </pre> |
| 157 | and the port number is: |
| 158 | <pre> |
| 159 | p1 * 256 + p2 |
| 160 | </pre> |
| 161 | If the address seen in the server's response is not a public IP address |
| 162 | or the port is not in the port range configured by your |
| 163 | <code>PassivePorts</code>, double-check your <code>proftpd.conf</code>. |
| 164 | Non-public IP addresses are defined by <a href="http://www.faqs.org/rfcs/rfc1918.html">RFC 1918</a>, and include <code>10.<i>x</i></code>, |
| 165 | <code>172.16.<i>x</i></code> and <code>192.168.0.<i>x</i></code>. |
| 166 | |
| 167 | <p> |
| 168 | <font color=red>Question</font>: I am using the <code>PassivePorts</code> |
| 169 | directive, but my FTP client still doesn't work. In the proftpd debug logging, |
| 170 | I see: |
| 171 | <pre> |
| 172 | May 20 17:00:55 www.example.com proftpd[10078] wwww.example.com (::ffff:1.2.3.4[::ffff:1.2.3.4]): Refused PORT 192,168,1,2,193,116 (address mismatch) |
| 173 | </pre> |
| 174 | <font color=blue>Answer</font>: The <code>PORT</code> there means that the |
| 175 | FTP client is requesting an <i>active</i> data transfer; this means that |
| 176 | proftpd is being asked to <i>actively</i> connect <b>to</b> the client |
| 177 | (to the given address 192.168.1.2, port 49524). |
| 178 | |
| 179 | <p> |
| 180 | The first problem is, as the log message indicates, that the IP address |
| 181 | given in the <code>PORT</code> command, 192.168.1.2, does <b>not</b> match |
| 182 | the IP address from which the client connected (<i>i.e.</i> 1.2.3.4). By |
| 183 | default, proftpd will refuse to create a data transfer connection to anywhere |
| 184 | <b>except</b> back to the requesting client's IP address; see the |
| 185 | <a href="../howto/FXP.html"><code>AllowForeignAddress</code></a> directive for |
| 186 | more information. |
| 187 | |
| 188 | <p> |
| 189 | The next problem is that 192.168.1.2 IP address is not a publicly routable I |
| 190 | address; specifically, it is part of an |
| 191 | <a href="http://www.faqs.org/rfcs/rfc1918.html">RFC 1918</a> address space. |
| 192 | This means that is <b>not possible</b> for proftpd to connect to that |
| 193 | address (unless proftpd is located in the same LAN). |
| 194 | |
| 195 | <p> |
| 196 | The solution for these situations is to <i>a)</i> configure proftpd to use |
| 197 | <b>both</b> <code>PassivePorts</code> <i>and</i> <code>MasqueradeAddress</code>, |
| 198 | and <i>b)</i> to configure your FTP client to request <i>passive</i> data |
| 199 | transfers (via the <code>PASV</code> and <code>EPSV</code> commands), rather |
| 200 | than active data transfers. |
| 201 | |
| 202 | <p> |
| 203 | <font color=red>Question</font>: I am using <code>MasqueradeAddress</code> |
| 204 | in my config, using the external DNS name for my site, but <code>proftpd</code> |
| 205 | still returns the internal/LAN IP address in the <code>PASV</code> response. |
| 206 | Is it a bug?<br> |
| 207 | <font color=blue>Answer</font>: Most likely not. If you use something like: |
| 208 | <pre> |
| 209 | MasqueradeAddress <i>ftp.example.com</i> |
| 210 | </pre> |
| 211 | and see the internal IP address in the <code>PASV</code> response, it |
| 212 | suggests that <code>proftpd</code>, when starting up, resolves that DNS name |
| 213 | from within your LAN, and gets that internal IP address. Rather than using |
| 214 | the DNS name, you should explicitly use the external IP address in your |
| 215 | <code>MasqueradeAddress</code> directive: |
| 216 | <pre> |
| 217 | MasqueradeAddress <i>1.2.3.4</i> |
| 218 | </pre> |
| 219 | where "1.2.3.4" is your real external IP address. |
| 220 | |
| 221 | <p> |
| 222 | <font color=red>Question</font>: Can I configure <code>proftpd</code> so that |
| 223 | it refuses to handle passive transfers?<br> |
| 224 | <font color=blue>Answer</font>: If you are using a version of |
| 225 | <code>proftpd</code> older than 1.2.10rc1, no. In 1.2.10rc1, support for |
| 226 | placing limits on the <code>PASV</code> and <code>PORT</code> (and their |
| 227 | IPv6 equivalents <code>EPSV</code> and <code>EPRT</code>) was added, so that |
| 228 | you could do the following: |
| 229 | <pre> |
| 230 | <Limit EPSV PASV> |
| 231 | DenyAll |
| 232 | </Limit> |
| 233 | </pre> |
| 234 | |
| 235 | <p><a name="UsingClasses"> |
| 236 | <font color=red>Question</font>: How can I make <code>proftpd</code> use |
| 237 | a different <code>MasqueradeAddress</code> based on the address of the |
| 238 | connecting client?<br> |
| 239 | <font color=blue>Answer</font>: This question usually arises in the case |
| 240 | where FTP clients connecting from inside the LAN see the same |
| 241 | <code>MasqueradeAddress</code> as external clients, which causes problems. |
| 242 | That <code>MasqueradeAddress</code> may be necessary in order to allow |
| 243 | external FTP clients to do passive data transfers. The internal clients |
| 244 | do not need it. To handle this, create a <code><VirtualHost></code> |
| 245 | section in your <code>proftpd.conf</code> to handle the LAN address of |
| 246 | the FTP server, the address that the internal clients are contacting. |
| 247 | In this <code><VirtualHost></code> section, make sure there is no |
| 248 | <code>MasqueradeAddress</code> directive. This way, the external FTP |
| 249 | clients "see" the configuration with the |
| 250 | <code>MasqueradeAddress</code> they need, and the internal FTP clients |
| 251 | "see" a different configuration, one with no |
| 252 | <code>MasqueradeAddress</code>. |
| 253 | |
| 254 | <p> |
| 255 | For those that need to see a concrete example configuration of this: |
| 256 | <pre> |
| 257 | ServerName "Some Server Name" |
| 258 | MasqueradeAddress my.domain.com |
| 259 | PassivePorts 60000 65535 |
| 260 | |
| 261 | <font color=green># Note that your LAN address should be used here</font> |
| 262 | <VirtualHost 192.168.0.10> |
| 263 | ServerName "Some Other Server Name" |
| 264 | |
| 265 | <font color=blue># Note that there is <b>no</b> MasqueradeAddress directive |
| 266 | # used in this section!</font> |
| 267 | </VirtualHost> |
| 268 | </pre> |
| 269 | |
| 270 | <p> |
| 271 | On the other hand, there <em>can</em> be cases where you really do need |
| 272 | different <code>MasqueradeAddress</code> values, <i>in the same vhost</i>, |
| 273 | depending on the client IP address. For these situations, you should |
| 274 | use <a href="Classes.html">classes</a> and the |
| 275 | <a href="../contrib/mod_ifsession.html"><code>mod_ifsession</code></a> |
| 276 | module, like so: |
| 277 | <pre> |
| 278 | <IfModule mod_ifsession.c> |
| 279 | # Define a class for internal clients |
| 280 | <Class internal> |
| 281 | From 10.0.0.0/8 |
| 282 | </Class> |
| 283 | |
| 284 | # Internal clients get a different MasqueradeAddress |
| 285 | <IfClass internal> |
| 286 | MasqueradeAddress 10.1.2.3 |
| 287 | </IfClass> |
| 288 | |
| 289 | # All other clients get some different, public MasqueradeAddress |
| 290 | <IfClass !internal> |
| 291 | MasqueradeAddress 1.2.3.4 |
| 292 | </IfClass> |
| 293 | </IfModule> |
| 294 | </pre> |
| 295 | |
| 296 | <p> |
| 297 | <hr> |
| 298 | Contributor: Tobias Ekbom <tobias <i>at</i> vallcom <i>dot</i>com><br> |
| 299 | <hr> |
| 300 | |
| 301 | <p> |
| 302 | <hr> |
| 303 | <font size=2><b><i> |
| 304 | © Copyright 2017 The ProFTPD Project<br> |
| 305 | All Rights Reserved<br> |
| 306 | </i></b></font> |
| 307 | <hr> |
| 308 | |
| 309 | </body> |
| 310 | </html> |