/* * Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved. * * This file is part of System Informer. * * Authors: * * jxy-s * */ namespace CustomBuildTool { /// /// Provides dynamic configuration generation for System Informer kernel and user-mode components. /// Handles manifest parsing and config file creation. /// public static class DynData { /// /// The autogenerated file header for output files. /// private const string FileHeader = @"/* * Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved. * * This file is part of System Informer. * * THIS IS AN AUTOGENERATED FILE, DO NOT MODIFY * */"; /// /// The required include directive for generated C files. /// private const string Includes = @"#include "; /// /// The dynamic configuration version. /// private const UInt32 Version = 16; /// /// The public key used for session token validation. /// private static readonly byte[] SessionTokenPublicKey = [ 0x52, 0x53, 0x41, 0x31, 0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xDE, 0x4C, 0x64, 0xCA, 0x4E, 0xEA, 0x1C, 0x06, 0xBD, 0x40, 0x42, 0x46, 0x3A, 0x80, 0x2E, 0xB1, 0x13, 0xCE, 0x53, 0x6B, 0x00, 0xC0, 0x53, 0x79, 0x54, 0xB2, 0xF3, 0xC2, 0xAD, 0x21, 0xFD, 0xB4, 0x06, 0xB2, 0xA7, 0x8F, 0x75, 0xC4, 0xF2, 0xC9, 0x6B, 0x30, 0x7E, 0x46, 0x48, 0x70, 0x58, 0x38, 0x26, 0x14, 0x63, 0xC9, 0xB7, 0xB6, 0x28, 0x90, 0xA5, 0x3E, 0x03, 0xFC, 0x30, 0xD7, 0xE0, 0xF9, 0x06, 0xC1, 0x08, 0x5A, 0x7A, 0x1D, 0x64, 0x2B, 0x2C, 0x21, 0x25, 0x4D, 0x97, 0x22, 0x66, 0xD6, 0x2B, 0x16, 0x4B, 0x83, 0xA7, 0x00, 0x40, 0xFF, 0xD4, 0xB3, 0xB9, 0xED, 0x6B, 0x11, 0x89, 0x87, 0x7B, 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05, 0x7C, 0xD7, 0x11, 0x15, 0x45, 0xD4, 0x19, 0xE1, 0xF4, 0x23, 0xD9, 0x9D, 0xB0, 0x80, 0x19, 0xB8, 0x79, 0x20, 0x8C, 0xCB, 0xA3, 0xE3, 0x53, 0x4E, 0x8B, 0x2E, 0xC1, 0x9C, 0x0A, 0x53, 0x1A, 0x14, 0x65, 0x71, 0xC4, 0x66, 0x4A, 0x82, 0x8A, 0xF3, 0x67, 0x50, 0xFA, 0xB7, 0x3A, 0x25, 0x61 ]; /// /// Caches FieldInfo for to optimize reflection performance. /// private static readonly FrozenDictionary DynFieldsKernelFieldCache = typeof(DynFieldsKernel).GetFields().ToFrozenDictionary(F => F.Name, F => F, StringComparer.OrdinalIgnoreCase); /// /// Caches FieldInfo for to optimize reflection performance. /// private static readonly FrozenDictionary DynFieldsLxcoreFieldCache = typeof(DynFieldsLxcore).GetFields().ToFrozenDictionary(F => F.Name, F => F, StringComparer.OrdinalIgnoreCase); /// /// Represents the supported kernel class types. /// private enum ClassType : ushort { /// /// Windows kernel (ntoskrnl.exe). /// Ntoskrnl = 0, /// /// Alternate Windows kernel (ntkrla57.exe). /// Ntkrla57 = 1, /// /// Linux core (lxcore.sys). /// Lxcore = 2, }; /// /// Maps a file name to its corresponding . /// /// The file name. /// The matching . private static ClassType ClassFromString(string Input) { return Input switch { "ntoskrnl.exe" => ClassType.Ntoskrnl, "ntkrla57.exe" => ClassType.Ntkrla57, "lxcore.sys" => ClassType.Lxcore, _ => throw new Exception($"invalid file name {Input}") }; } /// /// Maps an architecture string to its corresponding machine code. /// /// The architecture name. /// The machine code. private static UInt16 MachineFromString(string Input) { return Input switch { "amd64" => 0x8664, "arm64" => 0xAA64, _ => throw new Exception($"invalid machine {Input}") }; } /// /// The C header content for dynamic configuration, including structure definitions and macros. /// private static readonly string DynConfigC = $$""" #define KPH_DYN_CONFIGURATION_VERSION ((ULONG){{DynData.Version}}) #define KPH_DYN_SESSION_TOKEN_PUBLIC_KEY_LENGTH ((ULONG){{DynData.SessionTokenPublicKey.Length}}) #define KPH_DYN_CLASS_NTOSKRNL ((USHORT){{(UInt16)ClassType.Ntoskrnl}}) #define KPH_DYN_CLASS_NTKRLA57 ((USHORT){{(UInt16)ClassType.Ntkrla57}}) #define KPH_DYN_CLASS_LXCORE ((USHORT){{(UInt16)ClassType.Lxcore}}) #include typedef struct _KPH_DYN_KERNEL_FIELDS { USHORT EgeGuid; // dt nt!_ETW_GUID_ENTRY Guid USHORT EpObjectTable; // dt nt!_EPROCESS ObjectTable USHORT EreGuidEntry; // dt nt!_ETW_REG_ENTRY GuidEntry USHORT HtHandleContentionEvent; // dt nt!_HANDLE_TABLE HandleContentionEvent USHORT OtName; // dt nt!_OBJECT_TYPE Name USHORT OtIndex; // dt nt!_OBJECT_TYPE Index USHORT ObDecodeShift; // dt nt!_HANDLE_TABLE_ENTRY ObjectPointerBits USHORT ObAttributesShift; // dt nt!_HANDLE_TABLE_ENTRY Attributes USHORT AlpcCommunicationInfo; // dt nt!_ALPC_PORT CommunicationInfo USHORT AlpcOwnerProcess; // dt nt!_ALPC_PORT OwnerProcess USHORT AlpcConnectionPort; // dt nt!_ALPC_COMMUNICATION_INFO ConnectionPort USHORT AlpcServerCommunicationPort; // dt nt!_ALPC_COMMUNICATION_INFO ServerCommunicationPort USHORT AlpcClientCommunicationPort; // dt nt!_ALPC_COMMUNICATION_INFO ClientCommunicationPort USHORT AlpcHandleTable; // dt nt!_ALPC_COMMUNICATION_INFO HandleTable USHORT AlpcHandleTableLock; // dt nt!_ALPC_HANDLE_TABLE Lock USHORT AlpcAttributes; // dt nt!_ALPC_PORT PortAttributes USHORT AlpcAttributesFlags; // dt nt!_ALPC_PORT_ATTRIBUTES Flags USHORT AlpcPortContext; // dt nt!_ALPC_PORT PortContext USHORT AlpcPortObjectLock; // dt nt!_ALPC_PORT PortObjectLock USHORT AlpcSequenceNo; // dt nt!_ALPC_PORT SequenceNo USHORT AlpcState; // dt nt!_ALPC_PORT u1.State USHORT KtInitialStack; // dt nt!_KTHREAD InitialStack USHORT KtStackLimit; // dt nt!_KTHREAD StackLimit USHORT KtStackBase; // dt nt!_KTHREAD StackBase USHORT KtKernelStack; // dt nt!_KTHREAD KernelStack USHORT KtReadOperationCount; // dt nt!_KTHREAD ReadOperationCount USHORT KtWriteOperationCount; // dt nt!_KTHREAD WriteOperationCount USHORT KtOtherOperationCount; // dt nt!_KTHREAD OtherOperationCount USHORT KtReadTransferCount; // dt nt!_KTHREAD ReadTransferCount USHORT KtWriteTransferCount; // dt nt!_KTHREAD WriteTransferCount USHORT KtOtherTransferCount; // dt nt!_KTHREAD OtherTransferCount USHORT MmSectionControlArea; // dt nt!_SECTION u1.ControlArea USHORT MmControlAreaListHead; // dt nt!_CONTROL_AREA ListHead USHORT MmControlAreaLock; // dt nt!_CONTROL_AREA ControlAreaLock USHORT EpSectionObject; // dt nt!_EPROCESS SectionObject } KPH_DYN_KERNEL_FIELDS, *PKPH_DYN_KERNEL_FIELDS; typedef KPH_DYN_KERNEL_FIELDS KPH_DYN_NTOSKRNL_FIELDS; typedef PKPH_DYN_KERNEL_FIELDS PKPH_DYN_NTOSKRNL_FIELDS; typedef KPH_DYN_KERNEL_FIELDS KPH_DYN_NTKRLA57_FIELDS; typedef PKPH_DYN_KERNEL_FIELDS PKPH_DYN_NTKRLA57_FIELDS; typedef struct _KPH_DYN_LXCORE_FIELDS { USHORT LxPicoProc; // uf lxcore!LxpSyscall_GETPID USHORT LxPicoProcInfo; // uf lxcore!LxpSyscall_GETPID USHORT LxPicoProcInfoPID; // uf lxcore!LxpSyscall_GETPID USHORT LxPicoThrdInfo; // uf lxcore!LxpSyscall_GETTID USHORT LxPicoThrdInfoTID; // uf lxcore!LxpSyscall_GETTID } KPH_DYN_LXCORE_FIELDS, *PKPH_DYN_LXCORE_FIELDS; typedef struct _KPH_DYN_FIELDS { ULONG FieldsId; USHORT Length; BYTE Fields[ANYSIZE_ARRAY]; } KPH_DYN_FIELDS, *PKPH_DYN_FIELDS; typedef struct _KPH_DYN_DATA { USHORT Class; USHORT Machine; ULONG TimeDateStamp; ULONG SizeOfImage; ULONG Offset; } KPH_DYN_DATA, *PKPH_DYN_DATA; typedef struct _KPH_DYN_CONFIG { ULONG Version; BYTE SessionTokenPublicKey[KPH_DYN_SESSION_TOKEN_PUBLIC_KEY_LENGTH]; ULONG Count; KPH_DYN_DATA Data[ANYSIZE_ARRAY]; // BYTE Fields[ANYSIZE_ARRAY]; } KPH_DYN_CONFIG, *PKPH_DYN_CONFIG; #include """; /// /// Represents a single dynamic data entry for kernel or core configuration. /// [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct DynDataEntry { /// /// The class type. /// public UInt16 Class; /// /// The machine architecture code. /// public UInt16 Machine; /// /// The image timestamp. /// public UInt32 TimeDateStamp; /// /// The image size. /// public UInt32 SizeOfImage; /// /// The offset to the field data. /// public UInt32 Offset; } /// /// Represents kernel field offsets for dynamic configuration. /// [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct DynFieldsKernel { public UInt16 EgeGuid; public UInt16 EpObjectTable; public UInt16 EreGuidEntry; public UInt16 HtHandleContentionEvent; public UInt16 OtName; public UInt16 OtIndex; public UInt16 ObDecodeShift; public UInt16 ObAttributesShift; public UInt16 AlpcCommunicationInfo; public UInt16 AlpcOwnerProcess; public UInt16 AlpcConnectionPort; public UInt16 AlpcServerCommunicationPort; public UInt16 AlpcClientCommunicationPort; public UInt16 AlpcHandleTable; public UInt16 AlpcHandleTableLock; public UInt16 AlpcAttributes; public UInt16 AlpcAttributesFlags; public UInt16 AlpcPortContext; public UInt16 AlpcPortObjectLock; public UInt16 AlpcSequenceNo = UInt16.MaxValue; public UInt16 AlpcState; public UInt16 KtInitialStack; public UInt16 KtStackLimit; public UInt16 KtStackBase; public UInt16 KtKernelStack; public UInt16 KtReadOperationCount; public UInt16 KtWriteOperationCount; public UInt16 KtOtherOperationCount; public UInt16 KtReadTransferCount; public UInt16 KtWriteTransferCount; public UInt16 KtOtherTransferCount; public UInt16 MmSectionControlArea; public UInt16 MmControlAreaListHead; public UInt16 MmControlAreaLock; public UInt16 EpSectionObject; /// /// Initializes all fields to . /// public DynFieldsKernel() { EgeGuid = UInt16.MaxValue; EpObjectTable = UInt16.MaxValue; EreGuidEntry = UInt16.MaxValue; HtHandleContentionEvent = UInt16.MaxValue; OtName = UInt16.MaxValue; OtIndex = UInt16.MaxValue; ObDecodeShift = UInt16.MaxValue; ObAttributesShift = UInt16.MaxValue; AlpcCommunicationInfo = UInt16.MaxValue; AlpcOwnerProcess = UInt16.MaxValue; AlpcConnectionPort = UInt16.MaxValue; AlpcServerCommunicationPort = UInt16.MaxValue; AlpcClientCommunicationPort = UInt16.MaxValue; AlpcHandleTable = UInt16.MaxValue; AlpcHandleTableLock = UInt16.MaxValue; AlpcAttributes = UInt16.MaxValue; AlpcAttributesFlags = UInt16.MaxValue; AlpcPortContext = UInt16.MaxValue; AlpcPortObjectLock = UInt16.MaxValue; AlpcSequenceNo = UInt16.MaxValue; AlpcState = UInt16.MaxValue; KtInitialStack = UInt16.MaxValue; KtStackLimit = UInt16.MaxValue; KtStackBase = UInt16.MaxValue; KtKernelStack = UInt16.MaxValue; KtReadOperationCount = UInt16.MaxValue; KtWriteOperationCount = UInt16.MaxValue; KtOtherOperationCount = UInt16.MaxValue; KtReadTransferCount = UInt16.MaxValue; KtWriteTransferCount = UInt16.MaxValue; KtOtherTransferCount = UInt16.MaxValue; MmSectionControlArea = UInt16.MaxValue; MmControlAreaListHead = UInt16.MaxValue; MmControlAreaLock = UInt16.MaxValue; EpSectionObject = UInt16.MaxValue; } } /// /// Represents LXCore field offsets for dynamic configuration. /// [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct DynFieldsLxcore { public UInt16 LxPicoProc; public UInt16 LxPicoProcInfo; public UInt16 LxPicoProcInfoPID; public UInt16 LxPicoThrdInfo; public UInt16 LxPicoThrdInfoTID; /// /// Initializes all fields to . /// public DynFieldsLxcore() { LxPicoProc = UInt16.MaxValue; LxPicoProcInfo = UInt16.MaxValue; LxPicoProcInfoPID = UInt16.MaxValue; LxPicoThrdInfo = UInt16.MaxValue; LxPicoThrdInfoTID = UInt16.MaxValue; } } /// /// Generates and writes dynamic configuration files and headers. /// /// The output directory for the config file. /// Whether to enforce strict signature checks. /// True if successful; otherwise, false. public static bool Execute(string OutDir, bool StrictChecks) { string manifestFile = Path.Join([Build.BuildWorkingFolder, "\\kphlib\\kphdyn.xml"]); string headerFile = Path.Join([Build.BuildWorkingFolder, "\\kphlib\\include\\kphdyn.h"]); string sourceFile = Path.Join([Build.BuildWorkingFolder, "\\kphlib\\kphdyn.c"]); // Check for new or modified content. We don't want to touch the file if it's unnecessary. { Utils.WriteTextIfChanged(headerFile, GenerateHeader); Program.PrintColorMessage($"Dynamic header -> {headerFile}", ConsoleColor.Cyan); } byte[] config = GenerateConfig(manifestFile); { Utils.WriteTextIfChanged(sourceFile, writer => GenerateSource(writer, config)); Program.PrintColorMessage($"Dynamic source -> {sourceFile}", ConsoleColor.Cyan); } if (string.IsNullOrWhiteSpace(OutDir)) return true; Win32.CreateDirectory(OutDir); string configFile = Path.Join([OutDir, "\\ksidyn.bin"]); if (File.Exists(configFile)) { var configUpdateBytes = Utils.ReadAllBytes(configFile).AsSpan(); var configSigFileName = Path.ChangeExtension(configFile, ".sig"); if (configUpdateBytes.SequenceEqual(config) && File.Exists(configSigFileName)) { Program.PrintColorMessage($"Dynamic config -> {configFile}", ConsoleColor.Cyan); return true; } File.Delete(configFile); File.Delete(configSigFileName); Utils.WriteAllBytes(configFile, config); bool configFileSig = BuildVerify.CreateSigFile("kph", configFile, StrictChecks); Program.PrintColorMessage($"Dynamic config -> {configFile}", ConsoleColor.Cyan); return configFileSig; } else { Utils.WriteAllBytes(configFile, config); bool configFileSig = BuildVerify.CreateSigFile("kph", configFile, StrictChecks); Program.PrintColorMessage($"Dynamic config -> {configFile}", ConsoleColor.Cyan); return configFileSig; } } /// /// Generates and writes dynamic configuration files and headers. /// /// True if successful; otherwise, false. private static void GenerateHeader(TextWriter sb) { sb.WriteLine(FileHeader); sb.WriteLine(); sb.WriteLine("#pragma once"); sb.WriteLine(); sb.WriteLine(Includes); sb.WriteLine(DynConfigC); sb.WriteLine(); sb.WriteLine("#ifdef _WIN64"); sb.WriteLine("extern CONST BYTE KphDynConfig[];"); sb.WriteLine("extern CONST ULONG KphDynConfigLength;"); sb.WriteLine("#endif"); } /// /// Generates the dynamic source file content. /// /// The configuration data as a string. /// The source file content as a string. private static void GenerateSource( TextWriter sb, byte[] Config ) { sb.WriteLine(FileHeader); sb.WriteLine(); sb.WriteLine(Includes); sb.WriteLine(); sb.WriteLine("#ifdef _WIN64"); sb.WriteLine("CONST BYTE KphDynConfig[] ="); sb.WriteLine("{"); WriteBytes(sb, Config); sb.WriteLine("};"); sb.WriteLine(); sb.WriteLine("CONST ULONG KphDynConfigLength = ARRAYSIZE(KphDynConfig);"); sb.WriteLine("#endif"); } /// /// Generates the dynamic configuration binary from the manifest XML. /// /// The manifest XML file path. /// The configuration binary as a byte array. private static byte[] GenerateConfig(string ManifestFile) { var xml = new XmlDocument(); var xmlSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Prohibit, IgnoreWhitespace = true }; using (var xmlReader = XmlReader.Create(ManifestFile, xmlSettings)) { xml.Load(xmlReader); } var dyn = xml.SelectSingleNode("/dyn"); var dataNodes = dyn?.SelectNodes("data"); var fieldsNodes = dyn?.SelectNodes("fields"); if (dataNodes == null) return null; if (fieldsNodes == null) return null; var fieldsStream = new ArrayBufferWriter(); var fieldsMap = new Dictionary(fieldsNodes.Count); var fieldsOffsets = new Dictionary(fieldsNodes.Count); var entries = new List(dataNodes.Count); foreach (XmlNode field in fieldsNodes) { var name = field.Attributes?.GetNamedItem("id")?.Value; if (string.IsNullOrWhiteSpace(name)) continue; fieldsMap.Add(UInt32.Parse(name.AsSpan()), field); } foreach (XmlNode data in dataNodes) { var file = data.Attributes?.GetNamedItem("file")?.Value; var arch = data.Attributes?.GetNamedItem("arch")?.Value; var timestamp = data.Attributes?.GetNamedItem("timestamp")?.Value; var size = data.Attributes?.GetNamedItem("size")?.Value; var dynClass = ClassFromString(file); if (string.IsNullOrWhiteSpace(timestamp)) continue; if (string.IsNullOrWhiteSpace(size)) continue; DynDataEntry entry = new DynDataEntry { Class = (UInt16)dynClass, Machine = MachineFromString(arch), TimeDateStamp = UInt32.Parse(timestamp.AsSpan(2), NumberStyles.HexNumber), SizeOfImage = UInt32.Parse(size.AsSpan(2), NumberStyles.HexNumber) }; var fieldId = UInt32.Parse(data.InnerText.AsSpan()); if (!fieldsOffsets.TryGetValue(fieldId, out UInt32 offset)) { offset = (UInt32)fieldsStream.WrittenCount; fieldsOffsets.Add(fieldId, offset); switch (dynClass) { case ClassType.Ntoskrnl: case ClassType.Ntkrla57: { var fieldsData = new DynFieldsKernel(); var fieldNodes = fieldsMap[fieldId].SelectNodes("field"); if (fieldNodes != null) { foreach (XmlNode field in fieldNodes) { string value = field.Attributes?.GetNamedItem("value")?.Value; string name = field.Attributes?.GetNamedItem("name")?.Value; if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(value)) continue; if (DynFieldsKernelFieldCache.TryGetValue(name, out var member)) { member.SetValueDirect(__makeref(fieldsData), UInt16.Parse(value.AsSpan(2), NumberStyles.HexNumber)); } } } fieldsStream.Write(MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref fieldsData, 1))); } break; case ClassType.Lxcore: { var fieldsData = new DynFieldsLxcore(); var fieldNodes = fieldsMap[fieldId].SelectNodes("field"); if (fieldNodes != null) { foreach (XmlNode field in fieldNodes) { string value = field.Attributes?.GetNamedItem("value")?.Value; string name = field.Attributes?.GetNamedItem("name")?.Value; if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(value)) continue; if (DynFieldsLxcoreFieldCache.TryGetValue(name, out var member)) { member.SetValueDirect(__makeref(fieldsData), UInt16.Parse(value.AsSpan(2), NumberStyles.HexNumber)); } } } fieldsStream.Write(MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref fieldsData, 1))); } break; default: { throw new InvalidOperationException($"invalid class {dynClass}"); } } } entry.Offset = offset; entries.Add(entry); } // Allocate a pre-sized buffer for the entire configuration binary. // The size is calculated as: Version (4) + Public Key Length + Count (4) + (Entries * EntrySize) + Fields Data Length. byte[] result = new byte[sizeof(uint) + SessionTokenPublicKey.Length + sizeof(uint) + (entries.Count * Unsafe.SizeOf()) + fieldsStream.WrittenCount]; // Create a span over the result buffer for efficient writing. Span span = result; // Write the configuration version in little-endian format and advance the span. BinaryPrimitives.WriteUInt32LittleEndian(span, Version); span = span[sizeof(uint)..]; // Copy the session token public key into the buffer and advance the span. SessionTokenPublicKey.CopyTo(span); span = span[SessionTokenPublicKey.Length..]; // Write the count of dynamic data entries and advance the span. BinaryPrimitives.WriteUInt32LittleEndian(span, (uint)entries.Count); span = span[sizeof(uint)..]; // Convert the entry list to a read-only byte span and copy it to the buffer. ReadOnlySpan entriesSpan = MemoryMarshal.AsBytes(CollectionsMarshal.AsSpan(entries)); entriesSpan.CopyTo(span); span = span[entriesSpan.Length..]; // Copy the field data from the ArrayBufferWriter to the remaining buffer space. fieldsStream.WrittenSpan.CopyTo(span); return result; } /// /// Converts a byte array to a formatted C array string for source file output. /// /// The byte array to convert. /// The formatted string. private static void WriteBytes(TextWriter sb, byte[] Buffer) { if (Buffer == null || Buffer.Length == 0) return; // This method avoids intermediate string allocations using InterpolatedStringHandler // and calculates exact or near-exact capacity to avoid reallocation. Each full line of 8 bytes is: // " " (4) + 8 * "0xXX, " (48) - 1 (trailing space) + \r\n (2) = 53-55 chars. for (var i = 0; i < Buffer.Length; i++) { if (i % 8 == 0) { sb.Write(" "); } sb.Write($"0x{Buffer[i]:x2},"); if (i % 8 == 7 || i == Buffer.Length - 1) sb.WriteLine(); else sb.Write(' '); } } /// /// Validates the session token public key. /// /// True if the key is valid; otherwise, false. public static bool IsValidSessionKey() { try { BuildVerify.PrintCngPublicKeyInfo(SessionTokenPublicKey, CngKeyBlobFormat.GenericPublicBlob); } catch (Exception ex) { Program.PrintColorMessage($"[ERROR] {ex}", ConsoleColor.Red); return false; } return true; } } }