Skip to content

Commit b0fb1b2

Browse files
committed
vmonkey/pull_embedded_pe_files: use in-memory unzipping instead of temp file
1 parent dc88546 commit b0fb1b2

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

vipermonkey/vmonkey.py

+4-15
Original file line numberDiff line numberDiff line change
@@ -1106,25 +1106,14 @@ def pull_embedded_pe_files(data, out_dir):
11061106
# Is this a Office 2007 (zip) file?
11071107
if filetype.is_office2007_file(data, is_data=True):
11081108

1109-
# Write the zip data to a temp file.
1110-
# we use tempfile.NamedTemporaryFile to create a temporary file in a platform-independent
1111-
# and secure way. The file needs to be accessible with a filename until it is explicitly
1112-
# deleted (hence the option delete=False).
1113-
# TODO: [Phil] I think we could avoid this and use a bytes buffer in memory instead, zipfile supports it
1114-
# This is really required on Windows because the antivirus blocks the temp file on disk
1115-
f = tempfile.NamedTemporaryFile(delete=False)
1116-
fname = f.name
1117-
f.write(data)
1118-
f.close()
1119-
1109+
# convert data to a BytesIO buffer so that we can use zipfile in memory
1110+
# without writing a temp file on disk:
1111+
data_io = io.BytesIO(data)
11201112
# Pull embedded PE files from each file in the zip.
1121-
with zipfile.ZipFile(fname, "r") as f:
1113+
with zipfile.ZipFile(data_io, "r") as f:
11221114
for name in f.namelist():
11231115
curr_data = f.read(name)
11241116
pull_embedded_pe_files(curr_data, out_dir)
1125-
1126-
# Clean up and leave.
1127-
os.remove(fname)
11281117
return
11291118

11301119
# Is a PE file in the data at all?

0 commit comments

Comments
 (0)