420 lines
15 KiB
C#
420 lines
15 KiB
C#
/*
|
|
* Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved.
|
|
*
|
|
* This file is part of System Informer.
|
|
*
|
|
* Authors:
|
|
*
|
|
* jxy-s
|
|
*
|
|
*/
|
|
|
|
namespace CustomBuildTool
|
|
{
|
|
public static class DynData
|
|
{
|
|
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
|
|
*
|
|
*/";
|
|
|
|
private const string Includes =
|
|
@"#include <kphlibbase.h>";
|
|
|
|
private const UInt32 Version = 5;
|
|
|
|
private static string DynConfigC =
|
|
$@"#define KPH_DYN_CONFIGURATION_VERSION { Version }
|
|
|
|
#define KPH_DYN_CI_INVALID ((SHORT)-1)
|
|
#define KPH_DYN_CI_V1 ((SHORT)1)
|
|
#define KPH_DYN_CI_V2 ((SHORT)2)
|
|
|
|
#include <pshpack1.h>
|
|
|
|
typedef struct _KPH_DYN_CONFIGURATION
|
|
{{
|
|
USHORT MajorVersion;
|
|
USHORT MinorVersion;
|
|
USHORT ServicePackMajor; // -1 to ignore
|
|
USHORT BuildNumberMin; // -1 to ignore
|
|
USHORT RevisionMin; // -1 to ignore
|
|
USHORT BuildNumberMax; // -1 to ignore
|
|
USHORT RevisionMax; // -1 to ignore
|
|
|
|
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 CiVersion; // ci.dll exports version
|
|
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 AlpcSequenceNo; // dt nt!_ALPC_PORT SequenceNo
|
|
USHORT AlpcState; // dt nt!_ALPC_PORT State
|
|
|
|
}} KPH_DYN_CONFIGURATION, *PKPH_DYN_CONFIGURATION;
|
|
|
|
typedef struct _KPH_DYNDATA
|
|
{{
|
|
ULONG Version;
|
|
ULONG Count;
|
|
KPH_DYN_CONFIGURATION Configs[ANYSIZE_ARRAY];
|
|
|
|
}} KPH_DYNDATA, *PKPH_DYNDATA;
|
|
|
|
#include <poppack.h>";
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
public struct DynConfig
|
|
{
|
|
public UInt16 MajorVersion;
|
|
public UInt16 MinorVersion;
|
|
public UInt16 ServicePackMajor;
|
|
public UInt16 BuildNumberMin;
|
|
public UInt16 BuildNumberMax;
|
|
public UInt16 RevisionMin;
|
|
public UInt16 RevisionMax;
|
|
|
|
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 CiVersion;
|
|
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 AlpcSequenceNo;
|
|
public UInt16 AlpcState;
|
|
|
|
public DynConfig()
|
|
{
|
|
MajorVersion = ushort.MaxValue;
|
|
MinorVersion = ushort.MaxValue;
|
|
ServicePackMajor = ushort.MaxValue;
|
|
BuildNumberMin = ushort.MaxValue;
|
|
BuildNumberMax = ushort.MaxValue;
|
|
RevisionMin = ushort.MaxValue;
|
|
RevisionMax = ushort.MaxValue;
|
|
|
|
EgeGuid = ushort.MaxValue;
|
|
EpObjectTable = ushort.MaxValue;
|
|
EreGuidEntry = ushort.MaxValue;
|
|
HtHandleContentionEvent = ushort.MaxValue;
|
|
OtName = ushort.MaxValue;
|
|
OtIndex = ushort.MaxValue;
|
|
ObDecodeShift = ushort.MaxValue;
|
|
ObAttributesShift = ushort.MaxValue;
|
|
CiVersion = ushort.MaxValue;
|
|
AlpcCommunicationInfo = ushort.MaxValue;
|
|
AlpcOwnerProcess = ushort.MaxValue;
|
|
AlpcConnectionPort = ushort.MaxValue;
|
|
AlpcServerCommunicationPort = ushort.MaxValue;
|
|
AlpcClientCommunicationPort = ushort.MaxValue;
|
|
AlpcHandleTable = ushort.MaxValue;
|
|
AlpcHandleTableLock = ushort.MaxValue;
|
|
AlpcAttributes = ushort.MaxValue;
|
|
AlpcAttributesFlags = ushort.MaxValue;
|
|
AlpcPortContext = ushort.MaxValue;
|
|
AlpcSequenceNo = ushort.MaxValue;
|
|
AlpcState = ushort.MaxValue;
|
|
}
|
|
}
|
|
|
|
public static void Execute()
|
|
{
|
|
string manifestFile = "kphlib\\kphdyn.xml";
|
|
string headerFile = "kphlib\\include\\kphdyn.h";
|
|
string sourceFile = "kphlib\\kphdyn.c";
|
|
|
|
LoadConfig(
|
|
manifestFile,
|
|
out string config64,
|
|
out string sig64,
|
|
out string config32,
|
|
out string sig32
|
|
);
|
|
|
|
string header = GenerateHeader();
|
|
string source = GenerateSource(config64, sig64, config32, sig32);
|
|
|
|
File.WriteAllText(headerFile, header);
|
|
File.WriteAllText(sourceFile, source);
|
|
}
|
|
|
|
private static string GenerateHeader()
|
|
{
|
|
StringBuilder sb = new StringBuilder(8192);
|
|
|
|
sb.AppendLine(FileHeader);
|
|
sb.AppendLine();
|
|
sb.AppendLine("#pragma once");
|
|
sb.AppendLine();
|
|
sb.AppendLine(Includes);
|
|
sb.AppendLine();
|
|
sb.AppendLine(DynConfigC);
|
|
sb.AppendLine();
|
|
sb.AppendLine("extern BYTE KphDynData[];");
|
|
sb.AppendLine("extern ULONG KphDynDataLength;");
|
|
sb.AppendLine("extern BYTE KphDynDataSig[];");
|
|
sb.AppendLine("extern ULONG KphDynDataSigLength;");
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
private static string GenerateSource(
|
|
string Config64,
|
|
string Sig64,
|
|
string Config32,
|
|
string Sig32
|
|
)
|
|
{
|
|
StringBuilder sb = new StringBuilder(16348);
|
|
|
|
sb.AppendLine(FileHeader);
|
|
sb.AppendLine();
|
|
sb.AppendLine(Includes);
|
|
sb.AppendLine();
|
|
sb.AppendLine("BYTE KphDynData[] =");
|
|
sb.AppendLine("{");
|
|
sb.AppendLine("#ifdef _WIN64");
|
|
sb.Append(Config64);
|
|
sb.AppendLine("#else");
|
|
sb.Append(Config32);
|
|
sb.AppendLine("#endif");
|
|
sb.AppendLine("};");
|
|
sb.AppendLine();
|
|
sb.AppendLine("ULONG KphDynDataLength = ARRAYSIZE(KphDynData);");
|
|
sb.AppendLine();
|
|
sb.AppendLine("BYTE KphDynDataSig[] =");
|
|
sb.AppendLine("{");
|
|
sb.AppendLine("#ifdef _WIN64");
|
|
sb.Append(Sig64);
|
|
sb.AppendLine("#else");
|
|
sb.Append(Sig32);
|
|
sb.AppendLine("#endif");
|
|
sb.AppendLine("};");
|
|
sb.AppendLine();
|
|
sb.AppendLine("ULONG KphDynDataSigLength = ARRAYSIZE(KphDynDataSig);");
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
private static void LoadConfig(
|
|
string ManifestFile,
|
|
out string Config64,
|
|
out string Sig64,
|
|
out string Config32,
|
|
out string Sig32
|
|
)
|
|
{
|
|
var xml = new XmlDocument();
|
|
xml.Load(ManifestFile);
|
|
var arch64 = xml.SelectSingleNode("/dyn/arch[@id='64']");
|
|
var arch32 = xml.SelectSingleNode("/dyn/arch[@id='32']");
|
|
|
|
LoadConfigForArch(arch64, out Config64, out Sig64);
|
|
LoadConfigForArch(arch32, out Config32, out Sig32);
|
|
}
|
|
|
|
private static void LoadConfigForArch(
|
|
XmlNode ArchNode,
|
|
out string ConfigData,
|
|
out string SigData
|
|
)
|
|
{
|
|
var configs = new List<DynConfig>(10);
|
|
var configNames = new List<string>(10);
|
|
|
|
Program.PrintColorMessage($"Loading Arch: {ArchNode.Attributes.GetNamedItem("id").Value}", ConsoleColor.Green);
|
|
|
|
foreach (XmlNode data in ArchNode.SelectNodes("data"))
|
|
{
|
|
var config = new DynConfig();
|
|
var configName = data.Attributes.GetNamedItem("name").Value;
|
|
|
|
Program.PrintColorMessage(configName, ConsoleColor.Cyan);
|
|
|
|
foreach (XmlNode field in data.SelectNodes("field"))
|
|
{
|
|
var value = field.Attributes.GetNamedItem("value").Value;
|
|
var name = field.Attributes.GetNamedItem("name").Value;
|
|
var member = typeof(DynConfig).GetField(name);
|
|
|
|
if (value.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
value = Convert.ToUInt64(value, 16).ToString();
|
|
}
|
|
else if (value.Equals("-1", StringComparison.OrdinalIgnoreCase) && member.FieldType == typeof(ushort))
|
|
{
|
|
value = ushort.MaxValue.ToString();
|
|
}
|
|
|
|
member.SetValueDirect(__makeref(config), Convert.ChangeType(value, member.FieldType));
|
|
}
|
|
|
|
configs.Add(config);
|
|
configNames.Add(configName);
|
|
}
|
|
|
|
if (!Validate(configs, configNames))
|
|
{
|
|
throw new Exception("Dynamic configuration is invalid!");
|
|
}
|
|
|
|
string tempName = Path.GetTempPath() + Guid.NewGuid().ToString();
|
|
string configFile = $"{tempName}.bin";
|
|
string sigFile = $"{tempName}.sig";
|
|
|
|
using (var file = new FileStream(configFile, FileMode.CreateNew))
|
|
using (var writer = new BinaryWriter(file))
|
|
{
|
|
//
|
|
// Write the version and count first, then the blocks.
|
|
// This conforms with KPH_DYNDATA defined above.
|
|
//
|
|
writer.Write(Version);
|
|
writer.Write((uint)configs.Count);
|
|
writer.Write(MemoryMarshal.AsBytes(CollectionsMarshal.AsSpan(configs)));
|
|
|
|
//foreach (var config in configs)
|
|
//{
|
|
// byte[] buffer = new byte[Marshal.SizeOf<DynConfig>()];
|
|
// Unsafe.As<byte, DynConfig>(ref buffer[0]) = config;
|
|
// writer.Write(buffer);
|
|
//}
|
|
//
|
|
//foreach (var config in configs)
|
|
//{
|
|
// var ptr = Marshal.AllocHGlobal(Marshal.SizeOf<DynConfig>());
|
|
// Marshal.StructureToPtr(config, ptr, false);
|
|
// var bytes = new byte[Marshal.SizeOf<DynConfig>()];
|
|
// Marshal.Copy(ptr, bytes, 0, bytes.Length);
|
|
// Marshal.FreeHGlobal(ptr);
|
|
// writer.Write(bytes);
|
|
//}
|
|
}
|
|
|
|
if (Verify.CreateSignatureFile(Verify.GetPath("kph.key"), configFile))
|
|
{
|
|
ConfigData = BytesToString(configFile);
|
|
SigData = BytesToString(sigFile);
|
|
|
|
Win32.DeleteFile(configFile);
|
|
Win32.DeleteFile(sigFile);
|
|
}
|
|
else
|
|
{
|
|
ConfigData = string.Empty;
|
|
SigData = string.Empty;
|
|
}
|
|
}
|
|
|
|
private static bool Validate(List<DynConfig> Configs, List<string> ConfigNames)
|
|
{
|
|
bool valid = true;
|
|
|
|
for (int i = 0; i < Configs.Count; i++)
|
|
{
|
|
var config = Configs[i];
|
|
var configName = ConfigNames[i];
|
|
|
|
if (config.MajorVersion == ushort.MaxValue)
|
|
{
|
|
Program.PrintColorMessage($"{configName} - MajorVersion required", ConsoleColor.Red);
|
|
valid = false;
|
|
}
|
|
if (config.MinorVersion == ushort.MaxValue)
|
|
{
|
|
Program.PrintColorMessage($"{configName} - MinorVersion required", ConsoleColor.Red);
|
|
valid = false;
|
|
}
|
|
|
|
if ((config.BuildNumberMax != ushort.MaxValue) &&
|
|
(config.BuildNumberMin != ushort.MaxValue) &&
|
|
(config.BuildNumberMax < config.BuildNumberMin))
|
|
{
|
|
Program.PrintColorMessage($"{configName} - BuildNumber range is invalid", ConsoleColor.Red);
|
|
valid = false;
|
|
}
|
|
|
|
if ((config.RevisionMax != ushort.MaxValue) &&
|
|
(config.RevisionMin != ushort.MaxValue) &&
|
|
(config.RevisionMax < config.RevisionMin))
|
|
{
|
|
Program.PrintColorMessage($"{configName} - Revision range is invalid", ConsoleColor.Red);
|
|
valid = false;
|
|
}
|
|
}
|
|
|
|
return valid;
|
|
}
|
|
|
|
private static string BytesToString(string Path)
|
|
{
|
|
var file = File.ReadAllBytes(Path);
|
|
|
|
using (MemoryStream stream = new MemoryStream(file, false))
|
|
{
|
|
StringBuilder hex = new StringBuilder(64);
|
|
StringBuilder sb = new StringBuilder(8192);
|
|
Span<byte> bytes = stackalloc byte[8];
|
|
|
|
while (true)
|
|
{
|
|
var len = stream.Read(bytes);
|
|
|
|
if (len == 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
hex.AppendFormat("0x{0:x2}, ", bytes[i]);
|
|
}
|
|
hex.Remove(hex.Length - 1, 1);
|
|
|
|
sb.Append(" ");
|
|
sb.AppendLine(hex.ToString());
|
|
hex.Clear();
|
|
|
|
if (len < bytes.Length)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|