fucking FINALLY got this BULLSHIT working

This commit is contained in:
lhodges1 2023-09-10 20:20:35 +10:00
parent fafcc9da1e
commit ee088939e5
15 changed files with 137 additions and 143 deletions

View file

@ -1,4 +1,4 @@
using Microsoft.Extensions.Logging;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
@ -13,16 +13,25 @@ namespace server.Database.Entity
public class HardwareConfigurationEntity : HardwareConfiguration
{
private readonly ModelContext _modelContext;
public UserEntity UserEntity { get; set; }
public HardwareConfigurationEntity(ModelContext modelContext)
{
UserEntity = new UserEntity(modelContext);
_modelContext = modelContext;
}
public bool CheckIfHardwareConfigurationExists()
public bool CheckIfHardwareIsBanned()
{
return _modelContext.HardwareConfiguration.Any(h => h.MotherboardSerial == MotherboardSerial &&
h.DeviceDrive0Serial == DeviceDrive0Serial);
return _modelContext.HardwareConfiguration.Any(
h => h.MotherboardSerial == MotherboardSerial &&
h.DeviceDrive0Serial == DeviceDrive0Serial &&
h.IsBanned);
}
public void InsertHardwareConfiguration()
{
_modelContext.HardwareConfiguration.Add(this);
}
}
}

View file

@ -10,15 +10,11 @@ namespace server.Database.Entity
{
public class UserEntity : User
{
private readonly ILogger _logger;
private readonly ModelContext _modelContext;
public HardwareConfigurationEntity HardwareConfigurationEntity { get; set; }
public UserEntity(ILogger logger, ModelContext modelContext)
public UserEntity(ModelContext modelContext)
{
_logger = logger;
_modelContext = modelContext;
HardwareConfigurationEntity = new HardwareConfigurationEntity(_modelContext);
}
public bool CheckIfUserExists()
@ -31,15 +27,6 @@ namespace server.Database.Entity
return _modelContext.Users.Any(u => u.Steam64Id == Steam64Id && u.IsBanned);
}
public bool IsUsersHardwareBanned()
{
HardwareConfigurationEntity hwConfig = new HardwareConfigurationEntity(_modelContext);
hwConfig.MotherboardSerial = HardwareConfigurationEntity.MotherboardSerial;
hwConfig.DeviceDrive0Serial = HardwareConfigurationEntity.DeviceDrive0Serial;
return hwConfig.CheckIfHardwareConfigurationExists() && hwConfig.IsBanned;
}
public void InsertUser()
{
_modelContext.Users.Add(this);

View file

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
@ -13,16 +14,15 @@ namespace server.Database.Model
public int UserId { get; set; }
public ulong Steam64Id { get; set; }
public bool IsBanned { get; set; }
public ICollection<HardwareConfiguration> HardwareConfigurations { get; set; }
public virtual ICollection<HardwareConfiguration> HardwareConfigurations { get; set; }
}
public class HardwareConfiguration
{
public int HardwareId { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public bool IsBanned { get; set; }
public string DeviceDrive0Serial { get; set; }
public string MotherboardSerial { get; set; }
}
}
}

View file

@ -33,12 +33,7 @@ namespace server.Database.Model
.IsRequired();
entity.Property(e => e.IsBanned)
.IsRequired();
entity.HasMany(g => g.HardwareConfigurations)
.WithOne(s => s.User)
.HasForeignKey(s => s.UserId)
.HasPrincipalKey(keyExpression: e => e.UserId);
.HasDefaultValue(false);
});
modelBuilder.Entity<HardwareConfiguration>(entity =>
@ -48,12 +43,17 @@ namespace server.Database.Model
entity.Property(e => e.HardwareId)
.UseMySQLAutoIncrementColumn(entity.Property(e => e.HardwareId).Metadata.Name);
entity.Property(f => f.IsBanned)
entity.Property(e => e.IsBanned)
.HasDefaultValue(false);
entity.HasOne(s => s.User)
.WithMany(g => g.HardwareConfigurations)
.HasForeignKey(s => s.UserId);
entity.Property(e => e.MotherboardSerial)
.IsRequired();
entity.Property(e => e.DeviceDrive0Serial)
.IsRequired();
entity.HasOne(d => d.User)
.WithMany(f => f.HardwareConfigurations);
});
}
}

View file

