C++ Memory Hacking Error please help.

Posts 18 of 8 · Page 1 of 1
C++ Memory Hacking Error please help.
Hi, so im trying to make a trainer for GTAV singleplayer but it isn't working.
There is no errors.
I have tried
Code:
WriteProcessMemory(hProcHandle, (LPVOID)("GTA5.exe"+HealthPointer+HealthOffset), &HealthVal, sizeof(HealthVal), NULL);
And that didn't work either.

Please help!
CE.PNG9 KB · 51 downloads CE2.PNG5 KB · 41 downloads Ce3.PNG5 KB · 12 downloads Ce4.PNG5 KB · 36 downloads
== screenshots not yet approved ==
so idk the exact problem but here is how you should do this:

1. OpenProcess handle using Windows.h lib
2. Get a base address of GTA5 process (not the string lol)
3. Read (health pointer + gta base address)
4. Write your health to (value from 3. + health offset)
Okay thanks... But the thing is that the screenshots are pictures of the pointer and such things from Cheat Engine
(LPVOID)("GTA5.exe"+HealthPointer+HealthOffset) -> This is wrong. Utterly wrong. Did you miss the pointers lesson?

"GTA5.exe" -> Base address of EXE. Sometimes it's a fixed address like 0x400000. But can be different on each EXE launch.
https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

if you are coding a 32bit program then DWORD HealthAddress = *(DWORD*)((DWORD)BaseAddress + HealthPointer) + HealthOffset;
Now I have done this. It is still not working. I made it print out the value of hAddr but it came out as 0:

Code:
#include <windows.h>
#include <iostream>
using namespace std;

DWORD dwProcId;
DWORD64 hAddr = NULL;
DWORD64 bAddr = 0x7FF722F80000;
DWORD64 pointer = 0x01BED750;
DWORD64 offset = 0x280;
float HealthVal = 200.0;
float health;
int h;

int main(void)
{
	HWND hWnd = FindWindowA(NULL, LPCSTR("Grand Theft Auto V"));
	GetWindowThreadProcessId(hWnd, &dwProcId);
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcId);

	ReadProcessMemory(handle, (LPVOID)(bAddr+pointer), &hAddr, sizeof(hAddr), NULL);
	cout << hAddr << endl;
	while (!GetAsyncKeyState(VK_INSERT))
	{
		WriteProcessMemory(handle, (LPVOID)HealthAddress, &HealthVal, sizeof(HealthVal), NULL);
	}
stdafx.h
Code:
#include <vector>
#include <string>
#include <Windows.h>
#include <TlHelp32.h>
#include <codecvt>
#include <sstream>

#include "mem.h"
mem.h
Code:
#ifndef __MEMORY__

class Memory
{
private:
	HANDLE pHandle;
	int pID;
	uint64_t BaseAddress;
	std::string FilePath;
	uint64_t iSize;
public:
	Memory(const char*, const char* = nullptr);
	~Memory();
	bool isHex(char c);
	std::string getFilePath();
	uint64_t getBaseAddress();
	int getProcessId();
	uint64_t getImageSize();
	HANDLE getHandle();
	void Memory::initVars(MODULEENTRY32);
	MODULEENTRY32 getModule(const char*, PROCESSENTRY32);
	PROCESSENTRY32 getProcess(const char*);
	uint64_t getAddress(uint64_t, std::vector<uint64_t>);
	uint64_t patternScan(char* sig, std::string, uint64_t = 0);
	uint64_t patternScan(const std::string&, uint64_t = 0);
	bool createPattern(const std::string&, std::string&, std::string&);

	template<typename T> T read(uint64_t Address, std::vector<uint64_t> offsets = {})
	{
		uint64_t nAddress = getAddress(Address, offsets);
		T ret;
		ReadProcessMemory(pHandle, (void*)nAddress, &ret, sizeof(T), 0);
		return ret;
	}

	template<typename T> void write(uint64_t Address, T value, std::vector<uint64_t> offsets = {})
	{
		uint64_t nAddress = getAddress(Address, offsets);
		WriteProcessMemory(pHandle, (void*)nAddress, &value, sizeof(T), 0);
	}

