mirror-ac/server/Message/ClientReport.cs

598 lines
21 KiB
C#
Raw Normal View History

2023-09-11 16:14:21 +02:00
using Serilog;
2023-09-11 19:39:00 +02:00
using server.Database.Entity;
using server.Database.Entity.Report;
2023-09-21 16:01:47 +02:00
using server.Database.Entity.Report.Types;
2023-09-11 19:39:00 +02:00
using server.Database.Model;
2023-09-11 16:14:21 +02:00
using server.Types.ClientReport;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
2023-09-23 09:22:43 +02:00
using System.Runtime.InteropServices;
2023-09-11 16:14:21 +02:00
using System.Text;
using System.Threading.Tasks;
using static server.Message.MessageHandler;
namespace server.Message
{
public class ClientReport : IClientMessage
{
private readonly ILogger _logger;
private byte[] _buffer;
private int _bufferSize;
2023-09-24 05:10:13 +02:00
private int _bytesRead;
2023-09-11 16:14:21 +02:00
private PACKET_HEADER _packetHeader;
2023-09-24 05:10:13 +02:00
CLIENT_REPORT_PACKET_HEADER _currentReportHeader;
2023-09-11 16:14:21 +02:00
private CLIENT_REPORT_PACKET_RESPONSE _responsePacket;
private enum CLIENT_SEND_REPORT_ID
{
2023-09-21 16:01:47 +02:00
PROCESS_MODULE_VERIFICATION = 10,
2023-09-12 17:14:23 +02:00
START_ADDRESS_VERIFICATION = 20,
PAGE_PROTECTION_VERIFICATION = 30,
PATTERN_SCAN_FAILURE = 40,
NMI_CALLBACK_FAILURE = 50,
MODULE_VALIDATION_FAILURE = 60,
ILLEGAL_HANDLE_OPERATION = 70,
INVALID_PROCESS_ALLOCATION = 80,
HIDDEN_SYSTEM_THREAD = 90,
ILLEGAL_ATTACH_PROCESS = 100
2023-09-11 16:14:21 +02:00
}
private struct CLIENT_REPORT_PACKET_HEADER
{
public int reportCode;
}
private struct CLIENT_REPORT_PACKET_RESPONSE
{
public int success;
}
2023-09-11 17:34:44 +02:00
public ClientReport(ILogger logger, ref byte[] buffer, int bufferSize, PACKET_HEADER packetHeader)
2023-09-11 16:14:21 +02:00
{
this._logger = logger;
this._buffer = buffer;
this._bufferSize = bufferSize;
this._packetHeader = packetHeader;
2023-09-24 05:10:13 +02:00
this._bytesRead = 0;
2023-09-11 16:14:21 +02:00
this._responsePacket = new CLIENT_REPORT_PACKET_RESPONSE();
2023-09-24 05:46:33 +02:00
this.GetPacketHeader();
2023-09-23 09:22:43 +02:00
_logger.Information("buffer size: {0}", bufferSize);
2023-09-11 16:14:21 +02:00
}
unsafe public void GetPacketHeader()
{
2023-09-24 05:10:13 +02:00
this._currentReportHeader =
2023-09-24 05:46:33 +02:00
Helper.BytesToStructure<CLIENT_REPORT_PACKET_HEADER>(this._buffer, Marshal.SizeOf(typeof(PACKET_HEADER)) + this._bytesRead);
2023-09-11 16:14:21 +02:00
}
public byte[] GetResponsePacket()
{
return Helper.StructureToBytes<CLIENT_REPORT_PACKET_RESPONSE>(ref this._responsePacket);
}
2023-09-11 17:34:44 +02:00
private void SetResponsePacketData(int success)
{
this._responsePacket.success = success;
}
2023-09-24 05:10:13 +02:00
unsafe public bool HandleMessage()
2023-09-23 09:22:43 +02:00
{
2023-09-24 05:10:13 +02:00
if (this._currentReportHeader.reportCode == 0)
2023-09-23 09:22:43 +02:00
{
_logger.Error("Failed to get the report packet code");
2023-09-24 13:13:20 +02:00
SetResponsePacketData(1);
2023-09-23 09:22:43 +02:00
return false;
}
2023-09-24 05:46:33 +02:00
while (this._bytesRead < this._bufferSize)
2023-09-23 09:22:43 +02:00
{
2023-09-24 05:10:13 +02:00
this.GetPacketHeader();
2023-09-24 13:13:20 +02:00
_logger.Information("Report code: {0}", this._currentReportHeader.reportCode);
2023-09-24 05:10:13 +02:00
switch (this._currentReportHeader.reportCode)
2023-09-23 09:22:43 +02:00
{
case (int)CLIENT_SEND_REPORT_ID.PROCESS_MODULE_VERIFICATION:
_logger.Information("REPORT CODE: MODULE_VERIFICATION");
break;
case (int)CLIENT_SEND_REPORT_ID.START_ADDRESS_VERIFICATION:
_logger.Information("REPORT CODE: START_ADDRESS_VERIFICATION");
2023-09-24 05:10:13 +02:00
HandleReportStartAddressVerification(this._bytesRead);
2023-09-24 05:46:33 +02:00
this._bytesRead += Marshal.SizeOf(typeof(PROCESS_THREAD_START_FAILURE)) +
Marshal.SizeOf(typeof(PACKET_HEADER));
2023-09-24 05:10:13 +02:00
2023-09-23 09:22:43 +02:00
break;
case (int)CLIENT_SEND_REPORT_ID.PAGE_PROTECTION_VERIFICATION:
_logger.Information("REPORT CODE: PAGE_PROTECTION_VERIFICATION");
2023-09-24 05:46:33 +02:00
HandleReportPageProtection(this._bytesRead);
2023-09-24 05:10:13 +02:00
2023-09-24 05:46:33 +02:00
this._bytesRead += Marshal.SizeOf(typeof(PAGE_PROTECTION_FAILURE)) +
Marshal.SizeOf(typeof(PACKET_HEADER));
2023-09-24 05:10:13 +02:00
2023-09-23 09:22:43 +02:00
break;
case (int)CLIENT_SEND_REPORT_ID.PATTERN_SCAN_FAILURE:
_logger.Information("REPORT_PATTERN_SCAN_FAILURE");
2023-09-24 05:46:33 +02:00
HandleReportPatternScan(this._bytesRead);
this._bytesRead += Marshal.SizeOf(typeof(PATTERN_SCAN_FAILURE)) +
Marshal.SizeOf(typeof(PACKET_HEADER));
2023-09-24 05:10:13 +02:00
2023-09-23 09:22:43 +02:00
break;
case (int)CLIENT_SEND_REPORT_ID.NMI_CALLBACK_FAILURE:
_logger.Information("REPORT_NMI_CALLBACK_FAILURE");
2023-09-24 05:46:33 +02:00
HandleReportNmiCallback(this._bytesRead);
this._bytesRead += Marshal.SizeOf(typeof(NMI_CALLBACK_FAILURE)) +
Marshal.SizeOf(typeof(PACKET_HEADER));
2023-09-24 05:10:13 +02:00
2023-09-23 09:22:43 +02:00
break;
case (int)CLIENT_SEND_REPORT_ID.MODULE_VALIDATION_FAILURE:
_logger.Information("REPORT_MODULE_VALIDATION_FAILURE");
2023-09-24 05:46:33 +02:00
HandleReportSystemModuleValidation(this._bytesRead);
2023-09-24 05:10:13 +02:00
2023-09-24 05:46:33 +02:00
this._bytesRead += Marshal.SizeOf(typeof(MODULE_VALIDATION_FAILURE)) +
Marshal.SizeOf(typeof(PACKET_HEADER));
2023-09-24 05:10:13 +02:00
2023-09-23 09:22:43 +02:00
break;
case (int)CLIENT_SEND_REPORT_ID.ILLEGAL_HANDLE_OPERATION:
_logger.Information("REPORT_ILLEGAL_HANDLE_OPERATION");
2023-09-24 05:46:33 +02:00
HandleReportIllegalHandleOperation(this._bytesRead);
2023-09-24 05:10:13 +02:00
2023-09-24 05:46:33 +02:00
this._bytesRead += Marshal.SizeOf(typeof(OPEN_HANDLE_FAILURE)) +
Marshal.SizeOf(typeof(PACKET_HEADER));
2023-09-24 05:10:13 +02:00
2023-09-23 09:22:43 +02:00
break;
case (int)CLIENT_SEND_REPORT_ID.INVALID_PROCESS_ALLOCATION:
_logger.Information("REPORT_INVALID_PROCESS_ALLOCATION");
2023-09-24 05:46:33 +02:00
HandleReportInvalidProcessAllocation(this._bytesRead);
2023-09-24 05:10:13 +02:00
2023-09-24 05:46:33 +02:00
this._bytesRead += Marshal.SizeOf(typeof(INVALID_PROCESS_ALLOCATION_FAILURE)) +
Marshal.SizeOf(typeof(PACKET_HEADER));
2023-09-24 05:10:13 +02:00
2023-09-23 09:22:43 +02:00
break;
case (int)CLIENT_SEND_REPORT_ID.HIDDEN_SYSTEM_THREAD:
_logger.Information("REPORT_HIDDEN_SYSTEM_THREAD");
2023-09-24 05:46:33 +02:00
HandleReportHiddenSystemThread(this._bytesRead);
2023-09-24 05:10:13 +02:00
2023-09-24 05:46:33 +02:00
this._bytesRead += Marshal.SizeOf(typeof(HIDDEN_SYSTEM_THREAD_FAILURE)) +
Marshal.SizeOf(typeof(PACKET_HEADER));
2023-09-24 05:10:13 +02:00
2023-09-23 09:22:43 +02:00
break;
case (int)CLIENT_SEND_REPORT_ID.ILLEGAL_ATTACH_PROCESS:
_logger.Information("REPORT_ILLEGAL_ATTACH_PROCESS");
2023-09-24 05:46:33 +02:00
HandleReportAttachProcess(this._bytesRead);
this._bytesRead += Marshal.SizeOf(typeof(ATTACH_PROCESS_FAILURE)) +
Marshal.SizeOf(typeof(PACKET_HEADER));
2023-09-24 05:10:13 +02:00
2023-09-23 09:22:43 +02:00
break;
default:
_logger.Information("Report code not handled yet");
2023-09-24 05:46:33 +02:00
SetResponsePacketData(0);
return false;
2023-09-23 09:22:43 +02:00
}
2023-09-11 16:14:21 +02:00
}
2023-09-11 17:34:44 +02:00
SetResponsePacketData(1);
2023-09-11 16:14:21 +02:00
return true;
}
2023-09-11 19:39:00 +02:00
2023-09-23 09:22:43 +02:00
unsafe public void HandleReportIllegalHandleOperation(int offset)
2023-09-11 19:39:00 +02:00
{
2023-09-22 14:29:51 +02:00
OPEN_HANDLE_FAILURE report =
2023-09-23 09:22:43 +02:00
Helper.BytesToStructure<OPEN_HANDLE_FAILURE>(_buffer, sizeof(PACKET_HEADER) + offset);
2023-09-11 19:39:00 +02:00
2023-09-23 14:36:46 +02:00
if (report.IsKernelHandle == 0 &&
2023-09-23 11:20:38 +02:00
report.ProcessId == 0 &&
2023-09-23 14:36:46 +02:00
report.DesiredAccess == 0)
2023-09-23 10:55:42 +02:00
{
return;
}
2023-09-23 14:36:46 +02:00
2023-09-11 19:39:00 +02:00
_logger.Information("ProcessName: {0}, ProcessID: {1:x}, ThreadId: {2:x}, DesiredAccess{3:x}",
report.ProcessName,
report.ProcessId,
report.ThreadId,
report.DesiredAccess);
using (var context = new ModelContext())
{
/*
* This doesn't seem to be the most optimal way to do this, but it works..
* Maybe look into it further at somepoint..
*/
UserEntity user = new UserEntity(context);
2023-09-21 16:01:47 +02:00
var newReport = new ReportEntity(context)
2023-09-11 19:39:00 +02:00
{
User = user.GetUserBySteamId(this._packetHeader.steam64_id),
2023-09-21 16:01:47 +02:00
ReportCode = (int)CLIENT_SEND_REPORT_ID.ILLEGAL_HANDLE_OPERATION
};
newReport.InsertReport();
var reportTypeIllegalHandleOperation = new ReportTypeIllegalHandleOperationEntity(context)
{
Report = newReport,
2023-09-11 19:39:00 +02:00
IsKernelHandle = report.IsKernelHandle,
ProcessId = report.ProcessId,
ThreadId = report.ThreadId,
DesiredAccess = report.DesiredAccess,
ProcessName = report.ProcessName
};
2023-09-21 16:01:47 +02:00
reportTypeIllegalHandleOperation.InsertReport();
2023-09-11 19:39:00 +02:00
context.SaveChanges();
}
}
2023-09-22 14:29:51 +02:00
2023-09-23 09:22:43 +02:00
unsafe public void HandleReportStartAddressVerification(int offset)
2023-09-22 14:29:51 +02:00
{
PROCESS_THREAD_START_FAILURE report =
2023-09-23 09:22:43 +02:00
Helper.BytesToStructure<PROCESS_THREAD_START_FAILURE>(_buffer, sizeof(PACKET_HEADER) + offset);
2023-09-22 14:29:51 +02:00
2023-09-23 14:36:46 +02:00
if (report.ThreadId == 0 &&
report.StartAddress == 0)
{
return;
}
2023-09-22 14:29:51 +02:00
_logger.Information("ThreadId: {0}, ThreadStartAddress: {1:x}",
report.ThreadId,
report.StartAddress);
using (var context = new ModelContext())
{
UserEntity user = new UserEntity(context);
var newReport = new ReportEntity(context)
{
User = user.GetUserBySteamId(this._packetHeader.steam64_id),
ReportCode = (int)CLIENT_SEND_REPORT_ID.START_ADDRESS_VERIFICATION
};
newReport.InsertReport();
var reportTypeStartAddress = new StartAddressEntity(context)
{
Report = newReport,
ThreadId = report.ThreadId,
ThreadStartAddress = report.StartAddress
};
reportTypeStartAddress.InsertReport();
context.SaveChanges();
}
}
2023-09-23 09:22:43 +02:00
unsafe public void HandleReportPageProtection(int offset)
2023-09-22 14:29:51 +02:00
{
PAGE_PROTECTION_FAILURE report =
2023-09-23 09:22:43 +02:00
Helper.BytesToStructure<PAGE_PROTECTION_FAILURE>(_buffer, sizeof(PACKET_HEADER) + offset);
2023-09-22 14:29:51 +02:00
2023-09-23 14:36:46 +02:00
if (report.AllocationProtection == 0 &&
report.PageBaseAddress == 0 &&
report.AllocationState == 0 &&
report.AllocationType == 0 )
{
return;
}
2023-09-22 14:29:51 +02:00
_logger.Information("Page base address: {0:x}, allocation protection: {1:x}, allocation state: {2:x}, allocationtype: {3:x}",
report.PageBaseAddress,
report.AllocationProtection,
report.AllocationState,
report.AllocationType);
using (var context = new ModelContext())
{
UserEntity user = new UserEntity(context);
var newReport = new ReportEntity(context)
{
User = user.GetUserBySteamId(this._packetHeader.steam64_id),
ReportCode = (int)CLIENT_SEND_REPORT_ID.PAGE_PROTECTION_VERIFICATION
};
newReport.InsertReport();
var reportTypePageProtection = new PageProtectionEntity(context)
{
Report = newReport,
PageBaseAddress = report.PageBaseAddress,
AllocationProtection = report.AllocationProtection,
AllocationState = report.AllocationState,
AllocationType = report.AllocationType
};
reportTypePageProtection.InsertReport();
context.SaveChanges();
}
}
2023-09-23 09:22:43 +02:00
unsafe public void HandleReportPatternScan(int offset)
2023-09-22 14:29:51 +02:00
{
PATTERN_SCAN_FAILURE report =
2023-09-23 09:22:43 +02:00
Helper.BytesToStructure<PATTERN_SCAN_FAILURE>(_buffer, sizeof(PACKET_HEADER) + offset);
2023-09-22 14:29:51 +02:00
2023-09-23 14:36:46 +02:00
if (report.Address == 0 &&
report.SignatureId == 0)
{
return;
}
2023-09-22 14:29:51 +02:00
_logger.Information("signature id: {0}, address: {1:x}",
report.SignatureId,
report.Address);
using (var context = new ModelContext())
{
UserEntity user = new UserEntity(context);
var newReport = new ReportEntity(context)
{
User = user.GetUserBySteamId(this._packetHeader.steam64_id),
ReportCode = (int)CLIENT_SEND_REPORT_ID.PATTERN_SCAN_FAILURE
};
newReport.InsertReport();
var reportTypePatternScan = new PatternScanEntity(context)
{
Report = newReport,
SignatureId = report.SignatureId,
Address = report.Address
};
reportTypePatternScan.InsertReport();
context.SaveChanges();
}
}
2023-09-23 09:22:43 +02:00
unsafe public void HandleReportNmiCallback(int offset)
2023-09-22 14:29:51 +02:00
{
NMI_CALLBACK_FAILURE report =
2023-09-23 09:22:43 +02:00
Helper.BytesToStructure<NMI_CALLBACK_FAILURE>(_buffer, sizeof(PACKET_HEADER) + offset);
2023-09-22 14:29:51 +02:00
2023-09-23 14:36:46 +02:00
if (report.InvalidRip == 0 &&
report.WereNmisDisabled == 0 &&
report.KThreadAddress == 0)
{
return;
}
2023-09-22 14:29:51 +02:00
_logger.Information("were nmis disabled: {0}, kthread: {1:x}, invalid rip: {2:x}",
report.WereNmisDisabled,
report.KThreadAddress,
report.InvalidRip);
using (var context = new ModelContext())
{
UserEntity user = new UserEntity(context);
var newReport = new ReportEntity(context)
{
User = user.GetUserBySteamId(this._packetHeader.steam64_id),
ReportCode = (int)CLIENT_SEND_REPORT_ID.NMI_CALLBACK_FAILURE
};
newReport.InsertReport();
var reportTypeNmiCallback = new NmiCallbackEntity(context)
{
Report = newReport,
WereNmisDisabled = report.WereNmisDisabled,
KThreadAddress = report.KThreadAddress,
InvalidRip = report.InvalidRip
};
reportTypeNmiCallback.InsertReport();
context.SaveChanges();
}
}
2023-09-23 09:22:43 +02:00
unsafe public void HandleReportSystemModuleValidation(int offset)
2023-09-22 14:29:51 +02:00
{
MODULE_VALIDATION_FAILURE report =
2023-09-23 09:22:43 +02:00
Helper.BytesToStructure<MODULE_VALIDATION_FAILURE>(_buffer, sizeof(PACKET_HEADER) + offset);
2023-09-22 14:29:51 +02:00
2023-09-23 14:36:46 +02:00
if (report.ReportType == 0 &&
report.ReportCode == 0 &&
report.DriverSize == 0 &&
report.DriverBaseAddress == 0)
{
return;
}
2023-09-22 14:29:51 +02:00
_logger.Information("report type: {0}, driver base: {1:x}, size: {2}, module name: {3}",
report.ReportType,
report.DriverBaseAddress,
report.DriverSize,
report.ModuleName);
using (var context = new ModelContext())
{
UserEntity user = new UserEntity(context);
var newReport = new ReportEntity(context)
{
User = user.GetUserBySteamId(this._packetHeader.steam64_id),
ReportCode = (int)CLIENT_SEND_REPORT_ID.MODULE_VALIDATION_FAILURE
};
newReport.InsertReport();
var reportTypeSystemModuleValidation = new SystemModuleValidationEntity(context)
{
Report = newReport,
ReportType = report.ReportType,
DriverBaseAddress = report.DriverBaseAddress,
DriverSize = report.DriverSize,
ModuleName = report.ModuleName
};
reportTypeSystemModuleValidation.InsertReport();
context.SaveChanges();
}
}
2023-09-23 09:22:43 +02:00
unsafe public void HandleReportHiddenSystemThread(int offset)
2023-09-22 14:29:51 +02:00
{
HIDDEN_SYSTEM_THREAD_FAILURE report =
2023-09-23 09:22:43 +02:00
Helper.BytesToStructure<HIDDEN_SYSTEM_THREAD_FAILURE>(_buffer, sizeof(PACKET_HEADER) + offset);
2023-09-22 14:29:51 +02:00
2023-09-23 14:36:46 +02:00
if (report.FoundInPspCidTable == 0 &&
report.FoundInKThreadList == 0 &&
report.ThreadId == 0 &&
report.ThreadAddress == 0)
{
return;
}
2023-09-22 14:29:51 +02:00
_logger.Information("found in kthread list: {0}, found in pspcidtable: {1}, thread address: {2:x}, thread id: {3:x}",
report.FoundInKThreadList,
report.FoundInPspCidTable,
report.ThreadAddress,
report.ThreadId);
using (var context = new ModelContext())
{
UserEntity user = new UserEntity(context);
var newReport = new ReportEntity(context)
{
User = user.GetUserBySteamId(this._packetHeader.steam64_id),
ReportCode = (int)CLIENT_SEND_REPORT_ID.HIDDEN_SYSTEM_THREAD
};
newReport.InsertReport();
var reportTypeHiddenSystemThread = new HiddenSystemThreadEntity(context)
{
Report = newReport,
FoundInKThreadList = report.FoundInKThreadList,
FoundInPspCidTable = report.FoundInPspCidTable,
ThreadAddress = report.ThreadAddress,
2023-09-23 14:27:02 +02:00
ThreadId = report.ThreadId,
ThreadStructure = report.ThreadStructure
2023-09-22 14:29:51 +02:00
};
reportTypeHiddenSystemThread.InsertReport();
context.SaveChanges();
}
}
2023-09-23 09:22:43 +02:00
unsafe public void HandleReportAttachProcess(int offset)
2023-09-22 14:29:51 +02:00
{
ATTACH_PROCESS_FAILURE report =
2023-09-23 09:22:43 +02:00
Helper.BytesToStructure<ATTACH_PROCESS_FAILURE>(_buffer, sizeof(PACKET_HEADER) + offset);
2023-09-22 14:29:51 +02:00
2023-09-23 14:36:46 +02:00
if (report.ThreadAddress == 0 &&
report.ThreadId == 0)
{
return;
}
2023-09-22 14:29:51 +02:00
_logger.Information("thread id: {0:x}, thread address: {1:x}",
report.ThreadId,
report.ThreadAddress);
using (var context = new ModelContext())
{
UserEntity user = new UserEntity(context);
var newReport = new ReportEntity(context)
{
User = user.GetUserBySteamId(this._packetHeader.steam64_id),
ReportCode = (int)CLIENT_SEND_REPORT_ID.ILLEGAL_ATTACH_PROCESS
};
newReport.InsertReport();
var reportTypeAttachProcess = new AttachProcessEntity(context)
{
Report = newReport,
ThreadId = report.ThreadId,
ThreadAddress = report.ThreadAddress,
};
reportTypeAttachProcess.InsertReport();
context.SaveChanges();
}
}
2023-09-24 05:46:33 +02:00
unsafe public void HandleReportInvalidProcessAllocation(int offset)
2023-09-22 14:29:51 +02:00
{
2023-09-23 14:27:02 +02:00
INVALID_PROCESS_ALLOCATION_FAILURE report =
Helper.BytesToStructure<INVALID_PROCESS_ALLOCATION_FAILURE>(_buffer, sizeof(PACKET_HEADER) + offset);
2023-09-23 10:55:42 +02:00
2023-09-22 14:29:51 +02:00
_logger.Information("received invalid process allocation structure");
using (var context = new ModelContext())
{
UserEntity user = new UserEntity(context);
var newReport = new ReportEntity(context)
{
User = user.GetUserBySteamId(this._packetHeader.steam64_id),
ReportCode = (int)CLIENT_SEND_REPORT_ID.INVALID_PROCESS_ALLOCATION
};
newReport.InsertReport();
var reportTypeInvalidProcessAllocation = new InvalidProcessAllocationEntity(context)
{
Report = newReport,
2023-09-23 14:27:02 +02:00
ProcessStructure = report.ProcessStructure
2023-09-22 14:29:51 +02:00
};
reportTypeInvalidProcessAllocation.InsertReport();
context.SaveChanges();
}
}
2023-09-11 16:14:21 +02:00
}
}