Skip to content
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

Improve boot time #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ group :development do
# Metasploit::Aggregator external session proxy
# disabled during 2.5 transition until aggregator is available
#gem 'metasploit-aggregator'
gem 'stackprof'
end

group :development, :test do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ GEM
tilt (~> 2.0)
sqlite3 (1.3.13)
sshkey (2.0.0)
stackprof (0.2.15)
swagger-blocks (3.0.0)
thin (1.7.2)
daemons (~> 1.0, >= 1.0.9)
Expand Down Expand Up @@ -457,6 +458,7 @@ DEPENDENCIES
ruby-prof
simplecov (= 0.18.2)
sqlite3 (~> 1.3.0)
stackprof
swagger-blocks
timecop
yard
Expand Down
2 changes: 1 addition & 1 deletion lib/metasploit/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require 'active_support'
require 'bcrypt'
require 'json'
require 'msgpack'
# require 'msgpack'
require 'metasploit/credential'
require 'nokogiri'
require 'packetfu'
Expand Down
33 changes: 17 additions & 16 deletions lib/metasploit/framework/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ class Metasploit::Framework::Command::Base
def self.require_environment!
parsed_options = self.parsed_options
# RAILS_ENV must be set before requiring 'config/application.rb'
parsed_options.environment!
ARGV.replace(parsed_options.positional)

# allow other Rails::Applications to use this command
if !defined?(Rails) || Rails.application.nil?
# @see https://github.com/rails/rails/blob/v3.2.17/railties/lib/rails/commands.rb#L39-L40
require Pathname.new(__FILE__).parent.parent.parent.parent.parent.join('config', 'application')
end

# have to configure before requiring environment because
# config/environment.rb calls initialize! and the initializers will use
# the configuration from the parsed options.
parsed_options.configure(Rails.application)

Rails.application.require_environment!
# parsed_options.environment!
# ARGV.replace(parsed_options.positional)
#
# # allow other Rails::Applications to use this command
# if !defined?(Rails) || Rails.application.nil?
# # @see https://github.com/rails/rails/blob/v3.2.17/railties/lib/rails/commands.rb#L39-L40
# require Pathname.new(__FILE__).parent.parent.parent.parent.parent.join('config', 'application')
# end
#
# # have to configure before requiring environment because
# # config/environment.rb calls initialize! and the initializers will use
# # the configuration from the parsed options.
# parsed_options.configure(Rails.application)
#
# Rails.application.require_environment!

parsed_options
end
Expand All @@ -79,7 +79,8 @@ def self.parsed_options_class_name

def self.start
parsed_options = require_environment!
new(application: Rails.application, parsed_options: parsed_options).start
# new(application: Rails.application, parsed_options: parsed_options).start
new(application: nil, parsed_options: parsed_options).start
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/metasploit/framework/command/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def start
def driver
unless @driver
# require here so minimum loading is done before {start} is called.
require 'msf/ui'
require 'msf/ui/console/driver'

