win-dll-blocklist.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. // VTuber Maker capture filter < 2023-03-13 without above fix implemented
  120. {L"\\live3dvirtualcam\\lib64_new2.dll", 0, 1678695956, TS_LESS_THAN},
  121. // Obsolete unfixed versions of VTuber Maker capture filter
  122. {L"\\live3dvirtualcam\\lib64_new.dll", 0, 0, TS_IGNORE},
  123. {L"\\live3dvirtualcam\\lib64.dll", 0, 0, TS_IGNORE},
  124. };
  125. static bool is_module_blocked(wchar_t *dll, uint32_t timestamp)
  126. {
  127. blocked_module_t *first_allowed = NULL;
  128. size_t len;
  129. len = wcslen(dll);
  130. wcslwr(dll);
  131. // Default behavior is to not block
  132. bool should_block = false;
  133. for (int i = 0; i < _countof(blocked_modules); i++) {
  134. blocked_module_t *b = &blocked_modules[i];
  135. wchar_t *dll_ptr;
  136. if (len >= b->name_len)
  137. dll_ptr = dll + len - b->name_len;
  138. else
  139. dll_ptr = dll;
  140. if (!wcscmp(dll_ptr, b->name)) {
  141. if (b->method == TS_IGNORE) {
  142. b->blocked_count++;
  143. return true;
  144. } else if (b->method == TS_EQUAL &&
  145. timestamp == b->timestamp) {
  146. b->blocked_count++;
  147. return true;
  148. } else if (b->method == TS_LESS_THAN &&
  149. timestamp < b->timestamp) {
  150. b->blocked_count++;
  151. return true;
  152. } else if (b->method == TS_GREATER_THAN &&
  153. timestamp > b->timestamp) {
  154. b->blocked_count++;
  155. return true;
  156. } else if (b->method == TS_ALLOW_ONLY_THIS) {
  157. // Invert default behavior to block if
  158. // we don't find any matching timestamps
  159. // for this DLL.
  160. should_block = true;
  161. if (timestamp == b->timestamp)
  162. return false;
  163. // Bit of a hack to support counting of
  164. // TS_ALLOW_ONLY_THIS blocks as there may
  165. // be multiple entries for the same DLL.
  166. if (!first_allowed)
  167. first_allowed = b;
  168. }
  169. }
  170. }
  171. if (first_allowed)
  172. first_allowed->blocked_count++;
  173. return should_block;
  174. }
  175. static NTSTATUS
  176. NtMapViewOfSection_hook(HANDLE SectionHandle, HANDLE ProcessHandle,
  177. PVOID *BaseAddress, ULONG_PTR ZeroBits,
  178. SIZE_T CommitSize, PLARGE_INTEGER SectionOffset,
  179. PSIZE_T ViewSize, SECTION_INHERIT InheritDisposition,
  180. ULONG AllocationType, ULONG Win32Protect)
  181. {
  182. SECTION_BASIC_INFORMATION section_information;
  183. wchar_t fileName[MAX_PATH];
  184. SIZE_T wrote = 0;
  185. NTSTATUS ret;
  186. uint32_t timestamp = 0;
  187. ret = ntMap(SectionHandle, ProcessHandle, BaseAddress, ZeroBits,
  188. CommitSize, SectionOffset, ViewSize, InheritDisposition,
  189. AllocationType, Win32Protect);
  190. // Verify map and process
  191. if (ret < 0 || ProcessHandle != GetCurrentProcess())
  192. return ret;
  193. // Fetch section information
  194. if (ntQuery(SectionHandle, SectionBasicInformation,
  195. &section_information, sizeof(section_information),
  196. &wrote) < 0)
  197. return ret;
  198. // Verify fetch was successful
  199. if (wrote != sizeof(section_information))
  200. return ret;
  201. // We're not interested in non-image maps
  202. if (!(section_information.Attributes & SEC_IMAGE))
  203. return ret;
  204. // Examine the PE header. Perhaps the map is small
  205. // so wrap it in an exception handler in case we
  206. // read past the end of the buffer.
  207. __try {
  208. BYTE *p = (BYTE *)*BaseAddress;
  209. IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)p;
  210. if (dos->e_magic != IMAGE_DOS_SIGNATURE)
  211. return ret;
  212. IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS *)(p + dos->e_lfanew);
  213. if (nt->Signature != IMAGE_NT_SIGNATURE)
  214. return ret;
  215. timestamp = nt->FileHeader.TimeDateStamp;
  216. } __except (EXCEPTION_EXECUTE_HANDLER) {
  217. return ret;
  218. }
  219. // Get the actual filename if possible
  220. if (K32GetMappedFileNameW(ProcessHandle, *BaseAddress, fileName,
  221. _countof(fileName)) == 0)
  222. return ret;
  223. if (is_module_blocked(fileName, timestamp)) {
  224. ntUnmap(ProcessHandle, BaseAddress);
  225. ret = STATUS_UNSUCCESSFUL;
  226. }
  227. return ret;
  228. }
  229. void install_dll_blocklist_hook(void)
  230. {
  231. HMODULE nt = GetModuleHandle(L"NTDLL");
  232. if (!nt)
  233. return;
  234. ntMap = (fn_NtMapViewOfSection)GetProcAddress(nt, "NtMapViewOfSection");
  235. if (!ntMap)
  236. return;
  237. ntUnmap = (fn_NtUnmapViewOfSection)GetProcAddress(
  238. nt, "NtUnmapViewOfSection");
  239. if (!ntUnmap)
  240. return;
  241. ntQuery = (fn_NtQuerySection)GetProcAddress(nt, "NtQuerySection");
  242. if (!ntQuery)
  243. return;
  244. // Pre-compute length of all DLL names for exact matching
  245. for (int i = 0; i < _countof(blocked_modules); i++) {
  246. blocked_module_t *b = &blocked_modules[i];
  247. b->name_len = wcslen(b->name);
  248. }
  249. DetourTransactionBegin();
  250. if (DetourAttach((PVOID *)&ntMap, NtMapViewOfSection_hook) != NO_ERROR)
  251. DetourTransactionAbort();
  252. else
  253. DetourTransactionCommit();
  254. }
  255. void log_blocked_dlls(void)
  256. {
  257. for (int i = 0; i < _countof(blocked_modules); i++) {
  258. blocked_module_t *b = &blocked_modules[i];
  259. if (b->blocked_count) {
  260. blog(LOG_WARNING,
  261. "Blocked loading of '%S' %" PRIu64 " time%S.",
  262. b->name, b->blocked_count,
  263. b->blocked_count == 1 ? L"" : L"s");
  264. }
  265. }
  266. }