Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.05 KB | None | 0 0
  1. /**
  2. * Simple Memory Scanner Example
  3. * (c) 2014 atom0s [atom0s@live.com]
  4. */
  5.  
  6. #include <Windows.h>
  7. #include <string>
  8. #include <TlHelp32.h>
  9. #include <tchar.h>
  10. //#include "stdafx.h"
  11.  
  12. /**
  13. * @brief The target process to scan within.
  14. */
  15. #define TARGET_NAME TEXT("Tutorial-i386.exe")
  16.  
  17. /**
  18. * @brief Obtains the process id of the given target.
  19. *
  20. * @return The process id if found, 0 otherwise.
  21. */
  22. unsigned int getTargetProcessId()
  23. {
  24.     PROCESSENTRY32 pe32 = { sizeof(PROCESSENTRY32) };
  25.  
  26.     // Obtain a snapshot of the current process list..
  27.     auto handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  28.     if (handle == INVALID_HANDLE_VALUE)
  29.         return 0;
  30.  
  31.     // Obtain the first process..
  32.     if (!::Process32First(handle, &pe32))
  33.     {
  34.         ::CloseHandle(handle);
  35.         return 0;
  36.     }
  37.  
  38.     // Loop each process looking for the target..
  39.     do
  40.     {
  41.         if (!_tcsicmp(pe32.szExeFile, TARGET_NAME))
  42.         {
  43.             ::CloseHandle(handle);
  44.             return pe32.th32ProcessID;
  45.         }
  46.     } while (::Process32Next(handle, &pe32));
  47.  
  48.     // Cleanup..
  49.     ::CloseHandle(handle);
  50.     return 0;
  51. }
  52.  
  53. /**
  54. * @brief Entry point of this application.
  55. *
  56. * @param argc  The count of arguments passed to this application.
  57. * @param argv  The array of arguments passed to this application.
  58. *
  59. * @return Non-important return.
  60. */
  61. int __cdecl main(int argc, char* argv[])
  62. {
  63.     // Obtain the target process id..
  64.     auto processId = getTargetProcessId();
  65.     if (processId == 0)
  66.         return 0;
  67.  
  68.     // Open a handle to the target..
  69.     auto handle = ::OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, FALSE, processId);
  70.     if (handle == INVALID_HANDLE_VALUE)
  71.         return 0;
  72.  
  73.     // Obtain the current system information..
  74.     SYSTEM_INFO sysInfo;
  75.     ::GetSystemInfo(&sysInfo);
  76.  
  77.     auto addr_min = (intptr_t)sysInfo.lpMinimumApplicationAddress;
  78.     auto addr_max = (intptr_t)sysInfo.lpMaximumApplicationAddress;
  79.  
  80.     auto found = 0;
  81.  
  82.     // Loop the pages of memory of the application..
  83. #define TARGET_SCAN_TYPE_INT 1
  84. #define TARGET_SCAN_TYPE_8BYTE 2
  85. #define TARGET_SCAN_TYPE_FLOAT 3
  86. #define TARGET_SCAN_TYPE_DOUBLE 4
  87.  
  88. #define TARGET_SCAN_TYPE TARGET_SCAN_TYPE_8BYTE
  89.  
  90. #if TARGET_SCAN_TYPE == TARGET_SCAN_TYPE_INT
  91.     typedef int32_t target_t;
  92.     double TARGET_EPSILON = 0;
  93. #define PRITARGET PRII32
  94. #elif TARGET_SCAN_TYPE == TARGET_SCAN_TYPE_8BYTE
  95.     typedef int64_t target_t;
  96.     double TARGET_EPSILON = 0;
  97. #define PRITARGET "%lli"
  98. #elif TARGET_SCAN_TYPE == TARGET_SCAN_TYPE_FLOAT
  99.     typedef float target_t;
  100.     double TARGET_EPSILON = std::numeric_limits<float>::epsilon();
  101. #define PRITARGET "%f"
  102. #elif TARGET_SCAN_TYPE == TARGET_SCAN_TYPE_DOUBLE
  103.     typedef double target_t;
  104.     double TARGET_EPSILON = std::numeric_limits<double>::epsilon();
  105. #define PRITARGET "%f"
  106. #endif
  107.     target_t targtValue = (target_t)13371337.0f;
  108.     target_t targtWriteValue = (target_t)-13371337.0f;
  109.     while (addr_min < addr_max)
  110.     {
  111.         MEMORY_BASIC_INFORMATION mbi = { 0 };
  112.         if (!::VirtualQueryEx(handle, (LPCVOID)addr_min, &mbi, sizeof(mbi)))
  113.         {
  114.             printf_s("Failed to query memory.\n");
  115.             break;
  116.         }
  117.  
  118.         // Determine if we have access to the page..
  119.         if (mbi.State == MEM_COMMIT && ((mbi.Protect & PAGE_GUARD) == 0) && ((mbi.Protect & PAGE_NOACCESS) == 0))
  120.         {
  121.             //
  122.             // Below are flags about the current region of memory. If you want to specifically scan for only
  123.             // certain things like if the area is writable, executable, etc. you can use these flags to prevent
  124.             // reading non-desired protection types.
  125.             //
  126.  
  127.             auto isCopyOnWrite = ((mbi.Protect & PAGE_WRITECOPY) != 0 || (mbi.Protect & PAGE_EXECUTE_WRITECOPY) != 0);
  128.             auto isExecutable = ((mbi.Protect & PAGE_EXECUTE) != 0 || (mbi.Protect & PAGE_EXECUTE_READ) != 0 || (mbi.Protect & PAGE_EXECUTE_READWRITE) != 0 || (mbi.Protect & PAGE_EXECUTE_WRITECOPY) != 0);
  129.             auto isWritable = ((mbi.Protect & PAGE_READWRITE) != 0 || (mbi.Protect & PAGE_WRITECOPY) != 0 || (mbi.Protect & PAGE_EXECUTE_READWRITE) != 0 || (mbi.Protect & PAGE_EXECUTE_WRITECOPY) != 0);
  130.  
  131.             // Dump the region into a memory block..
  132.             auto dump = new uint8_t[mbi.RegionSize + 1];
  133.             //memset(dump, 0x00, mbi.RegionSize + 1);
  134.             if (!::ReadProcessMemory(handle, mbi.BaseAddress, dump, mbi.RegionSize, NULL))
  135.             {
  136.                 printf_s("Failed to read memory of location: %p\n", mbi.BaseAddress);
  137.                 break;
  138.             }
  139.  
  140.             for (size_t x = 0; x < mbi.RegionSize - 4; x += 4)
  141.             {
  142.                 target_t value = *(target_t*)(dump + x);
  143.                 if (abs(value-targtValue) <= TARGET_EPSILON)
  144.                 {
  145.                     found++;
  146.                     // assuming unqiue value for testing
  147.                     SIZE_T written;
  148.                     BOOL success = WriteProcessMemory(handle, (void*)(addr_min + x), &targtWriteValue, sizeof(targtWriteValue), &written);
  149.                     printf_s("%s in trying to write %zu bytes (" PRITARGET ") written to %p\n",
  150.                         success ? "Succeded" : "Failed", written, targtWriteValue, (void*)(addr_min + x));
  151.                 }
  152.             }
  153.  
  154.             // Cleanup the memory dump..
  155.             delete[] dump;
  156.         }
  157.  
  158.         // Step the current address by this regions size..
  159.         addr_min += mbi.RegionSize;
  160.     }
  161.  
  162.     printf_s("Found %d results!\n", found);
  163.  
  164.     // Cleanup..
  165.     ::CloseHandle(handle);
  166.     return ERROR_SUCCESS;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement