continuity/Server/AuthSession.cs

74 lines
2.6 KiB
C#
Raw Normal View History

using System.Security.Cryptography;
2023-08-09 14:23:41 +00:00
using System.Text;
using MassTransit.Mediator;
using Microsoft.Extensions.Logging;
using NetCoreServer;
namespace Server;
public abstract class AuthSession : TcpSession
2023-08-09 14:23:41 +00:00
{
private readonly ILogger<AuthSession> _logger;
2023-08-09 14:23:41 +00:00
private readonly IMediator _mediator;
protected AuthSession(TcpServer
server, ILogger<AuthSession> logger, IMediator mediator) : base(server)
2023-08-09 14:23:41 +00:00
{
_logger = logger;
_mediator = mediator;
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
2023-08-10 06:45:51 +00:00
Console.WriteLine($"Length: {size} & offset: {offset}");
//Console.WriteLine(BitConverter.ToString(buffer.ToArray()).Replace("-", string.Empty));
Span<byte> decryptedBuffer = new byte[size];
2023-08-09 14:23:41 +00:00
2023-08-10 06:45:51 +00:00
// xor every value after the first 8 bytes
// then apply AES decryption
var dataBuffer = Decrypt(new ArraySegment<byte>(buffer, 8, (int)size - 8).ToArray());
/*using (var ms = new MemoryStream(dataBuffer))
using (var cs = new CryptoStream(ms, _decryptor, CryptoStreamMode.Read))
2023-08-09 14:23:41 +00:00
{
2023-08-10 06:45:51 +00:00
cs.Read(decryptedBuffer);
}*/
Console.WriteLine("Length " + BitConverter.ToUInt16(buffer, 0));
var opCode = BitConverter.ToUInt16(buffer.ToArray(), 2);
Console.WriteLine("Packet Op Code: " + opCode);
Console.WriteLine("Some Value: " + buffer[4]);
/*
* 20s = 5
* 15s = 4
* 10s = 3
*
* client alive time * 5s => uptime
*/
var clientAliveTime = BitConverter.ToUInt16(buffer.ToArray(), 5);
Console.WriteLine("Client Alive time: " + clientAliveTime);
Console.WriteLine("Might be a flag:" + buffer[7]);
Console.WriteLine("username: " + Encoding.ASCII.GetString(dataBuffer.ToArray(), 0, 20));
Console.WriteLine("password: " + Encoding.ASCII.GetString(dataBuffer.ToArray(), 20, 32));
Console.WriteLine("Full buffer: " + Encoding.ASCII.GetString(dataBuffer.ToArray()));
RawPacket rawPacket = new RawPacket((OperationCode)opCode, dataBuffer, clientAliveTime, buffer[0],
buffer[3],
Id, this);
Parallel.Invoke(() => _mediator.Send(rawPacket));
_logger.LogInformation("Connection from: {@RemoteEndpoint}", Socket.RemoteEndPoint?.ToString());
base.OnReceived(decryptedBuffer.ToArray(), offset, decryptedBuffer.Length);
2023-08-09 14:23:41 +00:00
}
private byte[] Decrypt(byte[] buffer)
{
for (int i = 0; i < buffer.Length; ++i)
{
buffer[i] = (byte)(buffer[i] ^ i ^ (3 * (0xFE - i)));
}
return buffer;
}
}