|
| 1 | +# -*- coding: binary -*- |
| 2 | +require 'rex/post/meterpreter' |
| 3 | + |
| 4 | +module Rex |
| 5 | +module Post |
| 6 | +module Meterpreter |
| 7 | +module Ui |
| 8 | + |
| 9 | +### |
| 10 | +# |
| 11 | +# Python extension - interact with a python interpreter |
| 12 | +# |
| 13 | +### |
| 14 | +class Console::CommandDispatcher::Python |
| 15 | + |
| 16 | + Klass = Console::CommandDispatcher::Python |
| 17 | + |
| 18 | + include Console::CommandDispatcher |
| 19 | + |
| 20 | + # |
| 21 | + # Name for this dispatcher |
| 22 | + # |
| 23 | + def name |
| 24 | + 'Python' |
| 25 | + end |
| 26 | + |
| 27 | + # |
| 28 | + # List of supported commands. |
| 29 | + # |
| 30 | + def commands |
| 31 | + { |
| 32 | + 'python_reset' => 'Resets/restarts the Python interpreter', |
| 33 | + 'python_execute' => 'Execute a python command string', |
| 34 | + 'python_import' => 'Import/run a python file or module' |
| 35 | + } |
| 36 | + end |
| 37 | + |
| 38 | + def cmd_python_reset(*args) |
| 39 | + client.python.reset |
| 40 | + print_good('Python interpreter successfully reset') |
| 41 | + end |
| 42 | + |
| 43 | + @@python_import_opts = Rex::Parser::Arguments.new( |
| 44 | + '-h' => [false, 'Help banner'], |
| 45 | + '-f' => [true, 'Path to the file (.py, .pyc), or module directory to import'], |
| 46 | + '-n' => [true, 'Name of the module (optional, for single files only)'], |
| 47 | + '-r' => [true, 'Name of the variable containing the result (optional, single files only)'] |
| 48 | + ) |
| 49 | + |
| 50 | + def python_import_usage |
| 51 | + print_line('Usage: python_import <-f file path> [-n mod name] [-r result var name]') |
| 52 | + print_line |
| 53 | + print_line('Loads a python code file or module from disk into memory on the target.') |
| 54 | + print_line('The module loader requires a path to a folder that contains the module,') |
| 55 | + print_line('and the folder name will be used as the module name. Only .py files will') |
| 56 | + print_line('work with modules.') |
| 57 | + print_line(@@python_import_opts.usage) |
| 58 | + end |
| 59 | + |
| 60 | + # |
| 61 | + # Import/run a python file |
| 62 | + # |
| 63 | + def cmd_python_import(*args) |
| 64 | + if args.length == 0 || args.include?('-h') |
| 65 | + python_import_usage |
| 66 | + return false |
| 67 | + end |
| 68 | + |
| 69 | + result_var = nil |
| 70 | + source = nil |
| 71 | + mod_name = nil |
| 72 | + |
| 73 | + @@python_import_opts.parse(args) { |opt, idx, val| |
| 74 | + case opt |
| 75 | + when '-f' |
| 76 | + source = val |
| 77 | + when '-n' |
| 78 | + mod_name = val |
| 79 | + when '-r' |
| 80 | + result_var = val |
| 81 | + end |
| 82 | + } |
| 83 | + |
| 84 | + unless source |
| 85 | + print_error("The -f parameter must be specified") |
| 86 | + return false |
| 87 | + end |
| 88 | + |
| 89 | + if ::File.directory?(source) |
| 90 | + files = ::Find.find(source).select { |p| /.*\.py$/ =~ p } |
| 91 | + if files.length == 0 |
| 92 | + fail_with("No .py files found in #{source}") |
| 93 | + end |
| 94 | + |
| 95 | + base_name = ::File.basename(source) |
| 96 | + unless source.end_with?('/') |
| 97 | + source << '/' |
| 98 | + end |
| 99 | + |
| 100 | + print_status("Importing #{source} with base module name #{base_name} ...") |
| 101 | + |
| 102 | + files.each do |file| |
| 103 | + rel_path = file[source.length, file.length - source.length] |
| 104 | + parts = rel_path.split('/') |
| 105 | + |
| 106 | + mod_parts = [base_name] + parts[0, parts.length - 1] |
| 107 | + |
| 108 | + if parts[-1] != '__init__.py' |
| 109 | + mod_parts << ::File.basename(parts[-1], '.*') |
| 110 | + end |
| 111 | + |
| 112 | + mod_name = mod_parts.join('.') |
| 113 | + print_status("Importing #{file} as #{mod_name} ...") |
| 114 | + result = client.python.import(file, mod_name, nil) |
| 115 | + handle_exec_result(result, nil) |
| 116 | + end |
| 117 | + else |
| 118 | + print_status("Importing #{source} ...") |
| 119 | + result = client.python.import(source, mod_name, result_var) |
| 120 | + handle_exec_result(result, result_var) |
| 121 | + end |
| 122 | + |
| 123 | + end |
| 124 | + |
| 125 | + @@python_execute_opts = Rex::Parser::Arguments.new( |
| 126 | + '-h' => [false, 'Help banner'], |
| 127 | + '-r' => [true, 'Name of the variable containing the result (optional)'] |
| 128 | + ) |
| 129 | + |
| 130 | + def python_execute_usage |
| 131 | + print_line('Usage: python_execute <python code> [-r result var name]') |
| 132 | + print_line |
| 133 | + print_line('Runs the given python string on the target. If a result is required,') |
| 134 | + print_line('it should be stored in a python variable, and that variable should') |
| 135 | + print_line('passed using the -r parameter.') |
| 136 | + print_line(@@python_execute_opts.usage) |
| 137 | + end |
| 138 | + |
| 139 | + # |
| 140 | + # Execute a simple python command string |
| 141 | + # |
| 142 | + def cmd_python_execute(*args) |
| 143 | + if args.length == 0 || args.include?('-h') |
| 144 | + python_execute_usage |
| 145 | + return false |
| 146 | + end |
| 147 | + |
| 148 | + code = args.shift |
| 149 | + result_var = nil |
| 150 | + |
| 151 | + @@python_execute_opts.parse(args) { |opt, idx, val| |
| 152 | + case opt |
| 153 | + when '-r' |
| 154 | + result_var = val |
| 155 | + end |
| 156 | + } |
| 157 | + |
| 158 | + result = client.python.execute_string(code, result_var) |
| 159 | + |
| 160 | + handle_exec_result(result, result_var) |
| 161 | + end |
| 162 | + |
| 163 | +private |
| 164 | + |
| 165 | + def handle_exec_result(result, result_var) |
| 166 | + if result[:result] |
| 167 | + print_good("#{result_var} = #{result[:result]}") |
| 168 | + elsif result[:stdout].length == 0 and result[:stderr].length == 0 |
| 169 | + print_good("Command executed without returning a result") |
| 170 | + end |
| 171 | + |
| 172 | + if result[:stdout].length > 0 |
| 173 | + print_good("Content written to stdout:\n#{result[:stdout]}") |
| 174 | + end |
| 175 | + |
| 176 | + if result[:stderr].length > 0 |
| 177 | + print_error("Content written to stderr:\n#{result[:stderr]}") |
| 178 | + end |
| 179 | + end |
| 180 | + |
| 181 | +end |
| 182 | + |
| 183 | +end |
| 184 | +end |
| 185 | +end |
| 186 | +end |
| 187 | + |
0 commit comments