Untitled
unknown
csharp
3 months ago
2.7 kB
6
Indexable
using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Security.Cryptography.X509Certificates; using IWshRuntimeLibrary; using steamcito.Models; using steamcito.Models.Dtos; using File = System.IO.File; namespace steamcito.Services; public class PathManager { #region WinVerifyTrust P/Invoke private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); private const uint WTD_UI_NONE = 2; private const uint WTD_REVOKE_NONE = 0; private const uint WTD_CHOICE_FILE = 1; private const uint WTD_STATEACTION_IGNORE = 0; private const uint WTD_SAFER_FLAG = 0x100; private const uint WTD_UCHOICE_FILE = 1; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct WinTrustFileInfo { public uint cbStruct; public string pcwszFilePath; public IntPtr hFile; public IntPtr pgKnownSubject; } [StructLayout(LayoutKind.Sequential)] private struct WinTrustData { public uint cbStruct; public IntPtr pPolicyCallbackData; public IntPtr pSIPClientData; public uint dwUIChoice; public uint fdwRevocationChecks; public uint dwUnionChoice; public IntPtr pFileInfo; public uint dwStateAction; public IntPtr hWVTStateData; public IntPtr pwszURLReference; public uint dwProvFlags; public uint dwUIContext; public WinTrustData(WinTrustFileInfo fileInfo) { cbStruct = (uint)Marshal.SizeOf(typeof(WinTrustData)); pPolicyCallbackData = IntPtr.Zero; pSIPClientData = IntPtr.Zero; dwUIChoice = WTD_UI_NONE; fdwRevocationChecks = WTD_REVOKE_NONE; dwUnionChoice = WTD_CHOICE_FILE; pFileInfo = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WinTrustFileInfo))); Marshal.StructureToPtr(fileInfo, pFileInfo, false); dwStateAction = WTD_STATEACTION_IGNORE; hWVTStateData = IntPtr.Zero; pwszURLReference = IntPtr.Zero; dwProvFlags = WTD_SAFER_FLAG; dwUIContext = 0; } public void Dispose() { if (pFileInfo != IntPtr.Zero) { Marshal.FreeHGlobal(pFileInfo); pFileInfo = IntPtr.Zero; } } } [DllImport("wintrust.dll", ExactSpelling = true, SetLastError = false, CharSet = CharSet.Unicode)] private static extern int WinVerifyTrust(IntPtr hwnd, [MarshalAs(UnmanagedType.LPStruct)] Guid pgActionID, WinTrustData pWinTrustData); private static readonly Guid WINTRUST_ACTION_GENERIC_VERIFY_V2 = new Guid("{00AAC56B-CD44-11d0-8CC2-00C04FC295EE}"); private bool WinVerifyTrustCheck(string filePath) { var fileInfo = new WinTrustFileInfo { cbStruct = (uint)Marshal.SizeOf(typeof(WinTrustFileInfo)), pcwszFilePath = filePath, hFile = IntPtr.Zero, pgKnownSubject = IntPtr.Zero }; var trustData = new WinTrustData(fileInfo); try { int result = WinVerifyTrust(INVALID_HANDLE_VALUE, WINTRUST_ACTION_GENERIC_VERIFY_V2, trustData); return result == 0; } catch { return false; } finally { trustData.Dispose(); } } #endregionEditor is loading...
Leave a Comment