@ -25,6 +25,7 @@ namespace server
private ILogger _logger;
private PACKET_HEADER _header;
private NetworkStream _networkStream;
private TcpClient _tcpClient;
private enum MESSAGE_TYPE
{
@ -62,9 +63,10 @@ namespace server
USER_BAN = 20
}
public Message(NetworkStream networkStream, byte[] buffer, int bufferSize, ILogger logger)
public Message(TcpClient client, byte[] buffer, int bufferSize, ILogger logger)
{
_networkStream = networkStream;
_tcpClient = client;
_networkStream = client.GetStream();
_buffer = buffer;
_bufferSize = bufferSize;
_logger = logger;
@ -148,55 +150,59 @@ namespace server
{
context.Database.EnsureCreated();
UserEntity user = new UserEntity(_logger, context);
user.Steam64Id = _header.steam64_id;
user.HardwareConfigurationEntity = new HardwareConfigurationEntity(context);
user.HardwareConfigurationEntity.MotherboardSerial = moboSerial;
user.HardwareConfigurationEntity.DeviceDrive0Serial = driveSerial;
if (user.IsUsersHardwareBanned())
// Create a new user
var user = new User
{
//return packet saying user is banned
_logger.Information("Users hardware is banned");
BuildSystemVerificationResponseHeader(0, sendPacketHeader.RequestId, (int)USER_BAN_REASONS.HARDWARE_BAN);
context.SaveChanges();
return;
}
if (user.CheckIfUserExists())
Steam64Id = 12312322222, // Replace with a valid Steam64Id
};
context.Users.Add(user);
// Create a new hardware configuration associated with the user
var hardwareConfiguration = new HardwareConfiguration
{
if (user.CheckIfUserIsBanned())
{
//same here, send packet back saying user is banned
_logger.Information("User is banned");
DeviceDrive0Serial = "Serial123", // Replace with a valid serial number
MotherboardSerial = "MotherboardSerial123", // Replace with a valid serial number
User = user // Associate the hardware configuration with the user
};
BuildSystemVerificationResponseHeader(0, sendPacketHeader.RequestId, (int)USER_BAN_REASONS.USER_BAN);
context.SaveChanges();
return;
}
else
{
//send packet back that the user has successfully been authenticated
_logger.Information("User is not banned");
BuildSystemVerificationResponseHeader(1, sendPacketHeader.RequestId, 0);
context.SaveChanges();
return;
}
}
_logger.Information("User does not exist and is on valid hardware, creating new user");
user.InsertUser();
BuildSystemVerificationResponseHeader(1, sendPacketHeader.RequestId, 0);
// Add the user and hardware configuration to the context and save changes
context.HardwareConfiguration.Add(hardwareConfiguration);
context.SaveChanges();
/* UserEntity user = new UserEntity(context);
user.Steam64Id = _header.steam64_id;
if (!user.CheckIfUserExists())
{
_logger.Information("Creating new user");
user.InsertUser();
context.SaveChanges();
}
else if (user.CheckIfUserIsBanned())
{
_logger.Information("User is banned");
BuildSystemVerificationResponseHeader(0, sendPacketHeader.RequestId, (int)USER_BAN_REASONS.USER_BAN);
return;
}
HardwareConfigurationEntity hwConfig = new HardwareConfigurationEntity(context)
{
User = user,
IsBanned = false,
MotherboardSerial = moboSerial,
DeviceDrive0Serial = driveSerial
};*/
/* if (hwConfig.CheckIfHardwareIsBanned())
{
_logger.Information("Users hardware is banned");
BuildSystemVerificationResponseHeader(0, sendPacketHeader.RequestId, (int)USER_BAN_REASONS.HARDWARE_BAN);
return;
}*/
//context.HardwareConfiguration.Add(hwConfig);
//context.SaveChanges();
}
}

View file

@ -30,10 +30,10 @@ namespace server
while (true)
{
using TcpClient _client = await _tcpListener.AcceptTcpClientAsync();
TcpClient client = await _tcpListener.AcceptTcpClientAsync();
NetworkStream _stream = _client.GetStream();
NetworkStream clientStreamReference = _stream;
NetworkStream _stream = client.GetStream();
TcpClient clientReference = client;
byte[] buffer = new byte[2048];
int bytesRead = 0;
@ -49,14 +49,14 @@ namespace server
byte[] message = stream.ToArray();
ThreadPool.QueueUserWorkItem(state => DispatchMessage(state, clientStreamReference, message, message.Length));
ThreadPool.QueueUserWorkItem(state => DispatchMessage(state, clientReference, message, message.Length));
}
}
}
private void DispatchMessage(Object? stateInfo, NetworkStream clientStreamReference, byte[] buffer, int bufferSize)
private void DispatchMessage(Object? stateInfo, TcpClient client, byte[] buffer, int bufferSize)
{
Message message = new Message(clientStreamReference, buffer, bufferSize, _logger);
Message message = new Message(client, buffer, bufferSize, _logger);
}
}
}

View file

@ -14,7 +14,7 @@ namespace service
private byte[] _buffer;
private int _bufferSize;
public Client(byte[] buffer, int bufferSize)
public Client(ref byte[] buffer, int bufferSize)
{
_ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
_tcpClient = new TcpClient();
@ -29,5 +29,22 @@ namespace service
stream.BeginWrite(_buffer, 0, _bufferSize, null, null);
}
public async Task<byte[]> GetResponseFromServer()
{
byte[] buffer = new byte[1024];
NetworkStream stream = _tcpClient.GetStream();
using (MemoryStream memoryStream = new MemoryStream())
{
while (stream.DataAvailable)
{
await stream.ReadAsync(buffer, 0, 1024);
memoryStream.Write(buffer, 0, buffer.Length);
}
return memoryStream.ToArray();
}
}
}
}

