2012-06-29 00:18:28 -05:00
|
|
|
# -*- coding: binary -*-
|
2012-04-15 23:35:38 -05:00
|
|
|
|
2014-10-08 14:27:22 -05:00
|
|
|
#
|
|
|
|
|
# Gems
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
require 'rex/socket'
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Project
|
|
|
|
|
#
|
|
|
|
|
require 'metasploit/framework/require'
|
2017-07-07 13:33:42 -05:00
|
|
|
require 'metasploit/framework/data_service'
|
|
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
|
2009-10-25 17:18:23 +00:00
|
|
|
# The db module provides persistent storage and events. This class should be instantiated LAST
|
|
|
|
|
# as the active_suppport library overrides Kernel.require, slowing down all future code loads.
|
2014-10-13 15:26:19 -05:00
|
|
|
class Msf::DBManager
|
2014-10-08 14:27:22 -05:00
|
|
|
extend Metasploit::Framework::Require
|
|
|
|
|
|
2015-03-31 16:15:04 -05:00
|
|
|
# Default proto for making new `Mdm::Service`s. This should probably be a
|
|
|
|
|
# const on `Mdm::Service`
|
|
|
|
|
DEFAULT_SERVICE_PROTO = "tcp"
|
|
|
|
|
|
2014-10-13 14:49:26 -05:00
|
|
|
autoload :Adapter, 'msf/core/db_manager/adapter'
|
2014-10-13 08:27:09 -05:00
|
|
|
autoload :Client, 'msf/core/db_manager/client'
|
2014-10-13 12:07:13 -05:00
|
|
|
autoload :Connection, 'msf/core/db_manager/connection'
|
2014-10-09 11:41:04 -05:00
|
|
|
autoload :Cred, 'msf/core/db_manager/cred'
|
2020-09-22 02:56:51 +01:00
|
|
|
autoload :DBExport, 'msf/core/db_manager/db_export'
|
2014-10-13 09:05:10 -05:00
|
|
|
autoload :Event, 'msf/core/db_manager/event'
|
2014-10-13 09:39:07 -05:00
|
|
|
autoload :ExploitAttempt, 'msf/core/db_manager/exploit_attempt'
|
2014-10-09 12:54:04 -05:00
|
|
|
autoload :ExploitedHost, 'msf/core/db_manager/exploited_host'
|
2014-10-09 11:11:36 -05:00
|
|
|
autoload :Host, 'msf/core/db_manager/host'
|
2014-10-13 10:15:14 -05:00
|
|
|
autoload :HostDetail, 'msf/core/db_manager/host_detail'
|
2014-10-13 11:00:36 -05:00
|
|
|
autoload :HostTag, 'msf/core/db_manager/host_tag'
|
2014-10-09 14:51:24 -05:00
|
|
|
autoload :Import, 'msf/core/db_manager/import'
|
2014-10-13 15:28:12 -05:00
|
|
|
autoload :ImportMsfXml, 'msf/core/db_manager/import_msf_xml'
|
2014-10-09 09:35:19 -05:00
|
|
|
autoload :IPAddress, 'msf/core/db_manager/ip_address'
|
2018-05-31 15:33:11 -05:00
|
|
|
autoload :Login, 'msf/core/db_manager/login'
|
2014-10-09 15:15:40 -05:00
|
|
|
autoload :Loot, 'msf/core/db_manager/loot'
|
2014-10-13 15:39:15 -05:00
|
|
|
autoload :Migration, 'msf/core/db_manager/migration'
|
2014-10-09 08:53:41 -05:00
|
|
|
autoload :ModuleCache, 'msf/core/db_manager/module_cache'
|
2014-10-09 15:29:07 -05:00
|
|
|
autoload :Note, 'msf/core/db_manager/note'
|
2018-09-04 15:31:06 -05:00
|
|
|
autoload :Payload, 'msf/core/db_manager/payload'
|
2014-10-13 10:06:37 -05:00
|
|
|
autoload :Ref, 'msf/core/db_manager/ref'
|
2014-10-13 09:19:39 -05:00
|
|
|
autoload :Report, 'msf/core/db_manager/report'
|
2014-10-13 11:23:37 -05:00
|
|
|
autoload :Route, 'msf/core/db_manager/route'
|
2014-10-09 11:31:29 -05:00
|
|
|
autoload :Service, 'msf/core/db_manager/service'
|
2014-10-13 10:50:11 -05:00
|
|
|
autoload :Session, 'msf/core/db_manager/session'
|
2014-10-13 11:09:27 -05:00
|
|
|
autoload :SessionEvent, 'msf/core/db_manager/session_event'
|
2014-10-13 09:28:48 -05:00
|
|
|
autoload :Task, 'msf/core/db_manager/task'
|
2018-06-22 15:09:36 -04:00
|
|
|
autoload :User, 'msf/core/db_manager/user'
|
2014-10-09 15:47:34 -05:00
|
|
|
autoload :Vuln, 'msf/core/db_manager/vuln'
|
2014-10-13 11:32:15 -05:00
|
|
|
autoload :VulnAttempt, 'msf/core/db_manager/vuln_attempt'
|
2014-10-13 09:54:38 -05:00
|
|
|
autoload :VulnDetail, 'msf/core/db_manager/vuln_detail'
|
2014-10-09 10:11:51 -05:00
|
|
|
autoload :WMAP, 'msf/core/db_manager/wmap'
|
2014-10-13 11:39:17 -05:00
|
|
|
autoload :Web, 'msf/core/db_manager/web'
|
2014-10-08 15:46:35 -05:00
|
|
|
autoload :Workspace, 'msf/core/db_manager/workspace'
|
2014-10-08 15:26:28 -05:00
|
|
|
|
2014-10-08 14:27:22 -05:00
|
|
|
optionally_include_metasploit_credential_creation
|
|
|
|
|
|
2017-07-07 13:33:42 -05:00
|
|
|
# Interface must be included first
|
|
|
|
|
include Metasploit::Framework::DataService
|
|
|
|
|
|
2014-10-13 14:49:26 -05:00
|
|
|
include Msf::DBManager::Adapter
|
2014-10-13 08:27:09 -05:00
|
|
|
include Msf::DBManager::Client
|
2014-10-13 12:07:13 -05:00
|
|
|
include Msf::DBManager::Connection
|
2014-10-09 11:41:04 -05:00
|
|
|
include Msf::DBManager::Cred
|
2020-09-22 02:56:51 +01:00
|
|
|
include Msf::DBManager::DBExport
|
2014-10-13 09:05:10 -05:00
|
|
|
include Msf::DBManager::Event
|
2014-10-13 09:39:07 -05:00
|
|
|
include Msf::DBManager::ExploitAttempt
|
2014-10-09 12:54:04 -05:00
|
|
|
include Msf::DBManager::ExploitedHost
|
2014-10-09 11:11:36 -05:00
|
|
|
include Msf::DBManager::Host
|
2014-10-13 10:15:14 -05:00
|
|
|
include Msf::DBManager::HostDetail
|
2014-10-13 11:00:36 -05:00
|
|
|
include Msf::DBManager::HostTag
|
2014-10-09 14:51:24 -05:00
|
|
|
include Msf::DBManager::Import
|
2014-10-09 09:35:19 -05:00
|
|
|
include Msf::DBManager::IPAddress
|
2018-05-31 15:33:11 -05:00
|
|
|
include Msf::DBManager::Login
|
2014-10-09 15:15:40 -05:00
|
|
|
include Msf::DBManager::Loot
|
2013-05-20 12:45:17 -05:00
|
|
|
include Msf::DBManager::Migration
|
2014-10-09 08:53:41 -05:00
|
|
|
include Msf::DBManager::ModuleCache
|
2014-10-09 15:29:07 -05:00
|
|
|
include Msf::DBManager::Note
|
2018-09-04 15:31:06 -05:00
|
|
|
include Msf::DBManager::Payload
|
2014-10-13 10:06:37 -05:00
|
|
|
include Msf::DBManager::Ref
|
2014-10-13 09:19:39 -05:00
|
|
|
include Msf::DBManager::Report
|
2014-10-13 11:23:37 -05:00
|
|
|
include Msf::DBManager::Route
|
2014-10-09 11:31:29 -05:00
|
|
|
include Msf::DBManager::Service
|
2014-10-13 10:50:11 -05:00
|
|
|
include Msf::DBManager::Session
|
2014-10-13 11:09:27 -05:00
|
|
|
include Msf::DBManager::SessionEvent
|
2014-10-13 09:28:48 -05:00
|
|
|
include Msf::DBManager::Task
|
2018-06-22 15:09:36 -04:00
|
|
|
include Msf::DBManager::User
|
2014-10-09 15:47:34 -05:00
|
|
|
include Msf::DBManager::Vuln
|
2014-10-13 11:32:15 -05:00
|
|
|
include Msf::DBManager::VulnAttempt
|
2014-10-13 09:54:38 -05:00
|
|
|
include Msf::DBManager::VulnDetail
|
2014-10-09 10:11:51 -05:00
|
|
|
include Msf::DBManager::WMAP
|
2014-10-13 11:39:17 -05:00
|
|
|
include Msf::DBManager::Web
|
2014-10-08 15:46:35 -05:00
|
|
|
include Msf::DBManager::Workspace
|
2014-10-08 15:26:28 -05:00
|
|
|
|
|
|
|
|
# Provides :framework and other accessors
|
2013-05-20 12:45:17 -05:00
|
|
|
include Msf::Framework::Offspring
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2017-07-07 13:33:42 -05:00
|
|
|
def name
|
|
|
|
|
'local_db_service'
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-10 18:31:02 -04:00
|
|
|
def is_local?
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
2014-10-13 15:13:28 -05:00
|
|
|
#
|
|
|
|
|
# Attributes
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
# Stores the error message for why the db was not loaded
|
|
|
|
|
attr_accessor :error
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
# Returns true if the prerequisites have been installed
|
|
|
|
|
attr_accessor :usable
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2014-10-13 15:13:28 -05:00
|
|
|
#
|
2015-04-13 13:21:41 +05:00
|
|
|
# initialize
|
2014-10-13 15:13:28 -05:00
|
|
|
#
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2010-08-05 02:24:40 +00:00
|
|
|
def initialize(framework, opts = {})
|
2006-03-21 04:37:48 +00:00
|
|
|
self.framework = framework
|
2023-03-15 10:00:11 +00:00
|
|
|
self.migrated = nil
|
2012-07-10 23:56:48 -05:00
|
|
|
self.modules_cached = false
|
|
|
|
|
self.modules_caching = false
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
@usable = false
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2011-01-25 22:58:21 +00:00
|
|
|
# Don't load the database if the user said they didn't need it.
|
2010-08-05 02:24:40 +00:00
|
|
|
if (opts['DisableDatabase'])
|
2011-01-25 22:58:21 +00:00
|
|
|
self.error = "disabled"
|
2010-08-05 02:24:40 +00:00
|
|
|
return
|
|
|
|
|
end
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2017-10-16 17:07:26 -05:00
|
|
|
return initialize_database_support
|
2010-08-05 02:24:40 +00:00
|
|
|
end
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2014-10-13 15:13:28 -05:00
|
|
|
#
|
|
|
|
|
# Instance Methods
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Determines if the database is functional
|
|
|
|
|
#
|
|
|
|
|
def check
|
2020-04-03 09:38:15 -05:00
|
|
|
::ApplicationRecord.connection_pool.with_connection {
|
2015-02-06 10:13:14 -06:00
|
|
|
res = ::Mdm::Host.first
|
2014-10-13 15:13:28 -05:00
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2010-08-05 02:24:40 +00:00
|
|
|
#
|
|
|
|
|
# Do what is necessary to load our database support
|
|
|
|
|
#
|
|
|
|
|
def initialize_database_support
|
2009-11-14 21:41:38 +00:00
|
|
|
begin
|
2014-05-12 15:03:51 -05:00
|
|
|
add_rails_engine_migration_paths
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2007-02-11 23:47:34 +00:00
|
|
|
@usable = true
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
rescue ::Exception => e
|
2009-06-03 16:28:28 +00:00
|
|
|
self.error = e
|
2020-06-11 13:09:25 +01:00
|
|
|
elog('DB is not enabled due to load error', error: e)
|
2010-08-05 02:24:40 +00:00
|
|
|
return false
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2009-03-28 20:45:46 +00:00
|
|
|
#
|
|
|
|
|
# Determine what drivers are available
|
|
|
|
|
#
|
2014-05-12 15:03:51 -05:00
|
|
|
initialize_adapter
|
2013-08-30 16:28:33 -05:00
|
|
|
|
2010-08-05 02:24:40 +00:00
|
|
|
true
|
2009-03-28 20:45:46 +00:00
|
|
|
end
|
2017-07-07 13:33:42 -05:00
|
|
|
|
|
|
|
|
def init_db(opts)
|
|
|
|
|
init_success = false
|
|
|
|
|
|
|
|
|
|
# Append any migration paths necessary to bring the database online
|
|
|
|
|
if opts['DatabaseMigrationPaths']
|
|
|
|
|
opts['DatabaseMigrationPaths'].each do |migrations_path|
|
|
|
|
|
ActiveRecord::Migrator.migrations_paths << migrations_path
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-23 13:26:45 +00:00
|
|
|
configuration_pathname = Metasploit::Framework::Database.configurations_pathname(path: opts['DatabaseYAML'])
|
|
|
|
|
|
|
|
|
|
if configuration_pathname.nil?
|
|
|
|
|
self.error = "No database YAML file"
|
2017-07-07 13:33:42 -05:00
|
|
|
else
|
2021-03-23 13:26:45 +00:00
|
|
|
if configuration_pathname.readable?
|
2022-03-23 13:05:42 +00:00
|
|
|
# parse specified database YAML file, using the same pattern as Rails https://github.com/rails/rails/pull/42249
|
|
|
|
|
dbinfo = begin
|
|
|
|
|
YAML.load_file(configuration_pathname, aliases: true) || {}
|
|
|
|
|
rescue ArgumentError
|
|
|
|
|
YAML.load_file(configuration_pathname) || {}
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-23 13:26:45 +00:00
|
|
|
dbenv = opts['DatabaseEnv'] || Rails.env
|
|
|
|
|
db_opts = dbinfo[dbenv]
|
|
|
|
|
else
|
|
|
|
|
elog("Warning, #{configuration_pathname} is not readable. Try running as root or chmod.")
|
|
|
|
|
end
|
2017-07-07 13:33:42 -05:00
|
|
|
|
2021-03-23 13:26:45 +00:00
|
|
|
if db_opts
|
|
|
|
|
init_success = connect(db_opts)
|
2018-04-10 18:31:02 -04:00
|
|
|
else
|
2021-03-23 13:26:45 +00:00
|
|
|
elog("No database definition for environment #{dbenv}")
|
2017-07-07 13:33:42 -05:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# framework.db.active will be true if after_establish_connection ran directly when connection_established? was
|
|
|
|
|
# already true or if framework.db.connect called after_establish_connection.
|
|
|
|
|
if !! error
|
|
|
|
|
if error.to_s =~ /RubyGem version.*pg.*0\.11/i
|
2020-06-11 13:09:25 +01:00
|
|
|
err_msg = <<~ERROR
|
|
|
|
|
***
|
|
|
|
|
*
|
|
|
|
|
* Metasploit now requires version 0.11 or higher of the 'pg' gem for database support
|
|
|
|
|
* There are three ways to accomplish this upgrade:
|
|
|
|
|
* 1. If you run Metasploit with your system ruby, simply upgrade the gem:
|
|
|
|
|
* $ rvmsudo gem install pg
|
|
|
|
|
* 2. Use the Community Edition web interface to apply a Software Update
|
|
|
|
|
* 3. Uninstall, download the latest version, and reinstall Metasploit
|
|
|
|
|
*
|
|
|
|
|
***
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ERROR
|
|
|
|
|
elog(err_msg)
|
2017-07-07 13:33:42 -05:00
|
|
|
end
|
|
|
|
|
|
2020-06-11 13:09:25 +01:00
|
|
|
# +error+ is not an instance of +Exception+, it is, in fact, a +String+
|
2017-07-07 13:33:42 -05:00
|
|
|
elog("Failed to connect to the database: #{error}")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return init_success
|
|
|
|
|
end
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|