Module: Msf::DbConnector

Defined in:
lib/msf/core/db_connector.rb

Overview

This class is used when wanting to connect/disconnect framework from a particular database or http service

Constant Summary collapse

DbConfigGroup =
'framework/database'

Class Method Summary collapse

Class Method Details

.as_connection_options(conf_options) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/msf/core/db_connector.rb', line 297

def self.as_connection_options(conf_options)
  opts = {}
  https_opts = {}
  if conf_options
    opts[:url] = conf_options[:url] if conf_options[:url]
    opts[:api_token] = conf_options[:api_token] if conf_options[:api_token]
    https_opts[:cert] = conf_options[:cert] if conf_options[:cert]
    https_opts[:skip_verify] = conf_options[:skip_verify] if conf_options[:skip_verify]
  else
    return
  end

  opts[:https_opts] = https_opts unless https_opts.empty?
  opts
end

.build_postgres_urlObject



259
260
261
262
263
264
265
266
267
268
# File 'lib/msf/core/db_connector.rb', line 259

def self.build_postgres_url
  conn_params = ApplicationRecord.connection_db_config.configuration_hash
  url = ''
  url += "#{conn_params[:username]}" if conn_params[:username]
  url += ":#{conn_params[:password]}" if conn_params[:password]
  url += "@#{conn_params[:host]}" if conn_params[:host]
  url += ":#{conn_params[:port]}" if conn_params[:port]
  url += "/#{conn_params[:database]}" if conn_params[:database]
  url
end

.data_service_search(name: nil, url: nil) ⇒ Object

Search for a human readable data service name based on the search criteria The search criteria can match against a service name or url



274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/msf/core/db_connector.rb', line 274

def self.data_service_search(name: nil, url: nil)
  conf = Msf::Config.load
  result = nil

  conf.each_pair do |key, value|
    conf_name = key.split('/').last
    has_name_match = !name.nil? && (conf_name == name)
    has_url_match = !url.nil? && (value.is_a?(Hash) && value['url'] == url)
    if has_name_match || has_url_match
      result = conf_name
    end
  end
  result
end

.db_connect(framework, opts = {}) ⇒ Object

Connect to the required database



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
91
92
93
94
95
96
97
# File 'lib/msf/core/db_connector.rb', line 60

def self.db_connect(framework, opts = {})
  unless framework.db.driver
    return { error: 'No database driver installed.'}
  end

  if !opts[:url] && !opts[:yaml_file]
    return { error: 'A URL or saved data service name is required.' }
  end

  if opts[:url] =~ /http/
    new_conn_type = 'http'
  else
    new_conn_type = framework.db.driver
  end

  # Currently only able to be connected to one DB at a time
  if framework.db.connection_established?
    # But the http connection still requires a local database to support AR, so we have to allow that
    # Don't allow more than one HTTP service, though
    if new_conn_type != 'http' || framework.db..count >= 2
      return {
        error: 'Connection already established. Only one connection is allowed at a time. Run db_disconnect first if you wish to connect to a different data service.'
      }
    end
  end

  if opts[:yaml_file]
    db_connect_yaml(framework, opts)
  elsif new_conn_type == 'http'
    db_connect_http(framework, as_connection_options(opts))
  elsif new_conn_type == 'postgresql'
    db_connect_postgresql(framework, as_connection_options(opts))
  else
    {
      error: "This database driver #{new_conn_type} is not currently supported"
    }
  end
end

.db_connect_from_config(framework, path = nil) ⇒ Object

Connect to a database by using the default framework config, or the config file provided



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/msf/core/db_connector.rb', line 17

def self.db_connect_from_config(framework, path = nil)
  begin
    conf = Msf::Config.load(path)
  rescue StandardError => e
    wlog("Failed to load configuration: #{e}")
    return {}
  end

  if conf.group?(DbConfigGroup)
    conf[DbConfigGroup].each_pair do |k, v|
      next unless k.downcase == 'default_db'

      ilog 'Default data service found. Attempting to connect...'
      db_name = v
      config = load_db_config(db_name, path)
      if config
        if framework.db.active && config[:url] !~ /http/
          ilog 'Existing local data connection found. Disconnecting first.'
          db_disconnect(framework)
        end

        return db_connect(framework, config).merge(data_service_name: db_name)
      else
        elog "Config entry for '#{db_name}' could not be found. Config file might be corrupt."
      end
    end
  end

  {}
end

.db_connect_http(framework, opts) ⇒ Object

Connect to an existing http database



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/msf/core/db_connector.rb', line 157