	std::string readString(uint64_t, int, std::vector<uint64_t> = {});
	void writeString(uint64_t, std::string&, std::vector<uint64_t> = {});
};

#define __MEMORY__
#endif
mem.cpp
Code:
#include "stdafx.h"

Memory::Memory(const char* processName, const char* moduleName)
{
	Memory::pID = 0;
	Memory::BaseAddress = 0;
	Memory::iSize = 0;
	Memory::initVars(Memory::getModule(moduleName == nullptr ? processName : moduleName, Memory::getProcess(processName)));
	Memory::pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pID);
}

Memory::~Memory()
{
	CloseHandle(pHandle);
}

std::string Memory::getFilePath()
{
	return FilePath;
}

uint64_t Memory::getBaseAddress()
{
	return BaseAddress;
}

int Memory::getProcessId()
{
	return pID;
}

uint64_t Memory::getImageSize()
{
	return iSize;
}

HANDLE Memory::getHandle()
{
	return pHandle;
}

void Memory::initVars(MODULEENTRY32 m)
{
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> w2sConverter;
	Memory::FilePath = w2sConverter.to_bytes(m.szExePath).c_str();

	Memory::BaseAddress = (uint64_t)m.modBaseAddr;
	Memory::pID = m.th32ProcessID;
	Memory::iSize = m.modBaseSize;
}

PROCESSENTRY32 Memory::getProcess(const char* processName)
{
	PROCESSENTRY32 pe32;
	std::wstring wprocessName;
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> s2wconverter;
	HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	
	if (hSnap != INVALID_HANDLE_VALUE)
	{
		pe32.dwSize = sizeof(PROCESSENTRY32);
		if (Process32First(hSnap, &pe32) == 0)
			CloseHandle(hSnap);
		do
		{
			wprocessName = s2wconverter.from_bytes(processName);
			if (lstrcmp(pe32.szExeFile, wprocessName.c_str()) == 0)
			{
				CloseHandle(hSnap);
				return pe32;
			}
		} while (Process32Next(hSnap, &pe32));
		CloseHandle(hSnap);
	}
	return pe32;
}

MODULEENTRY32 Memory::getModule(const char* moduleName, PROCESSENTRY32 process)
{
	MODULEENTRY32 xModule;
	std::wstring wmoduleName;
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> s2wconverter;
	HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, process.th32ProcessID);
	
	if (hSnap != INVALID_HANDLE_VALUE)
	{
		xModule.dwSize = sizeof(MODULEENTRY32);
		if (Module32First(hSnap, &xModule) == 0)
			CloseHandle(hSnap);
		do
		{
			wmoduleName = s2wconverter.from_bytes(moduleName);
			if (lstrcmp(xModule.szModule, wmoduleName.c_str()) == 0)
			{
				return xModule;
			}
		} while (Module32Next(hSnap, &xModule));
		CloseHandle(hSnap);
	}
	return xModule;
}

