DLL-Extractor/main.py

71 lines
2.4 KiB
Python
Raw Normal View History

2024-12-29 05:56:35 +00:00
import os
2024-12-29 05:42:51 +00:00
import pefile
import sys
2024-12-29 05:56:35 +00:00
import shutil
# Common DLLs that come with the OS
OS_DLLS = {
"KERNEL32.dll", "USER32.dll", "GDI32.dll", "ADVAPI32.dll",
"SHELL32.dll", "OLE32.dll", "OLEAUT32.dll", "COMDLG32.dll",
"SHLWAPI.dll", "MSVCRT.dll", "NTDLL.dll", "RPCRT4.dll",
"WININET.dll", "WS2_32.dll", "CRYPT32.dll", "OPENGL32.dll"
}
2024-12-29 05:42:51 +00:00
def get_dll_dependencies(exe_path):
"""Extract DLL dependencies from an executable file."""
try:
pe = pefile.PE(exe_path)
# Check for the IMPORT_DIRECTORY entry
if hasattr(pe, 'DIRECTORY_ENTRY_IMPORT'):
dlls = [entry.dll.decode('utf-8') for entry in pe.DIRECTORY_ENTRY_IMPORT]
return dlls
else:
print("No DLL dependencies found in the executable.")
return []
except FileNotFoundError:
print(f"File not found: {exe_path}")
return []
except pefile.PEFormatError:
print(f"The file is not a valid PE executable: {exe_path}")
return []
except Exception as e:
print(f"An error occurred: {e}")
return []
2024-12-29 05:56:35 +00:00
def find_and_copy_dlls(dlls, output_folder):
"""Find DLL files and copy them to the output folder."""
os.makedirs(output_folder, exist_ok=True)
found = []
for dll in dlls:
for path in os.environ['PATH'].split(os.pathsep):
dll_path = os.path.join(path, dll)
if os.path.exists(dll_path):
shutil.copy(dll_path, os.path.join(output_folder, dll))
found.append(dll)
break
return found
2024-12-29 05:42:51 +00:00
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python get_dll_dependencies.py <path_to_exe>")
else:
exe_path = sys.argv[1]
dependencies = get_dll_dependencies(exe_path)
if dependencies:
2024-12-29 05:56:35 +00:00
non_os_dependencies = [dll for dll in dependencies if dll.upper() not in OS_DLLS]
print("Non-OS DLL Dependencies:")
for dll in non_os_dependencies:
2024-12-29 05:42:51 +00:00
print(f" {dll}")
2024-12-29 05:56:35 +00:00
if non_os_dependencies:
found_dlls = find_and_copy_dlls(non_os_dependencies, "found_dlls")
print("\nCopied DLLs:")
for dll in found_dlls:
print(f" {dll}")
else:
print("No non-OS DLL dependencies found.")
2024-12-29 05:42:51 +00:00
else:
print("No DLL dependencies found or failed to retrieve dependencies.")