DLL injection is a technique used to inject a dynamic-link library (DLL) into a running process to extend or modify its functionality. In this case, we'll demonstrate how to do DLL injection in a remote process for patching a bug in a legacy application.
To perform DLL injection in a remote process, we'll use the following steps:
Create a DLL with the desired functionality
Find the target application's process ID (PID)
Open the target process
Allocate memory in the target process for the DLL
Write the DLL into the allocated memory
Load the DLL into the target process
Enumerate loaded modules in the target process
Clean up
Let's go through each step in more detail:
Create a DLL with the desired functionality:
First, create a DLL that contains the code you want to inject into the target process. For example, let's say you have a DLL called "Patch.dll" with a function named "FixBug".
Find the target application's process ID (PID):
To find the PID of the target application, you can use tools like Task Manager or Process Explorer. Alternatively, you can use the following C/C++ code:
c++
#include <iostream>
#include <Windows.h>
int main() {
PROCESSENTRY32 processInfo;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
processInfo.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(snapshot, &processInfo)) {
do {
if (!_stricmp(processInfo.szExeFile, "legacyApp.exe")) {
DWORD pid = processInfo.th32ProcessID;
std::cout << "Process ID: " << pid << std::endl;
CloseHandle(snapshot);
return 0;
}
} while (Process32Next(snapshot, &processInfo));
}
CloseHandle(snapshot);
std::cout << "Process not found." << std::endl;
return 1;
}
This code will output the PID of the target application (replace "legacyApp.exe" with the name of your target application).
Open the target process:
To open the target process, use the OpenProcess function with the PROCESS_QUERY_INFORMATION and PROCESS_VM_OPERATION access rights.
c++
#include <iostream>
#include <Windows.h>
int main() {
DWORD pid = <target_process_id>;
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION, FALSE, pid);
if (process == NULL) {
std::cout << "Failed to open process." << std::endl;
return 1;
}
std::cout << "Process opened." << std::endl;
CloseHandle(process);
return 0;
}
Allocate memory in the target process for the DLL:
Allocate memory in the target process using the VirtualAllocEx function.
c++
#include <iostream>
#include <Windows.h>
int main() {
DWORD pid = <target_process_id>;
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION, FALSE, pid);
if (process == NULL) {
std::cout << "Failed to open process." << std::endl;
return 1;
}
LPVOID memory = VirtualAllocEx(process, NULL, sizeof(Patch), MEM_COMMIT, PAGE_READWRITE);
if (memory == NULL) {
std::cout << "Failed to allocate memory." << std::endl;
return 1;
}
std::cout << "Memory allocated." << std::endl;
CloseHandle(process);
return 0;
}
Write the DLL into the allocated memory:
Write the DLL into the allocated memory using the WriteProcessMemory function.