iFarbod

CPatch

Aug 28th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. #pragma once
  2. #include <Windows.h>
  3.  
  4. class CPatch
  5. {
  6. public:
  7.     inline static void Patch(void* address, void* data, int size)
  8.     {
  9.         unsigned long protect[2];
  10.         VirtualProtect(address, size, PAGE_EXECUTE_READWRITE, &protect[0]);
  11.         memcpy(address, data, size);
  12.         VirtualProtect(address, size, protect[0], &protect[1]);
  13.     }
  14.  
  15.     inline static void Patch2(int address, void* data, int size)
  16.     {
  17.         unsigned long protect[2];
  18.         VirtualProtect((void *)address, size, PAGE_EXECUTE_READWRITE, &protect[0]);
  19.         memcpy((void *)address, data, size);
  20.         VirtualProtect((void *)address, size, protect[0], &protect[1]);
  21.     }
  22.  
  23.     inline static void Unprotect(int address, int size)
  24.     {
  25.         unsigned long protect[2];
  26.         VirtualProtect((void *)address, size, PAGE_EXECUTE_READWRITE, &protect[0]);
  27.     }
  28.     inline static void Nop(int address, int size)
  29.     {
  30.         unsigned long protect[2];
  31.         VirtualProtect((void *)address, size, PAGE_EXECUTE_READWRITE, &protect[0]);
  32.         memset((void *)address, 0x90, size);
  33.         VirtualProtect((void *)address, size, protect[0], &protect[1]);
  34.     }
  35.     inline static void FillWithZeroes(int address, int size)
  36.     {
  37.         unsigned long protect[2];
  38.         VirtualProtect((void *)address, size, PAGE_EXECUTE_READWRITE, &protect[0]);
  39.         memset((void *)address, 0x00, size);
  40.         VirtualProtect((void *)address, size, protect[0], &protect[1]);
  41.     }
  42.     inline static void RedirectCall(int address, void *func)
  43.     {
  44.         int temp = 0xE8;
  45.         Patch((void *)address, &temp, 1);
  46.         temp = (int)func - ((int)address + 5);
  47.         Patch((void *)((int)address + 1), &temp, 4);
  48.     }
  49.     inline static void RedirectJump(int address, void *func)
  50.     {
  51.         int temp = 0xE9;
  52.         Patch((void *)address, &temp, 1);
  53.         temp = (int)func - ((int)address + 5);
  54.         Patch((void *)((int)address + 1), &temp, 4);
  55.     }
  56.     inline static void SetChar(int address, char value)
  57.     {
  58.         Patch((void *)address, &value, 1);
  59.     }
  60.     inline static void SetUChar(int address, unsigned char value)
  61.     {
  62.         Patch((void *)address, &value, 1);
  63.     }
  64.     inline static void SetShort(int address, short value)
  65.     {
  66.         Patch((void *)address, &value, 2);
  67.     }
  68.     inline static void SetUShort(int address, unsigned short value)
  69.     {
  70.         Patch((void *)address, &value, 2);
  71.     }
  72.     inline static void SetInt(int address, int value)
  73.     {
  74.         Patch((void *)address, &value, 4);
  75.     }
  76.     inline static void SetUInt(int address, unsigned int value)
  77.     {
  78.         Patch((void *)address, &value, 4);
  79.     }
  80.     inline static void SetUIntWithCheck(int address, unsigned int value, unsigned int expectedValue)
  81.     {
  82.         if (*(unsigned int *)address == expectedValue)
  83.         Patch((void *)address, &value, 4);
  84.     }
  85.     inline static void SetFloat(int address, float value)
  86.     {
  87.         Patch((void *)address, &value, 4);
  88.     }
  89.     inline static void SetDouble(int address, double value)
  90.     {
  91.         Patch((void *)address, &value, 8);
  92.     }
  93.     inline static void SetPointer(int address, void *value)
  94.     {
  95.         Patch((void *)address, &value, 4);
  96.     }
  97.  
  98.     inline static void AdjustPointer(int address, void *value, DWORD offset, DWORD end)
  99.     {
  100.         int result;
  101.         if((DWORD)*(DWORD*)address >= offset && (DWORD)*(DWORD*)address <= end) {
  102.         result = (DWORD)value + (DWORD)*(DWORD*)address - (DWORD)offset;
  103.         Patch((void *)address, &result, 4);
  104.         } else {
  105.         address = address + 1;
  106.             if((DWORD)*(DWORD*)address >= offset && (DWORD)*(DWORD*)address <= end) {
  107.             result = (DWORD)value + (DWORD)*(DWORD*)address - (DWORD)offset;
  108.             Patch((void *)address, &result, 4);
  109.             } else {
  110.                 address = address + 1;
  111.                 if((DWORD)*(DWORD*)address >= offset && (DWORD)*(DWORD*)address <= end) {
  112.                 result = (DWORD)value + (DWORD)*(DWORD*)address - (DWORD)offset;
  113.                 Patch((void *)address, &result, 4);
  114.                 } else {
  115.                     address = address + 1;
  116.                     if((DWORD)*(DWORD*)address >= offset && (DWORD)*(DWORD*)address <= end) {
  117.                 result = (DWORD)value + (DWORD)*(DWORD*)address - (DWORD)offset;
  118.                 Patch((void *)address, &result, 4);
  119.                 } else {
  120.                     address = address + 1;
  121.                     if((DWORD)*(DWORD*)address >= offset && (DWORD)*(DWORD*)address <= end) {
  122.                         result = (DWORD)value + (DWORD)*(DWORD*)address - (DWORD)offset;
  123.                         Patch((void *)address, &result, 4);
  124.                     } else {
  125.                         address = address + 1;
  126.                         if((DWORD)*(DWORD*)address >= offset && (DWORD)*(DWORD*)address <= end) {
  127.                             result = (DWORD)value + (DWORD)*(DWORD*)address - (DWORD)offset;
  128.                             Patch((void *)address, &result, 4);
  129.                         }
  130.                     }
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.     }
  136.    
  137.     inline static bool FileExists(const TCHAR *fileName)
  138.     {
  139.     DWORD       fileAttr;
  140.     fileAttr = GetFileAttributes(fileName);
  141.     if (0xFFFFFFFF == fileAttr && GetLastError()==ERROR_FILE_NOT_FOUND)
  142.         return false;
  143.     return true;
  144.     }
  145. };
Advertisement
Add Comment
Please, Sign In to add comment