Recreate original package structure inside PYZ

This commit is contained in:
extremecoders-re
2020-06-13 15:38:01 +05:30
parent be970c6e0d
commit a025c925ef

View File

@@ -226,8 +226,18 @@ class PyInstArchive:
parsedLen += entrySize parsedLen += entrySize
print('[+] Found {0} files in CArchive'.format(len(self.tocList))) print('[+] Found {0} files in CArchive'.format(len(self.tocList)))
def _writeRawData(self, filepath, data):
nm = filepath.replace('\\', os.path.sep).replace('/', os.path.sep).replace('..', '__')
nmDir = os.path.dirname(nm)
if nmDir != '' and not os.path.exists(nmDir): # Check if path exists, create if not
os.makedirs(nmDir)
with open(nm, 'wb') as f:
f.write(data)
def extractFiles(self): def extractFiles(self):
print('[+] Beginning extraction...please standby') print('[+] Beginning extraction...please standby')
extractionDir = os.path.join(os.getcwd(), os.path.basename(self.filePath) + '_extracted') extractionDir = os.path.join(os.getcwd(), os.path.basename(self.filePath) + '_extracted')
@@ -263,12 +273,10 @@ class PyInstArchive:
# M -> ARCHIVE_ITEM_PYPACKAGE # M -> ARCHIVE_ITEM_PYPACKAGE
# m -> ARCHIVE_ITEM_PYMODULE # m -> ARCHIVE_ITEM_PYMODULE
# packages and modules are pyc files with their header's intact # packages and modules are pyc files with their header's intact
with open(entry.name + '.pyc', 'wb') as f: self._writeRawData(entry.name + '.pyc', data)
f.write(data)
else: else:
with open(entry.name, 'wb') as f: self._writeRawData(entry.name, data)
f.write(data)
if entry.typeCmprsData == b'z' or entry.typeCmprsData == b'Z': if entry.typeCmprsData == b'z' or entry.typeCmprsData == b'Z':
self._extractPyz(entry.name) self._extractPyz(entry.name)
@@ -327,29 +335,35 @@ class PyInstArchive:
for key in toc.keys(): for key in toc.keys():
(ispkg, pos, length) = toc[key] (ispkg, pos, length) = toc[key]
f.seek(pos, os.SEEK_SET) f.seek(pos, os.SEEK_SET)
fileName = key fileName = key
try: try:
# for Python > 3.3 some keys are bytes object some are str object # for Python > 3.3 some keys are bytes object some are str object
fileName = key.decode('utf-8') fileName = fileName.decode('utf-8')
except: except:
pass pass
# Make sure destination directory exists, ensuring we keep inside dirName # Prevent writing outside dirName
destName = os.path.join(dirName, fileName.replace("..", "__")) fileName = fileName.replace('..', '__').replace('.', os.path.sep)
destDirName = os.path.dirname(destName)
if not os.path.exists(destDirName): if ispkg == 1:
os.makedirs(destDirName) filePath = os.path.join(dirName, fileName, '__init__.pyc')
else:
filePath = os.path.join(dirName, fileName + '.pyc')
fileDir = os.path.dirname(filePath)
if not os.path.exists(fileDir):
os.makedirs(fileDir)
try: try:
data = f.read(length) data = f.read(length)
data = zlib.decompress(data) data = zlib.decompress(data)
except: except:
print('[!] Error: Failed to decompress {0}, probably encrypted. Extracting as is.'.format(fileName)) print('[!] Error: Failed to decompress {0}, probably encrypted. Extracting as is.'.format(filePath))
open(destName + '.pyc.encrypted', 'wb').write(data) open(filePath + '.encrypted', 'wb').write(data)
continue else:
self._writePyc(filePath, data)
self._writePyc(destName + '.pyc', data)
def main(): def main():