From 176b1572452701b3a40cd6f752686369c8bc9f9c Mon Sep 17 00:00:00 2001 From: extremecoders-re Date: Fri, 27 Mar 2020 14:55:19 +0530 Subject: [PATCH] Use importlib instead of imp for Python3 --- pyinstxtractor.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pyinstxtractor.py b/pyinstxtractor.py index 5258727..8b38b0a 100644 --- a/pyinstxtractor.py +++ b/pyinstxtractor.py @@ -89,9 +89,16 @@ import struct import marshal import zlib import sys -import imp from uuid import uuid4 as uniquename +# imp is deprecated in Python 2 in favour of importlib +if sys.version_info.major == 3: + import importlib + pyc_magic = importlib.util.MAGIC_NUMBER +else: + import imp + pyc_magic = imp.get_magic() + class CTOCEntry: def __init__(self, position, cmprsdDataSize, uncmprsdDataSize, cmprsFlag, typeCmprsData, name): @@ -269,7 +276,7 @@ class PyInstArchive: def _writePyc(self, filename, data): with open(filename, 'wb') as pycFile: - pycFile.write(imp.get_magic()) # pyc magic + pycFile.write(pyc_magic) # pyc magic if self.pyver >= 37: # PEP 552 -- Deterministic pycs pycFile.write(b'\0' * 4) # Bitfield @@ -296,7 +303,7 @@ class PyInstArchive: pycHeader = f.read(4) # Python magic value # Skip PYZ extraction if not running under the same python version - if imp.get_magic() != pycHeader: + if pyc_magic != pycHeader: print('[!] Warning: This script is running in a different Python version than the one used to build the executable.') print('[!] Please run this script in Python{0} to prevent extraction errors during unmarshalling'.format(self.pyver)) print('[!] Skipping pyz extraction')