1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
<!DOCTYPE html>
<html>
<head>
<title>ProFTPD module mod_proxy_protocol</title>
</head>
<body bgcolor=white>
<hr>
<center>
<h2><b>ProFTPD module <code>mod_proxy_protocol</code></b></h2>
</center>
<hr><br>
<p>
The purpose of the <code>mod_proxy_protocol</code> module is to handle
protocols which are used by proxies, <i>e.g.</i> <code>haproxy</code>, for
conveying information about the real origin/client to the backend server.
Protocols like HTTP often have their own mechanism for doing so, via headers
such as "X-Forwarded-For". Unfortunately, FTP does <b>not</b> have such a
mechanism, nor does SSH.
<p>
However, there <em>are</em> protocols which an provide this information without
impacting FTP. HAproxy's <a href="http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt"><code>PROXY</code></a> protocol is one such mechanism. The
<code>mod_proxy_protocol</code> module uses these mechanisms to change the
information about the "remote" client so that it is the real client, not the
proxy, whose IP address/port are logged and used for <i>e.g.</i> network ACLs.
<p>
This module is contained in the <code>mod_proxy_protocol.c</code> file for
ProFTPD 1.3.<i>x</i>, and is not compiled by default. Installation
instructions are discussed <a href="#Installation">here</a>; detailed
notes on best practices for using this module are <a href="#Usage">here</a>.
<p>
The most current version of <code>mod_proxy_protocol</code> can be found at:
<pre>
<a href="https://github.com/Castaglia/proftpd-mod_proxy_protocol.git">https://github.com/Castaglia/proftpd-mod_proxy_protocol.git</a>
</pre>
<h2>Author</h2>
<p>
Please contact TJ Saunders <tj <i>at</i> castaglia.org> with any
questions, concerns, or suggestions regarding this module.
<h2>Directives</h2>
<ul>
<li><a href="#ProxyProtocolEngine">ProxyProtocolEngine</a>
<li><a href="#ProxyProtocolIgnore">ProxyProtocolIgnore</a>
<li><a href="#ProxyProtocolOptions">ProxyProtocolOptions</a>
<li><a href="#ProxyProtocolTimeout">ProxyProtocolTimeout</a>
<li><a href="#ProxyProtocolVersion">ProxyProtocolVersion</a>
</ul>
<p>
<hr>
<h3><a name="ProxyProtocolEngine">ProxyProtocolEngine</a></h3>
<strong>Syntax:</strong> ProxyProtocolEngine <em>on|off</em><br>
<strong>Default:</strong> None<br>
<strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code><br>
<strong>Module:</strong> mod_proxy_protocol<br>
<strong>Compatibility:</strong> 1.3.5rc4 and later
<p>
The <code>ProxyProtocolEngine</code> directive enables the expectation
and handling of protocols which provide information on proxied connections;
support for these protocols is provided by <code>mod_proxy_protocol</code>.
<p>
<hr>
<h3><a name="ProxyProtocolIgnore">ProxyProtocolIgnore</a></h3>
<strong>Syntax:</strong> ProxyProtocolIgnore <em>on|off</em><br>
<strong>Default:</strong> off<br>
<strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code><br>
<strong>Module:</strong> mod_proxy_protocol<br>
<strong>Compatibility:</strong> 1.3.5rc4 and later
<p>
The <code>ProxyProtocolIgnore</code> directive determines whether the
<code>mod_proxy_protocol</code> module will <em>use</em> the information it
reads, in the PROXY protocol data that may be sent by a client.
<p>
This is useful for cases where clients may be sending/using the PROXY protocol,
but you may not want <code>mod_proxy_protocol</code> to use the data from some
clients. For example:
<pre>
<Class untrusted-senders>
From 1.2.3.4
From 5.6.7.8
</Class>
<IfModule mod_proxy_protocol.c>
# We always to read PROXY protocol headers from clients...
ProxyProtocolEngine on
# ...but we don't always trust the senders
<IfClass untrusted-senders>
ProxyProtocolIgnore on
</IfClass>
</IfModule>
</pre>
<p>
<hr>
<h3><a name="ProxyProtocolOptions">ProxyProtocolOptions</a></h3>
<strong>Syntax:</strong> ProxyProtocolOptions <em>opt1 ...</em><br>
<strong>Default:</strong> None<br>
<strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code><br>
<strong>Module:</strong> mod_proxy_protocol<br>
<strong>Compatibility:</strong> 1.3.5a and later
<p>
The <code>ProxyProtocolOptions</code> directive is used to configure various
optional behavior of <code>mod_proxy_protocol</code>. For example:
<pre>
ProxyProtocolOptions UseProxiedServerAddress
</pre>
<p>
The currently implemented options are:
<ul>
<li><code>UseProxiedServerAddress</code><br>
<p>
The <code>PROXY</code> protocol sends both the original source (client)
IP address/port, <i>and</i> the original destination (server) address/port.
The <code>mod_proxy_protocol</code> module only uses the client IP
address/port in the <code>PROXY</code> protocol message by default. Use
this option to tell <code>mod_proxy_protocol</code> to use the provided
server address/port as well, by looking for any matching
<code><VirtualHost></code> configurations, and using them if found.
Note that if no matching <code><VirtualHost></code> configurations
are found, the proxied server address/port information will be ignored.
</li>
</ul>
<p>
<hr>
<h3><a name="ProxyProtocolTimeout">ProxyProtocolTimeout</a></h3>
<strong>Syntax:</strong> ProxyProtocolTimeout <em>seconds</em><br>
<strong>Default:</strong> 3sec<br>
<strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code><br>
<strong>Module:</strong> mod_proxy_protocol<br>
<strong>Compatibility:</strong> 1.3.5rc4 and later
<p>
The <code>ProxyProtocolTimeout</code> directive is used to configure the
amount of time, in seconds, that <code>mod_proxy_protocol</code> will wait to
receive the full expected proxy information. If the full information is
not received within the given number of seconds, the connection to the client
is closed.
<p>
<hr>
<h3><a name="ProxyProtocolVersion">ProxyProtocolVersion</a></h3>
<strong>Syntax:</strong> ProxyProtocolVersion <em>protocolVersion</em><br>
<strong>Default:</strong> haproxyV1<br>
<strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code><br>
<strong>Module:</strong> mod_proxy_protocol<br>
<strong>Compatibility:</strong> 1.3.5rc4 and later
<p>
The <code>ProxyProtocolVersion</code> directive is used to configure the
protocol that <code>mod_proxy_protocol</code> expects to handle. The
currently supported values are:
<ul>
<li><code>haproxyV1</code>
<li><code>haproxyV2</code>
</ul>
<p>
<hr>
<h2><a name="Usage">Usage</a></h2>
<p>
<b>Example Configuration</b><br>
<pre>
<IfModule mod_proxy_protocol.c>
ProxyProtocolEngine on
ProxyProtocolTimeout 3sec
# Necessary to allow data transfers
AllowForeignAddress on
</IfModule>
</pre>
<p>
<b>Module Load Order</b><br>
In order for <code>mod_proxy_protocol</code> to work its magic, it <b>must</b>
the first module in line to handle the bytes coming in from the client.
If some other module (such as <code>mod_sftp</code> or <code>mod_tls</code>)
tries to handle the incoming bytes first, Bad Things will happen, since those
modules will expect different protocols than the <code>PROXY</code> protocol.
<p>
For <code>mod_proxy_protocol</code> to be the first module called, it must
the <b>last</b> module loaded. To do this as a static module, you would
use something like this when building proftpd:
<pre>
# ./configure --with-modules=...:mod_proxy_protocol
</pre>
ensuring that <code>mod_proxy_protocol</code> is the <b>last</b> module in
your <code>--with-modules</code> list.
<p>
As a shared module, configuring <code>mod_proxy_protocol</code> to be the
last module loaded is much easier. Your configuration will have a list
of <code>LoadModule</code> directives; the last of which would be:
<pre>
LoadModule mod_proxy_protocol.c
</pre>
<b>Note</b> that using <code>mod_proxy_protocol</code> as a shared module
is <i>required</i> in cases where you want to use both
<code>mod_proxy_protocol</code> <i>and</i> <code>mod_ifsession</code>. For
example, perhaps you want to use <code>mod_ifsession</code> to change the
behavior of some module, <i>e.g.</i> <code>mod_ban</code>, based on the IP
address of the original client. This means that <code>mod_proxy_protocol</code>
would need to hande the connection <i>first</i>, so that it can decode the
<code>PROXY</code> protocol and set the correct client IP address.
<i>However</i>, the ProFTPD build system is hardcoded to ensure that the
<code>mod_ifsession</code> will always be first -- <i>if</i> using static
modules. By using <i>shared</i> modules, you can enforce the proper ordering
using the <code>LoadModule</code> directive, like so:
<pre>
<IfModule mod_dso.c>
...
LoadModule mod_ifsession.c
LoadModule mod_proxy_protocol.c
</IfModule>
</pre>
The <i>last</i> module loaded will be the <i>first</i> module called.
<p>
<b>Trusting Senders of Proxy Data</b><br>
Use of these proxy protocols means changes in audit trails and/or client
access permissions (<i>e.g.</i> different <code>mod_wrap2</code> and/or
<code>mod_geoip</code> rules will apply). Unscrupulous senders may try to
actively lie to your server about the original client using these protocols.
Thus you <b>must</b> trust the upstream machines <b>before</b> enabling the
<code>mod_proxy_protocol</code> module.
<p>
Put another way: do <b>not</b> use the <code>mod_proxy_protocol</code> module
if your server handles connections from the open Internet. Doing so means
that any machine can use the proxy protocol to hide their activities, or
make it look like the connection is coming from someone else. <b>Only accept
proxy information from trusted sources.</b>
<p>
If you need to <i>selectively</i> support the <code>PROXY</code> protocol
from some IPs, but not others, then you can use <a href="http://www.proftpd.org/docs/howto/Classes.html">classes</a>, and the <a href="http://www.proftpd.org/docs/contrib/mod_ifsession.html"><code>mod_ifsession</code></a> module, like
this:
<pre>
<Class expect-proxy-protocol>
From 192.168.18.33/24
From 192.168.33.2
From 10.0.0.0/8
</Class>
<IfClass expect-proxy-protocol>
<IfModule mod_proxy_protocol.c>
# Enable PROXY protocol support for clients in this class
ProxyProtocolEngine on
# Necessary to allow data transfers from this class
AllowForeignAddress expect-proxy-protocol
</IfModule>
</IfClass>
</pre>
Note that per-user or per-group enabling of the <code>PROXY</code> protocol
<b>cannot</b> be done; we only <i>know</i> the user/group of the connection
<i>after</i> the <code>PROXY</code> protocol has been used.
<p>
<b>Why <code>AllowForeignAddress</code> Is Needed</b><br>
One of the consequences of allowing <code>mod_proxy_protocol</code> to change
the remote IP address is that security checks performed on data transfers
will cause problems. For active data transfers (<i>i.e.</i> for clients
which send the <code>PORT</code> or <code>EPRT</code> commands),
<code>proftpd</code> requires that the IP address sent in the command matches
the IP address of the client which sends the command. Otherwise, a message
like the following is logged:
<pre>
Refused PORT 127,0,0,1,218,225 (address mismatch)
</pre>
and the command is rejected.
<p>
Similarly for passive data transfers (<i>i.e.</i> for clients which send the
<code>PASV</code> or <code>EPSV</code> commands), <code>proftpd</code> requires
that the remote IP address of the client which connects to the data transfer
address <b>must</b> match the remote IP address of the client on the control
connection. If the addresses do no match, then the following is logged:
<pre>
SECURITY VIOLATION: Passive connection from 127.0.0.1 rejected.
</pre>
and the control connection is closed.
<p>
These security measures are done to prevent abuses of FTP data transfers
such as the <a href="http://www.proftpd.org/docs/howto/FXP.html">FTP bounce</a>
attack. However, the very fact that <code>mod_proxy_protocol</code> changes
the remote IP address means that to allow data transfers when using this module,
you need to use:
<pre>
AllowForeignAddress on
</pre>
in the same virtual host section in which the <code>ProxyProtocolEngine</code>
directive appears.
<p>
A more secure configuration for <code>AllowForeignAddress</code> is possible
when you use the <code><Class></code> and <code><IfClass></code>
configurations, where you can limit the scope of
<code>AllowForeignAddress</code> to just that class, using:
<pre>
# Allow foreign addresses, but only from the "expect-proxy-protocol" class
AllowForeignAddress expect-proxy-protocol
</pre>
rather than allowing <b>any</b> IP address to connect to the data sockets.
<p><a name="SessionNotes">
<b>Session Notes</b><br>
The <code>mod_proxy_protocol</code> module may provide the following <em>session notes</em> for <i>e.g.</i> PROXY protocol V2 Type-Length-Value (TLVs) as well:
<ul>
<li><code>mod_proxy_protocol.alpn</code>
<li><code>mod_proxy_protocol.authority</code>
<li><code>mod_proxy_protocol.tls.cipher</code>
<li><code>mod_proxy_protocol.tls.version</code>
<li><code>mod_proxy_protocol.unique-id</code>
</ul>
These note variables are intended for informational purposes, <i>e.g.</i>
<code>ExtendedLog</code> or <code>SQLLog</code> directives via the
<code>%{note:...}</code> syntax.
<p>
<hr>
<h2><a name="Installation">Installation</a></h2>
To install <code>mod_proxy_protocol</code>, copy the
<code>mod_proxy_protocol.c</code> file into:
<pre>
<i>proftpd-dir</i>/contrib/
</pre>
after unpacking the latest proftpd-1.3.<i>x</i> source code. For including
<code>mod_proxy_protocol</code> as a staticly linked module:
<pre>
$ ./configure --with-modules=...:mod_proxy_protocol
</pre>
To build <code>mod_proxy_protocol</code> as a DSO module:
<pre>
$ ./configure --enable-dso --with-shared=...:mod_proxy_protocol
</pre>
Then follow the usual steps:
<pre>
$ make
$ make install
</pre>
<p>
For those with an existing ProFTPD installation, you can use the
<code>prxs</code> tool to add <code>mod_proxy_protocol</code>, as a DSO module,
to your existing server:
<pre>
$ prxs -c -i -d mod_proxy_protocol.c
</pre>
<p><a name="FAQ">
<b>Frequently Asked Questions</b><br>
<p><a name="HAProxyHeadersInDataFiles">
<font color=red>Question</font>: We are using HAProxy in front of ProFTPD, and
we see <code>PROXY</code> protocol headers appended to our upload files. What
is wrong?<br>
<font color=blue>Answer</font>: Usually this happens because of the specific
HAProxy configuration used -- and can be fixed by adjusting that HAProxy
configuration.
<p>
Let's assume your HAProxy configuration looks something like this:
<pre>
listen ftp
bind 0.0.0.0:21,:60000-60250
mode tcp
balance source
server services-a 192.168.1.71 send-proxy-v2 check port 21
server services-b 192.168.1.72 send-proxy-v2 check port 21
</pre>
This causes HAProxy to use the PROXY protocol (via <code>send-proxy-v2</code>)
for control <em>and data connections</em>, which is probably not what you
intended.
<p>
The solution is to use separate HAProxy configurations for the FTP control
and FTP data addresses/ports, and to <b>not</b> use <code>send-proxy-v2</code>
(or <code>send-proxy</code>) for the FTP data ports:
<pre>
listen ftp
bind 0.0.0.0:21
mode tcp
balance source
server services-a 192.168.1.71 send-proxy-v2 check port 21
server services-b 192.168.1.72 send-proxy-v2 check port 21
listen ftp-data
bind :60000-60250
mode tcp
balance source
server services-a 192.168.1.71 check port 21
server services-b 192.168.1.72 check port 21
</pre>
<p>
<hr>
<font size=2><b><i>
© Copyright 2013-2021 TJ Saunders<br>
All Rights Reserved<br>
</i></b></font>
<hr>
</body>
</html>
|