Advertisement
Guest User

Untitled

a guest
Jan 20th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.46 KB | None | 0 0
  1. module hexext.system.memutils;
  2.  
  3. import core.sys.windows.windows : DWORD, HMODULE, VirtualProtect, LoadLibraryA, GetModuleHandleA, PAGE_READWRITE, PAGE_EXECUTE, GetProcAddress;
  4. import core.sys.windows.dll;
  5.  
  6. static immutable size_t MODULE_NOT_FOUND    =   cast(size_t)(cast(ptrdiff_t)-1);
  7. static immutable ubyte push_imm32           =   0x68;
  8. static immutable ubyte retn                 =   0xC3;
  9.  
  10. //the max number of bytes that the make_page_writable
  11. //and make_page_executable functions can affect
  12. static immutable size_t max_page_mod    =   1024;
  13.  
  14. void make_page_writable(void* sub)
  15. {
  16.     assert( sub != null );
  17.     DWORD oldprotect;
  18.     VirtualProtect(sub, max_page_mod, PAGE_READWRITE, &oldprotect);
  19. }
  20.  
  21. void make_page_executable(void* sub)
  22. {
  23.     assert(sub != null);
  24.     DWORD oldprotect;
  25.     VirtualProtect(sub, max_page_mod, PAGE_EXECUTE, &oldprotect);
  26. }
  27.  
  28. //returns the size of the change made to location
  29. static size_t push_immediate_at_location(void *location, const size_t imm)
  30. {
  31.     *(cast(ubyte*)location)                     =   push_imm32;
  32.     *cast(size_t*)((cast(size_t)location)+1)    =   imm;
  33.     return ubyte.sizeof + size_t.sizeof;
  34. }
  35.  
  36. //returns the size of the change made to location
  37. static size_t insert_retn_at_location(void *location)
  38. {
  39.     *(cast(ubyte*)location) = retn;
  40.     return ubyte.sizeof;
  41. }
  42.  
  43. //address gets pushed onto stack, then immediately afterwards
  44. //it is popped off the stack into the pc
  45.  
  46. void make_jmp(void *hookAddress, void* patchAddress)
  47. {
  48.     assert(hookAddress && patchAddress);
  49.     make_page_writable(patchAddress);
  50.    
  51.     const size_t pushSize       =   push_immediate_at_location( patchAddress, cast(size_t)hookAddress   );
  52.     const size_t returnOffset   =   (cast(size_t)patchAddress) + pushSize;
  53.    
  54.     insert_retn_at_location(returnOffset);
  55.     make_page_executable(patchAddress);
  56. }
  57.  
  58. size_t get_module_base(const char *moduleName)
  59. {
  60.     assert(moduleName);
  61.  
  62.     const size_t base =  cast(size_t)(  GetModuleHandleA(moduleName)    );
  63.     return base != null ? base : MODULE_NOT_FOUND;
  64. }
  65.  
  66. //pretty much just a wrapper for LoadLibrary
  67. bool load_dynamic_library(const char* libName)
  68. {
  69.     assert(libName);
  70.     return LoadLibraryA(libName) != null;
  71. }
  72.  
  73. bool is_module_loaded(const char * moduleName)
  74. {
  75.     assert(module_name);
  76.     return get_module_base(moduleName) != MODULE_NOT_FOUND;
  77. }
  78.  
  79. void* get_module_export(const char* module_name, const char* exportSymbol)
  80. {
  81.     assert(module_name && exportSymbol);
  82.     const HMODULE handle = GetModuleHandleA(module_name);
  83.  
  84.     return handle != null ? GetProcAddress(handle, exportSymbol) : null;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement