Class: Msf::Ui::Console::CommandDispatcher::Developer
- Inherits:
-
Object
- Object
- Msf::Ui::Console::CommandDispatcher::Developer
- Includes:
- Msf::Ui::Console::CommandDispatcher
- Defined in:
- lib/msf/ui/console/command_dispatcher/developer.rb
Constant Summary collapse
- @@irb_opts =
Rex::Parser::Arguments.new( '-h' => [false, 'Help menu.' ], '-e' => [true, 'Expression to evaluate.'] )
- @@time_opts =
Rex::Parser::Arguments.new( ['-h', '--help'] => [ false, 'Help banner.' ], '--cpu' => [false, 'Profile the CPU usage.'], '--memory' => [false, 'Profile the memory usage.'] )
- @@_servicemanager_opts =
Rex::Parser::Arguments.new( ['-l', '--list'] => [false, 'View the currently running services' ] )
- @@_historymanager_opts =
Rex::Parser::Arguments.new( '-h' => [false, 'Help menu.' ], ['-l', '--list'] => [true, 'View the current history manager contexts.'], ['-d', '--debug'] => [true, 'Debug the current history manager contexts.'] )
Instance Attribute Summary
Attributes included from Msf::Ui::Console::CommandDispatcher
Attributes included from Rex::Ui::Text::DispatcherShell::CommandDispatcher
Instance Method Summary collapse
-
#cmd__historymanager(*args) ⇒ Object
Interact with framework’s history manager.
- #cmd__historymanager_help ⇒ Object
-
#cmd__historymanager_tabs(_str, words) ⇒ Object
Tab completion for the _historymanager command.
-
#cmd__servicemanager(*args) ⇒ Object
Interact with framework’s service manager.
- #cmd__servicemanager_help ⇒ Object
-
#cmd__servicemanager_tabs(_str, words) ⇒ Object
Tab completion for the _servicemanager command.
-
#cmd_edit(*args) ⇒ Object
Edit the current module or a file with the preferred editor.
- #cmd_edit_help ⇒ Object
-
#cmd_edit_tabs(str, words) ⇒ Object
Tab completion for the edit command.
-
#cmd_irb(*args) ⇒ Object
Open an interactive Ruby shell in the current context.
- #cmd_irb_help ⇒ Object
-
#cmd_irb_tabs(_str, words) ⇒ Object
Tab completion for the irb command.
-
#cmd_log(*args) ⇒ Object
Display framework.log paged to the end if possible.
- #cmd_log_help ⇒ Object
-
#cmd_pry(*args) ⇒ Object
Open the Pry debugger on the current module or Framework.
- #cmd_pry_help ⇒ Object
-
#cmd_reload_lib(*args) ⇒ Object
Reload Ruby library files from specified paths.
- #cmd_reload_lib_help ⇒ Object
-
#cmd_reload_lib_tabs(str, words) ⇒ Object
Tab completion for the reload_lib command.
-
#cmd_time(*args) ⇒ Object
Time how long in seconds a command takes to execute.
- #cmd_time_help ⇒ Object
- #commands ⇒ Object
-
#initialize(driver) ⇒ Developer
constructor
A new instance of Developer.
- #local_editor ⇒ Object
- #local_pager ⇒ Object
-
#modified_file_paths(print_errors: true) ⇒ Array<String>
The list of modified file paths since startup.
- #modified_files ⇒ Object protected
- #name ⇒ Object
-
#reload_file(full_path, print_errors: true) ⇒ Object
XXX: This will try to reload any .rb and break on modules.
- #source_directories ⇒ Object protected
Methods included from Msf::Ui::Console::CommandDispatcher
#active_module, #active_module=, #active_session, #active_session=, #build_range_array, #docs_dir, #framework, #load_config, #log_error, #remove_lines
Methods included from Rex::Ui::Text::DispatcherShell::CommandDispatcher
#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #docs_dir, #help_to_s, included, #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
Constructor Details
#initialize(driver) ⇒ Developer
Returns a new instance of Developer.
28 29 30 31 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 28 def initialize(driver) super @modified_files = modified_file_paths(print_errors: false) end |
Instance Method Details
#cmd__historymanager(*args) ⇒ Object
Interact with framework’s history manager
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 405 def cmd__historymanager(*args) if args.include?('-h') || args.include?('--help') cmd__historymanager_help return false end opts = {} @@_historymanager_opts.parse(args) do |opt, idx, val| case opt when '-l', '--list' opts[:list] = true when '-d', '--debug' opts[:debug] = val.nil? ? true : val.downcase.start_with?(/t|y/) end end if opts.empty? opts[:list] = true end if opts.key?(:debug) framework.history_manager._debug = opts[:debug] print_status("HistoryManager debugging is now #{opts[:debug] ? 'on' : 'off'}") end if opts[:list] table = Rex::Text::Table.new( 'Header' => 'History contexts', 'Indent' => 1, 'Columns' => ['Id', 'File', 'Name'] ) framework.history_manager._contexts.each.with_index do |context, id| table << [id, context[:history_file], context[:name]] end if table.rows.empty? print_status("No history contexts present.") else print_line(table.to_s) end end end |
#cmd__historymanager_help ⇒ Object
457 458 459 460 461 462 463 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 457 def cmd__historymanager_help print_line 'Usage: _historymanager' print_line print_line 'Manage the history manager' print @@_historymanager_opts.usage print_line end |
#cmd__historymanager_tabs(_str, words) ⇒ Object
Tab completion for the _historymanager command
451 452 453 454 455 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 451 def cmd__historymanager_tabs(_str, words) return [] if words.length > 1 @@_historymanager_opts.option_keys end |
#cmd__servicemanager(*args) ⇒ Object
Interact with framework’s service manager
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 348 def cmd__servicemanager(*args) if args.include?('-h') || args.include?('--help') cmd__servicemanager_help return false end opts = {} @@_servicemanager_opts.parse(args) do |opt, idx, val| case opt when '-l', '--list' opts[:list] = true end end if opts.empty? opts[:list] = true end if opts[:list] table = Rex::Text::Table.new( 'Header' => 'Services', 'Indent' => 1, 'Columns' => ['Id', 'Name', 'References'] ) Rex::ServiceManager.instance.each.with_index do |(name, instance), id| # TODO: Update rex-core to support querying the reference count table << [id, name, instance.instance_variable_get(:@_references)] end if table.rows.empty? print_status("No framework services are currently running.") else print_line(table.to_s) end end end |
#cmd__servicemanager_help ⇒ Object
394 395 396 397 398 399 400 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 394 def cmd__servicemanager_help print_line 'Usage: _servicemanager' print_line print_line 'Manage running framework services' print @@_servicemanager_opts.usage print_line end |
#cmd__servicemanager_tabs(_str, words) ⇒ Object
Tab completion for the _servicemanager command
388 389 390 391 392 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 388 def cmd__servicemanager_tabs(_str, words) return [] if words.length > 1 @@_servicemanager_opts.option_keys end |
#cmd_edit(*args) ⇒ Object
Edit the current module or a file with the preferred editor
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 222 def cmd_edit(*args) editing_module = false if args.length > 0 path = File.(args[0]) elsif active_module editing_module = true path = active_module.file_path end unless path print_error('Nothing to edit. Try using a module first or specifying a library file to edit.') return end editor = local_editor unless editor # ed(1) is the standard editor editor = 'ed' print_warning("LocalEditor or $VISUAL/$EDITOR should be set. Falling back on #{editor}.") end # XXX: No vprint_status in this context? # XXX: VERBOSE is a string instead of Bool?? print_status("Launching #{editor} #{path}") if framework.datastore['VERBOSE'].to_s == 'true' unless system(*editor.split, path) print_error("Could not execute #{editor} #{path}") return end return if editing_module reload_file(path) end |
#cmd_edit_help ⇒ Object
209 210 211 212 213 214 215 216 217 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 209 def cmd_edit_help print_line 'Usage: edit [file/to/edit]' print_line print_line "Edit the currently active module or a local file with #{local_editor}." print_line 'To change the preferred editor, you can "setg LocalEditor".' print_line 'If a library file is specified, it will automatically be reloaded after editing.' print_line 'Otherwise, you can reload the active module with "reload" or "rerun".' print_line end |
#cmd_edit_tabs(str, words) ⇒ Object
Tab completion for the edit command
262 263 264 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 262 def cmd_edit_tabs(str, words) tab_complete_filenames(str, words) end |
#cmd_irb(*args) ⇒ Object
Open an interactive Ruby shell in the current context
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 118 def cmd_irb(*args) expressions = [] # Parse the command options @@irb_opts.parse(args) do |opt, idx, val| case opt when '-e' expressions << val when '-h' cmd_irb_help return false end end if expressions.empty? print_status('Starting IRB shell...') framework.history_manager.with_context(name: :irb) do begin if active_module print_status("You are in #{active_module.fullname}\n") Rex::Ui::Text::IrbShell.new(active_module).run else print_status("You are in the \"framework\" object\n") Rex::Ui::Text::IrbShell.new(framework).run end rescue print_error("Error during IRB: #{$!}\n\n#{$@.join("\n")}") end end # Reset tab completion if (driver.input.supports_readline) driver.input.reset_tab_completion end else # XXX: No vprint_status here either if framework.datastore['VERBOSE'].to_s == 'true' print_status("You are executing expressions in #{binding.receiver}") end expressions.each { |expression| eval(expression, binding) } end end |
#cmd_irb_help ⇒ Object
108 109 110 111 112 113 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 108 def cmd_irb_help print_line 'Usage: irb' print_line print_line 'Open an interactive Ruby shell in the current context.' print @@irb_opts.usage end |
#cmd_irb_tabs(_str, words) ⇒ Object
Tab completion for the irb command
166 167 168 169 170 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 166 def cmd_irb_tabs(_str, words) return [] if words.length > 1 @@irb_opts.option_keys end |
#cmd_log(*args) ⇒ Object
Display framework.log paged to the end if possible
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 325 def cmd_log(*args) path = File.join(Msf::Config.log_directory, 'framework.log') # XXX: +G isn't portable and may hang on large files pager = local_pager.to_s.include?('less') ? "#{local_pager} +G" : local_pager unless pager pager = 'tail -n 50' print_warning("LocalPager or $PAGER/$MANPAGER should be set. Falling back on #{pager}.") end # XXX: No vprint_status in this context? # XXX: VERBOSE is a string instead of Bool?? print_status("Launching #{pager} #{path}") if framework.datastore['VERBOSE'].to_s == 'true' unless system(*pager.split, path) print_error("Could not execute #{pager} #{path}") end end |
#cmd_log_help ⇒ Object
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 311 def cmd_log_help print_line 'Usage: log' print_line print_line 'Display framework.log paged to the end if possible.' print_line 'To change the preferred pager, you can "setg LocalPager".' print_line 'For full effect, "setg LogLevel 3" before running modules.' print_line print_line "Log location: #{File.join(Msf::Config.log_directory, 'framework.log')}" print_line end |
#cmd_pry(*args) ⇒ Object
Open the Pry debugger on the current module or Framework
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 182 def cmd_pry(*args) if args.include?('-h') cmd_pry_help return end begin require 'pry' rescue LoadError print_error('Failed to load Pry, try "gem install pry"') return end print_status('Starting Pry shell...') Pry.config.history_load = false framework.history_manager.with_context(history_file: Msf::Config.pry_history, name: :pry) do if active_module print_status("You are in the \"#{active_module.fullname}\" module object\n") active_module.pry else print_status("You are in the \"framework\" object\n") framework.pry end end end |
#cmd_pry_help ⇒ Object
172 173 174 175 176 177 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 172 def cmd_pry_help print_line 'Usage: pry' print_line print_line 'Open the Pry debugger on the current module or Framework.' print_line end |
#cmd_reload_lib(*args) ⇒ Object
Reload Ruby library files from specified paths
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 273 def cmd_reload_lib(*args) files = [] = OptionParser.new do |opts| opts. = 'Usage: reload_lib lib/to/reload.rb [...]' opts.separator '' opts.separator 'Reload Ruby library files from specified paths.' opts.separator '' opts.on '-h', '--help', 'Help banner.' do return print(opts.help) end opts.on '-a', '--all', 'Reload all* changed files in your current Git working tree. *Excludes modules and non-Ruby files.' do files.concat(modified_file_paths) end end # The remaining unparsed arguments are files files.concat(.order(args)) files.uniq! return print(.help) if files.empty? files.each do |file| reload_file(file) rescue ScriptError, StandardError => e print_error("Error while reloading file #{file.inspect}: #{e}:\n#{e.backtrace.to_a.map { |line| " #{line}" }.join("\n")}") end end |
#cmd_reload_lib_help ⇒ Object
266 267 268 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 266 def cmd_reload_lib_help cmd_reload_lib('-h') end |
#cmd_reload_lib_tabs(str, words) ⇒ Object
Tab completion for the reload_lib command
307 308 309 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 307 def cmd_reload_lib_tabs(str, words) tab_complete_filenames(str, words) end |
#cmd_time(*args) ⇒ Object
Time how long in seconds a command takes to execute
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 468 def cmd_time(*args) if args.empty? || args.first == '-h' || args.first == '--help' cmd_time_help return true end profiler = nil while args.first == '--cpu' || args.first == '--memory' profiler = args.shift end begin start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) command = Shellwords.shelljoin(args) case profiler when '--cpu' Metasploit::Framework::Profiler.record_cpu do driver.run_single(command) end when '--memory' Metasploit::Framework::Profiler.record_memory do driver.run_single(command) end else driver.run_single(command) end ensure end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) elapsed_time = end_time - start_time print_good("Command #{command.inspect} completed in #{elapsed_time} seconds") end end |
#cmd_time_help ⇒ Object
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 502 def cmd_time_help print_line 'Usage: time [options] [command]' print_line print_line 'Time how long a command takes to execute in seconds. Also supports profiling options.' print_line print_line ' Usage:' print_line ' * time db_import ./db_import.html' print_line ' * time show exploits' print_line ' * time reload_all' print_line ' * time missing_command' print_line ' * time --cpu db_import ./db_import.html' print_line ' * time --memory db_import ./db_import.html' print @@time_opts.usage print_line end |
#commands ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 37 def commands commands = { 'irb' => 'Open an interactive Ruby shell in the current context', 'pry' => 'Open the Pry debugger on the current module or Framework', 'edit' => 'Edit the current module or a file with the preferred editor', 'reload_lib' => 'Reload Ruby library files from specified paths', 'log' => 'Display framework.log paged to the end if possible', 'time' => 'Time how long it takes to run a particular command' } if framework.features.enabled?(Msf::FeatureManager::MANAGER_COMMANDS) commands['_servicemanager'] = 'Interact with the Rex::ServiceManager' commands['_historymanager'] = 'Interact with the Rex::Ui::Text::Shell::HistoryManager' end commands end |
#local_editor ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 53 def local_editor framework.datastore['LocalEditor'] || Rex::Compat.getenv('VISUAL') || Rex::Compat.getenv('EDITOR') || Msf::Util::Helper.which('vim') || Msf::Util::Helper.which('vi') end |
#local_pager ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 61 def local_pager framework.datastore['LocalPager'] || Rex::Compat.getenv('PAGER') || Rex::Compat.getenv('MANPAGER') || Msf::Util::Helper.which('less') || Msf::Util::Helper.which('more') end |
#modified_file_paths(print_errors: true) ⇒ Array<String>
Returns The list of modified file paths since startup.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 87 def modified_file_paths(print_errors: true) files, is_success = modified_files unless is_success print_error("Git is not available") if print_errors files = [] end ignored_patterns = %w[ **/Gemfile **/Gemfile.lock **/*_spec.rb **/spec_helper.rb ] @modified_files ||= [] @modified_files |= files.reject do |file| ignored_patterns.any? { |pattern| File.fnmatch(pattern, file) } end @modified_files end |
#modified_files ⇒ Object (protected)
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 524 def modified_files # Using an array avoids shelling out, so we avoid escaping/quoting changed_files = %w[git diff --name-only] is_success = true files = [] source_directories.each do |directory| begin output, status = Open3.capture2e(*changed_files, chdir: directory) is_success = status.success? break unless is_success files += output.split("\n").map do |path| realpath = Pathname.new(directory).join(path).realpath raise "invalid path" unless realpath.to_s.start_with?(directory) realpath.to_s end rescue => e elog(e) is_success = false break end end [files, is_success] end |
#name ⇒ Object
33 34 35 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 33 def name 'Developer' end |
#reload_file(full_path, print_errors: true) ⇒ Object
XXX: This will try to reload any .rb and break on modules
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 70 def reload_file(full_path, print_errors: true) unless File.exist?(full_path) && full_path.end_with?('.rb') print_error("#{full_path} must exist and be a .rb file") if print_errors return end # The file must exist to reach this, so we try our best here if full_path.start_with?(Msf::Config.module_directory, Msf::Config.user_module_directory) print_error('Reloading Metasploit modules is not supported (try "reload")') if print_errors return end print_status("Reloading #{full_path}") load full_path end |
#source_directories ⇒ Object (protected)
520 521 522 |
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 520 def source_directories [Msf::Config.install_root] end |