kiwidog

Untitled

May 1st, 2015
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. // pAddress = address where we want to place our hook
  2. // dwJumpTo = address where we want to jump to
  3. // dwLen    = length of the bytes to overwrite
  4.  
  5. void MakeJMP(BYTE *pAddress, DWORD dwJumpTo, DWORD dwLen)
  6. {
  7.     DWORD dwOldProtect, dwBkup, dwRelAddr;
  8.  
  9.     // give the paged memory read/write permissions
  10.    
  11.     VirtualProtect(pAddress, dwLen, PAGE_EXECUTE_READWRITE, &dwOldProtect);
  12.    
  13.     // calculate the distance between our address and our target location
  14.     // and subtract the 5bytes, which is the size of the jmp
  15.     // (0xE9 0xAA 0xBB 0xCC 0xDD) = 5 bytes
  16.  
  17.     dwRelAddr = (DWORD) (dwJumpTo - (DWORD) pAddress) - 5;
  18.  
  19.     // overwrite the byte at pAddress with the jmp opcode (0xE9)
  20.    
  21.     *pAddress = 0xE9;
  22.    
  23.     // overwrite the next 4 bytes (which is the size of a DWORD)
  24.     // with the dwRelAddr
  25.    
  26.     *((DWORD *)(pAddress + 0x1)) = dwRelAddr;
  27.  
  28.     // overwrite the remaining bytes with the NOP opcode (0x90)
  29.     // NOP opcode = No OPeration
  30.    
  31.     for(DWORD x = 0x5; x < dwLen; x++) *(pAddress + x) = 0x90;
  32.  
  33.     // restore the paged memory permissions saved in dwOldProtect
  34.    
  35.     VirtualProtect(pAddress, dwLen, dwOldProtect, &dwBkup);
  36.    
  37.     return;
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment