bed time c:

This commit is contained in:
lhodges1 2023-08-24 01:16:13 +10:00
parent 5a4870515a
commit 05210ba08a
6 changed files with 169 additions and 26 deletions

View file

@ -13,22 +13,16 @@ namespace service
{
private readonly ILogger<Worker> _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<Worker> 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);

34
service/helper.cs Normal file
View file

@ -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<T>(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

View file

@ -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
{
}
}

View file

@ -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<Report> _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<Report> 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;
}
}
}

13
service/messages/Send.cs Normal file
View file

@ -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
{
}
}

View file

@ -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 ) );