Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/core/db_manager.rb
Views: 11778
# -*- coding: binary -*-12#3# Gems4#56require 'rex/socket'78#9# Project10#11require 'metasploit/framework/require'12require 'metasploit/framework/data_service'131415# The db module provides persistent storage and events. This class should be instantiated LAST16# as the active_suppport library overrides Kernel.require, slowing down all future code loads.17class Msf::DBManager18extend Metasploit::Framework::Require1920# Default proto for making new `Mdm::Service`s. This should probably be a21# const on `Mdm::Service`22DEFAULT_SERVICE_PROTO = "tcp"2324autoload :Adapter, 'msf/core/db_manager/adapter'25autoload :Client, 'msf/core/db_manager/client'26autoload :Connection, 'msf/core/db_manager/connection'27autoload :Cred, 'msf/core/db_manager/cred'28autoload :DBExport, 'msf/core/db_manager/db_export'29autoload :Event, 'msf/core/db_manager/event'30autoload :ExploitAttempt, 'msf/core/db_manager/exploit_attempt'31autoload :ExploitedHost, 'msf/core/db_manager/exploited_host'32autoload :Host, 'msf/core/db_manager/host'33autoload :HostDetail, 'msf/core/db_manager/host_detail'34autoload :HostTag, 'msf/core/db_manager/host_tag'35autoload :Import, 'msf/core/db_manager/import'36autoload :ImportMsfXml, 'msf/core/db_manager/import_msf_xml'37autoload :IPAddress, 'msf/core/db_manager/ip_address'38autoload :Login, 'msf/core/db_manager/login'39autoload :Loot, 'msf/core/db_manager/loot'40autoload :Migration, 'msf/core/db_manager/migration'41autoload :ModuleCache, 'msf/core/db_manager/module_cache'42autoload :Note, 'msf/core/db_manager/note'43autoload :Payload, 'msf/core/db_manager/payload'44autoload :Ref, 'msf/core/db_manager/ref'45autoload :Report, 'msf/core/db_manager/report'46autoload :Route, 'msf/core/db_manager/route'47autoload :Service, 'msf/core/db_manager/service'48autoload :Session, 'msf/core/db_manager/session'49autoload :SessionEvent, 'msf/core/db_manager/session_event'50autoload :Task, 'msf/core/db_manager/task'51autoload :User, 'msf/core/db_manager/user'52autoload :Vuln, 'msf/core/db_manager/vuln'53autoload :VulnAttempt, 'msf/core/db_manager/vuln_attempt'54autoload :VulnDetail, 'msf/core/db_manager/vuln_detail'55autoload :WMAP, 'msf/core/db_manager/wmap'56autoload :Web, 'msf/core/db_manager/web'57autoload :Workspace, 'msf/core/db_manager/workspace'5859optionally_include_metasploit_credential_creation6061# Interface must be included first62include Metasploit::Framework::DataService6364include Msf::DBManager::Adapter65include Msf::DBManager::Client66include Msf::DBManager::Connection67include Msf::DBManager::Cred68include Msf::DBManager::DBExport69include Msf::DBManager::Event70include Msf::DBManager::ExploitAttempt71include Msf::DBManager::ExploitedHost72include Msf::DBManager::Host73include Msf::DBManager::HostDetail74include Msf::DBManager::HostTag75include Msf::DBManager::Import76include Msf::DBManager::IPAddress77include Msf::DBManager::Login78include Msf::DBManager::Loot79include Msf::DBManager::Migration80include Msf::DBManager::ModuleCache81include Msf::DBManager::Note82include Msf::DBManager::Payload83include Msf::DBManager::Ref84include Msf::DBManager::Report85include Msf::DBManager::Route86include Msf::DBManager::Service87include Msf::DBManager::Session88include Msf::DBManager::SessionEvent89include Msf::DBManager::Task90include Msf::DBManager::User91include Msf::DBManager::Vuln92include Msf::DBManager::VulnAttempt93include Msf::DBManager::VulnDetail94include Msf::DBManager::WMAP95include Msf::DBManager::Web96include Msf::DBManager::Workspace9798# Provides :framework and other accessors99include Msf::Framework::Offspring100101def name102'local_db_service'103end104105def is_local?106true107end108109#110# Attributes111#112113# Stores the error message for why the db was not loaded114attr_accessor :error115116# Returns true if the prerequisites have been installed117attr_accessor :usable118119#120# initialize121#122123def initialize(framework, opts = {})124self.framework = framework125self.migrated = nil126self.modules_cached = false127self.modules_caching = false128129@usable = false130131# Don't load the database if the user said they didn't need it.132if (opts['DisableDatabase'])133self.error = "disabled"134return135end136137return initialize_database_support138end139140#141# Instance Methods142#143144#145# Determines if the database is functional146#147def check148::ApplicationRecord.connection_pool.with_connection {149res = ::Mdm::Host.first150}151end152153#154# Do what is necessary to load our database support155#156def initialize_database_support157begin158add_rails_engine_migration_paths159160@usable = true161162rescue ::Exception => e163self.error = e164elog('DB is not enabled due to load error', error: e)165return false166end167168#169# Determine what drivers are available170#171initialize_adapter172173true174end175176def init_db(opts)177init_success = false178179# Append any migration paths necessary to bring the database online180if opts['DatabaseMigrationPaths']181opts['DatabaseMigrationPaths'].each do |migrations_path|182ActiveRecord::Migrator.migrations_paths << migrations_path183end184end185186configuration_pathname = Metasploit::Framework::Database.configurations_pathname(path: opts['DatabaseYAML'])187188if configuration_pathname.nil?189self.error = "No database YAML file"190else191if configuration_pathname.readable?192# parse specified database YAML file, using the same pattern as Rails https://github.com/rails/rails/pull/42249193dbinfo = begin194YAML.load_file(configuration_pathname, aliases: true) || {}195rescue ArgumentError196YAML.load_file(configuration_pathname) || {}197end198199dbenv = opts['DatabaseEnv'] || Rails.env200db_opts = dbinfo[dbenv]201else202elog("Warning, #{configuration_pathname} is not readable. Try running as root or chmod.")203end204205if db_opts206init_success = connect(db_opts)207else208elog("No database definition for environment #{dbenv}")209end210end211212# framework.db.active will be true if after_establish_connection ran directly when connection_established? was213# already true or if framework.db.connect called after_establish_connection.214if !! error215if error.to_s =~ /RubyGem version.*pg.*0\.11/i216err_msg = <<~ERROR217***218*219* Metasploit now requires version 0.11 or higher of the 'pg' gem for database support220* There are three ways to accomplish this upgrade:221* 1. If you run Metasploit with your system ruby, simply upgrade the gem:222* $ rvmsudo gem install pg223* 2. Use the Community Edition web interface to apply a Software Update224* 3. Uninstall, download the latest version, and reinstall Metasploit225*226***227228229ERROR230elog(err_msg)231end232233# +error+ is not an instance of +Exception+, it is, in fact, a +String+234elog("Failed to connect to the database: #{error}")235end236237return init_success238end239end240241242