View file

@ -1,33 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO.Pipes;
using service;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace service
{
public class Message
{
private byte[] _buffer;
private int _bufferSize;
public Message(byte[] buffer, int bufferSize)
{
_bufferSize = bufferSize;
_buffer = buffer;
}
public void DispatchMessage()
{
Client client = new Client(_buffer, _bufferSize);
client.SendMessageToServer();
}
public T GetPacketHeader<T>(ref byte[] buffer)
{
return Helper.BytesToStructure<T>(ref buffer, 0);
}
}
}

View file

@ -17,24 +17,9 @@ namespace service
{
private readonly ILogger<Worker> _logger;
private NamedPipeServerStream _pipeServer;
private byte[] _buffer;
private int _bufferSize;
private static int MAX_BUFFER_SIZE = 8192;
private enum MESSAGE_TYPE
{
MESSAGE_TYPE_REPORT = 1,
MESSAGE_TYPE_SEND = 2,
MESSAGE_TYPE_RECEIVE = 3
}
struct PIPE_PACKET_HEADER
{
int message_type;
Int64 steam64_id;
};
private static int MAX_BUFFER_SIZE = 60000;
public Worker(ILogger<Worker> logger)
{
@ -63,8 +48,12 @@ namespace service
{
_logger.LogInformation("Message received at pipe server with size: {0}", numBytesRead);
Message message = new Message(_buffer, numBytesRead);
message.DispatchMessage();
Client message = new Client(ref _buffer, numBytesRead);
message.SendMessageToServer();
byte[] responseMessage = await message.GetResponseFromServer();
_pipeServer.Write(responseMessage, 0, responseMessage.Length);
}
}
catch (Exception ex)

View file

@ -15,9 +15,9 @@ global::Client::Client( std::shared_ptr<global::ThreadPool> ThreadPool, LPTSTR P
/*
* Request an item from the server
*/
void global::Client::ServerReceive()
void global::Client::ServerReceive(PVOID Buffer, SIZE_T Size)
{
this->pipe->ReadPipe( Buffer, Size );
}
/*
@ -62,6 +62,8 @@ void global::Client::ServerSend(PVOID Buffer, SIZE_T Size, INT RequestId )
memcpy(PVOID((UINT64)send_buffer + total_header_size), Buffer, Size);
LOG_INFO( "Writing to pipe" );
this->pipe->WriteToPipe( send_buffer, header_extension.packet_size );
mutex.unlock();

View file

@ -84,7 +84,7 @@ namespace global
mutex.unlock();
}
void ServerReceive();
void ServerReceive(PVOID Buffer, SIZE_T Size);
void ServerSend( PVOID Buffer, SIZE_T Size, INT RequestId );
};
@ -185,6 +185,13 @@ namespace global
UINT32 thread_id;
UINT64 thread_address;
};
struct SYSTEM_INFORMATION_REQUEST_RESPONSE
{
INT RequestId;
INT CanUserProceed;
INT reason;
};
}
}

View file

@ -600,7 +600,7 @@ VOID kernelmode::Driver::SendClientHardwareInformation()
BOOLEAN status;
global::headers::SYSTEM_INFORMATION system_information;
DWORD bytes_returned;
std::cout << "HELLO?>?" << std::endl;
status = DeviceIoControl(
this->driver_handle,
IOCTL_REQUEST_HARDWARE_INFORMATION,
@ -618,8 +618,6 @@ VOID kernelmode::Driver::SendClientHardwareInformation()
return;
}
std::cout << system_information.motherboard_serial << " " << system_information.drive_0_serial << std::endl;
this->report_interface->ServerSend(
&system_information, sizeof( global::headers::SYSTEM_INFORMATION ), CLIENT_SEND_SYSTEM_INFORMATION );
}

View file

@ -58,5 +58,5 @@ VOID kernelmode::KManager::ValidateProcessModules()
VOID kernelmode::KManager::SendClientHardwareInformation()
{
this->thread_pool->QueueJob( [ this ]() { this->driver_interface->SendClientHardwareInformation(); } );
this->driver_interface->SendClientHardwareInformation();
}

View file

@ -31,6 +31,13 @@ DWORD WINAPI Init(HINSTANCE hinstDLL)
global::headers::SYSTEM_INFORMATION system_information;
kmanager.SendClientHardwareInformation();
global::report_structures::SYSTEM_INFORMATION_REQUEST_RESPONSE response;
client_interface->ServerReceive( &response, sizeof( response ) );
std::cout << "RequestID: " << response.RequestId << " CanUserProceed: " <<
response.CanUserProceed << " Reason: " << response.reason << std::endl;
while ( !GetAsyncKeyState( VK_DELETE ) )
{
kmanager.MonitorCallbackReports();

View file

@ -59,6 +59,11 @@ void global::Pipe::ReadPipe(PVOID Buffer, SIZE_T Size)
NULL
);
LOG_INFO("Bytes read: %d", bytes_read);
if ( bytes_read > Size )
break;
if ( !status && GetLastError() != ERROR_MORE_DATA )
break;