Class: Rex::Post::Meterpreter::Ui::Console::CommandDispatcher::Python

Inherits:
Object
  • Object
show all
Includes:
Rex::Post::Meterpreter::Ui::Console::CommandDispatcher
Defined in:
lib/rex/post/meterpreter/ui/console/command_dispatcher/python.rb

Overview

Python extension - interact with a python interpreter

Constant Summary collapse

Klass =
Console::CommandDispatcher::Python
@@python_import_opts =
Rex::Parser::Arguments.new(
  '-h' => [false, 'Help banner'],
  '-f' => [true,  'Path to the file (.py, .pyc), or module directory to import'],
  '-n' => [true,  'Name of the module (optional, for single files only)'],
  '-r' => [true,  'Name of the variable containing the result (optional, single files only)']
)
@@python_execute_opts =
Rex::Parser::Arguments.new(
  '-h' => [false, 'Help banner'],
  '-r' => [true,  'Name of the variable containing the result (optional)']
)

Instance Attribute Summary

Attributes included from Ui::Text::DispatcherShell::CommandDispatcher

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Rex::Post::Meterpreter::Ui::Console::CommandDispatcher

check_hash, #client, #docs_dir, #filter_commands, #initialize, #log_error, #msf_loaded?, set_hash, #unknown_command

Methods included from Ui::Text::DispatcherShell::CommandDispatcher

#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #docs_dir, #help_to_s, included, #initialize, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_directory, #tab_complete_filenames, #tab_complete_generic, #tab_complete_source_address, #unknown_command, #update_prompt

Instance Method Details

#cmd_python_execute(*args) ⇒ Object

Execute a simple python command string



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/python.rb', line 142

def cmd_python_execute(*args)
  if args.length == 0 || args.include?('-h')
    python_execute_usage
    return false
  end

  code = args.shift
  result_var = nil

  @@python_execute_opts.parse(args) { |opt, idx, val|
    case opt
    when '-r'
      result_var = val
    end
  }

  result = client.python.execute_string(code, result_var)

  handle_exec_result(result, result_var)
end

#cmd_python_import(*args) ⇒ Object

Import/run a python file



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/python.rb', line 63

def cmd_python_import(*args)
  if args.length == 0 || args.include?('-h')
    python_import_usage
    return false
  end

  result_var = nil
  source = nil
  mod_name = nil

  @@python_import_opts.parse(args) { |opt, idx, val|
    case opt
    when '-f'
      source = val
    when '-n'
      mod_name = val
    when '-r'
      result_var = val
    end
  }

  unless source
    print_error("The -f parameter must be specified")
    return false
  end

  if ::File.directory?(source)
    files = ::Find.find(source).select { |p| /.*\.py$/ =~ p }
    if files.length == 0
      fail_with("No .py files found in #{source}")
    end

    base_name = ::File.basename(source)
    unless source.end_with?('/')
      source << '/'
    end

    print_status("Importing #{source} with base module name #{base_name} ...")

    files.each do |file|
      rel_path = file[source.length, file.length - source.length]
      parts = rel_path.split('/')

      mod_parts = [base_name] + parts[0, parts.length - 1]

      if parts[-1] != '__init__.py'
        mod_parts << ::File.basename(parts[-1], '.*')
      end

      mod_name = mod_parts.join('.')
      print_status("Importing #{file} as #{mod_name} ...")
      result = client.python.import(file, mod_name, nil)
      handle_exec_result(result, nil)
    end
  else
    print_status("Importing #{source} ...")
    result = client.python.import(source, mod_name, result_var)
    handle_exec_result(result, result_var)
  end

end

#cmd_python_reset(*args) ⇒ Object



38
39
40
41
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/python.rb', line 38

def cmd_python_reset(*args)
  client.python.reset
  print_good('Python interpreter successfully reset')
end

#commandsObject

List of supported commands.



30
31
32
33
34
35
36
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/python.rb', line 30

def commands
  {
    'python_reset'   => 'Resets/restarts the Python interpreter',
    'python_execute' => 'Execute a python command string',
    'python_import'  => 'Import/run a python file or module'
  }
end

#nameObject

Name for this dispatcher



23
24
25
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/python.rb', line 23

def name
  'Python'
end

#python_execute_usageObject



130
131
132
133
134
135
136
137
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/python.rb', line 130

def python_execute_usage
  print_line('Usage: python_execute <python code> [-r result var name]')
  print_line
  print_line('Runs the given python string on the target. If a result is required,')
  print_line('it should be stored in a python variable, and that variable should')
  print_line('passed using the -r parameter.')
  print_line(@@python_execute_opts.usage)
end

#python_import_usageObject



50
51
52
53
54
55
56
57
58
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/python.rb', line 50

def python_import_usage
  print_line('Usage: python_import <-f file path> [-n mod name] [-r result var name]')
  print_line
  print_line('Loads a python code file or module from disk into memory on the target.')
  print_line('The module loader requires a path to a folder that contains the module,')
  print_line('and the folder name will be used as the module name. Only .py files will')
  print_line('work with modules.')
  print_line(@@python_import_opts.usage)
end