96辅助游戏论坛 - 辅助工具|www.96fuzhu.com

 找回密码
 立即注册
3391742623
5498057
查看: 1960|回复: 2

[Windows] 【1803】x64读写驱动

[复制链接]
发表于 2020-4-18 04:12 | 显示全部楼层 |阅读模式
仅适用于1803及以下的Windows版本。

使用的函数是MmMapIoSpace。

用法:
  1. // cr3 (system) can be: 0x1AA000, 0x1AB000, 0x1AC000, 0x1AD000
  2. // you will bsod if you enter wrong cr3

  3. // init
  4. auto bypass = new PhysicalMemory(get_system_eprocess(), 0x1AA000);

  5. if (!bypass->is_initialized())
  6. {
  7.         bypass->unload();
  8.         delete bypass;
  9.         return;
  10. }
  11. if (!bypass->find_eprocess(1337)) //enter pid here
  12. {
  13.         bypass->unload();
  14.         delete bypass;
  15.         return;
  16. }
  17. if (!bypass->find_base_address("1337.exe") //enter module name here
  18. {
  19.         bypass->unload();
  20.         delete bypass;
  21.         return;
  22. }

  23. // reading
  24. uintptr_t buffer;
  25. if (!bypass->read(0xADDRESS, &buffer))
  26. {
  27.         error...
  28. }

  29. // writing
  30. uintptr_t buffer = 0x1337;
  31. if (!bypass->write(0xADDRESS, &buffer))
  32. {
  33.         error...
  34. }

  35. //get base addr obtained earlier
  36. uintptr_t base_addr = bypass->get_base_address();
复制代码

源:

  1. uint64_t get_system_eprocess()
  2. {
  3.         typedef struct _SYSTEM_HANDLE {
  4.                 ULONG ProcessId;
  5.                 UCHAR ObjectTypeNumber;
  6.                 UCHAR Flags;
  7.                 USHORT Handle;
  8.                 PVOID Object;
  9.                 ACCESS_MASK GrantedAccess;
  10.         } SYSTEM_HANDLE, *PSYSTEM_HANDLE;
  11.         typedef struct _SYSTEM_HANDLE_INFORMATION {
  12.                 ULONG HandleCount; // Or NumberOfHandles if you prefer
  13.                 SYSTEM_HANDLE Handles[1];
  14.         } SYSTEM_HANDLE_INFORMATION, *PSYSTEM_HANDLE_INFORMATION;

  15.         NTSTATUS status;
  16.         ULONG handleInfoSize = 0x10000;
  17.         auto handleInfo = reinterpret_cast<PSYSTEM_HANDLE_INFORMATION>(malloc(handleInfoSize));

  18.         while ((status = NtQuerySystemInformation(static_cast<SYSTEM_INFORMATION_CLASS>(16), handleInfo, handleInfoSize, nullptr)) == (NTSTATUS)0xC0000004L)
  19.                 handleInfo = reinterpret_cast<PSYSTEM_HANDLE_INFORMATION>(realloc(handleInfo, handleInfoSize *= 2));

  20.         if (!NT_SUCCESS(status)) return 0;

  21.         for (auto i = 0; i < handleInfo->HandleCount; i++)
  22.         {
  23.                 auto handle = handleInfo->Handles[i];

  24.                 if (handle.ObjectTypeNumber == 7 && handle.ProcessId == 4)
  25.                         return reinterpret_cast<uint64_t>(handle.Object);
  26.         }
  27. }

  28. namespace dynamic_data
  29. {
  30.         uint32_t offset_directorytable;
  31.         uint32_t offset_process_id;
  32.         uint32_t offset_process_links;
  33.         uint32_t offset_object_table;
  34.         uint32_t offset_peb;

  35.         bool get_dynamic_data()
  36.         {
  37.                 //hard coded for 1709 only!!!
  38.                 offset_directorytable = 0x28;
  39.                 offset_process_id = 0x2E0;
  40.                 offset_process_links = 0x2E8;
  41.                 offset_object_table = 0x418;
  42.                 offset_peb = 0x3F8;

  43.                 return true;
  44.         }
  45. }

  46. class CoreTempDriver
  47. {
  48. #define CTL_READ_PHYSICAL_ADDRESS        0x9C402618
  49. #define CTL_WRITE_PHYSICAL_ADDRESS        0x9C40261C

  50. public:
  51.         CoreTempDriver::CoreTempDriver(const std::string& service) : _service_name(service),
  52.                 _driver(CreateFileA(service.c_str(), 0xC0000000, 0,
  53.                         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))
  54.         {
  55.                 if (_driver != INVALID_HANDLE_VALUE)
  56.                         _initialized = true;
  57.         }
  58.         CoreTempDriver::~CoreTempDriver()
  59.         {
  60.                 CloseHandle(_driver);
  61.         }
  62.         bool is_initialized()
  63.         {
  64.                 return _initialized;
  65.         }
  66.         bool unload()
  67.         {
  68.                 auto hManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
  69.                 if (!hManager) return false;

  70.                 auto hService = OpenServiceA(hManager, _service_name.c_str(), SERVICE_ALL_ACCESS);
  71.                 if (!hService)
  72.                 {
  73.                         CloseServiceHandle(hManager);
  74.                         return false;
  75.                 }

  76.                 SERVICE_STATUS ss;
  77.                 if (!ControlService(hService, SERVICE_CONTROL_STOP, &ss))
  78.                 {
  79.                         DeleteService(hService);
  80.                         CloseServiceHandle(hManager);
  81.                         CloseServiceHandle(hService);
  82.                         return false;
  83.                 }

  84.                 if (!DeleteService(hService)) return false;
  85.                 CloseServiceHandle(hManager);
  86.                 CloseServiceHandle(hService);

  87.                 return true;
  88.         }

  89.         template <typename T>
  90.         bool physical_read(uint64_t address_phys, T* buffer, uint32_t size = sizeof(T))
  91.         {
  92.                 uint64_t input_buffer[2];

  93.                 input_buffer[0] = address_phys;
  94.                 input_buffer[1] = size;

  95.                 return DeviceIoControl(hDriver, CTL_READ_PHYSICAL_ADDRESS,
  96.                         &input_buffer, sizeof(input_buffer), buffer, size, nullptr, nullptr);
  97.         }

  98.         template <typename T>
  99.         bool physical_write(uint64_t address_phys, T* buffer, uint32_t size = sizeof(T))
  100.         {
  101.                 //direct heap allocation causes BSOD for whatever reason, so do it via WINAPI
  102.                 //15/04/2020: not sure what this is about?? please change this

  103.                 auto input_buffer = reinterpret_cast<uint64_t>(VirtualAlloc(NULL,
  104.                         size + 0xC, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE));
  105.                 if (!input_buffer)
  106.                         return false;

  107.                 ZeroMemory((void*)input_buffer, size + 0xC);
  108.                 memcpy((void*)input_buffer, &address_phys, sizeof(address_phys));
  109.                 memcpy((void*)(input_buffer + 0x8), &size, sizeof(size));
  110.                 memcpy((void*)(input_buffer + 0xC), buffer, size);

  111.                 auto result = DeviceIoControl(_driver, CTL_WRITE_PHYSICAL_ADDRESS,
  112.                         (LPVOID)input_buffer, size + 0xC, nullptr, NULL, nullptr, NULL);
  113.                 if (!VirtualFree((LPVOID)input_buffer, NULL, MEM_RELEASE))
  114.                         return false;

  115.                 return result;
  116.         }
  117. private:
  118.         HANDLE _driver = INVALID_HANDLE_VALUE;
  119.         bool _initialized;
  120.         std::string _service_name;
  121. };

  122. class PhysicalMemory
  123. {
  124. public:
  125.         PhysicalMemory(uint64_t eproc, uint64_t cr3) :
  126.                 _coretemp(new CoreTempDriver("\\\\.\\ALSysIO")), _system({ eproc, cr3 })
  127.         {
  128.                 if (_coretemp->is_initialized() && _system.cr3 && _system.eproc)
  129.                         _initialized = true;
  130.         }
  131.         ~PhysicalMemory()
  132.         {
  133.                 delete _coretemp;
  134.         }
  135.         bool is_initialized()
  136.         {
  137.                 return _initialized;
  138.         }
  139.         bool unload()
  140.         {
  141.                 return _coretemp->unload();
  142.         }

  143.         bool find_eprocess(uint32_t pid)
  144.         {
  145.                 using namespace dynamic_data;
  146.                 if (!get_dynamic_data())
  147.                         return false;

  148.                 auto list_head = _system.eproc + offset_process_links; //ActiveProcessLinks
  149.                 auto link_current = list_head;

  150.                 uint64_t link_last = 0;
  151.                 if (!system_read(list_head + sizeof(uint64_t), &link_last))
  152.                         return false;

  153.                 if (!link_last)
  154.                         return false;

  155.                 do
  156.                 {
  157.                         auto entry = link_current - offset_process_links;

  158.                         uint64_t current_link_process_id = 0;
  159.                         if (!system_read(entry + offset_process_id, ¤t_link_process_id))
  160.                                 return false;

  161.                         if (current_link_process_id == pid)
  162.                         {
  163.                                 _target_eproc = entry;
  164.                                 return true;
  165.                         }

  166.                         if (!system_read(link_current, &link_current))
  167.                                 return false;
  168.                 } while (link_current != link_last);
  169.                 return false;
  170.         }
  171.         bool find_base_address(std::string module)
  172.         {
  173.                 uint64_t peb;
  174.                 if (!read(_target_eproc + dynamic_data::offset_peb, &peb))
  175.                         return false;

  176.                 uint64_t ldr;
  177.                 if (!read(peb + 0x18, &ldr))
  178.                         return false;

  179.                 uint64_t first_module;
  180.                 if (!read(ldr + 0x10, &first_module))
  181.                         return false;

  182.                 uint64_t current_module = first_module;

  183.                 do
  184.                 {
  185.                         uint64_t wstring_ptr;
  186.                         if (!read(current_module + 0x60, &wstring_ptr))
  187.                                 return false;

  188.                         wchar_t wstring[256];
  189.                         if (!read(wstring_ptr, wstring, 256))
  190.                                 return false;

  191.                         char module_name[256];
  192.                         sprintf(module_name, "%ws", wstring);

  193.                         if (!_stricmp(module_name, module.c_str()))
  194.                         {
  195.                                 uint64_t base;
  196.                                 if (!read(current_module + 0x30, &base))
  197.                                         return false;

  198.                                 _target_base = base;
  199.                                 return !!_target_base;
  200.                         }

  201.                         if (!read(current_module, ¤t_module))
  202.                                 return false;

  203.                 } while (first_module != current_module);

  204.                 return false;
  205.         }
  206.         uint64_t get_base_address()
  207.         {
  208.                 return _target_base;
  209.         }
  210.         void set_base_address(uint64_t base)
  211.         {
  212.                 _target_base = base;
  213.         }

  214.         template <typename T>
  215.         bool read(uint64_t address, T* buffer, uint32_t size = sizeof(T))
  216.         {
  217.                 address = virtual_to_physical(address, get_cr3());
  218.                 if (!address)
  219.                         return false;

  220.                 return _coretemp->physical_read(address, buffer, size);
  221.         }
  222.         template <typename T>
  223.         bool write(uint64_t address, T* buffer, uint32_t size = sizeof(T))
  224.         {
  225.                 address = virtual_to_physical(address, get_cr3());
  226.                 if (!address)
  227.                         return false;

  228.                 return coretemp->physical_write(address, buffer, size);
  229.         }

  230. private:
  231.         template <typename T>
  232.         bool system_read(uint64_t address, T* buffer, uint32_t size = sizeof(T))
  233.         {
  234.                 address = virtual_to_physical(address);
  235.                 if (!address)
  236.                         return false;

  237.                 return coretemp->physical_read(address, buffer, size);
  238.         }
  239.         template <typename T>
  240.         bool system_write(uint64_t address, T* buffer, uint32_t size = sizeof(T))
  241.         {
  242.                 address = virtual_to_physical(address);
  243.                 if (!address)
  244.                         return false;

  245.                 return coretemp->physical_write(address, buffer, size);
  246.         }

  247.         uint64_t get_cr3()
  248.         {
  249.                 uint64_t buffer;
  250.                 if (!system_read(_target_eproc + dynamic_data::offset_directorytable, &buffer))
  251.                         return 0;

  252.                 buffer -= buffer % 0x10; //this is going to always zero out the last digit e.g. (0x1ab002 -> 0x1ab000)
  253.                 return buffer;
  254.         }
  255.         uint64_t virtual_to_physical(uint64_t address, uint64_t cr3 = 0)
  256.         {
  257.                 //credits to markhc
  258.                 if (!cr3)
  259.                         cr3 = _system.cr3;

  260.                 std::int16_t pml4 = (address >> 39) & 0x1FF;
  261.                 std::int16_t directory_pointer = (address >> 30) & 0x1FF;
  262.                 std::int16_t directory = (address >> 21) & 0x1FF;
  263.                 std::int16_t table = (address >> 12) & 0x1FF;

  264.                 uint64_t pml4e = 0;
  265.                 if (!_coretemp->physical_read(cr3 + pml4 * sizeof(uint64_t), &pml4e))
  266.                         return 0;
  267.                 if (!pml4e)
  268.                         return 0;

  269.                 uint64_t pdpte = 0;
  270.                 if (!_coretemp->physical_read((pml4e & 0xFFFFFFFFFF000) + directory_pointer * sizeof(uint64_t), &pdpte))
  271.                         return 0;
  272.                 if (!pdpte)
  273.                         return 0;

  274.                 if ((pdpte & (1 << 7)) != 0)
  275.                         return (pdpte & 0xFFFFFC0000000) + (address & 0x3FFFFFFF);

  276.                 uint64_t pde = 0;
  277.                 if (!_coretemp->physical_read((pdpte & 0xFFFFFFFFFF000) + directory * sizeof(uint64_t), &pde)) return 0;
  278.                 if (!pde)
  279.                         return 0;

  280.                 // check PS bit
  281.                 if ((pde & (1 << 7)) != 0)
  282.                         return (pde & 0xFFFFFFFE00000) + (address & 0x1FFFFF);

  283.                 uint64_t pte = 0;
  284.                 if (!_coretemp->physical_read((pde & 0xFFFFFFFFFF000) + table * sizeof(uint64_t), &pte)) return 0;
  285.                 if (!pte)
  286.                         return 0;

  287.                 return (pte & 0xFFFFFFFFFF000) + (address & 0xFFF);
  288.         }

  289.         CoreTempDriver* _coretemp;
  290.         bool _initialized = false;
  291.         uint64_t _target_eproc;
  292.         uint64_t _target_base;

  293.         struct System
  294.         {
  295.                 uint64_t eproc;
  296.                 uint64_t cr3;
  297.         } _system;
  298. };
复制代码


【1803】x64读写驱动.zip

22.75 KB, 下载次数: 14

回复

使用道具 举报

发表于 2020-4-18 06:48 | 显示全部楼层
6
回复

使用道具 举报

发表于 2020-4-18 17:28 | 显示全部楼层
谢谢!一直在找!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|96辅助游戏论坛

GMT+8, 2024-5-18 09:11 , Processed in 0.068477 second(s), 22 queries .

Powered by Discuz! X3.4

© 2016-2023 Comsenz Inc.

快速回复 返回顶部 返回列表