Mirror of metasploit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db_manager.rb 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # -*- coding: binary -*-
  2. #
  3. # Gems
  4. #
  5. require 'rex/socket'
  6. #
  7. # Project
  8. #
  9. require 'metasploit/framework/require'
  10. require 'msf/base/config'
  11. require 'msf/core'
  12. require 'msf/core/database_event'
  13. require 'msf/core/db_import_error'
  14. require 'msf/core/host_state'
  15. require 'msf/core/service_state'
  16. # The db module provides persistent storage and events. This class should be instantiated LAST
  17. # as the active_suppport library overrides Kernel.require, slowing down all future code loads.
  18. class Msf::DBManager
  19. extend Metasploit::Framework::Require
  20. # Default proto for making new `Mdm::Service`s. This should probably be a
  21. # const on `Mdm::Service`
  22. DEFAULT_SERVICE_PROTO = "tcp"
  23. autoload :Adapter, 'msf/core/db_manager/adapter'
  24. autoload :Client, 'msf/core/db_manager/client'
  25. autoload :Connection, 'msf/core/db_manager/connection'
  26. autoload :Cred, 'msf/core/db_manager/cred'
  27. autoload :Event, 'msf/core/db_manager/event'
  28. autoload :ExploitAttempt, 'msf/core/db_manager/exploit_attempt'
  29. autoload :ExploitedHost, 'msf/core/db_manager/exploited_host'
  30. autoload :Host, 'msf/core/db_manager/host'
  31. autoload :HostDetail, 'msf/core/db_manager/host_detail'
  32. autoload :HostTag, 'msf/core/db_manager/host_tag'
  33. autoload :Import, 'msf/core/db_manager/import'
  34. autoload :ImportMsfXml, 'msf/core/db_manager/import_msf_xml'
  35. autoload :IPAddress, 'msf/core/db_manager/ip_address'
  36. autoload :Loot, 'msf/core/db_manager/loot'
  37. autoload :Migration, 'msf/core/db_manager/migration'
  38. autoload :ModuleCache, 'msf/core/db_manager/module_cache'
  39. autoload :Note, 'msf/core/db_manager/note'
  40. autoload :Ref, 'msf/core/db_manager/ref'
  41. autoload :Report, 'msf/core/db_manager/report'
  42. autoload :Route, 'msf/core/db_manager/route'
  43. autoload :Service, 'msf/core/db_manager/service'
  44. autoload :Session, 'msf/core/db_manager/session'
  45. autoload :SessionEvent, 'msf/core/db_manager/session_event'
  46. autoload :Task, 'msf/core/db_manager/task'
  47. autoload :Vuln, 'msf/core/db_manager/vuln'
  48. autoload :VulnAttempt, 'msf/core/db_manager/vuln_attempt'
  49. autoload :VulnDetail, 'msf/core/db_manager/vuln_detail'
  50. autoload :WMAP, 'msf/core/db_manager/wmap'
  51. autoload :Web, 'msf/core/db_manager/web'
  52. autoload :Workspace, 'msf/core/db_manager/workspace'
  53. optionally_include_metasploit_credential_creation
  54. include Msf::DBManager::Adapter
  55. include Msf::DBManager::Client
  56. include Msf::DBManager::Connection
  57. include Msf::DBManager::Cred
  58. include Msf::DBManager::Event
  59. include Msf::DBManager::ExploitAttempt
  60. include Msf::DBManager::ExploitedHost
  61. include Msf::DBManager::Host
  62. include Msf::DBManager::HostDetail
  63. include Msf::DBManager::HostTag
  64. include Msf::DBManager::Import
  65. include Msf::DBManager::IPAddress
  66. include Msf::DBManager::Loot
  67. include Msf::DBManager::Migration
  68. include Msf::DBManager::ModuleCache
  69. include Msf::DBManager::Note
  70. include Msf::DBManager::Ref
  71. include Msf::DBManager::Report
  72. include Msf::DBManager::Route
  73. include Msf::DBManager::Service
  74. include Msf::DBManager::Session
  75. include Msf::DBManager::SessionEvent
  76. include Msf::DBManager::Task
  77. include Msf::DBManager::Vuln
  78. include Msf::DBManager::VulnAttempt
  79. include Msf::DBManager::VulnDetail
  80. include Msf::DBManager::WMAP
  81. include Msf::DBManager::Web
  82. include Msf::DBManager::Workspace
  83. # Provides :framework and other accessors
  84. include Msf::Framework::Offspring
  85. #
  86. # Attributes
  87. #
  88. # Stores the error message for why the db was not loaded
  89. attr_accessor :error
  90. # Returns true if the prerequisites have been installed
  91. attr_accessor :usable
  92. #
  93. # initialize
  94. #
  95. def initialize(framework, opts = {})
  96. self.framework = framework
  97. self.migrated = false
  98. self.modules_cached = false
  99. self.modules_caching = false
  100. @usable = false
  101. # Don't load the database if the user said they didn't need it.
  102. if (opts['DisableDatabase'])
  103. self.error = "disabled"
  104. return
  105. end
  106. initialize_database_support
  107. end
  108. #
  109. # Instance Methods
  110. #
  111. #
  112. # Determines if the database is functional
  113. #
  114. def check
  115. ::ActiveRecord::Base.connection_pool.with_connection {
  116. res = ::Mdm::Host.first
  117. }
  118. end
  119. #
  120. # Do what is necessary to load our database support
  121. #
  122. def initialize_database_support
  123. begin
  124. # Database drivers can reset our KCODE, do not let them
  125. $KCODE = 'NONE' if RUBY_VERSION =~ /^1\.8\./
  126. add_rails_engine_migration_paths
  127. @usable = true
  128. rescue ::Exception => e
  129. self.error = e
  130. elog("DB is not enabled due to load error: #{e}")
  131. return false
  132. end
  133. #
  134. # Determine what drivers are available
  135. #
  136. initialize_adapter
  137. true
  138. end
  139. end