self_modify.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. int allow_self_modification()
  12. {
  13. int ReturnValue = 0;
  14. HMODULE OurModule = GetModuleHandle(0);
  15. BYTE *pBaseOfImage = 0;
  16. if ( (GetVersion() & 0xC0000000) == 0x80000000)
  17. {
  18. // We're on Win32s, so get the real pointer
  19. HMODULE Win32sKernel = GetModuleHandle("W32SKRNL.DLL");
  20. typedef DWORD __stdcall translator(DWORD);
  21. translator *pImteFromHModule =
  22. (translator *) GetProcAddress(Win32sKernel, "_ImteFromHModule@4");
  23. translator *pBaseAddrFromImte =
  24. (translator *) GetProcAddress(Win32sKernel, "_BaseAddrFromImte@4");
  25. if (pImteFromHModule && pBaseAddrFromImte)
  26. {
  27. DWORD Imte = (*pImteFromHModule) ( (DWORD) OurModule);
  28. pBaseOfImage = (BYTE *) (*pBaseAddrFromImte) (Imte);
  29. }
  30. }
  31. else
  32. {
  33. pBaseOfImage = (BYTE *) OurModule;
  34. }
  35. if (pBaseOfImage)
  36. {
  37. IMAGE_OPTIONAL_HEADER *pHeader = (IMAGE_OPTIONAL_HEADER *)
  38. (pBaseOfImage + ( (IMAGE_DOS_HEADER *) pBaseOfImage)->e_lfanew +
  39. sizeof (IMAGE_NT_SIGNATURE) + sizeof (IMAGE_FILE_HEADER));
  40. DWORD OldRights;
  41. if (VirtualProtect(pBaseOfImage + pHeader->BaseOfCode, pHeader->SizeOfCode,
  42. PAGE_READWRITE, &OldRights))
  43. {
  44. ReturnValue = 1;
  45. }
  46. }
  47. return ReturnValue;
  48. }