| 3729 | } |
| 3730 | |
| 3731 | void HookAPIForVirtualFiles() |
| 3732 | { |
| 3733 | static bool virtualFileHooksActive = false; |
| 3734 | bool shouldHaveHooks = HasVirtualFiles(); |
| 3735 | |
| 3736 | if (!virtualFileHooksActive && shouldHaveHooks) |
| 3737 | { |
| 3738 | std::wstring mutexName = L"Ultimate-ASI-Loader-VirtualFiles" + std::to_wstring(GetCurrentProcessId()); |
| 3739 | |
| 3740 | auto hVirtualFilesMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, mutexName.c_str()); |
| 3741 | if (hVirtualFilesMutex) |
| 3742 | { |
| 3743 | CloseHandle(hVirtualFilesMutex); |
| 3744 | hVirtualFilesMutex = nullptr; |
| 3745 | return; |
| 3746 | } |
| 3747 | |
| 3748 | hVirtualFilesMutex = CreateMutexW(nullptr, TRUE, mutexName.c_str()); |
| 3749 | if (!hVirtualFilesMutex || GetLastError() == ERROR_ALREADY_EXISTS) |
| 3750 | { |
| 3751 | if (hVirtualFilesMutex) CloseHandle(hVirtualFilesMutex); |
| 3752 | hVirtualFilesMutex = nullptr; |
| 3753 | return; |
| 3754 | } |
| 3755 | |
| 3756 | std::vector<std::tuple<std::wstring, std::unique_ptr<FunctionHookMinHook>&, std::string, uintptr_t>> hookFunctions = |
| 3757 | { |
| 3758 | { L"KernelBase.dll", mhCreateFileA, "CreateFileA", (uintptr_t)shCustomCreateFileA, }, |
| 3759 | { L"KernelBase.dll", mhCreateFileW, "CreateFileW", (uintptr_t)shCustomCreateFileW, }, |
| 3760 | { L"KernelBase.dll", mhCreateFile2, "CreateFile2", (uintptr_t)shCustomCreateFile2, }, |
| 3761 | { L"KernelBase.dll", mhCreateFile3, "CreateFile3", (uintptr_t)shCustomCreateFile3, }, |
| 3762 | { L"KernelBase.dll", mhGetFileAttributesA, "GetFileAttributesA", (uintptr_t)shCustomGetFileAttributesA, }, |
| 3763 | { L"KernelBase.dll", mhGetFileAttributesW, "GetFileAttributesW", (uintptr_t)shCustomGetFileAttributesW, }, |
| 3764 | { L"KernelBase.dll", mhGetFileAttributesExA, "GetFileAttributesExA", (uintptr_t)shCustomGetFileAttributesExA, }, |
| 3765 | { L"KernelBase.dll", mhGetFileAttributesExW, "GetFileAttributesExW", (uintptr_t)shCustomGetFileAttributesExW, }, |
| 3766 | { L"KernelBase.dll", mhReadFile, "ReadFile", (uintptr_t)shCustomReadFile, }, |
| 3767 | { L"KernelBase.dll", mhReadFileEx, "ReadFileEx", (uintptr_t)shCustomReadFileEx, }, |
| 3768 | { L"KernelBase.dll", mhGetFileSize, "GetFileSize", (uintptr_t)shCustomGetFileSize, }, |
| 3769 | { L"KernelBase.dll", mhGetFileSizeEx, "GetFileSizeEx", (uintptr_t)shCustomGetFileSizeEx, }, |
| 3770 | { L"KernelBase.dll", mhSetFilePointer, "SetFilePointer", (uintptr_t)shCustomSetFilePointer, }, |
| 3771 | { L"KernelBase.dll", mhSetFilePointerEx, "SetFilePointerEx", (uintptr_t)shCustomSetFilePointerEx, }, |
| 3772 | { L"KernelBase.dll", mhCloseHandle, "CloseHandle", (uintptr_t)shCustomCloseHandle, }, |
| 3773 | { L"KernelBase.dll", mhGetFileInformationByHandle, "GetFileInformationByHandle", (uintptr_t)shCustomGetFileInformationByHandle, }, |
| 3774 | { L"KernelBase.dll", mhGetFileInformationByHandleEx, "GetFileInformationByHandleEx", (uintptr_t)shCustomGetFileInformationByHandleEx, }, |
| 3775 | { L"KernelBase.dll", mhGetFileType, "GetFileType", (uintptr_t)shCustomGetFileType, }, |
| 3776 | }; |
| 3777 | |
| 3778 | if (shouldHaveHooks && !virtualFileHooksActive) |
| 3779 | { |
| 3780 | for (auto& [dllName, hookVar, funcName, customHook] : hookFunctions) |
| 3781 | { |
| 3782 | if (auto pDll = GetModuleHandleW(dllName.c_str())) |
| 3783 | { |
| 3784 | if (!hookVar) |
| 3785 | { |
| 3786 | if (auto pFunc = (uintptr_t)GetProcAddress(pDll, funcName.c_str())) |
| 3787 | { |
| 3788 | hookVar = std::make_unique<FunctionHookMinHook>(pFunc, customHook); |
no test coverage detected