|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Msf |
| 4 | + module Ui |
| 5 | + module Console |
| 6 | + module CommandDispatcher |
| 7 | + module Session |
| 8 | + include Rex::Ui::Text::DispatcherShell::CommandDispatcher |
| 9 | + |
| 10 | + @@irb_opts = Rex::Parser::Arguments.new( |
| 11 | + %w[-h --help] => [false, 'Help menu.' ], |
| 12 | + '-e' => [true, 'Expression to evaluate.'] |
| 13 | + ) |
| 14 | + def commands |
| 15 | + { |
| 16 | + '?' => 'Help menu', |
| 17 | + 'background' => 'Backgrounds the current session', |
| 18 | + 'bg' => 'Alias for background', |
| 19 | + 'exit' => 'Terminate the session', |
| 20 | + 'help' => 'Help menu', |
| 21 | + 'irb' => 'Open an interactive Ruby shell on the current session', |
| 22 | + 'pry' => 'Open the Pry debugger on the current session', |
| 23 | + 'quit' => 'Terminate the session', |
| 24 | + 'resource' => 'Run the commands stored in a file', |
| 25 | + 'uuid' => 'Get the UUID for the current session', |
| 26 | + 'sessions' => 'Quickly switch to another session' |
| 27 | + } |
| 28 | + end |
| 29 | + |
| 30 | + def cmd_background_help |
| 31 | + print_line('Usage: background') |
| 32 | + print_line |
| 33 | + print_line('Stop interacting with this session and return to the parent prompt') |
| 34 | + print_line |
| 35 | + end |
| 36 | + |
| 37 | + def cmd_background(*args) |
| 38 | + if args.include?('-h') || args.include?('--help') |
| 39 | + cmd_background_help |
| 40 | + return |
| 41 | + end |
| 42 | + print_status("Backgrounding session #{client.name}...") |
| 43 | + client.interacting = false |
| 44 | + end |
| 45 | + |
| 46 | + alias cmd_bg cmd_background |
| 47 | + alias cmd_bg_help cmd_background_help |
| 48 | + |
| 49 | + # |
| 50 | + # Terminates the session. |
| 51 | + # |
| 52 | + def cmd_exit(*args) |
| 53 | + print_status("Shutting down session: #{client.sid}") |
| 54 | + client.exit |
| 55 | + end |
| 56 | + |
| 57 | + alias cmd_quit cmd_exit |
| 58 | + |
| 59 | + def cmd_irb_help |
| 60 | + print_line('Usage: irb') |
| 61 | + print_line |
| 62 | + print_line('Open an interactive Ruby shell on the current session.') |
| 63 | + print @@irb_opts.usage |
| 64 | + end |
| 65 | + |
| 66 | + def cmd_irb_tabs(str, words) |
| 67 | + return [] if words.length > 1 |
| 68 | + |
| 69 | + @@irb_opts.option_keys |
| 70 | + end |
| 71 | + |
| 72 | + # |
| 73 | + # Open an interactive Ruby shell on the current session |
| 74 | + # |
| 75 | + def cmd_irb(*args) |
| 76 | + expressions = [] |
| 77 | + |
| 78 | + # Parse the command options |
| 79 | + @@irb_opts.parse(args) do |opt, _idx, val| |
| 80 | + case opt |
| 81 | + when '-e' |
| 82 | + expressions << val |
| 83 | + when '-h', '--help' |
| 84 | + return cmd_irb_help |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + session = client |
| 89 | + framework = client.framework |
| 90 | + |
| 91 | + if expressions.empty? |
| 92 | + print_status('Starting IRB shell...') |
| 93 | + print_status("You are in the \"client\" (session) object\n") |
| 94 | + framework.history_manager.with_context(name: :irb) do |
| 95 | + Rex::Ui::Text::IrbShell.new(client).run |
| 96 | + end |
| 97 | + else |
| 98 | + # XXX: No vprint_status here |
| 99 | + if framework.datastore['VERBOSE'].to_s == 'true' |
| 100 | + print_status("You are executing expressions in #{binding.receiver}") |
| 101 | + end |
| 102 | + |
| 103 | + expressions.each { |expression| eval(expression, binding) } |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + def cmd_pry_help |
| 108 | + print_line 'Usage: pry' |
| 109 | + print_line |
| 110 | + print_line 'Open the Pry debugger on the current session.' |
| 111 | + print_line |
| 112 | + end |
| 113 | + |
| 114 | + # |
| 115 | + # Open the Pry debugger on the current session |
| 116 | + # |
| 117 | + def cmd_pry(*args) |
| 118 | + if args.include?('-h') || args.include?('--help') |
| 119 | + cmd_pry_help |
| 120 | + return |
| 121 | + end |
| 122 | + |
| 123 | + begin |
| 124 | + require 'pry' |
| 125 | + rescue LoadError |
| 126 | + print_error('Failed to load Pry, try "gem install pry"') |
| 127 | + return |
| 128 | + end |
| 129 | + |
| 130 | + print_status('Starting Pry shell...') |
| 131 | + print_status("You are in the \"client\" (session) object\n") |
| 132 | + |
| 133 | + Pry.config.history_load = false |
| 134 | + client.framework.history_manager.with_context(history_file: Msf::Config.pry_history, name: :pry) do |
| 135 | + client.pry |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + def cmd_sessions_help |
| 140 | + print_line('Usage: sessions <id>') |
| 141 | + print_line |
| 142 | + print_line('Interact with a different session Id.') |
| 143 | + print_line('This works the same as calling this from the MSF shell: sessions -i <session id>') |
| 144 | + print_line |
| 145 | + end |
| 146 | + |
| 147 | + def cmd_sessions(*args) |
| 148 | + if args.empty? || args[0].to_i == 0 |
| 149 | + cmd_sessions_help |
| 150 | + elsif args[0].to_s == client.name.to_s |
| 151 | + print_status("Session #{client.name} is already interactive.") |
| 152 | + else |
| 153 | + print_status("Backgrounding session #{client.name}...") |
| 154 | + # store the next session id so that it can be referenced as soon |
| 155 | + # as this session is no longer interacting |
| 156 | + client.next_session = args[0] |
| 157 | + client.interacting = false |
| 158 | + end |
| 159 | + end |
| 160 | + |
| 161 | + def cmd_resource_help |
| 162 | + print_line 'Usage: resource path1 [path2 ...]' |
| 163 | + print_line |
| 164 | + print_line 'Run the commands stored in the supplied files. (- for stdin, press CTRL+D to end input from stdin)' |
| 165 | + print_line 'Resource files may also contain ERB or Ruby code between <ruby></ruby> tags.' |
| 166 | + print_line |
| 167 | + end |
| 168 | + |
| 169 | + def cmd_resource(*args) |
| 170 | + if args.empty? || args.include?('-h') || args.include?('--help') |
| 171 | + cmd_resource_help |
| 172 | + return false |
| 173 | + end |
| 174 | + |
| 175 | + args.each do |res| |
| 176 | + good_res = nil |
| 177 | + if res == '-' |
| 178 | + good_res = res |
| 179 | + elsif ::File.exist?(res) |
| 180 | + good_res = res |
| 181 | + elsif [ |
| 182 | + ::Msf::Config.script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter', |
| 183 | + ::Msf::Config.user_script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter' |
| 184 | + ].each do |dir| |
| 185 | + res_path = dir + ::File::SEPARATOR + res |
| 186 | + if ::File.exist?(res_path) |
| 187 | + good_res = res_path |
| 188 | + break |
| 189 | + end |
| 190 | + end |
| 191 | + # let's check to see if it's in the scripts/resource dir (like when tab completed) |
| 192 | + end |
| 193 | + unless good_res |
| 194 | + print_error("#{res} is not a valid resource file") |
| 195 | + next |
| 196 | + end |
| 197 | + |
| 198 | + client.console.load_resource(good_res) |
| 199 | + end |
| 200 | + end |
| 201 | + |
| 202 | + def cmd_resource_tabs(str, words) |
| 203 | + tabs = [] |
| 204 | + # return tabs if words.length > 1 |
| 205 | + if (str && str =~ (/^#{Regexp.escape(::File::SEPARATOR)}/)) |
| 206 | + # then you are probably specifying a full path so let's just use normal file completion |
| 207 | + return tab_complete_filenames(str, words) |
| 208 | + elsif (!(words[1]) || !words[1].match(%r{^/})) |
| 209 | + # then let's start tab completion in the scripts/resource directories |
| 210 | + begin |
| 211 | + [ |
| 212 | + ::Msf::Config.script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter', |
| 213 | + ::Msf::Config.user_script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter', |
| 214 | + '.' |
| 215 | + ].each do |dir| |
| 216 | + next if !::File.exist? dir |
| 217 | + |
| 218 | + tabs += ::Dir.new(dir).find_all do |e| |
| 219 | + path = dir + ::File::SEPARATOR + e |
| 220 | + ::File.file?(path) and ::File.readable?(path) |
| 221 | + end |
| 222 | + end |
| 223 | + rescue StandardError => e |
| 224 | + elog('Problem tab completing resource file names in the scripts/resource directories', error: e) |
| 225 | + end |
| 226 | + else |
| 227 | + tabs += tab_complete_filenames(str, words) |
| 228 | + end |
| 229 | + |
| 230 | + return tabs |
| 231 | + end |
| 232 | + end |
| 233 | + end |
| 234 | + end |
| 235 | + end |
| 236 | +end |
0 commit comments