Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
fix module_base calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
skomski committed May 22, 2015
1 parent 6d84cad commit 3f1609c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
8 changes: 7 additions & 1 deletion hooking/Hooking.Patterns.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "Hooking.h"

#include <sstream>

namespace hook
{
class pattern_match
Expand Down Expand Up @@ -71,7 +73,11 @@ namespace hook
EnsureMatches(expected);
}

assert(m_matches.size() == expected);
if (m_matches.size() != expected) {
std::stringstream message;
message << m_matches.size() << " != " << expected << " " << m_bytes;
MessageBoxA(NULL, message.str().c_str(), "MatchesCountError", MB_OK | MB_ICONEXCLAMATION);
};

return *this;
}
Expand Down
3 changes: 3 additions & 0 deletions hooking/Hooking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace hook
{

uintptr_t baseAddress;

#ifndef _M_AMD64
void inject_hook::inject()
{
Expand Down
33 changes: 6 additions & 27 deletions hooking/Hooking.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@
namespace hook
{
// for link /DYNAMICBASE executables
static ptrdiff_t baseAddressDifference;
extern uintptr_t baseAddress;

// sets the base address difference based on an obtained pointer
inline void set_base(uintptr_t address)
{
#ifdef _M_IX86
uintptr_t addressDiff = (address - 0x400000);
#elif defined(_M_AMD64)
uintptr_t addressDiff = (address - 0x140000000);
#endif

// pointer-style cast to ensure unsigned overflow ends up copied directly into a signed value
baseAddressDifference = *(ptrdiff_t*)&addressDiff;
baseAddress = address;
}

// sets the base to the process main base
Expand All @@ -37,20 +30,6 @@ inline void set_base()
set_base((uintptr_t)GetModuleHandle(NULL));
}

// adjusts the address passed to the base as set above
template<typename T>
inline void adjust_base(T& address)
{
*(uintptr_t*)&address += baseAddressDifference;
}

// returns the adjusted address to the stated base
template<typename T>
inline uintptr_t get_adjusted(T address)
{
return (uintptr_t)address + baseAddressDifference;
}

struct pass
{
template<typename ...T> pass(T...) {}
Expand Down Expand Up @@ -134,9 +113,9 @@ template<typename T>
inline T* getRVA(uintptr_t rva)
{
#ifdef _M_IX86
return (T*)(baseAddressDifference + 0x400000 + rva);
return (T*)(baseAddress + rva);
#elif defined(_M_AMD64)
return (T*)(0x140000000 + rva);
return (T*)(baseAddress + rva);
#endif
}

Expand Down Expand Up @@ -177,9 +156,9 @@ template<typename T, typename TOrdinal>
void iat(const char* moduleName, T function, TOrdinal ordinal)
{
#ifdef _M_IX86
IMAGE_DOS_HEADER* imageHeader = (IMAGE_DOS_HEADER*)(baseAddressDifference + 0x400000);
IMAGE_DOS_HEADER* imageHeader = (IMAGE_DOS_HEADER*)(baseAddress);
#elif defined(_M_AMD64)
IMAGE_DOS_HEADER* imageHeader = (IMAGE_DOS_HEADER*)(baseAddressDifference + 0x140000000);
IMAGE_DOS_HEADER* imageHeader = (IMAGE_DOS_HEADER*)(baseAddress);
#endif
IMAGE_NT_HEADERS* ntHeader = getRVA<IMAGE_NT_HEADERS>(imageHeader->e_lfanew);

Expand Down
2 changes: 2 additions & 0 deletions main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ bool OnViewportInputDebugAlwaysHook(void* thisptr, void* viewport, int input_key
}

DWORD WINAPI InitializeHook(void* arguments) {
hook::set_base();

char* location = hook::pattern("48 8B 05 ? ? ? ? 48 8D 4C 24 ? C6 44 24").count(1).get(0).get<char>(3);
void* global_game = *reinterpret_cast<void**>(location + *(int32_t*)location + 4);

Expand Down

0 comments on commit 3f1609c

Please sign in to comment.