Skip to content

Commit

Permalink
ProcManager Class to edit memory of a process
Browse files Browse the repository at this point in the history
  • Loading branch information
faceslog committed Apr 20, 2021
1 parent 21650d3 commit 5057aa8
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 5 deletions.
8 changes: 5 additions & 3 deletions D3D9Hook/D3D9Hook.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
Expand Down Expand Up @@ -72,12 +72,12 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>C:\Users\hugo_\source\repos\D3D9Hook\imgui;C:\Program Files %28x86%29\Microsoft DirectX SDK %28June 2010%29\Include;C:\Users\hugo_\Documents\Dev\lib\detours-master\detours;$(IncludePath)</IncludePath>
<IncludePath>C:\Users\hugo_\Documents\Dev\d9hook\imgui;C:\Program Files %28x86%29\Microsoft DirectX SDK %28June 2010%29\Include;C:\Users\hugo_\Documents\Dev\lib\detours-master\detours;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files %28x86%29\Microsoft DirectX SDK %28June 2010%29\Lib\x86;C:\Users\hugo_\Documents\Dev\lib\detours-master\Win32\Release;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>C:\Users\hugo_\source\repos\D3D9Hook\imgui;C:\Program Files %28x86%29\Microsoft DirectX SDK %28June 2010%29\Include;C:\Users\hugo_\Documents\Dev\lib\detours-master\detours;$(IncludePath)</IncludePath>
<IncludePath>C:\Users\hugo_\Documents\Dev\d9hook\imgui;C:\Program Files %28x86%29\Microsoft DirectX SDK %28June 2010%29\Include;C:\Users\hugo_\Documents\Dev\lib\detours-master\detours;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files %28x86%29\Microsoft DirectX SDK %28June 2010%29\Lib\x86;C:\Users\hugo_\Documents\Dev\lib\detours-master\Win32\Release;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand Down Expand Up @@ -164,6 +164,7 @@
<ClCompile Include="..\imgui\imgui_tables.cpp" />
<ClCompile Include="..\imgui\imgui_widgets.cpp" />
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="ProcManager.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\imgui\imconfig.h" />
Expand All @@ -178,6 +179,7 @@
<ClInclude Include="..\imgui\imstb_rectpack.h" />
<ClInclude Include="..\imgui\imstb_textedit.h" />
<ClInclude Include="..\imgui\imstb_truetype.h" />
<ClInclude Include="ProcManager.h" />
</ItemGroup>
<ItemGroup>
<None Include="..\imgui\imgui_impl_metal.mm" />
Expand Down
6 changes: 6 additions & 0 deletions D3D9Hook/D3D9Hook.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<ClCompile Include="..\imgui\imgui_tables.cpp">
<Filter>imgui</Filter>
</ClCompile>
<ClCompile Include="ProcManager.cpp">
<Filter>Header Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\imgui\imconfig.h">
Expand Down Expand Up @@ -83,6 +86,9 @@
<ClInclude Include="..\imgui\imgui_stdlib.h">
<Filter>imgui</Filter>
</ClInclude>
<ClInclude Include="ProcManager.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\imgui\imgui_impl_metal.mm">
Expand Down
92 changes: 92 additions & 0 deletions D3D9Hook/ProcManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "ProcManager.h"

// Process ID is a DWORD type in Windows API
DWORD ProcManager::GetProcId(const wchar_t* procName)
{
DWORD procId = 0;
// Snapshot of the processes
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

// When it does not fail
if (hSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(procEntry);

if (Process32First(hSnap, &procEntry))
{
do
{
if (!_wcsicmp(procEntry.szExeFile, procName))
{
procId = procEntry.th32ProcessID;
break;
}

} while (Process32Next(hSnap, &procEntry));
}
}

CloseHandle(hSnap);
return procId;
}

uintptr_t ProcManager::GetModuleBaseAddress(DWORD procId, const wchar_t* modNamme)
{
uintptr_t modBaseAdrr = 0;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, procId);

if (hSnap != INVALID_HANDLE_VALUE)
{
MODULEENTRY32 modEntry;
modEntry.dwSize = sizeof(modEntry);

if (Module32First(hSnap, &modEntry))
{
do
{
if (!_wcsicmp(modEntry.szModule, modNamme))
{
modBaseAdrr = (uintptr_t)modEntry.modBaseAddr;
break;
}

} while (Module32Next(hSnap, &modEntry));
}
}

