Commit cd60c369283d34885ccb7f9b4366238d7bb53585
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 | 855 | try: |
| 856 | 856 | offset = struct.unpack_from('<H', data, offset=0x1E)[0] + 46 |
| 857 | 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 | 859 | log.exception('Unable to parse MSO/ActiveMime file header') |
| 863 | 860 | raise RuntimeError('Unable to parse MSO/ActiveMime file header') |
| 864 | 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 | 867 | log.debug('Attempting zlib decompression from MSO file offset 0x%X' % start) |
| 871 | 868 | extracted_data = zlib.decompress(data[start:]) |
| 872 | 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 | 871 | log.exception('zlib decompression failed') |
| 878 | 872 | # None of the guessed offsets worked, let's try brute-forcing by looking |
| 879 | 873 | # for potential zlib-compressed blocks starting with 0x78: |
| ... | ... | @@ -884,10 +878,7 @@ def mso_file_extract(data): |
| 884 | 878 | log.debug('Attempting zlib decompression from MSO file offset 0x%X' % start) |
| 885 | 879 | extracted_data = zlib.decompress(data[start:]) |
| 886 | 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 | 882 | log.exception('zlib decompression failed') |
| 892 | 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 | 1606 | decoded = base64.b64decode(value) |
| 1616 | 1607 | results.append((value, decoded)) |
| 1617 | 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 | 1610 | # if an exception occurs, it is likely not a base64-encoded string |
| 1623 | 1611 | pass |
| 1624 | 1612 | return results |
| ... | ... | @@ -1645,10 +1633,7 @@ def detect_dridex_strings(vba_code): |
| 1645 | 1633 | decoded = DridexUrlDecode(value) |
| 1646 | 1634 | results.append((value, decoded)) |
| 1647 | 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 | 1637 | # if an exception occurs, it is likely not a dridex-encoded string |
| 1653 | 1638 | pass |
| 1654 | 1639 | return results |
| ... | ... | @@ -2015,10 +2000,7 @@ class VBA_Parser(object): |
| 2015 | 2000 | # TODO: raise TypeError if this is a Powerpoint 97 file, since VBA macros cannot be detected yet |
| 2016 | 2001 | # set type only if parsing succeeds |
| 2017 | 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 | 2004 | # TODO: handle OLE parsing exceptions |
| 2023 | 2005 | log.exception('Failed OLE parsing for file %r' % self.filename) |
| 2024 | 2006 | pass |
| ... | ... | @@ -2047,19 +2029,13 @@ class VBA_Parser(object): |
| 2047 | 2029 | ole_data = z.open(subfile).read() |
| 2048 | 2030 | try: |
| 2049 | 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 | 2033 | log.debug('%s is not a valid OLE file' % subfile) |
| 2055 | 2034 | continue |
| 2056 | 2035 | z.close() |
| 2057 | 2036 | # set type only if parsing succeeds |
| 2058 | 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 | 2039 | # TODO: handle parsing exceptions |
| 2064 | 2040 | log.exception('Failed Zip/OpenXML parsing for file %r' % self.filename) |
| 2065 | 2041 | pass |
| ... | ... | @@ -2089,19 +2065,13 @@ class VBA_Parser(object): |
| 2089 | 2065 | ole_data = mso_file_extract(mso_data) |
| 2090 | 2066 | try: |
| 2091 | 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 | 2069 | log.error('%s does not contain a valid OLE file' % fname) |
| 2097 | 2070 | else: |
| 2098 | 2071 | log.error('%s is not a valid MSO file' % fname) |
| 2099 | 2072 | # set type only if parsing succeeds |
| 2100 | 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 | 2075 | # TODO: differentiate exceptions for each parsing stage |
| 2106 | 2076 | log.exception('Failed XML parsing for file %r' % self.filename) |
| 2107 | 2077 | pass |
| ... | ... | @@ -2151,15 +2121,9 @@ class VBA_Parser(object): |
| 2151 | 2121 | # TODO: check if it is actually an OLE file |
| 2152 | 2122 | # TODO: get the MSO filename from content_location? |
| 2153 | 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 | 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 | 2127 | log.exception('Failed decompressing an MSO container in %r - %s' |
| 2164 | 2128 | % (fname, MSG_OLEVBA_ISSUES)) |
| 2165 | 2129 | # TODO: bug here - need to split in smaller functions/classes? |
| ... | ... | @@ -2167,17 +2131,11 @@ class VBA_Parser(object): |
| 2167 | 2131 | try: |
| 2168 | 2132 | log.debug('type(part_data) = %s' % type(part_data)) |
| 2169 | 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 | 2135 | pass |
| 2175 | 2136 | # set type only if parsing succeeds |
| 2176 | 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 | 2139 | log.exception('Failed MIME parsing for file %r - %s' |
| 2182 | 2140 | % (self.filename, MSG_OLEVBA_ISSUES)) |
| 2183 | 2141 | pass |
| ... | ... | @@ -2196,10 +2154,7 @@ class VBA_Parser(object): |
| 2196 | 2154 | self.contains_macros = True |
| 2197 | 2155 | # set type only if parsing succeeds |
| 2198 | 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 | 2158 | log.exception('Failed text parsing for file %r - %s' |
| 2204 | 2159 | % (self.filename, MSG_OLEVBA_ISSUES)) |
| 2205 | 2160 | pass |
| ... | ... | @@ -2664,10 +2619,7 @@ class VBA_Parser_CLI(VBA_Parser): |
| 2664 | 2619 | print self.reveal() |
| 2665 | 2620 | else: |
| 2666 | 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 | 2623 | #raise |
| 2672 | 2624 | #TODO: print more info if debug mode |
| 2673 | 2625 | #print sys.exc_value |
| ... | ... | @@ -2738,10 +2690,7 @@ class VBA_Parser_CLI(VBA_Parser): |
| 2738 | 2690 | result['code_deobfuscated'] = self.reveal() |
| 2739 | 2691 | result['macros'] = macros |
| 2740 | 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 | 2694 | #raise |
| 2746 | 2695 | #TODO: print more info if debug mode |
| 2747 | 2696 | #print sys.exc_value |
| ... | ... | @@ -2793,10 +2742,7 @@ class VBA_Parser_CLI(VBA_Parser): |
| 2793 | 2742 | # file type not OLE nor OpenXML |
| 2794 | 2743 | flags = '?' |
| 2795 | 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 | 2746 | # another error occurred |
| 2801 | 2747 | #raise |
| 2802 | 2748 | #TODO: print more info if debug mode | ... | ... |