From 50b10ba0bf61fc5d30f033e239b5a81ac478ec78 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 14 Aug 2016 21:55:59 +0300 Subject: [PATCH] Dll hijacking fixes: Windows version check + SetDllDirectory(""). --- .../SourceFiles/platform/win/windows_dlls.cpp | 46 ++++++++++++++----- .../SourceFiles/platform/win/windows_dlls.h | 6 +++ Telegram/SourceFiles/pspecific_win.cpp | 1 + 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/Telegram/SourceFiles/platform/win/windows_dlls.cpp b/Telegram/SourceFiles/platform/win/windows_dlls.cpp index 048aed9291..299136f315 100644 --- a/Telegram/SourceFiles/platform/win/windows_dlls.cpp +++ b/Telegram/SourceFiles/platform/win/windows_dlls.cpp @@ -24,6 +24,23 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org namespace Platform { namespace Dlls { +f_SetDllDirectory SetDllDirectory; + +HINSTANCE LibKernel32; + +void init() { + static bool inited = false; + if (inited) return; + inited = true; + + LibKernel32 = LoadLibrary(L"KERNEL32.DLL"); + load(LibKernel32, "SetDllDirectoryW", SetDllDirectory); + if (SetDllDirectory) { + // Remove the current directory from the DLL search order. + SetDllDirectory(L""); + } +} + f_SetWindowTheme SetWindowTheme; f_OpenAs_RunDLL OpenAs_RunDLL; f_SHOpenWithDialog SHOpenWithDialog; @@ -45,8 +62,7 @@ HINSTANCE LibPropSys; HINSTANCE LibComBase; void start() { - LibUxTheme = LoadLibrary(L"UXTHEME.DLL"); - load(LibUxTheme, "SetWindowTheme", SetWindowTheme); + init(); LibShell32 = LoadLibrary(L"SHELL32.DLL"); load(LibShell32, "SHAssocEnumHandlers", SHAssocEnumHandlers); @@ -56,17 +72,25 @@ void start() { load(LibShell32, "SHQueryUserNotificationState", SHQueryUserNotificationState); load(LibShell32, "SetCurrentProcessExplicitAppUserModelID", SetCurrentProcessExplicitAppUserModelID); - LibWtsApi32 = LoadLibrary(L"WTSAPI32.DLL"); - load(LibWtsApi32, "WTSRegisterSessionNotification", WTSRegisterSessionNotification); - load(LibWtsApi32, "WTSUnRegisterSessionNotification", WTSUnRegisterSessionNotification); + auto version = QSysInfo::windowsVersion(); + if (version >= QSysInfo::WV_VISTA) { + LibUxTheme = LoadLibrary(L"UXTHEME.DLL"); + load(LibUxTheme, "SetWindowTheme", SetWindowTheme); - LibPropSys = LoadLibrary(L"PROPSYS.DLL"); - load(LibPropSys, "PropVariantToString", PropVariantToString); + LibWtsApi32 = LoadLibrary(L"WTSAPI32.DLL"); + load(LibWtsApi32, "WTSRegisterSessionNotification", WTSRegisterSessionNotification); + load(LibWtsApi32, "WTSUnRegisterSessionNotification", WTSUnRegisterSessionNotification); - LibComBase = LoadLibrary(L"COMBASE.DLL"); - load(LibComBase, "RoGetActivationFactory", RoGetActivationFactory); - load(LibComBase, "WindowsCreateStringReference", WindowsCreateStringReference); - load(LibComBase, "WindowsDeleteString", WindowsDeleteString); + LibPropSys = LoadLibrary(L"PROPSYS.DLL"); + load(LibPropSys, "PropVariantToString", PropVariantToString); + + if (version >= QSysInfo::WV_WINDOWS8) { + LibComBase = LoadLibrary(L"COMBASE.DLL"); + load(LibComBase, "RoGetActivationFactory", RoGetActivationFactory); + load(LibComBase, "WindowsCreateStringReference", WindowsCreateStringReference); + load(LibComBase, "WindowsDeleteString", WindowsDeleteString); + } + } } } // namespace Dlls diff --git a/Telegram/SourceFiles/platform/win/windows_dlls.h b/Telegram/SourceFiles/platform/win/windows_dlls.h index 0e684d46c3..27a82d5029 100644 --- a/Telegram/SourceFiles/platform/win/windows_dlls.h +++ b/Telegram/SourceFiles/platform/win/windows_dlls.h @@ -27,6 +27,12 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org namespace Platform { namespace Dlls { +void init(); + +// KERNEL32.DLL +typedef BOOL (FAR STDAPICALLTYPE *f_SetDllDirectory)(LPCWSTR lpPathName); +extern f_SetDllDirectory SetDllDirectory; + void start(); template diff --git a/Telegram/SourceFiles/pspecific_win.cpp b/Telegram/SourceFiles/pspecific_win.cpp index c1215a59e6..636db7fddc 100644 --- a/Telegram/SourceFiles/pspecific_win.cpp +++ b/Telegram/SourceFiles/pspecific_win.cpp @@ -737,6 +737,7 @@ void psShowInFolder(const QString &name) { namespace Platform { void start() { + Dlls::init(); } void finish() {