Skip to content

Commit e024c11

Browse files
committed
Don't do any escaping on platforms with unknown escaping
1 parent 851beb7 commit e024c11

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

lib/metasploit/framework/ssh/platform.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def self.get_platform_info(ssh_socket, timeout: 10)
9494
info
9595
end
9696

97+
def self.is_posix(platform)
98+
return ['unifi','linux','osx','solaris','bsd','hpux','aix'].include?(platform)
99+
end
100+
97101
def self.get_platform_from_info(info)
98102
case info
99103
when /unifi\.version|UniFiSecurityGateway/i # Ubiquiti Unifi. uname -a is left in, so we got to pull before Linux

lib/msf/base/sessions/ssh_command_shell_bind.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def bootstrap(datastore = {}, handler = nil)
240240
@platform = Metasploit::Framework::Ssh::Platform.get_platform(ssh_connection)
241241
if @platform == 'windows'
242242
extend(Msf::Sessions::WindowsEscaping)
243-
else
243+
elsif Metasploit::Framework::Ssh::Platform.is_posix(@platform)
244244
extend(Msf::Sessions::UnixEscaping)
245245
end
246246

lib/msf/base/sessions/windows_escaping.rb

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,34 @@ def escape_cmd(executable)
2828
# to cmd.exe, whereas this is expected to be passed directly to the Win32 API, anticipating that it
2929
# will in turn be interpreted by CommandLineToArgvW.
3030
def argv_to_commandline(args)
31-
escaped_args = args.map do |arg|
32-
escape_arg(arg)
33-
end
31+
escaped_args = args.map { |arg| escape_arg(arg) }
3432

3533
escaped_args.join(' ')
3634
end
3735

3836
# Escape an individual argument per Windows shell rules
3937
# @param arg [String] Shell argument
4038
def escape_arg(arg)
41-
needs_quoting = space_chars.any? do |char|
42-
arg.include?(char)
43-
end
39+
needs_quoting = space_chars.any? { |char| arg.include?(char) }
4440

45-
# Fix the weird behaviour when backslashes are treated differently when immediately prior to a double-quote
46-
# We need to send double the number of backslashes to make it work as expected
47-
# See: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw#remarks
48-
arg = arg.gsub(/(\\*)"/, '\\1\\1"')
41+
# Fix the weird behaviour when backslashes are treated differently when immediately prior to a double-quote
42+
# We need to send double the number of backslashes to make it work as expected
43+
# See: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw#remarks
44+
arg = arg.gsub(/(\\*)"/, '\\1\\1"')
4945

50-
# Quotes need to be escaped
51-
arg = arg.gsub('"', '\\"')
46+
# Quotes need to be escaped
47+
arg = arg.gsub('"', '\\"')
5248

53-
if needs_quoting
54-
# At the end of the argument, we're about to add another quote - so any backslashes need to be doubled here too
55-
arg = arg.gsub(/(\\*)$/, '\\1\\1')
56-
arg = "\"#{arg}\""
57-
end
49+
if needs_quoting
50+
# At the end of the argument, we're about to add another quote - so any backslashes need to be doubled here too
51+
arg = arg.gsub(/(\\*)$/, '\\1\\1')
52+
arg = "\"#{arg}\""
53+
end
5854

59-
# Empty string needs to be coerced to have a value
60-
arg = '""' if arg == ''
55+
# Empty string needs to be coerced to have a value
56+
arg = '""' if arg == ''
6157

62-
arg
58+
arg
6359
end
6460

6561
# Convert the executable and argument array to a command that can be run in this command shell

0 commit comments

Comments
 (0)