Skip to content

Commit ede804e

Browse files
committed
Make psexec mixin a bit better
* Removes copy-pasted code from psexec_command module and uses the mixin instead * Uses the SMB protocol to delete files rather than psexec'ing to call cmd.exe and del * Replaces several instances of "rescue StandardError" with better exception handling so we don't accidentally swallow things like NoMethodError * Moves file reading and existence checking into the Exploit::SMB mixin
1 parent 49f00ac commit ede804e

File tree

3 files changed

+148
-276
lines changed

3 files changed

+148
-276
lines changed

lib/msf/core/exploit/psexec.rb

Lines changed: 50 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,57 @@
11
require 'msf/core'
2+
require 'msf/core/exploit/dcerpc'
23

34
module Msf
45

56
####
6-
# This module alows for reuse of the psexec code execution module
7-
# This code was stolen straight out of psexec.rb.Thanks very much for all
8-
# who contributed to that module!! Instead of uploading and runing a binary.
7+
# Allows for reuse of the psexec code execution technique
8+
#
9+
# This code was stolen straight out of the psexec module. Thanks very
10+
# much for all who contributed to that module!! Instead of uploading
11+
# and runing a binary.
912
####
1013

1114
module Exploit::Remote::Psexec
1215

1316
include Msf::Exploit::Remote::DCERPC
1417
include Msf::Exploit::Remote::SMB
1518

16-
1719
# Retrives output from the executed command
20+
#
1821
# @param smbshare [String] The SMBshare to connect to. Usually C$
19-
# @param ip [IP Address] Remote Host to Connect To
20-
# @param file [File name] Path to the output file relative to the smbshare
21-
# Example: '\WINDOWS\Temp\outputfile.txt'
22-
# @return output or nil if fails
23-
def get_output(smbshare, ip, file)
22+
# @param host [String] Remote host to connect to, as an IP address or
23+
# hostname
24+
# @param file [String] Path to the output file relative to the smbshare
25+
# Example: '\WINDOWS\Temp\outputfile.txt'
26+
# @return [String,nil] output or nil on failure
27+
def smb_read_file(smbshare, host, file)
2428
begin
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 Rex::Proto::SMB::Exceptions::ErrorCode => output_error
32-
print_error("#{peer} - The file #{file} doesn't exist. #{output_error}.")
29+
simple.connect("\\\\#{host}\\#{smbshare}")
30+
file = simple.open(file, 'ro')
31+
contents = file.read
32+
file.close
33+
simple.disconnect("\\\\#{host}\\#{smbshare}")
34+
return contents
35+
rescue Rex::Proto::SMB::Exceptions::ErrorCode => e
36+
print_error("#{peer} - Unable to read file #{file}. #{e.class}: #{e}.")
3337
return nil
3438
end
3539
end
3640

