Advertisement
Guest User

Untitled

a guest
Feb 12th, 2023
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. #include <format>
  4. #include <vector>
  5.  
  6. #define WIN32_LEAN_AND_MEAN
  7. #include <Windows.h>
  8.  
  9. // Read length bytes of memory from location
  10. std::vector<char> readMemory(LPVOID location, SIZE_T length) {
  11.     std::vector<char> buffer(length);
  12.  
  13.     // Before reading the memory, we must enable reads.
  14.     DWORD oldProtection;
  15.     VirtualProtect(location, length, PAGE_EXECUTE_READWRITE, &oldProtection);
  16.     memcpy(buffer.data(), location, length);
  17.  
  18.     // Restore old memory protection.
  19.     VirtualProtect(location, length, oldProtection, &oldProtection);
  20.  
  21.     return buffer;
  22. }
  23.  
  24. int main() {
  25.     // Find address of kernel32
  26.     HMODULE kernel32 = GetModuleHandleA("KERNEL32.DLL");
  27.  
  28.     if (kernel32 == NULL) {
  29.         std::cout << "Failed to locate kernel32.dll\n";
  30.         return 1;
  31.     }
  32.  
  33.     // Get function pointer to QueryPerformanceCounter
  34.     LPVOID codeLocation = GetProcAddress(kernel32, "QueryPerformanceCounter");
  35.  
  36.     if (codeLocation == NULL) {
  37.         std::cout << "Failed to locate QueryPerformanceCounter()\n";
  38.         return 1;
  39.     }
  40.  
  41.     // Start out by reading 16 bytes from QueryPerformanceCounter, and then loop for more memory probes if necessary
  42.     SIZE_T length = 16;
  43.  
  44.     std::cout << "QueryPerformanceCounter() ";
  45.  
  46.     do {
  47.         // Print address
  48.         std::cout << "@ " << std::format("{:016x}", reinterpret_cast<uintptr_t>(codeLocation)) << ":\n";
  49.  
  50.         // Read the memory
  51.         std::vector<char> instructionBytes = readMemory(codeLocation, length);
  52.  
  53.         // Print bytes read from memory
  54.         for (auto ch : instructionBytes)
  55.             std::cout << std::format("{:02x}", static_cast<unsigned>(static_cast<unsigned char>(ch))) << ' ';
  56.  
  57.         std::cout << '\n';
  58.  
  59.         // Ask the user if more memory should be read
  60.         std::cout << "Next read (0 to quit): ";
  61.  
  62.         // Get memory address of next read
  63.         uintptr_t nextPtr;
  64.         std::cin >> std::hex >> nextPtr;
  65.         codeLocation = reinterpret_cast<LPVOID>(nextPtr);
  66.  
  67.         if (codeLocation != 0) {
  68.             // Get size of next read
  69.             std::cout << "Next length: ";
  70.             std::cin >> std::hex >> length;
  71.         }
  72.     } while (codeLocation);
  73.  
  74.     return 0;
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement