Author Topic: Windows backslash replacement, yes again but with a twist  (Read 103 times)

Offline SimonSlaytor

  • New user
  • *
  • Posts: 3
    • View Profile
Windows backslash replacement, yes again but with a twist
« on: March 07, 2017, 10:18:34 pm »
Hi Folks,

Sorry to drag this old chestnut up again.

We are trying to replacing an old Windoze based FTP server with ProFTPD running on Debian, all good! The FTP server is used as patch source for a Windows based computer game and uses the now customary \ instead of / when traversing the FTP server.

Using a slightly modded version of the configuration found in this forum I can happily change this like cd \Patches to cd /Patches, all good. However annoyingly the gaming client issues a CD \ as it's first command, my guess is to escape a chroot'ed folder on the previous server. Unfortunately I can't seem to trap / replace this command and the resulting error causes the client to fail.

Any suggestions gratefully received.

Below is a manual example:

ftp x.x.x.x
Connected to x.x.x.x
220 ProFTPD 1.3.5d Server (Welcome to the EU x.x.x. Patching FTP service.) [10.1.13.15]
Name (x.x.x.x:root): ftp
331 Anonymous login ok, send your complete email address as your password
Password:
230 Anonymous access granted, restrictions apply
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd \FTP_EraFableClientUpdate
250 CWD command successful
ftp> pwd
Remote directory: /FTP_EraFableClientUpdate
ftp> cd ..\
250 CWD command successful
ftp> pwd
Remote directory: /
ftp> cd \FTP_EraFableClientUpdate
250 CWD command successful
ftp> pwd
Remote directory: /FTP_EraFableClientUpdate
ftp> cd \          <---------- This is the command that breaks
(remote-directory)
501 Invalid number of arguments
ftp> pwd
Remote directory: /FTP_EraFableClientUpdate
ftp> quit
221 Goodbye.

Config snippet:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteLog /var/log/pfrewrite.log
RewriteMap replace int:replaceall
RewriteCondition %m CWD|STOR|PUT|GET
RewriteRule ^(.*) "${replace:!$1!\\\\!/}"
</IfModule>


Log:

017-03-07 22:09:17,493 mod_rewrite/0.9[30428]: rewrite_fixup(): skipping EPSV (no arg)
2017-03-07 22:09:17,630 mod_rewrite/0.9[30428]: rewrite_fixup(): skipping NLST (no arg)
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_fixup(): found RewriteRule
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_fixup(): examining RewriteRule conditions
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_match_cond(): original cond: '%m'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): original pattern: '%m'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): rule backref subst'd pattern: '%m'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): pattern '%m' had no cond backrefs
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst_vars(): replacing variable '%m' with 'CWD'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): var subst'd pattern: 'CWD'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_parse_map_str(): parsing 'CWD'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): maps subst'd pattern: 'CWD'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): env subst'd pattern: 'CWD'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_match_cond: subst'd cond: 'CWD'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_match_cond(): checking regex cond against 'CWD'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_fixup(): condition met
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_fixup(): executing RewriteRule
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): original pattern: '${replace:!$1!\\!/}'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst_backrefs(): replacing backref '$1' with 'FTP_EraFableClientUpdate'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): rule backref subst'd pattern: '${replace:!FTP_EraFableClientUpdate!\\!/}'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): pattern '${replace:!$1!\\!/}' had no cond backrefs
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): var subst'd pattern: '${replace:!FTP_EraFableClientUpdate!\\!/}'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_parse_map_str(): parsing '${replace:!FTP_EraFableClientUpdate!\\!/}'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst_maps(): map name: 'replace'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst_maps(): lookup key: '!FTP_EraFableClientUpdate!\\!/'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst_maps(): default value: ''
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst_maps(): mapping '!FTP_EraFableClientUpdate!\\!/' using 'replace'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_map_int_replaceall(): actual key: 'FTP_EraFableClientUpdate'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_map_int_replaceall(): replacing '\\' with '/'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_map_int_replaceall(): '\\' does not occur in given key 'FTP_EraFableClientUpdate'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst_maps(): internal map 'replace' returned ''
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst_maps(): substituting '' for '${replace:!FTP_EraFableClientUpdate!\\!/}'
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_parse_map_str(): parsing ''
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): maps subst'd pattern: ''
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_subst(): env subst'd pattern: ''
2017-03-07 22:09:18,727 mod_rewrite/0.9[30428]: rewrite_fixup(): error processing RewriteRule
2017-03-07 22:09:18,793 mod_rewrite/0.9[30428]: rewrite_fixup(): skipping PWD (no arg)
2017-03-07 22:09:24,428 mod_rewrite/0.9[30428]: rewrite_fixup(): skipping CWD (no arg)
2017-03-07 22:09:29,912 mod_rewrite/0.9[30428]: rewrite_fixup(): skipping QUIT (no arg)

