mirror-ac/server/Message.cs

250 lines
8.7 KiB
C#
Raw Normal View History

2023-09-02 15:47:15 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
2023-09-02 17:56:46 +02:00
using System.Runtime.CompilerServices;
2023-09-02 15:47:15 +02:00
using System.Text;
using System.Threading.Tasks;
using Serilog;
2023-09-02 17:56:46 +02:00
using server;
2023-09-07 19:49:36 +02:00
using System.Net;
using System.Net.Sockets;
2023-09-08 20:41:11 +02:00
using server.Types.ClientReport;
using server.Types.ClientSend;
2023-09-07 19:49:36 +02:00
using System.Runtime.InteropServices;
2023-09-09 19:26:19 +02:00
using server.Database.Model;
using server.Database.Entity;
using Org.BouncyCastle.Asn1.BC;
2023-09-02 15:47:15 +02:00
namespace server
{
public class Message
{
private byte[] _buffer;
private int _bufferSize;
2023-09-02 17:56:46 +02:00
private ILogger _logger;
2023-09-07 09:21:00 +02:00
private PACKET_HEADER _header;
2023-09-07 19:49:36 +02:00
private NetworkStream _networkStream;
private TcpClient _tcpClient;
2023-09-02 15:47:15 +02:00
private enum MESSAGE_TYPE
{
2023-09-08 20:41:11 +02:00
MESSAGE_TYPE_CLIENT_REPORT = 1,
MESSAGE_TYPE_CLIENT_SEND = 2,
MESSAGE_TYPE_CLIENT_REQUEST = 3
2023-09-02 15:47:15 +02:00
}
2023-09-08 20:41:11 +02:00
private enum CLIENT_SEND_REQUEST_ID
{
SYSTEM_INFORMATION = 10
}
public struct PACKET_HEADER
2023-09-02 15:47:15 +02:00
{
2023-09-07 09:21:00 +02:00
public int message_type;
2023-09-09 19:26:19 +02:00
public ulong steam64_id;
2023-09-07 09:21:00 +02:00
};
2023-09-02 15:47:15 +02:00
2023-09-09 19:26:19 +02:00
private struct PACKET_REQUEST_HEADER
2023-09-02 15:47:15 +02:00
{
2023-09-08 20:41:11 +02:00
public int RequestId;
2023-09-02 15:47:15 +02:00
}
2023-09-09 19:26:19 +02:00
private struct SYSTEM_INFORMATION_REQUEST_RESPONSE
{
public int RequestId;
public int CanUserProceed;
public int reason;
}
2023-09-10 18:30:46 +02:00
private struct REPORT_RESPONSE
{
public int success;
}
2023-09-09 19:26:19 +02:00
private enum USER_BAN_REASONS
{
HARDWARE_BAN = 10,
USER_BAN = 20
}
2023-09-10 19:32:12 +02:00
private enum ReportCodes
{
REPORT_CODE_MODULE_VERIFICATION = 10,
REPORT_CODE_START_ADDRESS_VERIFICATION = 20,
REPORT_PAGE_PROTECTION_VERIFICATION = 30,
REPORT_PATTERN_SCAN_FAILURE = 40,
REPORT_NMI_CALLBACK_FAILURE = 50,
REPORT_MODULE_VALIDATION_FAILURE = 60,
REPORT_ILLEGAL_HANDLE_OPERATION = 70,
REPORT_INVALID_PROCESS_ALLOCATION = 80,
REPORT_HIDDEN_SYSTEM_THREAD = 90,
REPORT_ILLEGAL_ATTACH_PROCESS = 100
}
public Message(TcpClient client, byte[] buffer, int bufferSize, ILogger logger)
2023-09-02 15:47:15 +02:00
{
_tcpClient = client;
_networkStream = client.GetStream();
2023-09-02 15:47:15 +02:00
_buffer = buffer;
_bufferSize = bufferSize;
2023-09-02 17:56:46 +02:00
_logger = logger;
2023-09-07 09:21:00 +02:00
_header = this.GetMessageHeader();
2023-09-02 15:47:15 +02:00
2023-09-07 19:49:36 +02:00
switch (_header.message_type)
2023-09-02 15:47:15 +02:00
{
2023-09-08 20:41:11 +02:00
case (int)MESSAGE_TYPE.MESSAGE_TYPE_CLIENT_REPORT:
int reportId = GetPacketRequestId().RequestId;
2023-09-07 09:21:00 +02:00
this.HandleReportMessage(reportId);
2023-09-02 15:47:15 +02:00
break;
2023-09-08 20:41:11 +02:00
case (int)MESSAGE_TYPE.MESSAGE_TYPE_CLIENT_SEND:
int requestId = GetPacketRequestId().RequestId;
this.HandleClientSendMessage(requestId);
break;
2023-09-02 15:47:15 +02:00
default:
2023-09-02 17:56:46 +02:00
_logger.Information("This message type is not accepted at the moment.");
2023-09-02 15:47:15 +02:00
break;
}
}
2023-09-08 20:41:11 +02:00
2023-09-07 09:21:00 +02:00
private PACKET_HEADER GetMessageHeader()
2023-09-02 15:47:15 +02:00
{
2023-09-10 18:30:46 +02:00
return Helper.BytesToStructure<PACKET_HEADER>(_buffer, 0);
2023-09-02 15:47:15 +02:00
}
2023-09-08 20:41:11 +02:00
unsafe private PACKET_REQUEST_HEADER GetPacketRequestId()
{
2023-09-10 18:30:46 +02:00
return Helper.BytesToStructure<PACKET_REQUEST_HEADER>(_buffer, sizeof(PACKET_HEADER));
2023-09-08 20:41:11 +02:00
}
unsafe private CLIENT_SEND_PACKET_HEADER GetClientSendPacketHeader()
2023-09-02 15:47:15 +02:00
{
2023-09-10 18:30:46 +02:00
return Helper.BytesToStructure<CLIENT_SEND_PACKET_HEADER>(_buffer, sizeof(PACKET_HEADER));
2023-09-02 15:47:15 +02:00
}
2023-09-07 09:21:00 +02:00
unsafe private void HandleReportMessage(int reportId)
2023-09-08 21:06:13 +02:00
{
2023-09-10 19:32:12 +02:00
switch (reportId)
{
case (int)ReportCodes.REPORT_ILLEGAL_HANDLE_OPERATION:
_logger.Information("REPORT_ILLEGAL_HANDLE_OPERATION");
break;
case (int)ReportCodes.REPORT_CODE_MODULE_VERIFICATION:
_logger.Information("REPORT_CODE_MODULE_VERIFICATION");
break;
case (int)ReportCodes.REPORT_NMI_CALLBACK_FAILURE:
_logger.Information("REPORT_NMI_CALLBACK_FAILURE");
break;
case (int)ReportCodes.REPORT_MODULE_VALIDATION_FAILURE:
_logger.Information("REPORT_MODULE_VALIDATION_FAILURE");
break;
default:
_logger.Information("Report code not handled yet");
break;
}
/* OPEN_HANDLE_FAILURE_REPORT openHandleFailure =
Helper.BytesToStructure<OPEN_HANDLE_FAILURE_REPORT>(_buffer, sizeof(PACKET_HEADER));*/
2023-09-02 17:56:46 +02:00
2023-09-10 19:32:12 +02:00
/* _logger.Information("Report code: {0}, ProcessID: {1:x}, ThreadId: {2:x}, DesiredAccess{3:x}",
2023-09-02 17:56:46 +02:00
openHandleFailure.ReportCode,
openHandleFailure.ProcessId,
openHandleFailure.ThreadId,
2023-09-10 19:32:12 +02:00
openHandleFailure.DesiredAccess);*/
2023-09-10 18:30:46 +02:00
BuildReportResponseMessage(1);
}
private void BuildReportResponseMessage(int success)
{
REPORT_RESPONSE response = new REPORT_RESPONSE();
response.success = 1;
byte[] responseBytes = Helper.StructureToBytes<REPORT_RESPONSE>(ref response);
_networkStream.Write(responseBytes, 0, responseBytes.Length);
2023-09-02 15:47:15 +02:00
}
2023-09-08 20:41:11 +02:00
private void HandleClientSendMessage(int clientSendId)
{
CLIENT_SEND_PACKET_HEADER header = GetClientSendPacketHeader();
switch (header.RequestId)
{
case (int)CLIENT_SEND_REQUEST_ID.SYSTEM_INFORMATION:
2023-09-08 21:06:13 +02:00
this.HandleClientSendHardwareInformation(header);
2023-09-08 20:41:11 +02:00
break;
}
}
2023-09-08 21:06:13 +02:00
unsafe private void HandleClientSendHardwareInformation(CLIENT_SEND_PACKET_HEADER sendPacketHeader)
2023-09-08 20:41:11 +02:00
{
_logger.Information("Handling client send hardware information");
2023-09-10 18:30:46 +02:00
PACKET_CLIENT_HARDWARE_INFORMATION info = Helper.BytesToStructure<PACKET_CLIENT_HARDWARE_INFORMATION>(_buffer, sizeof(PACKET_HEADER) + sizeof(CLIENT_SEND_PACKET_HEADER));
2023-09-08 21:06:13 +02:00
2023-09-10 18:30:46 +02:00
_logger.Information("SteamId: {0}, Mobo Serial: {1}, drive serial: {2}", _header.steam64_id, info.MotherboardSerialNumber, info.DeviceDriver0Serial);
2023-09-08 21:06:13 +02:00
2023-09-09 19:26:19 +02:00
using (var context = new ModelContext())
{
context.Database.EnsureCreated();
2023-09-10 13:12:38 +02:00
var user = new UserEntity(context)
2023-09-09 19:26:19 +02:00
{
2023-09-10 13:12:38 +02:00
Steam64Id = _header.steam64_id
};
2023-09-10 13:12:38 +02:00
if (!user.CheckIfUserExists())
{
_logger.Information("Creating new user");
user.InsertUser();
}
else if (user.CheckIfUserIsBanned())
{
_logger.Information("User is banned");
BuildSystemVerificationResponseHeader(0, sendPacketHeader.RequestId, (int)USER_BAN_REASONS.USER_BAN);
return;
}
var hardwareConfiguration = new HardwareConfigurationEntity(context)
2023-09-09 19:26:19 +02:00
{
2023-09-10 18:30:46 +02:00
DeviceDrive0Serial = info.DeviceDriver0Serial,
MotherboardSerial = info.MotherboardSerialNumber,
2023-09-10 13:12:38 +02:00
User = user
};
2023-09-09 19:26:19 +02:00
2023-09-10 13:12:38 +02:00
if (hardwareConfiguration.CheckIfHardwareIsBanned())
{
_logger.Information("Users hardware is banned");
BuildSystemVerificationResponseHeader(0, sendPacketHeader.RequestId, (int)USER_BAN_REASONS.HARDWARE_BAN);
return;
}
2023-09-09 19:26:19 +02:00
2023-09-10 13:12:38 +02:00
if (hardwareConfiguration.CheckIfHardwareExists())
{
_logger.Information("User hardware already exists");
BuildSystemVerificationResponseHeader(1, sendPacketHeader.RequestId, 0);
return;
}
2023-09-10 13:12:38 +02:00
hardwareConfiguration.InsertHardwareConfiguration();
BuildSystemVerificationResponseHeader(1, sendPacketHeader.RequestId, 0);
context.SaveChanges();
2023-09-09 19:26:19 +02:00
}
}
private void BuildSystemVerificationResponseHeader(int canUserProceed, int requestId, int reason)
{
SYSTEM_INFORMATION_REQUEST_RESPONSE response = new SYSTEM_INFORMATION_REQUEST_RESPONSE();
response.CanUserProceed = canUserProceed;
response.RequestId = requestId;
response.reason = reason;
byte[] responseBytes = Helper.StructureToBytes<SYSTEM_INFORMATION_REQUEST_RESPONSE>(ref response);
2023-09-08 21:06:13 +02:00
2023-09-10 18:30:46 +02:00
_networkStream.Write(responseBytes, 0, responseBytes.Length);
2023-09-08 20:41:11 +02:00
}
2023-09-02 15:47:15 +02:00
}
}
2023-09-08 20:41:11 +02:00