CloseHandle(hSnap);
return modBaseAdrr;
}

// Find Dynamic Memory Allocation
uintptr_t ProcManager::FindDMAAddy(HANDLE hProc, uintptr_t ptr, std::vector<unsigned int> offsets)
{
uintptr_t addr = ptr;

for (auto const& curr_off : offsets)
{
ReadProcessMemory(hProc, (BYTE*)addr, &addr, sizeof(addr), 0);
addr += curr_off;
}

return addr;
}

ProcManager::ProcManager(const wchar_t* target_process_name)
{
this->procId = GetProcId(target_process_name);
this->moduleBase = GetModuleBaseAddress(procId, target_process_name);
this->hProcess = OpenProcess(PROCESS_ALL_ACCESS, NULL, procId);
}

uintptr_t ProcManager::GetDynamicBaseAddress(const unsigned int& relative_offset)
{
return moduleBase + relative_offset;
}

uintptr_t ProcManager::GetResolvedPointerChain(const unsigned int& relative_offset, std::vector<unsigned int> offsets)
{
return FindDMAAddy(hProcess, GetDynamicBaseAddress(relative_offset), offsets);
}

56 changes: 56 additions & 0 deletions D3D9Hook/ProcManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <vector>
#include <Windows.h>
#include <TlHelp32.h>

class ProcManager
{

public:

static DWORD GetProcId(const wchar_t* procName);
static uintptr_t GetModuleBaseAddress(DWORD procID, const wchar_t* modNamme);
static uintptr_t FindDMAAddy(HANDLE hProc, uintptr_t ptr, std::vector<unsigned int> offsets);

ProcManager(const wchar_t* target_process_name);
// Resolve base address of the pointer chain
uintptr_t GetDynamicBaseAddress(const unsigned int& relative_offset);
// Resolve our pointer chain from offsets
uintptr_t GetResolvedPointerChain(const unsigned int& relative_offset, std::vector<unsigned int> offsets);

// Read a Process Memory value
template<typename T>
void ReadValueFromMemory(T& value, uintptr_t address)
{
ReadProcessMemory(hProcess, (BYTE*)address, &value, sizeof(value), nullptr);
}

// Read a Process Memory value Redefinition using offsets is gonna call Get Resolved Pointer Chain
template<typename T>
void ReadValueFromMemory(T& value, const unsigned int& relative_offset, std::vector<unsigned int> offsets)
{
uintptr_t address = GetResolvedPointerChain(relative_offset, offsets);
ReadProcessMemory(hProcess, (BYTE*)address, &value, sizeof(value), nullptr);
}

// Write to a Process Memory Address
template<typename T>
void WriteValueToMemory(T& value, uintptr_t address)
{
WriteProcessMemory(hProcess, (BYTE*)address, &value, sizeof(value), nullptr);
}

// Write to a Process Memory Address Redefinition using offsets is gonna call Get Resolved Pointer Chain
template<typename T>
void WriteValueToMemory(T& value, const unsigned int& relative_offset, std::vector<unsigned int> offsets)
{
uintptr_t address = GetResolvedPointerChain(relative_offset, offsets);
WriteProcessMemory(hProcess, (BYTE*)address, &value, sizeof(value), nullptr);
}

private:
DWORD procId;
HANDLE hProcess;
uintptr_t moduleBase;
};
4 changes: 2 additions & 2 deletions D3D9Hook/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <d3d9.h>
#include <d3dx9.h>

#include "ProcManager.h"

typedef HRESULT(_stdcall* EndScene)(LPDIRECT3DDEVICE9 pDevice);
HRESULT _stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice);
EndScene oEndScene;
Expand Down Expand Up @@ -128,9 +130,7 @@ void DrawMenu()
ImGui::NewFrame();

ImGui::Begin("Faces Menu", &isMenuToggled);

// Draw the checkboxes for the cheat
ImGui::Text("Hello, World %d", 2106);
ImGui::Checkbox("Chams", &isChamsToggled);

ImGui::Render();
Expand Down

0 comments on commit 5057aa8

Please sign in to comment.