Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 90 additions & 88 deletions Memory.cpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
#include "stdafx.hpp"
#include "Memory.hpp"

int Memory::GetProcessId(char* processName) {
SetLastError(0);
PROCESSENTRY32 pe32;
HANDLE hSnapshot = NULL;
GetLastError();
pe32.dwSize = sizeof( PROCESSENTRY32 );
hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

if( Process32First( hSnapshot, &pe32 ) ) {
do {
if( strcmp( pe32.szExeFile, processName ) == 0 )
break;
} while( Process32Next( hSnapshot, &pe32 ) );
}

if( hSnapshot != INVALID_HANDLE_VALUE )
CloseHandle( hSnapshot );
int err = GetLastError();
//std::cout << err << std::endl;
if (err != 0)
int Memory::GetProcessId(const char* processName)
{
PROCESSENTRY32 pe32{};
pe32.dwSize = sizeof(pe32);

HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
return 0;
return pe32.th32ProcessID;

if (Process32First(hSnapshot, &pe32))
{
do
{
if (_stricmp(pe32.szExeFile, processName) == 0)
{
CloseHandle(hSnapshot);
return pe32.th32ProcessID;
}
} while (Process32Next(hSnapshot, &pe32));
}

CloseHandle(hSnapshot);
return 0;
}
int Memory::GetModuleBase(HANDLE processHandle, string &sModuleName)
{
HMODULE *hModules = NULL;
char szBuf[50];
DWORD cModules;
DWORD dwBase = -1;

EnumProcessModules(processHandle, hModules, 0, &cModules);
hModules = new HMODULE[cModules/sizeof(HMODULE)];

if(EnumProcessModules(processHandle, hModules, cModules/sizeof(HMODULE), &cModules)) {
for(size_t i = 0; i < cModules/sizeof(HMODULE); i++) {
if(GetModuleBaseName(processHandle, hModules[i], szBuf, sizeof(szBuf))) {
if(sModuleName.compare(szBuf) == 0) {
dwBase = (DWORD)hModules[i];
break;
}
}
}
}

delete[] hModules;
return dwBase;

long Memory::GetModuleBase(HANDLE processHandle, std::string& sModuleName)
{
HMODULE hModules[1024];
DWORD cbNeeded;

if (!EnumProcessModules(processHandle, hModules, sizeof(hModules), &cbNeeded))
return -1;

char szBuf[MAX_PATH];

for (unsigned int i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
{
if (GetModuleBaseNameA(processHandle, hModules[i], szBuf, sizeof(szBuf)))
{
if (sModuleName == szBuf)
return (long)hModules[i];
}
}

return -1;
}

BOOL Memory::SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege)
{
TOKEN_PRIVILEGES tp;
Expand All @@ -65,30 +64,30 @@ BOOL Memory::SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePriv
else
tp.Privileges[0].Attributes = 0;

if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) {
//printf("AdjustTokenPrivileges error: %u\n", GetLastError() );
return FALSE;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) {
//printf("AdjustTokenPrivileges error: %u\n", GetLastError() );
return FALSE;
}

if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
//printf("The token does not have the specified privilege. \n");
return FALSE;
//printf("The token does not have the specified privilege. \n");
return FALSE;
}

return TRUE;
}
BOOL Memory::GetDebugPrivileges(void) {
HANDLE hToken = NULL;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
HANDLE hToken = NULL;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
return FALSE; //std::cout << "OpenProcessToken() failed, error\n>> " << GetLastError() << std::endl;
//else std::cout << "OpenProcessToken() is OK, got the handle!" << std::endl;
if(!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE))

if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE))
return FALSE; //std::cout << "Failed to enable privilege, error:\n>> " << GetLastError() << std::endl;
return TRUE;

