Class: Msf::Ui::Console::CommandDispatcher::Developer

Inherits:
Object
  • Object
show all
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.']
)

Instance Attribute Summary

Attributes included from Msf::Ui::Console::CommandDispatcher

#driver

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

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Msf::Ui::Console::CommandDispatcher

#active_module, #active_module=, #active_session, #active_session=, #build_range_array, #docs_dir, #framework, #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, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_directory, #tab_complete_filenames, #tab_complete_generic, #tab_complete_source_address, #update_prompt

Constructor Details

#initialize(driver) ⇒ Developer

Returns a new instance of Developer.


12
13
14
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 12

def initialize(driver)
  super
end

Instance Method Details

#cmd_edit(*args) ⇒ Object

Edit the current module or a file with the preferred editor


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 194

def cmd_edit(*args)
  editing_module = false

  if args.length > 0
    path = File.expand_path(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_helpObject


182
183
184
185
186
187
188
189
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 182

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 '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


234
235
236
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 234

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


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
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 95

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...')

    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#{[email protected].join("\n")}")
    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_helpObject


85
86
87
88
89
90
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 85

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


141
142
143
144
145
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 141

def cmd_irb_tabs(_str, words)
  return [] if words.length > 1

  @@irb_opts.fmt.keys
end

#cmd_log(*args) ⇒ Object

Display framework.log paged to the end if possible


290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 290

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_helpObject


277
278
279
280
281
282
283
284
285
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 277

def cmd_log_help
  print_line 'Usage: log'
  print_line
  print_line 'Display framework.log paged to the end if possible.'
  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


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 157

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...')

  unless active_module
    print_status("You are in the \"framework\" object\n")
    framework.pry
    return
  end

  print_status("You are in #{active_module.fullname}\n")
  active_module.pry
end

#cmd_pry_helpObject


147
148
149
150
151
152
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 147

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


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 245

def cmd_reload_lib(*args)
  options = OptionParser.new do |opts|
    opts.banner = '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
      return reload_changed_files
    end
  end

  # The remaining unparsed arguments are files
  files = options.order(args)

  return print(options.help) if files.empty?

  files.each { |file| reload_file(file) }
end

#cmd_reload_lib_helpObject


238
239
240
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 238

def cmd_reload_lib_help
  cmd_reload_lib('-h')
end

#cmd_reload_lib_tabs(str, words) ⇒ Object

Tab completion for the reload_lib command


273
274
275
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 273

def cmd_reload_lib_tabs(str, words)
  tab_complete_filenames(str, words)
end

#commandsObject


20
21
22
23
24
25
26
27
28
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 20

def 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'
  }
end

#local_editorObject


30
31
32
33
34
35
36
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 30

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_pagerObject


38
39
40
41
42
43
44
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 38

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

#nameObject


16
17
18
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 16

def name
  'Developer'
end

#reload_changed_filesObject


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 65

def reload_changed_files
  # Using an array avoids shelling out, so we avoid escaping/quoting
  changed_files = %w[git diff --name-only]

  output, status = Open3.capture2e(*changed_files, chdir: Msf::Config.install_root)

  unless status.success?
    print_error("Git is not available: #{output.chomp}")
    return
  end

  files = output.split("\n")

  files.each do |file|
    next if file.end_with?('_spec.rb')
    f = File.join(Msf::Config.install_root, file)
    reload_file(f, print_errors: false)
  end
end

#reload_file(path, print_errors: true) ⇒ Object

XXX: This will try to reload any .rb and break on modules


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/msf/ui/console/command_dispatcher/developer.rb', line 47

def reload_file(path, print_errors: true)
  full_path = File.expand_path(path)

  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