Skip to content

Commit cbfef6e

Browse files
incoporating jvazquez feedback
1 parent 6b330ad commit cbfef6e

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

modules/auxiliary/admin/smb/psexec_command.rb

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def initialize(info = {})
5353

5454
register_advanced_options([
5555
OptString.new('FILEPREFIX', [false, 'Add a custom prefix to the temporary files','']),
56-
OptInt.new('DELAY', [true, 'Wait this many seconds before reading output and cleaning up', 1]),
57-
OptInt.new('RETRY', [true, 'Retry this many times to check if the process is complete', 10]),
56+
OptInt.new('DELAY', [true, 'Wait this many seconds before reading output and cleaning up', 0]),
57+
OptInt.new('RETRY', [true, 'Retry this many times to check if the process is complete', 0]),
5858
OptPath.new('LOGDIR', [false, 'File to log output', nil]),
5959
], self.class)
6060

@@ -82,16 +82,16 @@ def run_host(ip)
8282
end
8383
res = execute_command(text, bat)
8484

85-
for i in 0..(datastore['RETRY'])
86-
sleep datastore['DELAY']
87-
# if the output file is still locked then the program is still likely running
88-
if (exclusive_access(text))
89-
break
90-
elsif (i == datastore['RETRY'])
91-
print_error("Command seems to still be executing. Try increasing RETRY and DELAY")
92-
end
93-
end
9485
if res
86+
for i in 0..(datastore['RETRY'])
87+
Rex.sleep(datastore['DELAY'])
88+
# if the output file is still locked then the program is still likely running
89+
if (exclusive_access(text))
90+
break
91+
elsif (i == datastore['RETRY'])
92+
print_error("Command seems to still be executing. Try increasing RETRY and DELAY")
93+
end
94+
end
9595
get_output(text)
9696
end
9797

@@ -129,27 +129,20 @@ def get_output(file)
129129
log_dir = ::File.join(Msf::Config.log_directory,'scripts', 'psexec_command')
130130
::FileUtils.mkdir_p(log_dir)
131131

132-
# Define log filename
133-
timestamp = ::Time.now.strftime('%Y%m%d:%H%M%S')
134-
filename = "#{datastore['RHOSTS']}_#{timestamp}"
135-
if (datastore['LOGDIR'].nil?)
136-
log_file = ::File.join(log_dir,"#{filename}.txt")
137-
else
138-
log_file = ::File.join(datastore['LOGDIR'], "#{filename}.txt")
139-
end
140-
132+
# Report output
141133
print_good("#{peer} - Command completed successfuly!")
142-
print_status("Logging output to #{log_file}.")
143-
output = "# CMD: #{datastore['COMMAND']}" + output
144-
145-
fd = ::File.new(log_file, 'w+')
146-
fd.write(output)
147-
fd.close()
148-
149-
if datastore["VERBOSE"]
150-
print_status("Output:")
134+
if datastore['VERBOSE']
135+
print_status("Output for \"#{datastore['COMMAND']}\":")
151136
print_line("#{output}")
152137
end
138+
139+
report_note(
140+
:host => datastore['RHOSTS'],
141+
:type => "psexec_command",
142+
:name => datastore['COMMAND'],
143+
:data => output
144+
)
145+
153146
end
154147

155148
#check if our process is done using these files
@@ -164,6 +157,7 @@ def exclusive_access(*files)
164157
print_status("#{peer} - Unable to get handle: #{accesserror}")
165158
return false
166159
end
160+
simple.disconnect("\\\\#{@ip}\\#{@smbshare}")
167161
end
168162
return true
169163
end

0 commit comments

Comments
 (0)