From b829de1268073f10a384087bf414182885c239c6 Mon Sep 17 00:00:00 2001 From: OusmBlueNinja <89956790+OusmBlueNinja@users.noreply.github.com> Date: Sat, 28 Dec 2024 23:56:35 -0600 Subject: [PATCH] Updated --- .gitignore | 1 + README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ main.py | 37 ++++++++++++++++++++++++++++++++++--- requirements.txt | 1 + 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..adb36c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.exe \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..77ae231 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# DLL Extractor + +This Python script extracts the DLL dependencies of a Windows executable file and filters out DLLs that are commonly included with the operating system. + +## Features +- Identifies all DLLs required by a specified `.exe` file. +- Filters out DLLs that are part of the standard Windows OS (e.g., `KERNEL32.dll`, `USER32.dll`). + +## Requirements +- Python 3.6 or later +- `pefile` library + +## Installation +Install the required Python library: +```bash +pip install pefile +``` + +## Usage +Run the script with the path to an executable file as an argument: +```bash +python get_dll_dependencies.py +``` + +### Example +```bash +python get_dll_dependencies.py my_program.exe +``` +Output: +``` +Non-OS DLL Dependencies: + glew32.dll + glfw3.dll + libyaml-cpp.dll + lua54.dll +``` + +## Notes +- The script assumes the executable is a valid PE (Portable Executable) file. +- It gracefully handles errors such as missing files or invalid PE formats. + +## License +This project is open-source and available under the MIT License. + diff --git a/main.py b/main.py index 24f522c..223e487 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,19 @@ +import os import pefile import sys +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" +} def get_dll_dependencies(exe_path): """Extract DLL dependencies from an executable file.""" try: - # Load the PE file pe = pefile.PE(exe_path) # Check for the IMPORT_DIRECTORY entry @@ -24,6 +33,19 @@ def get_dll_dependencies(exe_path): print(f"An error occurred: {e}") return [] +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 + if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: python get_dll_dependencies.py ") @@ -32,8 +54,17 @@ if __name__ == "__main__": dependencies = get_dll_dependencies(exe_path) if dependencies: - print("DLL Dependencies:") - for dll in dependencies: + 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: print(f" {dll}") + + 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.") else: print("No DLL dependencies found or failed to retrieve dependencies.") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5088fdb --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pefile \ No newline at end of file