Class: Metasploit::Framework::DataService::DataProxy

Inherits:
Object
  • Object
show all
Includes:
DataProxyAutoLoader
Defined in:
lib/metasploit/framework/data_service/proxy/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PayloadDataProxy

#create_payload, #delete_payload, #payloads, #update_payload

Methods included from MsfDataProxy

#get_msf_version

Methods included from VulnAttemptDataProxy

#report_vuln_attempt, #vuln_attempts

Methods included from DbImportDataProxy

#import, #import_file

Methods included from DbExportDataProxy

#run_db_export

Methods included from NmapDataProxy

#import_nmap_xml_file

Methods included from LoginDataProxy

#create_credential_login, #invalidate_login, #logins, #update_login

Methods included from CredentialDataProxy

#create_cracked_credential, #create_credential, #create_credential_and_login, #creds, #delete_credentials, #update_credential

Methods included from SessionEventDataProxy

#report_session_event, #session_events

Methods included from LootDataProxy

#find_or_create_loot, #loots, #report_loot, #update_loot

Methods included from ExploitDataProxy

#report_exploit_attempt, #report_exploit_failure, #report_exploit_success

Methods included from SessionDataProxy

convert_msf_session_to_hash, #report_session, #sessions, #update_session

Methods included from WebDataProxy

#report_web_form, #report_web_page, #report_web_site, #report_web_vuln

Methods included from NoteDataProxy

#delete_note, #find_or_create_note, #notes, #report_note, #update_note

Methods included from WorkspaceDataProxy

#add_workspace, #default_workspace, #delete_workspaces, #find_workspace, #update_workspace, #workspace, #workspace=, #workspaces

Methods included from EventDataProxy

#events, #report_event

Methods included from VulnDataProxy

#delete_vuln, #find_or_create_vuln, #report_vuln, #update_vuln, #vulns

Methods included from HostDataProxy

#add_host_tag, #delete_host, #delete_host_tag, #find_or_create_host, #get_host, #get_host_tags, #hosts, #report_host, #update_host

Methods included from ServiceDataProxy

#delete_service, #find_or_create_service, #report_service, #services, #update_service

Constructor Details

#initialize(opts = {}) ⇒ DataProxy

Returns a new instance of DataProxy.


18
19
20
21
22
23
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 18

