mirror of
https://github.com/GameHackingBook/GameHackingCode
synced 2026-06-08 11:08:54 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{4C3B87D1-43D3-44DB-913E-E4A1D99909CB}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>Chapter6_AccessingMemory</RootNamespace>
|
||||
<ProjectName>Chapter6_AccessingMemory</ProjectName>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<IntDir>$(SolutionDir)\BuildTemp\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<IntDir>$(SolutionDir)\BuildTemp\$(ProjectName)_$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<StructMemberAlignment>4Bytes</StructMemberAlignment>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<RandomizedBaseAddress>false</RandomizedBaseAddress>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<StructMemberAlignment>4Bytes</StructMemberAlignment>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<FixedBaseAddress>
|
||||
</FixedBaseAddress>
|
||||
<RandomizedBaseAddress>false</RandomizedBaseAddress>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main-accessingMemory.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,169 @@
|
||||
#include <iostream>
|
||||
#include <windows.h>
|
||||
#include <tlhelp32.h>
|
||||
|
||||
|
||||
void printMyPid()
|
||||
{
|
||||
wchar_t myTitle[1024];
|
||||
GetConsoleTitle(&myTitle[0], 1024);
|
||||
|
||||
HWND myWindow = FindWindow(NULL, myTitle);
|
||||
|
||||
DWORD pid;
|
||||
GetWindowThreadProcessId(myWindow, &pid);
|
||||
|
||||
printf("My pid is %d\n", pid);
|
||||
}
|
||||
|
||||
void printExplorerPid()
|
||||
{
|
||||
PROCESSENTRY32 entry;
|
||||
entry.dwSize = sizeof(PROCESSENTRY32);
|
||||
|
||||
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
|
||||
|
||||
if (Process32First(snapshot, &entry) == TRUE)
|
||||
{
|
||||
while (Process32Next(snapshot, &entry) == TRUE)
|
||||
{
|
||||
std::wstring binaryPath = entry.szExeFile;
|
||||
if (binaryPath.find(L"explorer.exe") != std::wstring::npos)
|
||||
{
|
||||
printf("Explorer's pid is %d\n", entry.th32ProcessID);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CloseHandle(snapshot);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T readMemoryAPI(HANDLE process, LPVOID address)
|
||||
{
|
||||
T value;
|
||||
ReadProcessMemory(process, address, &value, sizeof(T), NULL);
|
||||
return value;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void writeMemoryAPI(HANDLE process, LPVOID address, T value)
|
||||
{
|
||||
WriteProcessMemory(process, address, &value, sizeof(T), NULL);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
DWORD protectMemory(HANDLE process, LPVOID address, DWORD prot)
|
||||
{
|
||||
DWORD oldProt;
|
||||
VirtualProtectEx(process, address, sizeof(T), prot, &oldProt);
|
||||
return oldProt;
|
||||
}
|
||||
|
||||
void readAndWriteMemoryAPI(HANDLE process, LPVOID address)
|
||||
{
|
||||
DWORD value = readMemoryAPI<DWORD>(process, address);
|
||||
|
||||
printf("Current mem value is %d\n", value);
|
||||
value++;
|
||||
|
||||
DWORD oldProt = protectMemory<DWORD>(process, address, PAGE_READWRITE);
|
||||
writeMemoryAPI<DWORD>(process, address, value);
|
||||
protectMemory<DWORD>(process, address, oldProt);
|
||||
|
||||
|
||||
value = readMemoryAPI<DWORD>(process, address);
|
||||
|
||||
printf("New mem value is %d\n", value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T readMemoryPointer(LPVOID address)
|
||||
{
|
||||
return *((T*)address);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void writeMemoryPointer(LPVOID address, T value)
|
||||
{
|
||||
*((T*)address) = value;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T* pointMemory(LPVOID address)
|
||||
{
|
||||
return ((T*)address);
|
||||
}
|
||||
void readAndWriteMemoryMarshall(LPVOID address)
|
||||
{
|
||||
DWORD value = readMemoryPointer<DWORD>(address);
|
||||
|
||||
printf("Current mem value is %d\n", value);
|
||||
value++;
|
||||
|
||||
writeMemoryPointer<DWORD>(address, value);
|
||||
value = readMemoryPointer<DWORD>(address);
|
||||
|
||||
printf("New mem value is %d\n", value);
|
||||
}
|
||||
|
||||
|
||||
DWORD getMyBaseAddressGMH()
|
||||
{
|
||||
return (DWORD)GetModuleHandle(NULL);
|
||||
}
|
||||
|
||||
DWORD getMyBaseAddressFS()
|
||||
{
|
||||
DWORD newBase;
|
||||
__asm
|
||||
{
|
||||
MOV EAX, DWORD PTR FS:[0x30]
|
||||
MOV EAX, DWORD PTR DS:[EAX+0x8]
|
||||
MOV newBase, EAX
|
||||
}
|
||||
return newBase;
|
||||
}
|
||||
|
||||
DWORD getMyBaseRemoteGMH(HANDLE Process)
|
||||
{
|
||||
LPVOID TIB;
|
||||
__asm
|
||||
{
|
||||
MOV EAX, DWORD PTR FS:[0x18]
|
||||
ADD EAX, 0x30
|
||||
MOV TIB, EAX
|
||||
}
|
||||
// read 0x30 bytes past _the_game's_ TIB to get the PEB
|
||||
DWORD PEB = readMemoryAPI<DWORD>(Process, TIB);
|
||||
// read 0x8 bytes past _the_game's_ PEB to get the base
|
||||
return readMemoryAPI<DWORD>(Process, (LPVOID)(PEB + 0x08));
|
||||
}
|
||||
|
||||
void printMyBaseAddresses(HANDLE Process)
|
||||
{
|
||||
DWORD base1 = getMyBaseAddressGMH();
|
||||
DWORD base2 = getMyBaseAddressFS();
|
||||
DWORD base3 = getMyBaseRemoteGMH(Process);
|
||||
if (base1 != base2 || base2 != base3)
|
||||
printf("Woah, this should be impossible!\n");
|
||||
else
|
||||
printf("My base address is 0x%08x\n", base1);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
|
||||
|
||||
printMyBaseAddresses(proc);
|
||||
printMyPid();
|
||||
printExplorerPid();
|
||||
|
||||
// lets do some memory stuff.. to ourself
|
||||
DWORD someValue = 1234;
|
||||
readAndWriteMemoryAPI(proc, &someValue);
|
||||
readAndWriteMemoryMarshall(&someValue);
|
||||
|
||||
system("pause");
|
||||
}
|
||||
Reference in New Issue
Block a user