1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
| #r "nuget:Detours.Win32Metadata" #r "nuget:Microsoft.Windows.CsWin32" using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.System.Registry; using Detours = Microsoft.Detours.PInvoke;
public partial class HookRegistry : IDisposable { private bool disposed; private static int refCount; private static readonly Dictionary<HKEY, bool> xamlKeyMap = []; private static readonly object locker = new(); private static unsafe delegate* unmanaged[Stdcall]<HKEY, PCWSTR, uint, REG_SAM_FLAGS, HKEY*, WIN32_ERROR> RegOpenKeyExW; private static unsafe delegate* unmanaged[Stdcall]<HKEY, WIN32_ERROR> RegCloseKey; private static unsafe delegate* unmanaged[Stdcall]<HKEY, PCWSTR, uint*, REG_VALUE_TYPE*, byte*, uint*, WIN32_ERROR> RegQueryValueExW; public HookRegistry() { refCount++; StartHook(); } ~HookRegistry() { Dispose(); } public static bool IsHooked { get; private set; } private static unsafe void StartHook() { if (!IsHooked) { using FreeLibrarySafeHandle library = PInvoke.GetModuleHandle("ADVAPI32.dll"); if (!library.IsInvalid && NativeLibrary.TryGetExport(library.DangerousGetHandle(), "RegOpenKeyExW", out nint regOpenKeyExW) && NativeLibrary.TryGetExport(library.DangerousGetHandle(), nameof(PInvoke.RegCloseKey), out nint regCloseKey) && NativeLibrary.TryGetExport(library.DangerousGetHandle(), "RegQueryValueExW", out nint regQueryValueExW)) { void* regOpenKeyExWPtr = (void*)regOpenKeyExW; void* regCloseKeyPtr = (void*)regCloseKey; void* regQueryValueExWPtr = (void*)regQueryValueExW; delegate* unmanaged[Stdcall]<HKEY, PCWSTR, uint, REG_SAM_FLAGS, HKEY*, WIN32_ERROR> overrideRegOpenKeyExW = &OverrideRegOpenKeyExW; delegate* unmanaged[Stdcall]<HKEY, WIN32_ERROR> overrideRegCloseKey = &OverrideRegCloseKey; delegate* unmanaged[Stdcall]<HKEY, PCWSTR, uint*, REG_VALUE_TYPE*, byte*, uint*, WIN32_ERROR> overrideRegQueryValueExW = &OverrideRegQueryValueExW; _ = Detours.DetourRestoreAfterWith(); _ = Detours.DetourTransactionBegin(); _ = Detours.DetourUpdateThread(PInvoke.GetCurrentThread()); _ = Detours.DetourAttach(ref regOpenKeyExWPtr, overrideRegOpenKeyExW); _ = Detours.DetourAttach(ref regCloseKeyPtr, overrideRegCloseKey); _ = Detours.DetourAttach(ref regQueryValueExWPtr, overrideRegQueryValueExW); _ = Detours.DetourTransactionCommit(); RegOpenKeyExW = (delegate* unmanaged[Stdcall]<HKEY, PCWSTR, uint, REG_SAM_FLAGS, HKEY*, WIN32_ERROR>)regOpenKeyExWPtr; RegCloseKey = (delegate* unmanaged[Stdcall]<HKEY, WIN32_ERROR>)regCloseKeyPtr; RegQueryValueExW = (delegate* unmanaged[Stdcall]<HKEY, PCWSTR, uint*, REG_VALUE_TYPE*, byte*, uint*, WIN32_ERROR>)regQueryValueExWPtr; IsHooked = true; } } } public static unsafe void EndHook() { if (--refCount == 0 && IsHooked) { void* regOpenKeyExWPtr = RegOpenKeyExW; void* regCloseKeyPtr = RegCloseKey; void* regQueryValueExWPtr = RegQueryValueExW; delegate* unmanaged[Stdcall]<HKEY, PCWSTR, uint, REG_SAM_FLAGS, HKEY*, WIN32_ERROR> overrideRegOpenKeyExW = &OverrideRegOpenKeyExW; delegate* unmanaged[Stdcall]<HKEY, WIN32_ERROR> overrideRegCloseKey = &OverrideRegCloseKey; delegate* unmanaged[Stdcall]<HKEY, PCWSTR, uint*, REG_VALUE_TYPE*, byte*, uint*, WIN32_ERROR> overrideRegQueryValueExW = &OverrideRegQueryValueExW; _ = Detours.DetourTransactionBegin(); _ = Detours.DetourUpdateThread(PInvoke.GetCurrentThread()); _ = Detours.DetourDetach(®OpenKeyExWPtr, overrideRegOpenKeyExW); _ = Detours.DetourDetach(®CloseKeyPtr, overrideRegCloseKey); _ = Detours.DetourDetach(®QueryValueExWPtr, overrideRegQueryValueExW); _ = Detours.DetourTransactionCommit(); RegOpenKeyExW = null; RegCloseKey = null; RegQueryValueExW = null; IsHooked = false; } } [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static unsafe WIN32_ERROR OverrideRegOpenKeyExW(HKEY hKey, PCWSTR lpSubKey, uint ulOptions, REG_SAM_FLAGS samDesired, HKEY* phkResult) { WIN32_ERROR result = RegOpenKeyExW(hKey, lpSubKey, ulOptions, samDesired, phkResult); if (hKey == HKEY.HKEY_LOCAL_MACHINE && lpSubKey.ToString().Equals(@"Software\Microsoft\WinUI\Xaml", StringComparison.OrdinalIgnoreCase)) { if (result == WIN32_ERROR.ERROR_FILE_NOT_FOUND) { HKEY key = new(HANDLE.INVALID_HANDLE_VALUE); xamlKeyMap[key] = false; *phkResult = key; result = WIN32_ERROR.ERROR_SUCCESS; } else if (result == WIN32_ERROR.ERROR_SUCCESS) { xamlKeyMap[*phkResult] = true; } } return result; } [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static unsafe WIN32_ERROR OverrideRegCloseKey(HKEY hKey) { bool isXamlKey; lock (locker) { if (isXamlKey = xamlKeyMap.TryGetValue(hKey, out bool isRealKey)) { xamlKeyMap.Remove(hKey); } return isXamlKey ? isRealKey ? RegCloseKey(hKey) : WIN32_ERROR.ERROR_SUCCESS : hKey == HANDLE.INVALID_HANDLE_VALUE ? WIN32_ERROR.ERROR_INVALID_HANDLE : RegCloseKey(hKey); } } [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static unsafe WIN32_ERROR OverrideRegQueryValueExW(HKEY hKey, PCWSTR lpValueName, [Optional] uint* lpReserved, [Optional] REG_VALUE_TYPE* lpType, [Optional] byte* lpData, [Optional] uint* lpcbData) { if (lpValueName.Value != default && lpValueName.ToString().Equals("EnableUWPWindow", StringComparison.OrdinalIgnoreCase)) { lock (locker) { if (xamlKeyMap.TryGetValue(hKey, out bool isRealKey)) { WIN32_ERROR result; if (isRealKey) { result = RegQueryValueExW(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); if (result == WIN32_ERROR.ERROR_SUCCESS && lpData != default) { *lpData = 1; } else if (result == WIN32_ERROR.ERROR_FILE_NOT_FOUND) { if (lpData == default && lpcbData != default) { *lpcbData = sizeof(int); result = WIN32_ERROR.ERROR_SUCCESS; } else if (lpData != default && lpcbData != default) { if (*lpcbData >= sizeof(int)) { *lpData = 1; result = WIN32_ERROR.ERROR_SUCCESS; } else { result = WIN32_ERROR.ERROR_MORE_DATA; } } } } else { result = WIN32_ERROR.ERROR_FILE_NOT_FOUND; if (lpData == default && lpcbData != default) { *lpcbData = sizeof(int); result = WIN32_ERROR.ERROR_SUCCESS; } else if (lpData != default && lpcbData != default) { if (*lpcbData >= sizeof(int)) { *lpData = 1; result = WIN32_ERROR.ERROR_SUCCESS; } else { result = WIN32_ERROR.ERROR_MORE_DATA; } } } return result; } } } return RegQueryValueExW(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); } public void Dispose() { if (!disposed && IsHooked) { EndHook(); } GC.SuppressFinalize(this); disposed = true; } }
|