Skip to content

Commit

Permalink
Fix for Win10 ShellExecuteW in Windows.Storage.dll
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMangan committed Jun 7, 2024
1 parent 773c7a0 commit c0cd8ed
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,4 @@ tests/scenarios/Appx/MFRIlvAwareTests.appx.signtool.txt
tests/scenarios/Appx/MFRIlvAwareTests.appx.makeappx.txt
tests/scenarios/Appx/MFRTests.appx.makeappx.txt
.gitignore
/.VSCodeCounter/2024-03-24_08-46-39
4 changes: 4 additions & 0 deletions Notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ ideas:
as an extra xxx.yyy.deleted file in the redirection area. But then all methods looking to see files would have to look for this, and anything creating a file might have to remove it.
Needs more thought on finding a way to distinguish.

v.2024.next:
* Prevent crash due to downrev Windows.storage.dll Detouring.


v.2024.03.04:
* MFR: Add support for SetCurrentDirectory ILV-Aware mode to force path to be a
package path when appropriate. When an app uses a file-picker dialog (which currently bypasses MFR), it might cause the app
Expand Down
4 changes: 2 additions & 2 deletions PsfRuntime/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ void attach()
check_win32(::DetourUpdateThread(::GetCurrentThread()));

#if _DEBUG
//Log("Debug: PsfRuntime before attach all");
Log("Debug: PsfRuntime before attach all");
#endif
// Call DetourAttach for all APIs that PsfRuntime detours
psf::attach_all();
#if _DEBUG
//Log("DEBUG: PsfRuntime after attach all");
Log("DEBUG: PsfRuntime after attach all");
#endif
// We can't call LoadLibrary in DllMain, so hook the application's entry point and do initialization then
ApplicationEntryPoint = reinterpret_cast<EntryPoint_t>(::DetourGetEntryPoint(nullptr));
Expand Down
12 changes: 11 additions & 1 deletion fixups/MFRFixup/FunctionImplementations_WindowsStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ inline Func GetWindowsStorageDllInternalFunction(const char* functionName)
Log(L">>>WindowsStorage Fixup mistaken loaded name=??? 0x%x", result);
}
#endif
assert(result);
/////assert(result);
if (result == NULL)
{
Log(L"Failed to locate exported function name=%S from WindowsStorage.dll, possibly not supported on this OS.", functionName);
}
else
{
#if _DEBUG
Log(L"Located exported function name=%S from WindowsStorage.dll at 0x%x", functionName, result);
#endif
}
return result;
}
#define WINDOWSSTORAGE_FUNCTION(Name) (GetWindowsStorageDllInternalFunction<decltype(&Name)>(#Name));
Expand Down
2 changes: 1 addition & 1 deletion fixups/MFRFixup/InitializeMFRFixup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ std::filesystem::path g_short_finalPackageRootPath;


#if _DEBUG
//#define MOREDEBUG 1
#define MOREDEBUG 1
#endif

DWORD g_InterceptInstance = 60000;
Expand Down
30 changes: 23 additions & 7 deletions fixups/MFRFixup/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <windows.h>
#endif

/////#define PSF_DEFINE_EXPORTS
#include <psf_framework.h>
#include <psf_logging.h>

Expand All @@ -19,6 +20,9 @@
#if DEBUG_NEW_FIXUPS
#define DEBUG_NEW_FIXUPS_MFR 1
#endif
#else
//#define MOREDEBUG 1
#define DEBUG_NEW_FIXUPS_MFR 1
#endif

bool trace_function_entry = false;
Expand All @@ -31,7 +35,7 @@ void InitializeConfiguration();

