From 81338225f05a3dd930a3d93560cccb79d7e2bad6 Mon Sep 17 00:00:00 2001 From: Ashley Donaldson Date: Tue, 16 Nov 2021 09:32:14 +1100 Subject: [PATCH] Respect path expansion when performing path-based tab completion --- .../meterpreter/extensions/stdapi/fs/file.rb | 3 +-- .../console/command_dispatcher/stdapi/fs.rb | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/rex/post/meterpreter/extensions/stdapi/fs/file.rb b/lib/rex/post/meterpreter/extensions/stdapi/fs/file.rb index be8378c3a8..68e9fca709 100644 --- a/lib/rex/post/meterpreter/extensions/stdapi/fs/file.rb +++ b/lib/rex/post/meterpreter/extensions/stdapi/fs/file.rb @@ -142,8 +142,7 @@ class File < Rex::Post::Meterpreter::Extensions::Stdapi::Fs::IO # First check for ~ path_components = path.split(separator) if path_components.length > 0 && path_components[0] == '~' - path_components[0] = '$HOME' - path = path_components.join(separator) + path = "$HOME#{path[1..-1]}" end # Now find the environment variables we'll need from the client diff --git a/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb b/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb index 08143b3596..d359eedcd2 100644 --- a/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb +++ b/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb @@ -1073,7 +1073,10 @@ class Console::CommandDispatcher::Stdapi::Fs def tab_complete_path(str, words, dir_only) if client.commands.include?(COMMAND_ID_STDAPI_FS_LS) - results = client.fs.dir.match(str, dir_only) rescue [] + expanded = str + expanded = client.fs.file.expand_path(expanded) if expanded =~ path_expand_regex + results = client.fs.dir.match(expanded, dir_only) rescue [] + results = unexpand_path_for_suggestions(str, expanded, results) if results.length == 1 && results[0] != str && results[0].end_with?(client.fs.file.separator) # If Readline receives a single value from this function, it will assume we're done with the tab # completing, and add an extra space at the end. @@ -1088,6 +1091,23 @@ class Console::CommandDispatcher::Stdapi::Fs end end + # + # After a path expansion followed by a tab completion suggestion set, + # unexpand the path back so that Readline is happy + # + def unexpand_path_for_suggestions(original_path, expanded_path, suggestions) + if original_path == expanded_path + suggestions + else + result = [] + suggestions.each do |suggestion| + addition = suggestion[expanded_path.length..-1] + result.append("#{original_path}#{addition}") + end + result + end + end + end end