using System.Runtime.InteropServices; using System.Runtime.Versioning; using Microsoft.Win32.SafeHandles; namespace XFEToolBox.Client.Core.Search.Service; [SupportedOSPlatform("windows")] internal static class IndexingServiceTrust { internal static bool IsTrustedServer(SafePipeHandle pipe) { if (!GetNamedPipeServerProcessId(pipe, out var pipePid) || pipePid == 0 || !GetNamedPipeServerSessionId(pipe, out var session) || session != 0) return false; // ProcessIdToSessionId opens the SYSTEM process and is denied to ordinary // callers on a correctly protected service. Query the same session-zero // invariant directly from the already connected kernel pipe instead. var manager = OpenSCManagerW(null, null, 0x0001); if (manager == IntPtr.Zero) return false; try { var service = OpenServiceW(manager, IndexingServiceProtocol.ServiceName, 0x0004 | 0x0001); if (service == IntPtr.Zero) return false; try { if (!QueryServiceStatusEx(service, 0, out var state, Marshal.SizeOf(), out _) || state.CurrentState != 4 || state.ProcessId != pipePid) return false; QueryServiceConfigW(service, IntPtr.Zero, 0, out var needed); if (needed is 0 or > 32768) return false; var memory = Marshal.AllocHGlobal((int)needed); try { if (!QueryServiceConfigW(service, memory, needed, out _)) return false; var config = Marshal.PtrToStructure(memory); var account = Marshal.PtrToStringUni(config.StartName); if (!string.Equals(account, "LocalSystem", StringComparison.OrdinalIgnoreCase)) return false; var image = Marshal.PtrToStringUni(config.BinaryPath)?.Trim(); var expected = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "XFEToolBox", "Services", "IndexingService", "XFEToolBox.Indexing.Service.exe"); if (!string.Equals(image?.Trim('"'), expected, StringComparison.OrdinalIgnoreCase)) return false; // Recheck after the configuration query to rule out a stopped/restarted instance. return QueryServiceStatusEx(service, 0, out state, Marshal.SizeOf(), out _) && state.CurrentState == 4 && state.ProcessId == pipePid; } finally { Marshal.FreeHGlobal(memory); } } finally { CloseServiceHandle(service); } } finally { CloseServiceHandle(manager); } } [StructLayout(LayoutKind.Sequential)] private struct ServiceStatusProcess { public uint ServiceType, CurrentState, ControlsAccepted, Win32ExitCode, ServiceSpecificExitCode, CheckPoint, WaitHint, ProcessId, ServiceFlags; } [StructLayout(LayoutKind.Sequential)] private struct ServiceConfig { public uint ServiceType, StartType, ErrorControl; public IntPtr BinaryPath, LoadOrderGroup; public uint TagId; public IntPtr Dependencies, StartName, DisplayName; } [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetNamedPipeServerProcessId(SafePipeHandle pipe, out uint pid); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetNamedPipeServerSessionId(SafePipeHandle pipe, out uint session); [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern IntPtr OpenSCManagerW(string? machine, string? database, uint access); [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern IntPtr OpenServiceW(IntPtr manager, string name, uint access); [DllImport("advapi32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool CloseServiceHandle(IntPtr handle); [DllImport("advapi32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool QueryServiceStatusEx(IntPtr service, int level, out ServiceStatusProcess status, int size, out uint needed); [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool QueryServiceConfigW(IntPtr service, IntPtr config, uint size, out uint needed); }