| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>ProFTPD: Filters</title> |
| 5 | </head> |
| 6 | |
| 7 | <body bgcolor=white> |
| 8 | |
| 9 | <hr> |
| 10 | <center><h2><b>ProFTPD: Filters</b></h2></center> |
| 11 | <hr> |
| 12 | |
| 13 | <p> |
| 14 | ProFTPD supports the ability to "filter" the FTP commands it receives |
| 15 | from the client. These filters are written as |
| 16 | <a href="Regex.html">regular expressions</a>. This means that while the |
| 17 | filters can be very powerful, they can also be complex and harder to construct. |
| 18 | |
| 19 | <p> |
| 20 | First, there are the <a href="../modules/mod_core.html#AllowFilter"><code>AllowFilter</code></a> and <a href="../modules/mod_core.html#DenyFilter"><code>DenyFilter</code></a> configuration directives. These configuration directives are used |
| 21 | to set filters on <b>every</b> FTP command. If an <code>AllowFilter</code> is |
| 22 | used, the command parameters <b>must</b> match the given filter, otherwise the |
| 23 | command will be denied. If a <code>DenyFilter</code> is used, the command |
| 24 | parameters <b>must not</b> match the given filter, otherwise the command will |
| 25 | be denied. If both <code>AllowFilter</code> and <code>DenyFilter</code> are |
| 26 | used, then the <code>AllowFilter</code> will be checked first. |
| 27 | |
| 28 | <p> |
| 29 | Second, there are some special filter configuration directives aimed at those |
| 30 | FTP commands that cause changes to files and directories on the server system: <a href="../modules/mod_core.html#PathAllowFilter"><code>PathAllowFilter</code></a> and <a href="../modules/mod_core.html#PathDenyFilter"><code>PathDenyFilter</code></a>. Like <code>AllowFilter</code> and <code>DenyFilter</code>, the |
| 31 | <code>Allow</code> filter, if present, is checked first, then a |
| 32 | <code>DenyFilter</code>, if present. <code>PathAllowFilter</code> and |
| 33 | <code>PathDenyFilter</code> are checked <i>after</i> the |
| 34 | <code>AllowFilter</code> and <code>DenyFilter</code> directives. These |
| 35 | <code>Path</code> filters are only applied to the following FTP commands: |
| 36 | <code>DELE</code>, <code>MKD/XMKD</code>, <code>RMD/XRMD</code>, |
| 37 | <code>RNFR</code>, <code>RNTO</code>, <code>STOR</code>, <code>STOU</code>, |
| 38 | and to the <code>SITE</code> commands <code>CHGRP</code> and <code>CHMOD</code>. |
| 39 | Note that using both <code>PathAllowFilter</code> and |
| 40 | <code>PathDenyFilter</code> at the same time is not a good idea; only one |
| 41 | filter is generally needed. |
| 42 | |
| 43 | <p> |
| 44 | One property that often catches the unwary administrator is the fact that |
| 45 | <code>proftpd</code> only operates on the first <code>Filter</code> directive |
| 46 | defined in the configuration file; it does not cycle through multiple |
| 47 | <code>Filter</code> directives. This is because multiple regular expressions |
| 48 | can be combined into a single (albeit more complex) regular expression. The |
| 49 | alternation metacharacter is helpful in creating such combined regular |
| 50 | expressions. For example, if you had the following in your |
| 51 | <code>proftpd.conf</code>: |
| 52 | <pre> |
| 53 | PathAllowFilter \.jpg$ |
| 54 | PathAllowFilter \.jpeg$ |
| 55 | PathAllowFilter \.mpeg$ |
| 56 | PathAllowFilter \.mpg$ |
| 57 | PathAllowFilter \.mp3$ |
| 58 | </pre> |
| 59 | only the first <code>PathAllowFilter</code> would be enforced. To enforce |
| 60 | all Filter simultaneously, use: |
| 61 | <pre> |
| 62 | PathAllowFilter \.(jpg|jpgeg|mpeg|mpg|mp3)$ |
| 63 | </pre> |
| 64 | Matches are case-sensitive! Also note that if you surround your regular |
| 65 | expression in quotation (") marks, any backslash will itself need |
| 66 | to be escaped; ProFTPD's configuration parser interprets backslashes inside |
| 67 | of quoted strings a little differently. Thus, the example above would look |
| 68 | like this: |
| 69 | <pre> |
| 70 | PathAllowFilter "\\.(jpg|jpgeg|mpeg|mpg|mp3)$" |
| 71 | </pre> |
| 72 | if using quotation marks. |
| 73 | |
| 74 | <p> |
| 75 | Another characteristic to keep in mind is that Filters are only applied to |
| 76 | FTP command <i>parameters</i>, not to the FTP command itself. Most of the |
| 77 | time, this is not a problem. It would be useful sometimes, though, to |
| 78 | be able to filter parameters of commands other than those filtered by the |
| 79 | <code>Path</code> filter directives. It would be an easy change to the |
| 80 | source code; however, there is no feature request for the ability to do this |
| 81 | on <a href="http://bugs.proftpd.org">bugs.proftpd.org</a> with a good |
| 82 | justification for such a feature. If you can think of one, please open |
| 83 | such a request. |
| 84 | |
| 85 | <p> |
| 86 | When developing your Filter directives, it often helps to keep an eye on the |
| 87 | server <a href="Debugging.html">debugging</a> output, to see how well your |
| 88 | filters are being applied. |
| 89 | |
| 90 | <p> |
| 91 | <b>Examples</b><br> |
| 92 | To prevent clients from using paths which may contain non-printable characters |
| 93 | (<i>e.g.</i> CR, LF, VB, <i>etc</i>), you can use the following |
| 94 | <code>PathDenyFilter</code> pattern: |
| 95 | <pre> |
| 96 | PathDenyFilter [^[:print:]] |
| 97 | </pre> |
| 98 | Alternatively, you could use a <code>PathAllowFilter</code> which only |
| 99 | <i>allows</i> printable characters in paths: |
| 100 | <pre> |
| 101 | PathAllowFilter [[:print:]] |
| 102 | </pre> |
| 103 | And if you want to prevent spaces and tabs from appearing in paths, you |
| 104 | can use: |
| 105 | <pre> |
| 106 | PathDenyFilter [[:blank:]] |
| 107 | </pre> |
| 108 | |
| 109 | <p> |
| 110 | What if you want to prevent clients from uploading so-called "dot files", |
| 111 | <i>i.e.</i> files whose names start with a period ('.'), so that they are |
| 112 | "hidden"? To prevent uploading of all dot files, you can use the following |
| 113 | <code>PathDenyFilter</code> configuration: |
| 114 | <pre> |
| 115 | PathDenyFilter \.[^/]*$ |
| 116 | </pre> |
| 117 | |
| 118 | <p> |
| 119 | In ProFTPD 1.3.3rc1 and later, you can use the <code>AllowFilter</code> and |
| 120 | <code>DenyFilter</code> configuration directives inside of |
| 121 | <code><Limit></code> sections, so that those <code>Filter</code> |
| 122 | directives only apply to the FTP commands listed in the |
| 123 | <code><Limit></code> section. This means you can specify regular |
| 124 | expression filters for the arguments for specific commands. For example, |
| 125 | you may want to configure a directory that only allows uploads of files with |
| 126 | specific extensions. You <i>could</i> use <code>PathAllowFilter</code> for |
| 127 | this -- but <code>PathAllowFilter</code> also applies to the <code>MKD</code> |
| 128 | command, and you might want to allow users to create subdirectories in your |
| 129 | special directory. Thus you only want your regular expression to apply |
| 130 | to the <code>STOR</code> command in your directory. Below is an example |
| 131 | of how to do this using <code>AllowFilter</code>: |
| 132 | <pre> |
| 133 | <Directory <i>/path/to/dir</i>> |
| 134 | <Limit STOR> |
| 135 | Order deny, allow |
| 136 | AllowFilter \.<i>ext</i>$ |
| 137 | </Limit> |
| 138 | </Directory> |
| 139 | </pre> |
| 140 | The key is the <code>Order</code> directive; without it, the configuration |
| 141 | will not work as you expect. |
| 142 | |
| 143 | <p><a name="FAQ"> |
| 144 | <b>Frequently Asked Questions</b><br> |
| 145 | <font color=red>Question</font>: How can I prevent <code>SITE CHMOD</code> |
| 146 | commands which use the mode 777, but allow other <code>SITE CHMOD</code> |
| 147 | commands?<br> |
| 148 | <font color=blue>Answer</font>: For this, you would use the |
| 149 | <code>DenyFilter</code> directive, like so: |
| 150 | <pre> |
| 151 | <Limit SITE_CHMOD> |
| 152 | DenyFilter 777 |
| 153 | </Limit> |
| 154 | </pre> |
| 155 | However, the desired mode can also be expressed <i>symbolically</i>, using |
| 156 | <i>e.g.</i> "SITE CHMOD a+rwx <i>file</i>". So our filter should take this into |
| 157 | account, as well as some variants: |
| 158 | <pre> |
| 159 | <Limit SITE_CHMOD> |
| 160 | DenyFilter (777|a+rwx|u=rwx,g=rwx,o=rwx) |
| 161 | </Limit> |
| 162 | </pre> |
| 163 | Looking better, but still not perfect. A determined user could still work |
| 164 | around our filter by using a series of <code>SITE CHMOD</code> commands |
| 165 | like this: |
| 166 | <pre> |
| 167 | SITE CHMOD u+rwx <i>file</i> |
| 168 | SITE CHMOD g+rwx <i>file</i> |
| 169 | SITE CHMOD o+rwx <i>file</i> |
| 170 | </pre> |
| 171 | Or the determined user could make the change even more granular: |
| 172 | <pre> |
| 173 | SITE CHMOD u+r <i>file</i> |
| 174 | SITE CHMOD u+w <i>file</i> |
| 175 | SITE CHMOD u+x <i>file</i> |
| 176 | SITE CHMOD g+r <i>file</i> |
| 177 | SITE CHMOD g+w <i>file</i> |
| 178 | SITE CHMOD g+x <i>file</i> |
| 179 | SITE CHMOD o+r <i>file</i> |
| 180 | SITE CHMOD o+w <i>file</i> |
| 181 | SITE CHMOD o+x <i>file</i> |
| 182 | </pre> |
| 183 | Another trap is that the numbers "777" might appear in a legitimate file name, |
| 184 | and thus the following allowed command would fail unnecessarily: |
| 185 | <pre> |
| 186 | SITE CHMOD 644 ServiceOrder777.doc |
| 187 | </pre> |
| 188 | |
| 189 | <p> |
| 190 | In short, it is possible to create filters for portions of commands, but you |
| 191 | do need to be very careful to not inadvertently cause commands to fail that |
| 192 | should succeed. |
| 193 | |
| 194 | <p> |
| 195 | <hr> |
| 196 | <font size=2><b><i> |
| 197 | © Copyright 2017 The ProFTPD Project<br> |
| 198 | All Rights Reserved<br> |
| 199 | </i></b></font> |
| 200 | <hr> |
| 201 | |
| 202 | </body> |
| 203 | </html> |