extern "C" {

#if _DEBUG
#if MOREDEBUG
#if DEBUG_NEW_FIXUPS_MFR
void PrintDebugAddrs()
{
Expand Down Expand Up @@ -87,18 +91,30 @@ extern "C" {

#if Intercept_WindowsStorage
#if DO_WS_Shex_A
Log("(windows.storage)ShellExecute to Ansi=%p\n", windowsstorageimpl::ShellExecuteAImpl);
if (windowsstorageimpl::ShellExecuteAImpl == nullptr)
Log("(windows.storage)ShellExecuteA to Ansi=NULL\n");
else
Log("(windows.storage)ShellExecuteA to Ansi=%p\n", windowsstorageimpl::ShellExecuteAImpl);
#endif
#if DO_WS_Shex_W
Log("(windows.storage)ShellExecute to Wide=%p\n", windowsstorageimpl::ShellExecuteWImpl);
if (windowsstorageimpl::ShellExecuteWImpl == nullptr)
Log("(windows.storage)ShellExecuteW to Wide=NULL\n");
else
Log("(windows.storage)ShellExecuteW to Wide=%p\n", windowsstorageimpl::ShellExecuteWImpl);
#endif
#if DO_WS_ShexEx_A
Log("(windows.storage)ShellExecuteExA Ansi=%p\n", &windowsstorageimpl::ShellExecuteExAImpl);
if (windowsstorageimpl::ShellExecuteExAImpl == nullptr)
Log("(windows.storage)ShellExecuteExA to Ansi=NULL\n");
else
Log("(windows.storage)ShellExecuteExA Ansi=%p\n", &windowsstorageimpl::ShellExecuteExAImpl);
#endif
#if DO_WS_ShexEx_W
Log("(windows.storage)ShellExecuteExW Wide=%p\n", &windowsstorageimpl::ShellExecuteExWImpl);
if (windowsstorageimpl::ShellExecuteExWImpl == nullptr)
Log("(windows.storage)ShellExecuteExW to Wide=NULL\n");
else
Log("(windows.storage)ShellExecuteExW Wide=%p\n", &windowsstorageimpl::ShellExecuteExWImpl);
#endif

Log("WindowsStorage Fixups loaded.\n");
#endif
}
#endif
Expand Down Expand Up @@ -143,7 +159,7 @@ extern "C" {
manual_wait_for_debugger();
#endif

#if _DEBUG
#if MOREDEBUG
PrintDebugAddrs();
psf::attach_count_all_debug();
//Log(L"[0] MFRFixup attaches %d fixups.", count);
Expand Down
84 changes: 58 additions & 26 deletions include/psf_framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
//-------------------------------------------------------------------------------------------------------
#pragma once

#if _DEBUG
#define MOREDEBUG 1
#else
//#define MOREDEBUG 1
#endif

#define DEBUG_NEW_FIXUPS 1

#include <algorithm>
Expand Down Expand Up @@ -66,13 +72,21 @@ namespace psf
{
if (target && !target->Registered)
{
check_win32(::PSFRegister(&target->Target, target->Detour));
target->Registered = true;
if (target->Target != nullptr)
{
check_win32(::PSFRegister(&target->Target, target->Detour));
target->Registered = true;
}
else
{
;
}
}
});
}

#if _DEBUG
//#if _DEBUG
#if MOREDEBUG
inline int attach_count_all()
{
int count = 0;
Expand Down Expand Up @@ -127,34 +141,45 @@ namespace psf
{
if (target && !target->Registered)
{
#if _DEBUG
#if DEBUG_NEW_FIXUPS
pvtLog(L">>>>>Register FIXUP from 0x0%x to 0x0%x", target->Target, target->Detour);
if (target->Target == (void*)0x743d77a0) //ws_ShellExecuteExW
if (target->Target != nullptr)
{
#if MOREDEBUG
#if DEBUG_NEW_FIXUPS
pvtLog(L">>>>>Register FIXUP from 0x0%x to 0x0%x", target->Target, target->Detour);
if (target->Target == (void*)0x743d77a0) //ws_ShellExecuteExW
{
pvtLog(L">>>>>Registering (WS)ShellExecuteExW");
}
if (target->Target == (void*)0x743d78e0) //ws_ShellExecuteW
{
pvtLog(L">>>>>Registering (WS)ShellExecuteW");
}
if (target->Target == (void*)0x75cac880) // KernelBase MoveFileExW
{
pvtLog(L">>>>>Registering (KernelBase)MoveFileExW");
}
if (target->Target == (void*)0x77306a40) // ntdll ZwQueryDirectoryFile
{
pvtLog(L">>>>>Registering (ntdll)ZwQueryDirectoryFile");
}
}
if (target->Target == (void*)0x743d78e0) //ws_ShellExecuteW
{
pvtLog(L">>>>>Registering (WS)ShellExecuteW");
}
if (target->Target == (void*)0x75cac880) // KernelBase MoveFileExW
{
pvtLog(L">>>>>Registering (KernelBase)MoveFileExW");
}
if (target->Target == (void*)0x77306a40) // ntdll ZwQueryDirectoryFile
{
pvtLog(L">>>>>Registering (ntdll)ZwQueryDirectoryFile");
}
#endif
#endif
check_win32(::PSFRegister(&target->Target, target->Detour));
target->Registered = true;
#if _DEBUG
check_win32(::PSFRegister(&target->Target, target->Detour));
target->Registered = true;
#if MOREDEBUG
#if DEBUG_NEW_FIXUPS
pvtLog(L"<<<<<Registered FIXUP Complete.\n");
#endif
#endif
}
else
{
#if MOREDEBUG
#if DEBUG_NEW_FIXUPS
pvtLog(L"<<<<<Registered FIXUP Complete.\n");
pvtLog(L">>>>>Register FIXUP was null target, skipped.\n");
#endif
#endif
}
}
});
return;
Expand Down Expand Up @@ -246,9 +271,16 @@ namespace psf
template <typename AnsiFunc, typename WideFunc>
inline auto detoured_string_function(AnsiFunc ansi, WideFunc wide)
{
#if _DEBUG
#if MOREDEBUG
#if DEBUG_NEW_FIXUPS
pvtLog(L">>>>>Define FIXUP pair Ansi=0x0%p Wide=0x0%p\n", ansi, wide);
if (ansi != nullptr && wide != nullptr)
{
pvtLog(L">>>>>Define FIXUP pair Ansi=0x0%p Wide=0x0%p\n", ansi, wide);
}
else
{
pvtLog(L">>>>>Define FIXUP null pair\n");
}
#endif
#endif
return detoured_string_function_t<AnsiFunc, WideFunc>{ ansi, wide };
Expand Down

0 comments on commit c0cd8ed

Please sign in to comment.