From 8f68844bc2634b0adcbd169a9406c55dc048f669 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Thu, 7 Dec 2017 17:44:04 +0100 Subject: [PATCH] olevba[3]: ensure file handles are closed --- oletools/olevba.py | 9 ++++++--- oletools/olevba3.py | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/oletools/olevba.py b/oletools/olevba.py index e2f5941..f1bad49 100644 --- a/oletools/olevba.py +++ b/oletools/olevba.py @@ -2336,7 +2336,8 @@ class VBA_Parser(object): # read file from disk, check if it is a Word 2003 XML file (WordProcessingML), Excel 2003 XML, # or a plain text file containing VBA code if data is None: - data = open(filename, 'rb').read() + with open(filename, 'rb') as file_handle: + data = file_handle.read() # check if it is a Word 2003 XML file (WordProcessingML): must contain the namespace if b'http://schemas.microsoft.com/office/word/2003/wordml' in data: self.open_word2003xml(data) @@ -2398,10 +2399,12 @@ class VBA_Parser(object): #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)) + with z.open(subfile) as file_handle: + magic = file_handle.read(len(olefile.MAGIC)) if magic == olefile.MAGIC: log.debug('Opening OLE file %s within zip' % subfile) - ole_data = z.open(subfile).read() + with z.open(subfile) as file_handle: + ole_data = file_handle.read() try: self.ole_subfiles.append( VBA_Parser(filename=subfile, data=ole_data, diff --git a/oletools/olevba3.py b/oletools/olevba3.py index 708f207..3bf4608 100644 --- a/oletools/olevba3.py +++ b/oletools/olevba3.py @@ -2289,7 +2289,8 @@ class VBA_Parser(object): # read file from disk, check if it is a Word 2003 XML file (WordProcessingML), Excel 2003 XML, # or a plain text file containing VBA code if data is None: - data = open(filename, 'rb').read() + with open(filename, 'rb') as file_handle: + data = file_handle.read() # check if it is a Word 2003 XML file (WordProcessingML): must contain the namespace if b'http://schemas.microsoft.com/office/word/2003/wordml' in data: self.open_word2003xml(data) @@ -2351,10 +2352,12 @@ class VBA_Parser(object): #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)) + with z.open(subfile) as file_handle: + magic = file_handle.read(len(olefile.MAGIC)) if magic == olefile.MAGIC: log.debug('Opening OLE file %s within zip' % subfile) - ole_data = z.open(subfile).read() + with z.open(subfile) as file_handle: + ole_data = file_handle.read() try: self.ole_subfiles.append( VBA_Parser(filename=subfile, data=ole_data, -- libgit2 0.21.4