Skip to content

olevba: fix TypeError in mso_file_extract brute-force fallback on Python 3 - #894

Open
ogre2007 wants to merge 1 commit into
decalage2:masterfrom
ogre2007:fix/mso-file-extract-bytes-pattern
Open

ogre2007 wants to merge 1 commit into
decalage2:masterfrom
ogre2007:fix/mso-file-extract-bytes-pattern

Conversation

@ogre2007

Copy link
Copy Markdown

Summary

The brute-force fallback of mso_file_extract — used to recover MSO/ActiveMime containers when neither the header hint (u16 at 0x1E) nor the known fixed offsets (0x32, 0x22A) point at the zlib stream — applies the str regex pattern re_zlib_header to bytes data:

re_zlib_header = re.compile(r'x')   # str pattern
...
for match in re_zlib_header.finditer(data):   # data is bytes

On Python 3 this raises TypeError: cannot use a string pattern on a bytes-like object instead of recovering the payload, so the whole extraction (and therefore VBA discovery in olevba) fails for such files.

Reproduction

Build an ActiveMime container whose zlib stream is at an offset other than the header hint / 0x32 / 0x22A — e.g. a hand-crafted MHTML sample — and run olevba on it:

TypeError: cannot use a string pattern on a bytes-like object
  File "oletools/olevba.py", line 1270, in mso_file_extract
    for match in re_zlib_header.finditer(data):

Encountered with a hand-built MHTML + ActiveMime test sample whose header did not follow the Word convention.

Fix

One line: make the pattern bytes (re.compile(b'x')), which works on both Python 2 and 3.

Tests

Added tests/olevba/test_mso_file_extract.py:

  • standard Word header layout (hint 4 → zlib at 0x32) extracts correctly;
  • regression test: bogus header hint + zlib stream at an unknown offset must be recovered by the brute-force fallback. Fails with the TypeError on unpatched master, passes with this fix.

Both tests pass locally (Python 3.13, pytest).

…hon 3

The brute-force fallback of mso_file_extract (used to recover MSO/ActiveMime
containers when neither the header hint nor the known fixed offsets 0x32 /
0x22A point at the zlib stream) applied the str regex pattern re_zlib_header
to bytes data, raising 'TypeError: cannot use a string pattern on a
bytes-like object' on Python 3 instead of recovering the payload.

Use a bytes pattern, which works on both Python 2 and 3.

Reproduction: build an ActiveMime container whose zlib stream is at an offset
other than the header hint / 0x32 / 0x22A (e.g. a hand-crafted MHTML sample),
and run olevba on it: the extraction crashes instead of finding the embedded
VBA project.

Add a regression test (tests/olevba/test_mso_file_extract.py) covering the
standard Word header layout and the brute-force fallback.
@decalage2
decalage2 self-requested a review September 26, 2026 13:26
@decalage2 decalage2 self-assigned this Sep 26, 2026
@decalage2 decalage2 added this to the oletools 0.60 milestone Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants