This Python script demonstrates how to perform remote DLL injection into a target process using the Windows API. This technique can be used to inject a dynamic link library (DLL) into another process for various purposes, including code execution and process manipulation.
This script is intended for educational purposes only. Unauthorized DLL injection can be illegal and unethical. Always ensure you have explicit permission before performing such actions on any system.
This Script was included in Python201 course by TCM Security. I have enchanced this a little bit and modified the dll file to make it more fun experience.
- Python 3.x
- Windows operating system
The script performs the following steps:
- Starts a new process (e.g., Notepad).
- Opens the target process with necessary permissions.
- Allocates memory in the target process.
- Writes the path to the DLL into the allocated memory.
- Retrieves the address of
LoadLibraryA
fromkernel32.dll
. - Creates a remote thread in the target process to load the DLL.
-
Prepare Your DLL:
- The provided .c file for the DLL is not included here. Compile it into a DLL yourself or create your own DLL.
- Modify the
dll_path
variable in the script to point to your compiled DLL.
-
Run the Script:
- Ensure you have Python installed on your system.
- Update the
dll_path
variable with the path to your DLL. - Run the script. It will start a new Notepad process and inject the DLL into it.
Here I used fun.cpp compiled dll which auto moves cursor to random position until the process is terminated.
- ctypes: A Python library for interfacing with C-style libraries and functions in Windows. It is used to call Windows API functions from Python.
- subprocess: A Python module used to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. In this script, it starts a new process (e.g., Notepad) for DLL injection.
- OpenProcess: Opens an existing local process object and obtains a handle to it. This handle is required for interacting with the process.
- VirtualAllocEx: Allocates memory within the address space of the specified process.
- WriteProcessMemory: Writes data to the memory of a specified process.
- GetModuleHandleA: Retrieves a module handle for the specified module (DLL or executable).
- GetProcAddress: Retrieves the address of an exported function or variable from the specified module.
- CreateRemoteThread: Creates a thread that runs in the virtual address space of another process.
- DLL Injection: A technique used to run code within the address space of another process. This can be used for debugging, extending application functionalities, or malicious purposes.
- Memory Allocation: Allocating memory within a process's address space to store data or code, such as a DLL path in this case.
- Remote Thread Creation: Creating a new thread in the target process to execute the injected DLL.
- Error Handling: Enhance error handling to provide more detailed feedback and recovery options.
- Dynamic DLL Path: Modify the script to accept the DLL path as a command-line argument for flexibility.
- Target Process Selection: Implement functionality to allow users to select the target process more dynamically.
- Security Considerations: Add features to ensure safe and authorized use of the injection technique.
For questions or further discussions, please contact me at syed4000saad@gmail.com.
Feel free to submit issues or pull requests for improvements.