Skip to content

Commit

Permalink
Better Memory::absolute_address
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolf49406 committed Jan 12, 2025
1 parent c028e3f commit 7d2743f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
6 changes: 5 additions & 1 deletion Dota2Patcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ int main() {

printf("[+] Modules loaded: %d\n", (int)Memory::loaded_modules.size());


//CreateInterface iface;
//iface.load_interfaces("client.dll", "Source2Client002");

// FIND GAMERULES

if (!CDOTAGamerules::find_gamerules()) {
Expand Down Expand Up @@ -122,7 +126,7 @@ int main() {
continue;
}

printf("[+] \"%s\" patch addr: %p\n", patch.name.c_str(), (void*)patch_addr.value());
printf("[+] \"%s\" patch addr -> [%p]\n", patch.name.c_str(), (void*)patch_addr.value());

if (!Memory::patch(patch_addr.value() + patch.offset, patch.patch_bytes)) {
printf("[-] Failed to patch \"%s\"!\n", patch.name.c_str());
Expand Down
30 changes: 24 additions & 6 deletions Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

class Memory {
public:
enum class ASMType {
LEA, // 3, 7
CALL // 2, 6
};

struct ModuleInfo {
uintptr_t start_address;
uintptr_t end_address;
Expand All @@ -22,24 +27,37 @@ class Memory {
static bool patch(const uintptr_t patch_addr, const std::string& replace_str);

template<typename T, typename N>
static std::optional<T> absolute_address(N instruction_ptr, ptrdiff_t offset, std::optional<uint32_t> size) {
static std::optional<T> absolute_address(N instruction_ptr, ASMType instr_type = ASMType::LEA) {
uintptr_t address = 0;

if constexpr (std::is_pointer_v<N>) {
if constexpr (std::is_pointer_v<N>)
address = reinterpret_cast<uintptr_t>(instruction_ptr);
}
else if constexpr (std::is_integral_v<N>) {
else if constexpr (std::is_integral_v<N>)
address = static_cast<uintptr_t>(instruction_ptr);

ptrdiff_t offset = 0;
uint32_t size = 0;
switch (instr_type) {
case ASMType::LEA:
offset = 3;
size = 7;
break;
case ASMType::CALL:
offset = 2;
size = 6;
break;
default:
printf("[-] (absolute_address) Unsupported instruction type\n");
return std::nullopt;
}

int32_t relative_offset = 0;

if (!ReadProcessMemory(ProcessHandle::get_handle(), reinterpret_cast<LPCVOID>(address + offset), &relative_offset, sizeof(relative_offset), nullptr)) {
printf("[-] (absolute_address) ReadProcessMemory failed: 0x%d\n", GetLastError());
return std::nullopt;
}

uintptr_t absolute_address = address + relative_offset + size.value_or(offset + sizeof(int32_t));
uintptr_t absolute_address = address + relative_offset + size;

return absolute_address;
}
Expand Down
4 changes: 2 additions & 2 deletions SourceSDK/CDOTACamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class CDOTACamera {
if (!base)
return false;

const auto camera_base_address = Memory::absolute_address<uintptr_t>(base.value(), 3, 7);
const auto camera_base_address = Memory::absolute_address<uintptr_t>(base.value());
if (!camera_base_address)
return false;

printf("[+] CDOTA_Camera: %p\n", reinterpret_cast<void*>(camera_base_address.value()));
printf("[+] CDOTA_Camera -> [%p]\n", reinterpret_cast<void*>(camera_base_address.value()));
camera_base_ = camera_base_address.value();
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions SourceSDK/CDOTAGamerules.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CDOTAGamerules {
}

gamerules_base_ = gamerules_ptr.value();
printf("[+] C_DOTAGamerules: %p\n", reinterpret_cast<void*>(gamerules_base_));
printf("[+] C_DOTAGamerules -> [%p]\n", reinterpret_cast<void*>(gamerules_base_));
return true;
}

Expand All @@ -49,13 +49,13 @@ class CDOTAGamerules {
return false;
}

const auto gamerules_proxy = Memory::absolute_address<uintptr_t>(base.value(), 3, 7);
const auto gamerules_proxy = Memory::absolute_address<uintptr_t>(base.value());
if (gamerules_proxy.value_or(0) == 0) {
return false;
}

// Not really C_DOTAGamerules_Proxy but who cares
printf("[+] C_DOTAGamerules_Proxy: %p\n", reinterpret_cast<void*>(gamerules_proxy.value()));
printf("[+] C_DOTAGamerules_Proxy -> [%p]\n", reinterpret_cast<void*>(gamerules_proxy.value()));
gamerules_proxy_ = gamerules_proxy.value();

return true;
Expand Down
22 changes: 11 additions & 11 deletions SourceSDK/CreateInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Interfase {
if (!base_ptr)
return std::nullopt;

const auto absolute_address = Memory::absolute_address<uintptr_t>(base_ptr.value(), 3, 7);
const auto absolute_address = Memory::absolute_address<uintptr_t>(base_ptr.value());
if (!absolute_address)
return std::nullopt;

Expand Down Expand Up @@ -46,15 +46,15 @@ class CreateInterface : public Interfase {
return false;
}

const auto CreateInterfacePtr = Memory::absolute_address<uintptr_t>(CreateInterface_base.value(), 3, 7);
const auto CreateInterfacePtr = Memory::absolute_address<uintptr_t>(CreateInterface_base.value());
if (!CreateInterfacePtr)
return false;

const auto CreateInterfaceFn = Memory::read_memory<uintptr_t>(CreateInterfacePtr.value());
if (!CreateInterfaceFn)
return false;

printf("[+] CreateInterface for %s: %p\n", module_name.c_str(), (void*)CreateInterfaceFn.value());
printf("[+] %s CreateInterface -> [%p]\n", module_name.c_str(), (void*)CreateInterfaceFn.value());

pCreateInterface_ = (Interfase*)CreateInterfaceFn.value();
return true;
Expand All @@ -70,22 +70,22 @@ class CreateInterface : public Interfase {

while (true) {
const auto name = iface->name();
if (!name)
return;
if (!name) continue;

std::cout << name.value() << std::endl;
const auto base = iface->base();
if (!base) continue;

printf("[~] %s -> [%p]\n", name.value().c_str(), (void*)base.value());

if (name.value() == interface_name) {
const auto base = iface->base();
if (!base)
break;
if (!base) break;

printf("[+] Interface %s found: %p\n", interface_name.c_str(), (void*)base.value());
printf("[+] Interface %s found -> [%p]\n", interface_name.c_str(), (void*)base.value());
}

auto next_ptr = iface->next();
if (!next_ptr)
break;
if (!next_ptr) break;

iface = next_ptr.value();
}
Expand Down

0 comments on commit 7d2743f

Please sign in to comment.