LuckyEcho

Untitled

Sep 21st, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. int ScanMemoryInt(HANDLE pHandle, int searchValue)
  2. {
  3.     int total = 0;
  4.    
  5.     // Get the page size information.
  6.     SYSTEM_INFO sSysInfo = {};
  7.     GetSystemInfo(&sSysInfo);
  8.  
  9.     // Variable to store the memory information for that page or pages.
  10.     MEMORY_BASIC_INFORMATION memInfo;
  11.     void * currentScanAddress = 0;
  12.     char* buffer;
  13.    
  14.     while (true)
  15.     {
  16.         SIZE_T readBytes = VirtualQueryEx(pHandle, currentScanAddress, &memInfo, sizeof(memInfo));
  17.  
  18.         if (!readBytes)
  19.         {
  20.             std::cout << "Total: " << total << " Addresses" << std::endl;
  21.             return total;
  22.         }
  23.  
  24.        
  25.  
  26.         // Only look at committed memory
  27.         if (memInfo.State == MEM_COMMIT && memInfo.Protect != PAGE_NOACCESS)
  28.         {
  29.             // Allocate a buffer to read the memory contents
  30.             std::vector<int> buffer(memInfo.RegionSize / sizeof(int));
  31.  
  32.             // Read the memory contents into the buffer
  33.             SIZE_T bytesRead;
  34.             if (ReadProcessMemory(pHandle, memInfo.BaseAddress, buffer.data(), memInfo.RegionSize, &bytesRead))
  35.             {
  36.                 //std::cout << "Successfully read " << bytesRead << " bytes from memory." << std::endl;
  37.  
  38.                 // Process mem contents and print addresses and integer values
  39.                 for (SIZE_T i = 0; i < bytesRead / sizeof(int); ++i)
  40.                 {
  41.                     void* address = reinterpret_cast<char*>(memInfo.BaseAddress) + (i * sizeof(int));
  42.                     int value = buffer[i];
  43.                     if (value == (int)searchValue)
  44.                     {
  45.                        
  46.                              // Do things with the address here, the address variable stores the memory address.
  47.                              // As for me I store it in a list.
  48.                               currentValuesInt[address] = value;
  49.                               total++;
  50.                     }
  51.                    
  52.                 }
  53.             }
  54.            
  55.         }  
  56.  
  57.         currentScanAddress = (char *)memInfo.BaseAddress + memInfo.RegionSize;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment