win-dll-blocklist.c 11 KB

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