def initialize(opts = {})
  @data_services = {}
  @data_service_id = 0
  @usable = false
  setup(opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Used to bridge the local db


134
135
136
137
138
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 134

def method_missing(method, *args, &block)
  unless @current_data_service.nil?
    @current_data_service.send(method, *args, &block)
  end
end

Instance Attribute Details

#usableObject (readonly)

Returns the value of attribute usable


16
17
18
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 16

def usable
  @usable
end

Instance Method Details

#activeObject

Determines if the data service is active


45
46
47
48
49
50
51
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 45

def active
  if @current_data_service
    return @current_data_service.active
  end

  return false
end

#add_opts_workspace(opts, wspace = nil) ⇒ Hash

Adds a valid workspace value to the opts hash before sending on to the data layer.

Parameters:

  • opts (Hash)

    The opts hash that will be passed to the data layer.

  • wspace (String) (defaults to: nil)

    A specific workspace name to add to the opts hash.

Returns:

  • (Hash)

    The opts hash with a valid :workspace value added.


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 181

def add_opts_workspace(opts, wspace = nil)
  # If :id is present the user only wants a specific record, so workspace isn't needed
  return if opts.key?(:id)

  # If the user passed in a specific workspace then use that in opts
  opts[:workspace] = wspace if wspace

  # We only want to pass the workspace name, so grab it if it is currently an object.
  if opts[:workspace] && opts[:workspace].is_a?(::Mdm::Workspace)
    opts[:workspace] = opts[:workspace].name
  end

  # If we still don't have a :workspace value, just set it to the current workspace.
  opts[:workspace] = workspace.name if opts[:workspace].nil?

  opts
end

#data_service_operation(&block) ⇒ Object

Performs a set of data service operations declared within the block. This passes the @current_data_service as a parameter to the block. If there is no current data service registered or the data service is not active, the block is not executed and the method simply returns.


157
158
159
160
161
162
163
164
165
166
167
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 157

def data_service_operation(&block)
  return unless block_given?

  begin
    data_service = self.get_data_service
  rescue
    return
  end

  block.call(data_service) if !data_service.nil? && self.active
end

#delete_current_data_serviceObject


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 81

def delete_current_data_service
  @data_services.each do |id, ds|
    if ds == @current_data_service
      if id == 1
        raise "Unable to delete the local data service. Please use db_disconnect."
      else
        @data_services.delete(id)
        @current_data_service = @data_services[1]
      end
    end
  end
end

#delete_data_service(data_service_id) ⇒ Object

Delete the specified data service

Raises:

  • (ArgumentError)

67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 67

def delete_data_service(data_service_id)
  raise ArgumentError.new('Cannot delete data service id: 1') if data_service_id.to_i == 1

  data_service = @data_services.delete(data_service_id.to_i)
  if data_service.nil?
    raise "Data service with id: #{data_service_id} does not exist"
  end

  if @current_data_service == data_service
    # set the current data service to the first data service created
    @current_data_service = @data_services[1]
  end
end

#errorObject

Returns current error state


28
29
30
31
32
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 28

def error
  return @error if (@error)
  return @current_data_service.error if @current_data_service && !@current_data_service.error.nil?
  return 'unknown'
end

#get_data_serviceObject


148
149
150
151
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 148

def get_data_service
  raise 'No registered data_service' unless @current_data_service
  return @current_data_service
end

#get_services_metadataObject

Retrieves metadata about the data services


119
120
121
122
123
124
125
126
127
128
129
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 119

def ()
   = []
  @data_services.each_key {|key|
    name = @data_services[key].name
    active = !@current_data_service.nil? && name == @current_data_service.name
    is_local = @data_services[key].is_local?
     << Metasploit::Framework::DataService::Metadata.new(key, name, active, is_local)
  }

  
end

#is_local?Boolean

Returns:

  • (Boolean)

34
35
36
37
38
39
40
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 34

def is_local?
  if @current_data_service
    return @current_data_service.is_local?
  end

  return false
end

#log_error(exception, ui_message) ⇒ Object


169
170
171
172
173
174
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 169

def log_error(exception, ui_message)
  elog(ui_message, error: exception)
  # TODO: We should try to surface the original exception, instead of just a generic one.
  # This should not display the full backtrace, only the message.
  raise exception
end

#register_data_service(data_service) ⇒ Object

Registers the specified data service with the proxy and immediately sets it as the primary if active


57
58
59
60
61
62
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 57

def register_data_service(data_service)
  validate(data_service)
  data_service_id = @data_service_id += 1
  @data_services[data_service_id] = data_service
  set_data_service(data_service_id)
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

140
141
142
143
144
145
146
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 140

def respond_to?(method_name, include_private=false)
  unless @current_data_service.nil?
    return @current_data_service.respond_to?(method_name, include_private)
  end

  false
end

#set_data_service(data_service_id) ⇒ Object

Set the data service to be used


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/metasploit/framework/data_service/proxy/core.rb', line 97

def set_data_service(data_service_id)
  data_service = @data_services[data_service_id.to_i]
  if data_service.nil?
    raise "Data service with id: #{data_service_id} does not exist"
  end

  if !data_service.is_local? && !data_service.active
    raise "Data service #{data_service.name} is not online, and won't be set as active"
  end

  prev_data_service = @current_data_service
  @current_data_service = data_service
  # reset the previous data service's active flag if it is remote
  # to ensure checks are performed the next time it is set
  if !prev_data_service.nil? && !prev_data_service.is_local?
    prev_data_service.active = false
  end
end