bool Memory::isHex(char c)
{
	return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

bool Memory::createPattern(const std::string& pattern, std::string& pattern_result, std::string& mask_result)
{
	bool result = false;
	size_t l = pattern.size();
	if (l-- > 0)
	{
		char buffer[2];
		std::stringstream pattern_s;
		std::stringstream mask_s;
		for (size_t i = 0; i < l; i++)
		{
			if (!Memory::isHex(pattern[i]))
			{
				if (pattern[i] == '?')
				{
					pattern_s << "\x90";
					mask_s << '?';
				}
			}
			else
			{
				buffer[0] = pattern[i];
				buffer[1] = (l >= i + 1 && Memory::isHex(pattern[i + 1])) ? pattern[++i] : 0;
				pattern_s << (char)strtol(buffer, nullptr, 16);
				mask_s << 'x';
			}
		}
		result = true;
		pattern_result = pattern_s.str();
		mask_result = mask_s.str();
	}
	return result;
}

uint64_t Memory::patternScan(const std::string& pattern, uint64_t startAddress)
{
	std::string sub_ptr;
	std::string sub_mask;
	Memory::createPattern(pattern, sub_ptr, sub_mask);
	return Memory::patternScan(&sub_ptr[0], sub_mask, startAddress);
}

uint64_t Memory::patternScan(char* sig, std::string pattern, uint64_t startAddress)
{
	if (startAddress < Memory::BaseAddress || startAddress > (Memory::BaseAddress + Memory::iSize))
		startAddress = Memory::BaseAddress;
	char* cBuffer = new char[Memory::iSize];
	ReadProcessMemory(Memory::pHandle, (void*)startAddress, cBuffer, Memory::iSize, 0);
	size_t patternLength = pattern.size() - 1;
	for (size_t i = 0, j = 0, matches = 0; i != Memory::iSize; i++)
	{
		if (cBuffer[i] == sig[j] || pattern[j] == '?')
		{
			if (++matches == patternLength)
			{
				delete cBuffer;
				return (startAddress + i - j);
			}
				
			j++;
		}
		else if (cBuffer[i] == sig[0] || pattern[0] == '?')
		{
			matches = 1;
			j = 1;
		}
		else
		{
			matches = 0;
			j = 0;
		}
	}
	delete[] cBuffer;
	return 0;
}

uint64_t Memory::getAddress(uint64_t Address, std::vector<uint64_t> offsets)
{
	uint64_t nAddress = Address;
	size_t size = offsets.size();
	if (size)
	{
		size--;
		ReadProcessMemory(Memory::pHandle, (void*)nAddress, &nAddress, sizeof(nAddress), 0);
		for (size_t i = 0; i != size; i++)
			ReadProcessMemory(Memory::pHandle, (void*)(nAddress + offsets[i]), &nAddress, sizeof(nAddress), 0);
		nAddress = nAddress + offsets[size];
	}
	return nAddress;
}

std::string Memory::readString(uint64_t Address, int size, std::vector<uint64_t> offsets)
{
	std::string ret;
	uint64_t nAddress = Memory::getAddress(Address, offsets);
	char* buffer = new char[size];
	ReadProcessMemory(Memory::pHandle, (void*)nAddress, buffer, size, 0);
	ret = buffer;
	delete[] buffer;
	return ret;
}

void Memory::writeString(uint64_t Address, std::string& str, std::vector<uint64_t> offsets)
{
	uint64_t nAddress = Memory::getAddress(Address, offsets);
	WriteProcessMemory(Memory::pHandle, (void*)nAddress, &str[0], str.size(), 0);
	return;
}
Code:
#include "stdafx.h"


int main()
{
	Memory GTA("GTA5.exe");
	uint64_t world = GTA.patternScan("48 8B 05 ? ? ? ? 45 ? ? ? ? 48 8B 48 08 48 85 C9 74 07");
	world = world + GTA.read<int>(world + 3) + 7;
	float Health = GTA.read<float>(world, { 8, 0x280 });
	printf("Health is %.2f\n", Health);
	std::string name = GTA.readString(world, 12, { 8, 0x10B8, 0x7C });
	printf("Playername: %s\n", &name[0]);
	_gettchar();
    return 0;
}
edit: nvm, i got it.

Thanks.
Quote Originally Posted by MikeRohsoft View Post
stdafx.h
Code:
#include <vector>
#include <string>
#include <Windows.h>
#include <TlHelp32.h>
#include <codecvt>
#include <sstream>

#include "mem.h"
mem.h
Code:
#ifndef __MEMORY__

class Memory
{
private:
	HANDLE pHandle;
	int pID;
	uint64_t BaseAddress;
	std::string FilePath;
	uint64_t iSize;
public:
	Memory(const char*, const char* = nullptr);
	~Memory();
	bool isHex(char c);
	std::string getFilePath();
	uint64_t getBaseAddress();
	int getProcessId();
	uint64_t getImageSize();
	HANDLE getHandle();
	void Memory::initVars(MODULEENTRY32);
	MODULEENTRY32 getModule(const char*, PROCESSENTRY32);
	PROCESSENTRY32 getProcess(const char*);
	uint64_t getAddress(uint64_t, std::vector<uint64_t>);
	uint64_t patternScan(char* sig, std::string, uint64_t = 0);
	uint64_t patternScan(const std::string&, uint64_t = 0);
	bool createPattern(const std::string&, std::string&, std::string&);

	template<typename T> T read(uint64_t Address, std::vector<uint64_t> offsets = {})
	{
		uint64_t nAddress = getAddress(Address, offsets);
		T ret;
		ReadProcessMemory(pHandle, (void*)nAddress, &ret, sizeof(T), 0);
		return ret;
	}

	template<typename T> void write(uint64_t Address, T value, std::vector<uint64_t> offsets = {})
	{
		uint64_t nAddress = getAddress(Address, offsets);
		WriteProcessMemory(pHandle, (void*)nAddress, &value, sizeof(T), 0);
	}

	std::string readString(uint64_t, int, std::vector<uint64_t> = {});
	void writeString(uint64_t, std::string&, std::vector<uint64_t> = {});
};

#define __MEMORY__
#endif
mem.cpp
Code:
#include "stdafx.h"

Memory::Memory(const char* processName, const char* moduleName)
{
	Memory::pID = 0;
	Memory::BaseAddress = 0;
	Memory::iSize = 0;
	Memory::initVars(Memory::getModule(moduleName == nullptr ? processName : moduleName, Memory::getProcess(processName)));
	Memory::pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pID);
}

