win32_self_modify.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. #include <windows.h>
  9. #include <process.h>
  10. #include "error/error.hh"
  11. #include "init/init.hh"
  12. class r1_self_modify_class : public i4_init_class
  13. {
  14. public:
  15. void init()
  16. {
  17. HMODULE OurModule = GetModuleHandle(0);
  18. BYTE *pBaseOfImage = 0;
  19. if ( (GetVersion() & 0xC0000000) == 0x80000000)
  20. {
  21. // We're on Win32s, so get the real pointer
  22. HMODULE Win32sKernel = GetModuleHandle("W32SKRNL.DLL");
  23. typedef DWORD __stdcall translator(DWORD);
  24. translator *pImteFromHModule =
  25. (translator *) GetProcAddress(Win32sKernel, "_ImteFromHModule@4");
  26. translator *pBaseAddrFromImte =
  27. (translator *) GetProcAddress(Win32sKernel, "_BaseAddrFromImte@4");
  28. if (pImteFromHModule && pBaseAddrFromImte)
  29. {
  30. DWORD Imte = (*pImteFromHModule) ( (DWORD) OurModule);
  31. pBaseOfImage = (BYTE *) (*pBaseAddrFromImte) (Imte);
  32. }
  33. }
  34. else
  35. pBaseOfImage = (BYTE *) OurModule;
  36. if (pBaseOfImage)
  37. {
  38. IMAGE_OPTIONAL_HEADER *pHeader = (IMAGE_OPTIONAL_HEADER *)
  39. (pBaseOfImage + ( (IMAGE_DOS_HEADER *) pBaseOfImage)->e_lfanew +
  40. sizeof (IMAGE_NT_SIGNATURE) + sizeof (IMAGE_FILE_HEADER));
  41. DWORD OldRights;
  42. VirtualProtect(pBaseOfImage + pHeader->BaseOfCode, pHeader->SizeOfCode,
  43. PAGE_READWRITE, &OldRights);
  44. }
  45. }
  46. } r1_self_modify_instance;