win-dll-blocklist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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)(HANDLE, HANDLE, PVOID, ULONG_PTR, SIZE_T, PLARGE_INTEGER,
  33. PSIZE_T, SECTION_INHERIT, ULONG, ULONG);
  34. typedef NTSTATUS(STDMETHODCALLTYPE *fn_NtUnmapViewOfSection)(HANDLE, PVOID);
  35. typedef NTSTATUS(STDMETHODCALLTYPE *fn_NtQuerySection)(HANDLE, SECTION_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T);
  36. static fn_NtMapViewOfSection ntMap;
  37. static fn_NtUnmapViewOfSection ntUnmap;
  38. static fn_NtQuerySection ntQuery;
  39. #define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L)
  40. // Method of matching timestamp of DLL in PE header
  41. typedef enum {
  42. TS_IGNORE = 0, // Ignore timestamp; block all DLLs with this name
  43. TS_EQUAL, // Block only DLL with this exact timestamp
  44. TS_LESS_THAN, // Block all DLLs with an earlier timestamp
  45. TS_GREATER_THAN, // Block all DLLs with a later timestamp
  46. TS_ALLOW_ONLY_THIS, // Invert behavior: only allow this specific timestamp
  47. } ts_compare_t;
  48. typedef struct {
  49. // DLL name, lower case
  50. const wchar_t *name;
  51. // Length of name, calculated at startup - leave as zero
  52. size_t name_len;
  53. // PE timestamp
  54. const uint32_t timestamp;
  55. // How to treat the timestamp field
  56. const ts_compare_t method;
  57. // Number of times we've blocked this DLL, for logging purposes
  58. uint64_t blocked_count;
  59. } blocked_module_t;
  60. /*
  61. * Note: The name matches at the end of the string based on its length, this allows
  62. * for matching DLLs that may have generic names but a problematic version only
  63. * exists in a certain directory. A name should always include a path component
  64. * so that e.g. fraps.dll doesn't match notfraps.dll.
  65. */
  66. static blocked_module_t blocked_modules[] = {
  67. // Dell / Alienware Backup & Recovery, crashes during "Browse" dialogs
  68. {L"\\dbroverlayiconbackuped.dll", 0, 0, TS_IGNORE},
  69. // RTSS, no good reason for this to be in OBS
  70. {L"\\rtsshooks.dll", 0, 0, TS_IGNORE},
  71. // Dolby Axon overlay
  72. {L"\\axonoverlay.dll", 0, 0, TS_IGNORE},
  73. // Action! Recorder Software
  74. {L"\\action_x64.dll", 0, 0, TS_IGNORE},
  75. // ASUS GamerOSD, breaks DX11 things
  76. {L"\\atkdx11disp.dll", 0, 0, TS_IGNORE},
  77. // Malware
  78. {L"\\sendori.dll", 0, 0, TS_IGNORE},
  79. // Astril VPN Proxy, hooks stuff and crashes
  80. {L"\\asproxy64.dll", 0, 0, TS_IGNORE},
  81. // Nahimic Audio
  82. {L"\\nahimicmsidevprops.dll", 0, 0, TS_IGNORE},
  83. {L"\\nahimicmsiosd.dll", 0, 0, TS_IGNORE},
  84. // FRAPS hook
  85. {L"\\fraps64.dll", 0, 0, TS_IGNORE},
  86. // ASUS GPU TWEAK II OSD
  87. {L"\\gtii-osd64.dll", 0, 0, TS_IGNORE},
  88. {L"\\gtii-osd64-vk.dll", 0, 0, TS_IGNORE},
  89. // EVGA Precision, D3D crashes
  90. {L"\\pxshw10_x64.dll", 0, 0, TS_IGNORE},
  91. // Wacom / Other tablet driver, locks up UI
  92. {L"\\wintab32.dll", 0, 0, TS_IGNORE},
  93. // MainConcept Image Scaler, crashes in its own thread. Block versions
  94. // older than the one Elgato uses (2016-02-15).
  95. {L"\\mc_trans_video_imagescaler.dll", 0, 1455495131, TS_LESS_THAN},
  96. // Weird Polish banking "security" software, breaks UI
  97. {L"\\wslbscr64.dll", 0, 0, TS_IGNORE},
  98. // Various things hooking with EasyHook that probably shouldn't touch OBS
  99. {L"\\easyhook64.dll", 0, 0, TS_IGNORE},
  100. // Ultramon
  101. {L"\\rtsultramonhook.dll", 0, 0, TS_IGNORE},
  102. // HiAlgo Boost, locks up UI
  103. {L"\\hookdll.dll", 0, 0, TS_IGNORE},
  104. // Adobe Core Sync? Crashes NDI.
  105. {L"\\coresync_x64.dll", 0, 0, TS_IGNORE},
  106. // Fasso DRM, crashes D3D
  107. {L"\\f_sps.dll", 0, 0, TS_IGNORE},
  108. // Korean banking "security" software, crashes randomly
  109. {L"\\t_prevent64.dll", 0, 0, TS_IGNORE},
  110. // Bandicam, doesn't unhook cleanly and freezes preview
  111. // Reference: https://github.com/obsproject/obs-studio/issues/8552
  112. {L"\\bdcam64.dll", 0, 0, TS_IGNORE},
  113. // "Citrix ICAService" that crashes during DShow enumeration
  114. // Reference: https://obsproject.com/forum/threads/165863/
  115. {L"\\ctxdsendpoints64.dll", 0, 0, TS_IGNORE},
  116. // Generic named unity capture filter. Unfortunately only a forked version
  117. // has a critical fix to prevent deadlocks during enumeration. We block
  118. // all versions since if someone didn't change the DLL name they likely
  119. // didn't implement the deadlock fix.
  120. // Reference: https://github.com/schellingb/UnityCapture/commit/2eabf0f
  121. {L"\\unitycapturefilter64bit.dll", 0, 0, TS_IGNORE},
  122. // VSeeFace capture filter < v1.13.38b3 without above fix implemented
  123. {L"\\vseefacecamera64bit.dll", 0, 1666993098, TS_LESS_THAN},
  124. // VTuber Maker capture filter < 2023-03-13 without above fix implemented
  125. {L"\\live3dvirtualcam\\lib64_new2.dll", 0, 1678695956, TS_LESS_THAN},
  126. // Obsolete unfixed versions of VTuber Maker capture filter
  127. {L"\\live3dvirtualcam\\lib64_new.dll", 0, 0, TS_IGNORE},
  128. {L"\\live3dvirtualcam\\lib64.dll", 0, 0, TS_IGNORE},
  129. // VirtualMotionCapture capture filter < 2022-12-18 without above fix
  130. // Reference: https://github.com/obsproject/obs-studio/issues/8552
  131. {L"\\vmc_camerafilter64bit.dll", 0, 1671349891, TS_LESS_THAN},
  132. // HolisticMotionCapture capture filter, not yet patched. Blocking
  133. // all previous versions in case an update is released.
  134. // Reference: https://github.com/obsproject/obs-studio/issues/8552
  135. {L"\\holisticmotioncapturefilter64bit.dll", 0, 1680044549, TS_LESS_THAN},
  136. // Elgato Stream Deck plugin < 2024-02-01
  137. // Blocking all previous versions because they have undefined behavior
  138. // that results in crashes.
  139. // Reference: https://github.com/obsproject/obs-studio/issues/10245
  140. {L"\\streamdeckplugin.dll", 0, 1706745600, TS_LESS_THAN},
  141. };
  142. static bool is_module_blocked(wchar_t *dll, uint32_t timestamp)
  143. {
  144. blocked_module_t *first_allowed = NULL;
  145. size_t len;
  146. len = wcslen(dll);
  147. wcslwr(dll);
  148. // Default behavior is to not block
  149. bool should_block = false;
  150. for (int i = 0; i < _countof(blocked_modules); i++) {
  151. blocked_module_t *b = &blocked_modules[i];
  152. wchar_t *dll_ptr;
  153. if (len >= b->name_len)
  154. dll_ptr = dll + len - b->name_len;
  155. else
  156. dll_ptr = dll;
  157. if (!wcscmp(dll_ptr, b->name)) {
  158. if (b->method == TS_IGNORE) {
  159. b->blocked_count++;
  160. return true;
  161. } else if (b->method == TS_EQUAL && timestamp == b->timestamp) {
  162. b->blocked_count++;
  163. return true;
  164. } else if (b->method == TS_LESS_THAN && timestamp < b->timestamp) {
  165. b->blocked_count++;
  166. return true;
  167. } else if (b->method == TS_GREATER_THAN && timestamp > b->timestamp) {
  168. b->blocked_count++;
  169. return true;
  170. } else if (b->method == TS_ALLOW_ONLY_THIS) {
  171. // Invert default behavior to block if
  172. // we don't find any matching timestamps
  173. // for this DLL.
  174. should_block = true;
  175. if (timestamp == b->timestamp)
  176. return false;
  177. // Bit of a hack to support counting of
  178. // TS_ALLOW_ONLY_THIS blocks as there may
  179. // be multiple entries for the same DLL.
  180. if (!first_allowed)
  181. first_allowed = b;
  182. }
  183. }
  184. }
  185. if (first_allowed)
  186. first_allowed->blocked_count++;
  187. return should_block;
  188. }
  189. static NTSTATUS NtMapViewOfSection_hook(HANDLE SectionHandle, HANDLE ProcessHandle, PVOID *BaseAddress,
  190. ULONG_PTR ZeroBits, SIZE_T CommitSize, PLARGE_INTEGER SectionOffset,
  191. PSIZE_T ViewSize, SECTION_INHERIT InheritDisposition, ULONG AllocationType,
  192. ULONG Win32Protect)
  193. {
  194. SECTION_BASIC_INFORMATION section_information;
  195. wchar_t fileName[MAX_PATH];
  196. SIZE_T wrote = 0;
  197. NTSTATUS ret;
  198. uint32_t timestamp = 0;
  199. ret = ntMap(SectionHandle, ProcessHandle, BaseAddress, ZeroBits, CommitSize, SectionOffset, ViewSize,
  200. InheritDisposition, AllocationType, Win32Protect);
  201. // Verify map and process
  202. if (ret < 0 || ProcessHandle != GetCurrentProcess())
  203. return ret;
  204. // Fetch section information
  205. if (ntQuery(SectionHandle, SectionBasicInformation, &section_information, sizeof(section_information), &wrote) <
  206. 0)
  207. return ret;
  208. // Verify fetch was successful
  209. if (wrote != sizeof(section_information))
  210. return ret;
  211. // We're not interested in non-image maps
  212. if (!(section_information.Attributes & SEC_IMAGE))
  213. return ret;
  214. // Examine the PE header. Perhaps the map is small
  215. // so wrap it in an exception handler in case we
  216. // read past the end of the buffer.
  217. __try {
  218. BYTE *p = (BYTE *)*BaseAddress;
  219. IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)p;
  220. if (dos->e_magic != IMAGE_DOS_SIGNATURE)
  221. return ret;
  222. IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS *)(p + dos->e_lfanew);
  223. if (nt->Signature != IMAGE_NT_SIGNATURE)
  224. return ret;
  225. timestamp = nt->FileHeader.TimeDateStamp;
  226. } __except (EXCEPTION_EXECUTE_HANDLER) {
  227. return ret;
  228. }
  229. // Get the actual filename if possible
  230. if (K32GetMappedFileNameW(ProcessHandle, *BaseAddress, fileName, _countof(fileName)) == 0)
  231. return ret;
  232. if (is_module_blocked(fileName, timestamp)) {
  233. ntUnmap(ProcessHandle, BaseAddress);
  234. ret = STATUS_UNSUCCESSFUL;
  235. }
  236. return ret;
  237. }
  238. void install_dll_blocklist_hook(void)
  239. {
  240. HMODULE nt = GetModuleHandle(L"NTDLL");
  241. if (!nt)
  242. return;
  243. ntMap = (fn_NtMapViewOfSection)GetProcAddress(nt, "NtMapViewOfSection");
  244. if (!ntMap)
  245. return;
  246. ntUnmap = (fn_NtUnmapViewOfSection)GetProcAddress(nt, "NtUnmapViewOfSection");
  247. if (!ntUnmap)
  248. return;
  249. ntQuery = (fn_NtQuerySection)GetProcAddress(nt, "NtQuerySection");
  250. if (!ntQuery)
  251. return;
  252. // Pre-compute length of all DLL names for exact matching
  253. for (int i = 0; i < _countof(blocked_modules); i++) {
  254. blocked_module_t *b = &blocked_modules[i];
  255. b->name_len = wcslen(b->name);
  256. }
  257. DetourTransactionBegin();
  258. if (DetourAttach((PVOID *)&ntMap, NtMapViewOfSection_hook) != NO_ERROR)
  259. DetourTransactionAbort();
  260. else
  261. DetourTransactionCommit();
  262. }
  263. void log_blocked_dlls(void)
  264. {
  265. for (int i = 0; i < _countof(blocked_modules); i++) {
  266. blocked_module_t *b = &blocked_modules[i];
  267. if (b->blocked_count) {
  268. blog(LOG_WARNING, "Blocked loading of '%S' %" PRIu64 " time%S.", b->name, b->blocked_count,
  269. b->blocked_count == 1 ? L"" : L"s");
  270. }
  271. }
  272. }