Memory::~Memory()
{
	CloseHandle(pHandle);
}

std::string Memory::getFilePath()
{
	return FilePath;
}

uint64_t Memory::getBaseAddress()
{
	return BaseAddress;
}

int Memory::getProcessId()
{
	return pID;
}

uint64_t Memory::getImageSize()
{
	return iSize;
}

HANDLE Memory::getHandle()
{
	return pHandle;
}

void Memory::initVars(MODULEENTRY32 m)
{
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> w2sConverter;
	Memory::FilePath = w2sConverter.to_bytes(m.szExePath).c_str();

	Memory::BaseAddress = (uint64_t)m.modBaseAddr;
	Memory::pID = m.th32ProcessID;
	Memory::iSize = m.modBaseSize;
}

PROCESSENTRY32 Memory::getProcess(const char* processName)
{
	PROCESSENTRY32 pe32;
	std::wstring wprocessName;
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> s2wconverter;
	HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	
	if (hSnap != INVALID_HANDLE_VALUE)
	{
		pe32.dwSize = sizeof(PROCESSENTRY32);
		if (Process32First(hSnap, &pe32) == 0)
			CloseHandle(hSnap);
		do
		{
			wprocessName = s2wconverter.from_bytes(processName);
			if (lstrcmp(pe32.szExeFile, wprocessName.c_str()) == 0)
			{
				CloseHandle(hSnap);
				return pe32;
			}
		} while (Process32Next(hSnap, &pe32));
		CloseHandle(hSnap);
	}
	return pe32;
}

MODULEENTRY32 Memory::getModule(const char* moduleName, PROCESSENTRY32 process)
{
	MODULEENTRY32 xModule;
	std::wstring wmoduleName;
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> s2wconverter;
	HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, process.th32ProcessID);
	
	if (hSnap != INVALID_HANDLE_VALUE)
	{
		xModule.dwSize = sizeof(MODULEENTRY32);
		if (Module32First(hSnap, &xModule) == 0)
			CloseHandle(hSnap);
		do
		{
			wmoduleName = s2wconverter.from_bytes(moduleName);
			if (lstrcmp(xModule.szModule, wmoduleName.c_str()) == 0)
			{
				return xModule;
			}
		} while (Module32Next(hSnap, &xModule));
		CloseHandle(hSnap);
	}
	return xModule;
}

