New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize JSON into MDM Objects #9394

Closed
wants to merge 57 commits into
base: master
from
Commits
Jump to file or symbol
Failed to load files and symbols.
+59 −131
Diff settings

Always

Just for now

Viewing a subset of changes. View all

More general cleanup including is_local db check

  • Loading branch information...
clee-r7 committed Oct 16, 2017
commit 2c8f27cd9866a82c35aa01afe50cff4891369ebd
View
@@ -23,7 +23,6 @@ group :development do
# module documentation
gem 'octokit'
# Metasploit::Aggregator external session proxy
# Disabled for now for crypttlv updates
# gem 'metasploit-aggregator'
end
View
@@ -110,8 +110,8 @@ GEM
bindata (2.4.1)
bit-struct (0.16)
builder (3.2.3)
coderay (1.1.1)
coderay (1.1.2)
daemons (1.2.4)
diff-lcs (1.3)
dnsruby (1.60.2)
docile (1.1.5)
@@ -315,7 +315,6 @@ GEM
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
slop (3.6.0)
sqlite3 (1.3.13)
sshkey (1.9.0)
thin (1.7.1)
@@ -29,6 +29,14 @@ def error
return "none"
end
def is_local?
if (@data_service)
return (@data_service.name == 'local_db_service')
end
return false
end
#
# Determines if the data service is active
#
@@ -117,7 +125,7 @@ def print_data_services()
# Used to bridge the local db
#
def method_missing(method, *args, &block)
puts "Attempting to delegate method: #{method}"
#puts "Attempting to delegate method: #{method}"
unless @data_service.nil?
@data_service.send(method, *args, &block)
end
@@ -10,7 +10,6 @@ def report_session(opts)
opts[:host] = opts[:host].address
end
#TODO: Fix
opts[:time_stamp] = Time.now.utc
self.post_data_async(SESSION_API_PATH, opts)
end
@@ -43,7 +42,7 @@ def parse_host_opts(msf_session)
def parse_session_data(msf_session)
hash = Hash.new()
# TODO: what to do with this shiz
# hash[:datastore] = msf_session.exploit_datastore.to_h
hash[:datastore] = msf_session.exploit_datastore.to_h
hash[:desc] = msf_session.info
hash[:local_id] = msf_session.sid
hash[:platform] = msf_session.session_type
@@ -137,7 +137,7 @@ def option_parser
options.database.config = path
end
option_parser.on('-dbrp', 'Run database as a remote process') do
option_parser.on('-dbrp', 'Run database as a separate local process') do
options.database.remote_process = true
end
@@ -61,14 +61,6 @@ def peer
super.ljust(21)
end
def formatted_duration (total_seconds)
hours = total_seconds / (60 * 60)
minutes = (total_seconds / 60) % 60
seconds = total_seconds % 60
" #{ hours } h #{ minutes } m #{ seconds } s"
end
#
# The command handler when launched from the console
#
@@ -121,8 +113,6 @@ def run
if (self.respond_to?('run_host'))
start_time = Time.now.to_i
loop do
# Stop scanning if we hit a fatal error
break if has_fatal_errors?
@@ -181,8 +171,6 @@ def run
scanner_show_progress() if @show_progress
end
puts "Time to scan hosts: #{formatted_duration(Time.now.to_i - start_time)}"
scanner_handle_fatal_errors
return
end
@@ -131,9 +131,7 @@ def initialize(framework, opts = {})
return
end
init_success = initialize_database_support
return init_success
return initialize_database_support
end
#
View
Binary file not shown.
View
@@ -232,7 +232,7 @@ def threads?
def search(match, logger: nil)
# Check if the database is usable
use_db = true
if self.db
if self.db and self.db.is_local?
if !(self.db.migrated && self.db.modules_cached)
logger.print_warning("Module database cache not built yet, using slow search") if logger
use_db = false
@@ -336,19 +336,19 @@ def on_module_error(instance, exception=nil)
include ::Msf::UiEventSubscriber
##
# :category: ::Msf::Uips -ef | EventSubscriber implementors
# :category: ::Msf::UiEventSubscriber implementors
def on_ui_command(command)
# if framework.db.active
# report_event(:name => "ui_command", :info => {:command => command})
# end
if (framework.db and framework.db.active)
report_event(:name => "ui_command", :info => {:command => command})
end
end
##
# :category: ::Msf::UiEventSubscriber implementors
def on_ui_stop()
# if framework.db.active
# report_event(:name => "ui_stop")
# end
if (framework.db and framework.db.active)
report_event(:name => "ui_stop")
end
end
##
@@ -131,8 +131,13 @@ def refresh_cache_from_database(allowed_paths=[""])
# @return [true] if migrations have been run
# @return [false] otherwise
def framework_migrated?
if (framework.db)
if (framework.db.is_local?)
return framework.db.migrated
end
end
return false
#framework.db && framework.db.migrated
end
# @!attribute [rw] module_info_by_path
@@ -98,6 +98,13 @@ def initialize(framework)
end
end
#
# Skip the database cleanup code below if there is no database
#
next if not (framework.db and framework.db.active and framework.db.is_local?)
#
# Mark all open session as alive every LAST_SEEN_INTERVAL
#
@@ -123,24 +130,19 @@ def initialize(framework)
end
#
# Skip the database cleanup code below if there is no database
#
#next if not (framework.db and framework.db.active)
#
# Clean out any stale sessions that have been orphaned by a dead
# framework instance.
#
# ::ActiveRecord::Base.connection_pool.with_connection do |conn|
# ::Mdm::Session.where(closed_at: nil).each do |db_session|
# if db_session.last_seen.nil? or ((Time.now.utc - db_session.last_seen) > (2*LAST_SEEN_INTERVAL))
# db_session.closed_at = db_session.last_seen || Time.now.utc
# db_session.close_reason = "Orphaned"
# db_session.save
# end
# end
# end
::ActiveRecord::Base.connection_pool.with_connection do |conn|
::Mdm::Session.where(closed_at: nil).each do |db_session|
if db_session.last_seen.nil? or ((Time.now.utc - db_session.last_seen) > (2*LAST_SEEN_INTERVAL))
db_session.closed_at = db_session.last_seen || Time.now.utc
db_session.close_reason = "Orphaned"
db_session.save
end
end
end
end
#
@@ -107,16 +107,16 @@ def spawn(name, crit, *args, &block)
)
elog("Call Stack\n#{e.backtrace.join("\n")}")
raise e
# ensure
# if framework.db and framework.db.active
# # NOTE: despite the Deprecation Warning's advice, this should *NOT*
# # be ActiveRecord::Base.connection.close which causes unrelated
# # threads to raise ActiveRecord::StatementInvalid exceptions at
# # some point in the future, presumably due to the pool manager
# # believing that the connection is still usable and handing it out
# # to another thread.
# ::ActiveRecord::Base.connection_pool.release_connection
# end
ensure
if framework.db and framework.db.active framework.db.is_local?
# NOTE: despite the Deprecation Warning's advice, this should *NOT*
# be ActiveRecord::Base.connection.close which causes unrelated
# threads to raise ActiveRecord::StatementInvalid exceptions at
# some point in the future, presumably due to the pool manager
# believing that the connection is still usable and handing it out
# to another thread.
::ActiveRecord::Base.connection_pool.release_connection
end
end
end
else
@@ -122,22 +122,6 @@ def initialize(prompt = DefaultPrompt, prompt_char = DefaultPromptChar, opts = {
enstack_dispatcher(dispatcher)
end
# Add the database dispatcher if it is usable
# if (framework.db.usable)
# require 'msf/ui/console/command_dispatcher/db'
# enstack_dispatcher(CommandDispatcher::Db)
# require 'msf/ui/console/command_dispatcher/creds'
# enstack_dispatcher(CommandDispatcher::Creds)
# else
# print_error("***")
# if framework.db.error == "disabled"
# print_error("* WARNING: Database support has been disabled")
# else
# print_error("* WARNING: No database support: #{framework.db.error.class} #{framework.db.error}")
# end
# print_error("***")
# end
framework.db.init(framework, opts)
if (framework.db.active)
require 'msf/ui/console/command_dispatcher/db'
@@ -175,71 +159,17 @@ def initialize(prompt = DefaultPrompt, prompt_char = DefaultPromptChar, opts = {
# Whether or not to confirm before exiting
self.confirm_exit = opts['ConfirmExit']
# Parse any specified database.yml file
# if framework.db.usable and not opts['SkipDatabaseInit']
#
# # 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 framework.db.connection_established?
# framework.db.after_establish_connection
# else
# configuration_pathname = Metasploit::Framework::Database.configurations_pathname(path: opts['DatabaseYAML'])
#
# unless configuration_pathname.nil?
# if configuration_pathname.readable?
# dbinfo = YAML.load_file(configuration_pathname) || {}
# dbenv = opts['DatabaseEnv'] || Rails.env
# db = dbinfo[dbenv]
# else
# print_error("Warning, #{configuration_pathname} is not readable. Try running as root or chmod.")
# end
#
# if not db
# print_error("No database definition for environment #{dbenv}")
# else
# framework.db.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 !! framework.db.error
# if framework.db.error.to_s =~ /RubyGem version.*pg.*0\.11/i
# print_error("***")
# print_error("*")
# print_error("* Metasploit now requires version 0.11 or higher of the 'pg' gem for database support")
# print_error("* There a three ways to accomplish this upgrade:")
# print_error("* 1. If you run Metasploit with your system ruby, simply upgrade the gem:")
# print_error("* $ rvmsudo gem install pg ")
# print_error("* 2. Use the Community Edition web interface to apply a Software Update")
# print_error("* 3. Uninstall, download the latest version, and reinstall Metasploit")
# print_error("*")
# print_error("***")
# print_error("")
# print_error("")
# end
#
# print_error("Failed to connect to the database: #{framework.db.error}")
# end
# end
# Initialize the module paths only if we didn't get passed a Framework instance and 'DeferModuleLoads' is false
unless opts['Framework'] || opts['DeferModuleLoads']
# Configure the framework module paths
self.framework.init_module_paths(module_paths: opts['ModulePath']) #, is_remote_database: opts['DatabaseRemoteProcess'])
self.framework.init_module_paths(module_paths: opts['ModulePath'])
end
# if framework.db.active && !opts['DeferModuleLoads']
# framework.threads.spawn("ModuleCacheRebuild", true) do
# framework.modules.refresh_cache_from_module_files
# end
# end
if framework.db.active && framework.db.is_local? && !opts['DeferModuleLoads']
framework.threads.spawn("ModuleCacheRebuild", true) do
framework.modules.refresh_cache_from_module_files
end
end
# Load console-specific configuration (after module paths are added)
load_config(opts['Config'])
ProTip! Use n and p to navigate between commits in a pull request.