From 05210ba08a6dcbf36c4191c106166dae6a1bb9e0 Mon Sep 17 00:00:00 2001 From: lhodges1 Date: Thu, 24 Aug 2023 01:16:13 +1000 Subject: [PATCH] bed time c: --- service/Worker.cs | 39 +++++++--------- service/helper.cs | 34 ++++++++++++++ service/messages/Receive.cs | 12 +++++ service/messages/Report.cs | 89 +++++++++++++++++++++++++++++++++++++ service/messages/Send.cs | 13 ++++++ user/client.cpp | 8 ++-- 6 files changed, 169 insertions(+), 26 deletions(-) create mode 100644 service/helper.cs create mode 100644 service/messages/Receive.cs create mode 100644 service/messages/Report.cs create mode 100644 service/messages/Send.cs diff --git a/service/Worker.cs b/service/Worker.cs index b323235..23d269e 100644 --- a/service/Worker.cs +++ b/service/Worker.cs @@ -13,22 +13,16 @@ namespace service { private readonly ILogger _logger; private NamedPipeServerStream _pipeServer; - private byte[] _buffer; - private byte[] _headerBuf; - private int _headerBufSize; - private const int REPORT_PROCESS_MODULE_FAILURE = 10; - private const int REPORT_PROCESS_THREAD_START_ADDRESS_FAILURE = 20; - private const int REPORT_PAGE_PROTECTION_VERIFICATION = 30; - private const int REPORT_PATTERN_SCAN_FAILURE = 40; - private const int REPORT_NMI_CALLBACK_FAILURE = 50; - private const int REPORT_KERNEL_MODULE_FAILURE = 60; - private const int REPORT_OPEN_HANDLE_FAILURE_REPORT = 70; + private byte[] _header; + private int _headerSize; - private const int MESSAGE_TYPE_REPORT = 1; - private const int MESSAGE_TYPE_REQUEST = 2; - - private int PIPE_BUFFER_READ_SIZE; + private enum MESSAGE_TYPE + { + MESSAGE_TYPE_REPORT, + MESSAGE_TYPE_RECEIVE, + MESSAGE_TYPE_SEND, + } struct PIPE_PACKET_HEADER { @@ -38,11 +32,14 @@ namespace service public Worker(ILogger logger) { _logger = logger; - _buffer = new byte[1024]; - unsafe { _headerBufSize = sizeof(PIPE_PACKET_HEADER); } - _headerBuf = new byte[_headerBufSize]; _pipeServer = new NamedPipeServerStream("DonnaACPipe", PipeDirection.InOut, 1); - PIPE_BUFFER_READ_SIZE = 1024 - _headerBufSize; + + unsafe + { + _headerSize = sizeof(PIPE_PACKET_HEADER); + } + + _header = new byte[_headerSize]; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) @@ -54,16 +51,14 @@ namespace service _logger.LogInformation("Client connected to the pipe server"); - int header = 0; - while (!stoppingToken.IsCancellationRequested) { try { - if (_pipeServer.Read(_headerBuf, 0, _headerBufSize) > 0) + if (_pipeServer.Read(_header, 0, _headerSize) > 0) { // for now the header is only an int... LOL - header = BitConverter.ToInt32(_headerBuf, 0); + int header = BitConverter.ToInt32(_header, 0); _logger.LogInformation("Message received with id: {0}", header); diff --git a/service/helper.cs b/service/helper.cs new file mode 100644 index 0000000..662ba14 --- /dev/null +++ b/service/helper.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +#pragma warning disable CS8600 +#pragma warning disable CS8603 + +namespace service +{ + public class Helper + { + unsafe public static T BytesToStructure(ref byte[] buffer) + { + int typeSize = Marshal.SizeOf(typeof(T)); + IntPtr ptr = Marshal.AllocHGlobal(typeSize); + + try + { + Marshal.Copy(buffer, 0, ptr, typeSize); + return (T)Marshal.PtrToStructure(ptr, typeof(T)); + } + finally + { + Marshal.FreeHGlobal(ptr); + } + } + } +} + +#pragma warning restore CS8600 +#pragma warning restore CS8603 \ No newline at end of file diff --git a/service/messages/Receive.cs b/service/messages/Receive.cs new file mode 100644 index 0000000..dfba382 --- /dev/null +++ b/service/messages/Receive.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace service.messages +{ + internal class Receive + { + } +} diff --git a/service/messages/Report.cs b/service/messages/Report.cs new file mode 100644 index 0000000..35586ed --- /dev/null +++ b/service/messages/Report.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.IO.Pipes; +using System.Linq; +using service; +using System.Runtime.ConstrainedExecution; +using System.Text; +using System.Threading.Tasks; + +namespace service.messages +{ + public class Report + { + private NamedPipeServerStream _pipeServer; + private readonly ILogger _logger; + + private byte[] _buffer; + + private static int REPORT_BUFFER_SIZE = 1024; + + private enum REPORT_TYPE + { + REPORT_PROCESS_MODULE_FAILURE = 10, + REPORT_PROCESS_THREAD_START_ADDRESS_FAILURE = 20, + REPORT_PAGE_PROTECTION_VERIFICATION = 30, + REPORT_PATTERN_SCAN_FAILURE = 40, + REPORT_NMI_CALLBACK_FAILURE = 50, + REPORT_KERNEL_MODULE_FAILURE = 60, + REPORT_OPEN_HANDLE_FAILURE_REPORT = 70 + } + + public Report( + ILogger logger, + NamedPipeServerStream pipeServer, + int pipePacketHeaderSize + ) + { + _logger = logger; + _pipeServer = pipeServer; + _buffer = new byte[REPORT_BUFFER_SIZE]; + + ReadReportIntoBuffer(pipePacketHeaderSize); + } + + private void ReadReportIntoBuffer(int pipePacketHeaderSize) + { + _pipeServer.Read(_buffer, 0, REPORT_BUFFER_SIZE + pipePacketHeaderSize); + } + + // This is fine for now as the report header is only an int + private int GetReportType() + { + return BitConverter.ToInt32( _buffer, 0 ); + } + private Task ConvertByteReportIntoStructure() + { + int reportType = GetReportType(); + + if (!Enum.IsDefined(typeof(REPORT_TYPE), reportType)) + { + _logger.LogError("Enum value of {0} is invalid.", reportType); + + return Task.CompletedTask; + } + + switch(reportType) + { + case (int)REPORT_TYPE.REPORT_PROCESS_MODULE_FAILURE: + break; + case (int)REPORT_TYPE.REPORT_PROCESS_THREAD_START_ADDRESS_FAILURE: + break; + case (int)REPORT_TYPE.REPORT_PAGE_PROTECTION_VERIFICATION: + break; + case (int)REPORT_TYPE.REPORT_PATTERN_SCAN_FAILURE: + break; + case (int)REPORT_TYPE.REPORT_NMI_CALLBACK_FAILURE: + break; + case (int)REPORT_TYPE.REPORT_KERNEL_MODULE_FAILURE: + break; + case (int)REPORT_TYPE.REPORT_OPEN_HANDLE_FAILURE_REPORT: + break; + default: + break; + } + + return Task.CompletedTask; + } + } +} diff --git a/service/messages/Send.cs b/service/messages/Send.cs new file mode 100644 index 0000000..9e7d97a --- /dev/null +++ b/service/messages/Send.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace service.messages +{ + internal class Send + { + + } +} diff --git a/user/client.cpp b/user/client.cpp index 3387015..03074e9 100644 --- a/user/client.cpp +++ b/user/client.cpp @@ -39,8 +39,8 @@ void global::Client::ServerSend(PVOID Buffer, SIZE_T Size, INT RequestId) { global::headers::PIPE_PACKET_SEND_EXTENSION_HEADER header_extension; header_extension.request_id = RequestId; - header_extension.total_incoming_packet_count = total_packets; - header_extension.total_incoming_packet_size = Size; + header_extension.total_incoming_packet_count = total_packets + 1; + header_extension.total_incoming_packet_size = Size + total_packets * total_size_of_headers; header_extension.current_packet_number = count; header_extension.packet_size = count == total_packets ? remaining_bytes : SEND_BUFFER_SIZE; @@ -65,9 +65,9 @@ void global::Client::ServerSend(PVOID Buffer, SIZE_T Size, INT RequestId) global::headers::PIPE_PACKET_SEND_EXTENSION_HEADER header_extension; header_extension.request_id = RequestId; header_extension.total_incoming_packet_count = 1; - header_extension.total_incoming_packet_size = Size; + header_extension.total_incoming_packet_size = Size + total_size_of_headers; header_extension.current_packet_number = 1; - header_extension.packet_size = Size; + header_extension.packet_size = Size + total_size_of_headers; memcpy( PVOID( ( UINT64 )this->send_buffer + sizeof( global::headers::PIPE_PACKET_HEADER ) ), &header_extension, sizeof( global::headers::PIPE_PACKET_SEND_EXTENSION_HEADER ) );