-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Msf::Module::Platform#find_platform: Match known platforms before search #20166
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
base: master
Are you sure you want to change the base?
Conversation
Out of curiosity, how did you generate the stackprof dump? |
It's not stackprof; but fwiw we've got a RubyProf and memory_profiler API wrapper over here: Implementation: https://github.com/rapid7/metasploit-framework/blob/b5129fe19874e74d5a103bb9d1372fb30f618b32/lib/metasploit/framework/profiler.rb |
diff --git a/msfconsole b/msfconsole
index 96630b48cb..5face106e6 100755
--- a/msfconsole
+++ b/msfconsole
@@ -6,6 +6,7 @@
#
require 'pathname'
+require 'stackprof'
begin
# Silences warnings as they only serve to confuse end users
@@ -15,12 +16,11 @@ begin
# @see https://github.com/rails/rails/blob/v3.2.17/railties/lib/rails/generators/rails/app/templates/script/rails#L3-L5
require Pathname.new(__FILE__).realpath.expand_path.parent.join('config', 'boot')
- require 'metasploit/framework/profiler'
- Metasploit::Framework::Profiler.start
-
+StackProf.run(mode: :cpu, out: 'stackprof-output.dump') do
require 'msfenv'
require 'metasploit/framework/command/console'
Metasploit::Framework::Command::Console.start
+end
rescue Interrupt
puts "\nAborting..."
exit(1) |
For reasons I didn't bother to investigate, the existing profiling implementation is sub-optimal.
|
Thanks! That'll be fixed by #20168 now |
Profiling reveals a lot of time is spent parsing module platforms in
Msf::Module::Platform#find_platform
during startup.The
Msf::Module::Platform
class was introduced in a commit titled "Wow this is overly complex, I suckx0r" in 2005. Thefind_platform
method was added in a commit titled "Hmmm" on the following day.This allows us to dynamically define supported platforms as a string within module metadata (using the
Platform
key). Platforms can also be defined abbreviated (ie,win
); however,win
appears to be the only abbreviation we use for any platform.The basic structure of the
find_platform
method has not changed since it was introduced. It callsfind_portion
in a loop, which in turn performs multiple comparisons and iterates through abbreviations in an attempt to match the string to a platform.Searching platform names and abbreviations is an unnecessarily complex method to map platform strings to classes. There are only ~30 platforms in Metasploit. Most callers of this method do not need this functionality and could simply use a Hash lookup table.
These methods were authored in 2005 when Metasploit contained far fewer modules. Now this method is called approximately 37 thousand (!) times during startup. Due to the ever-increasing number of modules, this behaviour will cause the startup time to grow.
For now, this PR uses simple string matching for known platforms which consistently decreases startup time by about 10% on my system.
In the long term, a simple
Hash
defined inlib/msf/core/constants.rb
could be used to lookup platforms, here and elsewhere, instead.Before
After