Offline castaglia

  • Administrator
  • Support Hero
  • *****
  • Posts: 5254
    • View Profile
    • http://www.castaglia.org/
Re: Windows backslash replacement, yes again but with a twist
« Reply #1 on: March 07, 2017, 10:49:16 pm »
Well, first:

  RewriteCondition %m CWD|STOR|PUT|GET

Should be:

  RewriteCondition %m CWD|STOR|RETR

That is, "PUT" and "GET" are not actual FTP commands; they're commands that the client presents to users, but they are not the protocol-level commands sent on the wire.

Second, your reproduction recipe isn't quite right.  Using this:

  ftp> cd \

You'll see:

  (remote-directory)

emitted -- because the single backslash is being handled _ by the client there_ -- it's not sending that command to the server yet.  Instead, you might try:

  ftp> cd \\

so that you'll then see:

  550 \: No such file or directory

And then we can go from there.

Offline SimonSlaytor

  • New user
  • *
  • Posts: 3
    • View Profile
Re: Windows backslash replacement, yes again but with a twist
« Reply #2 on: March 08, 2017, 03:03:51 pm »
Thanks,

Interesting, I won't claim to have studied the man page in any detail I instead just pulled the module config from a previous Administrators post so assumed it was correct, I'll correct now.

If I understand you correctly you are saying the 501 error is being returned by the client i.e. the command is never sent to the server and hence isn't trapped and processed by the rewrite module.

The game client is issuing a CD \ and sadly I cannot change this behaviour however the error logged on the client is consistent with the error I reported initially. When I point the same client at a filezilla server running on windows the command returns the normal 250 success response. I've not tried pointing my linux ftp client at the filezilla box but from what you are suggesting it should result in the same error? i.e cd / works but cd \ doesn't? This wouldn't be consistent with what I'm seeing.

Why would the ftp client built into the game behave differently based on the differing servers?

If I issue the corresponding Linux friendly command the response is 'ok' for example:

230 Anonymous access granted, restrictions apply
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd /
250 CWD command successful
ftp> cd \
(remote-directory)
501 Invalid number of arguments
ftp>


« Last Edit: March 08, 2017, 03:08:23 pm by SimonSlaytor »

Offline castaglia

  • Administrator
  • Support Hero
  • *****
  • Posts: 5254
    • View Profile
    • http://www.castaglia.org/
Re: Windows backslash replacement, yes again but with a twist
« Reply #3 on: March 08, 2017, 11:55:20 pm »
I honestly think that trying to figure out behaviors of command-line FTP clients, with regard to this issue, are not as important as looking at the server logs to see how changing the configuration is helping address the root problem.

If you want to continue using the command-line FTP client, then _please_ use "CWD \\".  "cd" is another of those client-only commands that is NOT a real FTP command.  And you can *see* this in your RewriteLog; the actual command sent by the client is CWD, not "cd".
« Last Edit: March 08, 2017, 11:56:54 pm by castaglia »

Offline SimonSlaytor

  • New user
  • *
  • Posts: 3
    • View Profile
Re: Windows backslash replacement, yes again but with a twist
« Reply #4 on: March 11, 2017, 11:55:25 am »
Thanks for the feedback, I think you're missing the fundamental thing I'm testing with a client but what needs to work is the 'Video Game Software' which has the FTP functions 'built-in'.

It is because of this 'built-in' nature that I cannot change what it does.

Given the time constraints and your useful information that the errors are being thrown by the client I have abandoned ProFTP and used a Wndoze server running FileZilla and everything is up and running.

Thanks for the feedback, much appreciate but for now I have an non ideal solution.

 

sighted planning