Skip to content

Instantly share code, notes, and snippets.

@Fuwn
Last active September 26, 2022 20:07
Show Gist options
  • Save Fuwn/ee8be755100bddb6f33a02dd7f05eed3 to your computer and use it in GitHub Desktop.
Save Fuwn/ee8be755100bddb6f33a02dd7f05eed3 to your computer and use it in GitHub Desktop.
CSGO Memory Addressing
#include "Memory.h"
MemoryManager* Mem; // So I can use the MemoryManager later lol
int main() {
Mem = new MemoryManager();
delete Mem; //Don't forget to delete MemoryManager pointer, so it executes destructor and closes correctly the handle
return 0;
}
#pragma once
#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>
class MemoryManager {
private:
DWORD dwPID; // CS:GO process ID
HANDLE hProcess; // CS:GO handle process
public:
MODULEENTRY32 ClientDLL;
MODULEENTRY32 EngineDLL;
DWORD ClientDLL_Base, ClientDLL_Size;
DWORD EngineDLL_Base, EngineDLL_Size;
};
bool AttachProcess(char* ProcessName) {
HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(procEntry);
const WCHAR* procNameChar;
int nChars = MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, NULL, 0);
procNameChar = new WCHAR[nChars];
MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, (LPWSTR)procNameChar, nChars);
do
if (!wcscmp(procEntry.szExeFile, procNameChar)) {
this->dwPID = procEntry.th32ProcessID;
CloseHandle(hPID);
this->hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, this->dwPID);
return true;
}
while (Process32Next(hPID, &procEntry));
CloseHandle(hPID);
return false;
}
MODULEENTRY32 GetModule(char* ModuleName) {
HANDLE hModule = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
MODULEENTRY32 mEntry;
mEntry.dwSize = sizeof(mEntry);
const WCHAR* modNameChar;
int nChars = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
modNameChar = new WCHAR[nChars];
MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, (LPWSTR)modNameChar, nChars);
do
if (!wcscmp(mEntry.szModule, modNameChar)) {
CloseHandle(hModule);
return mEntry;
}
while (Module32Next(hModule, &mEntry));
CloseHandle(hModule);
mEntry.modBaseAddr = 0x0;
return mEntry;
}
template<class c>
c Read(DWORD dwAddress) {
c val;
ReadProcessMemory(hProcess, (LPVOID)dwAddress, &val, sizeof(c), NULL);
return val;
}
template<class c>
BOOL Write(DWORD dwAddress, c ValueToWrite) {
return WriteProcessMemory(hProcess, (LPVOID)dwAddress, &ValueToWrite, sizeof(c), NULL);
}
DWORD GetProcID() {
return this->dwPID;
}
HANDLE GetProcHandle() {
return this->hProcess;
}
MemoryManager() {
this->hProcess = NULL;
this->dwPID = NULL;
try {
if (!AttachProcess("csgo.exe")) throw 1;
this->ClientDLL = GetModule("client.dll");
this->EngineDLL = GetModule("engine.dll");
this->ClientDLL_Base = (DWORD)this->ClientDLL.modBaseAddr;
this->EngineDLL_Base = (DWORD)this->EngineDLL.modBaseAddr;
if (this->ClientDLL_Base == 0x0) throw 2;
if (this->EngineDLL_Base == 0x0) throw 3;
this->ClientDLL_Size = this->ClientDLL.dwSize;
this->EngineDLL_Size = this->EngineDLL.dwSize;
}
catch (int iEx) {
switch (iEx) {
case 1: MessageBoxA(NULL, "CS:GO must be running", "ERROR", MB_ICONSTOP | MB_OK); exit(0); break;
case 2: MessageBoxA(NULL, "Couldn't find Client.dll", "ERROR", MB_ICONSTOP | MB_OK); exit(0); break;
case 3: MessageBoxA(NULL, "Couldn't find Engine.dll", "ERROR", MB_ICONSTOP | MB_OK); exit(0); break;
default throw;
}
}
catch (...) {
MessageBoxA(NULL, "Unhandled exception thrown", "ERROR", MB_ICONSTOP | MB_OK);
exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment