Skip to content

Commit

Permalink
Fix: decompile utility.
Browse files Browse the repository at this point in the history
  • Loading branch information
remiliacn committed Dec 15, 2024
1 parent 37c8279 commit 7b7a0a1
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions utils/decompile_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
"""

from os import getcwd, path, remove, walk
from re import findall
from shutil import rmtree
from subprocess import DEVNULL, PIPE, run
from sys import platform
from typing import List, Optional

from loguru import logger

PYCDAS = "pycdas.exe" if platform == "win32" else "pycdas"


Expand All @@ -18,8 +21,26 @@ def extract_pyinstaller_exe(exe_path: str) -> str:
return path.join(getcwd(), path.basename(exe_path) + "_extracted")


def attempts_to_get_entry_point(exe_path: str) -> (str, Optional[str]):
extraction_path = None
try:
extraction_payload = run(["python", "pyinstxtractor.py", exe_path], stdout=PIPE, stderr=PIPE, text=True)
extraction_path = path.join(getcwd(), path.basename(exe_path) + "_extracted")
entry_points = findall(r'Possible entry point: (.*?\.pyc)', extraction_payload.stdout)

if entry_points:
return extraction_path, path.join(
extraction_path,
list(filter(lambda x: not x.startswith("pyi"), entry_points))[0])

except Exception as err:
logger.warning(f'Failed to extract pyinstaller entry point: {err}')

return extraction_path, None


def find_payload_files(
extracted_dir: str, find_file: str, *file_path, blacklist_filenames=None
extracted_dir: str, find_file: str, *file_path, blacklist_filenames=None
) -> Optional[List[str]]:
if blacklist_filenames is None:
blacklist_filenames = []
Expand All @@ -35,10 +56,10 @@ def find_payload_files(


def find_payload_file(
extracted_dir: str, find_file: str, *file_path, blacklist_filenames=None
extracted_dir: str, find_file: str, *file_path, blacklist_filenames=None
) -> Optional[str]:
if files := find_payload_files(
extracted_dir, find_file, *file_path, blacklist_filenames=blacklist_filenames
extracted_dir, find_file, *file_path, blacklist_filenames=blacklist_filenames
):
return files[0]

Expand All @@ -52,10 +73,12 @@ def decompile_pyc(pyc_path: str) -> str:

def clean_up_temp_files(directory: str):
if path.isfile(directory):
logger.info(f'Cleaning up file: {directory}')
remove(directory)
return

if not path.exists(directory):
return

logger.info(f'Cleaning up directory: {directory}')
rmtree(directory)

0 comments on commit 7b7a0a1

Please sign in to comment.