3741

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.
42+
# Executes a single windows command.
43+
#
44+
# If you want to retrieve the output of your command you'll have to
45+
# echo it to a .txt file and then use the {#smb_read_file} method to
46+
# retrieve it. Make sure to remove the files manually or use
47+
# {Exploit::FileDropper#register_files_for_cleanup} to have the
48+
# {Exploit::FileDropper#cleanup} and
49+
# {Exploit::FileDropper#on_new_session} handlers do it for you.
50+
#
51+
# @todo Figure out the actual exceptions this needs to deal with
52+
# instead of all the ghetto "rescue ::Exception" madness
4253
# @param command [String] Should be a valid windows command
43-
# @return true if everything wen't well
54+
# @return [Boolean] Whether everything went well
4455
def psexec(command)
4556
simple.connect("\\\\#{datastore['RHOST']}\\IPC$")
4657
handle = dcerpc_handle('367abb81-9844-35f1-ad32-98f038001003', '2.0', 'ncacn_np', ["\\svcctl"])
@@ -49,8 +60,7 @@ def psexec(command)
4960
vprint_status("#{peer} - Bound to #{handle} ...")
5061
vprint_status("#{peer} - Obtaining a service manager handle...")
5162
scm_handle = nil
52-
stubdata =
53-
NDR.uwstring("\\\\#{rhost}") + NDR.long(0) + NDR.long(0xF003F)
63+
stubdata = NDR.uwstring("\\\\#{rhost}") + NDR.long(0) + NDR.long(0xF003F)
5464
begin
5565
response = dcerpc.call(0x0f, stubdata)
5666
if dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil
@@ -66,19 +76,19 @@ def psexec(command)
6676
svc_handle = nil
6777
svc_status = nil
6878
stubdata =
69-
scm_handle + NDR.wstring(servicename) + NDR.uwstring(displayname) +
70-
NDR.long(0x0F01FF) + # Access: MAX
71-
NDR.long(0x00000110) + # Type: Interactive, Own process
72-
NDR.long(0x00000003) + # Start: Demand
73-
NDR.long(0x00000000) + # Errors: Ignore
74-
NDR.wstring( command ) +
75-
NDR.long(0) + # LoadOrderGroup
76-
NDR.long(0) + # Dependencies
77-
NDR.long(0) + # Service Start
78-
NDR.long(0) + # Password
79-
NDR.long(0) + # Password
80-
NDR.long(0) + # Password
81-
NDR.long(0) # Password
79+
scm_handle + NDR.wstring(servicename) + NDR.uwstring(displayname) +
80+
NDR.long(0x0F01FF) + # Access: MAX
81+
NDR.long(0x00000110) + # Type: Interactive, Own process
82+
NDR.long(0x00000003) + # Start: Demand
83+
NDR.long(0x00000000) + # Errors: Ignore
84+
NDR.wstring( command ) +
85+
NDR.long(0) + # LoadOrderGroup
86+
NDR.long(0) + # Dependencies
87+
NDR.long(0) + # Service Start
88+
NDR.long(0) + # Password
89+
NDR.long(0) + # Password
90+
NDR.long(0) + # Password
91+
NDR.long(0) # Password
8292
begin
8393
vprint_status("#{peer} - Creating the service...")
8494
response = dcerpc.call(0x0c, stubdata)
@@ -97,8 +107,7 @@ def psexec(command)
97107
end
98108
vprint_status("#{peer} - Opening service...")
99109
begin
100-
stubdata =
101-
scm_handle + NDR.wstring(servicename) + NDR.long(0xF01FF)
110+
stubdata = scm_handle + NDR.wstring(servicename) + NDR.long(0xF01FF)
102111
response = dcerpc.call(0x10, stubdata)
103112
if dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil
104113
svc_handle = dcerpc.last_response.stub_data[0,20]
@@ -108,8 +117,7 @@ def psexec(command)
108117
return false
109118
end
110119
vprint_status("#{peer} - Starting the service...")
111-
stubdata =
112-
svc_handle + NDR.long(0) + NDR.long(0)
120+
stubdata = svc_handle + NDR.long(0) + NDR.long(0)
113121
begin
114122
response = dcerpc.call(0x13, stubdata)
115123
if dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil
@@ -119,8 +127,7 @@ def psexec(command)
119127
return false
120128
end
121129
vprint_status("#{peer} - Removing the service...")
122-
stubdata =
123-
svc_handle
130+
stubdata = svc_handle
124131
begin
125132
response = dcerpc.call(0x02, stubdata)
126133
if dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil
@@ -139,52 +146,6 @@ def psexec(command)
139146
return true
140147
end
141148

142-
143-
# This method is called by file_dropper to remove files droped
144-
# By your module
145-
#
146-
# @example
147-
# file_rm('C:\WINDOWS\Temp\output.txt')
148-
#
149-
# @param file [String] Full path to a file on the remote host
150-
# @return [StandardError] only in the event of an error
151-
def file_rm(file)
152-
delete = "%COMSPEC% /C del #{file}"
153-
vprint_status("#{peer} - Deleting #{file}")
154-
psexec(delete)
155-
end
156-
157-
158-
# This method stores files in an Instance array
159-
# The files are then deleted from the remote host once
160-
# the cleanup_after method is called
161-
#
162-
# @example
163-
# register_file_for_cleanup("C:\\WINDOWS\\Temp\\output.txt")
164-
# @param file [String] Full path to the file on the remote host
165-
def register_file_for_cleanup(*file)
166-
@dropped_files ||= []
167-
@dropped_files += file
168-
end
169-
170-
171-
# This method removes any files that were dropped on the remote system
172-
# and marked with the register_file_for_cleanup method
173-
def cleanup_after
174-
print_status("#{peer} - Removing files dropped by your module/exploit")
175-
if !@dropped_files
176-
return
177-
end
178-
begin
179-
@dropped_files.delete_if do |file|
180-
file_rm(file)
181-
print_good("#{peer} - Deleted #{file}")
182-
end
183-
rescue Rex::Proto::SMB::Exceptions::ErrorCode => cleanup_error
184-
print_error("#{peer} - Unable to delte #{file}. #{cleanup_error}")
185-
end
186-
end
187-
188149
end
189150

