Method: Rex::Powershell::Command.generate_psh_args

Defined in:
lib/rex/powershell/command.rb

.generate_psh_args(opts) ⇒ String

Generate arguments for the powershell command The format will be have no space at the start and have a space afterwards e.g. “-Arg1 x -Arg -Arg x ”

Parameters:

  • opts (Hash)

    The options to generate the command line

Options Hash (opts):

  • :shorten (Boolean)

    Whether to shorten the powershell arguments (v2.0 or greater)

  • :encodedcommand (String)

    Powershell script as an encoded command (-EncodedCommand)

  • :executionpolicy (String)

    The execution policy (-ExecutionPolicy)

  • :inputformat (String)

    The input format (-InputFormat)

  • :file (String)

    The path to a powershell file (-File)

  • :noexit (Boolean)

    Whether to exit powershell after execution (-NoExit)

  • :nologo (Boolean)

    Whether to display the logo (-NoLogo)

  • :noninteractive (Boolean)

    Whether to load a non interactive powershell (-NonInteractive)

  • :mta (Boolean)

    Whether to run as Multi-Threaded Apartment (-Mta)

  • :outputformat (String)

    The output format (-OutputFormat)

  • :sta (Boolean)

    Whether to run as Single-Threaded Apartment (-Sta)

  • :noprofile (Boolean)

    Whether to use the current users powershell profile (-NoProfile)

  • :windowstyle (String)

    The window style to use (-WindowStyle)

Returns:

  • (String)

    Powershell command arguments



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rex/powershell/command.rb', line 108

def self.generate_psh_args(opts)
  return '' unless opts

  unless opts.key? :shorten
    opts[:shorten] = (opts[:method] != 'old')
  end

  arg_string = ' '
  opts.each_pair do |arg, value|
    case arg
      when :encodedcommand
        arg_string << "-EncodedCommand #{value} " if value
      when :executionpolicy
        arg_string << "-ExecutionPolicy #{value} " if value
      when :inputformat
        arg_string << "-InputFormat #{value} " if value
      when :file
        arg_string << "-File #{value} " if value
      when :noexit
        arg_string << '-NoExit ' if value
      when :nologo
        arg_string << '-NoLogo ' if value
      when :noninteractive
        arg_string << '-NonInteractive ' if value
      when :mta
        arg_string << '-Mta ' if value
      when :outputformat
        arg_string << "-OutputFormat #{value} " if value
      when :sta
        arg_string << '-Sta ' if value
      when :noprofile
        arg_string << '-NoProfile ' if value
      when :windowstyle
        arg_string << "-WindowStyle #{value} " if  value
    end
  end

  # Command must be last (unless from stdin - etc)
  if opts[:command]
    arg_string << "-Command #{opts[:command]}"
  end

  # Shorten arg if PSH 2.0+
  if opts[:shorten]
    # Invoke-Command and Out-File require these options to have
    # an additional space before to prevent Powershell code being
    # mangled.
    arg_string.gsub!(' -Command ', ' -c ')
    arg_string.gsub!('-EncodedCommand ', '-e ')
    arg_string.gsub!('-ExecutionPolicy ', '-ep ')
    arg_string.gsub!(' -File ', ' -f ')
    arg_string.gsub!('-InputFormat ', '-i ')
    arg_string.gsub!('-NoExit ', '-noe ')
    arg_string.gsub!('-NoLogo ', '-nol ')
    arg_string.gsub!('-NoProfile ', '-nop ')
    arg_string.gsub!('-NonInteractive ', '-noni ')
    arg_string.gsub!('-OutputFormat ', '-o ')
    arg_string.gsub!('-Sta ', '-s ')
    arg_string.gsub!('-WindowStyle ', '-w ')
  end

  # Strip off first space character
  arg_string = arg_string[1..-1]
  # Remove final space character
  arg_string = arg_string[0..-2] if (arg_string[-1] == ' ')

  arg_string
end