d3d9-offsets.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <d3d9.h>
  4. #include "get-graphics-offsets.h"
  5. typedef HRESULT(WINAPI *d3d9createex_t)(UINT, IDirect3D9Ex **);
  6. struct d3d9_info {
  7. HMODULE module;
  8. HWND hwnd;
  9. IDirect3D9Ex *d3d9ex;
  10. IDirect3DDevice9Ex *device;
  11. IDirect3DSwapChain9 *swap;
  12. };
  13. static inline bool d3d9_init(d3d9_info &info)
  14. {
  15. d3d9createex_t create;
  16. HRESULT hr;
  17. info.hwnd = CreateWindowExA(0, DUMMY_WNDCLASS, "d3d9 get-offset window",
  18. WS_POPUP, 0, 0, 1, 1, nullptr, nullptr,
  19. GetModuleHandleA(nullptr), nullptr);
  20. if (!info.hwnd) {
  21. return false;
  22. }
  23. info.module = LoadLibraryA("d3d9.dll");
  24. if (!info.module) {
  25. return false;
  26. }
  27. create = (d3d9createex_t)GetProcAddress(info.module,
  28. "Direct3DCreate9Ex");
  29. if (!create) {
  30. return false;
  31. }
  32. hr = create(D3D_SDK_VERSION, &info.d3d9ex);
  33. if (FAILED(hr)) {
  34. return false;
  35. }
  36. D3DPRESENT_PARAMETERS pp = {};
  37. pp.Windowed = true;
  38. pp.SwapEffect = D3DSWAPEFFECT_FLIP;
  39. pp.BackBufferFormat = D3DFMT_A8R8G8B8;
  40. pp.BackBufferWidth = 2;
  41. pp.BackBufferHeight = 2;
  42. pp.BackBufferCount = 1;
  43. pp.hDeviceWindow = info.hwnd;
  44. pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  45. hr = info.d3d9ex->CreateDeviceEx(
  46. D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, info.hwnd,
  47. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_NOWINDOWCHANGES,
  48. &pp, nullptr, &info.device);
  49. if (FAILED(hr)) {
  50. return false;
  51. }
  52. hr = info.device->GetSwapChain(0, &info.swap);
  53. if (FAILED(hr)) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. static inline void d3d9_free(d3d9_info &info)
  59. {
  60. if (info.swap)
  61. info.swap->Release();
  62. if (info.device)
  63. info.device->Release();
  64. if (info.d3d9ex)
  65. info.d3d9ex->Release();
  66. if (info.hwnd)
  67. DestroyWindow(info.hwnd);
  68. }
  69. #ifdef _WIN64
  70. #define MAX_CMP_SIZE 22
  71. static const uint8_t mask[][MAX_CMP_SIZE] = {
  72. {0xF8, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0xFF, 0x00, 0xF8, 0xF8, 0x00, 0x00, 0x00, 0x00},
  74. {0xF8, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xC0, 0x00, 0x00,
  75. 0x00, 0x00, 0xFF, 0x00, 0xF8, 0xF8, 0x00, 0x00, 0x00, 0x00},
  76. {0xF8, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xC0, 0x00, 0x00,
  77. 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00},
  78. };
  79. static const uint8_t mask_cmp[][MAX_CMP_SIZE] = {
  80. /*
  81. * Windows 7
  82. * 48 8B 83 B8 3D 00 00 mov rax, [rbx+3DB8h]
  83. * 44 39 B8 68 50 00 00 cmp [rax+5068h], r15d
  84. * 75 12 jnz short loc_7FF7AA90530
  85. * 41 B8 F9 19 00 00 mov r8d, 19F9h
  86. */
  87. {0x48, 0x8B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x44, 0x39, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0x75, 0x00, 0x40, 0xB8, 0x00, 0x00, 0x00, 0x00},
  89. /*
  90. * Windows ???+
  91. * 49 8B 87 78 41 00 00 mov rax, [r15+4178h]
  92. * 39 98 E0 51 00 00 cmp [rax+51E0h], ebx
  93. * 75 12 jnz short loc_1800AEC9C
  94. * 41 B9 C3 1A 00 00 mov r9d, 1AC3h
  95. */
  96. {0x48, 0x8B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x39, 0x80, 0x00, 0x00,
  97. 0x00, 0x00, 0x75, 0x00, 0x40, 0xB8, 0x00, 0x00, 0x00, 0x00},
  98. /*
  99. * Windows 10 April 2018
  100. * 49 8B 87 58 40 00 00 mov rax, [r15+4058h]
  101. * 39 98 C0 53 00 00 cmp [rax+53C0h], ebx
  102. * 75 12 jnz short loc_1800A7FEC
  103. * 48 8D 15 7F B6 09 00 lea rdx, addrErrorMsg
  104. *
  105. * Note: different instructions, last byte skipped due to MAX_CMP_SIZE
  106. */
  107. {0x48, 0x8B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x39, 0x80, 0x00, 0x00,
  108. 0x00, 0x00, 0x75, 0x00, 0x48, 0x8D, 0x00, 0x00, 0x00, 0x00}};
  109. // Offset into the code for the numbers we're interested in
  110. static const uint32_t code_offsets[][2] = {
  111. {3, 10},
  112. {3, 9},
  113. {3, 9},
  114. };
  115. #else
  116. #define MAX_CMP_SIZE 20
  117. static const uint8_t mask[][MAX_CMP_SIZE] = {
  118. {0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00,
  119. 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00},
  120. {0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xC0, 0x00, 0x00,
  121. 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00},
  122. {0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xC0, 0x00, 0x00,
  123. 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00},
  124. };
  125. static const uint8_t mask_cmp[][MAX_CMP_SIZE] = {
  126. /*
  127. * Windows 7+
  128. * 8B 83 E8 29 00 00 mov eax, [ebx+29E8h]
  129. * 39 B0 80 4B 00 00 cmp [eax+4B80h], esi
  130. * 75 14 jnz short loc_754CD9E1
  131. * 68 F9 19 00 00 push 19F9h
  132. */
  133. {0x8B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x39, 0x80, 0x00, 0x00, 0x00, 0x00,
  134. 0x75, 0x00, 0x68, 0x00, 0x00, 0x00, 0x00},
  135. /* Windows 10 Creator's Update+
  136. * 8B 86 F8 2B 00 00 mov eax, [esi+2BF8h]
  137. * 83 B8 00 4D 00 00 00 cmp dword ptr [eax+4D00h], 0
  138. * 75 0F jnz short loc_100D793C
  139. * 68 C3 1A 00 00 push 1AC3h
  140. */
  141. {0x8B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x83, 0x80, 0x00, 0x00,
  142. 0x00, 0x00, 0x00, 0x75, 0x00, 0x68, 0x00, 0x00, 0x00, 0x00},
  143. /*
  144. * Windows 10 April 2018 Update
  145. * 8B 86 68 2B 00 00 mov eax, [esi+2B68h]
  146. * 83 B8 F4 4D 00 00 00 cmp dword ptr [eax+4DF4h], 0
  147. * 75 0F jnz short loc_100D9A9C
  148. * BA 08 71 01 10 mov edx, offset errMsg
  149. */
  150. {0x8B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x83, 0x80, 0x00, 0x00,
  151. 0x00, 0x00, 0x00, 0x75, 0x00, 0xBA, 0x00, 0x00, 0x00, 0x00}};
  152. // Offset into the code for the numbers we're interested in
  153. static const uint32_t code_offsets[][2] = {
  154. {2, 8},
  155. {2, 8},
  156. {2, 8},
  157. };
  158. #endif
  159. #define MAX_FUNC_SCAN_BYTES 200
  160. static inline bool pattern_matches(uint8_t *byte, uint32_t *offset1,
  161. uint32_t *offset2)
  162. {
  163. for (size_t j = 0; j < sizeof(mask) / sizeof(mask[0]); j++) {
  164. for (size_t i = 0; i < MAX_CMP_SIZE; i++) {
  165. if ((byte[i] & mask[j][i]) != mask_cmp[j][i])
  166. goto next_signature;
  167. }
  168. *offset1 = code_offsets[j][0];
  169. *offset2 = code_offsets[j][1];
  170. return true;
  171. next_signature:;
  172. }
  173. return false;
  174. }
  175. void get_d3d9_offsets(struct d3d9_offsets *offsets)
  176. {
  177. d3d9_info info = {};
  178. bool success = d3d9_init(info);
  179. if (success) {
  180. uint8_t **vt = *(uint8_t ***)info.device;
  181. uint8_t *crr = vt[125];
  182. offsets->present = vtable_offset(info.module, info.device, 17);
  183. offsets->present_ex =
  184. vtable_offset(info.module, info.device, 121);
  185. offsets->present_swap =
  186. vtable_offset(info.module, info.swap, 3);
  187. uint32_t offset1, offset2;
  188. for (size_t i = 0; i < MAX_FUNC_SCAN_BYTES; i++) {
  189. if (pattern_matches(&crr[i], &offset1, &offset2)) {
  190. #define get_offset(x) *(uint32_t *)&crr[i + x]
  191. uint32_t off1 = get_offset(offset1);
  192. uint32_t off2 = get_offset(offset2);
  193. /* check to make sure offsets are within
  194. * expected values */
  195. if (off1 > 0xFFFF || off2 > 0xFFFF)
  196. break;
  197. /* check to make sure offsets actually point
  198. * toward expected data */
  199. #ifdef _MSC_VER
  200. __try {
  201. uint8_t *ptr = (uint8_t *)(info.device);
  202. uint8_t *d3d9_ptr =
  203. *(uint8_t **)(ptr + off1);
  204. if (d3d9_ptr != (uint8_t *)info.d3d9ex)
  205. break;
  206. BOOL &is_d3d9ex =
  207. *(BOOL *)(d3d9_ptr + off2);
  208. if (is_d3d9ex != TRUE)
  209. break;
  210. } __except (EXCEPTION_EXECUTE_HANDLER) {
  211. break;
  212. }
  213. #endif
  214. offsets->d3d9_clsoff = off1;
  215. offsets->is_d3d9ex_clsoff = off2;
  216. break;
  217. }
  218. }
  219. }
  220. d3d9_free(info);
  221. }