Class: Rex::Exploitation::Powershell::Script

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Obfu, Output, Parser
Defined in:
lib/rex/exploitation/powershell/script.rb

Constant Summary

Constants included from Obfu

Obfu::EMPTY_LINE_REGEX, Obfu::MULTI_LINE_COMMENTS_REGEX, Obfu::SINGLE_LINE_COMMENTS_REGEX, Obfu::UNIX_EOL_REGEX, Obfu::WHITESPACE_REGEX, Obfu::WINDOWS_EOL_REGEX

Constants included from Parser

Parser::RESERVED_VARIABLE_NAMES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Obfu

#standard_subs, #strip_comments, #strip_empty_lines, #strip_whitespace, #sub_funcs, #sub_vars

Methods included from Parser

#block_extract, #get_func, #get_func_names, #get_string_literals, #get_var_names, #match_start, #scan_with_index

Methods included from Output

#compress_code, #decompress_code, #deflate_code, #encode_code, #gzip_code, #size, #to_s, #to_s_lineno

Constructor Details

#initialize(code) ⇒ Script

Returns a new instance of Script.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rex/exploitation/powershell/script.rb', line 35

def initialize(code)
  @code = ''
  @rig = Rex::RandomIdentifierGenerator.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

Instance Attribute Details

#codeObject

Returns the value of attribute code.



10
11
12
# File 'lib/rex/exploitation/powershell/script.rb', line 10

def code
  @code
end

#functionsObject (readonly)

Returns the value of attribute functions.



11
12
13
# File 'lib/rex/exploitation/powershell/script.rb', line 11

def functions
  @functions
end

#rigObject (readonly)

Returns the value of attribute rig.



11
12
13
# File 'lib/rex/exploitation/powershell/script.rb', line 11

def rig
  @rig
end

Class Method Details

.code_modifiersArray

Return list of code modifier methods

Returns:

  • (Array)

    Code modifiers



93
94
95
# File 'lib/rex/exploitation/powershell/script.rb', line 93

def self.code_modifiers
  instance_methods.select { |m| m =~ /^(strip|sub)/ }
end

.to_byte_array(input_data, var_name = Rex::Text.rand_text_alpha(rand(3) + 3)) ⇒ String

Convert binary to byte array, read from file if able

Parameters:

  • input_data (String)

    Path to powershell file or powershell code string

  • var_name (String) (defaults to: Rex::Text.rand_text_alpha(rand(3) + 3))

    Byte array variable name

Returns:

  • (String)

    input_data as a powershell byte array



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rex/exploitation/powershell/script.rb', line 67

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