Skip to content

MeterpreterOptions break-up and default extension loading removal #20012

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

dledda-r7
Copy link
Contributor

@dledda-r7 dledda-r7 commented Apr 8, 2025

This PR is related to #19975
In order to reduce the memory footprint of Meterpreter on target system is necessary to be sure only necessary code is loaded inside Meterpreter.

While working on standard api split, I noticed the presence of two extensions that were loaded without the user knowladge, priv and unhook.
This PR modifies the loading logic to use a datastore option AutoLoadExtension which contains a ';' separated list of extensions to load. At the moment inside lib/msf/base/sessions/meterpreter.rb is present a code block (commented) that keep the compability of the current working logic of Meterpreter.

Since the AutoLoadExtension logic depends on the platform, the break-up of meterpreter_option.rb to platform-specific option was necessary.

Tasks:

  • meterpreter_options/common.rb
  • meterpreter_options/linux.rb
  • meterpreter_options/windows.rb
  • meterpreter_options/android.rb
  • meterpreter_options/osx.rb
  • meterpreter_options/python.rb
  • meterpreter_options/php.rb
  • meterpreter_options/java.rb
  • meterpreter_options/apple_ios.rb
  • meterpreter_options/bsd.rb
  • Remove meterpreter_options.rb

@dledda-r7
Copy link
Contributor Author

dledda-r7 commented Apr 9, 2025

Also blocked by #19799
because need of modify: tools/modules/meterpreter_reverse.erb

To future me: modify it to have the architecutre specific MeterpreterOptions

Let's resolve the conflicts once one of them is landed.

@dledda-r7 dledda-r7 changed the title [WIP] Remove default loaded extensions on Meterpreter [WIP] MeterpreterOptions break-up and default extension loading removal Apr 9, 2025
@dledda-r7 dledda-r7 self-assigned this Apr 9, 2025
@dledda-r7 dledda-r7 added payload rn-payload-enhancement release notes for enhanced payloads labels Apr 9, 2025
@dledda-r7 dledda-r7 moved this to In Progress in Metasploit Kanban Apr 9, 2025
@dledda-r7 dledda-r7 added the blocked Blocked by one or more additional tasks label Apr 9, 2025
@dledda-r7 dledda-r7 marked this pull request as ready for review April 15, 2025 10:37
@dledda-r7 dledda-r7 changed the title [WIP] MeterpreterOptions break-up and default extension loading removal MeterpreterOptions break-up and default extension loading removal Jun 2, 2025
@dledda-r7 dledda-r7 force-pushed the feat/remove-default-extensions branch from 9c568c0 to 70bafdf Compare June 2, 2025 12:08
@dledda-r7 dledda-r7 removed the blocked Blocked by one or more additional tasks label Jun 2, 2025
@dledda-r7 dledda-r7 mentioned this pull request Jun 2, 2025
@dledda-r7 dledda-r7 removed their assignment Jun 11, 2025
@msutovsky-r7 msutovsky-r7 self-assigned this Jun 27, 2025
end
end

extensions = datastore['AutoLoadExtensions']&.split(';') || []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth a quick data-mine to see if splitting by , is more common as a pattern, as well as stripping whitespace etc, to handle stdapi, priv etc

Long-term: Maybe we should have an Opt for supporting a comma separated values for handling this consistently

if session.platform == 'android'
session.load_android
extensions.each do |extension|
console.run_single("load #{extension}")
Copy link
Contributor

@adfoster-r7 adfoster-r7 Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should have some begin/rescue/ensure block mechanisms here so that if an extension fails to load, the output can be safely re-enabled again

@@ -8,7 +8,7 @@
module MetasploitModule

include Msf::Payload::Single
include Msf::Sessions::MeterpreterOptions
include Msf::Sessions::MeterpreterOptions::<%= platform.split('_').each { |s| s.capitalize! }.join %>
Copy link
Contributor

@adfoster-r7 adfoster-r7 Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
include Msf::Sessions::MeterpreterOptions::<%= platform.split('_').each { |s| s.capitalize! }.join %>
include Msf::Sessions::MeterpreterOptions::<%= platform.split('_').each { |s| s.casecmp?('osx') ? 'OSX' : s.capitalize! }.join %>

Or we can change lib/msf_autoload.rb - but I believe OSX is the existing convention to follow


extensions = datastore['AutoLoadExtensions']&.split(';') || []

# BEGIN: This should be removed on MSF 7
Copy link
Contributor

@adfoster-r7 adfoster-r7 Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can keep this around in MSF 7 still; maybe we could just change the default behaviors in the upcoming malleable profile config file setup that we're wanting to ship so that these aren't loaded by default, and they need to be opt-in. That way we're being less detected by default, rather than fighting against these older default values

# BEGIN: This should be removed on MSF 7
# Unhook the process prior to loading stdapi to reduce logging/inspection by any AV/PSP (by default unhook is first, see meterpreter_options/windows.rb)
extensions.push('unhook') if datastore['AutoUnhookProcess'] && session.platform == 'windows'
extensions.push('stdapi') if datastore['AutoLoadStdapi']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering whether this isn't maybe bit redundant in some cases:
payload/linux/x64/meterpreter_reverse_tcp has two advanced datastore options AutoLoadExtensions and AutoLoadStdapi, which basically do the same thing (?). The AutoLoadExtensions has stdapi as value and AutoLoadStdapi is set to true, so if I understand correctly, it does the same thing. Wouldn't it be better to either set AutoLoadStdapi to false by default or remove stdapi from AutoLoadExtensions for those payloads which have AutoLoadStdapi set to true?

@@ -6,7 +6,8 @@
require 'securerandom'

module MetasploitModule
include Msf::Sessions::MeterpreterOptions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
payload rn-payload-enhancement release notes for enhanced payloads
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants