Skip to content

Commit

Permalink
Improve the loader in the face of the file/python-magic duplicate mod…
Browse files Browse the repository at this point in the history
…ule naming.
  • Loading branch information
ikelos committed Jan 28, 2018
1 parent 663cf3e commit bf8173f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions volatility/framework/layers/__init__.py
Expand Up @@ -6,6 +6,7 @@
import lzma
import os
import ssl
import sys
import typing
import urllib.parse
import urllib.request
Expand All @@ -14,10 +15,8 @@

try:
import magic

IMPORTED_MAGIC = True
except ImportError:
IMPORTED_MAGIC = False
pass

try:
import smb.SMBHandler
Expand All @@ -26,7 +25,6 @@

from volatility import framework
from volatility.framework import constants, validity
from volatility.framework.interfaces.layers import IMPORTED_MAGIC
from volatility.framework.layers import intel, lime, physical, segmented, vmware

vollog = logging.getLogger(__name__)
Expand Down Expand Up @@ -92,13 +90,18 @@ def open(self, url, mode = "rb"):
curfile = open(temp_filename, mode = "rb")

# Determine whether the file is a particular type of file, and if so, open it as such
if IMPORTED_MAGIC:
IMPORTED_MAGIC = False
if 'magic' in sys.modules:
while True:
detected = None
try:
# Detect the content
detected = magic.detect_from_fobj(curfile)
IMPORTED_MAGIC = True
except AttributeError:
pass
except:
break
pass

if detected:
if detected.mime_type == 'application/x-xz':
Expand All @@ -115,7 +118,7 @@ def open(self, url, mode = "rb"):
# Read and rewind to ensure we're inside any compressed file layers
curfile.read(1)
curfile.seek(0)
else:
if not IMPORTED_MAGIC:
# Somewhat of a hack, but prevents a hard dependency on the magic module
url_path = parsed_url.path
while True:
Expand Down

0 comments on commit bf8173f

Please sign in to comment.