win-dll-blocklist.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /******************************************************************************
  2. Copyright (C) 2023 by Richard Stanway
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <Windows.h>
  15. #include <psapi.h>
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #include <inttypes.h>
  19. #include "detours.h"
  20. #include "obs.h"
  21. // Undocumented NT structs / function definitions !
  22. typedef enum _SECTION_INHERIT { ViewShare = 1, ViewUnmap = 2 } SECTION_INHERIT;
  23. typedef enum _SECTION_INFORMATION_CLASS {
  24. SectionBasicInformation = 0,
  25. SectionImageInformation
  26. } SECTION_INFORMATION_CLASS;
  27. typedef struct _SECTION_BASIC_INFORMATION {
  28. PVOID BaseAddress;
  29. ULONG Attributes;
  30. LARGE_INTEGER Size;
  31. } SECTION_BASIC_INFORMATION;
  32. typedef NTSTATUS(STDMETHODCALLTYPE *fn_NtMapViewOfSection)(
  33. HANDLE, HANDLE, PVOID, ULONG_PTR, SIZE_T, PLARGE_INTEGER, PSIZE_T,
  34. SECTION_INHERIT, ULONG, ULONG);
  35. typedef NTSTATUS(STDMETHODCALLTYPE *fn_NtUnmapViewOfSection)(HANDLE, PVOID);
  36. typedef NTSTATUS(STDMETHODCALLTYPE *fn_NtQuerySection)(
  37. HANDLE, SECTION_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T);
  38. static fn_NtMapViewOfSection ntMap;
  39. static fn_NtUnmapViewOfSection ntUnmap;
  40. static fn_NtQuerySection ntQuery;
  41. #define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L)
  42. // Method of matching timestamp of DLL in PE header
  43. typedef enum {
  44. TS_IGNORE = 0, // Ignore timestamp; block all DLLs with this name
  45. TS_EQUAL, // Block only DLL with this exact timestamp
  46. TS_LESS_THAN, // Block all DLLs with an earlier timestamp
  47. TS_GREATER_THAN, // Block all DLLs with a later timestamp
  48. TS_ALLOW_ONLY_THIS, // Invert behavior: only allow this specific timestamp
  49. } ts_compare_t;
  50. typedef struct {
  51. // DLL name, lower case
  52. const wchar_t *name;
  53. // Length of name, calculated at startup - leave as zero
  54. size_t name_len;
  55. // PE timestamp
  56. const uint32_t timestamp;
  57. // How to treat the timestamp field
  58. const ts_compare_t method;
  59. // Number of times we've blocked this DLL, for logging purposes
  60. uint64_t blocked_count;
  61. } blocked_module_t;
  62. /*
  63. * Note: The name matches at the end of the string based on its length, this allows
  64. * for matching DLLs that may have generic names but a problematic version only
  65. * exists in a certain directory. A name should always include a path component
  66. * so that e.g. fraps.dll doesn't match notfraps.dll.
  67. */
  68. static blocked_module_t blocked_modules[] = {
  69. // Dell / Alienware Backup & Recovery, crashes during "Browse" dialogs
  70. {L"\\dbroverlayiconbackuped.dll", 0, 0, TS_IGNORE},
  71. // RTSS, no good reason for this to be in OBS
  72. {L"\\rtsshooks.dll", 0, 0, TS_IGNORE},
  73. // Dolby Axon overlay
  74. {L"\\axonoverlay.dll", 0, 0, TS_IGNORE},
  75. // Action! Recorder Software
  76. {L"\\action_x64.dll", 0, 0, TS_IGNORE},
  77. // ASUS GamerOSD, breaks DX11 things
  78. {L"\\atkdx11disp.dll", 0, 0, TS_IGNORE},
  79. // Malware
  80. {L"\\sendori.dll", 0, 0, TS_IGNORE},
  81. // Astril VPN Proxy, hooks stuff and crashes
  82. {L"\\asproxy64.dll", 0, 0, TS_IGNORE},
  83. // Nahimic Audio
  84. {L"\\nahimicmsidevprops.dll", 0, 0, TS_IGNORE},
  85. {L"\\nahimicmsiosd.dll", 0, 0, TS_IGNORE},
  86. // FRAPS hook
  87. {L"\\fraps64.dll", 0, 0, TS_IGNORE},
  88. // ASUS GPU TWEAK II OSD
  89. {L"\\gtii-osd64.dll", 0, 0, TS_IGNORE},
  90. {L"\\gtii-osd64-vk.dll", 0, 0, TS_IGNORE},
  91. // EVGA Precision, D3D crashes
  92. {L"\\pxshw10_x64.dll", 0, 0, TS_IGNORE},
  93. // Wacom / Other tablet driver, locks up UI
  94. {L"\\wintab32.dll", 0, 0, TS_IGNORE},
  95. // Adobe Dynamic Link (Adobe CC), crashes in its own thread
  96. {L"\\mc_trans_video_imagescaler.dll", 0, 0, TS_IGNORE},
  97. // Weird Polish banking "security" software, breaks UI
  98. {L"\\wslbscr64.dll", 0, 0, TS_IGNORE},
  99. // Various things hooking with EasyHook that probably shouldn't touch OBS
  100. {L"\\easyhook64.dll", 0, 0, TS_IGNORE},
  101. // Ultramon
  102. {L"\\rtsultramonhook.dll", 0, 0, TS_IGNORE},
  103. // HiAlgo Boost, locks up UI
  104. {L"\\hookdll.dll", 0, 0, TS_IGNORE},
  105. // Adobe Core Sync? Crashes NDI.
  106. {L"\\coresync_x64.dll", 0, 0, TS_IGNORE},
  107. // Fasso DRM, crashes D3D
  108. {L"\\f_sps.dll", 0, 0, TS_IGNORE},
  109. // Korean banking "security" software, crashes randomly
  110. {L"\\t_prevent64.dll", 0, 0, TS_IGNORE},
  111. // Generic named unity capture filter. Unfortunately only a forked version
  112. // has a critical fix to prevent deadlocks during enumeration. We block
  113. // all versions since if someone didn't change the DLL name they likely
  114. // didn't implement the deadlock fix.
  115. // Reference: https://github.com/schellingb/UnityCapture/commit/2eabf0f
  116. {L"\\unitycapturefilter64bit.dll", 0, 0, TS_IGNORE},
  117. // VSeeFace capture filter < v1.13.38b3 without above fix implemented
  118. {L"\\vseefacecamera64bit.dll", 0, 1666993098, TS_LESS_THAN},
  119. };
  120. static bool is_module_blocked(wchar_t *dll, uint32_t timestamp)
  121. {
  122. blocked_module_t *first_allowed = NULL;
  123. size_t len;
  124. len = wcslen(dll);
  125. wcslwr(dll);
  126. // Default behavior is to not block
  127. bool should_block = false;
  128. for (int i = 0; i < _countof(blocked_modules); i++) {
  129. blocked_module_t *b = &blocked_modules[i];
  130. wchar_t *dll_ptr;
  131. if (len >= b->name_len)
  132. dll_ptr = dll + len - b->name_len;
  133. else
  134. dll_ptr = dll;
  135. if (!wcscmp(dll_ptr, b->name)) {
  136. if (b->method == TS_IGNORE) {
  137. b->blocked_count++;
  138. return true;
  139. } else if (b->method == TS_EQUAL &&
  140. timestamp == b->timestamp) {
  141. b->blocked_count++;
  142. return true;
  143. } else if (b->method == TS_LESS_THAN &&
  144. timestamp < b->timestamp) {
  145. b->blocked_count++;
  146. return true;
  147. } else if (b->method == TS_GREATER_THAN &&
  148. timestamp > b->timestamp) {
  149. b->blocked_count++;
  150. return true;
  151. } else if (b->method == TS_ALLOW_ONLY_THIS) {
  152. // Invert default behavior to block if
  153. // we don't find any matching timestamps
  154. // for this DLL.
  155. should_block = true;
  156. if (timestamp == b->timestamp)
  157. return false;
  158. // Bit of a hack to support counting of
  159. // TS_ALLOW_ONLY_THIS blocks as there may
  160. // be multiple entries for the same DLL.
  161. if (!first_allowed)
  162. first_allowed = b;
  163. }
  164. }
  165. }
  166. if (first_allowed)
  167. first_allowed->blocked_count++;
  168. return should_block;
  169. }
  170. static NTSTATUS
  171. NtMapViewOfSection_hook(HANDLE SectionHandle, HANDLE ProcessHandle,
  172. PVOID *BaseAddress, ULONG_PTR ZeroBits,
  173. SIZE_T CommitSize, PLARGE_INTEGER SectionOffset,
  174. PSIZE_T ViewSize, SECTION_INHERIT InheritDisposition,
  175. ULONG AllocationType, ULONG Win32Protect)
  176. {
  177. SECTION_BASIC_INFORMATION section_information;
  178. wchar_t fileName[MAX_PATH];
  179. SIZE_T wrote = 0;
  180. NTSTATUS ret;
  181. uint32_t timestamp = 0;
  182. ret = ntMap(SectionHandle, ProcessHandle, BaseAddress, ZeroBits,
  183. CommitSize, SectionOffset, ViewSize, InheritDisposition,
  184. AllocationType, Win32Protect);
  185. // Verify map and process
  186. if (ret < 0 || ProcessHandle != GetCurrentProcess())
  187. return ret;
  188. // Fetch section information
  189. if (ntQuery(SectionHandle, SectionBasicInformation,
  190. &section_information, sizeof(section_information),
  191. &wrote) < 0)
  192. return ret;
  193. // Verify fetch was successful
  194. if (wrote != sizeof(section_information))
  195. return ret;
  196. // We're not interested in non-image maps
  197. if (!(section_information.Attributes & SEC_IMAGE))
  198. return ret;
  199. // Examine the PE header. Perhaps the map is small
  200. // so wrap it in an exception handler in case we
  201. // read past the end of the buffer.
  202. __try {
  203. BYTE *p = (BYTE *)*BaseAddress;
  204. IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)p;
  205. if (dos->e_magic != IMAGE_DOS_SIGNATURE)
  206. return ret;
  207. IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS *)(p + dos->e_lfanew);
  208. if (nt->Signature != IMAGE_NT_SIGNATURE)
  209. return ret;
  210. timestamp = nt->FileHeader.TimeDateStamp;
  211. } __except (EXCEPTION_EXECUTE_HANDLER) {
  212. return ret;
  213. }
  214. // Get the actual filename if possible
  215. if (K32GetMappedFileNameW(ProcessHandle, *BaseAddress, fileName,
  216. _countof(fileName)) == 0)
  217. return ret;
  218. if (is_module_blocked(fileName, timestamp)) {
  219. ntUnmap(ProcessHandle, BaseAddress);
  220. ret = STATUS_UNSUCCESSFUL;
  221. }
  222. return ret;
  223. }
  224. void install_dll_blocklist_hook(void)
  225. {
  226. HMODULE nt = GetModuleHandle(L"NTDLL");
  227. if (!nt)
  228. return;
  229. ntMap = (fn_NtMapViewOfSection)GetProcAddress(nt, "NtMapViewOfSection");
  230. if (!ntMap)
  231. return;
  232. ntUnmap = (fn_NtUnmapViewOfSection)GetProcAddress(
  233. nt, "NtUnmapViewOfSection");
  234. if (!ntUnmap)
  235. return;
  236. ntQuery = (fn_NtQuerySection)GetProcAddress(nt, "NtQuerySection");
  237. if (!ntQuery)
  238. return;
  239. // Pre-compute length of all DLL names for exact matching
  240. for (int i = 0; i < _countof(blocked_modules); i++) {
  241. blocked_module_t *b = &blocked_modules[i];
  242. b->name_len = wcslen(b->name);
  243. }
  244. DetourTransactionBegin();
  245. if (DetourAttach((PVOID *)&ntMap, NtMapViewOfSection_hook) != NO_ERROR)
  246. DetourTransactionAbort();
  247. else
  248. DetourTransactionCommit();
  249. }
  250. void log_blocked_dlls(void)
  251. {
  252. for (int i = 0; i < _countof(blocked_modules); i++) {
  253. blocked_module_t *b = &blocked_modules[i];
  254. if (b->blocked_count) {
  255. blog(LOG_WARNING,
  256. "Blocked loading of '%S' %" PRIu64 " time%S.",
  257. b->name, b->blocked_count,
  258. b->blocked_count == 1 ? L"" : L"s");
  259. }
  260. }
  261. }