@driver = Msf::Ui::Console::Driver.new(
Msf::Ui::Console::Driver::DefaultPrompt,
Expand Down
6 changes: 5 additions & 1 deletion lib/metasploit/framework/thread_factory_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
# # framework.threads created here
# Rex::ThreadFactory.spawn("name", false) { ... }
#
class Metasploit::Framework::ThreadFactoryProvider < Metasploit::Model::Base
class Metasploit::Framework::ThreadFactoryProvider # WTF??? < Metasploit::Model::Base
#
# Attributes
#

def initialize(framework:)
self.framework = framework
end

# @!attribute framework
# The framework managing the spawned threads.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/base/logging.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: binary -*-
require 'rex'
require 'msf/base'
# require 'msf/base'

module Msf

Expand Down
16 changes: 8 additions & 8 deletions lib/msf/base/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
require 'msf/base/simple/statistics'

# Simplified module interfaces
require 'msf/base/simple/module'
require 'msf/base/simple/encoder'
require 'msf/base/simple/exploit'
require 'msf/base/simple/nop'
require 'msf/base/simple/payload'
require 'msf/base/simple/auxiliary'
require 'msf/base/simple/post'
require 'msf/base/simple/evasion'
# require 'msf/base/simple/module'
# require 'msf/base/simple/encoder'
# require 'msf/base/simple/exploit'
# require 'msf/base/simple/nop'
# require 'msf/base/simple/payload'
# require 'msf/base/simple/auxiliary'
# require 'msf/base/simple/post'
# require 'msf/base/simple/evasion'

# Simplified framework interface
require 'msf/base/simple/framework'
2 changes: 1 addition & 1 deletion lib/msf/base/simple/buffer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: binary -*-

require 'msf/base'
# require 'msf/base'

module Msf
module Simple
Expand Down
35 changes: 20 additions & 15 deletions lib/msf/base/simple/framework.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# -*- coding: binary -*-
require 'msf/base/simple'
# require 'msf/base/simple'
require 'msf/base/simple/framework/module_paths'
require 'msf/base/simple/noop_job_listener'
require 'msf/base/simple/statistics'
require 'msf/base/logging'


module Msf
module Simple
Expand Down Expand Up @@ -50,18 +53,19 @@ def load(path, opts = {})
# Simplifies module instances when they're created.
#
def on_module_created(instance)
Msf::Simple::Framework.simplify_module(instance)
# Msf::Simple::Framework.simplify_module(instance)
instance
end

ModuleSimplifiers =
{
Msf::MODULE_ENCODER => Msf::Simple::Encoder,
Msf::MODULE_EXPLOIT => Msf::Simple::Exploit,
Msf::MODULE_NOP => Msf::Simple::Nop,
Msf::MODULE_PAYLOAD => Msf::Simple::Payload,
Msf::MODULE_AUX => Msf::Simple::Auxiliary,
Msf::MODULE_POST => Msf::Simple::Post,
Msf::MODULE_EVASION => Msf::Simple::Evasion
# Msf::MODULE_ENCODER => Msf::Simple::Encoder,
# Msf::MODULE_EXPLOIT => Msf::Simple::Exploit,
# Msf::MODULE_NOP => Msf::Simple::Nop,
# Msf::MODULE_PAYLOAD => Msf::Simple::Payload,
# Msf::MODULE_AUX => Msf::Simple::Auxiliary,
# Msf::MODULE_POST => Msf::Simple::Post,
# Msf::MODULE_EVASION => Msf::Simple::Evasion
}

# Create a simplified instance of the framework. This routine takes a hash
Expand Down Expand Up @@ -131,12 +135,13 @@ def self.simplify(framework, opts)
# with the simplified module interface.
#
def self.simplify_module(instance, load_saved_config = true)
if ((ModuleSimplifiers[instance.type]) and
(instance.class.include?(ModuleSimplifiers[instance.type]) == false))
instance.extend(ModuleSimplifiers[instance.type])

instance.init_simplified(load_saved_config)
end
# if ((ModuleSimplifiers[instance.type]) and
# (instance.class.include?(ModuleSimplifiers[instance.type]) == false))
# instance.extend(ModuleSimplifiers[instance.type])
#
# instance.init_simplified(load_saved_config)
# end
instance
end


Expand Down
17 changes: 9 additions & 8 deletions lib/msf/core/db_manager/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,15 @@ def create_db(opts)
# @return [true] if an active connection can be made to the database using the current config.
# @return [false] if an active connection cannot be made to the database.
def connection_established?
begin
# use with_connection so the connection doesn't stay pinned to the thread.
ApplicationRecord.connection_pool.with_connection {
ApplicationRecord.connection.active?
}
rescue ActiveRecord::ConnectionNotEstablished, PG::ConnectionBad => error
false
end
# begin
# # use with_connection so the connection doesn't stay pinned to the thread.
# ApplicationRecord.connection_pool.with_connection {
# ApplicationRecord.connection.active?
# }
# rescue ActiveRecord::ConnectionNotEstablished, PG::ConnectionBad => error
# false
# end
false
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def search(match, logger: nil)
private

def get_db
unless options['DisableDatabase']
unless options['DisableDatabase'] || true
db_manager = Msf::DBManager.new(self)
options[:db_manager] = db_manager
unless options['SkipDatabaseInit']
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/ui/console/command_dispatcher/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
require 'msf/ui/console/command_dispatcher/resource'
require 'msf/ui/console/command_dispatcher/modules'
require 'msf/ui/console/command_dispatcher/developer'
require 'msf/util/document_generator'
# require 'msf/util/document_generator'

require 'optparse'

Expand Down
1 change: 1 addition & 0 deletions lib/msf/ui/console/command_dispatcher/payload.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: binary -*-

require 'rex/parser/arguments'
require 'msf/base/simple/buffer.rb'

module Msf
module Ui
Expand Down
72 changes: 37 additions & 35 deletions lib/msf/ui/console/driver.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# -*- coding: binary -*-
require 'msf/core'
require 'msf/base'
require 'msf/ui'
# require 'msf/base'
require 'msf/base/simple/framework'
# require 'msf/ui'
require 'msf/ui/driver'
require 'msf/ui/console/framework_event_manager'
require 'msf/ui/console/command_dispatcher'
require 'msf/ui/console/command_dispatcher/db'
require 'msf/ui/console/command_dispatcher/creds'
# require 'msf/ui/console/command_dispatcher/db'
# require 'msf/ui/console/command_dispatcher/creds'
require 'msf/ui/console/table'
require 'find'
require 'erb'
Expand Down Expand Up @@ -36,8 +38,8 @@ class Driver < Msf::Ui::Driver
CommandDispatcher::Modules,
CommandDispatcher::Jobs,
CommandDispatcher::Resource,
CommandDispatcher::Db,
CommandDispatcher::Creds,
# CommandDispatcher::Db,
# CommandDispatcher::Creds,
CommandDispatcher::Developer
]

