Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

olevba: prevent side effects on python lib "email" #604

Merged
merged 2 commits into from
Sep 3, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions oletools/olevba.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@
import os
import logging
import struct
from copy import copy
from io import BytesIO, StringIO
import math
import zipfile
Expand All @@ -276,6 +277,7 @@
import base64
import zlib
import email # for MHTML parsing
import email.feedparser
import string # for printable
import json # for json output mode (argument --json)

Expand Down Expand Up @@ -330,11 +332,6 @@
from oletools.common.io_encoding import ensure_stdout_handles_unicode
from oletools.common import codepages

# monkeypatch email to fix issue #32:
# allow header lines without ":"
import email.feedparser
email.feedparser.headerRE = re.compile(r'^(From |[\041-\071\073-\176]{1,}:?|[\t ])')

# === PYTHON 2+3 SUPPORT ======================================================

if sys.version_info[0] <= 2:
Expand Down Expand Up @@ -2946,11 +2943,17 @@ def open_mht(self, data):
elif content_offset > -1:
stripped_data = stripped_data[content_offset:]
# TODO: quick and dirty fix: insert a standard line with MIME-Version header?
# monkeypatch email to fix issue #32:
# allow header lines without ":"
oldHeaderRE = copy(email.feedparser.headerRE)
loosyHeaderRE = re.compile(r'^(From |[\041-\071\073-\176]{1,}:?|[\t ])')
email.feedparser.headerRE = loosyHeaderRE
if PYTHON2:
mhtml = email.message_from_string(stripped_data)
else:
# on Python 3, need to use message_from_bytes instead:
mhtml = email.message_from_bytes(stripped_data)
email.feedparser.headerRE = oldHeaderRE
# find all the attached files:
for part in mhtml.walk():
content_type = part.get_content_type() # always returns a value
Expand Down