-
Notifications
You must be signed in to change notification settings - Fork 13.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New process launch API #19108
Open
smashery
wants to merge
13
commits into
rapid7:master
Choose a base branch
from
smashery:new_cmd_exec
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
New process launch API #19108
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
38cb15d
Implement new cmd_exec API for PowerShell
smashery 3ce5b66
Implement new cmd_exec API for Windows cmd
smashery 725accf
Add unix shell to create_process API
smashery 580f8ca
Reworked unix create_process, as it was buggy
smashery eab9e23
create_process works for basic CommandShell instances
smashery 1700516
Handle CommandLineToArgv behaviour
smashery 6967d28
Comment function
smashery e9972ff
Changes from code review
smashery fece91e
Fix error sending legacy args
smashery 20f547f
Treat old-style path separately to new (unescaped) path
smashery 5ba717b
Rework unix command line based on testing
smashery c3dfc72
Fix bug when no arguments are present
smashery c4bf2da
Remove accidentally committed changes
smashery File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,113 @@ | ||
|
||
module Msf::Sessions | ||
|
||
class CommandShellWindows < CommandShell | ||
def initialize(*args) | ||
self.platform = "windows" | ||
super | ||
end | ||
|
||
def shell_command_token(cmd,timeout = 10) | ||
shell_command_token_win32(cmd,timeout) | ||
end | ||
|
||
# Convert the executable and argument array to a command that can be run in this command shell | ||
# @param executable [String] The process to launch | ||
# @param args [Array<String>] The arguments to the process | ||
def to_cmd(executable, args) | ||
self.class.to_cmd(executable, args) | ||
end | ||
|
||
# Escape a process for the command line | ||
# @param executable [String] The process to launch | ||
def self.escape_cmd(executable) | ||
space_chars = [' ', '\t', '\v'] | ||
needs_quoting = space_chars.any? do |char| | ||
executable.include?(char) | ||
end | ||
|
||
if needs_quoting | ||
executable = "\"#{executable}\"" | ||
end | ||
|
||
executable | ||
end | ||
|
||
# Convert the executable and argument array to a commandline that can be passed to CreateProcessAsUserW. | ||
# @param args [Array<String>] The arguments to the process | ||
# @remark The difference between this and `to_cmd` is that the output of `to_cmd` is expected to be passed | ||
# to cmd.exe, whereas this is expected to be passed directly to the Win32 API, anticipating that it | ||
# will in turn be interpreted by CommandLineToArgvW. | ||
def self.argv_to_commandline(args) | ||
space_chars = [' ', '\t', '\v'] | ||
escaped_args = args.map do |arg| | ||
needs_quoting = space_chars.any? do |char| | ||
arg.include?(char) | ||
end | ||
|
||
# Fix the weird behaviour when backslashes are treated differently when immediately prior to a double-quote | ||
# We need to send double the number of backslashes to make it work as expected | ||
# See: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw#remarks | ||
arg = arg.gsub(/(\\*)"/, '\\1\\1"') | ||
|
||
# Quotes need to be escaped | ||
arg = arg.gsub('"', '\\"') | ||
|
||
if needs_quoting | ||
# At the end of the argument, we're about to add another quote - so any backslashes need to be doubled here too | ||
arg = arg.gsub(/(\\*)$/, '\\1\\1') | ||
arg = "\"#{arg}\"" | ||
end | ||
|
||
# Empty string needs to be coerced to have a value | ||
arg = '""' if arg == '' | ||
|
||
arg | ||
end | ||
|
||
escaped_args.join(' ') | ||
end | ||
|
||
# Convert the executable and argument array to a command that can be run in this command shell | ||
# @param executable [String] The process to launch | ||
# @param args [Array<String>] The arguments to the process | ||
def self.to_cmd(executable, args) | ||
# The space, caret and quote chars need to be inside double-quoted strings. | ||
# The percent character needs to be escaped using a caret char, while being outside a double-quoted string. | ||
# | ||
# Situations where these two situations combine are going to be the trickiest cases: something that has quote-requiring | ||
# characters (e.g. spaces), but which also needs to avoid expanding an environment variable. In this case, | ||
# the string needs to end up being partially quoted; with parts of the string in quotes, but others (i.e. bits with percents) not. | ||
# For example: | ||
# 'env var is %temp%, yes, %TEMP%' needs to end up as '"env var is "^%temp^%", yes, "^%TEMP^%' | ||
# | ||
# There is flexibility in how you might implement this, but I think this one looks the most "human" to me, | ||
# which would make it less signaturable. | ||
# | ||
# To do this, we'll consider each argument character-by-character. Each time we encounter a percent sign, we break out of any quotes | ||
# (if we've been inside them in the current "token"), and then start a new "token". | ||
|
||
cmd_and_args = [executable] + args | ||
quote_requiring = ['"', '^', ' ', "\t", "\v", '&', '<', '>', '|'] | ||
|
||
escaped_cmd_and_args = cmd_and_args.map do |arg| | ||
# Double-up all quote chars | ||
arg = arg.gsub('"', '""') | ||
|
||
result = CommandShell._glue_cmdline_escape(arg, quote_requiring, '%', '^%', '"') | ||
|
||
# Fix the weird behaviour when backslashes are treated differently when immediately prior to a double-quote | ||
# We need to send double the number of backslashes to make it work as expected | ||
# See: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw#remarks | ||
result.gsub!(/(\\*)"/, '\\1\\1"') | ||
|
||
# Empty string needs to be coerced to have a value | ||
result = '""' if result == '' | ||
|
||
result | ||
end | ||
|
||
escaped_cmd_and_args.join(' ') | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems strange
InMemory
could be both a String and a Boolean, this could lead to some confusion. Does it need to be a String?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely agree that it's strange. For clarity, I didn't change the expected types - I'm just commenting the existing (admittedly confusing) behaviour.