Expand Down Expand Up @@ -232,35 +234,35 @@ def load_preconfig
end

def load_db_config(path=nil)
begin
conf = Msf::Config.load(path)
rescue
wlog("Failed to load configuration: #{$!}")
return
end

if conf.group?(DbConfigGroup)
conf[DbConfigGroup].each_pair do |k, v|
if k.downcase == 'default_db'
ilog "Default data service found. Attempting to connect..."
default_db_config_path = "#{DbConfigGroup}/#{v}"
default_db = conf[default_db_config_path]
if default_db
connect_string = "db_connect #{v}"

if framework.db.active && default_db['url'] !~ /http/
ilog "Existing local data connection found. Disconnecting first."
run_single("db_disconnect")
end

run_single(connect_string)
else
elog "Config entry for '#{default_db_config_path}' could not be found. Config file might be corrupt."
return
end
end
end
end
# begin
# conf = Msf::Config.load(path)
# rescue
# wlog("Failed to load configuration: #{$!}")
# return
# end
#
# if conf.group?(DbConfigGroup)
# conf[DbConfigGroup].each_pair do |k, v|
# if k.downcase == 'default_db'
# ilog "Default data service found. Attempting to connect..."
# default_db_config_path = "#{DbConfigGroup}/#{v}"
# default_db = conf[default_db_config_path]
# if default_db
# connect_string = "db_connect #{v}"
#
# if framework.db.active && default_db['url'] !~ /http/
# ilog "Existing local data connection found. Disconnecting first."
# run_single("db_disconnect")
# end
#
# run_single(connect_string)
# else
# elog "Config entry for '#{default_db_config_path}' could not be found. Config file might be corrupt."
# return
# end
# end
# end
# end
end

#
Expand Down
8 changes: 5 additions & 3 deletions msfconsole
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#

require 'pathname'

require 'stackprof'
begin

# Silences warnings as they only serve to confuse end users
Expand All @@ -19,8 +19,10 @@ begin
require 'metasploit/framework/profiler'
require 'metasploit/framework/command/console'
require 'msf/core/payload_generator'
Metasploit::Framework::Profiler.start
Metasploit::Framework::Command::Console.start
# Metasploit::Framework::Profiler.start
# StackProf.run(mode: :wall, out: 'test_prof_wall_time.dump', raw: true, ignore_gc: true) do
Metasploit::Framework::Command::Console.start
# end
rescue Interrupt
puts "\nAborting..."
exit(1)
Expand Down