| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>ProFTPD: Configuring FTP over SSH</title> |
| 5 | </head> |
| 6 | |
| 7 | <body bgcolor=white> |
| 8 | |
| 9 | <hr> |
| 10 | <center><h2><b><i>ProFTPD: Configuring FTP over SSH</i></b></h2></center> |
| 11 | <hr> |
| 12 | <i><b>IMPORTANT NOTICE</b>: the original instructions listed on this page, |
| 13 | based on <a href="http://www.proftpd.org/proftpd-l-archive/98-11/msg00066.html">http://www.proftpd.org/proftpd-l-archive/98-11/msg00066.html</a> were |
| 14 | incorrect, and did <b>not</b> lead to the proper encryption of the FTP control |
| 15 | channel. Many, many thanks to Michael Jochimsen for pointing out the |
| 16 | incorrect use of ssh port forwarding!</i><br> |
| 17 | <hr> |
| 18 | |
| 19 | <p> |
| 20 | <b>Premise</b><br> |
| 21 | The File Transfer Protocol is an old protocol, and like many of the older |
| 22 | protocols it was developed in a time when security was not of such a paramount |
| 23 | concern. One aspect of FTP that reflects this is the transmission of |
| 24 | passwords, used to prove to the server that the client is who they say they |
| 25 | are, "in the clear", unencrypted, visible to any packet sniffer. |
| 26 | Protocols like SSH, and its <code>scp</code> program, are replacing FTP |
| 27 | as a means to securely transfer files among hosts. |
| 28 | |
| 29 | <p> |
| 30 | However, many people still prefer to use their standard FTP clients. What |
| 31 | would be easiest would be a way to allow such clients to function while |
| 32 | transparently providing the encryption necessary for today's networks. Can |
| 33 | this be done? Yes - after a fashion. This document aims to describe how |
| 34 | to tunnel FTP over an SSH channel, providing for secure transmission of |
| 35 | the user's password. |
| 36 | |
| 37 | <p> |
| 38 | <b>Client Configuration</b><br> |
| 39 | The trick to encrypting the password is to make use of ssh's local port |
| 40 | forwarding capability. It works by creating a sort of proxy on the local host |
| 41 | that listens to some high-numbered port. Any traffic sent to that port |
| 42 | is encrypted, and forwarded to the configured remote address and port. |
| 43 | (<b>Note</b>: the prior instructions' flaw was that the local port forwarding |
| 44 | request was sent to localhost, rather than to the remote server, which |
| 45 | effectively set up the encrypted channel between localhost and itself). |
| 46 | Here's how to set up a local port forward: |
| 47 | <pre> |
| 48 | $ ssh -L<i>local-port</i>:<i>remote-addr</i>:<i>remote-port</i> <i>user</i>@<i>host</i> |
| 49 | </pre> |
| 50 | This says to listen on port <i>local-port</i> on localhost, and to send that |
| 51 | encrypted traffic to <i>host</i>'s <i>remote-addr</i> at port |
| 52 | <i>remote-port</i>. To use this trick to secure an FTP session (to be |
| 53 | specific, the control channel, through which passwords are transmitted, will |
| 54 | be encrypted; the data channel will not), it would look like: |
| 55 | <pre> |
| 56 | $ ssh -f -L3000:ftpserver:21 ftpserver 'exec sleep 10' && ftp localhost 3000 |
| 57 | </pre> |
| 58 | Note that the choice of <i>local-port</i> is arbitrary. Using port |
| 59 | <code>3000</code> is not a requirement. This trick also requires that |
| 60 | the ftp client use passive mode data transfers, so make sure to use a client |
| 61 | that understands FTP passive mode. |
| 62 | |
| 63 | <p> |
| 64 | <b>Remember that only the control connection is encrypted, not the data |
| 65 | connection: any data you transfer (<i>e.g.</i> directory listings, files |
| 66 | uploaded or downloaded) are still sent "in the clear".</b> Your |
| 67 | password (and the other FTP commands sent by the client) is not. |
| 68 | |
| 69 | <p> |
| 70 | <b>Server Configuration</b><br> |
| 71 | The server side of the connection needs some configuration to make the ssh |
| 72 | tunneling work as well. First, the client need a valid account on the remote |
| 73 | host is necessary in order to support the ssh tunnel. A valid shell is not |
| 74 | strictly necessary for these ssh tunnels, though; something like: |
| 75 | <pre> |
| 76 | #!/bin/sh |
| 77 | sleep 10 |
| 78 | </pre> |
| 79 | would suffice. This would prevent the FTP users from logging in, yet give them |
| 80 | enough time to establish a port forwarded ssh connection. With this sort |
| 81 | of quasi-shell (although, strictly speaking, there are better, more restrictive |
| 82 | shells than this example, as it could be escaped from), one can ssh over any |
| 83 | command: |
| 84 | <pre> |
| 85 | $ ssh -l test -f -L3000:ftpserver:21 ftpserver true && ftp localhost 3000 |
| 86 | </pre> |
| 87 | You'll also need to use the |
| 88 | <a href="../modules/mod_core.html#AllowForeignAddress"><code>AllowForeignAddress</code></a> configuration directive in your |
| 89 | configuration file: |
| 90 | <pre> |
| 91 | AllowForeignAddress on |
| 92 | </pre> |
| 93 | or <code>proftpd</code> will reject the passive transfer connections and log: |
| 94 | <pre> |
| 95 | SECURITY VIOLATION: Passive connection from {host} rejected |
| 96 | </pre> |
| 97 | |
| 98 | <p> |
| 99 | Note that the use of the server host's DNS name, or its IP address, in the |
| 100 | setting up of the ssh tunnel assumes that the routing of traffic to the |
| 101 | host's own IP address will be short-circuited in the kernel and thus not |
| 102 | actually be transmitted on the wire. On modern kernels this is a fair |
| 103 | assumption, but may not always be the case. For the truly paranoid, use |
| 104 | of <code>localhost</code> or <code>127.0.0.1</code> as the <i>remote-addr</i> |
| 105 | parameter in the ssh tunnel command would cause traffic on <i>host</i> to |
| 106 | be sent over its loopback address, and not over the network. However, this |
| 107 | will prevent data transfers from working altogether. The author has a |
| 108 | patch for scenarios like this; if interested, please contact him directly via |
| 109 | email.<br> |
| 110 | |
| 111 | <p> |
| 112 | <hr> |
| 113 | <font size=2><b><i> |
| 114 | © Copyright 2017 The ProFTPD Project<br> |
| 115 | All Rights Reserved<br> |
| 116 | </i></b></font> |
| 117 | <hr> |
| 118 | |
| 119 | </body> |
| 120 | </html> |