Files
metasploit-gs/lib/msf/core/db_manager.rb
T

244 lines
7.0 KiB
Ruby
Raw Normal View History

# -*- 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'
require 'msf/base/config'
2006-03-21 04:37:48 +00:00
require 'msf/core'
2014-10-08 14:27:22 -05:00
require 'msf/core/database_event'
require 'msf/core/db_import_error'
require 'msf/core/host_state'
require 'msf/core/service_state'
2017-07-07 13:33:42 -05:00
require 'metasploit/framework/data_service'
2006-03-21 04:37:48 +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
# 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'
2018-03-06 15:15:27 -06: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'
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'
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
2018-03-06 15:15:27 -06: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-08-30 16:28:33 -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
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-08-30 16:28:33 -05:00
include Msf::Framework::Offspring
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
# Returns true if the prerequisites have been installed
attr_accessor :usable
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
def initialize(framework, opts = {})
self.framework = framework
self.migrated = false
self.modules_cached = false
self.modules_caching = false
@usable = false
# Don't load the database if the user said they didn't need it.
if (opts['DisableDatabase'])
self.error = "disabled"
return
end
return initialize_database_support
2013-08-30 16:28:33 -05:00
end
2014-10-13 15:13:28 -05:00
#
# Instance Methods
#
#
# Determines if the database is functional
#
def check
::ActiveRecord::Base.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
2013-08-30 16:28:33 -05:00
#
# Do what is necessary to load our database support
#
def initialize_database_support
begin
add_rails_engine_migration_paths
2013-08-30 16:28:33 -05:00
@usable = true
rescue ::Exception => e
self.error = e
elog("DB is not enabled due to load error: #{e}")
return false
end
#
# Determine what drivers are available
#
initialize_adapter
2013-08-30 16:28:33 -05:00
true
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
if connection_established?
after_establish_connection
else
configuration_pathname = Metasploit::Framework::Database.configurations_pathname(path: opts['DatabaseYAML'])
2018-04-10 18:31:02 -04:00
if configuration_pathname.nil?
self.error = "No database YAML file"
else
2017-07-07 13:33:42 -05:00
if configuration_pathname.readable?
2018-04-10 18:31:02 -04:00
# parse specified database YAML file
2017-07-07 13:33:42 -05:00
dbinfo = YAML.load_file(configuration_pathname) || {}
dbenv = opts['DatabaseEnv'] || Rails.env
db = dbinfo[dbenv]
else
elog("Warning, #{configuration_pathname} is not readable. Try running as root or chmod.")
end
if not db
elog("No database definition for environment #{dbenv}")
else
init_success = connect(db)
end
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
elog("***")
elog("*")
elog("* Metasploit now requires version 0.11 or higher of the 'pg' gem for database support")
elog("* There a three ways to accomplish this upgrade:")
elog("* 1. If you run Metasploit with your system ruby, simply upgrade the gem:")
elog("* $ rvmsudo gem install pg ")
elog("* 2. Use the Community Edition web interface to apply a Software Update")
elog("* 3. Uninstall, download the latest version, and reinstall Metasploit")
elog("*")
elog("***")
elog("")
elog("")
end
elog("Failed to connect to the database: #{error}")
end
return init_success
end
2006-03-21 04:37:48 +00:00
end