Jump to content

Fix Memory Break


Recommended Posts

  • Bronze

Userinterface.cpp

1, put it to the beginning

PVOID* find(const char *szFunc, HMODULE hModule)
{
	if (!hModule)
		hModule = GetModuleHandle(0);

	PIMAGE_DOS_HEADER img_dos_headers = (PIMAGE_DOS_HEADER)hModule;

	PIMAGE_NT_HEADERS img_nt_headers = (PIMAGE_NT_HEADERS)((byte*)img_dos_headers + img_dos_headers->e_lfanew);

	PIMAGE_IMPORT_DESCRIPTOR img_import_desc = (PIMAGE_IMPORT_DESCRIPTOR)((byte*)img_dos_headers + img_nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);

	if (img_dos_headers->e_magic != IMAGE_DOS_SIGNATURE)
		printf("e_magic dos sig\n");

	for (IMAGE_IMPORT_DESCRIPTOR *iid = img_import_desc; iid->Name != 0; iid++)
	{
		for (int func_idx = 0; *(func_idx + (void**)(iid->FirstThunk + (size_t)hModule)) != nullptr; func_idx++)
		{
			char* mod_func_name = (char*)(*(func_idx + (size_t*)(iid->OriginalFirstThunk + (size_t)hModule)) + (size_t)hModule + 2);

			const intptr_t nmod_func_name = (intptr_t)mod_func_name;

			if (nmod_func_name >= 0)
			{
				if (!::strcmp(szFunc, mod_func_name))
					return func_idx + (void**)(iid->FirstThunk + (size_t)hModule);
			}
		}
	}
	return 0;
}

std::uint32_t detour_ptr(const char *szFunc, PVOID newfunction, HMODULE module)
{
	void **&&func_ptr = find(szFunc, module);

	if (*func_ptr == newfunction || *func_ptr == nullptr)
		return 0;

	DWORD old_rights;
	DWORD new_rights = PAGE_READWRITE;

	VirtualProtect(func_ptr, sizeof (uintptr_t), new_rights, &old_rights);

	uintptr_t ret = (uintptr_t)*func_ptr;

	*func_ptr = newfunction;

	VirtualProtect(func_ptr, sizeof (uintptr_t), old_rights, &new_rights);

	return ret;
}

2, include

#include <windows.h>
#include <cstdint>

3, WriteProcessMemory prototype, hook

using WriteProcessMemoryFn = BOOL(__stdcall*)(HANDLE, LPVOID, LPCVOID, SIZE_T, SIZE_T*);
WriteProcessMemoryFn oWriteProcessMemory;
BOOL __stdcall hkWriteProcessMemory(HANDLE hProcess, LPVOID lpBaseAddress, LPCVOID lpBuffer, SIZE_T nSize, SIZE_T *lpNumberOfBytesWritten)
{
	return oWriteProcessMemory(nullptr, lpBaseAddress, lpBuffer, nSize, lpNumberOfBytesWritten);
}

4, search-->  bool Main(HINSTANCE hInstance, LPSTR lpCmdLine)  and  put it to the beginning

    oWriteProcessMemory = (WriteProcessMemoryFn)detour_ptr("WriteProcessMemory", (PVOID)hkWriteProcessMemory, GetModuleHandleA("Kernel32.dll"));

 

 

This not only protects the MemoryBreak, but also all the cheats that WriteProcessMemory changes the game.

Now he's hooked, so if something invites WriteProcessMemory to get a nullpt to the handle.

 

 Original post

  • Metin2 Dev 1
  • Good 3
  • Love 10
Link to comment
Share on other sites

vor 2 Stunden schrieb devdeto:

Userinterface.cpp

1, put it to the beginning


PVOID* find(const char *szFunc, HMODULE hModule)
{
	if (!hModule)
		hModule = GetModuleHandle(0);

	PIMAGE_DOS_HEADER img_dos_headers = (PIMAGE_DOS_HEADER)hModule;

	PIMAGE_NT_HEADERS img_nt_headers = (PIMAGE_NT_HEADERS)((byte*)img_dos_headers + img_dos_headers->e_lfanew);

	PIMAGE_IMPORT_DESCRIPTOR img_import_desc = (PIMAGE_IMPORT_DESCRIPTOR)((byte*)img_dos_headers + img_nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);

	if (img_dos_headers->e_magic != IMAGE_DOS_SIGNATURE)
		printf("e_magic dos sig\n");

	for (IMAGE_IMPORT_DESCRIPTOR *iid = img_import_desc; iid->Name != 0; iid++)
	{
		for (int func_idx = 0; *(func_idx + (void**)(iid->FirstThunk + (size_t)hModule)) != nullptr; func_idx++)
		{
			char* mod_func_name = (char*)(*(func_idx + (size_t*)(iid->OriginalFirstThunk + (size_t)hModule)) + (size_t)hModule + 2);

			const intptr_t nmod_func_name = (intptr_t)mod_func_name;

			if (nmod_func_name >= 0)
			{
				if (!::strcmp(szFunc, mod_func_name))
					return func_idx + (void**)(iid->FirstThunk + (size_t)hModule);
			}
		}
	}
	return 0;
}

std::uint32_t detour_ptr(const char *szFunc, PVOID newfunction, HMODULE module)
{
	void **&&func_ptr = find(szFunc, module);

	if (*func_ptr == newfunction || *func_ptr == nullptr)
		return 0;

	DWORD old_rights;
	DWORD new_rights = PAGE_READWRITE;

	VirtualProtect(func_ptr, sizeof (uintptr_t), new_rights, &old_rights);

	uintptr_t ret = (uintptr_t)*func_ptr;

	*func_ptr = newfunction;

	VirtualProtect(func_ptr, sizeof (uintptr_t), old_rights, &new_rights);

	return ret;
}

2, include


#include <windows.h>
#include <cstdint>

3, WriteProcessMemory prototype, hook


using WriteProcessMemoryFn = BOOL(__stdcall*)(HANDLE, LPVOID, LPCVOID, SIZE_T, SIZE_T*);
WriteProcessMemoryFn oWriteProcessMemory;
BOOL __stdcall hkWriteProcessMemory(HANDLE hProcess, LPVOID lpBaseAddress, LPCVOID lpBuffer, SIZE_T nSize, SIZE_T *lpNumberOfBytesWritten)
{
	return oWriteProcessMemory(nullptr, lpBaseAddress, lpBuffer, nSize, lpNumberOfBytesWritten);
}

4, search-->  bool Main(HINSTANCE hInstance, LPSTR lpCmdLine)  and  put it to the beginning


    oWriteProcessMemory = (WriteProcessMemoryFn)detour_ptr("WriteProcessMemory", (PVOID)hkWriteProcessMemory, GetModuleHandleA("Kernel32.dll"));

 

 

This not only protects the MemoryBreak, but also all the cheats that WriteProcessMemory changes the game.

Now he's hooked, so if something invites WriteProcessMemory to get a nullpt to the handle.

 

 Original post

 

thank you for your release!

  • Love 2
Link to comment
Share on other sites

  • Premium
6 hours ago, Vlad Ionut said:

This it’s worst tutorial ever!

That makes you a kid, just by saying that. You'll have to read his information and deduct what he's saying.

 

Ontopic: tested on a live server, crashes the client and makes it unresponding, it even loads for good at the loading phase.

 

 

Link to comment
Share on other sites

  • 3 years later...

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.