Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- #include <format>
- #include <vector>
- #define WIN32_LEAN_AND_MEAN
- #include <Windows.h>
- // Read length bytes of memory from location
- std::vector<char> readMemory(LPVOID location, SIZE_T length) {
- std::vector<char> buffer(length);
- // Before reading the memory, we must enable reads.
- DWORD oldProtection;
- VirtualProtect(location, length, PAGE_EXECUTE_READWRITE, &oldProtection);
- memcpy(buffer.data(), location, length);
- // Restore old memory protection.
- VirtualProtect(location, length, oldProtection, &oldProtection);
- return buffer;
- }
- int main() {
- // Find address of kernel32
- HMODULE kernel32 = GetModuleHandleA("KERNEL32.DLL");
- if (kernel32 == NULL) {
- std::cout << "Failed to locate kernel32.dll\n";
- return 1;
- }
- // Get function pointer to QueryPerformanceCounter
- LPVOID codeLocation = GetProcAddress(kernel32, "QueryPerformanceCounter");
- if (codeLocation == NULL) {
- std::cout << "Failed to locate QueryPerformanceCounter()\n";
- return 1;
- }
- // Start out by reading 16 bytes from QueryPerformanceCounter, and then loop for more memory probes if necessary
- SIZE_T length = 16;
- std::cout << "QueryPerformanceCounter() ";
- do {
- // Print address
- std::cout << "@ " << std::format("{:016x}", reinterpret_cast<uintptr_t>(codeLocation)) << ":\n";
- // Read the memory
- std::vector<char> instructionBytes = readMemory(codeLocation, length);
- // Print bytes read from memory
- for (auto ch : instructionBytes)
- std::cout << std::format("{:02x}", static_cast<unsigned>(static_cast<unsigned char>(ch))) << ' ';
- std::cout << '\n';
- // Ask the user if more memory should be read
- std::cout << "Next read (0 to quit): ";
- // Get memory address of next read
- uintptr_t nextPtr;
- std::cin >> std::hex >> nextPtr;
- codeLocation = reinterpret_cast<LPVOID>(nextPtr);
- if (codeLocation != 0) {
- // Get size of next read
- std::cout << "Next length: ";
- std::cin >> std::hex >> length;
- }
- } while (codeLocation);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement