class Rex::Powershell::Script

Attributes

code[RW]
functions[R]
rig[R]

Public Class Methods

code_modifiers() click to toggle source

Return list of code modifier methods

@return [Array] Code modifiers

# File lib/rex/powershell/script.rb, line 91
def self.code_modifiers
  instance_methods.select { |m| m =~ /^(strip|sub)/ }
end
new(code) click to toggle source
# File lib/rex/powershell/script.rb, line 33
def initialize(code)
  @code = ''
  @rig = Rex::RandomIdentifier::Generator.new

  begin
    # Open code file for reading
    fd = ::File.new(code || '', 'rb')
    while (line = fd.gets)
      @code << line
    end

    # Close open file
    fd.close
  rescue Errno::ENAMETOOLONG, Errno::ENOENT
    # Treat code as a... code
    @code = code.to_s.dup # in case we're eating another script
  end
  @functions = get_func_names.map { |f| get_func(f) }
end
to_byte_array(input_data, var_name = Rex::Text.rand_text_alpha(rand(3) + 3)) click to toggle source

Convert binary to byte array, read from file if able

@param input_data [String] Path to powershell file or powershell

code string

@param var_name [String] Byte array variable name

@return [String] input_data as a powershell byte array

# File lib/rex/powershell/script.rb, line 65
def self.to_byte_array(input_data, var_name = Rex::Text.rand_text_alpha(rand(3) + 3))
  # File will raise an exception if the path contains null byte
  if input_data.include? "\x00"
    code = input_data
  else
    code = ::File.file?(input_data) ? ::File.read(input_data) : input_data
  end

  code = code.unpack('C*')
  psh = "[Byte[]] $#{var_name} = 0x#{code[0].to_s(16)}"
  lines = []
  1.upto(code.length - 1) do |byte|
    if (byte % 10 == 0)
      lines.push "\r\n$#{var_name} += 0x#{code[byte].to_s(16)}"
    else
      lines.push ",0x#{code[byte].to_s(16)}"
    end
  end

  psh << lines.join('') + "\r\n"
end