Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module hexext.system.memutils;
- import core.sys.windows.windows : DWORD, HMODULE, VirtualProtect, LoadLibraryA, GetModuleHandleA, PAGE_READWRITE, PAGE_EXECUTE, GetProcAddress;
- import core.sys.windows.dll;
- static immutable size_t MODULE_NOT_FOUND = cast(size_t)(cast(ptrdiff_t)-1);
- static immutable ubyte push_imm32 = 0x68;
- static immutable ubyte retn = 0xC3;
- //the max number of bytes that the make_page_writable
- //and make_page_executable functions can affect
- static immutable size_t max_page_mod = 1024;
- void make_page_writable(void* sub)
- {
- assert( sub != null );
- DWORD oldprotect;
- VirtualProtect(sub, max_page_mod, PAGE_READWRITE, &oldprotect);
- }
- void make_page_executable(void* sub)
- {
- assert(sub != null);
- DWORD oldprotect;
- VirtualProtect(sub, max_page_mod, PAGE_EXECUTE, &oldprotect);
- }
- //returns the size of the change made to location
- static size_t push_immediate_at_location(void *location, const size_t imm)
- {
- *(cast(ubyte*)location) = push_imm32;
- *cast(size_t*)((cast(size_t)location)+1) = imm;
- return ubyte.sizeof + size_t.sizeof;
- }
- //returns the size of the change made to location
- static size_t insert_retn_at_location(void *location)
- {
- *(cast(ubyte*)location) = retn;
- return ubyte.sizeof;
- }
- //address gets pushed onto stack, then immediately afterwards
- //it is popped off the stack into the pc
- void make_jmp(void *hookAddress, void* patchAddress)
- {
- assert(hookAddress && patchAddress);
- make_page_writable(patchAddress);
- const size_t pushSize = push_immediate_at_location( patchAddress, cast(size_t)hookAddress );
- const size_t returnOffset = (cast(size_t)patchAddress) + pushSize;
- insert_retn_at_location(returnOffset);
- make_page_executable(patchAddress);
- }
- size_t get_module_base(const char *moduleName)
- {
- assert(moduleName);
- const size_t base = cast(size_t)( GetModuleHandleA(moduleName) );
- return base != null ? base : MODULE_NOT_FOUND;
- }
- //pretty much just a wrapper for LoadLibrary
- bool load_dynamic_library(const char* libName)
- {
- assert(libName);
- return LoadLibraryA(libName) != null;
- }
- bool is_module_loaded(const char * moduleName)
- {
- assert(module_name);
- return get_module_base(moduleName) != MODULE_NOT_FOUND;
- }
- void* get_module_export(const char* module_name, const char* exportSymbol)
- {
- assert(module_name && exportSymbol);
- const HMODULE handle = GetModuleHandleA(module_name);
- return handle != null ? GetProcAddress(handle, exportSymbol) : null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement