def open_openxml(self, _file):
"""
Open an OpenXML file
:param _file: filename or file contents in a file object
:return: nothing
"""
# This looks like a zip file, need to look for vbaProject.bin inside
# It can be any OLE file inside the archive
#...because vbaProject.bin can be renamed:
# see http://www.decalage.info/files/JCV07_Lagadec_OpenDocument_OpenXML_v4_decalage.pdf#page=18
log.info('Opening ZIP/OpenXML file %s' % self.filename)
try:
z = zipfile.ZipFile(_file)
#TODO: check if this is actually an OpenXML file
#TODO: if the zip file is encrypted, suggest to use the -z option, or try '-z infected' automatically
# check each file within the zip if it is an OLE file, by reading its magic:
for subfile in z.namelist():
magic = z.open(subfile).read(len(olefile.MAGIC))
if magic == olefile.MAGIC:
log.debug('Opening OLE file %s within zip' % subfile)
ole_data = z.open(subfile).read()
try:
self.ole_subfiles.append(
VBA_Parser(filename=subfile, data=ole_data,
relaxed=self.relaxed))
except OlevbaBaseException as exc:
if self.relaxed:
log.info('%s is not a valid OLE file (%s)' % (subfile, exc))
log.debug('Trace:', exc_info=True)
continue
else:
raise SubstreamOpenError(self.filename, subfile,
exc)
z.close()
# set type only if parsing succeeds
self.type = TYPE_OpenXML
except OlevbaBaseException as exc:
if self.relaxed:
log.info('Error {0} caught in Zip/OpenXML parsing for file {1}'
.format(exc, self.filename))
log.debug('Trace:', exc_info=True)
else:
raise
except (RuntimeError, zipfile.BadZipfile, zipfile.LargeZipFile, IOError) as exc:
# TODO: handle parsing exceptions
log.info('Failed Zip/OpenXML parsing for file %r (%s)'
% (self.filename, exc))
log.debug('Trace:', exc_info=True)
评论列表
文章目录