diff --git a/Plugson/src/main_windows.c b/Plugson/src/main_windows.c index e6a0bf47a0f..066eb63bfd9 100644 --- a/Plugson/src/main_windows.c +++ b/Plugson/src/main_windows.c @@ -464,6 +464,60 @@ static int ParseCmdLine(LPSTR lpCmdLine, char *ip, char *port) return 0; } + + +// +//copy from Rufus +// +#include +// For delay-loaded DLLs, use LOAD_LIBRARY_SEARCH_SYSTEM32 to avoid DLL search order hijacking. +FARPROC WINAPI dllDelayLoadHook(unsigned dliNotify, PDelayLoadInfo pdli) +{ + if (dliNotify == dliNotePreLoadLibrary) { + // Windows 7 without KB2533623 does not support the LOAD_LIBRARY_SEARCH_SYSTEM32 flag. + // That is is OK, because the delay load handler will interrupt the NULL return value + // to mean that it should perform a normal LoadLibrary. + return (FARPROC)LoadLibraryExA(pdli->szDll, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); + } + return NULL; +} + +#if defined(_MSC_VER) +// By default the Windows SDK headers have a `const` while MinGW does not. +const +#endif +PfnDliHook __pfnDliNotifyHook2 = dllDelayLoadHook; + +typedef BOOL(WINAPI* SetDefaultDllDirectories_t)(DWORD); +static void DllProtect(void) +{ + SetDefaultDllDirectories_t pfSetDefaultDllDirectories = NULL; + + // Disable loading system DLLs from the current directory (sideloading mitigation) + // PS: You know that official MSDN documentation for SetDllDirectory() that explicitly + // indicates that "If the parameter is an empty string (""), the call removes the current + // directory from the default DLL search order"? Yeah, that doesn't work. At all. + // Still, we invoke it, for platforms where the following call might actually work... + SetDllDirectoryA(""); + + // For libraries on the KnownDLLs list, the system will always load them from System32. + // For other DLLs we link directly to, we can delay load the DLL and use a delay load + // hook to load them from System32. Note that, for this to work, something like: + // 'somelib.dll;%(DelayLoadDLLs)' must be added to the 'Delay Loaded Dlls' option of + // the linker properties in Visual Studio (which means this won't work with MinGW). + // For all other DLLs, use SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32). + // Finally, we need to perform the whole gymkhana below, where we can't call on + // SetDefaultDllDirectories() directly, because Windows 7 doesn't have the API exposed. + // Also, no, Coverity, we never need to care about freeing kernel32 as a library. + // coverity[leaked_storage] + + pfSetDefaultDllDirectories = (SetDefaultDllDirectories_t) + GetProcAddress(LoadLibraryW(L"kernel32.dll"), "SetDefaultDllDirectories"); + if (pfSetDefaultDllDirectories != NULL) + pfSetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32); +} + + int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow) { int rc; @@ -472,6 +526,8 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi UNREFERENCED_PARAMETER(hPrevInstance); + DllProtect(); + if (GetUserDefaultUILanguage() == 0x0804) { g_sysinfo.language = LANGUAGE_CN; diff --git a/Plugson/vs/VentoyPlugson/Release/VentoyPlugson.exe b/Plugson/vs/VentoyPlugson/Release/VentoyPlugson.exe index 4f78da8193b..ed09c29e8ab 100644 Binary files a/Plugson/vs/VentoyPlugson/Release/VentoyPlugson.exe and b/Plugson/vs/VentoyPlugson/Release/VentoyPlugson.exe differ diff --git a/Plugson/vs/VentoyPlugson/VentoyPlugson/VentoyPlugson.vcxproj b/Plugson/vs/VentoyPlugson/VentoyPlugson/VentoyPlugson.vcxproj index 382d989a73a..38daf77d957 100644 --- a/Plugson/vs/VentoyPlugson/VentoyPlugson/VentoyPlugson.vcxproj +++ b/Plugson/vs/VentoyPlugson/VentoyPlugson/VentoyPlugson.vcxproj @@ -14,18 +14,19 @@ {321D6EE2-2AB3-4103-9F05-EC4EC67A75E1} Win32Proj VentoyPlugson + 10.0 Application true - v120 + v142 MultiByte Application false - v120 + v142 true MultiByte @@ -61,6 +62,7 @@ Windows true RequireAdministrator + gdi32.dll;winspool.dll;comdlg32.dll;advapi32.dll;shell32.dll;ole32.dll;oleaut32.dll;uuid.dll;odbc32.dll;odbccp32.dll $(ProjectDir)\Res\Plugson32.manifest %(AdditionalManifestFiles) @@ -84,6 +86,7 @@ true true RequireAdministrator + gdi32.dll;winspool.dll;comdlg32.dll;advapi32.dll;shell32.dll;ole32.dll;oleaut32.dll;uuid.dll;odbc32.dll;odbccp32.dll $(ProjectDir)\Res\Plugson32.manifest %(AdditionalManifestFiles) @@ -151,7 +154,7 @@ - + diff --git a/Plugson/vs/VentoyPlugson/VentoyPlugson/VentoyPlugson.vcxproj.filters b/Plugson/vs/VentoyPlugson/VentoyPlugson/VentoyPlugson.vcxproj.filters index 9f7786610f0..b272f9fe003 100644 --- a/Plugson/vs/VentoyPlugson/VentoyPlugson/VentoyPlugson.vcxproj.filters +++ b/Plugson/vs/VentoyPlugson/VentoyPlugson/VentoyPlugson.vcxproj.filters @@ -167,7 +167,7 @@ - + 资源文件 diff --git a/Vlnk/src/main_windows.c b/Vlnk/src/main_windows.c index 089512026cd..d2f2c3c2b17 100644 --- a/Vlnk/src/main_windows.c +++ b/Vlnk/src/main_windows.c @@ -831,6 +831,58 @@ static int ParseCmdLine(LPSTR lpCmdLine) return argc; } + +// +//copy from Rufus +// +#include +// For delay-loaded DLLs, use LOAD_LIBRARY_SEARCH_SYSTEM32 to avoid DLL search order hijacking. +FARPROC WINAPI dllDelayLoadHook(unsigned dliNotify, PDelayLoadInfo pdli) +{ + if (dliNotify == dliNotePreLoadLibrary) { + // Windows 7 without KB2533623 does not support the LOAD_LIBRARY_SEARCH_SYSTEM32 flag. + // That is is OK, because the delay load handler will interrupt the NULL return value + // to mean that it should perform a normal LoadLibrary. + return (FARPROC)LoadLibraryExA(pdli->szDll, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); + } + return NULL; +} + +#if defined(_MSC_VER) +// By default the Windows SDK headers have a `const` while MinGW does not. +const +#endif +PfnDliHook __pfnDliNotifyHook2 = dllDelayLoadHook; + +typedef BOOL(WINAPI *SetDefaultDllDirectories_t)(DWORD); +static void DllProtect(void) +{ + SetDefaultDllDirectories_t pfSetDefaultDllDirectories = NULL; + + // Disable loading system DLLs from the current directory (sideloading mitigation) + // PS: You know that official MSDN documentation for SetDllDirectory() that explicitly + // indicates that "If the parameter is an empty string (""), the call removes the current + // directory from the default DLL search order"? Yeah, that doesn't work. At all. + // Still, we invoke it, for platforms where the following call might actually work... + SetDllDirectoryA(""); + + // For libraries on the KnownDLLs list, the system will always load them from System32. + // For other DLLs we link directly to, we can delay load the DLL and use a delay load + // hook to load them from System32. Note that, for this to work, something like: + // 'somelib.dll;%(DelayLoadDLLs)' must be added to the 'Delay Loaded Dlls' option of + // the linker properties in Visual Studio (which means this won't work with MinGW). + // For all other DLLs, use SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32). + // Finally, we need to perform the whole gymkhana below, where we can't call on + // SetDefaultDllDirectories() directly, because Windows 7 doesn't have the API exposed. + // Also, no, Coverity, we never need to care about freeing kernel32 as a library. + // coverity[leaked_storage] + + pfSetDefaultDllDirectories = (SetDefaultDllDirectories_t) + GetProcAddress(LoadLibraryW(L"kernel32.dll"), "SetDefaultDllDirectories"); + if (pfSetDefaultDllDirectories != NULL) + pfSetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32); +} + int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow) { DWORD dwAttrib; @@ -838,6 +890,8 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi UNREFERENCED_PARAMETER(hPrevInstance); + DllProtect(); + if (GetUserDefaultUILanguage() == 0x0804) { g_msg_lang = g_msg_cn; diff --git a/Vlnk/vs/VentoyVlnk/Release/VentoyVlnk.exe b/Vlnk/vs/VentoyVlnk/Release/VentoyVlnk.exe index 0f479e1053d..e633e3b11d9 100644 Binary files a/Vlnk/vs/VentoyVlnk/Release/VentoyVlnk.exe and b/Vlnk/vs/VentoyVlnk/Release/VentoyVlnk.exe differ diff --git a/Vlnk/vs/VentoyVlnk/VentoyVlnk/VentoyVlnk.vcxproj b/Vlnk/vs/VentoyVlnk/VentoyVlnk/VentoyVlnk.vcxproj index 62de30c43c4..108c3938fb9 100644 --- a/Vlnk/vs/VentoyVlnk/VentoyVlnk/VentoyVlnk.vcxproj +++ b/Vlnk/vs/VentoyVlnk/VentoyVlnk/VentoyVlnk.vcxproj @@ -14,18 +14,19 @@ {9987D9FE-1A40-4C5F-835C-D66B0FEADA26} Win32Proj VentoyVlnk + 10.0 Application true - v120 + v142 Unicode Application false - v120 + v142 true Unicode @@ -61,6 +62,7 @@ Windows true RequireAdministrator + gdi32.dll;winspool.dll;comdlg32.dll;advapi32.dll;shell32.dll;ole32.dll;oleaut32.dll;uuid.dll;odbc32.dll;odbccp32.dll $(ProjectDir)\Res\Vlnk32.manifest %(AdditionalManifestFiles) @@ -84,6 +86,7 @@ true true RequireAdministrator + gdi32.dll;winspool.dll;comdlg32.dll;advapi32.dll;shell32.dll;ole32.dll;oleaut32.dll;uuid.dll;odbc32.dll;odbccp32.dll $(ProjectDir)\Res\Vlnk32.manifest %(AdditionalManifestFiles)