bool Memory::isHex(char c)
{
	return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

bool Memory::createPattern(const std::string& pattern, std::string& pattern_result, std::string& mask_result)
{
	bool result = false;
	size_t l = pattern.size();
	if (l-- > 0)
	{
		char buffer[2];
		std::stringstream pattern_s;
		std::stringstream mask_s;
		for (size_t i = 0; i < l; i++)
		{
			if (!Memory::isHex(pattern[i]))
			{
				if (pattern[i] == '?')
				{
					pattern_s << "\x90";
					mask_s << '?';
				}
			}
			else
			{
				buffer[0] = pattern[i];
				buffer[1] = (l >= i + 1 && Memory::isHex(pattern[i + 1])) ? pattern[++i] : 0;
				pattern_s << (char)strtol(buffer, nullptr, 16);
				mask_s << 'x';
			}
		}
		result = true;
		pattern_result = pattern_s.str();
		mask_result = mask_s.str();
	}
	return result;
}

uint64_t Memory::patternScan(const std::string& pattern, uint64_t startAddress)
{
	std::string sub_ptr;
	std::string sub_mask;
	Memory::createPattern(pattern, sub_ptr, sub_mask);
	return Memory::patternScan(&sub_ptr[0], sub_mask, startAddress);
}

uint64_t Memory::patternScan(char* sig, std::string pattern, uint64_t startAddress)
{
	if (startAddress < Memory::BaseAddress || startAddress > (Memory::BaseAddress + Memory::iSize))
		startAddress = Memory::BaseAddress;
	char* cBuffer = new char[Memory::iSize];
	ReadProcessMemory(Memory::pHandle, (void*)startAddress, cBuffer, Memory::iSize, 0);
	size_t patternLength = pattern.size() - 1;
	for (size_t i = 0, j = 0, matches = 0; i != Memory::iSize; i++)
	{
		if (cBuffer[i] == sig[j] || pattern[j] == '?')
		{
			if (++matches == patternLength)
			{
				delete cBuffer;
				return (startAddress + i - j);
			}
				
			j++;
		}
		else if (cBuffer[i] == sig[0] || pattern[0] == '?')
		{
			matches = 1;
			j = 1;
		}
		else
		{
			matches = 0;
			j = 0;
		}
	}
	delete[] cBuffer;
	return 0;
}

uint64_t Memory::getAddress(uint64_t Address, std::vector<uint64_t> offsets)
{
	uint64_t nAddress = Address;
	size_t size = offsets.size();
	if (size)
	{
		size--;
		ReadProcessMemory(Memory::pHandle, (void*)nAddress, &nAddress, sizeof(nAddress), 0);
		for (size_t i = 0; i != size; i++)
			ReadProcessMemory(Memory::pHandle, (void*)(nAddress + offsets[i]), &nAddress, sizeof(nAddress), 0);
		nAddress = nAddress + offsets[size];
	}
	return nAddress;
}

std::string Memory::readString(uint64_t Address, int size, std::vector<uint64_t> offsets)
{
	std::string ret;
	uint64_t nAddress = Memory::getAddress(Address, offsets);
	char* buffer = new char[size];
	ReadProcessMemory(Memory::pHandle, (void*)nAddress, buffer, size, 0);
	ret = buffer;
	delete[] buffer;
	return ret;
}

void Memory::writeString(uint64_t Address, std::string& str, std::vector<uint64_t> offsets)
{
	uint64_t nAddress = Memory::getAddress(Address, offsets);
	WriteProcessMemory(Memory::pHandle, (void*)nAddress, &str[0], str.size(), 0);
	return;
}
Code:
#include "stdafx.h"


int main()
{
	Memory GTA("GTA5.exe");
	uint64_t world = GTA.patternScan("48 8B 05 ? ? ? ? 45 ? ? ? ? 48 8B 48 08 48 85 C9 74 07");
	world = world + GTA.read<int>(world + 3) + 7;
	float Health = GTA.read<float>(world, { 8, 0x280 });
	printf("Health is %.2f\n", Health);
	std::string name = GTA.readString(world, 12, { 8, 0x10B8, 0x7C });
	printf("Playername: %s\n", &name[0]);
	_gettchar();
    return 0;
}
Sorry for possibly bumping this thread!

Hi Mike, I know this might all be outdated... But still wanted to like ask a question:

Everything works how it should but eventually, I get this error:

Code:
Unhandled exception at 0x76D22CF2 in whatever.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x00F5F230.
Am I out of memory? I am on a 64Bit machine. Dissected PE Headers:

Thanks in advance
Posts 18 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?