190151
end

lib/msf/core/exploit/smb.rb

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module Msf
1818

1919
module Exploit::Remote::SMB
2020

21+
require 'msf/core/exploit/psexec'
22+
2123
include Exploit::Remote::Tcp
2224
include Exploit::Remote::NTLM::Client
2325

@@ -90,6 +92,13 @@ def initialize(info = {})
9092
register_autofilter_services(%W{ netbios-ssn microsoft-ds })
9193
end
9294

95+
# Override {Exploit::Remote::Tcp#connect} to setup an SMB connection
96+
# and configure evasion options
97+
#
98+
# Also populates {#simple}.
99+
#
100+
# @param (see Exploit::Remote::Tcp#connect)
101+
# @return (see Exploit::Remote::Tcp#connect)
93102
def connect(global=true)
94103

95104
disconnect() if global
@@ -132,7 +141,12 @@ def unicode(str)
132141
Rex::Text.to_unicode(str)
133142
end
134143

135-
# This method establishes a SMB session over the default socket
144+
# Establishes an SMB session over the default socket and connects to
145+
# the IPC$ share.
146+
#
147+
# You should call {#connect} before calling this
148+
#
149+
# @return [void]
136150
def smb_login
137151
simple.login(
138152
datastore['SMBName'],
@@ -217,13 +231,55 @@ def splitname(uname)
217231
end
218232
end
219233

234+
# Whether a remote file exists
235+
#
236+
# @param file [String] Path to a file to remove, relative to the
237+
# most-recently connected share
238+
# @raise [Rex::Proto::SMB::Exceptions::ErrorCode]
239+
def smb_file_exist?(file)
240+
begin
241+
fd = simple.open(file, 'ro')
242+
rescue XCEPT::ErrorCode => e
243+
# If attempting to open the file results in a "*_NOT_FOUND" error,
244+
# then we can be sure the file is not there.
245+
#
246+
# Copy-pasted from smb/exceptions.rb to avoid the gymnastics
247+
# required to pull them out of a giant inverted hash
248+
#
249+
# 0xC0000034 => "STATUS_OBJECT_NAME_NOT_FOUND",
250+
# 0xC000003A => "STATUS_OBJECT_PATH_NOT_FOUND",
251+
# 0xC0000225 => "STATUS_NOT_FOUND",
252+
error_is_not_found = [ 0xC0000034, 0xC000003A, 0xC0000225 ].include?(e.error_code)
253+
# If the server returns some other error, then there was a
254+
# permissions problem or some other difficulty that we can't
255+
# really account for and hope the caller can deal with it.
256+
raise e unless error_is_not_found
257+
found = !error_is_not_found
258+
else
259+
# There was no exception, so we know the file is openable
260+
fd.close
261+
found = true
262+
end
263+
264+
found
265+
end
266+
267+
# Remove remote file
268+
#
269+
# @param file (see #smb_file_exist?)
270+
# @return [void]
271+
def smb_file_rm(file)
272+
fd = smb_open(file, 'ro')
273+
fd.delete
274+
end
275+
220276

221277
#
222278
# Fingerprinting methods
223279
#
224280

225281

226-
# This method the EnumPrinters() function of the spooler service
282+
# Calls the EnumPrinters() function of the spooler service
227283
def smb_enumprinters(flags, name, level, blen)
228284
stub =
229285
NDR.long(flags) +
@@ -632,10 +688,7 @@ def smb_fingerprint
632688
fprint
633689
end
634690

635-
#
636-
# Accessors
637-
#
638-
691+
# @return [Rex::Proto::SMB::SimpleClient]
639692
attr_accessor :simple
640693

641694
end
@@ -785,7 +838,6 @@ def smb_error(cmd, c, errorclass, esn = false)
785838
c.put(pkt.to_s)
786839
end
787840

788-
789841
end
790842

791843

0 commit comments

Comments
 (0)