Class: Rex::Post::HWBridge::Ui::Console::CommandDispatcher::CustomMethods

Inherits:
Object
  • Object
show all
Includes:
Msf::Auxiliary::Report, Rex::Post::HWBridge::Ui::Console::CommandDispatcher
Defined in:
lib/rex/post/hwbridge/ui/console/command_dispatcher/custom_methods.rb

Overview

Custom Methods extension - a set of commands defined by the HW itself

Constant Summary collapse

@@generic_opts =
Rex::Parser::Arguments.new(
"-h" => [ false, "Help menu."                    ])

Instance Attribute Summary

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

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Msf::Auxiliary::Report

#active_db?, #create_cracked_credential, #create_credential, #create_credential_and_login, #create_credential_login, #db, #db_warning_given?, #get_client, #get_host, #inside_workspace_boundary?, #invalidate_login, #mytask, #myworkspace, #myworkspace_id, #report_auth_info, #report_client, #report_exploit, #report_host, #report_loot, #report_note, #report_service, #report_vuln, #report_web_form, #report_web_page, #report_web_site, #report_web_vuln, #store_cred, #store_local, #store_loot

Methods included from Metasploit::Framework::Require

optionally, optionally_active_record_railtie, optionally_include_metasploit_credential_creation, #optionally_include_metasploit_credential_creation, optionally_require_metasploit_db_gem_engines

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

check_hash, #client, #log_error, #msf_loaded?, set_hash

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, #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(shell) ⇒ CustomMethods

Returns a new instance of CustomMethods.



16
17
18
19
20
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/custom_methods.rb', line 16

def initialize(shell)
  super
  @cmds = {}
  @custom_methods = {}
end

Instance Method Details

#cmd_generic_handler(cmd, *args) ⇒ Object

A generic handler for all custom commands



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/custom_methods.rb', line 144

def cmd_generic_handler(cmd, *args)
  @@generic_opts.parse(args) { |opt, idx, val|
    case opt
    when "-h"
      cmd_generic_handler_help(cmd)
      return true
    end
  }
  if not has_required_args(cmd, args)
    print_error("Not all required arguments were used.  See command help (-h)")
    return true
  end
  res = client.custom_methods.send_request(cmd, args, @custom_methods)
  print_status(res["status"]) if res.key? "status"
  print_status(res["value"]) if res.key? "value"
end

#cmd_generic_handler_help(cmd) ⇒ Object

A generic help system to show the arguments needed for custom commands



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/custom_methods.rb', line 126

def cmd_generic_handler_help(cmd)
  @custom_methods.each do |meth|
    next unless meth["method_name"].split('/').last == cmd
    args = ""
    args = "<args>" if meth["args"].size > 0
    print_line("Usage: #{cmd} #{args}")
    print_line
    meth["args"].each do |arg|
      req = ""
      req = "  *required*" if arg.key? "required" and arg["required"] == true
      print_line("  #{arg["arg_name"]}=<#{arg["arg_type"]}> #{req}")
    end
  end
end

#command_name_in_use?(cmd) ⇒ Boolean

Whether a command called cmd is already provided, either by this dispatcher (including its own handlers and earlier custom methods) or by any other dispatcher in the shell.

Parameters:

  • cmd (String) —

    The command name, without the cmd_ prefix

Returns:

  • (Boolean)


115
116
117
118
119
120
121
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/custom_methods.rb', line 115

def command_name_in_use?(cmd)
  return true if respond_to?("cmd_#{cmd}", true)

  shell.dispatcher_stack.any? do |dispatcher|
    !dispatcher.equal?(self) && dispatcher.commands&.key?(cmd)
  end
end

#commands ⇒ Object

List of supported commands.



28
29
30
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/custom_methods.rb', line 28

def commands
  @cmds ||= {}
end

#has_required_args(cmd, args) ⇒ Object

Verify all required args are used



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/custom_methods.rb', line 164

def has_required_args(cmd, args)
  all_found = true
  arguments = {}
  args.each do |arg|
    (key, value) = arg.split('=')
    arguments[key] = value
  end
  @custom_methods.each do |meth|
    if meth["method_name"].split('/').last == cmd
      meth["args"].each do |arg|
        if arg.key? "required" and arg["required"] == true
          all_found = false if not arguments.key? arg["arg_name"]
        end
      end
    end
  end
  all_found
end

#load_methods(methods) ⇒ void

This method returns an undefined value.

Loaded from core and passed hash from custom_methods rest call.

Only the trailing path segment of method_name is used as the command name. Entries with an unsupported path or an existing command name are skipped. The remaining entries are kept with 'args' as an array of hashes.

Calling this again replaces the commands registered by an earlier call.

Parameters:

  • methods (Array<Hash>) —

    The method definitions from the hardware's custom_methods reply. Each definition has a 'method_name' string, and can have 'method_desc', 'args' and 'return'.



50
51
52
53
54
55
56
57
58
59
60
61
62
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
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/custom_methods.rb', line 50

def load_methods(methods)
  unless methods.is_a?(Array)
    print_error('Failed to load custom methods: expected an array')
    return
  end

  unload_methods
  methods.each do |method|
    next unless method.is_a?(Hash) && method['method_name'].is_a?(String)

    full_name = method['method_name']
    cmd = Rex::Post::HWBridge::Extensions::CustomMethods::CustomMethods.command_name(full_name)

    unless cmd
      print_error("Skipping custom method with unsupported name: #{full_name.inspect}")
      next
    end

    if command_name_in_use?(cmd)
      print_error("Skipping custom method #{full_name.inspect}: the command '#{cmd}' already exists")
      next
    end

    arg_list = method.fetch('args', [])
    valid_args = arg_list.is_a?(Array) && arg_list.all? do |arg|
      arg.is_a?(Hash) &&
        arg['arg_name'].is_a?(String) && !arg['arg_name'].empty? &&
        [true, false].include?(arg.fetch('required', false))
    end
    unless valid_args
      print_error("Skipping custom method #{full_name.inspect}: invalid arguments")
      next
    end

    @custom_methods << method.merge('args' => arg_list)
    @cmds[cmd] = method['method_desc'] || 'See HW manual for command description'
    define_singleton_method("cmd_#{cmd}") do |*args|
      cmd_generic_handler(cmd, *args)
    end
  end
end

#name ⇒ Object



32
33
34
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/custom_methods.rb', line 32

def name
  "Custom Methods"
end

#unload_methods ⇒ void

This method returns an undefined value.

Removes the commands registered by an earlier load_methods call, so the same commands can be registered again.



98
99
100
101
102
103
104
105
# File 'lib/rex/post/hwbridge/ui/console/command_dispatcher/custom_methods.rb', line 98

def unload_methods
  @cmds.each_key do |cmd|
    handler = :"cmd_#{cmd}"
    singleton_class.remove_method(handler) if singleton_methods(false).include?(handler)
  end
  @cmds.clear
  @custom_methods = []
end