return TRUE;
}
int Memory::ReadInt(HANDLE processHandle, int address) {
int Memory::ReadInt(HANDLE processHandle, long address) {
if (address == -1)
return -1;
int buffer = 0;
Expand All @@ -97,33 +96,36 @@ int Memory::ReadInt(HANDLE processHandle, int address) {
BOOL success = ReadProcessMemory(processHandle, (LPCVOID)address, &buffer, NumberOfBytesToRead, &NumberOfBytesActuallyRead);
if (!success || NumberOfBytesActuallyRead != NumberOfBytesToRead) {
std::cout << "Memory Error!" << std::endl;
DWORD lastError = GetLastError();
if (lastError != 0)
std::cout << lastError << std::endl;
return -1;
}
//if (err || NumberOfBytesActuallyRead != NumberOfBytesToRead) {
// DWORD lastError = GetLastError();
// if (lastError != 0)
// DWORD lastError = GetLastError();
// if (lastError != 0)
// std::cout << lastError << std::endl;
// std::cout << "blub" << std::endl;
//}
return buffer;
//}
return buffer;
}
int Memory::GetPointerAddress(HANDLE processHandle, int startAddress, int offsets[], int offsetCount) {
int Memory::GetPointerAddress(HANDLE processHandle, long startAddress, int offsets[], int offsetCount) {
if (startAddress == -1)
return -1;
int ptr = ReadInt(processHandle, startAddress);
for (int i=0; i<offsetCount-1; i++) {
ptr+=offsets[i];
ptr = ReadInt(processHandle, ptr);
}
ptr+=offsets[offsetCount-1];
return ptr;
int ptr = ReadInt(processHandle, startAddress);
for (int i = 0; i < offsetCount - 1; i++) {
ptr += offsets[i];
ptr = ReadInt(processHandle, ptr);
}
ptr += offsets[offsetCount - 1];
return ptr;
}
int Memory::ReadPointerInt(HANDLE processHandle, int startAddress, int offsets[], int offsetCount) {
int Memory::ReadPointerInt(HANDLE processHandle, long startAddress, int offsets[], int offsetCount) {
if (startAddress == -1)
return -1;
return ReadInt(processHandle, GetPointerAddress(processHandle, startAddress, offsets, offsetCount));
return ReadInt(processHandle, GetPointerAddress(processHandle, startAddress, offsets, offsetCount));
}
float Memory::ReadFloat(HANDLE processHandle, int address) {
float Memory::ReadFloat(HANDLE processHandle, long address) {
if (address == -1)
return -1;
float buffer = 0.0;
Expand All @@ -132,33 +134,33 @@ float Memory::ReadFloat(HANDLE processHandle, int address) {
BOOL success = ReadProcessMemory(processHandle, (LPCVOID)address, &buffer, NumberOfBytesToRead, &NumberOfBytesActuallyRead);
if (!success || NumberOfBytesActuallyRead != NumberOfBytesToRead)
return -1;
return buffer;
return buffer;
}
float Memory::ReadPointerFloat(HANDLE processHandle, int startAddress, int offsets[], int offsetCount) {
float Memory::ReadPointerFloat(HANDLE processHandle, long startAddress, int offsets[], int offsetCount) {
if (startAddress == -1)
return -1;
return ReadFloat(processHandle, GetPointerAddress(processHandle, startAddress, offsets, offsetCount));
return ReadFloat(processHandle, GetPointerAddress(processHandle, startAddress, offsets, offsetCount));
}
char* Memory::ReadText(HANDLE processHandle, int address) {
char* Memory::ReadText(HANDLE processHandle, long address) {
if (address == -1)
return "-1";
return nullptr;
char buffer = !0;
char* stringToRead = new char[128];
char* stringToRead = new char[128];
SIZE_T NumberOfBytesToRead = sizeof(buffer);
SIZE_T NumberOfBytesActuallyRead;
int i = 0;
while (buffer != 0) {
BOOL success = ReadProcessMemory(processHandle, (LPCVOID)address, &buffer, NumberOfBytesToRead, &NumberOfBytesActuallyRead);
int i = 0;
while (buffer != 0) {
BOOL success = ReadProcessMemory(processHandle, (LPCVOID)address, &buffer, NumberOfBytesToRead, &NumberOfBytesActuallyRead);
if (!success || NumberOfBytesActuallyRead != NumberOfBytesToRead)
return "-1";
return nullptr;
stringToRead[i] = buffer;
i++;
address++;
}
i++;
address++;
}
return stringToRead;
}
char* Memory::ReadPointerText(HANDLE processHandle, int startAddress, int offsets[], int offsetCount) {
char* Memory::ReadPointerText(HANDLE processHandle, long startAddress, int offsets[], int offsetCount) {
if (startAddress == -1)
return "-1";
return ReadText(processHandle, GetPointerAddress(processHandle, startAddress, offsets, offsetCount));
return nullptr;
return ReadText(processHandle, GetPointerAddress(processHandle, startAddress, offsets, offsetCount));
}
23 changes: 11 additions & 12 deletions Memory.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
#pragma once
#include "stdafx.hpp"
#include "Memory.hpp"
#include <Windows.h>
#include <TlHelp32.h>
#include <string>
#include <iostream>
#include <psapi.h>
#pragma comment(lib, "psapi")
using std::string;

class Memory
{
public:
int GetProcessId(char* processName);
int GetModuleBase(HANDLE processHandle, string &sModuleName);
int GetProcessId(const char* processName);
long GetModuleBase(HANDLE processHandle, std::string& sModuleName);
BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege);
BOOL GetDebugPrivileges(void);
int ReadInt(HANDLE processHandle, int address);
int GetPointerAddress(HANDLE processHandle, int startAddress, int offsets[], int offsetCount);
int ReadPointerInt(HANDLE processHandle, int startAddress, int offsets[], int offsetCount);
float ReadFloat(HANDLE processHandle, int address);
float ReadPointerFloat(HANDLE processHandle, int startAddress, int offsets[], int offsetCount);
char* ReadText(HANDLE processHandle, int address);
char* ReadPointerText(HANDLE processHandle, int startAddress, int offsets[], int offsetCount);
};
int ReadInt(HANDLE processHandle, long address);
int GetPointerAddress(HANDLE processHandle, long startAddress, int offsets[], int offsetCount);
int ReadPointerInt(HANDLE processHandle, long startAddress, int offsets[], int offsetCount);
float ReadFloat(HANDLE processHandle, long address);
float ReadPointerFloat(HANDLE processHandle, long startAddress, int offsets[], int offsetCount);
char* ReadText(HANDLE processHandle, long address);
char* ReadPointerText(HANDLE processHandle, long startAddress, int offsets[], int offsetCount);
};