bedtime (:

This commit is contained in:
lhodges1 2023-08-19 00:34:15 +10:00
parent b7d3fdc14c
commit 9356803b33
5 changed files with 96 additions and 14 deletions

View file

@ -72,9 +72,11 @@
<PropertyGroup />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>
<Inf2CatUseLocalTime>true</Inf2CatUseLocalTime>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>
<Inf2CatUseLocalTime>true</Inf2CatUseLocalTime>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>

View file

@ -1,14 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using System.Runtime.CompilerServices;
using service.Types;
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
@ -22,35 +13,70 @@ namespace service
private readonly ILogger<Worker> _logger;
private NamedPipeServerStream _pipeServer;
private byte[] _buffer;
private byte[] _headerBuf;
private int _headerBufSize;
private const int REPORT_CODE_MODULE_VERIFICATION = 10;
private const int REPORT_CODE_START_ADDRESS_VERIFICATION = 20;
private const int REPORT_PAGE_PROTECTION_VERIFICATION = 30;
private const int REPORT_PATTERN_SCAN_FAILURE = 40;
private const int MESSAGE_TYPE_REPORT = 1;
private const int MESSAGE_TYPE_REQUEST = 2;
private int PIPE_BUFFER_READ_SIZE;
struct PIPE_PACKET_HEADER
{
int message_type;
};
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;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Windows service starting, waiting for client to connect");
// to do: verify whos connecting
_pipeServer.WaitForConnection();
_logger.LogInformation("Client connected to the pipe server");
int header = 0;
while (!stoppingToken.IsCancellationRequested)
{
try
{
if (_pipeServer.Read(_buffer, 0, 1024) > 0)
if (_pipeServer.Read(_headerBuf, 0, _headerBufSize) > 0)
{
_logger.LogInformation("Report received, decoding buffer");
await TranslatePipeBuffer();
// for now the header is only an int... LOL
header = BitConverter.ToInt32(_headerBuf, 0);
_logger.LogInformation("Message received with id: {0}", header);
switch (header)
{
case MESSAGE_TYPE_REPORT:
_pipeServer.Read(_buffer, 0, PIPE_BUFFER_READ_SIZE + _headerBufSize);
await TranslatePipeBuffer();
break;
case MESSAGE_TYPE_REQUEST:
_logger.LogInformation("Request received lLOL");
Array.Clear(_buffer, 0, _buffer.Length);
break;
}
}
}
catch (Exception ex)
@ -133,7 +159,7 @@ namespace service
try
{
Marshal.Copy(_buffer, 0, ptr, size);
unsafe { Marshal.Copy(_buffer, 0, ptr, size); }
return (T)Marshal.PtrToStructure(ptr, typeof(T));
}
finally

View file

@ -20,6 +20,11 @@ global::Client::Client( LPTSTR PipeName )
LOG_ERROR( "CreateFile failed with status 0x%x", GetLastError() );
return;
}
/* test the write function */
global::headers::PIPE_PACKET_HEADER header;
header.message_type = REQUEST_PATTERNS_TO_BE_SCANNED;
this->WriteToPipe( &header, sizeof( global::headers::PIPE_PACKET_HEADER ) );
}
void global::Client::WriteToPipe( PVOID Buffer, SIZE_T Size )
@ -42,3 +47,30 @@ void global::Client::WriteToPipe( PVOID Buffer, SIZE_T Size )
LOG_INFO( "Sent bytes over pipe" );
}
void global::Client::ReadPipe(PVOID Buffer, SIZE_T Size)
{
BOOL status = FALSE;
DWORD bytes_read;
do
{
status = ReadFile(
this->pipe_handle,
Buffer,
Size,
&bytes_read,
NULL
);
if ( !status && GetLastError() != ERROR_MORE_DATA )
break;
} while ( !status );
if ( !status )
{
LOG_ERROR( "ReadFile failed with status 0x%x", GetLastError() );
return;
}
}

View file

@ -3,6 +3,9 @@
#include <Windows.h>
#define REPORT_PACKET_ID 1
#define REQUEST_PATTERNS_TO_BE_SCANNED 2
namespace global
{
class Client
@ -12,8 +15,18 @@ namespace global
public:
Client(LPTSTR PipeName);
void WriteToPipe( PVOID Buffer, SIZE_T Size );
void ReadPipe( PVOID Buffer, SIZE_T Size );
};
namespace headers
{
struct PIPE_PACKET_HEADER
{
int message_type;
};
}
}
#endif

View file

@ -15,6 +15,8 @@
#define REPORT_PAGE_PROTECTION_VERIFICATION 30
#define REPORT_PATTERN_SCAN_FAILURE 40
namespace global
{
class Report
@ -28,13 +30,20 @@ namespace global
Report( std::shared_ptr<global::ThreadPool> ThreadPool, LPTSTR PipeName );
/* lock buffer, copy report, send to service then clear buffer */
template <typename T>
void ReportViolation( T* Report )
{
mutex.lock();
memcpy( this->buffer, Report, sizeof( T ) );
global::headers::PIPE_PACKET_HEADER header;
header.message_type = REPORT_PACKET_ID;
memcpy( this->buffer, &header, sizeof( global::headers::PIPE_PACKET_HEADER ) );
memcpy( this->buffer + sizeof( global::headers::PIPE_PACKET_HEADER ), Report, sizeof(T));
this->client->WriteToPipe( buffer, sizeof(T) );
RtlZeroMemory( this->buffer, REPORT_BUFFER_SIZE );
mutex.unlock();
}
};