Commit cd60c369283d34885ccb7f9b4366238d7bb53585

Authored by Christian Herdtweck
1 parent 0184530e

replaced "except KeyboardInterrupt ... except: ..." with "except Exception:..."

Showing 1 changed file with 18 additions and 72 deletions
oletools/olevba.py
@@ -855,10 +855,7 @@ def mso_file_extract(data): @@ -855,10 +855,7 @@ def mso_file_extract(data):
855 try: 855 try:
856 offset = struct.unpack_from('<H', data, offset=0x1E)[0] + 46 856 offset = struct.unpack_from('<H', data, offset=0x1E)[0] + 46
857 log.debug('Parsing MSO file: data offset = 0x%X' % offset) 857 log.debug('Parsing MSO file: data offset = 0x%X' % offset)
858 - except KeyboardInterrupt:  
859 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
860 - raise  
861 - except: 858 + except Exception:
862 log.exception('Unable to parse MSO/ActiveMime file header') 859 log.exception('Unable to parse MSO/ActiveMime file header')
863 raise RuntimeError('Unable to parse MSO/ActiveMime file header') 860 raise RuntimeError('Unable to parse MSO/ActiveMime file header')
864 # In all the samples seen so far, Word always uses an offset of 0x32, 861 # In all the samples seen so far, Word always uses an offset of 0x32,
@@ -870,10 +867,7 @@ def mso_file_extract(data): @@ -870,10 +867,7 @@ def mso_file_extract(data):
870 log.debug('Attempting zlib decompression from MSO file offset 0x%X' % start) 867 log.debug('Attempting zlib decompression from MSO file offset 0x%X' % start)
871 extracted_data = zlib.decompress(data[start:]) 868 extracted_data = zlib.decompress(data[start:])
872 return extracted_data 869 return extracted_data
873 - except KeyboardInterrupt:  
874 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
875 - raise  
876 - except: 870 + except Exception:
877 log.exception('zlib decompression failed') 871 log.exception('zlib decompression failed')
878 # None of the guessed offsets worked, let's try brute-forcing by looking 872 # None of the guessed offsets worked, let's try brute-forcing by looking
879 # for potential zlib-compressed blocks starting with 0x78: 873 # for potential zlib-compressed blocks starting with 0x78:
@@ -884,10 +878,7 @@ def mso_file_extract(data): @@ -884,10 +878,7 @@ def mso_file_extract(data):
884 log.debug('Attempting zlib decompression from MSO file offset 0x%X' % start) 878 log.debug('Attempting zlib decompression from MSO file offset 0x%X' % start)
885 extracted_data = zlib.decompress(data[start:]) 879 extracted_data = zlib.decompress(data[start:])
886 return extracted_data 880 return extracted_data
887 - except KeyboardInterrupt:  
888 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
889 - raise  
890 - except: 881 + except Exception:
891 log.exception('zlib decompression failed') 882 log.exception('zlib decompression failed')
892 raise RuntimeError('Unable to decompress data from a MSO/ActiveMime file') 883 raise RuntimeError('Unable to decompress data from a MSO/ActiveMime file')
893 884
@@ -1615,10 +1606,7 @@ def detect_base64_strings(vba_code): @@ -1615,10 +1606,7 @@ def detect_base64_strings(vba_code):
1615 decoded = base64.b64decode(value) 1606 decoded = base64.b64decode(value)
1616 results.append((value, decoded)) 1607 results.append((value, decoded))
1617 found.add(value) 1608 found.add(value)
1618 - except KeyboardInterrupt:  
1619 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
1620 - raise  
1621 - except: 1609 + except Exception:
1622 # if an exception occurs, it is likely not a base64-encoded string 1610 # if an exception occurs, it is likely not a base64-encoded string
1623 pass 1611 pass
1624 return results 1612 return results
@@ -1645,10 +1633,7 @@ def detect_dridex_strings(vba_code): @@ -1645,10 +1633,7 @@ def detect_dridex_strings(vba_code):
1645 decoded = DridexUrlDecode(value) 1633 decoded = DridexUrlDecode(value)
1646 results.append((value, decoded)) 1634 results.append((value, decoded))
1647 found.add(value) 1635 found.add(value)
1648 - except KeyboardInterrupt:  
1649 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
1650 - raise  
1651 - except: 1636 + except Exception:
1652 # if an exception occurs, it is likely not a dridex-encoded string 1637 # if an exception occurs, it is likely not a dridex-encoded string
1653 pass 1638 pass
1654 return results 1639 return results
@@ -2015,10 +2000,7 @@ class VBA_Parser(object): @@ -2015,10 +2000,7 @@ class VBA_Parser(object):
2015 # TODO: raise TypeError if this is a Powerpoint 97 file, since VBA macros cannot be detected yet 2000 # TODO: raise TypeError if this is a Powerpoint 97 file, since VBA macros cannot be detected yet
2016 # set type only if parsing succeeds 2001 # set type only if parsing succeeds
2017 self.type = TYPE_OLE 2002 self.type = TYPE_OLE
2018 - except KeyboardInterrupt:  
2019 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2020 - raise  
2021 - except: 2003 + except Exception:
2022 # TODO: handle OLE parsing exceptions 2004 # TODO: handle OLE parsing exceptions
2023 log.exception('Failed OLE parsing for file %r' % self.filename) 2005 log.exception('Failed OLE parsing for file %r' % self.filename)
2024 pass 2006 pass
@@ -2047,19 +2029,13 @@ class VBA_Parser(object): @@ -2047,19 +2029,13 @@ class VBA_Parser(object):
2047 ole_data = z.open(subfile).read() 2029 ole_data = z.open(subfile).read()
2048 try: 2030 try:
2049 self.ole_subfiles.append(VBA_Parser(filename=subfile, data=ole_data)) 2031 self.ole_subfiles.append(VBA_Parser(filename=subfile, data=ole_data))
2050 - except KeyboardInterrupt:  
2051 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2052 - raise  
2053 - except: 2032 + except Exception:
2054 log.debug('%s is not a valid OLE file' % subfile) 2033 log.debug('%s is not a valid OLE file' % subfile)
2055 continue 2034 continue
2056 z.close() 2035 z.close()
2057 # set type only if parsing succeeds 2036 # set type only if parsing succeeds
2058 self.type = TYPE_OpenXML 2037 self.type = TYPE_OpenXML
2059 - except KeyboardInterrupt:  
2060 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2061 - raise  
2062 - except: 2038 + except Exception:
2063 # TODO: handle parsing exceptions 2039 # TODO: handle parsing exceptions
2064 log.exception('Failed Zip/OpenXML parsing for file %r' % self.filename) 2040 log.exception('Failed Zip/OpenXML parsing for file %r' % self.filename)
2065 pass 2041 pass
@@ -2089,19 +2065,13 @@ class VBA_Parser(object): @@ -2089,19 +2065,13 @@ class VBA_Parser(object):
2089 ole_data = mso_file_extract(mso_data) 2065 ole_data = mso_file_extract(mso_data)
2090 try: 2066 try:
2091 self.ole_subfiles.append(VBA_Parser(filename=fname, data=ole_data)) 2067 self.ole_subfiles.append(VBA_Parser(filename=fname, data=ole_data))
2092 - except KeyboardInterrupt:  
2093 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2094 - raise  
2095 - except: 2068 + except Exception:
2096 log.error('%s does not contain a valid OLE file' % fname) 2069 log.error('%s does not contain a valid OLE file' % fname)
2097 else: 2070 else:
2098 log.error('%s is not a valid MSO file' % fname) 2071 log.error('%s is not a valid MSO file' % fname)
2099 # set type only if parsing succeeds 2072 # set type only if parsing succeeds
2100 self.type = TYPE_Word2003_XML 2073 self.type = TYPE_Word2003_XML
2101 - except KeyboardInterrupt:  
2102 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2103 - raise  
2104 - except: 2074 + except Exception:
2105 # TODO: differentiate exceptions for each parsing stage 2075 # TODO: differentiate exceptions for each parsing stage
2106 log.exception('Failed XML parsing for file %r' % self.filename) 2076 log.exception('Failed XML parsing for file %r' % self.filename)
2107 pass 2077 pass
@@ -2151,15 +2121,9 @@ class VBA_Parser(object): @@ -2151,15 +2121,9 @@ class VBA_Parser(object):
2151 # TODO: check if it is actually an OLE file 2121 # TODO: check if it is actually an OLE file
2152 # TODO: get the MSO filename from content_location? 2122 # TODO: get the MSO filename from content_location?
2153 self.ole_subfiles.append(VBA_Parser(filename=fname, data=ole_data)) 2123 self.ole_subfiles.append(VBA_Parser(filename=fname, data=ole_data))
2154 - except KeyboardInterrupt:  
2155 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2156 - raise  
2157 - except: 2124 + except Exception:
2158 log.debug('%s does not contain a valid OLE file' % fname) 2125 log.debug('%s does not contain a valid OLE file' % fname)
2159 - except KeyboardInterrupt:  
2160 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2161 - raise  
2162 - except: 2126 + except Exception:
2163 log.exception('Failed decompressing an MSO container in %r - %s' 2127 log.exception('Failed decompressing an MSO container in %r - %s'
2164 % (fname, MSG_OLEVBA_ISSUES)) 2128 % (fname, MSG_OLEVBA_ISSUES))
2165 # TODO: bug here - need to split in smaller functions/classes? 2129 # TODO: bug here - need to split in smaller functions/classes?
@@ -2167,17 +2131,11 @@ class VBA_Parser(object): @@ -2167,17 +2131,11 @@ class VBA_Parser(object):
2167 try: 2131 try:
2168 log.debug('type(part_data) = %s' % type(part_data)) 2132 log.debug('type(part_data) = %s' % type(part_data))
2169 log.debug('part_data[0:20] = %r' % part_data[0:20]) 2133 log.debug('part_data[0:20] = %r' % part_data[0:20])
2170 - except KeyboardInterrupt:  
2171 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2172 - raise  
2173 - except: 2134 + except Exception:
2174 pass 2135 pass
2175 # set type only if parsing succeeds 2136 # set type only if parsing succeeds
2176 self.type = TYPE_MHTML 2137 self.type = TYPE_MHTML
2177 - except KeyboardInterrupt:  
2178 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2179 - raise  
2180 - except: 2138 + except Exception:
2181 log.exception('Failed MIME parsing for file %r - %s' 2139 log.exception('Failed MIME parsing for file %r - %s'
2182 % (self.filename, MSG_OLEVBA_ISSUES)) 2140 % (self.filename, MSG_OLEVBA_ISSUES))
2183 pass 2141 pass
@@ -2196,10 +2154,7 @@ class VBA_Parser(object): @@ -2196,10 +2154,7 @@ class VBA_Parser(object):
2196 self.contains_macros = True 2154 self.contains_macros = True
2197 # set type only if parsing succeeds 2155 # set type only if parsing succeeds
2198 self.type = TYPE_TEXT 2156 self.type = TYPE_TEXT
2199 - except KeyboardInterrupt:  
2200 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2201 - raise  
2202 - except: 2157 + except Exception:
2203 log.exception('Failed text parsing for file %r - %s' 2158 log.exception('Failed text parsing for file %r - %s'
2204 % (self.filename, MSG_OLEVBA_ISSUES)) 2159 % (self.filename, MSG_OLEVBA_ISSUES))
2205 pass 2160 pass
@@ -2664,10 +2619,7 @@ class VBA_Parser_CLI(VBA_Parser): @@ -2664,10 +2619,7 @@ class VBA_Parser_CLI(VBA_Parser):
2664 print self.reveal() 2619 print self.reveal()
2665 else: 2620 else:
2666 print 'No VBA macros found.' 2621 print 'No VBA macros found.'
2667 - except KeyboardInterrupt:  
2668 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2669 - raise  
2670 - except: #TypeError: 2622 + except Exception: #TypeError:
2671 #raise 2623 #raise
2672 #TODO: print more info if debug mode 2624 #TODO: print more info if debug mode
2673 #print sys.exc_value 2625 #print sys.exc_value
@@ -2738,10 +2690,7 @@ class VBA_Parser_CLI(VBA_Parser): @@ -2738,10 +2690,7 @@ class VBA_Parser_CLI(VBA_Parser):
2738 result['code_deobfuscated'] = self.reveal() 2690 result['code_deobfuscated'] = self.reveal()
2739 result['macros'] = macros 2691 result['macros'] = macros
2740 result['json_conversion_successful'] = True 2692 result['json_conversion_successful'] = True
2741 - except KeyboardInterrupt:  
2742 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2743 - raise  
2744 - except: #TypeError: 2693 + except Exception: #TypeError:
2745 #raise 2694 #raise
2746 #TODO: print more info if debug mode 2695 #TODO: print more info if debug mode
2747 #print sys.exc_value 2696 #print sys.exc_value
@@ -2793,10 +2742,7 @@ class VBA_Parser_CLI(VBA_Parser): @@ -2793,10 +2742,7 @@ class VBA_Parser_CLI(VBA_Parser):
2793 # file type not OLE nor OpenXML 2742 # file type not OLE nor OpenXML
2794 flags = '?' 2743 flags = '?'
2795 message = 'File format not supported' 2744 message = 'File format not supported'
2796 - except KeyboardInterrupt:  
2797 - # do not ignore exceptions when the user presses Ctrl+C/Pause:  
2798 - raise  
2799 - except: 2745 + except Exception:
2800 # another error occurred 2746 # another error occurred
2801 #raise 2747 #raise
2802 #TODO: print more info if debug mode 2748 #TODO: print more info if debug mode