2. Persistent DLL Injection
2. Persistent DLL Injection
C++ Code
#include <windows.h>
#include <iostream>
#include <tlhelp32.h>
#include "validations.h"
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); // Open the target process with all access rights
if (hProc == NULL) {
cout << "OpenProcess() failed: " << GetLastError() << endl;
return false;
}
return 0;
}
1. DLL Injection: It injects a custom DLL (s12maldev.dll) into a target process (in this case, “notepad.exe”).
2. Registry Key Modification: It adds the executable of the program to the Windows Run registry key, ensuring that the program runs automatically when Windows starts.
3. Monitoring Loop: It continuously monitors whether the target process is running and whether the DLL is already injected. If not, it performs the DLL injection.
• Header Includes:
◦ <windows.h>: Provides access to Windows API functions and data types.
◦ <iostream>: Provides input and output stream functionality.
◦ <tlhelp32.h>: Includes functions and data structures for working with processes and snapshots.
◦ "validations.h": Presumably contains custom validation functions (not included in the provided code).
• getPIDbyProcName Function:
◦ It finds the Process ID (PID) of a process by its name.
◦ It uses the ToolHelp32 API to iterate through running processes and match the target process name.
• DLLinjector Function:
◦ It injects a DLL into a specified process.
◦ It opens the target process with full access rights.
◦ It allocates memory in the target process to hold the DLL path.
◦ It writes the DLL path to the allocated memory.
◦ It creates a remote thread in the target process to execute LoadLibraryA and load the DLL.
◦ Finally, it releases allocated resources.
• runkeys Function:
◦ It adds the executable’s path to the Windows Run registry key to ensure the program starts with Windows.
◦ It uses the Windows Registry API to achieve this.
• main Function:
◦ It sets the DLL path (path) and the target process name (process).
◦ It continuously monitors the target process:
▪ Checks if the target process is running using IsProcessRunning.
▪ If the process is running:
▪ Obtains the process ID (pid) using getPIDbyProcName.
▪ Checks if the DLL is already loaded in the target process using IsDLLLoaded.
▪ If the DLL is not loaded, it injects the DLL using DLLinjector.
▪ It obtains the path to the current executable and adds it to the Windows Run registry key using runkeys.
In summary, this code is a utility that can be used for injecting a custom DLL into a target process and ensuring that a program starts automatically with Windows by adding it to the Run registry key.
It continuously monitors the target process, performing the injection when needed. The exact behavior and purpose of the custom DLL (s12maldev.dll) and other custom validation functions from
"validations.h" are not provided in this code snippet.
validations.h
C++ Code
#include <iostream>
PROCESSENTRY32 processEntry;
processEntry.dwSize = sizeof(PROCESSENTRY32);
CloseHandle(hSnapshot); // Close the handle to the snapshot if the process is not found
return false; // Return false if the process is not found
}
// Function to check if a DLL with the specified name is loaded in a given process.
bool IsDLLLoaded(DWORD processId, const std::wstring& dllName) {
HANDLE hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processId); // Take a snapshot of the modules loaded in the target process
if (hModuleSnap == INVALID_HANDLE_VALUE) {
return false; // Return false if the snapshot creation fails
}
MODULEENTRY32W moduleEntry;
moduleEntry.dwSize = sizeof(MODULEENTRY32W);