- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
class Memory
{
public:
Memory() : hProcess{ nullptr }, pID{ 0 } {}
~Memory() { SafeCloseHandle(hProcess); }
bool Attach(const char* _Process, DWORD rights)
{
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap == INVALID_HANDLE_VALUE)
return false;
PROCESSENTRY32 pe{ sizeof(PROCESSENTRY32) };
while (Process32Next(hSnap, &pe))
if (!strcmp(pe.szExeFile, _Process))
{
SafeCloseHandle(hSnap);
pID = pe.th32ProcessID;
hProcess = OpenProcess(rights, 0, pe.th32ProcessID);
#ifdef _DEBUG || DEBUG
if (hProcess) // Ok
{
std::cout << "Attaching to process: " << _Process << " -> ";
ColoredMessage("OK!", LGREEN, true);
}
#endif // DEBUG
return true;
}
#ifdef _DEBUG || DEBUG
std::cout << "Attaching to process: " << _Process << " -> ";
ColoredMessage("FAIL!", LRED, true);
#endif // DEBUG
SafeCloseHandle(hSnap);
return false;
}
PModule GetModule(const char* _Module)
{
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pID);
if (hSnap == INVALID_HANDLE_VALUE)
return PModule{ 0, 0 };
MODULEENTRY32 mEntry{ sizeof(MODULEENTRY32) };
while (Module32Next(hSnap, &mEntry))
if (!strcmp(mEntry.szModule, _Module))
{
SafeCloseHandle(hSnap);
return PModule{ (DWORD)mEntry.hModule, mEntry.modBaseSize };
}
#ifdef _DEBUG || DEBUG
std::cout << "Getting module: "; ColoredMessage("FAIL!", LRED, true);
#endif // DEBUG
SafeCloseHandle(hSnap);
return PModule{ 0, 0 };
}
template <class _Ty>
_Ty read(DWORD _Addr)
{
DWORD _dwRead;
_Ty _Val;
#ifdef _DEBUG || DEBUG
if (!ReadProcessMemory(hProcess, (LPCVOID)_Addr, &_Val, sizeof(_Ty), &_dwRead))
{
std::cout << "Reading the memory: ";
ColoredMessage("FAIL!", LRED, true);
}
#else // DEBUG
ReadProcessMemory(hProcess, (LPCVOID)_Addr, &_Val, sizeof(_Ty), &_dwRead)
#endif
return _Val;
}
template <class _Ty>
void write(DWORD _Addr, _Ty _Val)
{
DWORD _dwWrite;
#ifdef _DEBUG || DEBUG
if (!WriteProcessMemory(hProcess, (LPVOID)_Addr, &_Val, sizeof(_Ty), &_dwWrite))
{
std::cout << "Writing the memory: ";
ColoredMessage("FAIL!", LRED, true);