win-dll-blocklist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. // MainConcept Image Scaler, crashes in its own thread. Block versions
  96. // older than the one Elgato uses (2016-02-15).
  97. {L"\\mc_trans_video_imagescaler.dll", 0, 1455495131, TS_LESS_THAN},
  98. // Weird Polish banking "security" software, breaks UI
  99. {L"\\wslbscr64.dll", 0, 0, TS_IGNORE},
  100. // Various things hooking with EasyHook that probably shouldn't touch OBS
  101. {L"\\easyhook64.dll", 0, 0, TS_IGNORE},
  102. // Ultramon
  103. {L"\\rtsultramonhook.dll", 0, 0, TS_IGNORE},
  104. // HiAlgo Boost, locks up UI
  105. {L"\\hookdll.dll", 0, 0, TS_IGNORE},
  106. // Adobe Core Sync? Crashes NDI.
  107. {L"\\coresync_x64.dll", 0, 0, TS_IGNORE},
  108. // Fasso DRM, crashes D3D
  109. {L"\\f_sps.dll", 0, 0, TS_IGNORE},
  110. // Korean banking "security" software, crashes randomly
  111. {L"\\t_prevent64.dll", 0, 0, TS_IGNORE},
  112. // Bandicam, doesn't unhook cleanly and freezes preview
  113. // Reference: https://github.com/obsproject/obs-studio/issues/8552
  114. {L"\\bdcam64.dll", 0, 0, TS_IGNORE},
  115. // "Citrix ICAService" that crashes during DShow enumeration
  116. // Reference: https://obsproject.com/forum/threads/165863/
  117. {L"\\ctxdsendpoints64.dll", 0, 0, TS_IGNORE},
  118. // Generic named unity capture filter. Unfortunately only a forked version
  119. // has a critical fix to prevent deadlocks during enumeration. We block
  120. // all versions since if someone didn't change the DLL name they likely
  121. // didn't implement the deadlock fix.
  122. // Reference: https://github.com/schellingb/UnityCapture/commit/2eabf0f
  123. {L"\\unitycapturefilter64bit.dll", 0, 0, TS_IGNORE},
  124. // VSeeFace capture filter < v1.13.38b3 without above fix implemented
  125. {L"\\vseefacecamera64bit.dll", 0, 1666993098, TS_LESS_THAN},
  126. // VTuber Maker capture filter < 2023-03-13 without above fix implemented
  127. {L"\\live3dvirtualcam\\lib64_new2.dll", 0, 1678695956, TS_LESS_THAN},
  128. // Obsolete unfixed versions of VTuber Maker capture filter
  129. {L"\\live3dvirtualcam\\lib64_new.dll", 0, 0, TS_IGNORE},
  130. {L"\\live3dvirtualcam\\lib64.dll", 0, 0, TS_IGNORE},
  131. // VirtualMotionCapture capture filter < 2022-12-18 without above fix
  132. // Reference: https://github.com/obsproject/obs-studio/issues/8552
  133. {L"\\vmc_camerafilter64bit.dll", 0, 1671349891, TS_LESS_THAN},
  134. // HolisticMotionCapture capture filter, not yet patched. Blocking
  135. // all previous versions in case an update is released.
  136. // Reference: https://github.com/obsproject/obs-studio/issues/8552
  137. {L"\\holisticmotioncapturefilter64bit.dll", 0, 1680044549,
  138. TS_LESS_THAN},
  139. };
  140. static bool is_module_blocked(wchar_t *dll, uint32_t timestamp)
  141. {
  142. blocked_module_t *first_allowed = NULL;
  143. size_t len;
  144. len = wcslen(dll);
  145. wcslwr(dll);
  146. // Default behavior is to not block
  147. bool should_block = false;
  148. for (int i = 0; i < _countof(blocked_modules); i++) {
  149. blocked_module_t *b = &blocked_modules[i];
  150. wchar_t *dll_ptr;
  151. if (len >= b->name_len)
  152. dll_ptr = dll + len - b->name_len;
  153. else
  154. dll_ptr = dll;
  155. if (!wcscmp(dll_ptr, b->name)) {
  156. if (b->method == TS_IGNORE) {
  157. b->blocked_count++;
  158. return true;
  159. } else if (b->method == TS_EQUAL &&
  160. timestamp == b->timestamp) {
  161. b->blocked_count++;
  162. return true;
  163. } else if (b->method == TS_LESS_THAN &&
  164. timestamp < b->timestamp) {
  165. b->blocked_count++;
  166. return true;
  167. } else if (b->method == TS_GREATER_THAN &&
  168. timestamp > b->timestamp) {
  169. b->blocked_count++;
  170. return true;
  171. } else if (b->method == TS_ALLOW_ONLY_THIS) {
  172. // Invert default behavior to block if
  173. // we don't find any matching timestamps
  174. // for this DLL.
  175. should_block = true;
  176. if (timestamp == b->timestamp)
  177. return false;
  178. // Bit of a hack to support counting of
  179. // TS_ALLOW_ONLY_THIS blocks as there may
  180. // be multiple entries for the same DLL.
  181. if (!first_allowed)
  182. first_allowed = b;
  183. }
  184. }
  185. }
  186. if (first_allowed)
  187. first_allowed->blocked_count++;
  188. return should_block;
  189. }
  190. static NTSTATUS
  191. NtMapViewOfSection_hook(HANDLE SectionHandle, HANDLE ProcessHandle,
  192. PVOID *BaseAddress, ULONG_PTR ZeroBits,
  193. SIZE_T CommitSize, PLARGE_INTEGER SectionOffset,
  194. PSIZE_T ViewSize, SECTION_INHERIT InheritDisposition,
  195. ULONG AllocationType, ULONG Win32Protect)
  196. {
  197. SECTION_BASIC_INFORMATION section_information;
  198. wchar_t fileName[MAX_PATH];
  199. SIZE_T wrote = 0;
  200. NTSTATUS ret;
  201. uint32_t timestamp = 0;
  202. ret = ntMap(SectionHandle, ProcessHandle, BaseAddress, ZeroBits,
  203. CommitSize, SectionOffset, ViewSize, InheritDisposition,
  204. 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,
  210. &section_information, sizeof(section_information),
  211. &wrote) < 0)
  212. return ret;
  213. // Verify fetch was successful
  214. if (wrote != sizeof(section_information))
  215. return ret;
  216. // We're not interested in non-image maps
  217. if (!(section_information.Attributes & SEC_IMAGE))
  218. return ret;
  219. // Examine the PE header. Perhaps the map is small
  220. // so wrap it in an exception handler in case we
  221. // read past the end of the buffer.
  222. __try {
  223. BYTE *p = (BYTE *)*BaseAddress;
  224. IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)p;
  225. if (dos->e_magic != IMAGE_DOS_SIGNATURE)
  226. return ret;
  227. IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS *)(p + dos->e_lfanew);
  228. if (nt->Signature != IMAGE_NT_SIGNATURE)
  229. return ret;
  230. timestamp = nt->FileHeader.TimeDateStamp;
  231. } __except (EXCEPTION_EXECUTE_HANDLER) {
  232. return ret;
  233. }
  234. // Get the actual filename if possible
  235. if (K32GetMappedFileNameW(ProcessHandle, *BaseAddress, fileName,
  236. _countof(fileName)) == 0)
  237. return ret;
  238. if (is_module_blocked(fileName, timestamp)) {
  239. ntUnmap(ProcessHandle, BaseAddress);
  240. ret = STATUS_UNSUCCESSFUL;
  241. }
  242. return ret;
  243. }
  244. void install_dll_blocklist_hook(void)
  245. {
  246. HMODULE nt = GetModuleHandle(L"NTDLL");
  247. if (!nt)
  248. return;
  249. ntMap = (fn_NtMapViewOfSection)GetProcAddress(nt, "NtMapViewOfSection");
  250. if (!ntMap)
  251. return;
  252. ntUnmap = (fn_NtUnmapViewOfSection)GetProcAddress(
  253. nt, "NtUnmapViewOfSection");
  254. if (!ntUnmap)
  255. return;
  256. ntQuery = (fn_NtQuerySection)GetProcAddress(nt, "NtQuerySection");
  257. if (!ntQuery)
  258. return;
  259. // Pre-compute length of all DLL names for exact matching
  260. for (int i = 0; i < _countof(blocked_modules); i++) {
  261. blocked_module_t *b = &blocked_modules[i];
  262. b->name_len = wcslen(b->name);
  263. }
  264. DetourTransactionBegin();
  265. if (DetourAttach((PVOID *)&ntMap, NtMapViewOfSection_hook) != NO_ERROR)
  266. DetourTransactionAbort();
  267. else
  268. DetourTransactionCommit();
  269. }
  270. void log_blocked_dlls(void)
  271. {
  272. for (int i = 0; i < _countof(blocked_modules); i++) {
  273. blocked_module_t *b = &blocked_modules[i];
  274. if (b->blocked_count) {
  275. blog(LOG_WARNING,
  276. "Blocked loading of '%S' %" PRIu64 " time%S.",
  277. b->name, b->blocked_count,
  278. b->blocked_count == 1 ? L"" : L"s");
  279. }
  280. }
  281. }