Skip to content

[WIP] 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 2 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

…s, adding AutoLoadExtensions option (Windows, Linux)
Comment on lines +195 to +197
console.run_single("load #{extension}")
console.run_single('unhook_pe') if extension == 'unhook'
session.load_session_info if extension == 'stdapi' && datastore['AutoSystemInfo']
Copy link
Contributor Author

Choose a reason for hiding this comment

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

# TODO: abstract this a little, perhaps a "post load" function that removes
# platform-specific stuff?

…s, adding AutoLoadExtensions option (AppleIos,Php,Python,Java,Osx,Android)
@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

Comment on lines +17 to +20
OptString.new(
'AutoLoadExtensions',
[true, "Automatically load extensions on bootstrap, semicolon separated.", 'stdapi']
),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
OptString.new(
'AutoLoadExtensions',
[true, "Automatically load extensions on bootstrap, semicolon separated.", 'stdapi']
),
OptString.new(
'AutoLoadExtensions',
[true, "Automatically load extensions on bootstrap, semicolon separated.", 'stdapi']
),
OptString.new(
'PayloadProcessCommandLine',
[ false, 'The displayed command line that will be used by the payload', '']
),

@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
Comment on lines +186 to +188
extensions.push('unhook') if datastore['AutoUnhookProcess'] && session.platform == 'windows'
extensions.push('stdapi') if datastore['AutoLoadStdapi']
extensions.push('priv') if datastore['AutoLoadStdapi'] && session.platform('windows')
Copy link
Contributor

Choose a reason for hiding this comment

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

is it session.platform('windows') or `session.platform == 'windows' ? I would be nice to pick one and stick to it.

@@ -180,32 +180,23 @@ def bootstrap(datastore = {}, handler = nil)
print_warning('Meterpreter start up operations have been aborted. Use the session at your own risk.')
return nil
end
# Unhook the process prior to loading stdapi to reduce logging/inspection by any AV/PSP
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to keep this comment I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it would be incorrect as now the loading order is determined by the order in ';' list.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then maybe next to the default ; list, as it's not obvious why unhook is better loaded before stdapi.

@dledda-r7 dledda-r7 marked this pull request as ready for review April 15, 2025 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Blocked by one or more additional tasks payload rn-payload-enhancement release notes for enhanced payloads
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants