// Struct to ensure HANDLEs are properly closed
struct HandleCloser {
void operator()(HANDLE handle) const {
if (handle && handle != INVALID_HANDLE_VALUE) {
CloseHandle(handle);
}
}
};
using UniqueHandle = std::unique_ptr<std::remove_pointer::type, HandleCloser>;
// Function to log messages to a log file only
void log_message(std::ofstream& log_file, const char* format, ...) {
va_list args;
va_start(args, format);
char buffer[2048]; // Increased buffer size for longer messages
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
code
log_file << buffer;
log_file.flush();
}
// Function to retrieve the Process ID (PID) by process name
DWORD get_process_id_by_name(const std::string& process_name, std::ofstream& log_file) {
UniqueHandle hSnapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
if (!hSnapshot || hSnapshot.get() == INVALID_HANDLE_VALUE) {
log_message(log_file, "CreateToolhelp32Snapshot failed with error: %lu\n", GetLastError());
return 0;
}
code
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot.get(), &pe32)) {
log_message(log_file, "Process32First failed with error: %lu\n", GetLastError());
return 0;
}
do {
std::wstring ws(pe32.szExeFile);
std::string exeFile(ws.begin(), ws.end());
if (_stricmp(exeFile.c_str(), process_name.c_str()) == 0) {
return pe32.th32ProcessID;
}
} while (Process32Next(hSnapshot.get(), &pe32));
return 0;
}
// Function to retrieve the base address of the main module of the process
uintptr_t get_base_address(DWORD pid, const std::string& process_name, std::ofstream& log_file) {
UniqueHandle hSnapshot(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid));
if (!hSnapshot || hSnapshot.get() == INVALID_HANDLE_VALUE) {
log_message(log_file, "CreateToolhelp32Snapshot for modules failed with error: %lu\n", GetLastError());
return 0;
}
code
MODULEENTRY32 me32;
me32.dwSize = sizeof(MODULEENTRY32);
if (!Module32First(hSnapshot.get(), &me32)) {
log_message(log_file, "Module32First failed with error: %lu\n", GetLastError());
return 0;
}
do {
// Convert module name to std::string for comparison
std::wstring ws(me32.szModule);
std::string moduleName(ws.begin(), ws.end());
// Compare the module name with the process name
if (_stricmp(moduleName.c_str(), process_name.c_str()) == 0) {
log_message(log_file, "Main module %s found at base address 0x%llX\n", moduleName.c_str(),
reinterpret_cast<unsigned long long>(me32.modBaseAddr));
return reinterpret_cast<uintptr_t>(me32.modBaseAddr);
}
} while (Module32Next(hSnapshot.get(), &me32));
log_message(log_file, "Main module not found in the module list.\n");
return 0;
}
// Struct to store section information
struct Section {
std::string name;
uintptr_t start;
uintptr_t end;
};
// Function to parse PE headers and extract sections
bool get_sections(HANDLE hProcess, uintptr_t base_addr, std::vector& sections, std::ofstream& log_file) {
// Read the DOS header
IMAGE_DOS_HEADER dosHeader;
SIZE_T bytesRead;
if (!ReadProcessMemory(hProcess, reinterpret_cast(base_addr), &dosHeader, sizeof(dosHeader), &bytesRead) || bytesRead != sizeof(dosHeader)) {
log_message(log_file, "Failed to read DOS header. Error: %lu\n", GetLastError());
return false;
}
code
}
// Function to copy the memory of the target process to shadow memory
bool copy_memory_to_shadow(HANDLE hProcess, uintptr_t base_addr, std::vector& sections, std::vector& shadow_memory, std::ofstream& log_file) {
for (const auto& section : sections) {
uintptr_t addr = section.start;
while (addr < section.end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, reinterpret_cast(addr), &mbi, sizeof(mbi))) {
log_message(log_file, "VirtualQueryEx failed at address 0x%llX with error: %lu\n",
static_cast(addr), GetLastError());
break;
}
// Function to search for a string within the shadow memory
uintptr_t find_string_in_shadow_memory(const std::vector& shadow_memory, const std::string& string_to_search, std::ofstream& log_file) {
size_t string_length = string_to_search.length();
for (size_t i = 0; i < shadow_memory.size() - string_length + 1; ++i) {
if (std::memcmp(shadow_memory.data() + i, string_to_search.c_str(), string_length) == 0) {
return i;
}
}
code
return 0;
}
// Function to enable debug privileges
bool EnableDebugPrivileges() {
HANDLE hToken;
LUID luid;
TOKEN_PRIVILEGES tp;