def self.db_connect_http(framework, opts)
  # local database is required to use Mdm objects
  unless framework.db.active
    error = 'No local database connected, meaning some Metasploit features will not be available. A full list of '\
    'the affected features & database setup instructions can be found here: '\
    'https://docs.metasploit.com/docs/using-metasploit/intermediate/metasploit-database-support.html'

    return {
      error: error
    }
  end

  uri = db_parse_db_uri_http(opts[:url])

  remote_data_service = Metasploit::Framework::DataService::RemoteHTTPDataService.new(uri.to_s, opts)
  begin
    framework.db.register_data_service(remote_data_service)
    framework.db.workspace = framework.db.default_workspace
    {
      result: "Connected to HTTP data service: #{remote_data_service.name}",
      data_service_name: data_service_search(url: opts[:url])
    }
  rescue => e
    {
      error: "Failed to connect to the HTTP data service: #{e.message}"
    }
  end
end

.db_connect_postgresql(framework, cli_opts) ⇒ Object

Connect to an existing Postgres database



189
190
191
192
193
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
230
231
232
233
234
235
236
237
# File 'lib/msf/core/db_connector.rb', line 189

def self.db_connect_postgresql(framework, cli_opts)
  info = db_parse_db_uri_postgresql(cli_opts[:url])
  opts = { 'adapter' => 'postgresql' }

  opts['username'] = info[:user] if (info[:user])
  opts['password'] = info[:pass] if (info[:pass])
  opts['database'] = info[:name]
  opts['host'] = info[:host] if (info[:host])
  opts['port'] = info[:port] if (info[:port])

  opts['pass'] ||= ''

  # Do a little legwork to find the real database socket
  if !opts['host']
    while(true)
      done = false
      dirs = %W{ /var/run/postgresql /tmp }
      dirs.each do |dir|
        if ::File.directory?(dir)
          d = ::Dir.new(dir)
          d.entries.grep(/^\.s\.PGSQL.(\d+)$/).each do |ent|
            opts['port'] = ent.split('.')[-1].to_i
            opts['host'] = dir
            done = true
            break
          end
        end
        break if done
      end
      break
    end
  end

  # Default to loopback
  unless opts['host']
    opts['host'] = '127.0.0.1'
  end

  if framework.db.connect(opts) && framework.db.connection_established?
    {
      result: "Connected to Postgres data service: #{info[:host]}/#{info[:name]}",
      data_service_name: data_service_search(url: opts[:url]) || framework.db.name
    }
  else
    {
      error: "Failed to connect to the Postgres data service: #{framework.db.error}"
    }
  end
end

.db_connect_yaml(framework, opts) ⇒ Object

Connect to a database via the supplied yaml file



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/msf/core/db_connector.rb', line 132

def self.db_connect_yaml(framework, opts)
  file = opts[:yaml_file] || ::File.join(Msf::Config.config_directory, 'database.yml')
  file = ::File.expand_path(file)
  unless ::File.exist?(file)
    return { error: 'File not found' }
  end
  begin
    db = YAML.load(::File.read(file))['production']
  rescue => _e
    return { error: 'File did not contain valid production database credentials' }
  end

  framework.db.connect(db)

  local_db_url = build_postgres_url
  local_name = data_service_search(url: local_db_url)
  return {
    result: 'Connected to the database specified in the YAML file',
    data_service_name: local_name
  }
end

.db_disconnect(framework) ⇒ Object

Disconnect from the currently connected database. This will gracefully fallback from a remote data service to a local postgres instance if configured correctly.



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
# File 'lib/msf/core/db_connector.rb', line 103

def self.db_disconnect(framework)
  result = { old_data_service_name: framework.db.name }
  unless framework.db.driver
    result[:error] = 'No database driver installed.'
    return result
  end

  if framework.db.active
    if framework.db.driver == 'http'
      begin
        framework.db.delete_current_data_service
        local_db_url = build_postgres_url
        local_name = data_service_search(url: local_db_url)
        result[:data_service_name] = local_name
      rescue StandardError => e
        result[:error] = e.message
      end
    else
      framework.db.disconnect
      result[:data_service_name] = nil
    end
  end

  result
end

.db_parse_db_uri_http(path) ⇒ Object



255
256
257
# File 'lib/msf/core/db_connector.rb', line 255

def self.db_parse_db_uri_http(path)
  URI.parse(path)
end

.db_parse_db_uri_postgresql(path) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/msf/core/db_connector.rb', line 239

def self.db_parse_db_uri_postgresql(path)
  res = {}
  if path
    auth, dest = path.split('@')
    (dest = auth and auth = nil) if not dest
    # remove optional scheme in database url
    auth = auth.sub(/^\w+:\/\//, '') if auth
    res[:user],res[:pass] = auth.split(':') if auth
    targ,name = dest.split('/')
    (name = targ and targ = nil) if not name
    res[:host],res[:port] = targ.split(':') if targ
  end
  res[:name] = name || 'metasploit3'
  res
end

.load_db_config(db_name, path = nil) ⇒ Object



289
290
291
292
293
294
295
# File 'lib/msf/core/db_connector.rb', line 289

def self.load_db_config(db_name, path = nil)
  conf = Msf::Config.load(path)
  conf_options = conf["#{DbConfigGroup}/#{db_name}"]
  return unless conf_options

  conf_options.transform_keys(&:to_sym)
end