Advertisement
Guest User

Untitled

a guest
Feb 12th, 2023
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 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. std::vector<char> readMemory(LPVOID location, SIZE_T length) {
  10.     std::vector<char> buffer(length);
  11.  
  12.     DWORD oldProtection;
  13.     VirtualProtect(location, length, PAGE_EXECUTE_READWRITE, &oldProtection);
  14.     memcpy(buffer.data(), location, length);
  15.     VirtualProtect(location, length, oldProtection, &oldProtection);
  16.  
  17.     return buffer;
  18. }
  19.  
  20. int main() {
  21.     HMODULE kernel32 = GetModuleHandleA("KERNEL32.DLL");
  22.  
  23.     if (kernel32 == NULL) {
  24.         std::cout << "Failed to locate kernel32.dll\n";
  25.         return 1;
  26.     }
  27.  
  28.     LPVOID codeLocation = GetProcAddress(kernel32, "QueryPerformanceCounter");
  29.  
  30.     if (codeLocation == NULL) {
  31.         std::cout << "Failed to locate QueryPerformanceCounter()\n";
  32.         return 1;
  33.     }
  34.  
  35.     SIZE_T length = 16;
  36.  
  37.     std::cout << "QueryPerformanceCounter() ";
  38.  
  39.     do {
  40.         std::cout << "@ " << std::format("{:016x}", reinterpret_cast<uintptr_t>(codeLocation)) << ":\n";
  41.  
  42.         std::vector<char> instructionBytes = readMemory(codeLocation, length);
  43.  
  44.         for (auto ch : instructionBytes)
  45.             std::cout << std::format("{:02x}", static_cast<unsigned>(static_cast<unsigned char>(ch))) << ' ';
  46.  
  47.         std::cout << '\n';
  48.  
  49.         std::cout << "Next read (0 to quit): ";
  50.  
  51.         uintptr_t nextPtr;
  52.         std::cin >> std::hex >> nextPtr;
  53.         codeLocation = reinterpret_cast<LPVOID>(nextPtr);
  54.  
  55.         if (codeLocation != 0) {
  56.             std::cout << "Next length: ";
  57.             std::cin >> std::hex >> length;
  58.         }
  59.     } while (codeLocation);
  60.  
  61.     return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement